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