Refactor keygroup into library/
[lsnes.git] / include / core / window.hpp
blobfe3b28547432f2ef0517d68023a47fc62e6e4599
1 #ifndef _window__hpp__included__
2 #define _window__hpp__included__
4 #include "core/keymapper.hpp"
5 #include "library/messagebuffer.hpp"
6 #include "library/emustatus.hpp"
7 #include "library/framebuffer.hpp"
8 #include <string>
9 #include <map>
10 #include <list>
11 #include <stdexcept>
13 class emulator_status;
15 /**
16 * Mutex.
18 struct mutex
20 /**
21 * Hold mutex RAII-style.
23 struct holder
25 holder(mutex& m) throw();
26 ~holder() throw();
27 private:
28 mutex& mut;
30 /**
31 * Create a mutex. The returned mutex can be deleted using delete.
33 static mutex& aquire() throw(std::bad_alloc);
34 /**
35 * Create a recursive mutex. The returned mutex can be deleted using delete.
37 static mutex& aquire_rec() throw(std::bad_alloc);
38 /**
39 * Destroy a mutex.
41 virtual ~mutex() throw();
42 /**
43 * Lock a mutex.
45 virtual void lock() throw() = 0;
46 /**
47 * Lock a mutex.
49 virtual void unlock() throw() = 0;
50 protected:
51 mutex() throw();
54 /**
55 * Condition variable.
57 struct condition
59 /**
60 * Create a condition variable. The returned condition can be freed using delete.
62 static condition& aquire(mutex& m) throw(std::bad_alloc);
63 /**
64 * Destroy a condition.
66 virtual ~condition() throw();
67 /**
68 * Return associated mutex.
70 mutex& associated() throw();
71 /**
72 * Wait for condition. The associate mutex must be locked.
74 virtual bool wait(uint64_t max_usec) throw() = 0;
75 /**
76 * Signal a condition. The associated mutex should be locked.
78 virtual void signal() throw() = 0;
79 protected:
80 condition(mutex& m);
81 mutex& assoc;
84 /**
85 * Thread ID.
87 struct thread_id
89 /**
90 * Return thread id for this thread. Can be freed with delete.
92 static thread_id& me() throw(std::bad_alloc);
93 /**
94 * Destructor.
96 virtual ~thread_id() throw();
97 /**
98 * Is this thread me?
100 virtual bool is_me() throw() = 0;
101 protected:
102 thread_id() throw();
106 * Thread.
108 struct thread
111 * Create a thread, jumping to specified starting point.
113 static thread& create(void* (*entrypoint)(void* arg), void* arg) throw(std::bad_alloc, std::runtime_error);
115 * Destroy a thread, first joining it (if not already joined).
117 virtual ~thread() throw();
119 * Is this thread still alive?
121 bool is_alive() throw();
123 * Join a thread.
125 void* join() throw();
126 protected:
127 thread() throw();
129 * Notify that this thread has quit.
131 void notify_quit(void* retval) throw();
133 * Join a thread.
135 virtual void _join() throw() = 0;
136 private:
137 bool alive;
138 bool joined;
139 void* returns;
143 * Information about keypress.
145 struct keypress
148 * Create null keypress (no modifiers, NULL key and released).
150 keypress();
152 * Create new keypress.
154 keypress(keyboard_modifier_set mod, keyboard_key& _key, short _value);
156 * Create new keypress (two keys).
158 keypress(keyboard_modifier_set mod, keyboard_key& _key, keyboard_key& _key2, short _value);
160 * Modifier set.
162 keyboard_modifier_set modifiers;
164 * The actual key (first)
166 keyboard_key* key1;
168 * The actual key (second)
170 keyboard_key* key2;
172 * Value for the press
174 short value;
178 * Functions implemented by the graphics plugin.
180 * Unless explicitly noted otherwise, all the methods are to be called from emulation thread if that exists, otherwise
181 * from the main thread.
183 struct graphics_plugin
186 * Graphics initialization function.
188 * - The first initialization function to be called by platform::init().
190 static void init() throw();
192 * Graphics quit function.
194 * - The last quit function to be called by platform::quit().
196 static void quit() throw();
198 * Notification when messages get updated.
200 static void notify_message() throw();
202 * Notification when status gets updated.
204 static void notify_status() throw();
206 * Notification when main screen gets updated.
208 static void notify_screen() throw();
210 * Show modal message dialog.
212 * Parameter text: The text for dialog.
213 * Parameter confirm: If true, display confirmation dialog, if false, display notification dialog.
214 * Returns: True if confirmation dialog was confirmed, otherwise false.
216 static bool modal_message(const std::string& text, bool confirm = false) throw();
218 * Displays fatal error message.
220 * - After this routine returns, the program will quit.
221 * - The call can occur in any thread.
223 static void fatal_error() throw();
225 * Identification for graphics plugin.
227 static const char* name;
231 * Functions implemented by the joystick plugin.
233 * Unless explicitly noted otherwise, all the methods are to be called from emulation thread if that exists, otherwise
234 * from the main thread.
236 struct joystick_plugin
239 * Joystick initialization function.
241 * - The third initialization function to be called by window_init().
242 * - The call occurs in the main thread.
243 * - Implemented by the joystick plugin.
245 static void init() throw();
247 * Joystick quit function.
249 * - The third last quit function to be called by window_quit().
250 * - The call occurs in the main thread.
251 * - Implemented by the joystick plugin.
253 static void quit() throw();
255 * This thread becomes the joystick polling thread.
257 * - Called in joystick polling thread.
259 static void thread_fn() throw();
261 * Signal the joystick thread to quit.
263 static void signal() throw();
265 * Identification for joystick plugin.
267 static const char* name;
271 * Platform-specific-related functions.
273 struct platform
276 * Initialize the system.
278 static void init();
281 * Shut down the system.
283 static void quit();
285 * Get output stream printing into message queue.
287 * Note that lines printed there should be terminated by '\n'.
289 * Implemented by the generic window code.
291 * returns: The output stream.
292 * throws std::bad_alloc: Not enough memory.
294 static std::ostream& out() throw(std::bad_alloc);
296 * Get emulator status area
298 * returns: Emulator status area.
300 static emulator_status& get_emustatus() throw();
302 * Message buffer.
304 static messagebuffer msgbuf;
306 * Get message buffer lock.
308 static mutex& msgbuf_lock() throw();
310 * Set palette used on screen.
312 static void screen_set_palette(unsigned rshift, unsigned gshift, unsigned bshift) throw();
314 * Adds a messages to mesage queue to be shown.
316 * Implemented by the generic window code.
318 * parameter msg: The messages to add (split by '\n').
319 * throws std::bad_alloc: Not enough memory.
321 static void message(const std::string& msg) throw(std::bad_alloc);
324 * Displays fatal error message, quitting after the user acks it (called by fatal_error()).
326 * Needs to be implemented by the graphics plugin.
328 static void fatal_error() throw();
330 * Enable or disable sound.
332 * Implemented by the generic window code.
334 * parameter enable: Enable sounds if true, otherwise disable sounds.
336 static void sound_enable(bool enable) throw();
338 * Are sounds enabled?
340 static bool is_sound_enabled() throw();
342 * Set sound device.
344 static void set_sound_device(const std::string& dev) throw();
346 * Show modal message dialog.
348 * Parameter text: The text for dialog.
349 * Parameter confirm: If true, display confirmation dialog, if false, display notification dialog.
350 * Returns: True, if confirmation dialog was confirmed, otherwise false.
352 static bool modal_message(const std::string& text, bool confirm = false) throw()
354 return graphics_plugin::modal_message(text, confirm);
357 * Process command and keypress queues.
359 * - If emulating normally, this routine returns fast.
360 * - If emulator is in pause mode, this routine will block until emulator has left pause mode.
361 * - If emulator is in some special mode, this routine can block until said mode is left.
363 static void flush_command_queue() throw();
365 * Enable/Disable pause mode.
367 * - This function doesn't actually block. For actual paused blocking, use flush_command_queue().
369 * Parameter enable: Enable pause mode if true, disable pause mode if false.
371 static void set_paused(bool enable) throw();
373 * Wait specified number of milliseconds before returning.
375 * - The command and keypresses queues are processed while waiting.
377 * Parameter usec: The number of microseconds to wait.
379 static void wait(uint64_t usec) throw();
381 * Cause call to wait() to return immediately.
383 static void cancel_wait() throw();
385 * Notify received message.
387 static void notify_message() throw()
389 graphics_plugin::notify_message();
392 * Notify changed status.
394 static void notify_status() throw()
396 graphics_plugin::notify_status();
399 * Notify changed screen.
401 static void notify_screen() throw()
403 graphics_plugin::notify_screen();
406 * Set modal pause mode.
408 * - Modal pause works like ordinary pause, except it uses a separate flag.
410 * Parameter enable: If true, enable modal pause, else disable it.
412 static void set_modal_pause(bool enable) throw();
414 * Queue keypress.
416 * - Can be called from any thread.
418 * Parameter k: The keypress to queue.
420 static void queue(const keypress& k) throw(std::bad_alloc);
422 * Queue command.
424 * - Can be called from any thread.
426 * Parameter c: The command to queue.
428 static void queue(const std::string& c) throw(std::bad_alloc);
430 * Queue function to be called in emulation thread.
432 * - Can be called from any thread (exception: Synchronous mode can not be used from emulation nor main threads).
434 * Parameter f: The function to execute.
435 * Parameter arg: Argument to pass to the function.
436 * Parameter sync: If true, execute function call synchronously, else asynchronously.
438 static void queue(void (*f)(void* arg), void* arg, bool sync) throw(std::bad_alloc);
440 * Run all queues.
442 static void run_queues() throw();
444 static bool pausing_allowed;
445 static double global_volume;
446 static volatile bool do_exit_dummy_event_loop;
447 static void dummy_event_loop() throw();
448 static void exit_dummy_event_loop() throw();
451 class modal_pause_holder
453 public:
454 modal_pause_holder();
455 ~modal_pause_holder();
456 private:
457 modal_pause_holder(const modal_pause_holder&);
458 modal_pause_holder& operator=(const modal_pause_holder&);
461 template<typename T>
462 void functor_call_helper(void* args)
464 (*reinterpret_cast<T*>(args))();
467 template<typename T>
468 void runemufn(T fn)
470 platform::queue(functor_call_helper<T>, &fn, true);
474 * If set, queueing synchronous function produces a warning.
476 extern volatile bool queue_synchronous_fn_warning;
478 #endif