src/video/fbcon/SDL_fbevents.c
author Sam Lantinga <slouken@lokigames.com>
Sat, 14 Jul 2001 19:10:06 +0000
changeset 109 5a9c36a45db1
parent 104 a746656b7599
child 132 2604a7be65af
permissions -rw-r--r--
Fixed switching away from the SDL at the framebuffer console
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@91
   250
		static const char * const tty0[] = { "/dev/tty0", "/dev/vc/0", NULL };
slouken@91
   251
		static const char * const 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@109
   314
	MOUSE_MSC,	/* 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@59
   400
slouken@59
   401
/* rcg06112001 Set up IMPS/2 mode, if possible. This gives
slouken@59
   402
 *  us access to the mousewheel, etc. Returns zero if
slouken@59
   403
 *  writes to device failed, but you still need to query the
slouken@59
   404
 *  device to see which mode it's actually in.
slouken@59
   405
 */
slouken@59
   406
static int set_imps2_mode(int fd)
slouken@59
   407
{
slouken@59
   408
	/* If you wanted to control the mouse mode (and we do :)  ) ...
slouken@59
   409
		Set IMPS/2 protocol:
slouken@59
   410
			{0xf3,200,0xf3,100,0xf3,80}
slouken@59
   411
		Reset mouse device:
slouken@59
   412
			{0xFF}
slouken@59
   413
	*/
slouken@59
   414
	Uint8 set_imps2[] = {0xf3, 200, 0xf3, 100, 0xf3, 80};
slouken@59
   415
	Uint8 reset = 0xff;
slouken@59
   416
	fd_set fdset;
slouken@59
   417
	struct timeval tv;
slouken@59
   418
	int retval = 0;
slouken@59
   419
slouken@59
   420
	if ( write(fd, &set_imps2, sizeof(set_imps2)) == sizeof(set_imps2) ) {
slouken@59
   421
		if (write(fd, &reset, sizeof (reset)) == sizeof (reset) ) {
slouken@59
   422
			retval = 1;
slouken@59
   423
		}
slouken@59
   424
	}
slouken@59
   425
slouken@59
   426
	/* Get rid of any chatter from the above */
slouken@59
   427
	FD_ZERO(&fdset);
slouken@59
   428
	FD_SET(fd, &fdset);
slouken@59
   429
	tv.tv_sec = 0;
slouken@59
   430
	tv.tv_usec = 0;
slouken@59
   431
	while ( select(fd+1, &fdset, 0, 0, &tv) > 0 ) {
slouken@59
   432
		char temp[32];
slouken@59
   433
		read(fd, temp, sizeof(temp));
slouken@59
   434
	}
slouken@59
   435
slouken@59
   436
	return retval;
slouken@59
   437
}
slouken@59
   438
slouken@59
   439
slouken@0
   440
/* Returns true if the mouse uses the IMPS/2 protocol */
slouken@0
   441
static int detect_imps2(int fd)
slouken@0
   442
{
slouken@0
   443
	int imps2;
slouken@0
   444
slouken@0
   445
	imps2 = 0;
slouken@59
   446
slouken@0
   447
	if ( getenv("SDL_MOUSEDEV_IMPS2") ) {
slouken@0
   448
		imps2 = 1;
slouken@0
   449
	}
slouken@0
   450
	if ( ! imps2 ) {
slouken@59
   451
		Uint8 query_ps2 = 0xF2;
slouken@0
   452
		fd_set fdset;
slouken@0
   453
		struct timeval tv;
slouken@0
   454
slouken@0
   455
		/* Get rid of any mouse motion noise */
slouken@0
   456
		FD_ZERO(&fdset);
slouken@0
   457
		FD_SET(fd, &fdset);
slouken@0
   458
		tv.tv_sec = 0;
slouken@0
   459
		tv.tv_usec = 0;
slouken@0
   460
		while ( select(fd+1, &fdset, 0, 0, &tv) > 0 ) {
slouken@0
   461
			char temp[32];
slouken@0
   462
			read(fd, temp, sizeof(temp));
slouken@0
   463
		}
slouken@0
   464
slouken@59
   465
   		/* Query for the type of mouse protocol */
slouken@59
   466
   		if ( write(fd, &query_ps2, sizeof (query_ps2)) == sizeof (query_ps2)) {
slouken@59
   467
   			Uint8 ch = 0;
slouken@0
   468
slouken@0
   469
			/* Get the mouse protocol response */
slouken@0
   470
			do {
slouken@0
   471
				FD_ZERO(&fdset);
slouken@0
   472
				FD_SET(fd, &fdset);
slouken@0
   473
				tv.tv_sec = 1;
slouken@0
   474
				tv.tv_usec = 0;
slouken@0
   475
				if ( select(fd+1, &fdset, 0, 0, &tv) < 1 ) {
slouken@0
   476
					break;
slouken@0
   477
				}
slouken@59
   478
			} while ( (read(fd, &ch, sizeof (ch)) == sizeof (ch)) &&
slouken@0
   479
			          ((ch == 0xFA) || (ch == 0xAA)) );
slouken@0
   480
slouken@0
   481
			/* Experimental values (Logitech wheelmouse) */
slouken@0
   482
#ifdef DEBUG_MOUSE
slouken@0
   483
fprintf(stderr, "Last mouse mode: 0x%x\n", ch);
slouken@0
   484
#endif
slouken@104
   485
			if ( (ch == 3) || (ch == 4) ) {
slouken@0
   486
				imps2 = 1;
slouken@0
   487
			}
slouken@0
   488
		}
slouken@0
   489
	}
slouken@0
   490
	return imps2;
slouken@0
   491
}
slouken@0
   492
