# HG changeset patch # User John Tsiombikas # Date 1396847106 -10800 # Node ID 75bc89c2abc466d84be89e0600ac70d05cbe7f5f # Parent a68dbf80d547d041ea23781d33e0726c6c5d90f0 fixed the mouse handling problem diff -r a68dbf80d547 -r 75bc89c2abc4 src/main.cc --- a/src/main.cc Mon Apr 07 06:04:11 2014 +0300 +++ b/src/main.cc Mon Apr 07 08:05:06 2014 +0300 @@ -2,6 +2,7 @@ #include #include #include +#include #include "inttypes.h" #include "gfx.h" #include "keyb.h" @@ -14,13 +15,17 @@ static void cleanup(); static void display(); static void swap_buffers(); +static void draw_cursor(unsigned char *buf, int mx, int my); static void handle_keyboard(); static void handle_mouse(); +static void mouse_button(int bn, int x, int y); +static void mouse_motion(int x, int y); static bool parse_args(int argc, char **argv); +static void sig(int s); static int xsz = 640; static int ysz = 480; -static int bpp = 16; +static int bpp = 24; static int bytespp; static bool novideo; static unsigned char *fb; @@ -29,6 +34,10 @@ static int rshift, gshift, bshift; static unsigned int rmask, gmask, bmask; +static int mx, my; // mouse cursor +static float cam_theta, cam_phi = 25, cam_dist = 8; + +static bool use_mouse; static bool quit; struct m3d_image rbuf; @@ -61,6 +70,11 @@ static bool init() { + signal(SIGINT, sig); + signal(SIGSEGV, sig); + signal(SIGILL, sig); + signal(SIGFPE, sig); + if(!novideo) { if(kb_init(32) == -1) { fprintf(stderr, "failed to initialize keyboard driver\n"); @@ -81,6 +95,11 @@ printlog("bpp: %d (%d %d %d)\n", bpp, rbits, gbits, bbits); printlog("shift: %d %d %d\n", rshift, gshift, bshift); printlog("mask: %x %x %x\n", rmask, gmask, bmask); + + if(have_mouse()) { + use_mouse = true; + set_mouse_limits(0, 0, xsz - 1, ysz - 1); + } } else { logger_output(stdout); printlog("novideo (debug) mode\n"); @@ -132,10 +151,17 @@ m3d_matrix_mode(M3D_MODELVIEW); m3d_load_identity(); - m3d_translate(0, 0, -10); + m3d_translate(0, 0, -cam_dist); + m3d_rotate(cam_phi, 1, 0, 0); + m3d_rotate(cam_theta, 0, 1, 0); scn->draw(); + // draw the mouse cursor + if(use_mouse) { + draw_cursor(backbuf, mx, my); + } + if(!novideo) { swap_buffers(); wait_vsync(); @@ -186,6 +212,42 @@ } } +static void draw_cursor(unsigned char *buf, int mx, int my) +{ + unsigned char *cptr = buf + (my * xsz + mx) * 3; + int i, cw[2] = {4, 4}, ch[2] = {4, 4}; + + if(mx < cw[0]) cw[0] = mx; + if(my < ch[0]) ch[0] = my; + if(xsz - mx < cw[1]) cw[1] = xsz - mx - 1; + if(ysz - my < ch[1]) ch[1] = ysz - my - 1; + + for(i=1; i 90) cam_phi = 90; + } + if(bnstate & 2) { + cam_dist += dy * 0.1; + if(cam_dist < 0) cam_dist = 0; + } } static struct { @@ -272,3 +378,9 @@ } return true; } + +static void sig(int s) +{ + cleanup(); + fprintf(stderr, "signal caught: %d\n", s); +} diff -r a68dbf80d547 -r 75bc89c2abc4 src/mouse.c --- a/src/mouse.c Mon Apr 07 06:04:11 2014 +0300 +++ b/src/mouse.c Mon Apr 07 08:05:06 2014 +0300 @@ -1,7 +1,5 @@ -/* TODO: try NOT using the v8086 interrupts to avoid the overhead */ #include "mouse.h" #include "inttypes.h" -#include "dpmi.h" #define INTR 0x33 @@ -10,63 +8,86 @@ #define HIDE 2 #define READ 3 #define WRITE 4 +#define PIXRATE 0xf #define XLIM 7 #define YLIM 8 int have_mouse(void) { - struct dpmi_real_regs regs; - memset(®s, 0, sizeof regs); - regs.eax = QUERY; - dpmi_real_int(INTR, ®s); - return regs.eax & 0xffff; + uint16_t res = 0; + _asm { + mov eax, QUERY + int INTR + mov res, ax + } + return res; } void show_mouse(int show) { - struct dpmi_real_regs regs; - memset(®s, 0, sizeof regs); - regs.eax = show ? SHOW : HIDE; - dpmi_real_int(INTR, ®s); + uint16_t cmd = show ? SHOW : HIDE; + _asm { + mov ax, cmd + int INTR + } } int read_mouse(int *xp, int *yp) { - struct dpmi_real_regs regs; - memset(®s, 0, sizeof regs); + uint16_t x, y, state; + _asm { + mov eax, READ + int INTR + mov state, bx + mov x, cx + mov y, dx + } - regs.eax = READ; - dpmi_real_int(INTR, ®s); - - if(xp) *xp = regs.ecx & 0xffff; - if(yp) *yp = regs.edx & 0xffff; - return regs.ebx & 0xffff; + if(xp) *xp = x; + if(yp) *yp = y; + return state; } void set_mouse(int x, int y) { - struct dpmi_real_regs regs; - memset(®s, 0, sizeof regs); - - regs.eax = WRITE; - regs.ecx = x; - regs.edx = y; - dpmi_real_int(INTR, ®s); + _asm { + mov eax, WRITE + mov ecx, x + mov edx, y + int INTR + } } void set_mouse_limits(int xmin, int ymin, int xmax, int ymax) { - struct dpmi_real_regs regs; - memset(®s, 0, sizeof regs); - regs.eax = XLIM; - regs.ecx = xmin; - regs.edx = xmax; - dpmi_real_int(INTR, ®s); + _asm { + mov eax, XLIM + mov ecx, xmin + mov edx, xmax + int INTR + mov eax, YLIM + mov ecx, ymin + mov edx, ymax + int INTR + } +} - memset(®s, 0, sizeof regs); - regs.eax = YLIM; - regs.ecx = ymin; - regs.edx = ymax; - dpmi_real_int(INTR, ®s); +void set_mouse_rate(int xrate, int yrate) +{ + _asm { + mov ax, PIXRATE + mov ecx, xrate + mov edx, yrate + int INTR + } } + +void set_mouse_mode(enum mouse_mode mode) +{ + if(mode == MOUSE_GFX) { + set_mouse_rate(1, 1); + } else { + set_mouse_rate(8, 16); + } +} diff -r a68dbf80d547 -r 75bc89c2abc4 src/mouse.h --- a/src/mouse.h Mon Apr 07 06:04:11 2014 +0300 +++ b/src/mouse.h Mon Apr 07 08:05:06 2014 +0300 @@ -1,9 +1,16 @@ #ifndef MOUSE_H_ #define MOUSE_H_ -#define MOUSE_LEFT 1 -#define MOUSE_RIGHT 2 -#define MOUSE_MIDDLE 4 +enum { + MOUSE_LEFT = 1, + MOUSE_RIGHT = 2, + MOUSE_MIDDLE = 4 +}; + +enum mouse_mode { + MOUSE_GFX, + MOUSE_TEXT +}; #ifdef __cplusplus extern "C" { @@ -14,6 +21,8 @@ int read_mouse(int *xp, int *yp); void set_mouse(int x, int y); void set_mouse_limits(int xmin, int ymin, int xmax, int ymax); +void set_mouse_rate(int xrate, int yrate); +void set_mouse_mode(enum mouse_mode mode); #ifdef __cplusplus }