src/video/fbcon/SDL_fbevents.c
author Sam Lantinga <slouken@lokigames.com>
Thu, 26 Apr 2001 16:45:43 +0000
changeset 0 74212992fb08
child 4 4f6c5f021323
permissions -rw-r--r--
Initial revision
slouken@0
     1
/*
slouken@0
     2
	SDL - Simple DirectMedia Layer
slouken@0
     3
	Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
slouken@0
     4
slouken@0
     5
	This library is free software; you can redistribute it and/or
slouken@0
     6
	modify it under the terms of the GNU Library General Public
slouken@0
     7
	License as published by the Free Software Foundation; either
slouken@0
     8
	version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
	This library is distributed in the hope that it will be useful,
slouken@0
    11
	but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
	Library General Public License for more details.
slouken@0
    14
slouken@0
    15
	You should have received a copy of the GNU Library General Public
slouken@0
    16
	License along with this library; if not, write to the Free
slouken@0
    17
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
	Sam Lantinga
slouken@0
    20
	slouken@devolution.com
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
/* Handle the event stream, converting console events into SDL events */
slouken@0
    29
slouken@0
    30
#include <sys/types.h>
slouken@0
    31
#include <sys/time.h>
slouken@0
    32
#include <sys/ioctl.h>
slouken@0
    33
#include <stdlib.h>
slouken@0
    34
#include <stdio.h>
slouken@0
    35
#include <unistd.h>
slouken@0
    36
#include <fcntl.h>
slouken@0
    37
#include <string.h>
slouken@0
    38
#include <errno.h>
slouken@0
    39
#include <limits.h>
slouken@0
    40
slouken@0
    41
/* For parsing /proc */
slouken@0
    42
#include <dirent.h>
slouken@0
    43
#include <ctype.h>
slouken@0
    44
slouken@0
    45
#include <linux/vt.h>
slouken@0
    46
#include <linux/kd.h>
slouken@0
    47
#include <linux/keyboard.h>
slouken@0
    48
slouken@0
    49
#include "SDL.h"
slouken@0
    50
#include "SDL_mutex.h"
slouken@0
    51
#include "SDL_sysevents.h"
slouken@0
    52
#include "SDL_sysvideo.h"
slouken@0
    53
#include "SDL_events_c.h"
slouken@0
    54
#include "SDL_fbvideo.h"
slouken@0
    55
#include "SDL_fbevents_c.h"
slouken@0
    56
#include "SDL_fbkeys.h"
slouken@0
    57
slouken@0
    58
#include "SDL_fbelo.h"
slouken@0
    59
slouken@0
    60
#ifndef GPM_NODE_FIFO
slouken@0
    61
#define GPM_NODE_FIFO	"/dev/gpmdata"
slouken@0
    62
#endif
slouken@0
    63
slouken@0
    64
slouken@0
    65
/* The translation tables from a console scancode to a SDL keysym */
slouken@0
    66
#define NUM_VGAKEYMAPS	(1<<KG_CAPSSHIFT)
slouken@0
    67
static Uint16 vga_keymap[NUM_VGAKEYMAPS][NR_KEYS];
slouken@0
    68
static SDLKey keymap[128];
slouken@0
    69
static Uint16 keymap_temp[128]; /* only used at startup */
slouken@0
    70
static SDL_keysym *TranslateKey(int scancode, SDL_keysym *keysym);
slouken@0
    71
slouken@0
    72
/* Ugh, we have to duplicate the kernel's keysym mapping code...
slouken@0
    73
   Oh, it's not so bad. :-)
slouken@0
    74
slouken@0
    75
   FIXME: Add keyboard LED handling code
slouken@0
    76
 */
slouken@0
    77
static void FB_vgainitkeymaps(int fd)
slouken@0
    78
{
slouken@0
    79
	struct kbentry entry;
slouken@0
    80
	int map, i;
slouken@0
    81
slouken@0
    82
	/* Don't do anything if we are passed a closed keyboard */
slouken@0
    83
	if ( fd < 0 ) {
slouken@0
    84
		return;
slouken@0
    85
	}
slouken@0
    86
slouken@0
    87
	/* Load all the keysym mappings */
slouken@0
    88
	for ( map=0; map<NUM_VGAKEYMAPS; ++map ) {
slouken@0
    89
		memset(vga_keymap[map], 0, NR_KEYS*sizeof(Uint16));
slouken@0
    90
		for ( i=0; i<NR_KEYS; ++i ) {
slouken@0
    91
			entry.kb_table = map;
slouken@0
    92
			entry.kb_index = i;
slouken@0
    93
			if ( ioctl(fd, KDGKBENT, &entry) == 0 ) {
slouken@0
    94
				/* fill keytemp. This replaces SDL_fbkeys.h */
slouken@0
    95
				if ( (map == 0) && (i<128) ) {
slouken@0
    96
					keymap_temp[i] = entry.kb_value;
slouken@0
    97
				}
slouken@0
    98
				/* The "Enter" key is a special case */
slouken@0
    99
				if ( entry.kb_value == K_ENTER ) {
slouken@0
   100
					entry.kb_value = K(KT_ASCII,13);
slouken@0
   101
				}
slouken@0
   102
				/* Handle numpad specially as well */
slouken@0
   103
				if ( KTYP(entry.kb_value) == KT_PAD ) {
slouken@0
   104
					switch ( entry.kb_value ) {
slouken@0
   105
					case K_P0:
slouken@0
   106
					case K_P1:
slouken@0
   107
					case K_P2:
slouken@0
   108
					case K_P3:
slouken@0
   109
					case K_P4:
slouken@0
   110
					case K_P5:
slouken@0
   111
					case K_P6:
slouken@0
   112
					case K_P7:
slouken@0
   113
					case K_P8:
slouken@0
   114
					case K_P9:
slouken@0
   115
						vga_keymap[map][i]=entry.kb_value;
slouken@0
   116
						vga_keymap[map][i]+= '0';
slouken@0
   117
						break;
slouken@0
   118
										case K_PPLUS:
slouken@0
   119
						vga_keymap[map][i]=K(KT_ASCII,'+');
slouken@0
   120
						break;
slouken@0
   121
										case K_PMINUS:
slouken@0
   122
						vga_keymap[map][i]=K(KT_ASCII,'-');
slouken@0
   123
						break;
slouken@0
   124
										case K_PSTAR:
slouken@0
   125
						vga_keymap[map][i]=K(KT_ASCII,'*');
slouken@0
   126
						break;
slouken@0
   127
										case K_PSLASH:
slouken@0
   128
						vga_keymap[map][i]=K(KT_ASCII,'/');
slouken@0
   129
						break;
slouken@0
   130
										case K_PENTER:
slouken@0
   131
						vga_keymap[map][i]=K(KT_ASCII,'\r');
slouken@0
   132
						break;
slouken@0
   133
										case K_PCOMMA:
slouken@0
   134
						vga_keymap[map][i]=K(KT_ASCII,',');
slouken@0
   135
						break;
slouken@0
   136
										case K_PDOT:
slouken@0
   137
						vga_keymap[map][i]=K(KT_ASCII,'.');
slouken@0
   138
						break;
slouken@0
   139
					default:
slouken@0
   140
						break;
slouken@0
   141
					}
slouken@0
   142
				}
slouken@0
   143
				/* Do the normal key translation */
slouken@0
   144
				if ( (KTYP(entry.kb_value) == KT_LATIN) ||
slouken@0
   145
					 (KTYP(entry.kb_value) == KT_ASCII) ||
slouken@0
   146
					 (KTYP(entry.kb_value) == KT_LETTER) ) {
slouken@0
   147
					vga_keymap[map][i] = entry.kb_value;
slouken@0
   148
				}
slouken@0
   149
			}
slouken@0
   150
		}
slouken@0
   151
	}
slouken@0
   152
}
slouken@0
   153