slouken@0
   493
int FB_OpenMouse(_THIS)
slouken@0
   494
{
slouken@69
   495
	int i;
slouken@0
   496
	const char *mousedev;
slouken@0
   497
	const char *mousedrv;
slouken@0
   498
slouken@0
   499
	mousedrv = getenv("SDL_MOUSEDRV");
slouken@0
   500
	mousedev = getenv("SDL_MOUSEDEV");
slouken@0
   501
	mouse_fd = -1;
slouken@0
   502
slouken@0
   503
	/* ELO TOUCHSCREEN SUPPORT */
slouken@0
   504
slouken@0
   505
	if( (mousedrv != NULL) && (strcmp(mousedrv, "ELO") == 0) ) {
slouken@0
   506
		mouse_fd = open(mousedev, O_RDWR);
slouken@0
   507
		if ( mouse_fd >= 0 ) {
slouken@0
   508
			if(eloInitController(mouse_fd)) {
slouken@0
   509
#ifdef DEBUG_MOUSE
slouken@0
   510
fprintf(stderr, "Using ELO touchscreen\n");
slouken@0
   511
#endif
slouken@0
   512
   				mouse_drv = MOUSE_ELO;
slouken@0
   513
			}
slouken@0
   514
slouken@0
   515
		}
slouken@0
   516
		else if ( mouse_fd < 0 ) {
slouken@0
   517
			mouse_drv = MOUSE_NONE;
slouken@0
   518
		}
slouken@0
   519
slouken@0
   520
		return(mouse_fd);
slouken@0
   521
	}
slouken@0
   522
slouken@0
   523
	/* STD MICE */
slouken@0
   524
slouken@0
   525
	if ( mousedev == NULL ) {
slouken@69
   526
		/* FIXME someday... allow multiple mice in this driver */
slouken@91
   527
		static const char * const ps2mice[] = {
slouken@69
   528
		    "/dev/input/mice", "/dev/usbmouse", "/dev/psaux", NULL
slouken@69
   529
		};
slouken@0
   530
		/* First try to use GPM in repeater mode */
slouken@0
   531
		if ( mouse_fd < 0 ) {
slouken@0
   532
			if ( gpm_available() ) {
slouken@0
   533
				mouse_fd = open(GPM_NODE_FIFO, O_RDONLY, 0);
slouken@0
   534
				if ( mouse_fd >= 0 ) {
slouken@0
   535
#ifdef DEBUG_MOUSE
slouken@0
   536
fprintf(stderr, "Using GPM mouse\n");
slouken@0
   537
#endif
slouken@109
   538
					mouse_drv = MOUSE_MSC;
slouken@0
   539
				}
slouken@0
   540
			}
slouken@0
   541
		}
slouken@69
   542
		/* Now try to use a modern PS/2 mouse */
slouken@69
   543
		for ( i=0; (mouse_fd < 0) && ps2mice[i]; ++i ) {
slouken@69
   544
			mouse_fd = open(ps2mice[i], O_RDWR, 0);
slouken@59
   545
			if (mouse_fd < 0) {
slouken@69
   546
				mouse_fd = open(ps2mice[i], O_RDONLY, 0);
slouken@59
   547
			}
slouken@59
   548
			if (mouse_fd >= 0) {
slouken@69
   549
				/* rcg06112001 Attempt to set IMPS/2 mode */
slouken@69
   550
				if ( i == 0 ) {
slouken@69
   551
					set_imps2_mode(mouse_fd);
slouken@69
   552
				}
slouken@59
   553
				if (detect_imps2(mouse_fd)) {
slouken@0
   554
#ifdef DEBUG_MOUSE
slouken@69
   555
fprintf(stderr, "Using IMPS2 mouse\n");
slouken@0
   556
#endif
slouken@0
   557
					mouse_drv = MOUSE_IMPS2;
slouken@0
   558
				} else {
slouken@0
   559
#ifdef DEBUG_MOUSE
slouken@69
   560
fprintf(stderr, "Using PS2 mouse\n");
slouken@0
   561
#endif
slouken@0
   562
					mouse_drv = MOUSE_PS2;
slouken@0
   563
				}
slouken@0
   564
			}
slouken@0
   565
		}
slouken@0
   566
		/* Next try to use a PPC ADB port mouse */
slouken@0
   567
		if ( mouse_fd < 0 ) {
slouken@0
   568
			mouse_fd = open("/dev/adbmouse", O_RDONLY, 0);
slouken@0
   569
			if ( mouse_fd >= 0 ) {
slouken@0
   570
#ifdef DEBUG_MOUSE
slouken@0
   571
fprintf(stderr, "Using ADB mouse\n");
slouken@0
   572
#endif
slouken@0
   573
				mouse_drv = MOUSE_BM;
slouken@0
   574
			}
slouken@0
   575
		}
slouken@0
   576
	}
slouken@0
   577
	/* Default to a serial Microsoft mouse */
slouken@0
   578
	if ( mouse_fd < 0 ) {
slouken@0
   579
		if ( mousedev == NULL ) {
slouken@0
   580
			mousedev = "/dev/mouse";
slouken@0
   581
		}
slouken@0
   582
		mouse_fd = open(mousedev, O_RDONLY, 0);
slouken@0
   583
		if ( mouse_fd >= 0 ) {
slouken@0
   584
			struct termios mouse_termios;
slouken@0
   585
slouken@0
   586
			/* Set the sampling speed to 1200 baud */
slouken@0
   587
			tcgetattr(mouse_fd, &mouse_termios);
slouken@0
   588
			mouse_termios.c_iflag = IGNBRK | IGNPAR;
slouken@0
   589
			mouse_termios.c_oflag = 0;
slouken@0
   590
			mouse_termios.c_lflag = 0;
slouken@0
   591
			mouse_termios.c_line = 0;
slouken@0
   592
			mouse_termios.c_cc[VTIME] = 0;
slouken@0
   593
			mouse_termios.c_cc[VMIN] = 1;
slouken@0
   594
			mouse_termios.c_cflag = CREAD | CLOCAL | HUPCL;
slouken@0
   595
			mouse_termios.c_cflag |= CS8;
slouken@0
   596
			mouse_termios.c_cflag |= B1200;
slouken@0
   597
			tcsetattr(mouse_fd, TCSAFLUSH, &mouse_termios);
slouken@0
   598
#ifdef DEBUG_MOUSE
slouken@0
   599
fprintf(stderr, "Using Microsoft mouse on %s\n", mousedev);
slouken@0
   600
#endif
slouken@0
   601
			mouse_drv = MOUSE_MS;
slouken@0
   602
		}
slouken@0
   603
	}
slouken@0
   604
	if ( mouse_fd < 0 ) {
slouken@0
   605
		mouse_drv = MOUSE_NONE;
slouken@0
   606
	}
slouken@0
   607
	return(mouse_fd);
slouken@0
   608
}
slouken@0
   609
