Port the generic control stuff from wxwidgets work
[lsnes.git] / generic / window.hpp
blob60b703168ac871b10f341a71d651afe052eb749a
1 #ifndef _window__hpp__included__
2 #define _window__hpp__included__
4 #include "render.hpp"
5 #include "messagebuffer.hpp"
6 #include <string>
7 #include <map>
8 #include <list>
9 #include <stdexcept>
11 #define WINSTATE_NORMAL 0
12 #define WINSTATE_COMMAND 1
13 #define WINSTATE_MODAL 2
14 #define WINSTATE_IDENTIFY 3
16 class window;
18 /**
19 * Some backnotifications.
21 class window_callback
23 public:
24 /**
25 * Register a callback
27 window_callback() throw();
28 /**
29 * Unregister a callback.
31 virtual ~window_callback() throw();
32 /**
33 * Called when user tries to close the window.
35 virtual void on_close() throw();
36 /**
37 * Called when user clicks on the screen.
39 virtual void on_click(int32_t x, int32_t y, uint32_t buttonmask) throw();
40 /**
41 * Called when sound mute/unmute gets (possibly) changed.
43 virtual void on_sound_unmute(bool unmuted) throw();
44 /**
45 * Called when sound device gets (possibly) changed.
47 virtual void on_sound_change(const std::string& dev) throw();
48 /**
49 * Called when mode gets (possibly) changed.
51 virtual void on_mode_change(bool readonly) throw();
52 /**
53 * Called when autohold (possibly) changes.
55 virtual void on_autohold_update(unsigned pid, unsigned ctrlnum, bool newstate);
56 /**
57 * Called when controllers (possibly) change.
59 virtual void on_autohold_reconfigure();
60 /**
61 * Called when setting is changed.
63 virtual void on_setting_change(const std::string& setting, const std::string& value);
64 /**
65 * Called when setting is cleared.
67 virtual void on_setting_clear(const std::string& setting);
68 /**
69 * Do try to close the window.
71 static void do_close() throw();
72 /**
73 * Do click on the screen.
75 static void do_click(int32_t x, int32_t y, uint32_t buttonmask) throw();
76 /**
77 * Do on_sound_unmute.
79 static void do_sound_unmute(bool unmuted) throw();
80 /**
81 * Do on_sound_change
83 static void do_sound_change(const std::string& dev) throw();
84 /**
85 * Do on_mode_change
87 static void do_mode_change(bool readonly) throw();
88 /**
89 * Do on_autohold_update
91 static void do_autohold_update(unsigned pid, unsigned ctrlnum, bool newstate);
92 /**
93 * Do on_autohold_reconfigure
95 static void do_autohold_reconfigure();
96 /**
97 * Do on_setting_change
99 static void do_setting_change(const std::string& setting, const std::string& value);
101 * Do on_setting_clear
103 static void do_setting_clear(const std::string& setting);
104 private:
105 window_callback(window_callback&);
106 window_callback& operator=(window_callback&);
110 * Sound/Graphics init/quit functions. Sound init is called after graphics init, and vice versa for quit.
112 * These need to be implemented by the corresponding plugins.
114 void graphics_init();
115 void sound_init();
116 void sound_quit();
117 void graphics_quit();
118 void joystick_init();
119 void joystick_quit();
122 * This is a handle to graphics system. Note that creating multiple contexts produces undefined results.
124 class window
126 public:
128 * Window constructor.
130 window() throw() {}
133 * Initialize the graphics system.
135 * Implemented by generic window code.
137 static void init();
140 * Shut down the graphics system.
142 * Implemented by generic window code.
144 static void quit();
147 * Get output stream printing into message queue.
149 * Note that lines printed there should be terminated by '\n'.
151 * Implemented by the generic window code.
153 * returns: The output stream.
154 * throws std::bad_alloc: Not enough memory.
156 static std::ostream& out() throw(std::bad_alloc);
159 * Get emulator status area
161 * Implemented by the generic window code.
163 * returns: Emulator status area.
165 static std::map<std::string, std::string>& get_emustatus() throw();
168 * Set window main screen compensation parameters. This is used for mouse click reporting.
170 * Implemented by the generic window code.
172 * parameter xoffset: X coordinate of origin.
173 * parameter yoffset: Y coordinate of origin.
174 * parameter hscl: Horizontal scaling factor.
175 * parameter vscl: Vertical scaling factor.
177 static void set_window_compensation(uint32_t xoffset, uint32_t yoffset, uint32_t hscl, uint32_t vscl);
180 * Message buffer.
182 * Implemented by the generic window code.
184 static messagebuffer msgbuf;
187 * Adds a messages to mesage queue to be shown.
189 * Implemented by the generic window code.
191 * parameter msg: The messages to add (split by '\n').
192 * throws std::bad_alloc: Not enough memory.
194 static void message(const std::string& msg) throw(std::bad_alloc);
197 * Displays fatal error message, quitting after the user acks it (called by fatal_error()).
199 * Needs to be implemented by the graphics plugin.
201 static void fatal_error() throw();
204 * Enable or disable sound.
206 * Implemented by the generic window code.
208 * parameter enable: Enable sounds if true, otherwise disable sounds.
210 static void sound_enable(bool enable) throw();
213 * Are sounds enabled?
215 static bool is_sound_enabled() throw();
218 * Set sound device.
220 * Implemented by the generic window code.
222 static void set_sound_device(const std::string& dev) throw();
224 /******************************** GRAPHICS PLUGIN **********************************/
226 * Notification when messages get updated.
228 * Needs to be implemented by the graphics plugin.
230 static void notify_message() throw(std::bad_alloc, std::runtime_error);
233 * Displays a modal message, not returning until the message is acknowledged. Keybindings are not available, but
234 * should quit be generated somehow, modal message will be closed and command callback triggered.
236 * Needs to be implemented by the graphics plugin.
238 * parameter msg: The message to show.
239 * parameter confirm: If true, ask for Ok/cancel type input.
240 * returns: If confirm is true, true if ok was chosen, false if cancel was chosen. Otherwise always false.
241 * throws std::bad_alloc: Not enough memory.
243 static bool modal_message(const std::string& msg, bool confirm = false) throw(std::bad_alloc);
246 * Displays fatal error message, quitting after the user acks it (called by fatal_error()).
248 * Needs to be implemented by the graphics plugin.
250 static void fatal_error2() throw();
253 * Processes inputs. If in non-modal mode (normal mode without pause), this returns quickly. Otherwise it waits
254 * for modal mode to exit. Also needs to call window::poll_joysticks().
256 * Needs to be implemented by the graphics plugin.
258 * throws std::bad_alloc: Not enough memory.
260 static void poll_inputs() throw(std::bad_alloc);
263 * Notify that the screen has been updated.
265 * Needs to be implemented by the graphics plugin.
267 * parameter full: Do full refresh if true.
269 static void notify_screen_update(bool full = false) throw();
272 * Set the screen to use as main surface.
274 * Needs to be implemented by the graphics plugin.
276 * parameter scr: The screen to use.
278 static void set_main_surface(screen& scr) throw();
281 * Enable/Disable pause mode.
283 * Needs to be implemented by the graphics plugin.
285 * parameter enable: Enable pause if true, disable otherwise.
287 static void paused(bool enable) throw();
290 * Wait specified number of microseconds (polling for input).
292 * Needs to be implemented by the graphics plugin.
294 * parameter usec: Number of us to wait.
295 * throws std::bad_alloc: Not enough memory.
297 static void wait_usec(uint64_t usec) throw(std::bad_alloc);
300 * Cancel pending wait_usec, making it return now.
302 * Needs to be implemented by the graphics plugin.
304 static void cancel_wait() throw();
306 /******************************** SOUND PLUGIN **********************************/
308 * Enable or disable sound.
310 * Needs to be implemented by the sound plugin.
312 * parameter enable: Enable sounds if true, otherwise disable sounds.
314 static void _sound_enable(bool enable) throw();
317 * Input audio sample (at specified rate).
319 * Needs to be implemented by the sound plugin.
321 * parameter left: Left sample.
322 * parameter right: Right sample.
324 static void play_audio_sample(uint16_t left, uint16_t right) throw();
327 * Set sound sampling rate.
329 * Needs to be implemented by the sound plugin.
331 * parameter rate_n: Numerator of sampling rate.
332 * parameter rate_d: Denomerator of sampling rate.
334 static void set_sound_rate(uint32_t rate_n, uint32_t rate_d);
337 * Has the sound system been successfully initialized?
339 * Needs to be implemented by the sound plugin.
341 static bool sound_initialized();
344 * Set sound device.
346 * Needs to be implemented by the sound plugin.
348 static void _set_sound_device(const std::string& dev);
351 * Get current sound device.
353 * Needs to be implemented by the sound plugin.
355 static std::string get_current_sound_device();
358 * Get available sound devices.
360 * Needs to be implemented by the sound plugin.
362 static std::map<std::string, std::string> get_sound_devices();
364 /******************************** JOYSTICK PLUGIN **********************************/
366 * Poll joysticks.
368 * Needs to be implemented by the joystick plugin.
370 static void poll_joysticks();
371 private:
372 window(const window&);
373 window& operator==(const window&);
378 * Names of plugins.
380 extern const char* sound_plugin_name;
381 extern const char* graphics_plugin_name;
382 extern const char* joystick_plugin_name;
384 #endif