slouken@0
   154
int FB_InGraphicsMode(_THIS)
slouken@0
   155
{
slouken@0
   156
	return((keyboard_fd >= 0) && (saved_kbd_mode >= 0));
slouken@0
   157
}
slouken@0
   158
slouken@0
   159
int FB_EnterGraphicsMode(_THIS)
slouken@0
   160
{
slouken@0
   161
	struct termios keyboard_termios;
slouken@0
   162
slouken@0
   163
	/* Set medium-raw keyboard mode */
slouken@0
   164
	if ( (keyboard_fd >= 0) && !FB_InGraphicsMode(this) ) {
slouken@0
   165
slouken@0
   166
		/* Switch to the correct virtual terminal */
slouken@0
   167
		if ( current_vt > 0 ) {
slouken@0
   168
			struct vt_stat vtstate;
slouken@0
   169
slouken@0
   170
			if ( ioctl(keyboard_fd, VT_GETSTATE, &vtstate) == 0 ) {
slouken@0
   171
				saved_vt = vtstate.v_active;
slouken@0
   172
			}
slouken@0
   173
			if ( ioctl(keyboard_fd, VT_ACTIVATE, current_vt) == 0 ) {
slouken@0
   174
				ioctl(keyboard_fd, VT_WAITACTIVE, current_vt);
slouken@0
   175
			}
slouken@0
   176
		}
slouken@0
   177
slouken@0
   178
		/* Set the terminal input mode */
slouken@0
   179
		if ( tcgetattr(keyboard_fd, &saved_kbd_termios) < 0 ) {
slouken@0
   180
			SDL_SetError("Unable to get terminal attributes");
slouken@0
   181
			if ( keyboard_fd > 0 ) {
slouken@0
   182
				close(keyboard_fd);
slouken@0
   183
			}
slouken@0
   184
			keyboard_fd = -1;
slouken@0
   185
			return(-1);
slouken@0
   186
		}
slouken@0
   187
		if ( ioctl(keyboard_fd, KDGKBMODE, &saved_kbd_mode) < 0 ) {
slouken@0
   188
			SDL_SetError("Unable to get current keyboard mode");
slouken@0
   189
			if ( keyboard_fd > 0 ) {
slouken@0
   190
				close(keyboard_fd);
slouken@0
   191
			}
slouken@0
   192
			keyboard_fd = -1;
slouken@0
   193
			return(-1);
slouken@0
   194
		}
slouken@0
   195
		keyboard_termios = saved_kbd_termios;
slouken@0
   196
		keyboard_termios.c_lflag &= ~(ICANON | ECHO | ISIG);
slouken@0
   197
		keyboard_termios.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
slouken@0
   198
		keyboard_termios.c_cc[VMIN] = 0;
slouken@0
   199
		keyboard_termios.c_cc[VTIME] = 0;
slouken@0
   200
		if (tcsetattr(keyboard_fd, TCSAFLUSH, &keyboard_termios) < 0) {
slouken@0
   201
			FB_CloseKeyboard(this);
slouken@0
   202
			SDL_SetError("Unable to set terminal attributes");
slouken@0
   203
			return(-1);
slouken@0
   204
		}
slouken@0
   205
		/* This will fail if we aren't root or this isn't our tty */
slouken@0
   206
		if ( ioctl(keyboard_fd, KDSKBMODE, K_MEDIUMRAW) < 0 ) {
slouken@0
   207
			FB_CloseKeyboard(this);
slouken@0
   208
			SDL_SetError("Unable to set keyboard in raw mode");
slouken@0
   209
			return(-1);
slouken@0
   210
		}
slouken@0
   211
		if ( ioctl(keyboard_fd, KDSETMODE, KD_GRAPHICS) < 0 ) {
slouken@0
   212
			FB_CloseKeyboard(this);
slouken@0
   213
			SDL_SetError("Unable to set keyboard in graphics mode");
slouken@0
   214
			return(-1);
slouken@0
   215
		}
slouken@0
   216
	}
slouken@0
   217
	return(keyboard_fd);
slouken@0
   218
}
slouken@0
   219
slouken@0
   220
void FB_LeaveGraphicsMode(_THIS)
slouken@0
   221
{
slouken@0
   222
	if ( FB_InGraphicsMode(this) ) {
slouken@0
   223
		ioctl(keyboard_fd, KDSETMODE, KD_TEXT);
slouken@0
   224
		ioctl(keyboard_fd, KDSKBMODE, saved_kbd_mode);
slouken@0
   225
		tcsetattr(keyboard_fd, TCSAFLUSH, &saved_kbd_termios);
slouken@0
   226
		saved_kbd_mode = -1;
slouken@0
   227
slouken@0
   228
		/* Head back over to the original virtual terminal */
slouken@0
   229
		if ( saved_vt > 0 ) {
slouken@0
   230
			ioctl(keyboard_fd, VT_ACTIVATE, saved_vt);
slouken@0
   231
		}
slouken@0
   232
	}
slouken@0
   233
}
slouken@0
   234
slouken@0
   235
void FB_CloseKeyboard(_THIS)
slouken@0
   236
{
slouken@0
   237
	if ( keyboard_fd >= 0 ) {
slouken@0
   238
		FB_LeaveGraphicsMode(this);
slouken@0
   239
		if ( keyboard_fd > 0 ) {
slouken@0
   240
			close(keyboard_fd);
slouken@0
   241
		}
slouken@0
   242
	}
slouken@0
   243
	keyboard_fd = -1;
slouken@0
   244
}
slouken@0
   245
slouken@0
   246
int FB_OpenKeyboard(_THIS)
slouken@0
   247
{
slouken@0
   248
	/* Open only if not already opened */
slouken@0
   249
 	if ( keyboard_fd < 0 ) {
slouken@0
   250
		char *tty0[] = { "/dev/tty0", "/dev/vc/0", NULL };
slouken@0
   251
		char *vcs[] = { "/dev/vc/%d", "/dev/tty%d", NULL };
slouken@0
   252
		int i, tty0_fd;
slouken@0
   253
slouken@0
   254
		/* Try to query for a free virtual terminal */
slouken@0
   255
		tty0_fd = -1;
slouken@0
   256
		for ( i=0; tty0[i] && (tty0_fd < 0); ++i ) {
slouken@0
   257
			tty0_fd = open(tty0[i], O_WRONLY, 0);
slouken@0
   258
		}
slouken@0
   259
		if ( tty0_fd < 0 ) {
slouken@0
   260
			tty0_fd = dup(0); /* Maybe stdin is a VT? */
slouken@0
   261
		}
slouken@0
   262
		ioctl(tty0_fd, VT_OPENQRY, &current_vt);
slouken@0
   263
		close(tty0_fd);
slouken@0
   264
		if ( (geteuid() == 0) && (current_vt > 0) ) {
slouken@0
   265
			for ( i=0; vcs[i] && (keyboard_fd < 0); ++i ) {
slouken@0
   266
				char vtpath[12];
slouken@0
   267
slouken@0
   268
				sprintf(vtpath, vcs[i], current_vt);
slouken@0
   269
				keyboard_fd = open(vtpath, O_RDWR, 0);
slouken@0
   270
#ifdef DEBUG_KEYBOARD
slouken@0
   271
				fprintf(stderr, "vtpath = %s, fd = %d\n",
slouken@0
   272
					vtpath, keyboard_fd);
slouken@0
   273
#endif /* DEBUG_KEYBOARD */
slouken@0
   274
slouken@0
   275
				/* This needs to be our controlling tty
slouken@0
   276
				   so that the kernel ioctl() calls work
slouken@0
   277
				*/
slouken@0
   278
				if ( keyboard_fd >= 0 ) {
slouken@0
   279
					tty0_fd = open("/dev/tty", O_RDWR, 0);
slouken@0
   280
					if ( tty0_fd >= 0 ) {
slouken@0
   281
						ioctl(tty0_fd, TIOCNOTTY, 0);
slouken@0
   282
						close(tty0_fd);
slouken@0
   283
					}
slouken@0
   284
				}
slouken@0
   285
			}
slouken@0
   286
		}
slouken@0
   287
 		if ( keyboard_fd < 0 ) {
slouken@0
   288
			/* Last resort, maybe our tty is a usable VT */
slouken@0
   289
			current_vt = 0;
slouken@0
   290
			keyboard_fd = open("/dev/tty", O_RDWR);
slouken@0
   291
 		}
slouken@0
   292
#ifdef DEBUG_KEYBOARD
slouken@0
   293
		fprintf(stderr, "Current VT: %d\n", current_vt);
slouken@0
   294
#endif
slouken@0
   295
 		saved_kbd_mode = -1;
slouken@0
   296
slouken@0
   297
		/* Make sure that our input is a console terminal */
slouken@0
   298
		{ int dummy;
slouken@0
   299
		  if ( ioctl(keyboard_fd, KDGKBMODE, &dummy) < 0 ) {
slouken@0
   300
			close(keyboard_fd);
slouken@0
   301
			keyboard_fd = -1;
slouken@0
   302
			SDL_SetError("Unable to open a console terminal");
slouken@0
   303
		  }
slouken@0
   304
		}
slouken@0
   305
slouken@0
   306
		/* Set up keymap */
slouken@0
   307
		FB_vgainitkeymaps(keyboard_fd);
slouken@0
   308
 	}
slouken@0
   309
 	return(keyboard_fd);
slouken@0
   310
}
slouken@0
   311
