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