The Battle for Wesnoth  1.19.25+dev
events.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2025
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include <SDL3/SDL_events.h>
19 #include <vector>
20 #include <list>
21 #include <functional>
22 
23 #ifdef _WIN32
24 // Win32 API forward declaration (full definition in WinUser.h)
25 typedef struct tagMSG MSG;
26 #endif
27 
28 //our user-defined double-click event type
29 #define TIMER_EVENT (SDL_EVENT_USER + 1)
30 #define HOVER_REMOVE_POPUP_EVENT (SDL_EVENT_USER + 2)
31 #define DRAW_EVENT (SDL_EVENT_USER + 3)
32 #define CLOSE_WINDOW_EVENT (SDL_EVENT_USER + 4)
33 #define SHOW_HELPTIP_EVENT (SDL_EVENT_USER + 5)
34 #define INVOKE_FUNCTION_EVENT (SDL_EVENT_USER + 6)
35 
36 namespace events
37 {
38 
39 class sdl_handler;
40 
41 typedef std::list<sdl_handler*> handler_list;
42 
43 class context
44 {
45 public:
46  context() :
47  handlers(),
50  {
51  }
52 
53  ~context();
54 
55  context(const context&) = delete;
56 
57  void add_handler(sdl_handler* ptr);
58  /** Returns true if @a ptr is found in either the handlers or staging_handlers lists */
59  bool has_handler(const sdl_handler* ptr) const;
60  bool remove_handler(sdl_handler* ptr);
61  void cycle_focus();
62  void set_focus(const sdl_handler* ptr);
63  void add_staging_handlers();
64 
67  std::vector<sdl_handler*> staging_handlers;
68 };
69 
70 //any classes that derive from this class will automatically
71 //receive sdl events through the handle function for their lifetime,
72 //while the event context they were created in is active.
73 //
74 //NOTE: an event_context object must be initialized before a handler object
75 //can be initialized, and the event_context must be destroyed after
76 //the handler is destroyed.
78 {
79 friend class context;
80 public:
81  virtual void handle_event(const SDL_Event& event) = 0;
82  virtual void handle_window_event(const SDL_Event&) {};
83  virtual void process_event() {}
84 
85  virtual bool requires_event_focus(const SDL_Event * = nullptr) const { return false; }
86 
87  virtual void process_tooltip_string(int /*mousex*/, int /*mousey*/) {}
88 
89  virtual void join(); /*joins the current event context*/
90  virtual void join(context &c); /*joins the specified event context*/
91  virtual void join_same(sdl_handler* parent); /*joins the same event context as the parent is already associated with */
92  virtual void leave(); /*leave the event context*/
93 
94  virtual void join_global(); /*join the global event context*/
95  virtual void leave_global(); /*leave the global event context*/
96 
97  virtual bool has_joined() { return has_joined_;}
98  virtual bool has_joined_global() { return has_joined_global_;}
99 
100  /**
101  * Moving would require two instances' context membership to be handled,
102  * it's simpler to delete these and require the two instances to be
103  * separately constructed / destructed.
104  */
106  sdl_handler(sdl_handler &&) = delete;
107 
108 protected:
109  sdl_handler(const bool auto_join=true);
110  sdl_handler(const sdl_handler &);
112  virtual ~sdl_handler();
113  virtual std::vector<sdl_handler*> handler_members()
114  {
115  return std::vector<sdl_handler*>();
116  }
117 
118 private:
121 };
122 
123 void focus_handler(const sdl_handler* ptr);
124 
125 bool has_focus(const sdl_handler* ptr, const SDL_Event* event);
126 
127 void set_main_thread();
128 
129 // whether the currently executing thread is the main thread.
130 bool is_in_main_thread();
131 
132 void call_in_main_thread(const std::function<void (void)>& f);
133 
134 //event_context objects control the handler objects that SDL events are sent
135 //to. When an event_context is created, it will become the current event context.
136 //event_context objects MUST be created in LIFO ordering in relation to each other,
137 //and in relation to handler objects. That is, all event_context objects should be
138 //created as automatic/stack variables.
139 //
140 //handler objects need not be created as automatic variables (e.g. you could put
141 //them in a vector) however you must guarantee that handler objects are destroyed
142 //before their context is destroyed
144 {
145  event_context();
146  ~event_context();
147 };
148 
149 /** Process all events currently in the queue. */
150 void pump();
151 
152 /** Trigger a draw cycle. */
153 void draw();
154 
155 /** pump() then immediately draw() */
156 inline void pump_and_draw() { pump(); draw(); }
157 // TODO: draw_manager - should this also raise_process_event? Some things do some don't
158 
160 //pump_monitors receive notification after an events::pump() occurs
161 public:
162  pump_monitor();
163  virtual ~pump_monitor();
164  virtual void process() = 0;
165 };
166 
167 void raise_process_event();
168 void raise_resize_event();
169 /**
170  * Triggered by mouse-motion, sends the cursor position to all handlers to
171  * check whether a tooltip should be shown.
172  */
173 void process_tooltip_strings(int mousex, int mousey);
174 
175 
176 /**
177  * Is the event an input event?
178  *
179  * @returns Whether or not the event is an input event.
180  */
181 bool is_input(const SDL_Event& event);
182 
183 /**
184  * Check if this mouse button event is caused by a touch
185  *
186  * @returns Whether or not this event is caused by a touch
187  */
188 bool is_touch(const SDL_MouseButtonEvent& event);
189 
190 /**
191  * Check if this mouse motion event is caused by a touch
192  *
193  * @returns Whether or not this event is caused by a touch
194  */
195 bool is_touch(const SDL_MouseMotionEvent& event);
196 
197 /** Discards all input events. */
198 void discard_input();
199 
200 #ifdef _WIN32
201 /**
202  * Callback passed to SDL_SetWindowsMessageHook.
203  *
204  * Handles anything which interacts with the Windows event loop.
205  * Currently, this is limited to interactions with taskbar toast
206  * notifications.
207  *
208  * @param userdata Arbitrary userdata passed to SDL_SetWindowsMessageHook.
209  * @param msg A pointer to a Win32 event structure to process.
210  *
211  * @returns Whether the Windows event loop should continue
212  * processing this message.
213  */
214 bool handle_windows_message(void* userdata, MSG* msg);
215 #endif
216 
217 }
218 
219 typedef std::vector<events::sdl_handler*> sdl_handler_vector;
void set_focus(const sdl_handler *ptr)
Definition: events.cpp:183
bool has_handler(const sdl_handler *ptr) const
Returns true if ptr is found in either the handlers or staging_handlers lists.
Definition: events.cpp:98
void cycle_focus()
Definition: events.cpp:150
handler_list::iterator focused_handler
Definition: events.hpp:66
std::vector< sdl_handler * > staging_handlers
Definition: events.hpp:67
handler_list handlers
Definition: events.hpp:65
context(const context &)=delete
void add_handler(sdl_handler *ptr)
Definition: events.cpp:89
bool remove_handler(sdl_handler *ptr)
Definition: events.cpp:103
void add_staging_handlers()
Definition: events.cpp:191
virtual ~pump_monitor()
Definition: events.cpp:224
virtual void process()=0
virtual bool requires_event_focus(const SDL_Event *=nullptr) const
Definition: events.hpp:85
virtual void process_tooltip_string(int, int)
Definition: events.hpp:87
virtual void handle_window_event(const SDL_Event &)
Definition: events.hpp:82
virtual bool has_joined_global()
Definition: events.hpp:98
virtual bool has_joined()
Definition: events.hpp:97
virtual std::vector< sdl_handler * > handler_members()
Definition: events.hpp:113
virtual void join()
Definition: events.cpp:305
virtual void leave()
Definition: events.cpp:350
virtual void handle_event(const SDL_Event &event)=0
virtual void process_event()
Definition: events.hpp:83
virtual void join_global()
Definition: events.cpp:371
virtual void join_same(sdl_handler *parent)
Definition: events.cpp:334
virtual void leave_global()
Definition: events.cpp:391
sdl_handler & operator=(sdl_handler &&)=delete
Moving would require two instances' context membership to be handled, it's simpler to delete these an...
virtual ~sdl_handler()
Definition: events.cpp:294
sdl_handler(sdl_handler &&)=delete
std::vector< events::sdl_handler * > sdl_handler_vector
Definition: events.hpp:219
Handling of system events.
void raise_resize_event()
Definition: events.cpp:728
bool has_focus(const sdl_handler *hand, const SDL_Event *event)
Definition: events.cpp:409
void discard_input()
Discards all input events.
Definition: events.cpp:758
void draw()
Trigger a draw cycle.
Definition: events.cpp:712
bool is_input(const SDL_Event &event)
Is the event an input event?
Definition: events.cpp:753
void set_main_thread()
Definition: events.cpp:468
void call_in_main_thread(const std::function< void(void)> &f)
Definition: events.cpp:773
void raise_process_event()
Definition: events.cpp:717
void process_tooltip_strings(int mousex, int mousey)
Triggered by mouse-motion, sends the cursor position to all handlers to check whether a tooltip shoul...
Definition: events.cpp:740
std::list< sdl_handler * > handler_list
Definition: events.hpp:39
bool is_in_main_thread()
Definition: events.cpp:475
void pump_and_draw()
pump() then immediately draw()
Definition: events.hpp:156
void pump()
Process all events currently in the queue.
Definition: events.cpp:480
bool is_touch(const SDL_MouseButtonEvent &event)
Check if this mouse button event is caused by a touch.
Definition: events.cpp:763
void focus_handler(const sdl_handler *ptr)
Definition: events.cpp:402
std::string::const_iterator iterator
Definition: tokenizer.hpp:25
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
mock_char c
struct tagMSG MSG
#define f