slouken@0
   312
static enum {
slouken@0
   313
	MOUSE_NONE = -1,
slouken@0
   314
	MOUSE_GPM,	/* Note: GPM uses the MSC protocol */
slouken@0
   315
	MOUSE_PS2,
slouken@0
   316
	MOUSE_IMPS2,
slouken@0
   317
	MOUSE_MS,
slouken@0
   318
	MOUSE_BM,
slouken@0
   319
	MOUSE_ELO,
slouken@0
   320
	NUM_MOUSE_DRVS
slouken@0
   321
} mouse_drv = MOUSE_NONE;
slouken@0
   322
slouken@0
   323
void FB_CloseMouse(_THIS)
slouken@0
   324
{
slouken@0
   325
	if ( mouse_fd > 0 ) {
slouken@0
   326
		close(mouse_fd);
slouken@0
   327
	}
slouken@0
   328
	mouse_fd = -1;
slouken@0
   329
}
slouken@0
   330
slouken@0
   331
/* Returns processes listed in /proc with the desired name */
slouken@0
   332
static int find_pid(DIR *proc, const char *wanted_name)
slouken@0
   333
{
slouken@0
   334
	struct dirent *entry;
slouken@0
   335
	int pid;
slouken@0
   336
slouken@0
   337
	/* First scan proc for the gpm process */
slouken@0
   338
	pid = 0;
slouken@0
   339
	while ( (pid == 0) && ((entry=readdir(proc)) != NULL) ) {
slouken@0
   340
		if ( isdigit(entry->d_name[0]) ) {
slouken@0
   341
			FILE *status;
slouken@0
   342
			char path[PATH_MAX];
slouken@0
   343
			char name[PATH_MAX];
slouken@0
   344
slouken@0
   345
			sprintf(path, "/proc/%s/status", entry->d_name);
slouken@0
   346
			status=fopen(path, "r");
slouken@0
   347
			if ( status ) {
slouken@0
   348
				name[0] = '\0';
slouken@0
   349
				fscanf(status, "Name: %s", name);
slouken@0
   350
				if ( strcmp(name, wanted_name) == 0 ) {
slouken@0
   351
					pid = atoi(entry->d_name);
slouken@0
   352
				}
slouken@0
   353
				fclose(status);
slouken@0
   354
			}
slouken@0
   355
		}
slouken@0
   356
	}
slouken@0
   357
	return pid;
slouken@0
   358
}
slouken@0
   359
slouken@0
   360
/* Returns true if /dev/gpmdata is being written to by gpm */
slouken@0
   361
static int gpm_available(void)
slouken@0
   362
{
slouken@0
   363
	int available;
slouken@0
   364
	DIR *proc;
slouken@0
   365
	int pid;
slouken@0
   366
	int cmdline, len, arglen;
slouken@0
   367
	char path[PATH_MAX];
slouken@0
   368
	char args[PATH_MAX], *arg;
slouken@0
   369
slouken@0
   370
	/* Don't bother looking if the fifo isn't there */
slouken@0
   371
	if ( access(GPM_NODE_FIFO, F_OK) < 0 ) {
slouken@0
   372
		return(0);
slouken@0
   373
	}
slouken@0
   374
slouken@0
   375
	available = 0;
slouken@0
   376
	proc = opendir("/proc");
slouken@0
   377
	if ( proc ) {
slouken@0
   378
		while ( (pid=find_pid(proc, "gpm")) > 0 ) {
slouken@0
   379
			sprintf(path, "/proc/%d/cmdline", pid);
slouken@0
   380
			cmdline = open(path, O_RDONLY, 0);
slouken@0
   381
			if ( cmdline >= 0 ) {
slouken@0
   382
				len = read(cmdline, args, sizeof(args));
slouken@0
   383
				arg = args;
slouken@0
   384
				while ( len > 0 ) {
slouken@0
   385
					if ( strcmp(arg, "-R") == 0 ) {
slouken@0
   386
						available = 1;
slouken@0
   387
					}
slouken@0
   388
					arglen = strlen(arg)+1;
slouken@0
   389
					len -= arglen;
slouken@0
   390
					arg += arglen;
slouken@0
   391
				}
slouken@0
   392
				close(cmdline);
slouken@0
   393
			}
slouken@0
   394
		}
slouken@0
   395
		closedir(proc);
slouken@0
   396
	}
slouken@0
   397
	return available;
slouken@0
   398
}
slouken@0
   399
slouken@0
   400
/* Returns true if the mouse uses the IMPS/2 protocol */
slouken@0
   401
