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"
7 input_queue::input_queue(command::group
& _command
)
10 functions_executed
= 0;
12 system_thread_available
= false;
13 queue_function_run
= false;
17 keypress_info::keypress_info()
24 keypress_info::keypress_info(keyboard::modifier_set mod
, keyboard::key
& _key
, short _value
)
32 keypress_info::keypress_info(keyboard::modifier_set mod
, keyboard::key
& _key
, keyboard::key
& _key2
, short _value
)
41 void input_queue::queue(const keypress_info
& k
) throw(std::bad_alloc
)
43 threads::alock
h(queue_lock
);
44 keypresses
.push_back(k
);
45 queue_condition
.notify_all();
48 void input_queue::queue(const std::string
& c
) throw(std::bad_alloc
)
50 threads::alock
h(queue_lock
);
51 commands
.push_back(std::make_pair(nullptr, c
));
52 queue_condition
.notify_all();
55 void input_queue::queue(const char* c
, const std::string
& a
) throw(std::bad_alloc
)
57 threads::alock
h(queue_lock
);
58 commands
.push_back(std::make_pair(c
, a
));
59 queue_condition
.notify_all();
62 void input_queue::queue(std::function
<void()> f
, std::function
<void(std::exception
& e
)> onerror
, bool sync
)
65 if(!system_thread_available
) {
68 } catch(std::exception
& e
) {
73 threads::alock
h(queue_lock
);
75 function_queue_entry entry
;
77 entry
.onerror
= onerror
;
78 functions
.push_back(entry
);
79 queue_condition
.notify_all();
81 while(functions_executed
< next_function
&& system_thread_available
) {
82 threads::cv_timed_wait(queue_condition
, h
, threads::ustime(10000));
83 random_mix_timing_entropy();
87 void input_queue::run_queue(bool unlocked
) throw()
93 while(!keypresses
.empty()) {
94 keypress_info k
= keypresses
.front();
95 keypresses
.pop_front();
98 k
.key1
->set_state(k
.modifiers
, k
.value
);
100 k
.key2
->set_state(k
.modifiers
, k
.value
);
102 queue_function_run
= true;
105 while(!commands
.empty()) {
106 auto c
= commands
.front();
107 commands
.pop_front();
110 command
.invoke(c
.first
, c
.second
);
112 command
.invoke(c
.second
);
114 queue_function_run
= true;
117 while(!functions
.empty()) {
118 function_queue_entry f
= functions
.front();
119 functions
.pop_front();
123 } catch(std::exception
& e
) {
127 ++functions_executed
;
128 queue_function_run
= true;
130 queue_condition
.notify_all();
131 } catch(std::bad_alloc
& e
) {
133 } catch(std::exception
& e
) {
134 std::cerr
<< "Fault inside platform::run_queues(): " << e
.what() << std::endl
;