-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSUNFRONT.C
315 lines (293 loc) · 7.18 KB
/
SUNFRONT.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#ifndef lint
static char *sccsid = "@(#)sunfront.c 2.1 (Chris & John Downey) 7/29/92";
#endif
/***
* program name:
xvi
* function:
PD version of UNIX "vi" editor, with extensions.
* module name:
sunfront.c
* module function:
Terminal interface module for SunView: front end program.
* history:
STEVIE - ST Editor for VI Enthusiasts, Version 3.10
Originally by Tim Thompson (twitch!tjt)
Extensive modifications by Tony Andrews (onecom!wldrdg!tony)
Heavily modified by Chris & John Downey
***/
#include "xvi.h"
#include <suntool/sunview.h>
#include <suntool/panel.h>
#include <suntool/icon.h>
#include <suntool/canvas.h>
#include <suntool/tty.h>
#include <sys/filio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <vfork.h>
static short icon_image[] =
{
# include "xvi.icn"
};
mpr_static(iconpr, 64, 64, 1, icon_image);
static Frame frame;
static Tty xviwin;
/*
* Handle keyboard or mouse input.
*/
static Notify_value
ttyproc(win, event, na, type)
Tty win;
Event *event;
Notify_arg na;
Notify_event_type type;
{
static Pixfont *deffont = (Pixfont *) 0; /* default font */
register int evtcode;
static char seqbuf[(sizeof (unsigned int) * 12) + 7] =
{ PREFIXCHAR };
int nchars;
bool_t done;
static unsigned buttonstate, prevx, prevy;
unsigned mx, my;
#define BUTTONMASK(e) (1 << ((e) - BUT(1)))
if (deffont == NULL) {
/*
* Get default font.
*/
deffont = pf_default();
}
evtcode = event_action(event);
mx = event_x(event) / deffont->pf_defaultsize.x;
my = event_y(event) / deffont->pf_defaultsize.y;
nchars = 0;
done = FALSE;
if (evtcode == LOC_DRAG && buttonstate == BUTTONMASK(MS_MIDDLE)) {
if (mx != prevx || my != prevy) {
/*
* Mouse drag event. Send a PREFIXCHAR,
* followed by 'm', followed by the starting
* row, finishing row, starting column &
* finishing column, in that order.
*/
sprintf(&seqbuf[1], "m%x;%x;%x;%x;", prevy, my, prevx, mx);
nchars = strlen(seqbuf);
done = TRUE;
prevx = mx;
prevy = my;
}
} else if (event_is_down(event)) {
if (event_is_button(event)) {
switch (evtcode) {
case MS_MIDDLE:
prevx = mx;
prevy = my;
break;
case MS_RIGHT:
/*
* Right button pressed. We have to
* send a PREFIXCHAR, followed by 'p',
* followed by the position of the
* mouse cursor in character
* co-ordinates: y first, then x.
*/
sprintf(&seqbuf[1], "p%x;%x;", my, mx);
nchars = strlen(seqbuf);
done = TRUE;
}
buttonstate |= BUTTONMASK(evtcode);
} else {
/*
* nchars is the number of characters we have
* to send to xvi.main. In most of the cases
* we have to deal with here, this will be 2.
*/
nchars = 2;
done = TRUE;
switch (evtcode) {
case PREFIXCHAR:
seqbuf[1] = PREFIXCHAR;
break;
case KEY_RIGHT(7):
seqbuf[1] = 'H';
break;
case KEY_RIGHT(8):
seqbuf[1] = 'k';
break;
case KEY_RIGHT(9):
seqbuf[1] = CTRL('B');
break;
case KEY_RIGHT(10):
seqbuf[1] = '\b';
break;
case KEY_RIGHT(12):
seqbuf[1] = ' ';
break;
case KEY_RIGHT(13):
seqbuf[1] = 'L';
break;
case KEY_RIGHT(14):
seqbuf[1] = 'j';
break;
case KEY_RIGHT(15):
seqbuf[1] = CTRL('F');
break;
default:
nchars = 0;
done = FALSE;
}
}
} else if (event_is_up(event) && event_is_button(event)) {
if (evtcode == MS_RIGHT)
done = TRUE;
buttonstate &= ~BUTTONMASK(evtcode);
}
if (nchars > 0) {
ttysw_input(xviwin, seqbuf, nchars);
}
return done ?
NOTIFY_DONE :
notify_next_event_func(win, event, na, type);
}
/*
* Read messages coming from back-end process.
*/
static Notify_value
readsocket(client, fd)
Notify_client client;
int fd;
{
char c;
while (read(fd, &c, 1) == 1) {
if (c == 'q') {
(void) notify_set_input_func(client, NOTIFY_FUNC_NULL, fd);
window_set(frame, FRAME_NO_CONFIRM, TRUE, 0);
window_destroy(frame);
return NOTIFY_DONE;
}
}
return NOTIFY_DONE;
}
static Notify_value
sigign(client, signum, when)
Notify_client client;
int signum;
Notify_signal_mode when;
{
return NOTIFY_IGNORED;
}
/*
* Start up our back-end process and connect its standard input,
* output & error files to the tty subwindow we've created for it.
*
* We use a pair of connected stream sockets to communicate with it:
* it can reference its socket as file descriptor 3. Currently, this
* is only used by the back-end process to tell us when to exit (by
* sending the single character 'q').
*/
static void
forkmain(argv)
char **argv;
{
int winfd;
int commsock[2];
int savefd[4];
int nbflag;
int i;
char *progname;
for (i = 0; i <= 3; i++) {
while ((savefd[i] = dup(i)) <= 3) {
;
}
}
if (socketpair(AF_UNIX, SOCK_STREAM, 0, commsock) != 0) {
fprintf(stderr, "%s: can't create socket\n", argv[0]);
exit(1);
}
winfd = (int) window_get(xviwin, TTY_TTY_FD);
if ((progname = strdup(argv[0])) == NULL) {
progname = "xvi.sunview";
}
argv[0] = XVI_MAINPROG;
switch (vfork()) {
case 0: /* This is the child process. */
for (i = 0; i <= 2; i++) {
dup2(winfd, i);
}
dup2(commsock[1], 3);
ioctl(winfd, FIOCLEX, 0);
ioctl(commsock[0], FIOCLEX, 0);
ioctl(commsock[1], FIOCLEX, 0);
for (i = 0; i <= 3; i++) {
ioctl(savefd[i], FIOCLEX, 0);
}
execvp(argv[0], argv);
fprintf(stderr, "%s: can't execute %s\n", progname, argv[0]);
fflush(stderr);
_exit(1);
case -1:
fprintf(stderr, "%s: vfork() failed\n", progname);
fflush(stderr);
_exit(1);
default: /* This is the parent process. */
/*
* We should only reach this point after the
* child has called execvp() (or died).
*/
for (i = 0; i <= 3; i++) {
dup2(savefd[i], i);
close(savefd[i]);
}
close(commsock[1]);
/*
* commsock[0] is our end of the socketpair.
* We have to make it non-blocking & register
* an input handler for it.
*/
nbflag = 1;
ioctl(commsock[0], FIONBIO, &nbflag);
(void) notify_set_input_func((Notify_client) xviwin,
readsocket, commsock[0]);
}
}
main(argc, argv)
int argc;
char **argv;
{
Icon xvicon;
char *label;
if ((label = strrchr(argv[0], '/')) == NULL) {
label = argv[0];
} else {
label++;
}
xvicon = icon_create(ICON_IMAGE, &iconpr, 0);
frame = window_create(NULL, FRAME,
FRAME_LABEL, label,
FRAME_ICON, xvicon,
FRAME_ARGC_PTR_ARGV, &argc, argv,
WIN_ERROR_MSG, "Can't create window",
FRAME_NO_CONFIRM, TRUE,
FRAME_SUBWINDOWS_ADJUSTABLE,
FALSE,
0);
xviwin = window_create(frame, TTY,
TTY_ARGV, TTY_ARGV_DO_NOT_FORK,
WIN_CONSUME_KBD_EVENTS, WIN_RIGHT_KEYS,
0,
WIN_CONSUME_PICK_EVENTS, WIN_MOUSE_BUTTONS,
WIN_UP_EVENTS,
LOC_DRAG,
0,
0);
(void) notify_set_signal_func((Notify_client) xviwin, sigign,
SIGHUP, NOTIFY_ASYNC);
(void) notify_set_signal_func((Notify_client) xviwin, sigign,
SIGINT, NOTIFY_ASYNC);
(void) notify_set_signal_func((Notify_client) xviwin, sigign,
SIGQUIT, NOTIFY_ASYNC);
forkmain(argv);
notify_interpose_event_func(xviwin, ttyproc, NOTIFY_SAFE);
window_main_loop(frame);
}