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"
15 * Information about keypress.
20 * Create null keypress (no modifiers, NULL key and released).
24 * Create new keypress.
26 keypress_info(keyboard::modifier_set mod
, keyboard::key
& _key
, short _value
);
28 * Create new keypress (two keys).
30 keypress_info(keyboard::modifier_set mod
, keyboard::key
& _key
, keyboard::key
& _key2
, short _value
);
34 keyboard::modifier_set modifiers
;
36 * The actual key (first)
40 * The actual key (second)
50 void functor_call_helper2(void* args
)
52 (*reinterpret_cast<T
*>(args
))();
53 delete reinterpret_cast<T
*>(args
);
58 struct function_queue_entry
60 std::function
<void()> fn
;
61 std::function
<void(std::exception
& e
)> onerror
;
65 threads::lock queue_lock
;
66 threads::cv queue_condition
;
67 std::deque
<keypress_info
> keypresses
;
68 std::deque
<std::pair
<const char*, std::string
>> commands
;
69 std::deque
<function_queue_entry
> functions
;
70 volatile uint64_t functions_executed
;
71 volatile uint64_t next_function
;
72 volatile bool system_thread_available
;
73 bool queue_function_run
;
77 input_queue(command::group
& _command
);
81 * - Can be called from any thread.
83 * Parameter k: The keypress to queue.
85 void queue(const keypress_info
& k
) throw(std::bad_alloc
);
89 * - Can be called from any thread.
91 * Parameter c: The command to queue.
93 void queue(const std::string
& c
) throw(std::bad_alloc
);
95 * Queue command and arguments.
97 * - Can be called from any thread.
99 * Parameter c: The command to queue.
100 * Parameter a: The arguments for function.
102 void queue(const char* c
, const std::string
& a
) throw(std::bad_alloc
);
104 * Queue function to be called in emulation thread.
106 * - Can be called from any thread (exception: Synchronous mode can not be used from emulation nor main threads).
108 * Parameter f: The function to execute.
109 * Parameter onerror: Function to call on error.
110 * Parameter arg: Argument to pass to the function.
111 * Parameter sync: If true, execute function call synchronously, else asynchronously.
113 void queue(std::function
<void()> f
, std::function
<void(std::exception
& e
)> onerror
, bool sync
)
114 throw(std::bad_alloc
);
118 void run_queues() throw();
120 * Call function synchronously in emulation thread.
122 void run(std::function
<void()> fn
)
124 exrethrow::storage ex
;
125 queue(fn
, [&ex
](std::exception
& e
) { ex
= exrethrow::storage(e
); }, true);
129 * Queue asynchrous function in emulation thread.
131 void run_async(std::function
<void()> fn
,
132 std::function
<void(std::exception
& e
)> onerror
)
134 queue(fn
, onerror
, false);
137 * Run internal queues.
139 void run_queue(bool unlocked
) throw();
141 command::group
& command
;