static int detect_imps2(int fd)
slouken@0
   402
{
slouken@0
   403
	int imps2;
slouken@0
   404
slouken@0
   405
	imps2 = 0;
slouken@0
   406
	if ( getenv("SDL_MOUSEDEV_IMPS2") ) {
slouken@0
   407
		imps2 = 1;
slouken@0
   408
	}
slouken@0
   409
	if ( ! imps2 ) {
slouken@0
   410
		unsigned char query_ps2 = 0xF2;
slouken@0
   411
		fd_set fdset;
slouken@0
   412
		struct timeval tv;
slouken@0
   413
slouken@0
   414
slouken@0
   415
		/* Get rid of any mouse motion noise */
slouken@0
   416
		FD_ZERO(&fdset);
slouken@0
   417
		FD_SET(fd, &fdset);
slouken@0
   418
		tv.tv_sec = 0;
slouken@0
   419
		tv.tv_usec = 0;
slouken@0
   420
		while ( select(fd+1, &fdset, 0, 0, &tv) > 0 ) {
slouken@0
   421
			char temp[32];
slouken@0
   422
			read(fd, temp, sizeof(temp));
slouken@0
   423
		}
slouken@0
   424
slouken@0
   425
		/* Query for the type of mouse protocol */
slouken@0
   426
		if ( write(fd, &query_ps2, 1) == 1 ) {
slouken@0
   427
			unsigned char ch = 0;
slouken@0
   428
slouken@0
   429
			/* If you wanted to control the mouse mode:
slouken@0
   430
			   Set IMPS/2 protocol:
slouken@0
   431
				{0xf3,200,0xf3,100,0xf3,80}
slouken@0
   432
			   Reset mouse device:
slouken@0
   433
				{0xFF}
slouken@0
   434
			*/
slouken@0
   435
			/* Get the mouse protocol response */
slouken@0
   436
			do {
slouken@0
   437
				FD_ZERO(&fdset);
slouken@0
   438
				FD_SET(fd, &fdset);
slouken@0
   439
				tv.tv_sec = 1;
slouken@0
   440
				tv.tv_usec = 0;
slouken@0
   441
				if ( select(fd+1, &fdset, 0, 0, &tv) < 1 ) {
slouken@0
   442
					break;
slouken@0
   443
				}
slouken@0
   444
			} while ( (read(fd, &ch, 1) == 1) &&
slouken@0
   445
			          ((ch == 0xFA) || (ch == 0xAA)) );
slouken@0
   446
slouken@0
   447
			/* Experimental values (Logitech wheelmouse) */
slouken@0
   448
#ifdef DEBUG_MOUSE
slouken@0
   449
fprintf(stderr, "Last mouse mode: 0x%x\n", ch);
slouken@0
   450
#endif
slouken@0
   451
			if ( ch == 3 ) {
slouken@0
   452
				imps2 = 1;
slouken@0
   453
			}
slouken@0
   454
		}
slouken@0
   455
	}
slouken@0
   456
	return imps2;
slouken@0
   457
}
slouken@0
   458
slouken@0
   459
int FB_OpenMouse(_THIS)
slouken@0
   460
{
slouken@0
   461
	const char *mousedev;
slouken@0
   462
	const char *mousedrv;
slouken@0
   463
slouken@0
   464
	mousedrv = getenv("SDL_MOUSEDRV");
slouken@0
   465
	mousedev = getenv("SDL_MOUSEDEV");
slouken@0
   466
	mouse_fd = -1;
slouken@0
   467
slouken@0
   468
	/* ELO TOUCHSCREEN SUPPORT */
slouken@0
   469
slouken@0
   470
	if( (mousedrv != NULL) && (strcmp(mousedrv, "ELO") == 0) ) {
slouken@0
   471
		mouse_fd = open(mousedev, O_RDWR);
slouken@0
   472
		if ( mouse_fd >= 0 ) {
slouken@0
   473
			if(eloInitController(mouse_fd)) {
slouken@0
   474
#ifdef DEBUG_MOUSE
slouken@0
   475
fprintf(stderr, "Using ELO touchscreen\n");
slouken@0
   476
#endif
slouken@0
   477
   				mouse_drv = MOUSE_ELO;
slouken@0
   478
			}
slouken@0
   479
slouken@0
   480
		}
slouken@0
   481
		else if ( mouse_fd < 0 ) {
slouken@0
   482
			mouse_drv = MOUSE_NONE;
slouken@0
   483
		}
slouken@0
   484
slouken@0
   485
		return(mouse_fd);
slouken@0
   486
	}
slouken@0
   487
slouken@0
   488
	/* STD MICE */
slouken@0
   489
slouken@0
   490
	if ( mousedev == NULL ) {
slouken@0
   491
		/* First try to use GPM in repeater mode */
slouken@0
   492
		if ( mouse_fd < 0 ) {
slouken@0
   493
			if ( gpm_available() ) {
slouken@0
   494
				mouse_fd = open(GPM_NODE_FIFO, O_RDONLY, 0);
slouken@0
   495
				if ( mouse_fd >= 0 ) {
slouken@0
   496
#ifdef DEBUG_MOUSE
slouken@0
   497
fprintf(stderr, "Using GPM mouse\n");
slouken@0
   498
#endif
slouken@0
   499
					mouse_drv = MOUSE_GPM;
slouken@0
   500
				}
slouken@0
   501
			}
slouken@0
   502
		}
slouken@0
   503
		/* Now try to use the new HID unified mouse device */
slouken@0
   504
		if ( mouse_fd < 0 ) {
slouken@0
   505
			mouse_fd = open("/dev/input/mice", O_RDONLY, 0);
slouken@0
   506
			if ( mouse_fd >= 0 ) {
slouken@0
   507
				mouse_drv = MOUSE_IMPS2;
slouken@0
   508
			}
slouken@0
   509
		}
slouken@0
   510
		/* Now try to use a modern PS/2 port mouse */
slouken@0
   511
		if ( mouse_fd < 0 ) {
slouken@0
   512
			mouse_fd = open("/dev/psaux", O_RDWR, 0);
slouken@0
   513
			if ( mouse_fd < 0 ) {
slouken@0
   514
				mouse_fd = open("/dev/psaux", O_RDONLY, 0);
slouken@0
   515
			}
slouken@0
   516
			if ( mouse_fd >= 0 ) {
slouken@0
   517
				if ( detect_imps2(mouse_fd) ) {
slouken@0
   518
#ifdef DEBUG_MOUSE
slouken@0
   519
fprintf(stderr, "Using IMPS/2 mouse\n");
slouken@0
   520
#endif
slouken@0
   521
					mouse_drv = MOUSE_IMPS2;
slouken@0
   522
				} else {
slouken@0
   523
#ifdef DEBUG_MOUSE
slouken@0
   524
fprintf(stderr, "Using PS/2 mouse\n");
slouken@0
   525
#endif
slouken@0
   526
					mouse_drv = MOUSE_PS2;
slouken@0
   527
				}
slouken@0
   528
			}
slouken@0
   529
		}
slouken@0
   530
		/* Next try to use a PPC ADB port mouse */
slouken@0
   531
		if ( mouse_fd < 0 ) {
slouken@0
   532
			mouse_fd = open("/dev/adbmouse", O_RDONLY, 0);
slouken@0
   533
			if ( mouse_fd >= 0 ) {
slouken@0
   534
#ifdef DEBUG_MOUSE
slouken@0
   535
fprintf(stderr, "Using ADB mouse\n");
slouken@0
   536
#endif
slouken@0
   537
				mouse_drv = MOUSE_BM;
slouken@0
   538
			}
slouken@0
   539
		}
slouken@0
   540
	}
slouken@0
   541
	/* Default to a serial Microsoft mouse */
slouken@0
   542
	if ( mouse_fd < 0 ) {
slouken@0
   543
		if ( mousedev == NULL ) {
slouken@0
   544
			mousedev = "/dev/mouse";
slouken@0
   545
		}
slouken@0
   546
		mouse_fd = open(mousedev, O_RDONLY, 0);
slouken@0
   547
		if ( mouse_fd >= 0 ) {
slouken@0
   548
			struct termios mouse_termios;
slouken@0
   549
slouken@0
   550
			/* Set the sampling speed to 1200 baud */
slouken@0
   551
			tcgetattr(mouse_fd, &mouse_termios);
slouken@0
   552
			mouse_termios.c_iflag = IGNBRK | IGNPAR;
slouken@0
   553
			mouse_termios.c_oflag = 0;
slouken@0
   554
			mouse_termios.c_lflag = 0;
slouken@0
   555
			mouse_termios.c_line = 0;
slouken@0
   556
			mouse_termios.c_cc[VTIME] = 0;
slouken@0
   557
			mouse_termios.c_cc[VMIN] = 1;
slouken@0
   558
			mouse_termios.c_cflag = CREAD | CLOCAL | HUPCL;
slouken@0
   559
			mouse_termios.c_cflag |= CS8;
slouken@0
   560
			mouse_termios.c_cflag |= B1200;
slouken@0
   561
			tcsetattr(mouse_fd, TCSAFLUSH, &mouse_termios);
slouken@0
   562
#ifdef DEBUG_MOUSE
slouken@0
   563
fprintf(stderr, "Using Microsoft mouse on %s\n", mousedev);
slouken@0
   564
#endif
slouken@0
   565
			mouse_drv = MOUSE_MS;
slouken@0
   566
		}
slouken@0
   567
	}
slouken@0
   568
	if ( mouse_fd < 0 ) {
slouken@0
   569
		mouse_drv = MOUSE_NONE;
slouken@0
   570
	}
slouken@0
   571
	return(mouse_fd);
slouken@0
   572
}
slouken@0
   573
