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::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 function to be called in emulation thread.
97 * - Can be called from any thread (exception: Synchronous mode can not be used from emulation nor main threads).
99 * Parameter f: The function to execute.
100 * Parameter onerror: Function to call on error.
101 * Parameter arg: Argument to pass to the function.
102 * Parameter sync: If true, execute function call synchronously, else asynchronously.
104 void queue(std::function
<void()> f
, std::function
<void(std::exception
& e
)> onerror
, bool sync
)
105 throw(std::bad_alloc
);
109 void run_queues() throw();
111 * Call function synchronously in emulation thread.
113 void run(std::function
<void()> fn
)
115 exrethrow::storage ex
;
116 queue(fn
, [&ex
](std::exception
& e
) { ex
= exrethrow::storage(e
); }, true);
120 * Queue asynchrous function in emulation thread.
122 void run_async(std::function
<void()> fn
,
123 std::function
<void(std::exception
& e
)> onerror
)
125 queue(fn
, onerror
, false);
128 * Run internal queues.
130 void run_queue(bool unlocked
) throw();
132 command::group
& command
;