3 * Copyright (C) 1986 Free Software Foundation, Inc.
5 * This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 1, or (at your option)
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 * For Emacs in SunView
/Sun
-Windows
: (supported by Sun Unix v3
.2
)
23 * Insert a notifier filter
-function to convert all useful input
24 * to
"key" sequences that emacs can understand
. See
: Emacstool(1).
26 * Author
: Jeff Peck
, Sun Microsystems
, Inc
. <peck@sun
.com
>
28 * Original Idea
: Ian Batten
29 * Updated
15-Mar
-88, Jeff Peck
: set IN_EMACSTOOL
, TERM
, TERMCAP
33 #include <suntool/sunview.h>
34 #include <suntool/tty.h>
38 #define BUFFER_SIZE 128 /* Size of all the buffers */
40 /* define WANT_CAPS_LOCK to make f-key T1 (aka F1) behave as CapsLock */
41 #define WANT_CAPS_LOCK
43 int caps_lock
; /* toggle indicater for f-key T1 caps lock */
44 static char *Caps
= "[CAPS] "; /* Caps Lock prefix string */
45 #define CAPS_LEN 7 /* strlen (Caps) */
48 static char *mouse_prefix
= "\030\000"; /* C-x C-@ */
49 static int m_prefix_length
= 2; /* mouse_prefix length */
51 static char *key_prefix
= "\030*"; /* C-x * */
52 static int k_prefix_length
= 2; /* key_prefix length */
54 static char *emacs_name
= "emacs"; /* default run command */
55 static char buffer
[BUFFER_SIZE
]; /* send to ttysw_input */
56 static char *title
= "Emacstool - "; /* initial title */
58 Frame frame
; /* Base frame for system */
59 Tty ttysw
; /* Where emacs is */
60 int font_width
, font_height
; /* For translating pixels to chars */
62 int console_fd
= 0; /* for debugging: setenv DEBUGEMACSTOOL */
63 FILE *console
; /* for debugging: setenv DEBUGEMACSTOOL */
66 /* make an icon_image for the default frame_icon */
67 static short default_image
[258] =
69 #include <images/terminal.icon>
71 mpr_static(icon_image
, 64, 64, 1, default_image
);
75 * Assign a value to a set of keys
83 * Code up the current situation:
95 if (MS_LEFT
== (event_id (event
))) retval
= 1;
96 if (MS_MIDDLE
== (event_id (event
))) retval
= 2;
97 if (MS_RIGHT
== (event_id (event
))) retval
= 4;
99 if (event_shift_is_down (event
)) retval
+= 8;
100 if (event_ctrl_is_down (event
)) retval
+= 16;
101 if (event_meta_is_down (event
)) retval
+= 32;
102 if (event_is_up (event
)) retval
+= 128;
107 * Variables to store the time of the previous mouse event that was
110 * The theory is that to time double clicks while ignoreing UP buttons,
111 * we must keep track of the accumulated time.
113 * If someone writes a SUN-SET-INPUT-MASK for emacstool,
114 * That could be used to selectively disable UP events,
115 * and then this cruft wouldn't be necessary.
117 static long prev_event_sec
= 0;
118 static long prev_event_usec
= 0;
121 * Give the time difference in milliseconds, where one second
122 * is considered infinite.
125 time_delta (now_sec
, now_usec
, prev_sec
, prev_usec
)
126 long now_sec
, now_usec
, prev_sec
, prev_usec
;
128 long sec_delta
= now_sec
- prev_sec
;
129 long usec_delta
= now_usec
- prev_usec
;
131 if (usec_delta
< 0) { /* "borrow" a second */
132 usec_delta
+= 1000000;
137 return (9999); /* Infinity */
139 return ((sec_delta
* 1000) + (usec_delta
/ 1000));
144 * Filter function to translate selected input events for emacs
145 * Mouse button events become ^X^@(button x-col y-line time-delta) .
146 * Function keys: ESC-*{c}{lrt} l,r,t for Left, Right, Top;
147 * {c} encodes the keynumber as a character [a-o]
150 input_event_filter_function (window
, event
, arg
, type
)
154 Notify_event_type type
;
156 struct timeval time_stamp
;
158 if (console_fd
) fprintf(console
, "Event: %d\n", event_id(event
));
160 /* UP L1 is the STOP key */
161 if (event_id(event
) == WIN_STOP
) {
162 ttysw_input(ttysw
, "\007\007\007\007\007\007\007", 7);
163 return NOTIFY_IGNORED
;
166 /* UP L5 & L7 is Expose & Open, let them pass to sunview */
167 if (event_id(event
) == KEY_LEFT(5) || event_id(event
) == KEY_LEFT(7))
168 if(event_is_up (event
))
169 return notify_next_event_func (window
, event
, arg
, type
);
170 else return NOTIFY_IGNORED
;
172 if (event_is_button (event
)) { /* do Mouse Button events */
173 /* Commented out so that we send mouse up events too.
174 if (event_is_up (event))
175 return notify_next_event_func (window, event, arg, type);
177 time_stamp
= event_time (event
);
178 ttysw_input (ttysw
, mouse_prefix
, m_prefix_length
);
179 sprintf (buffer
, "(%d %d %d %d)\015",
180 button_value (event
),
181 event_x (event
) / font_width
,
182 event_y (event
) / font_height
,
183 time_delta (time_stamp
.tv_sec
, time_stamp
.tv_usec
,
184 prev_event_sec
, prev_event_usec
)
186 ttysw_input (ttysw
, buffer
, strlen(buffer
));
187 prev_event_sec
= time_stamp
.tv_sec
;
188 prev_event_usec
= time_stamp
.tv_usec
;
189 return NOTIFY_IGNORED
;
192 { /* Do the function key events */
195 if ((event_is_key_left (event
)) ?
196 ((d
= event_id(event
) - KEY_LEFT(1) + 'a'), c
='l') :
197 ((event_is_key_right (event
)) ?
198 ((d
= event_id(event
) - KEY_RIGHT(1) + 'a'), c
='r') :
199 ((event_is_key_top (event
)) ?
200 ((d
= event_id(event
) - KEY_TOP(1) + 'a'), c
='t') : 0)))
202 if (event_is_up(event
)) return NOTIFY_IGNORED
;
203 if (event_shift_is_down (event
)) c
= c
- 32;
204 /* this will give a non-{lrt} for unshifted keys */
205 if (event_ctrl_is_down (event
)) c
= c
- 64;
206 if (event_meta_is_down (event
)) c
= c
+ 128;
207 #ifdef WANT_CAPS_LOCK
208 /* set a toggle and relabel window so T1 can act like caps-lock */
209 if (event_id(event
) == KEY_TOP(1))
211 /* make a frame label with and without CAPS */
212 strcpy (buffer
, Caps
);
213 title
= &buffer
[CAPS_LEN
];
214 strncpy (title
, (char *)window_get (frame
, FRAME_LABEL
),
215 BUFFER_SIZE
- CAPS_LEN
);
216 buffer
[BUFFER_SIZE
] = (char) 0;
217 if (strncmp (title
, Caps
, CAPS_LEN
) == 0)
218 title
+= CAPS_LEN
; /* already Caps */
219 caps_lock
= (caps_lock
? 0 : CAPS_LEN
);
220 window_set(frame
, FRAME_LABEL
, (title
-= caps_lock
), 0);
221 return NOTIFY_IGNORED
;
224 ttysw_input (ttysw
, key_prefix
, k_prefix_length
);
225 sprintf (buffer
, "%c%c", d
, c
);
226 ttysw_input(ttysw
, buffer
, strlen(buffer
));
228 return NOTIFY_IGNORED
;
231 if ((event_is_ascii(event
) || event_is_meta(event
))
232 && event_is_up(event
)) return NOTIFY_IGNORED
;
233 #ifdef WANT_CAPS_LOCK
234 /* shift alpha chars to upper case if toggle is set */
235 if ((caps_lock
) && event_is_ascii(event
)
236 && (event_id(event
) >= 'a') && (event_id(event
) <= 'z'))
237 event_set_id(event
, (event_id(event
) - 32));
238 /* crufty, but it works for now. is there an UPCASE(event)? */
240 return notify_next_event_func (window
, event
, arg
, type
);
247 int error_code
; /* Error codes */
249 if(getenv("DEBUGEMACSTOOL"))
250 console
= fdopen (console_fd
= open("/dev/console",O_WRONLY
), "w");
252 /* do this first, so arglist can override it */
253 frame_icon
= icon_create (ICON_LABEL
, "Emacstool",
254 ICON_IMAGE
, &icon_image
,
257 putenv("IN_EMACSTOOL=t"); /* notify subprocess that it is in emacstool */
259 if (putenv("TERM=sun") != 0) /* TTYSW will be a TERM=sun window */
260 {fprintf (stderr
, "%s: Could not set TERM=sun, using `%s'\n",
261 argv
[0], (char *)getenv("TERM")) ;};
263 * If TERMCAP starts with a slash, it is the pathname of the
264 * termcap file, not an entry extracted from it, so KEEP it!
265 * Otherwise, it may not relate to the new TERM, so Nuke-It.
266 * If there is no TERMCAP environment variable, don't make one.
269 char *termcap
; /* Current TERMCAP value */
270 termcap
= (char *)getenv("TERMCAP") ;
271 if (termcap
&& (*termcap
!= '/'))
273 if (putenv("TERMCAP=") != 0)
274 {fprintf (stderr
, "%s: Could not clear TERMCAP\n", argv
[0]) ;} ;
278 /* find command to run as subprocess in window */
279 if (!(argv
[0] = (char *)getenv("EMACSTOOL"))) /* Set emacs command name */
280 argv
[0] = emacs_name
;
281 for (argc
= 1; argv
[argc
]; argc
++) /* Use last one on line */
282 if(!(strcmp ("-rc", argv
[argc
]))) /* Override if -rc given */
285 argv
[argc
--]=0; /* kill the -rc argument */
286 if (argv
[i
+1]) { /* move to agrv[0] and squeeze the rest */
288 for (; argv
[i
+2]; (argv
[i
]=argv
[i
+2],argv
[++i
]=0));
292 strcpy (buffer
, title
);
293 strncat (buffer
, argv
[0], /* append run command name */
294 (BUFFER_SIZE
- (strlen (buffer
)) - (strlen (argv
[0]))) - 1);
296 /* Build a frame to run in */
297 frame
= window_create ((Window
)NULL
, FRAME
,
299 FRAME_ICON
, frame_icon
,
300 FRAME_ARGC_PTR_ARGV
, &argc
, argv
,
303 /* Create a tty with emacs in it */
304 ttysw
= window_create (frame
, TTY
,
305 TTY_QUIT_ON_CHILD_DEATH
, TRUE
,
311 WIN_CONSUME_PICK_EVENTS
,
313 WIN_MOUSE_BUTTONS
, WIN_UP_EVENTS
,
314 /* LOC_WINENTER, LOC_WINEXIT, LOC_MOVE, */
317 WIN_CONSUME_KBD_EVENTS
,
320 WIN_LEFT_KEYS
, WIN_TOP_KEYS
, WIN_RIGHT_KEYS
,
321 /* WIN_UP_ASCII_EVENTS, */
326 font_height
= (int)window_get (ttysw
, WIN_ROW_HEIGHT
);
327 font_width
= (int)window_get (ttysw
, WIN_COLUMN_WIDTH
);
329 /* Interpose my event function */
330 error_code
= (int) notify_interpose_event_func
331 (ttysw
, input_event_filter_function
, NOTIFY_SAFE
);
333 if (error_code
!= 0) /* Barf */
335 fprintf (stderr
, "notify_interpose_event_func got %d.\n", error_code
);
339 window_main_loop (frame
); /* And away we go */