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"
16 * Information about keypress.
21 * Create null keypress (no modifiers, NULL key and released).
25 * Create new keypress.
27 keypress_info(keyboard::modifier_set mod
, keyboard::key
& _key
, short _value
);
29 * Create new keypress (two keys).
31 keypress_info(keyboard::modifier_set mod
, keyboard::key
& _key
, keyboard::key
& _key2
, short _value
);
35 keyboard::modifier_set modifiers
;
37 * The actual key (first)
41 * The actual key (second)
51 void functor_call_helper2(void* args
)
53 (*reinterpret_cast<T
*>(args
))();
54 delete reinterpret_cast<T
*>(args
);
59 struct function_queue_entry
61 std::function
<void()> fn
;
62 std::function
<void(std::exception
& e
)> onerror
;
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
;
78 input_queue(command::group
& _command
);
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
);
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
);
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
);
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);
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();
142 command::group
& command
;