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"
13 class emulator_status
;
21 * Hold mutex RAII-style.
25 holder(mutex
& m
) throw();
31 * Create a mutex. The returned mutex can be deleted using delete.
33 static mutex
& aquire() throw(std::bad_alloc
);
37 virtual ~mutex() throw();
41 virtual void lock() throw() = 0;
45 virtual void unlock() throw() = 0;
56 * Create a condition variable. The returned condition can be freed using delete.
58 static condition
& aquire(mutex
& m
) throw(std::bad_alloc
);
60 * Destroy a condition.
62 virtual ~condition() throw();
64 * Return associated mutex.
66 mutex
& associated() throw();
68 * Wait for condition. The associate mutex must be locked.
70 virtual bool wait(uint64_t max_usec
) throw() = 0;
72 * Signal a condition. The associated mutex should be locked.
74 virtual void signal() throw() = 0;
86 * Return thread id for this thread. Can be freed with delete.
88 static thread_id
& me() throw(std::bad_alloc
);
92 virtual ~thread_id() throw();
96 virtual bool is_me() throw() = 0;
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();
121 void* join() throw();
125 * Notify that this thread has quit.
127 void notify_quit(void* retval
) throw();
131 virtual void _join() throw() = 0;
139 * Information about keypress.
144 * Create null keypress (no modifiers, NULL key and released).
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
);
158 modifier_set modifiers
;
160 * The actual key (first)
164 * The actual key (second)
168 * Value for the press
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.
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();
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.
337 * Initialize the system.
342 * Shut down the system.
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();
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();
412 static void set_sound_device(const std::string
& dev
) throw();
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();
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
);
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
);
534 static void run_queues() throw();
538 * Send a mouse click.
540 * Call from UI thread.
542 void send_mouse_click(long x
, long y
, uint32_t buttons
);
545 void functor_call_helper(void* args
)
547 (*reinterpret_cast<T
*>(args
))();
553 platform::queue(functor_call_helper
<T
>, &fn
, true);
557 * If set, queueing synchronous function produces a warning.
559 extern volatile bool queue_synchronous_fn_warning
;