1 static const char rcsid[] = "$Rxvt: grxlib.c,v 1.2 1998/04/20 07:26:20 mason Exp $";
2 
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <math.h>
8 
9 #ifdef _AIX
10 # include <termio.h>
11 #else
12 # include <termios.h>
13 #endif
14 
15 #include "grxlib.h"
16 
17 /*----------------------------------------------------------------------*/
18 
Done(void)19 void Done (void)		{ putchar (':'); }
StartLine(long id)20 void StartLine (long id)	{ printf ("\033GL%ld", id); }
StartPoint(long id)21 void StartPoint (long id)	{ printf ("\033GP%ld", id); }
StartFill(long id)22 void StartFill (long id)	{ printf ("\033GF%ld", id); }
Extend(int x,int y)23 void Extend (int x, int y)	{ printf (";%d;%d", x, y); }
FillArea(int x1,int y1,int x2,int y2)24 void FillArea (int x1, int y1, int x2, int y2)
25 {
26    printf (";%d;%d;%d;%d", x1, y1, x2, y2);
27 }
28 
PlaceText(long id,int x,int y,int mode,char * text)29 void PlaceText (long id, int x, int y, int mode, char *text)
30 {
31    printf ("\033GT%ld;%d;%d;%d;%d:%s", id, x, y, mode, strlen(text), text);
32    fflush (stdout);
33 }
34 
ClearWindow(long id)35 void ClearWindow (long id)	{ printf ("\033GC%ld:", id); }
ForeColor(int col)36 void ForeColor (int col)	{ printf ("\033[3%dm", (col<0||col>7)?0:col); }
DefaultRendition(void)37 void DefaultRendition (void)	{ printf ("\033[m"); }
38 
39 #define LINESZ	100
40 static char line [LINESZ];
41 static FILE *infd = NULL;
42 
43 long
CreateWin(int x,int y,int w,int h)44 CreateWin (int x, int y, int w, int h)
45 {
46    long id = 0;
47 
48    fflush (stdout);
49    printf ("\033GW%d;%d;%d;%d:", x, y, w, h);
50    fflush (stdout);
51    while (1)
52      {
53 	if ((fgets (line, LINESZ, infd) != NULL) &&
54 	    (sscanf (line,"\033W%ld", &id) == 1))
55 	  break;
56      }
57    return id;
58 }
59 
60 void
QueryWin(long id,int * nfwidth,int * nfheight)61 QueryWin (long id, int *nfwidth, int *nfheight)
62 {
63    int id1, x, y, width, height, fwidth, fheight;
64    printf ("\033GG%ld:",id);
65    fflush (stdout);
66    while (1)
67      {
68 	if ((fgets (line, sizeof(line), infd) != NULL) &&
69 	    (sscanf (line,"\033G%ld %ld %ld %ld %ld %ld %ld %ld %ld",
70 		     &id1, &x, &y, &width, &height,
71 		     &fwidth, &fheight, nfwidth, nfheight) != 0))
72 	  break;
73      }
74 }
75 
76 int
WaitForCarriageReturn(long * win,int * x,int * y)77 WaitForCarriageReturn (long *win, int *x, int *y)
78 {
79    int i, len;
80 
81    fgets (line, LINESZ, infd);
82    line [LINESZ-1] = 0;
83    len = strlen (line);
84    for (i = 0; i < len; i++)
85      {
86 	if (line [i] == '\033')
87 	  {
88 	     int ret = 1;
89 	     i++;
90 	     switch (line[i]) {
91 	      case 'R': ret++;
92 		/* drop */
93 	      case 'P':
94 		sscanf (&line[i+1],"%ld;%d;%d", win, x, y);
95 		return ret;
96 		break;
97 	     }
98 	  }
99      }
100    return 0;
101 }
102 
103 static int fno2;
104 static struct termios ttmode;
105 
106 int
InitializeGraphics(int scroll_text_up)107 InitializeGraphics (int scroll_text_up)
108 {
109    int fno, i;
110    char *screen_tty;
111    struct winsize winsize;
112 
113    fno = fileno (stdout);
114    if (!isatty (fno))
115      {
116 	fprintf (stderr, "stdout must be a tty\n");
117 	return 0;
118      }
119    screen_tty = ttyname (fno);
120    ioctl (fno, TCGETS, (char *)&ttmode);
121    ttmode.c_lflag &= ~ECHO;
122    ioctl (fno, TCSETS, (char *)&ttmode);
123 
124    infd = fopen (screen_tty, "rw");
125 
126    fno2 = fileno (infd);
127    ioctl (fno2, TCGETS, (char *)&ttmode);
128    ttmode.c_lflag &= ~ECHO;
129    ioctl (fno2, TCSETS, (char *)&ttmode);
130 
131    /* query rxvt to find if graphics are available */
132    fflush (stdout);
133    printf ("\033GQ");
134    fflush (stdout);
135    while (1)
136      {
137 	if ((fgets (line, LINESZ, infd) != NULL) &&
138 	    (sscanf (line,"\033G%d", &i) == 1))
139 	  {
140 	     if (!i)
141 	       {
142 		  fprintf (stderr, "rxvt graphics not available\n");
143 		  CloseGraphics ();
144 		  return 0;
145 	       }
146 	     break;
147 	  }
148      }
149    if (scroll_text_up)
150      {
151 	ioctl (fno, TIOCGWINSZ, &winsize);
152 	fflush (stdout);
153 	for (i = 0; i < winsize.ws_row; i++)
154 	  putchar ('\n');
155 	fflush (stdout);
156      }
157    return i;
158 }
159 
160 void
CloseGraphics(void)161 CloseGraphics (void)
162 {
163    DefaultRendition ();
164    fflush (stdout);
165    ttmode.c_lflag |= ECHO;
166    ioctl (fno2, TCSETS, (char *)&ttmode);
167    fclose (infd);
168 }
169 /*----------------------- end-of-file (C source) -----------------------*/
170