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)
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.
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. */
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).
22 * Author: Jeff Peck, Sun Microsystems, Inc. <peck@eng.sun.com>
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
35 * [note: xvetool should be started with the "-nw" flag for emacs!]
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 */
47 #include <suntool/sunview.h>
48 #include <suntool/tty.h>
49 #include <suntool/ttysw.h>
59 #define BUFFER_SIZE 128 /* Size of all the buffers */
61 /* define WANT_CAPS_LOCK to make f-key T1 (aka F1) behave as CapsLock */
62 #define 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) */
69 static char *mouse_prefix
= "\030\000"; /* C-x C-@ */
70 static int m_prefix_length
= 2; /* mouse_prefix length */
72 static char *key_prefix
= "\030*"; /* C-x * */
73 static int k_prefix_length
= 2; /* key_prefix length */
76 static char *emacs_name
= "nemacs"; /* default run command */
77 static char *title
= "NEmacstool - "; /* initial title */
79 static char *emacs_name
= "emacs"; /* default run command */
80 static char *title
= "Emacstool - "; /* initial title */
83 static char buffer
[BUFFER_SIZE
]; /* send to ttysw_input */
84 static char *bold_name
= 0; /* for -bold option */
86 Frame frame
; /* Base frame for system */
90 Tty tty_win
; /* Where emacs is reading */
93 Termsw tty_win
; /* Termsw does follow-mouse */
97 Xv_Window tty_view
; /* Where the events are in Xview*/
99 Tty tty_view
; /* SunView place filler */
102 int font_width
, font_height
; /* For translating pixels to chars */
103 int left_margin
= 0; /* default window -- frame offset */
105 int console_fd
= 0; /* for debugging: setenv DEBUGEMACSTOOL */
106 FILE *console
; /* for debugging: setenv DEBUGEMACSTOOL */
109 /* make an icon_image for the default frame_icon */
110 static short default_image
[258] =
112 #include <images/terminal.icon>
114 mpr_static(icon_image
, 64, 64, 1, default_image
);
117 * Assign a value to a set of keys
125 * Code up the current situation:
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;
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;
149 * Variables to store the time of the previous mouse event that was
152 * The theory is that to time double clicks while ignoreing UP buttons,
153 * we must keep track of the accumulated time.
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.
159 static long prev_event_sec
= 0;
160 static long prev_event_usec
= 0;
163 * Give the time difference in milliseconds, where one second
164 * is considered infinite.
167 time_delta (now_sec
, now_usec
, prev_sec
, prev_usec
)
168 long now_sec
, now_usec
, prev_sec
, prev_usec
;
170 long sec_delta
= now_sec
- prev_sec
;
171 long usec_delta
= now_usec
- prev_usec
;
173 if (usec_delta
< 0) { /* "borrow" a second */
174 usec_delta
+= 1000000;
179 return (9999); /* Infinity */
181 return ((sec_delta
* 1000) + (usec_delta
/ 1000));
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]
192 input_event_filter_function (window
, event
, arg
, type
)
200 Notify_event_type type
;
202 struct timeval time_stamp
;
204 if (console_fd
) fprintf(console
, "Event: %d\n", event_id(event
));
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
;
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
;
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);
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
)
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
;
238 { /* Do the function key events */
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)))
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))
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
;
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
));
274 return NOTIFY_IGNORED
;
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)? */
287 /* under Openwindows/X, the meta bit is not set in the key event,
288 * emacs expects this so we add it in here:
290 if (event_is_ascii(event
) && event_meta_is_down(event
))
291 event_set_id(event
, 128 | event_id(event
));
293 return notify_next_event_func (window
, event
, arg
, type
);
300 int error_code
; /* Error codes */
303 setlocale(LC_ALL
, "");
306 if(getenv("DEBUGEMACSTOOL"))
307 console
= fdopen (console_fd
= open("/dev/console",O_WRONLY
), "w");
309 putenv("IN_EMACSTOOL=t"); /* notify subprocess that it is in emacstool */
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")) ;};
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.
321 char *termcap
; /* Current TERMCAP value */
322 termcap
= (char *)getenv("TERMCAP") ;
323 if (termcap
&& (*termcap
!= '/'))
325 if (putenv("TERMCAP=") != 0)
326 {fprintf (stderr
, "%s: Could not clear TERMCAP\n", argv
[0]) ;} ;
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 */
336 if(!(strcmp ("-rc", argv
[argc
]))) /* Override if -rc given */
338 argv
[argc
--]=0; /* kill the -rc argument */
339 if (argv
[i
+1]) { /* move to argv[0] and squeeze the rest */
341 for (; argv
[i
+2]; (argv
[i
]=argv
[i
+2],argv
[++i
]=0));
345 if (!(strcmp ("-bold", argv
[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));
355 strcpy (buffer
, title
);
356 strncat (buffer
, argv
[0], /* append run command name */
357 (BUFFER_SIZE
- (strlen (buffer
)) - (strlen (argv
[0]))) - 1);
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
);
366 xv_main_loop (frame
); /* And away we go */
368 window_main_loop (frame
);
373 int interpose_on_window(argc
,argv
)
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
);
390 /* initialize Xview, and strip window args */
391 xv_init(XV_INIT_ARGC_PTR_ARGV
, &argc
, argv
, 0);
393 /* do this first, so arglist can override it */
394 frame_icon
= icon_create (ICON_LABEL
, "Emacstool",
395 ICON_IMAGE
, &icon_image
,
398 /* Build a frame to run in */
399 frame
= xv_create ((Xv_Window
)NULL
, FRAME
,
401 FRAME_ICON
, frame_icon
,
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
,
412 (void)xv_set(tty_win
, TTY_BOLDSTYLE_NAME
, bold_name
, 0);
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
);
421 if (console_fd
) fprintf(console
, "Width = %d\n", font_width
);
424 font_width
-= font_width_adjust
; /* A guess! font bug in ttysw*/
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
);
432 tty_view
= (Xv_Window
) xv_get (tty_win
, OPENWIN_NTH_VIEW
, 0);
435 WIN_MOUSE_BUTTONS
, WIN_UP_EVENTS
,
436 ACTION_ADJUST
, ACTION_MENU
,
438 WIN_LEFT_KEYS
, WIN_TOP_KEYS
, WIN_RIGHT_KEYS
,
441 /* Interpose my event function */
442 return (int) notify_interpose_event_func
443 (tty_view
, input_event_filter_function
, NOTIFY_SAFE
);
446 int interpose_on_window (argc
, argv
)
450 /* do this first, so arglist can override it */
451 frame_icon
= icon_create (ICON_LABEL
, "Emacstool",
452 ICON_IMAGE
, &icon_image
,
455 /* Build a frame to run in */
456 frame
= window_create ((Window
)NULL
, FRAME
,
458 FRAME_ICON
, frame_icon
,
459 FRAME_ARGC_PTR_ARGV
, &argc
, argv
,
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
,
470 (void)window_set(tty_win
, TTY_BOLDSTYLE_NAME
, bold_name
, 0);
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
);
480 WIN_CONSUME_PICK_EVENTS
,
482 WIN_MOUSE_BUTTONS
, WIN_UP_EVENTS
,
483 /* LOC_WINENTER, LOC_WINEXIT, LOC_MOVE, */
485 WIN_CONSUME_KBD_EVENTS
,
488 WIN_LEFT_KEYS
, WIN_TOP_KEYS
, WIN_RIGHT_KEYS
,
489 /* WIN_UP_ASCII_EVENTS, */
492 /* Interpose my event function */
493 return (int) notify_interpose_event_func
494 (tty_view
, input_event_filter_function
, NOTIFY_SAFE
);