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;
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 |= MOD_SHIFT;
144 if (ev.modifiers & 2) modifiers |= MOD_META;
145 if (ev.modifiers & 4) modifiers |= MOD_CONTROL;
146 if (ev.modifiers & 8) modifiers |= 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;
221 Lisp_Object terminal_stream;
222 Lisp_Object output_string;
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 */
276 ssize_t 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)
316 #endif /* TIOCLINUX */
320 tty_own_selection (Lisp_Object selection_name, Lisp_Object selection_value)
322 /* There is no way to do this cleanly - the GPM selection
323 ** 'protocol' (actually the TIOCLINUX ioctl) requires a start and
324 ** end position on the _screen_, not a string to stick in there.
327 ** William Perry Nov 4, 1999
332 /* This function appears to work once in a blue moon. I'm not sure
333 ** exactly why either. *sigh*
335 ** William Perry Nov 4, 1999
337 ** Apparently, this is the way (mouse-position) is supposed to work,
338 ** and I was just expecting something else. (mouse-pixel-position)
341 ** William Perry Nov 7, 1999
344 tty_get_mouse_position (struct device *d, Lisp_Object *frame, int *x, int *y)
349 memset(&ev,'\0',sizeof(ev));
351 num_buttons = Gpm_GetSnapshot(&ev);
355 /* This means there are events pending... */
357 /* #### In theory, we should drain the events pending, stick
358 ** #### them in the queue, and return the mouse position
365 *frame = DEVICE_SELECTED_FRAME (d);
370 tty_set_mouse_position (struct window *w, int x, int y)
373 #### I couldn't find any GPM functions that set the mouse position.
374 #### Mr. Perry had left this function empty; that must be why.
379 static int gpm_event_pending_p (int user_p)
383 EVENT_CHAIN_LOOP (event, gpm_event_queue)
385 if (!user_p || command_event_p (event))
390 return (orig_event_pending_p (user_p));
393 static void gpm_next_event_cb (Lisp_Event *event)
395 /* #### It would be nice to preserve some sort of ordering of the
396 ** #### different types of events, but that would be quite a bit
397 ** #### of work, and would more than likely break the abstraction
398 ** #### between the other event loops and this one.
401 if (!NILP (gpm_event_queue))
403 Lisp_Object queued_event = dequeue_event (&gpm_event_queue, &gpm_event_queue_tail);
404 *event = *(XEVENT (queued_event));
406 if (event->event_type == pointer_motion_event)
408 struct device *d = decode_device (event->channel);
409 int fd = DEVICE_INFD (d);
411 /* Ok, now this is just freaky. Bear with me though.
413 ** If you run gnuclient and attach to a XEmacs running in
414 ** X or on another TTY, the mouse cursor does not get
415 ** drawn correctly. This is because the ioctl() fails
416 ** with EPERM because the TTY specified is not our
417 ** controlling terminal. If you are the superuser, it
418 ** will work just spiffy. The appropriate source file (at
419 ** least in linux 2.2.x) is
420 ** .../linux/drivers/char/console.c in the function
421 ** tioclinux(). The following bit of code is brutal to
424 ** if (current->tty != tty && !suser())
427 ** I even tried setting us as a process leader, removing
428 ** our controlling terminal, and then using the TIOCSCTTY
429 ** to set up a new controlling terminal, all with no luck.
431 ** What is even weirder is if you run XEmacs in a VC, and
432 ** attach to it from another VC with gnuclient, go back to
433 ** the original VC and hit a key, the mouse pointer
434 ** displays (in BOTH VCs), until you hit a key in the
435 ** second VC, after which it does not display in EITHER
438 ** All I can say is thank god Linux comes with source code
439 ** or I would have been completely confused. Well, ok,
440 ** I'm still completely confused. I don't see why they
441 ** don't just check the permissions on the device
442 ** (actually, if you have enough access to it to get the
443 ** console's file descriptor, you should be able to do
444 ** with it as you wish, but maybe that is just me).
446 ** William M. Perry - Nov 9, 1999
449 Gpm_DrawPointer (event->event.motion.x,event->event.motion.y, fd);
455 orig_next_event_cb (event);
458 static void hook_event_callbacks_once (void)
464 orig_event_pending_p = event_stream->event_pending_p;
465 orig_next_event_cb = event_stream->next_event_cb;
466 event_stream->event_pending_p = gpm_event_pending_p;
467 event_stream->next_event_cb = gpm_next_event_cb;
472 static void hook_console_methods_once (void)
478 /* Install the mouse position methods for the TTY console type */
479 CONSOLE_HAS_METHOD (tty, get_mouse_position);
480 CONSOLE_HAS_METHOD (tty, set_mouse_position);
481 CONSOLE_HAS_METHOD (tty, get_foreign_selection);
482 CONSOLE_HAS_METHOD (tty, selection_exists_p);
484 CONSOLE_HAS_METHOD (tty, own_selection);
489 DEFUN ("gpm-enabled-p", Fgpm_enabled_p, 0, 1, 0, /*
490 Return non-nil if GPM mouse support is currently enabled on DEVICE.
494 char *console_name = ttyname (DEVICE_INFD (decode_device (device)));
495 char process_name[1024];
503 memset (process_name, '\0', sizeof(process_name));
504 snprintf (process_name, sizeof(process_name) - 1, "gpm for %s", console_name);
506 proc = Fget_process (build_string (process_name));
513 if (1) /* (PROCESS_LIVE_P (proc)) */
520 DEFUN ("gpm-enable", Fgpm_enable, 0, 2, 0, /*
521 Toggle accepting of GPM mouse events.
527 Lisp_Object gpm_process;
528 Lisp_Object gpm_filter;
529 struct device *d = decode_device (device);
530 int fd = DEVICE_INFD (d);
531 char *console_name = ttyname (fd);
532 char process_name[1024];
534 hook_event_callbacks_once ();
535 hook_console_methods_once ();
539 error ("Can't connect to GPM in batch mode.");
544 /* Something seriously wrong here... */
548 memset (process_name, '\0', sizeof(process_name));
549 snprintf (process_name, sizeof(process_name) - 1, "gpm for %s", console_name);
553 turn_off_gpm (process_name);
558 ** Though shalt not call (gpm-enable t) after we have already
559 ** started, or stuff blows up.
561 if (!NILP (Fgpm_enabled_p (device)))
563 error ("GPM already enabled for this console.");
566 conn.eventMask = GPM_DOWN|GPM_UP|GPM_MOVE|GPM_DRAG;
567 conn.defaultMask = GPM_MOVE;
569 conn.maxMod = ((1<<KG_SHIFT)|(1<<KG_ALT)|(1<<KG_CTRL));
571 /* Reset some silly static variables so that multiple Gpm_Open()
572 ** calls have even a sligh chance of working
578 /* Make sure Gpm_Open() does ioctl() on the correct
579 ** descriptor, or it can get the wrong terminal sizes, etc.
583 /* We have to pass the virtual console manually, otherwise if you
584 ** use 'gnuclient -nw' to connect to an XEmacs that is running in
585 ** X, Gpm_Open() tries to use ttyname(0 | 1 | 2) to find out which
586 ** console you are using, which is of course not correct for the
589 if (strncmp (console_name, "/dev/tty",8) || !isdigit (console_name[8]))
591 /* Urk, something really wrong */
595 rval = Gpm_Open (&conn, atoi(console_name + 8));
598 case -1: /* General failure */
600 case -2: /* We are running under an XTerm */
604 /* Is this really necessary? */
605 set_descriptor_non_blocking (gpm_fd);
606 store_gpm_state (gpm_fd);
607 gpm_process = connect_to_file_descriptor (build_string (process_name), Qnil,
611 if (!NILP (gpm_process))
614 Fprocess_kill_without_query (gpm_process, Qnil);
615 XSETSUBR (gpm_filter, &SFreceive_gpm_event);
616 set_process_filter (gpm_process, gpm_filter, 1);
618 /* Keep track of the device for later */
619 /* Fput (gpm_process, intern ("gpm-device"), device); */
628 return(rval ? Qnil : Qt);
631 void vars_of_gpmevent (void)
633 gpm_event_queue = Qnil;
634 gpm_event_queue_tail = Qnil;
635 staticpro (&gpm_event_queue);
636 staticpro (&gpm_event_queue_tail);
637 pdump_wire (&gpm_event_queue);
638 pdump_wire (&gpm_event_queue_tail);
641 void syms_of_gpmevent (void)
643 DEFSUBR (Freceive_gpm_event);
644 DEFSUBR (Fgpm_enable);
645 DEFSUBR (Fgpm_enabled_p);
648 #endif /* HAVE_GPM */