slouken@0
   610
static int posted = 0;
slouken@0
   611
slouken@0
   612
void FB_vgamousecallback(int button, int relative, int dx, int dy)
slouken@0
   613
{
slouken@0
   614
	int button_1, button_3;
slouken@0
   615
	int button_state;
slouken@0
   616
	int state_changed;
slouken@0
   617
	int i;
slouken@0
   618
	Uint8 state;
slouken@0
   619
slouken@0
   620
	if ( dx || dy ) {
slouken@0
   621
		posted += SDL_PrivateMouseMotion(0, relative, dx, dy);
slouken@0
   622
	}
slouken@0
   623
slouken@0
   624
	/* Swap button 1 and 3 */
slouken@0
   625
	button_1 = (button & 0x04) >> 2;
slouken@0
   626
	button_3 = (button & 0x01) << 2;
slouken@0
   627
	button &= ~0x05;
slouken@0
   628
	button |= (button_1|button_3);
slouken@0
   629
slouken@0
   630
	/* See what changed */
slouken@0
   631
	button_state = SDL_GetMouseState(NULL, NULL);
slouken@0
   632
	state_changed = button_state ^ button;
slouken@0
   633
	for ( i=0; i<8; ++i ) {
slouken@0
   634
		if ( state_changed & (1<<i) ) {
slouken@0
   635
			if ( button & (1<<i) ) {
slouken@0
   636
				state = SDL_PRESSED;
slouken@0
   637
			} else {
slouken@0
   638
				state = SDL_RELEASED;
slouken@0
   639
			}
slouken@0
   640
			posted += SDL_PrivateMouseButton(state, i+1, 0, 0);
slouken@0
   641
		}
slouken@0
   642
	}
slouken@0
   643
}
slouken@0
   644
slouken@109
   645
/* For now, use MSC, PS/2, and MS protocols
slouken@0
   646
   Driver adapted from the SVGAlib mouse driver code (taken from gpm, etc.)
slouken@0
   647
 */
slouken@0
   648
