Delay committing fullscreen until seeing window size change
[lsnes.git] / include / core / queue.hpp
blobdcdbbc4a3ab890122791006629c573c2a58bdd2b
1 #ifndef _queue__hpp__included__
2 #define _queue__hpp__included__
4 #include "library/exrethrow.hpp"
5 #include "library/keyboard.hpp"
6 #include "library/threads.hpp"
7 #include <functional>
8 #include <deque>
10 namespace command
12 class group;
15 /**
16 * Information about keypress.
18 struct keypress_info
20 /**
21 * Create null keypress (no modifiers, NULL key and released).
23 keypress_info();
24 /**
25 * Create new keypress.
27 keypress_info(keyboard::modifier_set mod, keyboard::key& _key, short _value);
28 /**
29 * Create new keypress (two keys).
31 keypress_info(keyboard::modifier_set mod, keyboard::key& _key, keyboard::key& _key2, short _value);
32 /**
33 * Modifier set.
35 keyboard::modifier_set modifiers;
36 /**
37 * The actual key (first)
39 keyboard::key* key1;
40 /**
41 * The actual key (second)
43 keyboard::key* key2;
44 /**
45 * Value for the press
47 short value;
50 template<typename T>
51 void functor_call_helper2(void* args)
53 (*reinterpret_cast<T*>(args))();
54 delete reinterpret_cast<T*>(args);
57 struct input_queue
59 struct function_queue_entry
61 std::function<void()> fn;
62 std::function<void(std::exception& e)> onerror;
65 //Queue stuff.
66 threads::lock queue_lock;
67 threads::cv queue_condition;
68 std::deque<keypress_info> keypresses;
69 std::deque<std::pair<const char*, std::string>> commands;
70 std::deque<function_queue_entry> functions;
71 volatile uint64_t functions_executed;
72 volatile uint64_t next_function;
73 volatile bool system_thread_available;
74 bool queue_function_run;
75 /**
76 * Ctor.
78 input_queue(command::group& _command);
79 /**
80 * Queue keypress.
82 * - Can be called from any thread.
84 * Parameter k: The keypress to queue.
86 void queue(const keypress_info& k) throw(std::bad_alloc);
87 /**
88 * Queue command.
90 * - Can be called from any thread.
92 * Parameter c: The command to queue.
94 void queue(const std::string& c) throw(std::bad_alloc);
95 /**
96 * Queue command and arguments.
98 * - Can be called from any thread.
100 * Parameter c: The command to queue.
101 * Parameter a: The arguments for function.
103 void queue(const char* c, const std::string& a) throw(std::bad_alloc);
105 * Queue function to be called in emulation thread.
107 * - Can be called from any thread (exception: Synchronous mode can not be used from emulation nor main threads).
109 * Parameter f: The function to execute.
110 * Parameter onerror: Function to call on error.
111 * Parameter arg: Argument to pass to the function.
112 * Parameter sync: If true, execute function call synchronously, else asynchronously.
114 void queue(std::function<void()> f, std::function<void(std::exception& e)> onerror, bool sync)
115 throw(std::bad_alloc);
117 * Run all queues.
119 void run_queues() throw();
121 * Call function synchronously in emulation thread.
123 void run(std::function<void()> fn)
125 exrethrow::storage ex;
126 queue(fn, [&ex](std::exception& e) { ex = exrethrow::storage(e); }, true);
127 if(ex) ex.rethrow();
130 * Queue asynchrous function in emulation thread.
132 void run_async(std::function<void()> fn,
133 std::function<void(std::exception& e)> onerror)
135 queue(fn, onerror, false);
138 * Run internal queues.
140 void run_queue(bool unlocked) throw();
141 private:
142 command::group& command;
145 #endif