event.c
来自「适合KS8695X」· C语言 代码 · 共 1,361 行 · 第 1/3 页
C
1,361 行
ke.kb_table |= K_SHIFTTAB;
if (_PM_modifiers & (EVT_LEFTALT | EVT_ALTSTATE))
ke.kb_table |= K_ALTTAB;
if (ioctl(_PM_console_fd, KDGKBENT, (unsigned long)&ke)<0)
perror("ioctl(KDGKBENT)");
if ((_PM_leds & LED_NUM) && (getKeyMapping(keypad, NB_KEYPAD, c)!=c)) {
asc = getKeyMapping(keypad, NB_KEYPAD, c);
}
else {
switch (c) {
case 14:
asc = ASCII_backspace;
break;
case 15:
asc = ASCII_tab;
break;
case 28:
case 96:
asc = ASCII_enter;
break;
case 1:
asc = ASCII_esc;
default:
asc = ke.kb_value & 0xFF;
if (asc < 0x1B)
asc = 0;
break;
}
}
if ((_PM_modifiers & (EVT_CTRLSTATE|EVT_LEFTCTRL)) && isalpha(asc))
evt.message |= toupper(asc) - 'A' + 1;
else
evt.message |= asc;
keyUpMsg[c] = evt.message;
repeatKey[c]++;
}
if (EVT.count < EVENTQSIZE)
addEvent(&evt);
}
}
}
/* Poll mouse events */
if (_EVT_mouse_fd) {
int dx, dy, buts;
static int oldbuts;
while (dataReady(_EVT_mouse_fd)) {
if (readMouseData(&buts, &dx, &dy)) {
EVT.mx += dx;
EVT.my += dy;
if (EVT.mx < 0) EVT.mx = 0;
if (EVT.my < 0) EVT.my = 0;
if (EVT.mx > range_x) EVT.mx = range_x;
if (EVT.my > range_y) EVT.my = range_y;
evt.where_x = EVT.mx;
evt.where_y = EVT.my;
evt.relative_x = dx;
evt.relative_y = dy;
/* TODO: This is wrong! We need this to be the time stamp at */
/* ** interrupt ** time!! One solution would be to */
/* put the keyboard and mouse polling loops into */
/* a separate thread that can block on I/O to the */
/* necessay file descriptor. */
evt.when = _EVT_getTicks();
evt.modifiers = _PM_modifiers;
if (buts & 4)
evt.modifiers |= EVT_LEFTBUT;
if (buts & 1)
evt.modifiers |= EVT_RIGHTBUT;
if (buts & 2)
evt.modifiers |= EVT_MIDDLEBUT;
/* Left click events */
if ((buts&4) != (oldbuts&4)) {
if (buts&4)
evt.what = EVT_MOUSEDOWN;
else
evt.what = EVT_MOUSEUP;
evt.message = EVT_LEFTBMASK;
EVT.oldMove = -1;
if (EVT.count < EVENTQSIZE)
addEvent(&evt);
}
/* Right click events */
if ((buts&1) != (oldbuts&1)) {
if (buts&1)
evt.what = EVT_MOUSEDOWN;
else
evt.what = EVT_MOUSEUP;
evt.message = EVT_RIGHTBMASK;
EVT.oldMove = -1;
if (EVT.count < EVENTQSIZE)
addEvent(&evt);
}
/* Middle click events */
if ((buts&2) != (oldbuts&2)) {
if (buts&2)
evt.what = EVT_MOUSEDOWN;
else
evt.what = EVT_MOUSEUP;
evt.message = EVT_MIDDLEBMASK;
EVT.oldMove = -1;
if (EVT.count < EVENTQSIZE)
addEvent(&evt);
}
/* Mouse movement event */
if (dx || dy) {
evt.what = EVT_MOUSEMOVE;
evt.message = 0;
if (EVT.oldMove != -1) {
/* Modify existing movement event */
EVT.evtq[EVT.oldMove].where_x = evt.where_x;
EVT.evtq[EVT.oldMove].where_y = evt.where_y;
}
else {
/* Save id of this movement event */
EVT.oldMove = EVT.freeHead;
if (EVT.count < EVENTQSIZE)
addEvent(&evt);
}
}
oldbuts = buts;
}
}
}
#ifdef USE_OS_JOYSTICK
/* Poll joystick events using the 1.x joystick driver API in the 2.2 kernels */
if (js_version & ~0xffff) {
static struct js_event js;
/* Read joystick axis 0 */
evt.when = 0;
evt.modifiers = _PM_modifiers;
if (joystick0_fd && dataReady(joystick0_fd) &&
read(joystick0_fd, &js, sizeof(js)) == sizeof(js)) {
if (js.type & JS_EVENT_BUTTON) {
if (js.number < 2) { /* Only 2 buttons for now :( */
buts0[js.number] = js.value;
evt.what = EVT_JOYCLICK;
makeJoyEvent(&evt);
if (EVT.count < EVENTQSIZE)
addEvent(&evt);
}
}
else if (js.type & JS_EVENT_AXIS) {
axis0[js.number] = scaleJoyAxis(js.value,js.number);
evt.what = EVT_JOYMOVE;
if (EVT.oldJoyMove != -1) {
makeJoyEvent(&EVT.evtq[EVT.oldJoyMove]);
}
else if (EVT.count < EVENTQSIZE) {
EVT.oldJoyMove = EVT.freeHead;
makeJoyEvent(&evt);
addEvent(&evt);
}
}
}
/* Read joystick axis 1 */
if (joystick1_fd && dataReady(joystick1_fd) &&
read(joystick1_fd, &js, sizeof(js))==sizeof(js)) {
if (js.type & JS_EVENT_BUTTON) {
if (js.number < 2) { /* Only 2 buttons for now :( */
buts1[js.number] = js.value;
evt.what = EVT_JOYCLICK;
makeJoyEvent(&evt);
if (EVT.count < EVENTQSIZE)
addEvent(&evt);
}
}
else if (js.type & JS_EVENT_AXIS) {
axis1[js.number] = scaleJoyAxis(js.value,js.number<<2);
evt.what = EVT_JOYMOVE;
if (EVT.oldJoyMove != -1) {
makeJoyEvent(&EVT.evtq[EVT.oldJoyMove]);
}
else if (EVT.count < EVENTQSIZE) {
EVT.oldJoyMove = EVT.freeHead;
makeJoyEvent(&evt);
addEvent(&evt);
}
}
}
}
#endif
}
/****************************************************************************
REMARKS:
This macro/function is used to converts the scan codes reported by the
keyboard to our event libraries normalised format. We only have one scan
code for the 'A' key, and use shift _PM_modifiers to determine if it is a
Ctrl-F1, Alt-F1 etc. The raw scan codes from the keyboard work this way,
but the OS gives us 'cooked' scan codes, we have to translate them back
to the raw format.
****************************************************************************/
#define _EVT_maskKeyCode(evt)
/****************************************************************************
REMARKS:
Set the speed of the serial port
****************************************************************************/
static int setspeed(
int fd,
int old,
int new,
unsigned short flags)
{
struct termios tty;
char *c;
tcgetattr(fd, &tty);
tty.c_iflag = IGNBRK | IGNPAR;
tty.c_oflag = 0;
tty.c_lflag = 0;
tty.c_line = 0;
tty.c_cc[VTIME] = 0;
tty.c_cc[VMIN] = 1;
switch (old) {
case 9600: tty.c_cflag = flags | B9600; break;
case 4800: tty.c_cflag = flags | B4800; break;
case 2400: tty.c_cflag = flags | B2400; break;
case 1200:
default: tty.c_cflag = flags | B1200; break;
}
tcsetattr(fd, TCSAFLUSH, &tty);
switch (new) {
case 9600: c = "*q"; tty.c_cflag = flags | B9600; break;
case 4800: c = "*p"; tty.c_cflag = flags | B4800; break;
case 2400: c = "*o"; tty.c_cflag = flags | B2400; break;
case 1200:
default: c = "*n"; tty.c_cflag = flags | B1200; break;
}
write(fd, c, 2);
usleep(100000);
tcsetattr(fd, TCSAFLUSH, &tty);
return 0;
}
/****************************************************************************
REMARKS:
Generic mouse driver init code
****************************************************************************/
static void _EVT_mouse_init(void)
{
int i;
/* Change from any available speed to the chosen one */
for (i = 9600; i >= 1200; i /= 2)
setspeed(_EVT_mouse_fd, i, opt_baud, mouse_infos[mouse_driver].flags);
}
/****************************************************************************
REMARKS:
Logitech mouse driver init code
****************************************************************************/
static void _EVT_logitech_init(void)
{
int i;
struct stat buf;
int busmouse;
/* is this a serial- or a bus- mouse? */
if (fstat(_EVT_mouse_fd,&buf) == -1)
perror("fstat");
i = MAJOR(buf.st_rdev);
if (stat("/dev/ttyS0",&buf) == -1)
perror("stat");
busmouse=(i != MAJOR(buf.st_rdev));
/* Fix the howmany field, so that serial mice have 1, while busmice have 3 */
mouse_infos[mouse_driver].read = busmouse ? 3 : 1;
/* Change from any available speed to the chosen one */
for (i = 9600; i >= 1200; i /= 2)
setspeed(_EVT_mouse_fd, i, opt_baud, mouse_infos[mouse_driver].flags);
/* This stuff is peculiar of logitech mice, also for the serial ones */
write(_EVT_mouse_fd, "S", 1);
setspeed(_EVT_mouse_fd, opt_baud, opt_baud,CS8 |PARENB |PARODD |CREAD |CLOCAL |HUPCL);
/* Configure the sample rate */
for (i = 0; opt_sample <= sampletab[i].sample; i++)
;
write(_EVT_mouse_fd,sampletab[i].code,1);
}
/****************************************************************************
REMARKS:
Microsoft Intellimouse init code
****************************************************************************/
static void _EVT_pnpmouse_init(void)
{
struct termios tty;
tcgetattr(_EVT_mouse_fd, &tty);
tty.c_iflag = IGNBRK | IGNPAR;
tty.c_oflag = 0;
tty.c_lflag = 0;
tty.c_line = 0;
tty.c_cc[VTIME] = 0;
tty.c_cc[VMIN] = 1;
tty.c_cflag = mouse_infos[mouse_driver].flags | B1200;
tcsetattr(_EVT_mouse_fd, TCSAFLUSH, &tty); /* set parameters */
}
/****************************************************************************
PARAMETERS:
mouseMove - Callback function to call wheneve the mouse needs to be moved
REMARKS:
Initiliase the event handling module. Here we install our mouse handling ISR
to be called whenever any button's are pressed or released. We also build
the free list of events in the event queue.
We use handler number 2 of the mouse libraries interrupt handlers for our
event handling routines.
****************************************************************************/
void EVTAPI EVT_init(
_EVT_mouseMoveHandler mouseMove)
{
int i;
char *tmp;
/* Initialise the event queue */
EVT.mouseMove = mouseMove;
initEventQueue();
for (i = 0; i < 256; i++)
keyUpMsg[i] = 0;
/* Keyboard initialization */
if (_PM_console_fd == -1)
PM_fatalError("You must first call PM_openConsole to use the EVT functions!");
_PM_keyboard_rawmode();
fcntl(_PM_console_fd,F_SETFL,fcntl(_PM_console_fd,F_GETFL) | O_NONBLOCK);
/* Mouse initialization */
if ((tmp = getenv(ENV_MOUSEDRV)) != NULL) {
for (i = 0; i < NB_MICE; i++) {
if (!strcasecmp(tmp, mouse_infos[i].name)) {
mouse_driver = i;
break;
}
}
if (i == NB_MICE) {
fprintf(stderr,"Unknown mouse driver: %s\n", tmp);
mouse_driver = EVT_noMouse;
_EVT_mouse_fd = 0;
}
}
if (mouse_driver != EVT_noMouse) {
if (mouse_driver == EVT_gpm)
strcpy(mouse_dev,"/dev/gpmdata");
if ((tmp = getenv(ENV_MOUSEDEV)) != NULL)
strcpy(mouse_dev,tmp);
#ifdef CHECKED
fprintf(stderr,"Using the %s MGL mouse driver on %s.\n", mouse_infos[mouse_driver].name, mouse_dev);
#endif
if ((_EVT_mouse_fd = open(mouse_dev, O_RDWR)) < 0) {
perror("open");
fprintf(stderr, "Unable to open mouse device %s, dropping mouse support.\n", mouse_dev);
sleep(1);
mouse_driver = EVT_noMouse;
_EVT_mouse_fd = 0;
}
else {
char c;
/* Init and flush the mouse pending input queue */
if (mouse_infos[mouse_driver].init)
mouse_infos[mouse_driver].init();
while(dataReady(_EVT_mouse_fd) && read(_EVT_mouse_fd, &c, 1) == 1)
;
}
}
}
/****************************************************************************
REMARKS
Changes the range of coordinates returned by the mouse functions to the
specified range of values. This is used when changing between graphics
modes set the range of mouse coordinates for the new display mode.
****************************************************************************/
void EVTAPI EVT_setMouseRange(
int xRes,
int yRes)
{
range_x = xRes;
range_y = yRes;
}
/****************************************************************************
REMARKS
Modifes the mouse coordinates as necessary if scaling to OS coordinates,
and sets the OS mouse cursor position.
****************************************************************************/
#define _EVT_setMousePos(x,y)
/****************************************************************************
REMARKS:
Initiailises the internal event handling modules. The EVT_suspend function
can be called to suspend event handling (such as when shelling out to DOS),
and this function can be used to resume it again later.
****************************************************************************/
void EVT_resume(void)
{
/* Do nothing for Linux */
}
/****************************************************************************
REMARKS
Suspends all of our event handling operations. This is also used to
de-install the event handling code.
****************************************************************************/
void EVT_suspend(void)
{
/* Do nothing for Linux */
}
/****************************************************************************
REMARKS
Exits the event module for program terminatation.
****************************************************************************/
void EVT_exit(void)
{
/* Restore signal handlers */
_PM_restore_kb_mode();
if (_EVT_mouse_fd) {
close(_EVT_mouse_fd);
_EVT_mouse_fd = 0;
}
#ifdef USE_OS_JOYSTICK
if (joystick0_fd) {
close(joystick0_fd);
free(axis0);
free(buts0);
joystick0_fd = 0;
}
if (joystick1_fd) {
close(joystick1_fd);
free(axis1);
free(buts1);
joystick1_fd = 0;
}
#endif
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?