slouken@0
   574
static int posted = 0;
slouken@0
   575
slouken@0
   576
void FB_vgamousecallback(int button, int relative, int dx, int dy)
slouken@0
   577
{
slouken@0
   578
	int button_1, button_3;
slouken@0
   579
	int button_state;
slouken@0
   580
	int state_changed;
slouken@0
   581
	int i;
slouken@0
   582
	Uint8 state;
slouken@0
   583
slouken@0
   584
	if ( dx || dy ) {
slouken@0
   585
		posted += SDL_PrivateMouseMotion(0, relative, dx, dy);
slouken@0
   586
	}
slouken@0
   587
slouken@0
   588
	/* Swap button 1 and 3 */
slouken@0
   589
	button_1 = (button & 0x04) >> 2;
slouken@0
   590
	button_3 = (button & 0x01) << 2;
slouken@0
   591
	button &= ~0x05;
slouken@0
   592
	button |= (button_1|button_3);
slouken@0
   593
slouken@0
   594
	/* See what changed */
slouken@0
   595
	button_state = SDL_GetMouseState(NULL, NULL);
slouken@0
   596
	state_changed = button_state ^ button;
slouken@0
   597
	for ( i=0; i<8; ++i ) {
slouken@0
   598
		if ( state_changed & (1<<i) ) {
slouken@0
   599
			if ( button & (1<<i) ) {
slouken@0
   600
				state = SDL_PRESSED;
slouken@0
   601
			} else {
slouken@0
   602
				state = SDL_RELEASED;
slouken@0
   603
			}
slouken@0
   604
			posted += SDL_PrivateMouseButton(state, i+1, 0, 0);
slouken@0
   605
		}
slouken@0
   606
	}
slouken@0
   607
}
slouken@0
   608
slouken@0
   609
/* For now, use GPM, PS/2, and MS protocols
slouken@0
   610
   Driver adapted from the SVGAlib mouse driver code (taken from gpm, etc.)
slouken@0
   611
 */
slouken@0
   612
static void handle_mouse(_THIS)
slouken@0
   613
{
slouken@0
   614
	static int start = 0;
slouken@0
   615
	static unsigned char mousebuf[BUFSIZ];
slouken@0
   616
	static int relative = 1;
slouken@0
   617
slouken@0
   618
	int i, nread;
slouken@0
   619
	int button = 0;
slouken@0
   620
	int dx = 0, dy = 0;
slouken@0
   621
	int packetsize = 0;
slouken@0
   622
slouken@0
   623
	/* Figure out the mouse packet size */
slouken@0
   624
	switch (mouse_drv) {
slouken@0
   625
		case MOUSE_NONE:
slouken@0
   626
			/* Ack! */
slouken@0
   627
			read(mouse_fd, mousebuf, BUFSIZ);
slouken@0
   628
			return;
slouken@0
   629
		case MOUSE_GPM:
slouken@0
   630
			packetsize = 5;
slouken@0
   631
			break;
slouken@0
   632
		case MOUSE_IMPS2:
slouken@0
   633
			packetsize = 4;
slouken@0
   634
			break;
slouken@0
   635
		case MOUSE_PS2:
slouken@0
   636
		case MOUSE_MS:
slouken@0
   637
		case MOUSE_BM:
slouken@0
   638
			packetsize = 3;
slouken@0
   639
			break;
slouken@0
   640
		case MOUSE_ELO:
slouken@0
   641
			packetsize = ELO_PACKET_SIZE;
slouken@0
   642
			relative = 0;
slouken@0
   643
			break;
slouken@0
   644
		case NUM_MOUSE_DRVS:
slouken@0
   645
			/* Uh oh.. */
slouken@0
   646
			packetsize = 0;
slouken@0
   647
			break;
slouken@0
   648
	}
slouken@0
   649
slouken@0
   650
	/* Read as many packets as possible */
slouken@0
   651
	nread = read(mouse_fd, &mousebuf[start], BUFSIZ-start);
slouken@0
   652
	if ( nread < 0 ) {
slouken@0
   653
		return;
slouken@0
   654
	}
slouken@0
   655
	nread += start;
slouken@0
   656
#ifdef DEBUG_MOUSE
slouken@0
   657
	fprintf(stderr, "Read %d bytes from mouse, start = %d\n", nread, start);
slouken@0
   658
#endif
slouken@0
   659
	for ( i=0; i<(nread-(packetsize-1)); i += packetsize ) {
slouken@0
   660
		switch (mouse_drv) {
slouken@0
   661
			case MOUSE_NONE:
slouken@0
   662
				break;
slouken@0
   663
			case MOUSE_GPM:
slouken@0
   664
				/* GPM protocol has 0x80 in high byte */
slouken@0
   665
				if ( (mousebuf[i] & 0xF8) != 0x80 ) {
slouken@0
   666
					/* Go to next byte */
slouken@0
   667
					i -= (packetsize-1);
slouken@0
   668
					continue;
slouken@0
   669
				}
slouken@0
   670
				/* Get current mouse state */
slouken@0
   671
				button = (~mousebuf[i]) & 0x07;
slouken@0
   672
				dx =   (signed char)(mousebuf[i+1]) +
slouken@0
   673
				       (signed char)(mousebuf[i+3]);
slouken@0
   674
				dy = -((signed char)(mousebuf[i+2]) +
slouken@0
   675
				       (signed char)(mousebuf[i+4]));
slouken@0
   676
				break;
slouken@0
   677
			case MOUSE_PS2:
slouken@0
   678
				/* PS/2 protocol has nothing in high byte */
slouken@0
   679
				if ( (mousebuf[i] & 0xC0) != 0 ) {
slouken@0
   680
					/* Go to next byte */
slouken@0
   681
					i -= (packetsize-1);
slouken@0
   682
					continue;
slouken@0
   683
				}
slouken@0
   684
				/* Get current mouse state */
slouken@0
   685
				button = (mousebuf[i] & 0x04) >> 1 | /*Middle*/
slouken@0
   686
		  			 (mousebuf[i] & 0x02) >> 1 | /*Right*/
slouken@0
   687
		  			 (mousebuf[i] & 0x01) << 2;  /*Left*/
slouken@0
   688
		  		dx = (mousebuf[i] & 0x10) ?
slouken@0
   689
		  		      mousebuf[i+1] - 256 : mousebuf[i+1];
slouken@0
   690
		  		dy = (mousebuf[i] & 0x20) ?
slouken@0
   691
		  		      -(mousebuf[i+2] - 256) : -mousebuf[i+2];
slouken@0
   692
				break;
slouken@0
   693
			case MOUSE_IMPS2:
slouken@0
   694
				/* Get current mouse state */
slouken@0
   695
				button = (mousebuf[i] & 0x04) >> 1 | /*Middle*/
slouken@0
   696
		  			 (mousebuf[i] & 0x02) >> 1 | /*Right*/
slouken@0
   697
		  			 (mousebuf[i] & 0x01) << 2 | /*Left*/
slouken@0
   698
		  			 (mousebuf[i] & 0x40) >> 3 | /* 4 */
slouken@0
   699
		  			 (mousebuf[i] & 0x80) >> 3;  /* 5 */
slouken@0
   700
		  		dx = (mousebuf[i] & 0x10) ?
slouken@0
   701
		  		      mousebuf[i+1] - 256 : mousebuf[i+1];
slouken@0
   702
		  		dy = (mousebuf[i] & 0x20) ?
slouken@0
   703
		  		      -(mousebuf[i+2] - 256) : -mousebuf[i+2];
slouken@0
   704
				switch (mousebuf[i+3]&0x0F) {
slouken@0
   705
				    case 0x0E: /* DX = +1 */
slouken@0
   706
				    case 0x02: /* DX = -1 */
slouken@0
   707
					break;
slouken@0
   708
				    case 0x0F: /* DY = +1 (map button 4) */
slouken@0
   709
					button |= (1<<3);
slouken@0
   710
					break;
slouken@0
   711
				    case 0x01: /* DY = -1 (map button 5) */
slouken@0
   712
					button |= (1<<4);
slouken@0
   713
					break;
slouken@0
   714
				}
slouken@0
   715
				break;
slouken@0
   716
			case MOUSE_MS:
slouken@0
   717
				/* Microsoft protocol has 0x40 in high byte */
slouken@0
   718
				if ( (mousebuf[i] & 0x40) != 0x40 ) {
slouken@0
   719
					/* Go to next byte */
slouken@0
   720
					i -= (packetsize-1);
slouken@0
   721
					continue;
slouken@0
   722
				}
slouken@0
   723
				/* Get current mouse state */
slouken@0
   724
				button = ((mousebuf[i] & 0x20) >> 3) |
slouken@0
   725
				         ((mousebuf[i] & 0x10) >> 4);
slouken@0
   726
				dx = (signed char)(((mousebuf[i] & 0x03) << 6) |
slouken@0
   727
				                   (mousebuf[i + 1] & 0x3F));
slouken@0
   728
				dy = (signed char)(((mousebuf[i] & 0x0C) << 4) |
slouken@0
   729
				                    (mousebuf[i + 2] & 0x3F));
slouken@0
   730
				break;
slouken@0
   731
			case MOUSE_BM:
slouken@0
   732
				/* BusMouse protocol has 0xF8 in high byte */
slouken@0
   733
				if ( (mousebuf[i] & 0xF8) != 0x80 ) {
slouken@0
   734
					/* Go to next byte */
slouken@0
   735
					i -= (packetsize-1);
slouken@0
   736
					continue;
slouken@0
   737
				}
slouken@0
   738
				/* Get current mouse state */
slouken@0
   739
				button = (~mousebuf[i]) & 0x07;
slouken@0
   740
				dx =  (signed char)mousebuf[i+1];
slouken@0
   741
				dy = -(signed char)mousebuf[i+2];
slouken@0
   742
				break;
slouken@0
   743
			case MOUSE_ELO:
slouken@0
   744
				/* ELO protocol has ELO_START_BYTE as first byte */
slouken@0
   745
				if ( mousebuf[i] != ELO_START_BYTE ) {
slouken@0
   746
					/* Go to next byte */
slouken@0
   747
					i -= (packetsize-1);
slouken@0
   748
					continue;
slouken@0
   749
				}
slouken@0
   750
slouken@0
   751
				/* parse the packet */
slouken@0
   752
				if(!eloParsePacket(&(mousebuf[i]), &dx, &dy, &button)) {
slouken@0
   753
					break;
slouken@0
   754
				}
slouken@0
   755
				
slouken@0
   756
				button = (button & 0x01) << 2;
slouken@0
   757
slouken@0
   758
				/* convert to screen coordinates */
slouken@0
   759
				eloConvertXY(this, &dx, &dy);
slouken@0
   760
				break;
slouken@0
   761
slouken@0
   762
			case NUM_MOUSE_DRVS:
slouken@0
   763
				/* Uh oh.. */
slouken@0
   764
				dx = 0;
slouken@0
   765
				dy = 0;
slouken@0
   766
				break;
slouken@0
   767
		}
slouken@0
   768
		FB_vgamousecallback(button, relative, dx, dy);
slouken@0
   769
	}
slouken@0
   770
	if ( i < nread ) {
slouken@0
   771
		memcpy(mousebuf, &mousebuf[i], (nread-i));
slouken@0
   772
		start = (nread-i);
slouken@0
   773
	} else {
slouken@0
   774
		start = 0;
slouken@0
   775
	}
slouken@0
   776
	return;
slouken@0
   777
}
slouken@0
   778
