1 // -*- Mode: C++; tab-width:2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi:tw=80:et:ts=2:sts=2
3 //
4 // -----------------------------------------------------------------------
5 //
6 // This file is part of RLVM, a RealLive virtual machine clone.
7 //
8 // -----------------------------------------------------------------------
9 //
10 // Copyright (C) 2006 Elliot Glaysher
11 //
12 // This program is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation; either version 3 of the License, or
15 // (at your option) any later version.
16 //
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
25 //
26 // -----------------------------------------------------------------------
27 
28 #ifndef SRC_SYSTEMS_SDL_SDL_EVENT_SYSTEM_H_
29 #define SRC_SYSTEMS_SDL_SDL_EVENT_SYSTEM_H_
30 
31 #include <SDL/SDL_events.h>
32 
33 #include "systems/base/event_system.h"
34 #include "systems/base/rect.h"
35 
36 class SDLSystem;
37 
38 // Hack to ferry SDL_Events over to something like Guichan which wants to take
39 // control of the input.
40 class RawSDLInputHandler {
41  public:
~RawSDLInputHandler()42   virtual ~RawSDLInputHandler() {}
43 
44   virtual void pushInput(SDL_Event event) = 0;
45 };
46 
47 class SDLEventSystem : public EventSystem {
48  public:
49   SDLEventSystem(SDLSystem& sys, Gameexe& gexe);
50 
51   // We provide this accessor to let the Graphics system query what
52   // to do when redrawing the mouse.
mouse_inside_window()53   bool mouse_inside_window() const { return mouse_inside_window_; }
54 
set_raw_sdl_input_handler(RawSDLInputHandler * handler)55   void set_raw_sdl_input_handler(RawSDLInputHandler* handler) {
56     raw_handler_ = handler;
57   }
58 
59   // Implementation of EventSystem:
60   virtual void ExecuteEventSystem(RLMachine& machine) override;
61   virtual unsigned int GetTicks() const override;
62   virtual void Wait(unsigned int milliseconds) const override;
63   virtual bool ShiftPressed() const override;
64   virtual bool CtrlPressed() const override;
65   virtual Point GetCursorPos() override;
66   virtual void GetCursorPos(Point& position,
67                             int& button1,
68                             int& button2) override;
69   virtual void FlushMouseClicks() override;
70   virtual unsigned int TimeOfLastMouseMove() override;
71   virtual void InjectMouseMovement(RLMachine& machine,
72                                    const Point& loc) override;
73   virtual void InjectMouseDown(RLMachine& machine) override;
74   virtual void InjectMouseUp(RLMachine& machine) override;
75 
76  private:
77   // Called from GetCursorPos() functions to force a pause if it's been less
78   // than 10ms since the last GetCursorPos() call.
79   void PreventCursorPosSpinning();
80 
81   // RealLive event system commands
82   void HandleKeyDown(RLMachine& machine, SDL_Event& event);
83   void HandleKeyUp(RLMachine& machine, SDL_Event& event);
84   void HandleMouseMotion(RLMachine& machine, SDL_Event& event);
85   void HandleMouseButtonEvent(RLMachine& machine, SDL_Event& event);
86   void HandleActiveEvent(RLMachine& machine, SDL_Event& event);
87 
88   bool shift_pressed_, ctrl_pressed_;
89 
90   // Whether the mouse cursor is currently inside the window bounds.
91   bool mouse_inside_window_;
92 
93   Point mouse_pos_;
94 
95   int button1_state_, button2_state_;
96 
97   // The last time a GetCursorPos() function was called.
98   unsigned int last_get_currsor_time_;
99 
100   // The last time we received a mouse move notification.
101   unsigned int last_mouse_move_time_;
102 
103   // Our owning system.
104   SDLSystem& system_;
105 
106   // Handles raw SDL events when appropriate. (Used for things like Guichan,
107   // etc who want to suck raw SDL events).
108   RawSDLInputHandler* raw_handler_;
109 };
110 
111 #endif  // SRC_SYSTEMS_SDL_SDL_EVENT_SYSTEM_H_
112