📄 pcsim.c
字号:
//// $Id: pcsim.c,v 1.3 $//// pcsim.c: The pcsim IAL engine.// // Copyright (C) 2003, wen jian//// Created by wen jian (wen_jian_1973@163.com), 2003///*** This library is free software; you can redistribute it and/or** modify it under the terms of the GNU Library General Public** License as published by the Free Software Foundation; either** version 2 of the License, or (at your option) any later version.**** This library is distributed in the hope that it will be useful,** but WITHOUT ANY WARRANTY; without even the implied warranty of** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU** Library General Public License for more details.**** You should have received a copy of the GNU Library General Public** License along with this library; if not, write to the Free** Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,** MA 02111-1307, USA*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#ifndef __ECOS# include <sys/time.h># include <sys/types.h># include <sys/poll.h>#endif#include <sys/select.h>#include <fcntl.h>#include "common.h"#include "misc.h"#include "ial.h"#include "gal.h"#include "pcsim.h"typedef struct tagPOS{ short x; short y; short b;} POS;static int mouse_fd;static int xpos; /* current x position of mouse */static int ypos; /* current y position of mouse */static int minx; /* minimum allowed x position */static int maxx; /* maximum allowed x position */static int miny; /* minimum allowed y position */static int maxy; /* maximum allowed y position */static int buttons; /* current state of buttons *//************************ Low Level Input Operations **********************//* * Mouse operations -- Event */static int PS2_Read(int *dx, int *dy, int *bp){ static unsigned char buf[3]; static int nbytes; int n; while((n = read(mouse_fd, &buf[nbytes], 3 - nbytes))) { if(n < 0) { if ((errno == EINTR) || (errno == EAGAIN)) return 0; else return -1; } nbytes += n; if(nbytes == 3) { /* Check header byte. */ if ((buf[0] & 0x80) == 0) { buf[0] = buf[1]; buf[1] = buf[2]; nbytes = 2; return -1; } *bp = ((buf[0]>>4) & 0x07); *dx = buf[1]|((buf[0]&0x0c)<<5); *dy = buf[2]|((buf[0]&0x03)<<7); nbytes = 0; return 1; } } return 0;}static int mouse_update(void){ int r; while (1) { r = PS2_Read(&xpos, &ypos, &buttons); if (r < 0 ) return -1; if (r == 0) return 0; } return r;}static int mouse_getx(void){ return xpos;}static int mouse_gety(void){ return ypos;}static void mouse_setposition(int newx, int newy){ if (newx < minx) newx = minx; if (newx > maxx) newx = maxx; if (newy < miny) newy = miny; if (newy > maxy) newy = maxy; if (newx == xpos && newy == ypos) return; xpos = newx; ypos = newy;}static int mouse_getbutton(void){ return buttons;}static void mouse_setrange(int newminx,int newminy,int newmaxx,int newmaxy){ minx = newminx; miny = newminy; maxx = newmaxx; maxy = newmaxy; mouse_setposition ((newminx + newmaxx) / 2, (newminy + newmaxy) / 2);}static int keyboard_update(void){ return 0;}static char * keyboard_get_state (void){ return NULL;}static int wait_event (int which, fd_set *in, fd_set *out, fd_set *except, struct timeval *timeout){ fd_set rfds; int setsize = 0; int retvalue = 0; int e; FD_ZERO(&rfds); if (which | IAL_MOUSEEVENT){ FD_SET(mouse_fd,&rfds); if(mouse_fd > setsize) setsize = mouse_fd; }/* if (which | IAL_KEYEVENT){ FD_SET(kbd_fd, &rfds); if(kbd_fd > setsize) setsize = kbd_fd; }*/ ++setsize; e = select(setsize, &rfds, NULL, NULL, timeout) ; if(e > 0) { /* If data is present on the mouse fd, service it: */ if(mouse_fd >= 0 && FD_ISSET(mouse_fd, &rfds)) { mouse_update(); retvalue |= IAL_MOUSEEVENT; } /* If data is present on the keyboard fd, service it: *//* if(kbd_fd >= 0 && FD_ISSET(kbd_fd, &rfds)) { keyboard_update(); retvalue |= IAL_KEYEVENT; }*/ } else if(e < 0) { // fprintf(stderr,"Select() call in wait_event() failed!\n"); return -1; } return retvalue;}BOOL InitpcsimInput (INPUT* input, const char* mdev, const char* mtype){ /* open input device(s) here */ mouse_fd = open ("/dev/ser0", O_RDONLY|O_NONBLOCK); if (mouse_fd < 0) { fprintf (stderr, "IAL: Can not open mouse-like device!\n"); return FALSE; } xpos = 0; ypos = 0; buttons = 0; minx = 0; maxx = MAX_X - 2; miny = 0; maxy = MAX_Y - 2; input->update_mouse = mouse_update; input->get_mouse_x = mouse_getx; input->get_mouse_y = mouse_gety; input->set_mouse_xy = mouse_setposition; input->get_mouse_button = mouse_getbutton; input->set_mouse_range = mouse_setrange; input->update_keyboard = keyboard_update; input->get_keyboard_state = keyboard_get_state; input->set_leds = NULL; input->wait_event = wait_event; return TRUE;}void TermpcsimInput (void){ if (mouse_fd >= 0) close (mouse_fd);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -