/* * * SDL mouse driver * */ #include #include #include "fix.h" #include "timer.h" #include "event.h" #include "mouse.h" #include "playsave.h" struct mousebutton { ubyte pressed; fix time_went_down; fix time_held_down; uint num_downs; uint num_ups; }; static struct mouseinfo { struct mousebutton buttons[MOUSE_MAX_BUTTONS]; int delta_x, delta_y, delta_z, old_delta_x, old_delta_y; int x,y,z; int cursor_enabled; fix cursor_time; } Mouse; typedef struct d_event_mousebutton { event_type type; int button; } d_event_mousebutton; void mouse_init(void) { memset(&Mouse,0,sizeof(Mouse)); mouse_toggle_cursor(1); } void mouse_close(void) { SDL_ShowCursor(SDL_ENABLE); SDL_WM_GrabInput(SDL_GRAB_OFF); } void mouse_button_handler(SDL_MouseButtonEvent *mbe, fix time) { // to bad, SDL buttons use a different mapping as descent expects, // this is at least true and tested for the first three buttons int button_remap[17] = { MBTN_LEFT, MBTN_MIDDLE, MBTN_RIGHT, MBTN_Z_UP, MBTN_Z_DOWN, MBTN_PITCH_BACKWARD, MBTN_PITCH_FORWARD, MBTN_BANK_LEFT, MBTN_BANK_RIGHT, MBTN_HEAD_LEFT, MBTN_HEAD_RIGHT, MBTN_11, MBTN_12, MBTN_13, MBTN_14, MBTN_15, MBTN_16 }; int button = button_remap[mbe->button - 1]; // -1 since SDL seems to start counting at 1 d_event_mousebutton event; window *wind; if (GameArg.CtlNoMouse) return; Mouse.cursor_time = time; if (mbe->state == SDL_PRESSED) { Mouse.buttons[button].pressed = 1; Mouse.buttons[button].time_went_down = time; Mouse.buttons[button].num_downs++; if (button == MBTN_Z_UP) { Mouse.delta_z += Z_SENSITIVITY; Mouse.z += Z_SENSITIVITY; } else if (button == MBTN_Z_DOWN) { Mouse.delta_z -= Z_SENSITIVITY; Mouse.z -= Z_SENSITIVITY; } } else { Mouse.buttons[button].pressed = 0; Mouse.buttons[button].time_held_down += time - Mouse.buttons[button].time_went_down; Mouse.buttons[button].num_ups++; } event.type = (mbe->state == SDL_PRESSED) ? EVENT_MOUSE_BUTTON_DOWN : EVENT_MOUSE_BUTTON_UP; event.button = button; if ((wind = window_get_front())) { if (!window_send_event(wind, (d_event *)&event)) call_default_handler((d_event *)&event); } else call_default_handler((d_event *)&event); } void mouse_motion_handler(SDL_MouseMotionEvent *mme, fix time) { if (GameArg.CtlNoMouse) return; Mouse.cursor_time = time; Mouse.x += mme->xrel; Mouse.y += mme->yrel; } void mouse_flush() // clears all mice events... { int i; fix current_time; // event_poll(); current_time = timer_get_fixed_seconds(); for (i=0; itype == EVENT_MOUSE_BUTTON_DOWN) || (event->type == EVENT_MOUSE_BUTTON_UP)); return ((d_event_mousebutton *)event)->button; } int mouse_get_btns() { int i; uint flag=1; int status = 0; // event_poll(); for (i=0; i= time && !show) SDL_ShowCursor(SDL_ENABLE); else if ( (Mouse.cursor_time + (F1_0*2)) < time && show) SDL_ShowCursor(SDL_DISABLE); if (grab) SDL_WM_GrabInput(SDL_GRAB_OFF); } else { if (show) SDL_ShowCursor(SDL_DISABLE); if (!grab && GameArg.CtlGrabMouse) SDL_WM_GrabInput(SDL_GRAB_ON); } }