slouken@0
   779
/* Handle switching to another VC, returns when our VC is back */
slouken@0
   780
static void switch_vt(_THIS, unsigned short which)
slouken@0
   781
{
slouken@0
   782
	struct vt_stat vtstate;
slouken@0
   783
	unsigned short current;
slouken@0
   784
	SDL_Surface *screen;
slouken@0
   785
	__u16 saved_pal[3*256];
slouken@0
   786
	Uint32 screen_arealen;
slouken@0
   787
	Uint8 *screen_contents;
slouken@0
   788
slouken@0
   789
	/* Figure out whether or not we're switching to a new console */
slouken@0
   790
	if ( (ioctl(keyboard_fd, VT_GETSTATE, &vtstate) < 0) ||
slouken@0
   791
	     (which == vtstate.v_active) ) {
slouken@0
   792
		return;
slouken@0
   793
	}
slouken@0
   794
	current = vtstate.v_active;
slouken@0
   795
slouken@0
   796
	/* Save the contents of the screen, and go to text mode */
slouken@0
   797
	SDL_mutexP(hw_lock);
slouken@0
   798
	screen = SDL_VideoSurface;
slouken@0
   799
	screen_arealen = (screen->h*screen->pitch);
slouken@0
   800
	screen_contents = (Uint8 *)malloc(screen_arealen);
slouken@0
   801
	if ( screen_contents ) {
slouken@0
   802
		memcpy(screen_contents, screen->pixels, screen_arealen);
slouken@0
   803
	}
slouken@0
   804
	FB_SavePaletteTo(this, 256, saved_pal);
slouken@0
   805
	ioctl(keyboard_fd, KDSETMODE, KD_TEXT);
slouken@0
   806
slouken@0
   807
	/* New console, switch to it */
slouken@0
   808
	if ( ioctl(keyboard_fd, VT_ACTIVATE, which) == 0 ) {
slouken@0
   809
		/* Wait for our console to be activated again */
slouken@0
   810
		ioctl(keyboard_fd, VT_WAITACTIVE, which);
slouken@0
   811
		while ( ioctl(keyboard_fd, VT_WAITACTIVE, current) < 0 ) {
slouken@0
   812
			if ( (errno != EINTR) && (errno != EAGAIN) ) {
slouken@0
   813
				/* Unknown VT error - cancel this */
slouken@0
   814
				break;
slouken@0
   815
			}
slouken@0
   816
			SDL_Delay(500);
slouken@0
   817
		}
slouken@0
   818
	}
slouken@0
   819
slouken@0
   820
	/* Restore graphics mode and the contents of the screen */
slouken@0
   821
	ioctl(keyboard_fd, KDSETMODE, KD_GRAPHICS);
slouken@0
   822
	FB_RestorePaletteFrom(this, 256, saved_pal);
slouken@0
   823
	if ( screen_contents ) {
slouken@0
   824
		memcpy(screen->pixels, screen_contents, screen_arealen);
slouken@0
   825
		free(screen_contents);
slouken@0
   826
	}
slouken@0
   827
	SDL_mutexV(hw_lock);
slouken@0
   828
}
slouken@0
   829
