Make mouse be ordinary keys instead of being special
[lsnes.git] / include / core / window.hpp
blob7249b7d45b4ce819070618bfa828c00967c32059
1 #ifndef _window__hpp__included__
2 #define _window__hpp__included__
4 #include "core/keymapper.hpp"
5 #include "core/messagebuffer.hpp"
6 #include "core/render.hpp"
7 #include "core/status.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 * Destroy a mutex.
37 virtual ~mutex() throw();
38 /**
39 * Lock a mutex.
41 virtual void lock() throw() = 0;
42 /**
43 * Lock a mutex.
45 virtual void unlock() throw() = 0;
46 protected:
47 mutex() throw();
50 /**
51 * Condition variable.
53 struct condition
55 /**
56 * Create a condition variable. The returned condition can be freed using delete.
58 static condition& aquire(mutex& m) throw(std::bad_alloc);
59 /**
60 * Destroy a condition.
62 virtual ~condition() throw();
63 /**
64 * Return associated mutex.
66 mutex& associated() throw();
67 /**
68 * Wait for condition. The associate mutex must be locked.
70 virtual bool wait(uint64_t max_usec) throw() = 0;
71 /**
72 * Signal a condition. The associated mutex should be locked.
74 virtual void signal() throw() = 0;
75 protected:
76 condition(mutex& m);
77 mutex& assoc;
80 /**
81 * Thread ID.
83 struct thread_id
85 /**
86 * Return thread id for this thread. Can be freed with delete.
88 static thread_id& me() throw(std::bad_alloc);
89 /**
90 * Destructor.
92 virtual ~thread_id() throw();
93 /**
94 * Is this thread me?
96 virtual bool is_me() throw() = 0;
97 protected:
98 thread_id() throw();
102 * Thread.
104 struct thread
107 * Create a thread, jumping to specified starting point.
109 static thread& create(void* (*entrypoint)(void* arg), void* arg) throw(std::bad_alloc, std::runtime_error);
111 * Destroy a thread, first joining it (if not already joined).
113 virtual ~thread() throw();
115 * Is this thread still alive?
117 bool is_alive() throw();
119 * Join a thread.
121 void* join() throw();
122 protected:
123 thread() throw();
125 * Notify that this thread has quit.
127 void notify_quit(void* retval) throw();
129 * Join a thread.
131 virtual void _join() throw() = 0;
132 private:
133 bool alive;
134 bool joined;
135 void* returns;
139 * Information about keypress.
141 struct keypress
144 * Create null keypress (no modifiers, NULL key and released).
146 keypress();
148 * Create new keypress.
150 keypress(modifier_set mod, keygroup& _key, short _value);
152 * Create new keypress (two keys).
154 keypress(modifier_set mod, keygroup& _key, keygroup& _key2, short _value);
156 * Modifier set.
158 modifier_set modifiers;
160 * The actual key (first)
162 keygroup* key1;
164 * The actual key (second)
166 keygroup* key2;
168 * Value for the press
170 short value;
174 * Functions implemented by the graphics plugin.
176 * Unless explicitly noted otherwise, all the methods are to be called from emulation thread if that exists, otherwise
177 * from the main thread.
179 struct graphics_plugin
182 * Graphics initialization function.
184 * - The first initialization function to be called by platform::init().
186 static void init() throw();
188 * Graphics quit function.
190 * - The last quit function to be called by platform::quit().
192 static void quit() throw();
194 * Notification when messages get updated.
196 static void notify_message() throw();
198 * Notification when status gets updated.
200 static void notify_status() throw();
202 * Notification when main screen gets updated.
204 static void notify_screen() throw();
206 * Show modal message dialog.
208 * Parameter text: The text for dialog.
209 * Parameter confirm: If true, display confirmation dialog, if false, display notification dialog.
210 * Returns: True if confirmation dialog was confirmed, otherwise false.
212 static bool modal_message(const std::string& text, bool confirm = false) throw();
214 * Displays fatal error message.
216 * - After this routine returns, the program will quit.
217 * - The call can occur in any thread.
219 static void fatal_error() throw();
221 * Identification for graphics plugin.
223 static const char* name;
227 * Functions implemented by the sound plugin.
229 * Unless explicitly noted otherwise, all the methods are to be called from emulation thread if that exists, otherwise
230 * from the main thread.
232 struct sound_plugin
235 * Sound initialization function.
237 * - The second initialization function to be called by window_init().
239 static void init() throw();
241 * Sound quit function.
243 * - The second last quit function to be called by window_quit().
245 static void quit() throw();
247 * Enable or disable sound.
249 * parameter enable: Enable sounds if true, otherwise disable sounds.
251 static void enable(bool enable) throw();
253 * Input audio sample (at specified rate).
255 * parameter left: Left sample.
256 * parameter right: Right sample.
258 static void sample(uint16_t left, uint16_t right) throw();
260 * Has the sound system been successfully initialized?
262 * Returns: True if sound system has successfully initialized, false otherwise.
264 static bool initialized();
266 * Set sound device.
268 * - If new sound device is invalid, the sound device is not changed.
270 * Parameter dev: The new sound device.
272 static void set_device(const std::string& dev) throw(std::bad_alloc, std::runtime_error);
274 * Get current sound device.
276 * Returns: The current sound device.
278 static std::string get_device() throw(std::bad_alloc);
280 * Get available sound devices.
282 * Returns: The map of devices. Keyed by name of the device, values are human-readable names for devices.
284 static std::map<std::string, std::string> get_devices() throw(std::bad_alloc);
286 * Identification for sound plugin.
288 static const char* name;
292 * Functions implemented by the sound plugin.
294 * Unless explicitly noted otherwise, all the methods are to be called from emulation thread if that exists, otherwise
295 * from the main thread.
297 struct joystick_plugin
300 * Joystick initialization function.
302 * - The third initialization function to be called by window_init().
303 * - The call occurs in the main thread.
304 * - Implemented by the joystick plugin.
306 static void init() throw();
308 * Joystick quit function.
310 * - The third last quit function to be called by window_quit().
311 * - The call occurs in the main thread.
312 * - Implemented by the joystick plugin.
314 static void quit() throw();
316 * This thread becomes the joystick polling thread.
318 * - Called in joystick polling thread.
320 static void thread_fn() throw();
322 * Signal the joystick thread to quit.
324 static void signal() throw();
326 * Identification for joystick plugin.
328 static const char* name;
332 * Platform-specific-related functions.
334 struct platform
337 * Initialize the system.
339 static void init();
342 * Shut down the system.
344 static void quit();
346 * Get output stream printing into message queue.
348 * Note that lines printed there should be terminated by '\n'.
350 * Implemented by the generic window code.
352 * returns: The output stream.
353 * throws std::bad_alloc: Not enough memory.
355 static std::ostream& out() throw(std::bad_alloc);
357 * Get emulator status area
359 * returns: Emulator status area.
361 static emulator_status& get_emustatus() throw();
363 * Message buffer.
365 static messagebuffer msgbuf;
367 * Get message buffer lock.
369 static mutex& msgbuf_lock() throw();
371 * Set palette used on screen.
373 static void screen_set_palette(unsigned rshift, unsigned gshift, unsigned bshift) throw();
375 * Adds a messages to mesage queue to be shown.
377 * Implemented by the generic window code.
379 * parameter msg: The messages to add (split by '\n').
380 * throws std::bad_alloc: Not enough memory.
382 static void message(const std::string& msg) throw(std::bad_alloc);
385 * Displays fatal error message, quitting after the user acks it (called by fatal_error()).
387 * Needs to be implemented by the graphics plugin.
389 static void fatal_error() throw();
391 * Enable or disable sound.
393 * Implemented by the generic window code.
395 * parameter enable: Enable sounds if true, otherwise disable sounds.
397 static void sound_enable(bool enable) throw();
399 * Are sounds enabled?
401 static bool is_sound_enabled() throw();
403 * Is sound system initialized?
405 static bool sound_initialized() throw()
407 return sound_plugin::initialized();
410 * Set sound device.
412 static void set_sound_device(const std::string& dev) throw();
414 * Get sound device.
416 static std::string get_sound_device() throw(std::bad_alloc)
418 return sound_plugin::get_device();
421 * Get list of sound devices.
423 static std::map<std::string, std::string> get_sound_devices() throw(std::bad_alloc)
425 return sound_plugin::get_devices();
428 * Show modal message dialog.
430 * Parameter text: The text for dialog.
431 * Parameter confirm: If true, display confirmation dialog, if false, display notification dialog.
432 * Returns: True, if confirmation dialog was confirmed, otherwise false.
434 static bool modal_message(const std::string& text, bool confirm = false) throw()
436 return graphics_plugin::modal_message(text, confirm);
439 * Process command and keypress queues.
441 * - If emulating normally, this routine returns fast.
442 * - If emulator is in pause mode, this routine will block until emulator has left pause mode.
443 * - If emulator is in some special mode, this routine can block until said mode is left.
445 static void flush_command_queue() throw();
447 * Enable/Disable pause mode.
449 * - This function doesn't actually block. For actual paused blocking, use flush_command_queue().
451 * Parameter enable: Enable pause mode if true, disable pause mode if false.
453 static void set_paused(bool enable) throw();
455 * Wait specified number of milliseconds before returning.
457 * - The command and keypresses queues are processed while waiting.
459 * Parameter usec: The number of microseconds to wait.
461 static void wait(uint64_t usec) throw();
463 * Cause call to wait() to return immediately.
465 static void cancel_wait() throw();
467 * Notify received message.
469 static void notify_message() throw()
471 graphics_plugin::notify_message();
474 * Notify changed status.
476 static void notify_status() throw()
478 graphics_plugin::notify_status();
481 * Notify changed screen.
483 static void notify_screen() throw()
485 graphics_plugin::notify_screen();
488 * Input audio sample (at specified rate).
490 * parameter left: Left sample.
491 * parameter right: Right sample.
493 static void audio_sample(uint16_t left, uint16_t right) throw()
495 sound_plugin::sample(left, right);
498 * Set modal pause mode.
500 * - Modal pause works like ordinary pause, except it uses a separate flag.
502 * Parameter enable: If true, enable modal pause, else disable it.
504 static void set_modal_pause(bool enable) throw();
506 * Queue keypress.
508 * - Can be called from any thread.
510 * Parameter k: The keypress to queue.
512 static void queue(const keypress& k) throw(std::bad_alloc);
514 * Queue command.
516 * - Can be called from any thread.
518 * Parameter c: The command to queue.
520 static void queue(const std::string& c) throw(std::bad_alloc);
522 * Queue function to be called in emulation thread.
524 * - Can be called from any thread (exception: Synchronous mode can not be used from emulation nor main threads).
526 * Parameter f: The function to execute.
527 * Parameter arg: Argument to pass to the function.
528 * Parameter sync: If true, execute function call synchronously, else asynchronously.
530 static void queue(void (*f)(void* arg), void* arg, bool sync) throw(std::bad_alloc);
532 * Run all queues.
534 static void run_queues() throw();
537 template<typename T>
538 void functor_call_helper(void* args)
540 (*reinterpret_cast<T*>(args))();
543 template<typename T>
544 void runemufn(T fn)
546 platform::queue(functor_call_helper<T>, &fn, true);
550 * If set, queueing synchronous function produces a warning.
552 extern volatile bool queue_synchronous_fn_warning;
554 #endif