src/video/fbcon/SDL_fbevents.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 12 Dec 2005 09:26:32 +0000
changeset 1201 718d00094f82
parent 1022 3d4f1930ed02
child 1241 4b2146866b82
permissions -rw-r--r--
Date: Sat, 10 Dec 2005 18:29:41 +0100
From: Alberto Mardegan <mardy@users.sourceforge.net>
To: sdl@libsdl.org
Subject: [SDL] [PATCH] Touchscreen support to fbcon via tslib


Hi all!

I'm new to this list, I just subscribed. I've attached to this e-mail
a patch I've written in order to add Touchscreen support to SDL's fbcon
module via the tslib library.
Since it introduces a new dependency, I've also edited the the
configure.in file and added it as a compile-time option.

This patch is especially useful for handhelds (I've tested it in my
Zaurus).
Please consider applying it. :-)


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