static void PollEvent() { poll_mouse(); bool mouse_action = false; /* Mouse buttons */ static int prev_button_state; if (prev_button_state != mouse_b) { uint diff = prev_button_state ^ mouse_b; while (diff != 0) { uint button = FindFirstBit(diff); ClrBit(diff, button); if (HasBit(mouse_b, button)) { /* Pressed mouse button */ if (_rightclick_emulate && (key_shifts & KB_CTRL_FLAG)) { button = RIGHT_BUTTON; ClrBit(diff, RIGHT_BUTTON); } switch (button) { case LEFT_BUTTON: _left_button_down = true; break; case RIGHT_BUTTON: _right_button_down = true; _right_button_clicked = true; break; default: /* ignore rest */ break; } } else { /* Released mouse button */ if (_rightclick_emulate) { _right_button_down = false; _left_button_down = false; _left_button_clicked = false; } else if (button == LEFT_BUTTON) { _left_button_down = false; _left_button_clicked = false; } else if (button == RIGHT_BUTTON) { _right_button_down = false; } } } prev_button_state = mouse_b; mouse_action = true; } /* Mouse movement */ int dx = mouse_x - _cursor.pos.x; int dy = mouse_y - _cursor.pos.y; if (dx != 0 || dy != 0) { if (_cursor.fix_at) { _cursor.delta.x = dx; _cursor.delta.y = dy; position_mouse(_cursor.pos.x, _cursor.pos.y); } else { _cursor.delta.x = dx; _cursor.delta.y = dy; _cursor.pos.x = mouse_x; _cursor.pos.y = mouse_y; _cursor.dirty = true; } mouse_action = true; } static int prev_mouse_z = 0; if (prev_mouse_z != mouse_z) { _cursor.wheel = (prev_mouse_z - mouse_z) < 0 ? -1 : 1; prev_mouse_z = mouse_z; mouse_action = true; } if (mouse_action) HandleMouseEvents(); poll_keyboard(); if ((key_shifts & KB_ALT_FLAG) && (key[KEY_ENTER] || key[KEY_F])) { ToggleFullScreen(!_fullscreen); } else if (keypressed()) { WChar character; uint keycode = ConvertAllegroKeyIntoMy(&character); HandleKeypress(keycode, character); } }
int VideoDriver_SDL::PollEvent() { SDL_Event ev; if (!SDL_CALL SDL_PollEvent(&ev)) return -2; switch (ev.type) { case SDL_MOUSEMOTION: if (_cursor.fix_at) { int dx = ev.motion.x - _cursor.pos.x; int dy = ev.motion.y - _cursor.pos.y; if (dx != 0 || dy != 0) { _cursor.delta.x = dx; _cursor.delta.y = dy; SDL_CALL SDL_WarpMouse(_cursor.pos.x, _cursor.pos.y); } } else { _cursor.delta.x = ev.motion.x - _cursor.pos.x; _cursor.delta.y = ev.motion.y - _cursor.pos.y; _cursor.pos.x = ev.motion.x; _cursor.pos.y = ev.motion.y; _cursor.dirty = true; } HandleMouseEvents(); break; case SDL_MOUSEBUTTONDOWN: if (_rightclick_emulate && SDL_CALL SDL_GetModState() & KMOD_CTRL) { ev.button.button = SDL_BUTTON_RIGHT; } switch (ev.button.button) { case SDL_BUTTON_LEFT: _left_button_down = true; break; case SDL_BUTTON_RIGHT: _right_button_down = true; _right_button_clicked = true; break; case SDL_BUTTON_WHEELUP: _cursor.wheel--; break; case SDL_BUTTON_WHEELDOWN: _cursor.wheel++; break; default: break; } HandleMouseEvents(); break; case SDL_MOUSEBUTTONUP: if (_rightclick_emulate) { _right_button_down = false; _left_button_down = false; _left_button_clicked = false; } else if (ev.button.button == SDL_BUTTON_LEFT) { _left_button_down = false; _left_button_clicked = false; } else if (ev.button.button == SDL_BUTTON_RIGHT) { _right_button_down = false; } HandleMouseEvents(); break; case SDL_ACTIVEEVENT: if (!(ev.active.state & SDL_APPMOUSEFOCUS)) break; if (ev.active.gain) { // mouse entered the window, enable cursor _cursor.in_window = true; } else { UndrawMouseCursor(); // mouse left the window, undraw cursor _cursor.in_window = false; } break; case SDL_QUIT: HandleExitGameRequest(); break; case SDL_KEYDOWN: // Toggle full-screen on ALT + ENTER/F if ((ev.key.keysym.mod & (KMOD_ALT | KMOD_META)) && (ev.key.keysym.sym == SDLK_RETURN || ev.key.keysym.sym == SDLK_f)) { ToggleFullScreen(!_fullscreen); } else { HandleKeypress(ConvertSdlKeyIntoMy(&ev.key.keysym)); } break; case SDL_VIDEORESIZE: { int w = max(ev.resize.w, 64); int h = max(ev.resize.h, 64); CreateMainSurface(w, h); break; } case SDL_VIDEOEXPOSE: { /* Force a redraw of the entire screen. Note * that SDL 1.2 seems to do this automatically * in most cases, but 1.3 / 2.0 does not. */ _num_dirty_rects = MAX_DIRTY_RECTS + 1; break; } } return -1; }
/** Forward key presses to the window system. */ static LRESULT HandleCharMsg(uint keycode, WChar charcode) { #if !defined(UNICODE) static char prev_char = 0; char input[2] = {(char)charcode, 0}; int input_len = 1; if (prev_char != 0) { /* We stored a lead byte previously, combine it with this byte. */ input[0] = prev_char; input[1] = (char)charcode; input_len = 2; } else if (IsDBCSLeadByte(charcode)) { /* We got a lead byte, store and exit. */ prev_char = charcode; return 0; } prev_char = 0; wchar_t w[2]; // Can get up to two code points as a result. int len = MultiByteToWideChar(CP_ACP, 0, input, input_len, w, 2); switch (len) { case 1: // Normal unicode character. charcode = w[0]; break; case 2: // Got an UTF-16 surrogate pair back. charcode = Utf16DecodeSurrogate(w[0], w[1]); break; default: // Some kind of error. DEBUG(driver, 1, "Invalid DBCS character sequence encountered, dropping input"); charcode = 0; break; } #else static WChar prev_char = 0; /* Did we get a lead surrogate? If yes, store and exit. */ if (Utf16IsLeadSurrogate(charcode)) { if (prev_char != 0) DEBUG(driver, 1, "Got two UTF-16 lead surrogates, dropping the first one"); prev_char = charcode; return 0; } /* Stored lead surrogate and incoming trail surrogate? Combine and forward to input handling. */ if (prev_char != 0) { if (Utf16IsTrailSurrogate(charcode)) { charcode = Utf16DecodeSurrogate(prev_char, charcode); } else { DEBUG(driver, 1, "Got an UTF-16 lead surrogate without a trail surrogate, dropping the lead surrogate"); } } prev_char = 0; #endif /* UNICODE */ HandleKeypress(keycode, charcode); return 0; }