1 /* GPM (General purpose mouse) functions
2 Copyright (C) 1997 William M. Perry <wmperry@gnu.org>
3 Copyright (C) 1999 Free Software Foundation, Inc.
5 This file is part of XEmacs.
7 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with XEmacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Synched up with: Not in FSF. */
24 /* Authors: William Perry */
29 #include "console-tty.h"
32 #include "events-mod.h"
36 #include "sysproc.h" /* for MAXDESC */
43 #if (!defined(__linux__)) /* possible under xterm */
48 #include <linux/keyboard.h>
52 extern void *gpm_stack;
54 static int (*orig_event_pending_p) (int);
55 static void (*orig_next_event_cb) (Lisp_Event *);
57 static Lisp_Object gpm_event_queue;
58 static Lisp_Object gpm_event_queue_tail;
66 static struct __gpm_state gpm_state_information[MAXDESC];
69 store_gpm_state (int fd)
71 gpm_state_information[fd].gpm_tried = gpm_tried;
72 gpm_state_information[fd].gpm_flag = gpm_flag;
73 gpm_state_information[fd].gpm_stack = gpm_stack;
77 restore_gpm_state (int fd)
79 gpm_tried = gpm_state_information[fd].gpm_tried;
80 gpm_flag = gpm_state_information[fd].gpm_flag;
81 gpm_stack = gpm_state_information[fd].gpm_stack;
82 gpm_consolefd = gpm_fd = fd;
86 clear_gpm_state (int fd)
90 memset(&gpm_state_information[fd], '\0', sizeof(struct __gpm_state));
92 gpm_tried = gpm_flag = 1;
93 gpm_fd = gpm_consolefd = -1;
98 get_process_infd (Lisp_Process *p)
100 Lisp_Object instr, outstr;
101 get_process_streams (p, &instr, &outstr);
102 assert (!NILP (instr));
103 return filedesc_stream_fd (XLSTREAM (instr));
106 DEFUN ("receive-gpm-event", Freceive_gpm_event, 0, 2, 0, /*
108 This function is the process handler for the GPM connection.
115 Lisp_Object fake_event = Qnil;
116 Lisp_Event *event = NULL;
118 static int num_events;
120 CHECK_PROCESS (process);
122 restore_gpm_state (get_process_infd (XPROCESS (process)));
124 if (!Gpm_GetEvent(&ev))
126 warn_when_safe (Qnil, Qcritical, "Gpm_GetEvent failed - %d", gpm_fd);
134 fake_event = Fmake_event (Qnil, Qnil);
135 event = XEVENT(fake_event);
137 event->timestamp = 0;
138 event->channel = Fselected_frame (Qnil); /* CONSOLE_SELECTED_FRAME (con); */
140 /* Whow, wouldn't named defines be NICE!?!?! */
143 if (ev.modifiers & 1) modifiers |= XEMACS_MOD_SHIFT;
144 if (ev.modifiers & 2) modifiers |= XEMACS_MOD_META;
145 if (ev.modifiers & 4) modifiers |= XEMACS_MOD_CONTROL;
146 if (ev.modifiers & 8) modifiers |= XEMACS_MOD_META;
148 if (ev.buttons & GPM_B_LEFT)
152 else if (ev.buttons & GPM_B_MIDDLE)
156 else if (ev.buttons & GPM_B_RIGHT)
161 switch (GPM_BARE_EVENTS(ev.type)) {
165 (ev.type & GPM_DOWN) ? button_press_event : button_release_event;
166 event->event.button.x = ev.x;
167 event->event.button.y = ev.y;
168 event->event.button.button = button;
169 event->event.button.modifiers = modifiers;
173 event->event_type = pointer_motion_event;
174 event->event.motion.x = ev.x;
175 event->event.motion.y = ev.y;
176 event->event.motion.modifiers = modifiers;
178 /* This will never happen */
182 /* Handle the event */
183 enqueue_event (fake_event, &gpm_event_queue, &gpm_event_queue_tail);
190 static void turn_off_gpm (char *process_name)
192 Lisp_Object process = Fget_process (build_string (process_name));
197 /* Something happened to our GPM process - fail silently */
201 fd = get_process_infd (XPROCESS (process));
203 restore_gpm_state (fd);
207 clear_gpm_state (fd);
209 Fdelete_process (build_string (process_name));
214 tty_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_type)
216 /* This function can GC */
217 struct device *d = decode_device (Qnil);
218 int fd = DEVICE_INFD (d);
220 Lisp_Object output_stream = Qnil;
221 Lisp_Object terminal_stream = Qnil ;
222 Lisp_Object output_string = Qnil;
223 struct gcpro gcpro1,gcpro2,gcpro3;
225 GCPRO3(output_stream,terminal_stream,output_string);
227 /* The ioctl() to paste actually puts things in the input queue of
228 ** the virtual console, so we need to trap that data, since we are
229 ** supposed to return the actual string selection from this
233 /* I really hate doing this, but it doesn't seem to cause any
234 ** problems, and it makes the Lstream_read stuff further down
235 ** error out correctly instead of trying to indefinitely read from
238 ** There is no set_descriptor_blocking() function call, but in my
239 ** testing under linux, it has not proved fatal to leave the
240 ** descriptor in non-blocking mode.
242 ** William Perry Nov 5, 1999
244 set_descriptor_non_blocking (fd);
246 /* We need two streams, one for reading from the selected device,
247 ** and one to write the data into. There is no writable version
248 ** of the lisp-string lstream, so we make do with a resizing
249 ** buffer stream, and make a string out of it after we are
252 output_stream = make_resizing_buffer_output_stream ();
253 terminal_stream = make_filedesc_input_stream (fd, 0, -1, LSTR_BLOCKED_OK);
254 output_string = Qnil;
256 /* #### We should arguably use a specbind() and an unwind routine here,
257 ** #### but I don't care that much right now.
259 if (NILP (output_stream) || NILP (terminal_stream))
261 /* Should we signal an error here? */
265 if (ioctl (fd, TIOCLINUX, &c) < 0)
267 /* Could not get the selection - eek */
274 Bufbyte tempbuf[1024]; /* some random amount */
275 Lstream_data_count i;
276 Lstream_data_count size_in_bytes =
277 Lstream_read (XLSTREAM (terminal_stream),
278 tempbuf, sizeof (tempbuf));
280 if (size_in_bytes <= 0)
282 /* end of the stream */
287 for (i = 0; i < size_in_bytes; i++)
289 if (tempbuf[i] == '\r')
295 Lstream_write (XLSTREAM (output_stream), tempbuf, size_in_bytes);
298 Lstream_flush (XLSTREAM (output_stream));
300 output_string = make_string (resizing_buffer_stream_ptr (XLSTREAM (output_stream)),
301 Lstream_byte_count (XLSTREAM (output_stream)));
303 Lstream_delete (XLSTREAM (output_stream));
304 Lstream_delete (XLSTREAM (terminal_stream));
308 return (output_string);
312 tty_selection_exists_p (Lisp_Object selection, Lisp_Object selection_type)
316 #endif /* TIOCLINUX */
320 tty_own_selection (Lisp_Object selection_name, Lisp_Object selection_value,
321 Lisp_Object how_to_add, Lisp_Object selection_type)
323 /* There is no way to do this cleanly - the GPM selection
324 ** 'protocol' (actually the TIOCLINUX ioctl) requires a start and
325 ** end position on the _screen_, not a string to stick in there.
328 ** William Perry Nov 4, 1999
333 /* This function appears to work once in a blue moon. I'm not sure
334 ** exactly why either. *sigh*
336 ** William Perry Nov 4, 1999
338 ** Apparently, this is the way (mouse-position) is supposed to work,
339 ** and I was just expecting something else. (mouse-pixel-position)
342 ** William Perry Nov 7, 1999
345 tty_get_mouse_position (struct device *d, Lisp_Object *frame, int *x, int *y)
350 memset(&ev,'\0',sizeof(ev));
352 num_buttons = Gpm_GetSnapshot(&ev);
356 /* This means there are events pending... */
358 /* #### In theory, we should drain the events pending, stick
359 ** #### them in the queue, and return the mouse position
366 *frame = DEVICE_SELECTED_FRAME (d);
371 tty_set_mouse_position (struct window *w, int x, int y)
374 #### I couldn't find any GPM functions that set the mouse position.
375 #### Mr. Perry had left this function empty; that must be why.
380 static int gpm_event_pending_p (int user_p)
384 EVENT_CHAIN_LOOP (event, gpm_event_queue)
386 if (!user_p || command_event_p (event))
391 return (orig_event_pending_p (user_p));
394 static void gpm_next_event_cb (Lisp_Event *event)
396 /* #### It would be nice to preserve some sort of ordering of the
397 ** #### different types of events, but that would be quite a bit
398 ** #### of work, and would more than likely break the abstraction
399 ** #### between the other event loops and this one.
402 if (!NILP (gpm_event_queue))
404 Lisp_Object queued_event = dequeue_event (&gpm_event_queue, &gpm_event_queue_tail);
405 *event = *(XEVENT (queued_event));
407 if (event->event_type == pointer_motion_event)
409 struct device *d = decode_device (event->channel);
410 int fd = DEVICE_INFD (d);
412 /* Ok, now this is just freaky. Bear with me though.
414 ** If you run gnuclient and attach to a XEmacs running in
415 ** X or on another TTY, the mouse cursor does not get
416 ** drawn correctly. This is because the ioctl() fails
417 ** with EPERM because the TTY specified is not our
418 ** controlling terminal. If you are the superuser, it
419 ** will work just spiffy. The appropriate source file (at
420 ** least in linux 2.2.x) is
421 ** .../linux/drivers/char/console.c in the function
422 ** tioclinux(). The following bit of code is brutal to
425 ** if (current->tty != tty && !suser())
428 ** I even tried setting us as a process leader, removing
429 ** our controlling terminal, and then using the TIOCSCTTY
430 ** to set up a new controlling terminal, all with no luck.
432 ** What is even weirder is if you run XEmacs in a VC, and
433 ** attach to it from another VC with gnuclient, go back to
434 ** the original VC and hit a key, the mouse pointer
435 ** displays (in BOTH VCs), until you hit a key in the
436 ** second VC, after which it does not display in EITHER
439 ** All I can say is thank god Linux comes with source code
440 ** or I would have been completely confused. Well, ok,
441 ** I'm still completely confused. I don't see why they
442 ** don't just check the permissions on the device
443 ** (actually, if you have enough access to it to get the
444 ** console's file descriptor, you should be able to do
445 ** with it as you wish, but maybe that is just me).
447 ** William M. Perry - Nov 9, 1999
450 Gpm_DrawPointer (event->event.motion.x,event->event.motion.y, fd);
456 orig_next_event_cb (event);
459 static void hook_event_callbacks_once (void)
465 orig_event_pending_p = event_stream->event_pending_p;
466 orig_next_event_cb = event_stream->next_event_cb;
467 event_stream->event_pending_p = gpm_event_pending_p;
468 event_stream->next_event_cb = gpm_next_event_cb;
473 static void hook_console_methods_once (void)
479 /* Install the mouse position methods for the TTY console type */
480 CONSOLE_HAS_METHOD (tty, get_mouse_position);
481 CONSOLE_HAS_METHOD (tty, set_mouse_position);
482 CONSOLE_HAS_METHOD (tty, get_foreign_selection);
483 CONSOLE_HAS_METHOD (tty, selection_exists_p);
485 CONSOLE_HAS_METHOD (tty, own_selection);
490 DEFUN ("gpm-enabled-p", Fgpm_enabled_p, 0, 1, 0, /*
491 Return non-nil if GPM mouse support is currently enabled on DEVICE.
495 char *console_name = ttyname (DEVICE_INFD (decode_device (device)));
496 char process_name[1024];
504 memset (process_name, '\0', sizeof(process_name));
505 snprintf (process_name, sizeof(process_name) - 1, "gpm for %s", console_name);
507 proc = Fget_process (build_string (process_name));
514 if (1) /* (PROCESS_LIVE_P (proc)) */
521 DEFUN ("gpm-enable", Fgpm_enable, 0, 2, 0, /*
522 Toggle accepting of GPM mouse events.
528 Lisp_Object gpm_process;
529 Lisp_Object gpm_filter;
530 struct device *d = decode_device (device);
531 int fd = DEVICE_INFD (d);
532 char *console_name = ttyname (fd);
533 char process_name[1024];
535 hook_event_callbacks_once ();
536 hook_console_methods_once ();
540 error ("Can't connect to GPM in batch mode.");
545 /* Something seriously wrong here... */
549 memset (process_name, '\0', sizeof(process_name));
550 snprintf (process_name, sizeof(process_name) - 1, "gpm for %s", console_name);
554 turn_off_gpm (process_name);
559 ** Though shalt not call (gpm-enable t) after we have already
560 ** started, or stuff blows up.
562 if (!NILP (Fgpm_enabled_p (device)))
564 error ("GPM already enabled for this console.");
567 conn.eventMask = GPM_DOWN|GPM_UP|GPM_MOVE|GPM_DRAG;
568 conn.defaultMask = GPM_MOVE;
570 conn.maxMod = ((1<<KG_SHIFT)|(1<<KG_ALT)|(1<<KG_CTRL));
572 /* Reset some silly static variables so that multiple Gpm_Open()
573 ** calls have even a slight chance of working
579 /* Make sure Gpm_Open() does ioctl() on the correct
580 ** descriptor, or it can get the wrong terminal sizes, etc.
584 /* We have to pass the virtual console manually, otherwise if you
585 ** use 'gnuclient -nw' to connect to an XEmacs that is running in
586 ** X, Gpm_Open() tries to use ttyname(0 | 1 | 2) to find out which
587 ** console you are using, which is of course not correct for the
590 if (strncmp (console_name, "/dev/tty",8) || !isdigit (console_name[8]))
592 /* Urk, something really wrong */
596 rval = Gpm_Open (&conn, atoi(console_name + 8));
599 case -1: /* General failure */
601 case -2: /* We are running under an XTerm */
605 /* Is this really necessary? */
606 set_descriptor_non_blocking (gpm_fd);
607 store_gpm_state (gpm_fd);
608 gpm_process = connect_to_file_descriptor (build_string (process_name), Qnil,
612 if (!NILP (gpm_process))
615 Fprocess_kill_without_query (gpm_process, Qnil);
616 XSETSUBR (gpm_filter, &SFreceive_gpm_event);
617 set_process_filter (gpm_process, gpm_filter, 1);
619 /* Keep track of the device for later */
620 /* Fput (gpm_process, intern ("gpm-device"), device); */
629 return(rval ? Qnil : Qt);
632 void vars_of_gpmevent (void)
634 gpm_event_queue = Qnil;
635 gpm_event_queue_tail = Qnil;
636 staticpro (&gpm_event_queue);
637 staticpro (&gpm_event_queue_tail);
638 dump_add_root_object (&gpm_event_queue);
639 dump_add_root_object (&gpm_event_queue_tail);
642 void syms_of_gpmevent (void)
644 DEFSUBR (Freceive_gpm_event);
645 DEFSUBR (Fgpm_enable);
646 DEFSUBR (Fgpm_enabled_p);
649 #endif /* HAVE_GPM */