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