]> code.delx.au - gnu-emacs/blob - lib-src/emacstool.c
Use GPL.
[gnu-emacs] / lib-src / emacstool.c
1 /*
2 Copyright (C) 1986, 1988, 1990, 1991 Free Software Foundation, Inc.
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
16
17 /*
18 * For Emacs in SunView/Sun-Windows: (supported by Sun Unix v3.2 or greater)
19 * Insert a notifier filter-function to convert all useful input
20 * to "key" sequences that emacs can understand. See: Emacstool(1).
21 *
22 * Author: Jeff Peck, Sun Microsystems, Inc. <peck@eng.sun.com>
23 *
24 * Original Idea: Ian Batten
25 * Updated 15-Mar-88, Jeff Peck: set IN_EMACSTOOL, TERM, TERMCAP
26 * Updated 10-Sep-88, Jeff Peck: add XVIEW and JLE support
27 * Updated 8-Oct-90, Jeff Peck: add Meta-bit for Xview
28 * Updated 6-Mar-91, Jeff Peck: Hack to detect -Wt invocation
29 * [note, TTYSW limitation means you must Click-To-Type in Openwin]
30 * [fixed in OW3 or use local/tty.o]
31 * for better results, this should move to using TERMSW.
32 * Updated 10-Mar-91, Jeff Peck, et al: support for TERMSW (TTERM)
33 * allows point-to-type even in OW2
34 *
35 * [note: xvetool should be started with the "-nw" flag for emacs!]
36 */
37
38 #ifdef XVIEW
39 #include <xview/xview.h>
40 #include <xview/panel.h>
41 #include <xview/attr.h>
42 #include <xview/tty.h>
43 #include <xview/ttysw.h> /* private defines */
44 #include <xview/termsw.h> /* -DTTERM */
45 #include <xview/font.h> /* for testing */
46 #else
47 #include <suntool/sunview.h>
48 #include <suntool/tty.h>
49 #include <suntool/ttysw.h>
50 #endif XVIEW
51
52 #ifdef JLE
53 # include <locale.h>
54 #endif JLE
55
56 #include <stdio.h>
57 #include <sys/file.h>
58
59 #define BUFFER_SIZE 128 /* Size of all the buffers */
60
61 /* define WANT_CAPS_LOCK to make f-key T1 (aka F1) behave as CapsLock */
62 #define WANT_CAPS_LOCK
63 #ifdef WANT_CAPS_LOCK
64 int caps_lock; /* toggle indicator for f-key T1 caps lock */
65 static char *Caps = "[CAPS] "; /* Caps Lock prefix string */
66 #define CAPS_LEN 7 /* strlen (Caps) */
67 #endif
68
69 static char *mouse_prefix = "\030\000"; /* C-x C-@ */
70 static int m_prefix_length = 2; /* mouse_prefix length */
71
72 static char *key_prefix = "\030*"; /* C-x * */
73 static int k_prefix_length = 2; /* key_prefix length */
74
75 #ifdef JLE
76 static char *emacs_name = "nemacs"; /* default run command */
77 static char *title = "NEmacstool - "; /* initial title */
78 #else
79 static char *emacs_name = "emacs"; /* default run command */
80 static char *title = "Emacstool - "; /* initial title */
81 #endif JLE
82
83 static char buffer[BUFFER_SIZE]; /* send to ttysw_input */
84 static char *bold_name = 0; /* for -bold option */
85
86 Frame frame; /* Base frame for system */
87
88 #ifndef TTERM
89 #define SWTYPE TTY
90 Tty tty_win; /* Where emacs is reading */
91 #else
92 #define SWTYPE TERMSW
93 Termsw tty_win; /* Termsw does follow-mouse */
94 #endif TTERM
95
96 #ifdef XVIEW
97 Xv_Window tty_view; /* Where the events are in Xview*/
98 #else
99 Tty tty_view; /* SunView place filler */
100 #endif XVIEW
101
102 int font_width, font_height; /* For translating pixels to chars */
103 int left_margin = 0; /* default window -- frame offset */
104
105 int console_fd = 0; /* for debugging: setenv DEBUGEMACSTOOL */
106 FILE *console; /* for debugging: setenv DEBUGEMACSTOOL */
107
108 Icon frame_icon;
109 /* make an icon_image for the default frame_icon */
110 static short default_image[258] =
111 {
112 #include <images/terminal.icon>
113 };
114 mpr_static(icon_image, 64, 64, 1, default_image);
115 \f
116 /*
117 * Assign a value to a set of keys
118 */
119 int
120 button_value (event)
121 Event *event;
122 {
123 int retval = 0;
124 /*
125 * Code up the current situation:
126 *
127 * 1 = MS_LEFT;
128 * 2 = MS_MIDDLE;
129 * 4 = MS_RIGHT;
130 * 8 = SHIFT;
131 * 16 = CONTROL;
132 * 32 = META;
133 * 64 = DOUBLE;
134 * 128 = UP;
135 */
136
137 if (MS_LEFT == (event_id (event))) retval = 1;
138 if (MS_MIDDLE == (event_id (event))) retval = 2;
139 if (MS_RIGHT == (event_id (event))) retval = 4;
140
141 if (event_shift_is_down (event)) retval += 8;
142 if (event_ctrl_is_down (event)) retval += 16;
143 if (event_meta_is_down (event)) retval += 32;
144 if (event_is_up (event)) retval += 128;
145 return retval;
146 }
147 \f
148 /*
149 * Variables to store the time of the previous mouse event that was
150 * sent to emacs.
151 *
152 * The theory is that to time double clicks while ignoreing UP buttons,
153 * we must keep track of the accumulated time.
154 *
155 * If someone writes a SUN-SET-INPUT-MASK for emacstool,
156 * That could be used to selectively disable UP events,
157 * and then this cruft wouldn't be necessary.
158 */
159 static long prev_event_sec = 0;
160 static long prev_event_usec = 0;
161
162 /*
163 * Give the time difference in milliseconds, where one second
164 * is considered infinite.
165 */
166 int
167 time_delta (now_sec, now_usec, prev_sec, prev_usec)
168 long now_sec, now_usec, prev_sec, prev_usec;
169 {
170 long sec_delta = now_sec - prev_sec;
171 long usec_delta = now_usec - prev_usec;
172
173 if (usec_delta < 0) { /* "borrow" a second */
174 usec_delta += 1000000;
175 --sec_delta;
176 }
177
178 if (sec_delta >= 10)
179 return (9999); /* Infinity */
180 else
181 return ((sec_delta * 1000) + (usec_delta / 1000));
182 }
183
184 \f
185 /*
186 * Filter function to translate selected input events for emacs
187 * Mouse button events become ^X^@(button x-col y-line time-delta) .
188 * Function keys: ESC-*{c}{lrt} l,r,t for Left, Right, Top;
189 * {c} encodes the keynumber as a character [a-o]
190 */
191 static Notify_value
192 input_event_filter_function (window, event, arg, type)
193 #ifdef XVIEW
194 Xv_Window window;
195 #else
196 Window window;
197 #endif XVIEW
198 Event *event;
199 Notify_arg arg;
200 Notify_event_type type;
201 {
202 struct timeval time_stamp;
203
204 if (console_fd) fprintf(console, "Event: %d\n", event_id(event));
205
206 /* UP L1 is the STOP key */
207 if (event_id(event) == WIN_STOP) {
208 ttysw_input(tty_win, "\007\007\007\007\007\007\007", 7);
209 return NOTIFY_IGNORED;
210 }
211
212 /* UP L5 & L7 is Expose & Open, let them pass to sunview */
213 if (event_id(event) == KEY_LEFT(5) || event_id(event) == KEY_LEFT(7))
214 if(event_is_up (event))
215 return notify_next_event_func (window, event, arg, type);
216 else return NOTIFY_IGNORED;
217
218 if (event_is_button (event)) { /* do Mouse Button events */
219 /* Commented out so that we send mouse up events too.
220 if (event_is_up (event))
221 return notify_next_event_func (window, event, arg, type);
222 */
223 time_stamp = event_time (event);
224 ttysw_input (tty_win, mouse_prefix, m_prefix_length);
225 sprintf (buffer, "(%d %d %d %d)\015",
226 button_value (event),
227 (event_x (event) - left_margin) / font_width,
228 event_y (event) / font_height,
229 time_delta (time_stamp.tv_sec, time_stamp.tv_usec,
230 prev_event_sec, prev_event_usec)
231 );
232 ttysw_input (tty_win, buffer, strlen(buffer));
233 prev_event_sec = time_stamp.tv_sec;
234 prev_event_usec = time_stamp.tv_usec;
235 return NOTIFY_IGNORED;
236 }
237
238 { /* Do the function key events */
239 int d;
240 char c = (char) 0;
241 if ((event_is_key_left (event)) ?
242 ((d = event_id(event) - KEY_LEFT(1) + 'a'), c='l') :
243 ((event_is_key_right (event)) ?
244 ((d = event_id(event) - KEY_RIGHT(1) + 'a'), c='r') :
245 ((event_is_key_top (event)) ?
246 ((d = event_id(event) - KEY_TOP(1) + 'a'), c='t') : 0)))
247 {
248 if (event_is_up(event)) return NOTIFY_IGNORED;
249 if (event_shift_is_down (event)) c = c - 32;
250 /* this will give a non-{lrt} for unshifted keys */
251 if (event_ctrl_is_down (event)) c = c - 64;
252 if (event_meta_is_down (event)) c = c + 128;
253 #ifdef WANT_CAPS_LOCK
254 /* set a toggle and relabel window so T1 can act like caps-lock */
255 if (event_id(event) == KEY_TOP(1))
256 {
257 /* make a frame label with and without CAPS */
258 strcpy (buffer, Caps);
259 title = &buffer[CAPS_LEN];
260 strncpy (title, (char *)window_get (frame, FRAME_LABEL),
261 BUFFER_SIZE - CAPS_LEN);
262 buffer[BUFFER_SIZE] = (char) 0;
263 if (strncmp (title, Caps, CAPS_LEN) == 0)
264 title += CAPS_LEN; /* already Caps */
265 caps_lock = (caps_lock ? 0 : CAPS_LEN);
266 window_set(frame, FRAME_LABEL, (title -= caps_lock), 0);
267 return NOTIFY_IGNORED;
268 }
269 #endif
270 ttysw_input (tty_win, key_prefix, k_prefix_length);
271 sprintf (buffer, "%c%c", d, c);
272 ttysw_input(tty_win, buffer, strlen(buffer));
273
274 return NOTIFY_IGNORED;
275 }
276 }
277 if ((event_is_ascii(event) || event_is_meta(event))
278 && event_is_up(event)) return NOTIFY_IGNORED;
279 #ifdef WANT_CAPS_LOCK
280 /* shift alpha chars to upper case if toggle is set */
281 if ((caps_lock) && event_is_ascii(event)
282 && (event_id(event) >= 'a') && (event_id(event) <= 'z'))
283 event_set_id(event, (event_id(event) - 32));
284 /* crufty, but it works for now. is there an UPCASE(event)? */
285 #endif
286 #ifndef NO_META_BIT
287 /* under Openwindows/X, the meta bit is not set in the key event,
288 * emacs expects this so we add it in here:
289 */
290 if (event_is_ascii(event) && event_meta_is_down(event))
291 event_set_id(event, 128 | event_id(event));
292 #endif
293 return notify_next_event_func (window, event, arg, type);
294 }
295 \f
296 main (argc, argv)
297 int argc;
298 char **argv;
299 {
300 int error_code; /* Error codes */
301
302 #ifdef JLE
303 setlocale(LC_ALL, "");
304 #endif JLE
305
306 if(getenv("DEBUGEMACSTOOL"))
307 console = fdopen (console_fd = open("/dev/console",O_WRONLY), "w");
308
309 putenv("IN_EMACSTOOL=t"); /* notify subprocess that it is in emacstool */
310
311 if (putenv("TERM=sun") != 0) /* TTY_WIN will be a TERM=sun window */
312 {fprintf (stderr, "%s: Could not set TERM=sun, using `%s'\n",
313 argv[0], (char *)getenv("TERM")) ;};
314 /*
315 * If TERMCAP starts with a slash, it is the pathname of the
316 * termcap file, not an entry extracted from it, so KEEP it!
317 * Otherwise, it may not relate to the new TERM, so Nuke-It.
318 * If there is no TERMCAP environment variable, don't make one.
319 */
320 {
321 char *termcap ; /* Current TERMCAP value */
322 termcap = (char *)getenv("TERMCAP") ;
323 if (termcap && (*termcap != '/'))
324 {
325 if (putenv("TERMCAP=") != 0)
326 {fprintf (stderr, "%s: Could not clear TERMCAP\n", argv[0]) ;} ;
327 } ;
328 } ;
329
330 /* find command to run as subprocess in window */
331 if (!(argv[0] = (char *)getenv("EMACSTOOL"))) /* Set emacs command name */
332 argv[0] = emacs_name;
333 /* Emacstool recognizes two special args: -rc <file> and -bold <bold-name> */
334 for (argc = 1; argv[argc]; argc++) /* Use last one on line */
335 {
336 if(!(strcmp ("-rc", argv[argc]))) /* Override if -rc given */
337 {int i = argc;
338 argv[argc--]=0; /* kill the -rc argument */
339 if (argv[i+1]) { /* move to argv[0] and squeeze the rest */
340 argv[0]=argv[i+1];
341 for (; argv[i+2]; (argv[i]=argv[i+2],argv[++i]=0));
342 }
343 }
344
345 if (!(strcmp ("-bold", argv[argc])))
346 {int i = argc;
347 argv[argc--]=0; /* kill the -bold argument */
348 if (argv[i+1]) { /* move to bold_name and squeeze the rest */
349 bold_name = argv[i+1];
350 for (; argv[i+2]; (argv[i]=argv[i+2],argv[++i]=0));
351 }
352 }
353 };
354
355 strcpy (buffer, title);
356 strncat (buffer, argv[0], /* append run command name */
357 (BUFFER_SIZE - (strlen (buffer)) - (strlen (argv[0]))) - 1);
358
359 error_code = interpose_on_window(argc,argv);
360 if (error_code != 0) { /* Barf */
361 fprintf (stderr, "notify_interpose_event_func returns %d.\n", error_code);
362 exit (1);
363 }
364
365 #ifdef XVIEW
366 xv_main_loop (frame); /* And away we go */
367 #else
368 window_main_loop (frame);
369 #endif XVIEW
370 }
371
372 #ifdef XVIEW
373 int interpose_on_window(argc,argv)
374 int argc;
375 char **argv;
376 {
377 #ifndef TTERM
378 int i, font_width_adjust = 1; /* hackery, and hueristics */
379 /* if -Wt is not supplied, then font comes out as lucida-14 (width=8)
380 * rather than the screen.r.12 (width=7) typically used
381 * this hack attempts to workaround it.
382 * could use a env var EMACSTOOL_DEFAULT_FONT_WIDTH instead */
383 for (i = 1; argv[i]; i++) {
384 if (!(strcmp ("-Wt", argv[i])))
385 {font_width_adjust = 0;
386 if (console_fd) fprintf(console, "-Wt = %d\n", font_width_adjust);
387 break;}
388 }
389 #endif TTERM
390 /* initialize Xview, and strip window args */
391 xv_init(XV_INIT_ARGC_PTR_ARGV, &argc, argv, 0);
392
393 /* do this first, so arglist can override it */
394 frame_icon = icon_create (ICON_LABEL, "Emacstool",
395 ICON_IMAGE, &icon_image,
396 0);
397
398 /* Build a frame to run in */
399 frame = xv_create ((Xv_Window)NULL, FRAME,
400 FRAME_LABEL, buffer,
401 FRAME_ICON, frame_icon,
402 0);
403
404 /* Create a tty with emacs in it */
405 tty_win = xv_create (frame, SWTYPE, WIN_IS_CLIENT_PANE,
406 TTY_QUIT_ON_CHILD_DEATH, TRUE,
407 TTY_BOLDSTYLE, TTYSW_BOLD_INVERT,
408 TTY_ARGV, argv,
409 0);
410
411 if (bold_name) {
412 (void)xv_set(tty_win, TTY_BOLDSTYLE_NAME, bold_name, 0);
413 }
414
415 {
416 Xv_font font; /* declare temp font variable */
417 font = (Xv_font)xv_get (tty_win, XV_FONT);
418 font_height = (int)xv_get (font, FONT_DEFAULT_CHAR_HEIGHT);
419 font_width = (int)xv_get (font, FONT_DEFAULT_CHAR_WIDTH);
420 }
421 if (console_fd) fprintf(console, "Width = %d\n", font_width);
422
423 #ifndef TTERM
424 font_width -= font_width_adjust; /* A guess! font bug in ttysw*/
425 #else
426 /* make the termsw act as a tty */
427 xv_set(tty_win, TERMSW_MODE, TTYSW_MODE_TYPE, 0);
428 /* termsw has variable offset depending on scrollbar size/location */
429 left_margin = (int)xv_get (tty_win, TEXTSW_LEFT_MARGIN);
430 #endif TTERM
431
432 tty_view = (Xv_Window) xv_get (tty_win, OPENWIN_NTH_VIEW, 0);
433 xv_set(tty_view,
434 WIN_CONSUME_EVENTS,
435 WIN_MOUSE_BUTTONS, WIN_UP_EVENTS,
436 ACTION_ADJUST, ACTION_MENU,
437 WIN_ASCII_EVENTS,
438 WIN_LEFT_KEYS, WIN_TOP_KEYS, WIN_RIGHT_KEYS,
439 0,
440 0);
441 /* Interpose my event function */
442 return (int) notify_interpose_event_func
443 (tty_view, input_event_filter_function, NOTIFY_SAFE);
444 }
445 #else
446 int interpose_on_window (argc, argv)
447 int argc;
448 char **argv;
449 {
450 /* do this first, so arglist can override it */
451 frame_icon = icon_create (ICON_LABEL, "Emacstool",
452 ICON_IMAGE, &icon_image,
453 0);
454
455 /* Build a frame to run in */
456 frame = window_create ((Window)NULL, FRAME,
457 FRAME_LABEL, buffer,
458 FRAME_ICON, frame_icon,
459 FRAME_ARGC_PTR_ARGV, &argc, argv,
460 0);
461
462 /* Create a tty with emacs in it */
463 tty_win = window_create (frame, TTY,
464 TTY_QUIT_ON_CHILD_DEATH, TRUE,
465 TTY_BOLDSTYLE, TTYSW_BOLD_INVERT,
466 TTY_ARGV, argv,
467 0);
468
469 if (bold_name) {
470 (void)window_set(tty_win, TTY_BOLDSTYLE_NAME, bold_name, 0);
471 }
472
473 /* ttysw uses pf_default, one must set WIN_FONT explicitly */
474 window_set (tty_win, WIN_FONT, pf_default(), 0);
475 font_height = (int)window_get (tty_win, WIN_ROW_HEIGHT);
476 font_width = (int)window_get (tty_win, WIN_COLUMN_WIDTH);
477
478 tty_view = tty_win;
479 window_set(tty_view,
480 WIN_CONSUME_PICK_EVENTS,
481 WIN_STOP,
482 WIN_MOUSE_BUTTONS, WIN_UP_EVENTS,
483 /* LOC_WINENTER, LOC_WINEXIT, LOC_MOVE, */
484 0,
485 WIN_CONSUME_KBD_EVENTS,
486 WIN_STOP,
487 WIN_ASCII_EVENTS,
488 WIN_LEFT_KEYS, WIN_TOP_KEYS, WIN_RIGHT_KEYS,
489 /* WIN_UP_ASCII_EVENTS, */
490 0,
491 0);
492 /* Interpose my event function */
493 return (int) notify_interpose_event_func
494 (tty_view, input_event_filter_function, NOTIFY_SAFE);
495 }
496 #endif XVIEW