static void handle_mouse(_THIS)
slouken@0
   649
{
slouken@0
   650
	static int start = 0;
slouken@0
   651
	static unsigned char mousebuf[BUFSIZ];
slouken@0
   652
	static int relative = 1;
slouken@0
   653
slouken@0
   654
	int i, nread;
slouken@0
   655
	int button = 0;
slouken@0
   656
	int dx = 0, dy = 0;
slouken@0
   657
	int packetsize = 0;
slouken@4
   658
	int realx, realy;
slouken@4
   659
	
slouken@0
   660
	/* Figure out the mouse packet size */
slouken@0
   661
	switch (mouse_drv) {
slouken@0
   662
		case MOUSE_NONE:
slouken@0
   663
			/* Ack! */
slouken@0
   664
			read(mouse_fd, mousebuf, BUFSIZ);
slouken@0
   665
			return;
slouken@109
   666
		case MOUSE_MSC:
slouken@0
   667
			packetsize = 5;
slouken@0
   668
			break;
slouken@0
   669
		case MOUSE_IMPS2:
slouken@0
   670
			packetsize = 4;
slouken@0
   671
			break;
slouken@0
   672
		case MOUSE_PS2:
slouken@0
   673
		case MOUSE_MS:
slouken@0
   674
		case MOUSE_BM:
slouken@0
   675
			packetsize = 3;
slouken@0
   676
			break;
slouken@0
   677
		case MOUSE_ELO:
slouken@0
   678
			packetsize = ELO_PACKET_SIZE;
slouken@0
   679
			relative = 0;
slouken@0
   680
			break;
slouken@0
   681
		case NUM_MOUSE_DRVS:
slouken@0
   682
			/* Uh oh.. */
slouken@0
   683
			packetsize = 0;
slouken@0
   684
			break;
slouken@0
   685
	}
slouken@0
   686
slouken@4
   687
	/* Special handling for the quite sensitive ELO controller */
slouken@4
   688
	if (mouse_drv == MOUSE_ELO) {
slouken@4
   689
	
slouken@4
   690
	    /* try to read the next packet */
slouken@4
   691
	    if(eloReadPosition(this, mouse_fd, &dx, &dy, &button, &realx, &realy)) {
slouken@4
   692
		button = (button & 0x01) << 2;
slouken@4
   693
    		FB_vgamousecallback(button, relative, dx, dy);
slouken@4
   694
	    }
slouken@4
   695
	    
slouken@4
   696
	    return;
slouken@4
   697
	}
slouken@4
   698
	
slouken@0
   699
	/* Read as many packets as possible */
slouken@0
   700
	nread = read(mouse_fd, &mousebuf[start], BUFSIZ-start);
slouken@0
   701
	if ( nread < 0 ) {
slouken@0
   702
		return;
slouken@0
   703
	}
slouken@0
   704
	nread += start;
slouken@0
   705
#ifdef DEBUG_MOUSE
slouken@0
   706
	fprintf(stderr, "Read %d bytes from mouse, start = %d\n", nread, start);
slouken@0
   707
#endif
slouken@0
   708
	for ( i=0; i<(nread-(packetsize-1)); i += packetsize ) {
slouken@0
   709
		switch (mouse_drv) {
slouken@0
   710
			case MOUSE_NONE:
slouken@0
   711
				break;
slouken@109
   712
			case MOUSE_MSC:
slouken@109
   713
				/* MSC protocol has 0x80 in high byte */
slouken@0
   714
				if ( (mousebuf[i] & 0xF8) != 0x80 ) {
slouken@0
   715
					/* Go to next byte */
slouken@0
   716
					i -= (packetsize-1);
slouken@0
   717
					continue;
slouken@0
   718
				}
slouken@0
   719
				/* Get current mouse state */
slouken@0
   720
				button = (~mousebuf[i]) & 0x07;
slouken@0
   721
				dx =   (signed char)(mousebuf[i+1]) +
slouken@0
   722
				       (signed char)(mousebuf[i+3]);
slouken@0
   723
				dy = -((signed char)(mousebuf[i+2]) +
slouken@0
   724
				       (signed char)(mousebuf[i+4]));
slouken@0
   725
				break;
slouken@0
   726
			case MOUSE_PS2:
slouken@0
   727
				/* PS/2 protocol has nothing in high byte */
slouken@0
   728
				if ( (mousebuf[i] & 0xC0) != 0 ) {
slouken@0
   729
					/* Go to next byte */
slouken@0
   730
					i -= (packetsize-1);
slouken@0
   731
					continue;
slouken@0
   732
				}
slouken@0
   733
				/* Get current mouse state */
slouken@0
   734
				button = (mousebuf[i] & 0x04) >> 1 | /*Middle*/
slouken@0
   735
		  			 (mousebuf[i] & 0x02) >> 1 | /*Right*/
slouken@0
   736
		  			 (mousebuf[i] & 0x01) << 2;  /*Left*/
slouken@0
   737
		  		dx = (mousebuf[i] & 0x10) ?
slouken@0
   738
		  		      mousebuf[i+1] - 256 : mousebuf[i+1];
slouken@0
   739
		  		dy = (mousebuf[i] & 0x20) ?
slouken@0
   740
		  		      -(mousebuf[i+2] - 256) : -mousebuf[i+2];
slouken@0
   741
				break;
slouken@0
   742
			case MOUSE_IMPS2:
slouken@0
   743
				/* Get current mouse state */
slouken@0
   744
				button = (mousebuf[i] & 0x04) >> 1 | /*Middle*/
slouken@0
   745
		  			 (mousebuf[i] & 0x02) >> 1 | /*Right*/
slouken@0
   746
		  			 (mousebuf[i] & 0x01) << 2 | /*Left*/
slouken@0
   747
		  			 (mousebuf[i] & 0x40) >> 3 | /* 4 */
slouken@0
   748
		  			 (mousebuf[i] & 0x80) >> 3;  /* 5 */
slouken@0
   749
		  		dx = (mousebuf[i] & 0x10) ?
slouken@0
   750
		  		      mousebuf[i+1] - 256 : mousebuf[i+1];
slouken@0
   751
		  		dy = (mousebuf[i] & 0x20) ?
slouken@0
   752
		  		      -(mousebuf[i+2] - 256) : -mousebuf[i+2];
slouken@0
   753
				switch (mousebuf[i+3]&0x0F) {
slouken@0
   754
				    case 0x0E: /* DX = +1 */
slouken@0
   755
				    case 0x02: /* DX = -1 */
slouken@0
   756
					break;
slouken@0
   757
				    case 0x0F: /* DY = +1 (map button 4) */
slouken@0
   758
					button |= (1<<3);
slouken@0
   759
					break;
slouken@0
   760
				    case 0x01: /* DY = -1 (map button 5) */
slouken@0
   761
					button |= (1<<4);
slouken@0
   762
					break;
slouken@0
   763
				}
slouken@0
   764
				break;
slouken@0
   765
			case MOUSE_MS:
slouken@0
   766
				/* Microsoft protocol has 0x40 in high byte */
slouken@0
   767
				if ( (mousebuf[i] & 0x40) != 0x40 ) {
slouken@0
   768
					/* Go to next byte */
slouken@0
   769
					i -= (packetsize-1);
slouken@0
   770
					continue;
slouken@0
   771
				}
slouken@0
   772
				/* Get current mouse state */
slouken@0
   773
				button = ((mousebuf[i] & 0x20) >> 3) |
slouken@0
   774
				         ((mousebuf[i] & 0x10) >> 4);
slouken@0
   775
				dx = (signed char)(((mousebuf[i] & 0x03) << 6) |
slouken@0
   776
				                   (mousebuf[i + 1] & 0x3F));
slouken@0
   777
				dy = (signed char)(((mousebuf[i] & 0x0C) << 4) |
slouken@0
   778
				                    (mousebuf[i + 2] & 0x3F));
slouken@0
   779
				break;
slouken@0
   780
			case MOUSE_BM:
slouken@0
   781
				/* BusMouse protocol has 0xF8 in high byte */
slouken@0
   782
				if ( (mousebuf[i] & 0xF8) != 0x80 ) {
slouken@0
   783
					/* Go to next byte */
slouken@0
   784
					i -= (packetsize-1);
slouken@0
   785
					continue;
slouken@0
   786
				}
slouken@0
   787
				/* Get current mouse state */
slouken@0
   788
				button = (~mousebuf[i]) & 0x07;
slouken@0
   789
				dx =  (signed char)mousebuf[i+1];
slouken@0
   790
				dy = -(signed char)mousebuf[i+2];
slouken@0
   791
				break;
slouken@4
   792
			/*
slouken@0
   793
			case MOUSE_ELO:
slouken@0
   794
				if ( mousebuf[i] != ELO_START_BYTE ) {
slouken@0
   795
					i -= (packetsize-1);
slouken@0
   796
					continue;
slouken@0
   797
				}
slouken@0
   798
slouken@0
   799
				if(!eloParsePacket(&(mousebuf[i]), &dx, &dy, &button)) {
slouken@4
   800
					i -= (packetsize-1);
slouken@4
   801
					continue;
slouken@0
   802
				}
slouken@0
   803
				
slouken@0
   804
				button = (button & 0x01) << 2;
slouken@0
   805
slouken@0
   806
				eloConvertXY(this, &dx, &dy);
slouken@0
   807
				break;
slouken@4
   808
			*/
slouken@0
   809
slouken@4
   810
			case MOUSE_ELO:
slouken@0
   811
			case NUM_MOUSE_DRVS:
slouken@0
   812
				/* Uh oh.. */
slouken@0
   813
				dx = 0;
slouken@0
   814
				dy = 0;
slouken@0
   815
				break;
slouken@0
   816
		}
slouken@0
   817
		FB_vgamousecallback(button, relative, dx, dy);
slouken@0
   818
	}
slouken@0
   819
	if ( i < nread ) {
slouken@0
   820
		memcpy(mousebuf, &mousebuf[i], (nread-i));
slouken@0
   821
		start = (nread-i);
slouken@0
   822
	} else {
slouken@0
   823
		start = 0;
slouken@0
   824
	}
slouken@0
   825
	return;
slouken@0
   826
}
slouken@0
   827
