1 #include "core/misc.hpp"
2 #include "core/queue.hpp"
3 #include "core/random.hpp"
4 #include "library/command.hpp"
5 #include "library/threads.hpp"
8 input_queue::input_queue(command::group
& _command
)
11 functions_executed
= 0;
13 system_thread_available
= false;
14 queue_function_run
= false;
18 keypress_info::keypress_info()
25 keypress_info::keypress_info(keyboard::modifier_set mod
, keyboard::key
& _key
, short _value
)
33 keypress_info::keypress_info(keyboard::modifier_set mod
, keyboard::key
& _key
, keyboard::key
& _key2
, short _value
)
42 void input_queue::queue(const keypress_info
& k
) throw(std::bad_alloc
)
44 threads::alock
h(queue_lock
);
45 keypresses
.push_back(k
);
46 queue_condition
.notify_all();
49 void input_queue::queue(const std::string
& c
) throw(std::bad_alloc
)
51 threads::alock
h(queue_lock
);
52 commands
.push_back(std::make_pair(nullptr, c
));
53 queue_condition
.notify_all();
56 void input_queue::queue(const char* c
, const std::string
& a
) throw(std::bad_alloc
)
58 threads::alock
h(queue_lock
);
59 commands
.push_back(std::make_pair(c
, a
));
60 queue_condition
.notify_all();
63 void input_queue::queue(std::function
<void()> f
, std::function
<void(std::exception
& e
)> onerror
, bool sync
)
66 if(!system_thread_available
) {
69 } catch(std::exception
& e
) {
74 threads::alock
h(queue_lock
);
76 function_queue_entry entry
;
78 entry
.onerror
= onerror
;
79 functions
.push_back(entry
);
80 queue_condition
.notify_all();
82 while(functions_executed
< next_function
&& system_thread_available
) {
83 threads::cv_timed_wait(queue_condition
, h
, threads::ustime(10000));
84 random_mix_timing_entropy();
88 void input_queue::run_queue(bool unlocked
) throw()
94 while(!keypresses
.empty()) {
95 keypress_info k
= keypresses
.front();
96 keypresses
.pop_front();
99 k
.key1
->set_state(k
.modifiers
, k
.value
);
101 k
.key2
->set_state(k
.modifiers
, k
.value
);
103 queue_function_run
= true;
106 while(!commands
.empty()) {
107 auto c
= commands
.front();
108 commands
.pop_front();
111 command
.invoke(c
.first
, c
.second
);
113 command
.invoke(c
.second
);
115 queue_function_run
= true;
118 while(!functions
.empty()) {
119 function_queue_entry f
= functions
.front();
120 functions
.pop_front();
124 } catch(std::exception
& e
) {
128 ++functions_executed
;
129 queue_function_run
= true;
131 queue_condition
.notify_all();
132 } catch(std::bad_alloc
& e
) {
134 } catch(std::exception
& e
) {
135 std::cerr
<< "Fault inside platform::run_queues(): " << e
.what() << std::endl
;