slouken@0
   830
static void handle_keyboard(_THIS)
slouken@0
   831
{
slouken@0
   832
	unsigned char keybuf[BUFSIZ];
slouken@0
   833
	int i, nread;
slouken@0
   834
	int pressed;
slouken@0
   835
	int scancode;
slouken@0
   836
	SDL_keysym keysym;
slouken@0
   837
slouken@0
   838
	nread = read(keyboard_fd, keybuf, BUFSIZ);
slouken@0
   839
	for ( i=0; i<nread; ++i ) {
slouken@0
   840
		scancode = keybuf[i] & 0x7F;
slouken@0
   841
		if ( keybuf[i] & 0x80 ) {
slouken@0
   842
			pressed = SDL_RELEASED;
slouken@0
   843
		} else {
slouken@0
   844
			pressed = SDL_PRESSED;
slouken@0
   845
		}
slouken@0
   846
		TranslateKey(scancode, &keysym);
slouken@0
   847
		/* Handle Alt-FN for vt switch */
slouken@0
   848
		switch (keysym.sym) {
slouken@0
   849
		    case SDLK_F1:
slouken@0
   850
		    case SDLK_F2:
slouken@0
   851
		    case SDLK_F3:
slouken@0
   852
		    case SDLK_F4:
slouken@0
   853
		    case SDLK_F5:
slouken@0
   854
		    case SDLK_F6:
slouken@0
   855
		    case SDLK_F7:
slouken@0
   856
		    case SDLK_F8:
slouken@0
   857
		    case SDLK_F9:
slouken@0
   858
		    case SDLK_F10:
slouken@0
   859
		    case SDLK_F11:
slouken@0
   860
		    case SDLK_F12:
slouken@0
   861
			if ( SDL_GetModState() & KMOD_ALT ) {
slouken@0
   862
				switch_vt(this, (keysym.sym-SDLK_F1)+1);
slouken@0
   863
				break;
slouken@0
   864
			}
slouken@0
   865
			/* Fall through to normal processing */
slouken@0
   866
		    default:
slouken@0
   867
			posted += SDL_PrivateKeyboard(pressed, &keysym);
slouken@0
   868
			break;
slouken@0
   869
		}
slouken@0
   870
	}
slouken@0
   871
}
slouken@0
   872
slouken@0
   873
void FB_PumpEvents(_THIS)
slouken@0
   874
{
slouken@0
   875
	fd_set fdset;
slouken@0
   876
	int max_fd;
slouken@0
   877
	static struct timeval zero;
slouken@0
   878
slouken@0
   879
	do {
slouken@0
   880
		posted = 0;
slouken@0
   881
slouken@0
   882
		FD_ZERO(&fdset);
slouken@0
   883
		max_fd = 0;
slouken@0
   884
		if ( keyboard_fd >= 0 ) {
slouken@0
   885
			FD_SET(keyboard_fd, &fdset);
slouken@0
   886
			if ( max_fd < keyboard_fd ) {
slouken@0
   887
				max_fd = keyboard_fd;
slouken@0
   888
			}
slouken@0
   889
		}
slouken@0
   890
		if ( mouse_fd >= 0 ) {
slouken@0
   891
			FD_SET(mouse_fd, &fdset);
slouken@0
   892
			if ( max_fd < mouse_fd ) {
slouken@0
   893
				max_fd = mouse_fd;
slouken@0
   894
			}
slouken@0
   895
		}
slouken@0
   896
		if ( select(max_fd+1, &fdset, NULL, NULL, &zero) > 0 ) {
slouken@0
   897
			if ( keyboard_fd >= 0 ) {
slouken@0
   898
				if ( FD_ISSET(keyboard_fd, &fdset) ) {
slouken@0
   899
					handle_keyboard(this);
slouken@0
   900
				}
slouken@0
   901
			}
slouken@0
   902
			if ( mouse_fd >= 0 ) {
slouken@0
   903
				if ( FD_ISSET(mouse_fd, &fdset) ) {
slouken@0
   904
					handle_mouse(this);
slouken@0
   905
				}
slouken@0
   906
			}
slouken@0
   907
		}
slouken@0
   908
	} while ( posted );
slouken@0
   909
}
slouken@0
   910
slouken@0
   911