slouken@109
   828
/* Handle switching to another VC, returns when our VC is back.
slouken@109
   829
   This isn't necessarily the best solution.  For SDL 1.3 we need
slouken@109
   830
   a way of notifying the application when we lose access to the
slouken@109
   831
   video hardware and when we regain it.
slouken@109
   832
 */
slouken@0
   833
static void switch_vt(_THIS, unsigned short which)
slouken@0
   834
{
slouken@0
   835
	struct vt_stat vtstate;
slouken@0
   836
	unsigned short current;
slouken@0
   837
	SDL_Surface *screen;
slouken@0
   838
	__u16 saved_pal[3*256];
slouken@0
   839
	Uint32 screen_arealen;
slouken@0
   840
	Uint8 *screen_contents;
slouken@0
   841
slouken@0
   842
	/* Figure out whether or not we're switching to a new console */
slouken@0
   843
	if ( (ioctl(keyboard_fd, VT_GETSTATE, &vtstate) < 0) ||
slouken@0
   844
	     (which == vtstate.v_active) ) {
slouken@0
   845
		return;
slouken@0
   846
	}
slouken@0
   847
	current = vtstate.v_active;
slouken@0
   848
slouken@0
   849
	/* Save the contents of the screen, and go to text mode */
slouken@0
   850
	SDL_mutexP(hw_lock);
slouken@109
   851
	wait_idle(this);
slouken@0
   852
	screen = SDL_VideoSurface;
slouken@0
   853
	screen_arealen = (screen->h*screen->pitch);
slouken@0
   854
	screen_contents = (Uint8 *)malloc(screen_arealen);
slouken@0
   855
	if ( screen_contents ) {
slouken@0
   856
		memcpy(screen_contents, screen->pixels, screen_arealen);
slouken@0
   857
	}
slouken@0
   858
	FB_SavePaletteTo(this, 256, saved_pal);
slouken@0
   859
	ioctl(keyboard_fd, KDSETMODE, KD_TEXT);
slouken@0
   860
slouken@0
   861
	/* New console, switch to it */
slouken@0
   862
	if ( ioctl(keyboard_fd, VT_ACTIVATE, which) == 0 ) {
slouken@0
   863
		/* Wait for our console to be activated again */
slouken@0
   864
		ioctl(keyboard_fd, VT_WAITACTIVE, which);
slouken@0
   865
		while ( ioctl(keyboard_fd, VT_WAITACTIVE, current) < 0 ) {
slouken@0
   866
			if ( (errno != EINTR) && (errno != EAGAIN) ) {
slouken@0
   867
				/* Unknown VT error - cancel this */
slouken@0
   868
				break;
slouken@0
   869
			}
slouken@0
   870
			SDL_Delay(500);
slouken@0
   871
		}
slouken@0
   872
	}
slouken@0
   873
slouken@0
   874
	/* Restore graphics mode and the contents of the screen */
slouken@0
   875
	ioctl(keyboard_fd, KDSETMODE, KD_GRAPHICS);
slouken@0
   876
	FB_RestorePaletteFrom(this, 256, saved_pal);
slouken@0
   877
	if ( screen_contents ) {
slouken@0
   878
		memcpy(screen->pixels, screen_contents, screen_arealen);
slouken@0
   879
		free(screen_contents);
slouken@0
   880
	}
slouken@0
   881
	SDL_mutexV(hw_lock);
slouken@0
   882
}
slouken@0
   883
