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