void FB_InitOSKeymap(_THIS)
slouken@0
   912
{
slouken@0
   913
	int i;
slouken@0
   914
slouken@0
   915
	/* Initialize the Linux key translation table */
slouken@0
   916
slouken@0
   917
	/* First get the ascii keys and others not well handled */
slouken@0
   918
	for (i=0; i<SDL_TABLESIZE(keymap); ++i) {
slouken@0
   919
	  switch(i) {
slouken@0
   920
	  /* These aren't handled by the x86 kernel keymapping (?) */
slouken@0
   921
	  case SCANCODE_PRINTSCREEN:
slouken@0
   922
	    keymap[i] = SDLK_PRINT;
slouken@0
   923
	    break;
slouken@0
   924
	  case SCANCODE_BREAK:
slouken@0
   925
	    keymap[i] = SDLK_BREAK;
slouken@0
   926
	    break;
slouken@0
   927
	  case SCANCODE_BREAK_ALTERNATIVE:
slouken@0
   928
	    keymap[i] = SDLK_PAUSE;
slouken@0
   929
	    break;
slouken@0
   930
	  case SCANCODE_LEFTSHIFT:
slouken@0
   931
	    keymap[i] = SDLK_LSHIFT;
slouken@0
   932
	    break;
slouken@0
   933
	  case SCANCODE_RIGHTSHIFT:
slouken@0
   934
	    keymap[i] = SDLK_RSHIFT;
slouken@0
   935
	    break;
slouken@0
   936
	  case SCANCODE_LEFTCONTROL:
slouken@0
   937
	    keymap[i] = SDLK_LCTRL;
slouken@0
   938
	    break;
slouken@0
   939
	  case SCANCODE_RIGHTCONTROL:
slouken@0
   940
	    keymap[i] = SDLK_RCTRL;
slouken@0
   941
	    break;
slouken@0
   942
	  case SCANCODE_RIGHTWIN:
slouken@0
   943
	    keymap[i] = SDLK_RSUPER;
slouken@0
   944
	    break;
slouken@0
   945
	  case SCANCODE_LEFTWIN:
slouken@0
   946
	    keymap[i] = SDLK_LSUPER;
slouken@0
   947
	    break;
slouken@0
   948
	  case 127:
slouken@0
   949
	    keymap[i] = SDLK_MENU;
slouken@0
   950
	    break;
slouken@0
   951
	  /* this should take care of all standard ascii keys */
slouken@0
   952
	  default:
slouken@0
   953
	    keymap[i] = KVAL(vga_keymap[0][i]);
slouken@0
   954
	    break;
slouken@0
   955
          }
slouken@0
   956
	}
slouken@0
   957
	for (i=0; i<SDL_TABLESIZE(keymap); ++i) {
slouken@0
   958
	  switch(keymap_temp[i]) {
slouken@0
   959
	    case K_F1:  keymap[i] = SDLK_F1;  break;
slouken@0
   960
	    case K_F2:  keymap[i] = SDLK_F2;  break;
slouken@0
   961
	    case K_F3:  keymap[i] = SDLK_F3;  break;
slouken@0
   962
	    case K_F4:  keymap[i] = SDLK_F4;  break;
slouken@0
   963
	    case K_F5:  keymap[i] = SDLK_F5;  break;
slouken@0
   964
	    case K_F6:  keymap[i] = SDLK_F6;  break;
slouken@0
   965
	    case K_F7:  keymap[i] = SDLK_F7;  break;
slouken@0
   966
	    case K_F8:  keymap[i] = SDLK_F8;  break;
slouken@0
   967
	    case K_F9:  keymap[i] = SDLK_F9;  break;
slouken@0
   968
	    case K_F10: keymap[i] = SDLK_F10; break;
slouken@0
   969
	    case K_F11: keymap[i] = SDLK_F11; break;
slouken@0
   970
	    case K_F12: keymap[i] = SDLK_F12; break;
slouken@0
   971
slouken@0
   972
	    case K_DOWN:  keymap[i] = SDLK_DOWN;  break;
slouken@0
   973
	    case K_LEFT:  keymap[i] = SDLK_LEFT;  break;
slouken@0
   974
	    case K_RIGHT: keymap[i] = SDLK_RIGHT; break;
slouken@0
   975
	    case K_UP:    keymap[i] = SDLK_UP;    break;
slouken@0
   976
slouken@0
   977
	    case K_P0:     keymap[i] = SDLK_KP0; break;
slouken@0
   978
	    case K_P1:     keymap[i] = SDLK_KP1; break;
slouken@0
   979
	    case K_P2:     keymap[i] = SDLK_KP2; break;
slouken@0
   980
	    case K_P3:     keymap[i] = SDLK_KP3; break;
slouken@0
   981
	    case K_P4:     keymap[i] = SDLK_KP4; break;
slouken@0
   982
	    case K_P5:     keymap[i] = SDLK_KP5; break;
slouken@0
   983
	    case K_P6:     keymap[i] = SDLK_KP6; break;
slouken@0
   984
	    case K_P7:     keymap[i] = SDLK_KP7; break;
slouken@0
   985
	    case K_P8:     keymap[i] = SDLK_KP8; break;
slouken@0
   986
	    case K_P9:     keymap[i] = SDLK_KP9; break;
slouken@0
   987
	    case K_PPLUS:  keymap[i] = SDLK_KP_PLUS; break;
slouken@0
   988
	    case K_PMINUS: keymap[i] = SDLK_KP_MINUS; break;
slouken@0
   989
	    case K_PSTAR:  keymap[i] = SDLK_KP_MULTIPLY; break;
slouken@0
   990
	    case K_PSLASH: keymap[i] = SDLK_KP_DIVIDE; break;
slouken@0
   991
	    case K_PENTER: keymap[i] = SDLK_KP_ENTER; break;
slouken@0
   992
	    case K_PDOT:   keymap[i] = SDLK_KP_PERIOD; break;
slouken@0
   993
slouken@0
   994
	    case K_SHIFT:  if ( keymap[i] != SDLK_RSHIFT )
slouken@0
   995
	                     keymap[i] = SDLK_LSHIFT;
slouken@0
   996
	                   break;
slouken@0
   997
	    case K_SHIFTL: keymap[i] = SDLK_LSHIFT; break;
slouken@0
   998
	    case K_SHIFTR: keymap[i] = SDLK_RSHIFT; break;
slouken@0
   999
	    case K_CTRL:  if ( keymap[i] != SDLK_RCTRL )
slouken@0
  1000
	                     keymap[i] = SDLK_LCTRL;
slouken@0
  1001
	                   break;
slouken@0
  1002
	    case K_CTRLL:  keymap[i] = SDLK_LCTRL;  break;
slouken@0
  1003
	    case K_CTRLR:  keymap[i] = SDLK_RCTRL;  break;
slouken@0
  1004
	    case K_ALT:    keymap[i] = SDLK_LALT;   break;
slouken@0
  1005
	    case K_ALTGR:  keymap[i] = SDLK_RALT;   break;
slouken@0
  1006
slouken@0
  1007
	    case K_INSERT: keymap[i] = SDLK_INSERT;   break;
slouken@0
  1008
	    case K_REMOVE: keymap[i] = SDLK_DELETE;   break;
slouken@0
  1009
	    case K_PGUP:   keymap[i] = SDLK_PAGEUP;   break;
slouken@0
  1010
	    case K_PGDN:   keymap[i] = SDLK_PAGEDOWN; break;
slouken@0
  1011
	    case K_FIND:   keymap[i] = SDLK_HOME;     break;
slouken@0
  1012
	    case K_SELECT: keymap[i] = SDLK_END;      break;
slouken@0
  1013
slouken@0
  1014
	    case K_NUM:  keymap[i] = SDLK_NUMLOCK;   break;
slouken@0
  1015
	    case K_CAPS: keymap[i] = SDLK_CAPSLOCK;  break;
slouken@0
  1016
slouken@0
  1017
	    case K_F13:   keymap[i] = SDLK_PRINT;     break;
slouken@0
  1018
	    case K_HOLD:  keymap[i] = SDLK_SCROLLOCK; break;
slouken@0
  1019
	    case K_PAUSE: keymap[i] = SDLK_PAUSE;     break;
slouken@0
  1020
slouken@0
  1021
	    case 127: keymap[i] = SDLK_BACKSPACE; break;
slouken@0
  1022
	     
slouken@0
  1023
	    default: break;
slouken@0
  1024
	  }
slouken@0
  1025
	}
slouken@0
  1026
}
slouken@0
  1027
slouken@0
  1028
static SDL_keysym *TranslateKey(int scancode, SDL_keysym *keysym)
slouken@0
  1029
{
slouken@0
  1030
	/* Set the keysym information */
slouken@0
  1031
	keysym->scancode = scancode;
slouken@0
  1032
	keysym->sym = keymap[scancode];
slouken@0
  1033
	keysym->mod = KMOD_NONE;
slouken@0
  1034
slouken@0
  1035
	/* If UNICODE is on, get the UNICODE value for the key */
slouken@0
  1036
	keysym->unicode = 0;
slouken@0
  1037
	if ( SDL_TranslateUNICODE ) {
slouken@0
  1038
		int map;
slouken@0
  1039
		SDLMod modstate;
slouken@0
  1040
slouken@0
  1041
		modstate = SDL_GetModState();
slouken@0
  1042
		map = 0;
slouken@0
  1043
		if ( modstate & KMOD_SHIFT ) {
slouken@0
  1044
			map |= (1<<KG_SHIFT);
slouken@0
  1045
		}
slouken@0
  1046
		if ( modstate & KMOD_CTRL ) {
slouken@0
  1047
			map |= (1<<KG_CTRL);
slouken@0
  1048
		}
slouken@0
  1049
		if ( modstate & KMOD_ALT ) {
slouken@0
  1050
			map |= (1<<KG_ALT);
slouken@0
  1051
		}
slouken@0
  1052
		if ( modstate & KMOD_MODE ) {
slouken@0
  1053
			map |= (1<<KG_ALTGR);
slouken@0
  1054
		}
slouken@0
  1055
		if ( KTYP(vga_keymap[map][scancode]) == KT_LETTER ) {
slouken@0
  1056
			if ( modstate & KMOD_CAPS ) {
slouken@0
  1057
				map ^= (1<<KG_SHIFT);
slouken@0
  1058
			}
slouken@0
  1059
		}
slouken@0
  1060
		if ( KTYP(vga_keymap[map][scancode]) == KT_PAD ) {
slouken@0
  1061
			if ( modstate & KMOD_NUM ) {
slouken@0
  1062
				keysym->unicode=KVAL(vga_keymap[map][scancode]);
slouken@0
  1063
			}
slouken@0
  1064
		} else {
slouken@0
  1065
			keysym->unicode = KVAL(vga_keymap[map][scancode]);
slouken@0
  1066
		}
slouken@0
  1067
	}
slouken@0
  1068
	return(keysym);
slouken@0
  1069
}