slouken@0
   884
static void handle_keyboard(_THIS)
slouken@0
   885
{
slouken@0
   886
	unsigned char keybuf[BUFSIZ];
slouken@0
   887
	int i, nread;
slouken@0
   888
	int pressed;
slouken@0
   889
	int scancode;
slouken@0
   890
	SDL_keysym keysym;
slouken@0
   891
slouken@0
   892
	nread = read(keyboard_fd, keybuf, BUFSIZ);
slouken@0
   893
	for ( i=0; i<nread; ++i ) {
slouken@0
   894
		scancode = keybuf[i] & 0x7F;
slouken@0
   895
		if ( keybuf[i] & 0x80 ) {
slouken@0
   896
			pressed = SDL_RELEASED;
slouken@0
   897
		} else {
slouken@0
   898
			pressed = SDL_PRESSED;
slouken@0
   899
		}
slouken@0
   900
		TranslateKey(scancode, &keysym);
slouken@0
   901
		/* Handle Alt-FN for vt switch */
slouken@0
   902
		switch (keysym.sym) {
slouken@0
   903
		    case SDLK_F1:
slouken@0
   904
		    case SDLK_F2:
slouken@0
   905
		    case SDLK_F3:
slouken@0
   906
		    case SDLK_F4:
slouken@0
   907
		    case SDLK_F5:
slouken@0
   908
		    case SDLK_F6:
slouken@0
   909
		    case SDLK_F7:
slouken@0
   910
		    case SDLK_F8:
slouken@0
   911
		    case SDLK_F9:
slouken@0
   912
		    case SDLK_F10:
slouken@0
   913
		    case SDLK_F11:
slouken@0
   914
		    case SDLK_F12:
slouken@0
   915
			if ( SDL_GetModState() & KMOD_ALT ) {
slouken@109
   916
				if ( pressed ) {
slouken@109
   917
					switch_vt(this, (keysym.sym-SDLK_F1)+1);
slouken@109
   918
				}
slouken@0
   919
				break;
slouken@0
   920
			}
slouken@0
   921
			/* Fall through to normal processing */
slouken@0
   922
		    default:
slouken@0
   923
			posted += SDL_PrivateKeyboard(pressed, &keysym);
slouken@0
   924
			break;
slouken@0
   925
		}
slouken@0
   926
	}
slouken@0
   927
}
slouken@0
   928
slouken@0
   929
void FB_PumpEvents(_THIS)
slouken@0
   930
{
slouken@0
   931
	fd_set fdset;
slouken@0
   932
	int max_fd;
slouken@0
   933
	static struct timeval zero;
slouken@0
   934
slouken@0
   935
	do {
slouken@0
   936
		posted = 0;
slouken@0
   937
slouken@0
   938
		FD_ZERO(&fdset);
slouken@0
   939
		max_fd = 0;
slouken@0
   940
		if ( keyboard_fd >= 0 ) {
slouken@0
   941
			FD_SET(keyboard_fd, &fdset);
slouken@0
   942
			if ( max_fd < keyboard_fd ) {
slouken@0
   943
				max_fd = keyboard_fd;
slouken@0
   944
			}
slouken@0
   945
		}
slouken@0
   946
		if ( mouse_fd >= 0 ) {
slouken@0
   947
			FD_SET(mouse_fd, &fdset);
slouken@0
   948
			if ( max_fd < mouse_fd ) {
slouken@0
   949
				max_fd = mouse_fd;
slouken@0
   950
			}
slouken@0
   951
		}
slouken@0
   952
		if ( select(max_fd+1, &fdset, NULL, NULL, &zero) > 0 ) {
slouken@0
   953
			if ( keyboard_fd >= 0 ) {
slouken@0
   954
				if ( FD_ISSET(keyboard_fd, &fdset) ) {
slouken@0
   955
					handle_keyboard(this);
slouken@0
   956
				}
slouken@0
   957
			}
slouken@0
   958
			if ( mouse_fd >= 0 ) {
slouken@0
   959
				if ( FD_ISSET(mouse_fd, &fdset) ) {
slouken@0
   960
					handle_mouse(this);
slouken@0
   961
				}
slouken@0
   962
			}
slouken@0
   963
		}
slouken@0
   964
	} while ( posted );
slouken@0
   965
}
slouken@0
   966
slouken@0
   967
