2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 /* Handle the event stream, converting console events into SDL events */
27 #include <sys/types.h>
29 #include <sys/ioctl.h>
35 /* For parsing /proc */
41 #include <linux/keyboard.h>
43 #include "SDL_timer.h"
44 #include "SDL_mutex.h"
45 #include "../SDL_sysvideo.h"
46 #include "../../events/SDL_sysevents.h"
47 #include "../../events/SDL_events_c.h"
48 #include "SDL_fbvideo.h"
49 #include "SDL_fbevents_c.h"
50 #include "SDL_fbkeys.h"
52 #include "SDL_fbelo.h"
55 #define GPM_NODE_FIFO "/dev/gpmdata"
58 /*#define DEBUG_KEYBOARD*/
59 /*#define DEBUG_MOUSE*/
61 /* The translation tables from a console scancode to a SDL keysym */
62 #define NUM_VGAKEYMAPS (1<<KG_CAPSSHIFT)
63 static Uint16 vga_keymap[NUM_VGAKEYMAPS][NR_KEYS];
64 static SDLKey keymap[128];
65 static Uint16 keymap_temp[128]; /* only used at startup */
66 static SDL_keysym *TranslateKey(int scancode, SDL_keysym *keysym);
68 /* Ugh, we have to duplicate the kernel's keysym mapping code...
69 Oh, it's not so bad. :-)
71 FIXME: Add keyboard LED handling code
73 static void FB_vgainitkeymaps(int fd)
78 /* Don't do anything if we are passed a closed keyboard */
83 /* Load all the keysym mappings */
84 for ( map=0; map<NUM_VGAKEYMAPS; ++map ) {
85 SDL_memset(vga_keymap[map], 0, NR_KEYS*sizeof(Uint16));
86 for ( i=0; i<NR_KEYS; ++i ) {
89 if ( ioctl(fd, KDGKBENT, &entry) == 0 ) {
90 /* fill keytemp. This replaces SDL_fbkeys.h */
91 if ( (map == 0) && (i<128) ) {
92 keymap_temp[i] = entry.kb_value;
94 /* The "Enter" key is a special case */
95 if ( entry.kb_value == K_ENTER ) {
96 entry.kb_value = K(KT_ASCII,13);
98 /* Handle numpad specially as well */
99 if ( KTYP(entry.kb_value) == KT_PAD ) {
100 switch ( entry.kb_value ) {
111 vga_keymap[map][i]=entry.kb_value;
112 vga_keymap[map][i]+= '0';
115 vga_keymap[map][i]=K(KT_ASCII,'+');
118 vga_keymap[map][i]=K(KT_ASCII,'-');
121 vga_keymap[map][i]=K(KT_ASCII,'*');
124 vga_keymap[map][i]=K(KT_ASCII,'/');
127 vga_keymap[map][i]=K(KT_ASCII,'\r');
130 vga_keymap[map][i]=K(KT_ASCII,',');
133 vga_keymap[map][i]=K(KT_ASCII,'.');
139 /* Do the normal key translation */
140 if ( (KTYP(entry.kb_value) == KT_LATIN) ||
141 (KTYP(entry.kb_value) == KT_ASCII) ||
142 (KTYP(entry.kb_value) == KT_LETTER) ) {
143 vga_keymap[map][i] = entry.kb_value;
150 int FB_InGraphicsMode(_THIS)
152 return((keyboard_fd >= 0) && (saved_kbd_mode >= 0));
155 int FB_EnterGraphicsMode(_THIS)
157 struct termios keyboard_termios;
159 /* Set medium-raw keyboard mode */
160 if ( (keyboard_fd >= 0) && !FB_InGraphicsMode(this) ) {
162 /* Switch to the correct virtual terminal */
163 if ( current_vt > 0 ) {
164 struct vt_stat vtstate;
166 if ( ioctl(keyboard_fd, VT_GETSTATE, &vtstate) == 0 ) {
167 saved_vt = vtstate.v_active;
169 if ( ioctl(keyboard_fd, VT_ACTIVATE, current_vt) == 0 ) {
170 ioctl(keyboard_fd, VT_WAITACTIVE, current_vt);
174 /* Set the terminal input mode */
175 if ( tcgetattr(keyboard_fd, &saved_kbd_termios) < 0 ) {
176 SDL_SetError("Unable to get terminal attributes");
177 if ( keyboard_fd > 0 ) {
183 if ( ioctl(keyboard_fd, KDGKBMODE, &saved_kbd_mode) < 0 ) {
184 SDL_SetError("Unable to get current keyboard mode");
185 if ( keyboard_fd > 0 ) {
191 keyboard_termios = saved_kbd_termios;
192 keyboard_termios.c_lflag &= ~(ICANON | ECHO | ISIG);
193 keyboard_termios.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
194 keyboard_termios.c_cc[VMIN] = 0;
195 keyboard_termios.c_cc[VTIME] = 0;
196 if (tcsetattr(keyboard_fd, TCSAFLUSH, &keyboard_termios) < 0) {
197 FB_CloseKeyboard(this);
198 SDL_SetError("Unable to set terminal attributes");
201 /* This will fail if we aren't root or this isn't our tty */
202 if ( ioctl(keyboard_fd, KDSKBMODE, K_MEDIUMRAW) < 0 ) {
203 FB_CloseKeyboard(this);
204 SDL_SetError("Unable to set keyboard in raw mode");
207 if ( ioctl(keyboard_fd, KDSETMODE, KD_GRAPHICS) < 0 ) {
208 FB_CloseKeyboard(this);
209 SDL_SetError("Unable to set keyboard in graphics mode");
212 /* Prevent switching the virtual terminal */
213 ioctl(keyboard_fd, VT_LOCKSWITCH, 1);
218 void FB_LeaveGraphicsMode(_THIS)
220 if ( FB_InGraphicsMode(this) ) {
221 ioctl(keyboard_fd, KDSETMODE, KD_TEXT);
222 ioctl(keyboard_fd, KDSKBMODE, saved_kbd_mode);
223 tcsetattr(keyboard_fd, TCSAFLUSH, &saved_kbd_termios);
226 /* Head back over to the original virtual terminal */
227 ioctl(keyboard_fd, VT_UNLOCKSWITCH, 1);
228 if ( saved_vt > 0 ) {
229 ioctl(keyboard_fd, VT_ACTIVATE, saved_vt);
234 void FB_CloseKeyboard(_THIS)
236 if ( keyboard_fd >= 0 ) {
237 FB_LeaveGraphicsMode(this);
238 if ( keyboard_fd > 0 ) {
245 int FB_OpenKeyboard(_THIS)
247 /* Open only if not already opened */
248 if ( keyboard_fd < 0 ) {
249 static const char * const tty0[] = { "/dev/tty0", "/dev/vc/0", NULL };
250 static const char * const vcs[] = { "/dev/vc/%d", "/dev/tty%d", NULL };
253 /* Try to query for a free virtual terminal */
255 for ( i=0; tty0[i] && (tty0_fd < 0); ++i ) {
256 tty0_fd = open(tty0[i], O_WRONLY, 0);
259 tty0_fd = dup(0); /* Maybe stdin is a VT? */
261 ioctl(tty0_fd, VT_OPENQRY, ¤t_vt);
263 if ( (geteuid() == 0) && (current_vt > 0) ) {
264 for ( i=0; vcs[i] && (keyboard_fd < 0); ++i ) {
267 SDL_snprintf(vtpath, SDL_arraysize(vtpath), vcs[i], current_vt);
268 keyboard_fd = open(vtpath, O_RDWR, 0);
269 #ifdef DEBUG_KEYBOARD
270 fprintf(stderr, "vtpath = %s, fd = %d\n",
271 vtpath, keyboard_fd);
272 #endif /* DEBUG_KEYBOARD */
274 /* This needs to be our controlling tty
275 so that the kernel ioctl() calls work
277 if ( keyboard_fd >= 0 ) {
278 tty0_fd = open("/dev/tty", O_RDWR, 0);
279 if ( tty0_fd >= 0 ) {
280 ioctl(tty0_fd, TIOCNOTTY, 0);
286 if ( keyboard_fd < 0 ) {
287 /* Last resort, maybe our tty is a usable VT */
288 struct vt_stat vtstate;
290 keyboard_fd = open("/dev/tty", O_RDWR);
292 if ( ioctl(keyboard_fd, VT_GETSTATE, &vtstate) == 0 ) {
293 current_vt = vtstate.v_active;
298 #ifdef DEBUG_KEYBOARD
299 fprintf(stderr, "Current VT: %d\n", current_vt);
303 /* Make sure that our input is a console terminal */
305 if ( ioctl(keyboard_fd, KDGKBMODE, &dummy) < 0 ) {
308 SDL_SetError("Unable to open a console terminal");
313 FB_vgainitkeymaps(keyboard_fd);
320 MOUSE_MSC, /* Note: GPM uses the MSC protocol */
328 } mouse_drv = MOUSE_NONE;
330 void FB_CloseMouse(_THIS)
333 if (ts_dev != NULL) {
338 #endif /* SDL_INPUT_TSLIB */
339 if ( mouse_fd > 0 ) {
345 /* Returns processes listed in /proc with the desired name */
346 static int find_pid(DIR *proc, const char *wanted_name)
348 struct dirent *entry;
351 /* First scan proc for the gpm process */
353 while ( (pid == 0) && ((entry=readdir(proc)) != NULL) ) {
354 if ( isdigit(entry->d_name[0]) ) {
359 SDL_snprintf(path, SDL_arraysize(path), "/proc/%s/status", entry->d_name);
360 status=fopen(path, "r");
363 fscanf(status, "Name: %s", name);
364 if ( SDL_strcmp(name, wanted_name) == 0 ) {
365 pid = SDL_atoi(entry->d_name);
374 /* Returns true if /dev/gpmdata is being written to by gpm */
375 static int gpm_available(char *proto, size_t protolen)
380 int cmdline, len, arglen;
382 char args[PATH_MAX], *arg;
384 /* Don't bother looking if the fifo isn't there */
386 fprintf(stderr,"testing gpm\n");
388 if ( access(GPM_NODE_FIFO, F_OK) < 0 ) {
393 proc = opendir("/proc");
395 char raw_proto[10] = { '\0' };
396 char repeat_proto[10] = { '\0' };
397 while ( !available && (pid=find_pid(proc, "gpm")) > 0 ) {
398 SDL_snprintf(path, SDL_arraysize(path), "/proc/%d/cmdline", pid);
399 cmdline = open(path, O_RDONLY, 0);
400 if ( cmdline >= 0 ) {
401 len = read(cmdline, args, sizeof(args));
404 arglen = SDL_strlen(arg)+1;
406 fprintf(stderr,"gpm arg %s len %d\n",arg,arglen);
408 if ( SDL_strcmp(arg, "-t") == 0) {
409 /* protocol string, keep it for later */
412 s = SDL_strchr(t, ' ');
414 SDL_strlcpy(raw_proto, t, SDL_arraysize(raw_proto));
417 if ( SDL_strncmp(arg, "-R", 2) == 0 ) {
421 s = SDL_strchr(t, ' ');
423 SDL_strlcpy(repeat_proto, t, SDL_arraysize(repeat_proto));
435 if ( SDL_strcmp(repeat_proto, "raw") == 0 ) {
436 SDL_strlcpy(proto, raw_proto, protolen);
437 } else if ( *repeat_proto ) {
438 SDL_strlcpy(proto, repeat_proto, protolen);
440 SDL_strlcpy(proto, "msc", protolen);
448 /* rcg06112001 Set up IMPS/2 mode, if possible. This gives
449 * us access to the mousewheel, etc. Returns zero if
450 * writes to device failed, but you still need to query the
451 * device to see which mode it's actually in.
453 static int set_imps2_mode(int fd)
455 /* If you wanted to control the mouse mode (and we do :) ) ...
457 {0xf3,200,0xf3,100,0xf3,80}
461 Uint8 set_imps2[] = {0xf3, 200, 0xf3, 100, 0xf3, 80};
462 /*Uint8 reset = 0xff;*/
467 if ( write(fd, &set_imps2, sizeof(set_imps2)) == sizeof(set_imps2) ) {
468 /* Don't reset it, that'll clear IMPS/2 mode on some mice
469 if (write(fd, &reset, sizeof (reset)) == sizeof (reset) ) {
475 /* Get rid of any chatter from the above */
480 while ( select(fd+1, &fdset, 0, 0, &tv) > 0 ) {
482 read(fd, temp, sizeof(temp));
489 /* Returns true if the mouse uses the IMPS/2 protocol */
490 static int detect_imps2(int fd)
496 if ( SDL_getenv("SDL_MOUSEDEV_IMPS2") ) {
500 Uint8 query_ps2 = 0xF2;
504 /* Get rid of any mouse motion noise */
509 while ( select(fd+1, &fdset, 0, 0, &tv) > 0 ) {
511 read(fd, temp, sizeof(temp));
514 /* Query for the type of mouse protocol */
515 if ( write(fd, &query_ps2, sizeof (query_ps2)) == sizeof (query_ps2)) {
518 /* Get the mouse protocol response */
524 if ( select(fd+1, &fdset, 0, 0, &tv) < 1 ) {
527 } while ( (read(fd, &ch, sizeof (ch)) == sizeof (ch)) &&
528 ((ch == 0xFA) || (ch == 0xAA)) );
530 /* Experimental values (Logitech wheelmouse) */
532 fprintf(stderr, "Last mouse mode: 0x%x\n", ch);
534 if ( (ch == 3) || (ch == 4) ) {
542 int FB_OpenMouse(_THIS)
545 const char *mousedev;
546 const char *mousedrv;
548 mousedrv = SDL_getenv("SDL_MOUSEDRV");
549 mousedev = SDL_getenv("SDL_MOUSEDEV");
553 if ( mousedrv && (SDL_strcmp(mousedrv, "TSLIB") == 0) ) {
554 if (mousedev == NULL) mousedev = SDL_getenv("TSLIB_TSDEVICE");
555 if (mousedev != NULL) {
556 ts_dev = ts_open(mousedev, 1);
557 if ((ts_dev != NULL) && (ts_config(ts_dev) >= 0)) {
559 fprintf(stderr, "Using tslib touchscreen\n");
561 mouse_drv = MOUSE_TSLIB;
562 mouse_fd = ts_fd(ts_dev);
566 mouse_drv = MOUSE_NONE;
569 #endif /* SDL_INPUT_TSLIB */
571 /* ELO TOUCHSCREEN SUPPORT */
573 if ( mousedrv && (SDL_strcmp(mousedrv, "ELO") == 0) ) {
574 mouse_fd = open(mousedev, O_RDWR);
575 if ( mouse_fd >= 0 ) {
576 if(eloInitController(mouse_fd)) {
578 fprintf(stderr, "Using ELO touchscreen\n");
580 mouse_drv = MOUSE_ELO;
584 else if ( mouse_fd < 0 ) {
585 mouse_drv = MOUSE_NONE;
593 if ( mousedev == NULL ) {
594 /* FIXME someday... allow multiple mice in this driver */
595 static const char *ps2mice[] = {
596 "/dev/input/mice", "/dev/usbmouse", "/dev/psaux", NULL
598 /* First try to use GPM in repeater mode */
599 if ( mouse_fd < 0 ) {
601 if ( gpm_available(proto, SDL_arraysize(proto)) ) {
602 mouse_fd = open(GPM_NODE_FIFO, O_RDONLY, 0);
603 if ( mouse_fd >= 0 ) {
604 if ( SDL_strcmp(proto, "msc") == 0 ) {
605 mouse_drv = MOUSE_MSC;
606 } else if ( SDL_strcmp(proto, "ps2") == 0 ) {
607 mouse_drv = MOUSE_PS2;
608 } else if ( SDL_strcmp(proto, "imps2") == 0 ) {
609 mouse_drv = MOUSE_IMPS2;
610 } else if ( SDL_strcmp(proto, "ms") == 0 ||
611 SDL_strcmp(proto, "bare") == 0 ) {
612 mouse_drv = MOUSE_MS;
613 } else if ( SDL_strcmp(proto, "bm") == 0 ) {
614 mouse_drv = MOUSE_BM;
616 /* Unknown protocol... */
618 fprintf(stderr, "GPM mouse using unknown protocol = %s\n", proto);
625 if ( mouse_fd >= 0 ) {
626 fprintf(stderr, "Using GPM mouse, protocol = %s\n", proto);
628 #endif /* DEBUG_MOUSE */
631 /* Now try to use a modern PS/2 mouse */
632 for ( i=0; (mouse_fd < 0) && ps2mice[i]; ++i ) {
633 mouse_fd = open(ps2mice[i], O_RDWR, 0);
635 mouse_fd = open(ps2mice[i], O_RDONLY, 0);
638 /* rcg06112001 Attempt to set IMPS/2 mode */
639 set_imps2_mode(mouse_fd);
640 if (detect_imps2(mouse_fd)) {
642 fprintf(stderr, "Using IMPS2 mouse\n");
644 mouse_drv = MOUSE_IMPS2;
647 fprintf(stderr, "Using PS2 mouse\n");
649 mouse_drv = MOUSE_PS2;
653 /* Next try to use a PPC ADB port mouse */
654 if ( mouse_fd < 0 ) {
655 mouse_fd = open("/dev/adbmouse", O_RDONLY, 0);
656 if ( mouse_fd >= 0 ) {
658 fprintf(stderr, "Using ADB mouse\n");
660 mouse_drv = MOUSE_BM;
664 /* Default to a serial Microsoft mouse */
665 if ( mouse_fd < 0 ) {
666 if ( mousedev == NULL ) {
667 mousedev = "/dev/mouse";
669 mouse_fd = open(mousedev, O_RDONLY, 0);
670 if ( mouse_fd >= 0 ) {
671 struct termios mouse_termios;
673 /* Set the sampling speed to 1200 baud */
674 tcgetattr(mouse_fd, &mouse_termios);
675 mouse_termios.c_iflag = IGNBRK | IGNPAR;
676 mouse_termios.c_oflag = 0;
677 mouse_termios.c_lflag = 0;
678 mouse_termios.c_line = 0;
679 mouse_termios.c_cc[VTIME] = 0;
680 mouse_termios.c_cc[VMIN] = 1;
681 mouse_termios.c_cflag = CREAD | CLOCAL | HUPCL;
682 mouse_termios.c_cflag |= CS8;
683 mouse_termios.c_cflag |= B1200;
684 tcsetattr(mouse_fd, TCSAFLUSH, &mouse_termios);
685 if ( mousedrv && (SDL_strcmp(mousedrv, "PS2") == 0) ) {
687 fprintf(stderr, "Using (user specified) PS2 mouse on %s\n", mousedev);
689 mouse_drv = MOUSE_PS2;
692 fprintf(stderr, "Using (default) MS mouse on %s\n", mousedev);
694 mouse_drv = MOUSE_MS;
698 if ( mouse_fd < 0 ) {
699 mouse_drv = MOUSE_NONE;
704 static int posted = 0;
706 void FB_vgamousecallback(int button, int relative, int dx, int dy)
708 int button_1, button_3;
715 posted += SDL_PrivateMouseMotion(0, relative, dx, dy);
718 /* Swap button 1 and 3 */
719 button_1 = (button & 0x04) >> 2;
720 button_3 = (button & 0x01) << 2;
722 button |= (button_1|button_3);
724 /* See what changed */
725 button_state = SDL_GetMouseState(NULL, NULL);
726 state_changed = button_state ^ button;
727 for ( i=0; i<8; ++i ) {
728 if ( state_changed & (1<<i) ) {
729 if ( button & (1<<i) ) {
732 state = SDL_RELEASED;
734 posted += SDL_PrivateMouseButton(state, i+1, 0, 0);
739 /* Handle input from tslib */
741 static void handle_tslib(_THIS)
743 struct ts_sample sample;
746 while (ts_read(ts_dev, &sample, 1) > 0) {
747 button = (sample.pressure > 0) ? 1 : 0;
748 button <<= 2; /* must report it as button 3 */
749 FB_vgamousecallback(button, 0, sample.x, sample.y);
753 #endif /* SDL_INPUT_TSLIB */
755 /* For now, use MSC, PS/2, and MS protocols
756 Driver adapted from the SVGAlib mouse driver code (taken from gpm, etc.)
758 static void handle_mouse(_THIS)
760 static int start = 0;
761 static unsigned char mousebuf[BUFSIZ];
762 static int relative = 1;
770 /* Figure out the mouse packet size */
774 read(mouse_fd, mousebuf, BUFSIZ);
788 /* try to read the next packet */
789 if(eloReadPosition(this, mouse_fd, &dx, &dy, &button, &realx, &realy)) {
790 button = (button & 0x01) << 2;
791 FB_vgamousecallback(button, 0, dx, dy);
793 return; /* nothing left to do */
798 return; /* nothing left to do */
805 /* Special handling for the quite sensitive ELO controller */
806 if (mouse_drv == MOUSE_ELO) {
810 /* Read as many packets as possible */
811 nread = read(mouse_fd, &mousebuf[start], BUFSIZ-start);
817 fprintf(stderr, "Read %d bytes from mouse, start = %d\n", nread, start);
819 for ( i=0; i<(nread-(packetsize-1)); i += packetsize ) {
824 /* MSC protocol has 0x80 in high byte */
825 if ( (mousebuf[i] & 0xF8) != 0x80 ) {
826 /* Go to next byte */
830 /* Get current mouse state */
831 button = (~mousebuf[i]) & 0x07;
832 dx = (signed char)(mousebuf[i+1]) +
833 (signed char)(mousebuf[i+3]);
834 dy = -((signed char)(mousebuf[i+2]) +
835 (signed char)(mousebuf[i+4]));
838 /* PS/2 protocol has nothing in high byte */
839 if ( (mousebuf[i] & 0xC0) != 0 ) {
840 /* Go to next byte */
844 /* Get current mouse state */
845 button = (mousebuf[i] & 0x04) >> 1 | /*Middle*/
846 (mousebuf[i] & 0x02) >> 1 | /*Right*/
847 (mousebuf[i] & 0x01) << 2; /*Left*/
848 dx = (mousebuf[i] & 0x10) ?
849 mousebuf[i+1] - 256 : mousebuf[i+1];
850 dy = (mousebuf[i] & 0x20) ?
851 -(mousebuf[i+2] - 256) : -mousebuf[i+2];
854 /* Get current mouse state */
855 button = (mousebuf[i] & 0x04) >> 1 | /*Middle*/
856 (mousebuf[i] & 0x02) >> 1 | /*Right*/
857 (mousebuf[i] & 0x01) << 2 | /*Left*/
858 (mousebuf[i] & 0x40) >> 3 | /* 4 */
859 (mousebuf[i] & 0x80) >> 3; /* 5 */
860 dx = (mousebuf[i] & 0x10) ?
861 mousebuf[i+1] - 256 : mousebuf[i+1];
862 dy = (mousebuf[i] & 0x20) ?
863 -(mousebuf[i+2] - 256) : -mousebuf[i+2];
864 switch (mousebuf[i+3]&0x0F) {
865 case 0x0E: /* DX = +1 */
866 case 0x02: /* DX = -1 */
868 case 0x0F: /* DY = +1 (map button 4) */
869 FB_vgamousecallback(button | (1<<3),
872 case 0x01: /* DY = -1 (map button 5) */
873 FB_vgamousecallback(button | (1<<4),
879 /* Microsoft protocol has 0x40 in high byte */
880 if ( (mousebuf[i] & 0x40) != 0x40 ) {
881 /* Go to next byte */
885 /* Get current mouse state */
886 button = ((mousebuf[i] & 0x20) >> 3) |
887 ((mousebuf[i] & 0x10) >> 4);
888 dx = (signed char)(((mousebuf[i] & 0x03) << 6) |
889 (mousebuf[i + 1] & 0x3F));
890 dy = (signed char)(((mousebuf[i] & 0x0C) << 4) |
891 (mousebuf[i + 2] & 0x3F));
894 /* BusMouse protocol has 0xF8 in high byte */
895 if ( (mousebuf[i] & 0xF8) != 0x80 ) {
896 /* Go to next byte */
900 /* Get current mouse state */
901 button = (~mousebuf[i]) & 0x07;
902 dx = (signed char)mousebuf[i+1];
903 dy = -(signed char)mousebuf[i+2];
911 FB_vgamousecallback(button, relative, dx, dy);
914 SDL_memcpy(mousebuf, &mousebuf[i], (nread-i));
922 /* Handle switching to another VC, returns when our VC is back */
923 static void switch_vt_prep(_THIS)
925 SDL_Surface *screen = SDL_VideoSurface;
927 SDL_PrivateAppActive(0, (SDL_APPACTIVE|SDL_APPINPUTFOCUS|SDL_APPMOUSEFOCUS));
929 /* Save the contents of the screen, and go to text mode */
931 screen_arealen = ((screen->h + (2*this->offset_y)) * screen->pitch);
932 screen_contents = (Uint8 *)SDL_malloc(screen_arealen);
933 if ( screen_contents ) {
934 SDL_memcpy(screen_contents, screen->pixels, screen_arealen);
936 FB_SavePaletteTo(this, 256, screen_palette);
937 ioctl(console_fd, FBIOGET_VSCREENINFO, &screen_vinfo);
938 ioctl(keyboard_fd, KDSETMODE, KD_TEXT);
939 ioctl(keyboard_fd, VT_UNLOCKSWITCH, 1);
941 static void switch_vt_done(_THIS)
943 SDL_Surface *screen = SDL_VideoSurface;
945 /* Restore graphics mode and the contents of the screen */
946 ioctl(keyboard_fd, VT_LOCKSWITCH, 1);
947 ioctl(keyboard_fd, KDSETMODE, KD_GRAPHICS);
948 ioctl(console_fd, FBIOPUT_VSCREENINFO, &screen_vinfo);
949 FB_RestorePaletteFrom(this, 256, screen_palette);
950 if ( screen_contents ) {
951 SDL_memcpy(screen->pixels, screen_contents, screen_arealen);
952 SDL_free(screen_contents);
953 screen_contents = NULL;
956 /* Get updates to the shadow surface while switched away */
957 if ( SDL_ShadowSurface ) {
958 SDL_UpdateRect(SDL_ShadowSurface, 0, 0, 0, 0);
961 SDL_PrivateAppActive(1, (SDL_APPACTIVE|SDL_APPINPUTFOCUS|SDL_APPMOUSEFOCUS));
963 static void switch_vt(_THIS, unsigned short which)
965 struct vt_stat vtstate;
967 /* Figure out whether or not we're switching to a new console */
968 if ( (ioctl(keyboard_fd, VT_GETSTATE, &vtstate) < 0) ||
969 (which == vtstate.v_active) ) {
973 /* New console, switch to it */
975 switch_vt_prep(this);
976 if ( ioctl(keyboard_fd, VT_ACTIVATE, which) == 0 ) {
977 ioctl(keyboard_fd, VT_WAITACTIVE, which);
980 switch_vt_done(this);
985 static void handle_keyboard(_THIS)
987 unsigned char keybuf[BUFSIZ];
993 nread = read(keyboard_fd, keybuf, BUFSIZ);
994 for ( i=0; i<nread; ++i ) {
995 scancode = keybuf[i] & 0x7F;
996 if ( keybuf[i] & 0x80 ) {
997 pressed = SDL_RELEASED;
999 pressed = SDL_PRESSED;
1001 TranslateKey(scancode, &keysym);
1002 /* Handle Ctrl-Alt-FN for vt switch */
1003 switch (keysym.sym) {
1016 if ( (SDL_GetModState() & KMOD_CTRL) &&
1017 (SDL_GetModState() & KMOD_ALT) ) {
1019 switch_vt(this, (keysym.sym-SDLK_F1)+1);
1023 /* Fall through to normal processing */
1025 posted += SDL_PrivateKeyboard(pressed, &keysym);
1031 void FB_PumpEvents(_THIS)
1035 static struct timeval zero;
1038 if ( switched_away ) {
1039 struct vt_stat vtstate;
1041 SDL_mutexP(hw_lock);
1042 if ( (ioctl(keyboard_fd, VT_GETSTATE, &vtstate) == 0) &&
1043 vtstate.v_active == current_vt ) {
1045 switch_vt_done(this);
1047 SDL_mutexV(hw_lock);
1054 if ( keyboard_fd >= 0 ) {
1055 FD_SET(keyboard_fd, &fdset);
1056 if ( max_fd < keyboard_fd ) {
1057 max_fd = keyboard_fd;
1060 if ( mouse_fd >= 0 ) {
1061 FD_SET(mouse_fd, &fdset);
1062 if ( max_fd < mouse_fd ) {
1066 if ( select(max_fd+1, &fdset, NULL, NULL, &zero) > 0 ) {
1067 if ( keyboard_fd >= 0 ) {
1068 if ( FD_ISSET(keyboard_fd, &fdset) ) {
1069 handle_keyboard(this);
1072 if ( mouse_fd >= 0 ) {
1073 if ( FD_ISSET(mouse_fd, &fdset) ) {
1081 void FB_InitOSKeymap(_THIS)
1085 /* Initialize the Linux key translation table */
1087 /* First get the ascii keys and others not well handled */
1088 for (i=0; i<SDL_arraysize(keymap); ++i) {
1090 /* These aren't handled by the x86 kernel keymapping (?) */
1091 case SCANCODE_PRINTSCREEN:
1092 keymap[i] = SDLK_PRINT;
1094 case SCANCODE_BREAK:
1095 keymap[i] = SDLK_BREAK;
1097 case SCANCODE_BREAK_ALTERNATIVE:
1098 keymap[i] = SDLK_PAUSE;
1100 case SCANCODE_LEFTSHIFT:
1101 keymap[i] = SDLK_LSHIFT;
1103 case SCANCODE_RIGHTSHIFT:
1104 keymap[i] = SDLK_RSHIFT;
1106 case SCANCODE_LEFTCONTROL:
1107 keymap[i] = SDLK_LCTRL;
1109 case SCANCODE_RIGHTCONTROL:
1110 keymap[i] = SDLK_RCTRL;
1112 case SCANCODE_RIGHTWIN:
1113 keymap[i] = SDLK_RSUPER;
1115 case SCANCODE_LEFTWIN:
1116 keymap[i] = SDLK_LSUPER;
1118 case SCANCODE_LEFTALT:
1119 keymap[i] = SDLK_LALT;
1121 case SCANCODE_RIGHTALT:
1122 keymap[i] = SDLK_RALT;
1125 keymap[i] = SDLK_MENU;
1127 /* this should take care of all standard ascii keys */
1129 keymap[i] = KVAL(vga_keymap[0][i]);
1133 for (i=0; i<SDL_arraysize(keymap); ++i) {
1134 switch(keymap_temp[i]) {
1135 case K_F1: keymap[i] = SDLK_F1; break;
1136 case K_F2: keymap[i] = SDLK_F2; break;
1137 case K_F3: keymap[i] = SDLK_F3; break;
1138 case K_F4: keymap[i] = SDLK_F4; break;
1139 case K_F5: keymap[i] = SDLK_F5; break;
1140 case K_F6: keymap[i] = SDLK_F6; break;
1141 case K_F7: keymap[i] = SDLK_F7; break;
1142 case K_F8: keymap[i] = SDLK_F8; break;
1143 case K_F9: keymap[i] = SDLK_F9; break;
1144 case K_F10: keymap[i] = SDLK_F10; break;
1145 case K_F11: keymap[i] = SDLK_F11; break;
1146 case K_F12: keymap[i] = SDLK_F12; break;
1148 case K_DOWN: keymap[i] = SDLK_DOWN; break;
1149 case K_LEFT: keymap[i] = SDLK_LEFT; break;
1150 case K_RIGHT: keymap[i] = SDLK_RIGHT; break;
1151 case K_UP: keymap[i] = SDLK_UP; break;
1153 case K_P0: keymap[i] = SDLK_KP0; break;
1154 case K_P1: keymap[i] = SDLK_KP1; break;
1155 case K_P2: keymap[i] = SDLK_KP2; break;
1156 case K_P3: keymap[i] = SDLK_KP3; break;
1157 case K_P4: keymap[i] = SDLK_KP4; break;
1158 case K_P5: keymap[i] = SDLK_KP5; break;
1159 case K_P6: keymap[i] = SDLK_KP6; break;
1160 case K_P7: keymap[i] = SDLK_KP7; break;
1161 case K_P8: keymap[i] = SDLK_KP8; break;
1162 case K_P9: keymap[i] = SDLK_KP9; break;
1163 case K_PPLUS: keymap[i] = SDLK_KP_PLUS; break;
1164 case K_PMINUS: keymap[i] = SDLK_KP_MINUS; break;
1165 case K_PSTAR: keymap[i] = SDLK_KP_MULTIPLY; break;
1166 case K_PSLASH: keymap[i] = SDLK_KP_DIVIDE; break;
1167 case K_PENTER: keymap[i] = SDLK_KP_ENTER; break;
1168 case K_PDOT: keymap[i] = SDLK_KP_PERIOD; break;
1170 case K_SHIFT: if ( keymap[i] != SDLK_RSHIFT )
1171 keymap[i] = SDLK_LSHIFT;
1173 case K_SHIFTL: keymap[i] = SDLK_LSHIFT; break;
1174 case K_SHIFTR: keymap[i] = SDLK_RSHIFT; break;
1175 case K_CTRL: if ( keymap[i] != SDLK_RCTRL )
1176 keymap[i] = SDLK_LCTRL;
1178 case K_CTRLL: keymap[i] = SDLK_LCTRL; break;
1179 case K_CTRLR: keymap[i] = SDLK_RCTRL; break;
1180 case K_ALT: keymap[i] = SDLK_LALT; break;
1181 case K_ALTGR: keymap[i] = SDLK_RALT; break;
1183 case K_INSERT: keymap[i] = SDLK_INSERT; break;
1184 case K_REMOVE: keymap[i] = SDLK_DELETE; break;
1185 case K_PGUP: keymap[i] = SDLK_PAGEUP; break;
1186 case K_PGDN: keymap[i] = SDLK_PAGEDOWN; break;
1187 case K_FIND: keymap[i] = SDLK_HOME; break;
1188 case K_SELECT: keymap[i] = SDLK_END; break;
1190 case K_NUM: keymap[i] = SDLK_NUMLOCK; break;
1191 case K_CAPS: keymap[i] = SDLK_CAPSLOCK; break;
1193 case K_F13: keymap[i] = SDLK_PRINT; break;
1194 case K_HOLD: keymap[i] = SDLK_SCROLLOCK; break;
1195 case K_PAUSE: keymap[i] = SDLK_PAUSE; break;
1197 case 127: keymap[i] = SDLK_BACKSPACE; break;
1204 static SDL_keysym *TranslateKey(int scancode, SDL_keysym *keysym)
1206 /* Set the keysym information */
1207 keysym->scancode = scancode;
1208 keysym->sym = keymap[scancode];
1209 keysym->mod = KMOD_NONE;
1211 /* If UNICODE is on, get the UNICODE value for the key */
1212 keysym->unicode = 0;
1213 if ( SDL_TranslateUNICODE ) {
1217 modstate = SDL_GetModState();
1219 if ( modstate & KMOD_SHIFT ) {
1220 map |= (1<<KG_SHIFT);
1222 if ( modstate & KMOD_CTRL ) {
1223 map |= (1<<KG_CTRL);
1225 if ( modstate & KMOD_LALT ) {
1228 if ( modstate & KMOD_RALT ) {
1229 map |= (1<<KG_ALTGR);
1231 if ( KTYP(vga_keymap[map][scancode]) == KT_LETTER ) {
1232 if ( modstate & KMOD_CAPS ) {
1233 map ^= (1<<KG_SHIFT);
1236 if ( KTYP(vga_keymap[map][scancode]) == KT_PAD ) {
1237 if ( modstate & KMOD_NUM ) {
1238 keysym->unicode=KVAL(vga_keymap[map][scancode]);
1241 keysym->unicode = KVAL(vga_keymap[map][scancode]);