void FB_InitOSKeymap(_THIS)
slouken@0
   968
{
slouken@0
   969
	int i;
slouken@0
   970
slouken@0
   971
	/* Initialize the Linux key translation table */
slouken@0
   972
slouken@0
   973
	/* First get the ascii keys and others not well handled */
slouken@0
   974
	for (i=0; i<SDL_TABLESIZE(keymap); ++i) {
slouken@0
   975
	  switch(i) {
slouken@0
   976
	  /* These aren't handled by the x86 kernel keymapping (?) */
slouken@0
   977
	  case SCANCODE_PRINTSCREEN:
slouken@0
   978
	    keymap[i] = SDLK_PRINT;
slouken@0
   979
	    break;
slouken@0
   980
	  case SCANCODE_BREAK:
slouken@0
   981
	    keymap[i] = SDLK_BREAK;
slouken@0
   982
	    break;
slouken@0
   983
	  case SCANCODE_BREAK_ALTERNATIVE:
slouken@0
   984
	    keymap[i] = SDLK_PAUSE;
slouken@0
   985
	    break;
slouken@0
   986
	  case SCANCODE_LEFTSHIFT:
slouken@0
   987
	    keymap[i] = SDLK_LSHIFT;
slouken@0
   988
	    break;
slouken@0
   989
	  case SCANCODE_RIGHTSHIFT:
slouken@0
   990
	    keymap[i] = SDLK_RSHIFT;
slouken@0
   991
	    break;
slouken@0
   992
	  case SCANCODE_LEFTCONTROL:
slouken@0
   993
	    keymap[i] = SDLK_LCTRL;
slouken@0
   994
	    break;
slouken@0
   995
	  case SCANCODE_RIGHTCONTROL:
slouken@0
   996
	    keymap[i] = SDLK_RCTRL;
slouken@0
   997
	    break;
slouken@0
   998
	  case SCANCODE_RIGHTWIN:
slouken@0
   999
	    keymap[i] = SDLK_RSUPER;
slouken@0
  1000
	    break;
slouken@0
  1001
	  case SCANCODE_LEFTWIN:
slouken@0
  1002
	    keymap[i] = SDLK_LSUPER;
slouken@0
  1003
	    break;
slouken@0
  1004
	  case 127:
slouken@0
  1005
	    keymap[i] = SDLK_MENU;
slouken@0
  1006
	    break;
slouken@0
  1007
	  /* this should take care of all standard ascii keys */
slouken@0
  1008
	  default:
slouken@0
  1009
	    keymap[i] = KVAL(vga_keymap[0][i]);
slouken@0
  1010
	    break;
slouken@0
  1011
          }
slouken@0
  1012
	}
slouken@0
  1013
	for (i=0; i<SDL_TABLESIZE(keymap); ++i) {
slouken@0
  1014
	  switch(keymap_temp[i]) {
slouken@0
  1015
	    case K_F1:  keymap[i] = SDLK_F1;  break;
slouken@0
  1016
	    case K_F2:  keymap[i] = SDLK_F2;  break;
slouken@0
  1017
	    case K_F3:  keymap[i] = SDLK_F3;  break;
slouken@0
  1018
	    case K_F4:  keymap[i] = SDLK_F4;  break;
slouken@0
  1019
	    case K_F5:  keymap[i] = SDLK_F5;  break;
slouken@0
  1020
	    case K_F6:  keymap[i] = SDLK_F6;  break;
slouken@0
  1021
	    case K_F7:  keymap[i] = SDLK_F7;  break;
slouken@0
  1022
	    case K_F8:  keymap[i] = SDLK_F8;  break;
slouken@0
  1023
	    case K_F9:  keymap[i] = SDLK_F9;  break;
slouken@0
  1024
	    case K_F10: keymap[i] = SDLK_F10; break;
slouken@0
  1025
	    case K_F11: keymap[i] = SDLK_F11; break;
slouken@0
  1026
	    case K_F12: keymap[i] = SDLK_F12; break;
slouken@0
  1027
slouken@0
  1028
	    case K_DOWN:  keymap[i] = SDLK_DOWN;  break;
slouken@0
  1029
	    case K_LEFT:  keymap[i] = SDLK_LEFT;  break;
slouken@0
  1030
	    case K_RIGHT: keymap[i] = SDLK_RIGHT; break;
slouken@0
  1031
	    case K_UP:    keymap[i] = SDLK_UP;    break;
slouken@0
  1032
slouken@0
  1033
	    case K_P0:     keymap[i] = SDLK_KP0; break;
slouken@0
  1034
	    case K_P1:     keymap[i] = SDLK_KP1; break;
slouken@0
  1035
	    case K_P2:     keymap[i] = SDLK_KP2; break;
slouken@0
  1036
	    case K_P3:     keymap[i] = SDLK_KP3; break;
slouken@0
  1037
	    case K_P4:     keymap[i] = SDLK_KP4; break;
slouken@0
  1038
	    case K_P5:     keymap[i] = SDLK_KP5; break;
slouken@0
  1039
	    case K_P6:     keymap[i] = SDLK_KP6; break;
slouken@0
  1040
	    case K_P7:     keymap[i] = SDLK_KP7; break;
slouken@0
  1041
	    case K_P8:     keymap[i] = SDLK_KP8; break;
slouken@0
  1042
	    case K_P9:     keymap[i] = SDLK_KP9; break;
slouken@0
  1043
	    case K_PPLUS:  keymap[i] = SDLK_KP_PLUS; break;
slouken@0
  1044
	    case K_PMINUS: keymap[i] = SDLK_KP_MINUS; break;
slouken@0
  1045
	    case K_PSTAR:  keymap[i] = SDLK_KP_MULTIPLY; break;
slouken@0
  1046
	    case K_PSLASH: keymap[i] = SDLK_KP_DIVIDE; break;
slouken@0
  1047
	    case K_PENTER: keymap[i] = SDLK_KP_ENTER; break;
slouken@0
  1048
	    case K_PDOT:   keymap[i] = SDLK_KP_PERIOD; break;
slouken@0
  1049
slouken@0
  1050
	    case K_SHIFT:  if ( keymap[i] != SDLK_RSHIFT )
slouken@0
  1051
	                     keymap[i] = SDLK_LSHIFT;
slouken@0
  1052
	                   break;
slouken@0
  1053
	    case K_SHIFTL: keymap[i] = SDLK_LSHIFT; break;
slouken@0
  1054
	    case K_SHIFTR: keymap[i] = SDLK_RSHIFT; break;
slouken@0
  1055
	    case K_CTRL:  if ( keymap[i] != SDLK_RCTRL )
slouken@0
  1056
	                     keymap[i] = SDLK_LCTRL;
slouken@0
  1057
	                   break;
slouken@0
  1058
	    case K_CTRLL:  keymap[i] = SDLK_LCTRL;  break;
slouken@0
  1059
	    case K_CTRLR:  keymap[i] = SDLK_RCTRL;  break;
slouken@0
  1060
	    case K_ALT:    keymap[i] = SDLK_LALT;   break;
slouken@0
  1061
	    case K_ALTGR:  keymap[i] = SDLK_RALT;   break;
slouken@0
  1062
slouken@0
  1063
	    case K_INSERT: keymap[i] = SDLK_INSERT;   break;
slouken@0
  1064
	    case K_REMOVE: keymap[i] = SDLK_DELETE;   break;
slouken@0
  1065
	    case K_PGUP:   keymap[i] = SDLK_PAGEUP;   break;
slouken@0
  1066
	    case K_PGDN:   keymap[i] = SDLK_PAGEDOWN; break;
slouken@0
  1067
	    case K_FIND:   keymap[i] = SDLK_HOME;     break;
slouken@0
  1068
	    case K_SELECT: keymap[i] = SDLK_END;      break;
slouken@0
  1069
slouken@0
  1070
	    case K_NUM:  keymap[i] = SDLK_NUMLOCK;   break;
slouken@0
  1071
	    case K_CAPS: keymap[i] = SDLK_CAPSLOCK;  break;
slouken@0
  1072
slouken@0
  1073
	    case K_F13:   keymap[i] = SDLK_PRINT;     break;
slouken@0
  1074
	    case K_HOLD:  keymap[i] = SDLK_SCROLLOCK; break;
slouken@0
  1075
	    case K_PAUSE: keymap[i] = SDLK_PAUSE;     break;
slouken@0
  1076
slouken@0
  1077
	    case 127: keymap[i] = SDLK_BACKSPACE; break;
slouken@0
  1078
	     
slouken@0
  1079
	    default: break;
slouken@0
  1080
	  }
slouken@0
  1081
	}
slouken@0
  1082
}
slouken@0
  1083
slouken@0
  1084
static SDL_keysym *TranslateKey(int scancode, SDL_keysym *keysym)
slouken@0
  1085
{
slouken@0
  1086
	/* Set the keysym information */
slouken@0
  1087
	keysym->scancode = scancode;
slouken@0
  1088
	keysym->sym = keymap[scancode];
slouken@0
  1089
	keysym->mod = KMOD_NONE;
slouken@0
  1090
slouken@0
  1091
	/* If UNICODE is on, get the UNICODE value for the key */
slouken@0
  1092
	keysym->unicode = 0;
slouken@0
  1093
	if ( SDL_TranslateUNICODE ) {
slouken@0
  1094
		int map;
slouken@0
  1095
		SDLMod modstate;
slouken@0
  1096
slouken@0
  1097
		modstate = SDL_GetModState();
slouken@0
  1098
		map = 0;
slouken@0
  1099
		if ( modstate & KMOD_SHIFT ) {
slouken@0
  1100
			map |= (1<<KG_SHIFT);
slouken@0
  1101
		}
slouken@0
  1102
		if ( modstate & KMOD_CTRL ) {
slouken@0
  1103
			map |= (1<<KG_CTRL);
slouken@0
  1104
		}
slouken@0
  1105
		if ( modstate & KMOD_ALT ) {
slouken@0
  1106
			map |= (1<<KG_ALT);
slouken@0
  1107
		}
slouken@0
  1108
		if ( modstate & KMOD_MODE ) {
slouken@0
  1109
			map |= (1<<KG_ALTGR);
slouken@0
  1110
		}
slouken@0
  1111
		if ( KTYP(vga_keymap[map][scancode]) == KT_LETTER ) {
slouken@0
  1112
			if ( modstate & KMOD_CAPS ) {
slouken@0
  1113
				map ^= (1<<KG_SHIFT);
slouken@0
  1114
			}
slouken@0
  1115
		}
slouken@0
  1116
		if ( KTYP(vga_keymap[map][scancode]) == KT_PAD ) {
slouken@0
  1117
			if ( modstate & KMOD_NUM ) {
slouken@0
  1118
				keysym->unicode=KVAL(vga_keymap[map][scancode]);
slouken@0
  1119
			}
slouken@0
  1120
		} else {
slouken@0
  1121
			keysym->unicode = KVAL(vga_keymap[map][scancode]);
slouken@0
  1122
		}
slouken@0
  1123
	}
slouken@0
  1124
	return(keysym);
slouken@0
  1125
}