]>
Commit | Line | Data |
---|---|---|
4b2d79ca | 1 | from kivy.uix.widget import Widget |
2e404903 IB |
2 | from kivy.properties import AliasProperty, BooleanProperty, \ |
3 | ListProperty, StringProperty | |
4b2d79ca IB |
4 | from kivy.uix.behaviors import ButtonBehavior |
5 | ||
e55b29bb | 6 | from .action import Action |
6ebe6247 | 7 | from .helpers import debug_print |
be27763f | 8 | import time |
34382290 | 9 | import threading |
e55b29bb | 10 | from transitions.extensions import HierarchicalMachine as Machine |
4b2d79ca | 11 | |
a9324e30 IB |
12 | # All drawing operations should happen in the main thread |
13 | # https://github.com/kivy/kivy/wiki/Working-with-Python-threads-inside-a-Kivy-application | |
14 | from kivy.clock import mainthread | |
15 | ||
93a3e51e | 16 | class KeyMachine(): |
e55b29bb IB |
17 | STATES = [ |
18 | 'initial', | |
19 | 'configuring', | |
20 | 'configured', | |
21 | 'loading', | |
22 | 'failed', | |
23 | { | |
24 | 'name': 'loaded', | |
34382290 IB |
25 | 'children': [ |
26 | 'no_config', | |
27 | 'no_actions', | |
28 | 'running', | |
29 | 'protecting_repeat' | |
30 | ] | |
e55b29bb IB |
31 | } |
32 | ] | |
33 | ||
34 | TRANSITIONS = [ | |
35 | { | |
36 | 'trigger': 'configure', | |
37 | 'source': 'initial', | |
38 | 'dest': 'configuring' | |
39 | }, | |
40 | { | |
41 | 'trigger': 'fail', | |
42 | 'source': 'configuring', | |
8ba7d831 IB |
43 | 'dest': 'failed', |
44 | 'after': 'key_loaded_callback' | |
e55b29bb IB |
45 | }, |
46 | { | |
47 | 'trigger': 'success', | |
48 | 'source': 'configuring', | |
49 | 'dest': 'configured', | |
50 | 'after': 'load' | |
51 | }, | |
52 | { | |
53 | 'trigger': 'no_config', | |
54 | 'source': 'configuring', | |
55 | 'dest': 'loaded_no_config', | |
8ba7d831 | 56 | 'after': 'key_loaded_callback' |
e55b29bb IB |
57 | }, |
58 | { | |
59 | 'trigger': 'load', | |
60 | 'source': 'configured', | |
61 | 'dest': 'loading' | |
62 | }, | |
63 | { | |
64 | 'trigger': 'fail', | |
65 | 'source': 'loading', | |
8ba7d831 IB |
66 | 'dest': 'failed', |
67 | 'after': 'key_loaded_callback' | |
e55b29bb IB |
68 | }, |
69 | { | |
70 | 'trigger': 'success', | |
71 | 'source': 'loading', | |
8ba7d831 IB |
72 | 'dest': 'loaded', |
73 | 'after': 'key_loaded_callback' | |
e55b29bb IB |
74 | }, |
75 | { | |
76 | 'trigger': 'no_actions', | |
77 | 'source': 'loading', | |
78 | 'dest': 'loaded_no_actions', | |
8ba7d831 | 79 | 'after': 'key_loaded_callback' |
e55b29bb IB |
80 | }, |
81 | { | |
82 | 'trigger': 'reload', | |
ab47d2a1 | 83 | 'source': ['loaded','failed'], |
8ba7d831 IB |
84 | 'dest': 'configuring', |
85 | 'after': 'key_loaded_callback' | |
e55b29bb IB |
86 | }, |
87 | { | |
88 | 'trigger': 'run', | |
89 | 'source': 'loaded', | |
90 | 'dest': 'loaded_running', | |
b17aed6a | 91 | 'after': ['run_actions', 'finish'], |
6c42e32d IB |
92 | # if a child, like loaded_no_actions, has no transitions, then it |
93 | # is bubbled to the parent, and we don't want that. | |
e55b29bb IB |
94 | 'conditions': ['is_loaded'] |
95 | }, | |
96 | { | |
97 | 'trigger': 'finish', | |
98 | 'source': 'loaded_running', | |
34382290 IB |
99 | 'dest': 'loaded_protecting_repeat' |
100 | }, | |
101 | { | |
102 | 'trigger': 'repeat_protection_finished', | |
103 | 'source': 'loaded_protecting_repeat', | |
93a3e51e IB |
104 | 'dest': 'loaded', |
105 | 'after': 'callback_action_state_changed' | |
34382290 | 106 | }, |
e55b29bb IB |
107 | ] |
108 | ||
93a3e51e IB |
109 | def __setattr__(self, name, value): |
110 | if hasattr(self, 'initialized') and name == 'state': | |
111 | self.key.update_state(value) | |
112 | super().__setattr__(name, value) | |
ba219325 IB |
113 | |
114 | def __init__(self, key, **kwargs): | |
115 | self.key = key | |
116 | ||
117 | Machine(model=self, states=self.STATES, | |
118 | transitions=self.TRANSITIONS, initial='initial', | |
119 | ignore_invalid_triggers=True, queued=True) | |
93a3e51e IB |
120 | |
121 | self.initialized = True | |
ba219325 IB |
122 | |
123 | # Machine states / events | |
124 | def is_loaded_or_failed(self): | |
125 | return self.is_loaded(allow_substates=True) or self.is_failed() | |
126 | ||
127 | def is_loaded_inactive(self): | |
128 | return self.is_loaded_no_config() or self.is_loaded_no_actions() | |
129 | ||
a9324e30 | 130 | @mainthread |
ba219325 | 131 | def on_enter_configuring(self): |
045a1b63 IB |
132 | self.destroy_actions() |
133 | self.key.unset_description() | |
134 | self.key.unset_color() | |
135 | ||
ba219325 IB |
136 | if self.key.key_sym in self.key.parent.key_config: |
137 | self.key.config = self.key.parent.key_config[self.key.key_sym] | |
138 | ||
ba219325 IB |
139 | for key_action in self.key.config['actions']: |
140 | self.key.add_action(key_action[0], **key_action[1]) | |
141 | ||
142 | if 'description' in self.key.config['properties']: | |
143 | self.key.set_description(self.key.config['properties']['description']) | |
ba219325 IB |
144 | if 'color' in self.key.config['properties']: |
145 | self.key.set_color(self.key.config['properties']['color']) | |
ba219325 IB |
146 | self.success() |
147 | else: | |
148 | self.no_config() | |
149 | ||
150 | def on_enter_loading(self): | |
151 | if len(self.key.actions) > 0: | |
152 | for action in self.key.actions: | |
153 | action.load() | |
154 | else: | |
155 | self.no_actions() | |
156 | ||
045a1b63 IB |
157 | def destroy_actions(self): |
158 | for action in self.key.actions: | |
159 | action.destroy() | |
160 | self.key.actions = [] | |
161 | ||
ba219325 IB |
162 | def run_actions(self, modifiers): |
163 | self.key.parent.parent.ids['KeyList'].append(self.key.key_sym) | |
164 | debug_print("running actions for {}".format(self.key.key_sym)) | |
165 | start_time = time.time() | |
06ed113d | 166 | self.key.parent.start_running(self.key, start_time) |
ba219325 | 167 | for self.key.current_action in self.key.actions: |
06ed113d | 168 | if self.key.parent.keep_running(self.key, start_time): |
ba219325 IB |
169 | self.key.list_actions() |
170 | self.key.current_action.run(start_time) | |
171 | self.key.list_actions(last_action_finished=True) | |
172 | ||
06ed113d | 173 | self.key.parent.finished_running(self.key, start_time) |
ba219325 IB |
174 | |
175 | def on_enter_loaded_protecting_repeat(self, modifiers): | |
814c30c6 | 176 | if self.key.repeat_delay > 0: |
ba219325 | 177 | self.key.protecting_repeat_timer = threading.Timer( |
814c30c6 | 178 | self.key.repeat_delay, |
ba219325 IB |
179 | self.key.repeat_protection_finished) |
180 | self.key.protecting_repeat_timer.start() | |
181 | else: | |
182 | self.key.repeat_protection_finished() | |
183 | ||
184 | # Callbacks | |
a9324e30 | 185 | @mainthread |
ba219325 IB |
186 | def key_loaded_callback(self): |
187 | self.key.parent.key_loaded_callback() | |
188 | ||
93a3e51e IB |
189 | def callback_action_state_changed(self): |
190 | if self.state not in ['failed', 'loading', 'loaded']: | |
191 | return | |
192 | ||
193 | if any(action.is_failed() for action in self.key.actions): | |
194 | self.to_failed() | |
195 | elif any(action.is_loading() for action in self.key.actions): | |
196 | self.to_loading() | |
197 | else: | |
198 | self.to_loaded() | |
199 | self.key_loaded_callback() | |
ba219325 IB |
200 | |
201 | class Key(ButtonBehavior, Widget): | |
202 | ||
4b2d79ca | 203 | key_sym = StringProperty(None) |
e55b29bb | 204 | custom_color = ListProperty([0, 1, 0]) |
4b2d79ca IB |
205 | description_title = StringProperty("") |
206 | description = ListProperty([]) | |
ba219325 | 207 | machine_state = StringProperty("") |
4b2d79ca | 208 | |
635dea02 | 209 | def get_alias_line_cross_color(self): |
ea97edb3 IB |
210 | if not self.is_failed() and ( |
211 | not self.is_loaded(allow_substates=True)\ | |
212 | or self.is_loaded_running()\ | |
213 | or self.is_loaded_protecting_repeat()): | |
635dea02 IB |
214 | return [120/255, 120/255, 120/255, 1] |
215 | else: | |
216 | return [0, 0, 0, 0] | |
217 | ||
218 | def set_alias_line_cross_color(self): | |
219 | pass | |
220 | ||
221 | line_cross_color = AliasProperty( | |
222 | get_alias_line_cross_color, | |
223 | set_alias_line_cross_color, | |
ba219325 | 224 | bind=['machine_state']) |
635dea02 | 225 | |
1094ab1a IB |
226 | def get_alias_line_color(self): |
227 | if self.is_loaded_running(): | |
228 | return [0, 0, 0, 1] | |
229 | else: | |
230 | return [120/255, 120/255, 120/255, 1] | |
231 | ||
232 | def set_alias_line_color(self): | |
233 | pass | |
234 | ||
235 | line_color = AliasProperty(get_alias_line_color, set_alias_line_color, | |
ba219325 | 236 | bind=['machine_state']) |
1094ab1a | 237 | |
e55b29bb IB |
238 | def get_alias_color(self): |
239 | if self.is_loaded_inactive(): | |
4b2d79ca | 240 | return [1, 1, 1, 1] |
34382290 IB |
241 | elif self.is_loaded_protecting_repeat(): |
242 | return [*self.custom_color, 100/255] | |
70cfb266 IB |
243 | elif self.is_loaded_running(): |
244 | return [*self.custom_color, 100/255] | |
e55b29bb IB |
245 | elif self.is_loaded(allow_substates=True): |
246 | return [*self.custom_color, 1] | |
247 | elif self.is_failed(): | |
248 | return [0, 0, 0, 1] | |
be27763f | 249 | else: |
e55b29bb IB |
250 | return [*self.custom_color, 100/255] |
251 | def set_alias_color(self): | |
4b2d79ca IB |
252 | pass |
253 | ||
e55b29bb | 254 | color = AliasProperty(get_alias_color, set_alias_color, |
ba219325 IB |
255 | bind=['machine_state', 'custom_color']) |
256 | ||
257 | def __getattr__(self, name): | |
258 | if hasattr(self.machine, name): | |
259 | return getattr(self.machine, name) | |
260 | else: | |
261 | raise AttributeError | |
262 | ||
4b2d79ca | 263 | def __init__(self, **kwargs): |
be27763f | 264 | self.actions = [] |
b17aed6a | 265 | self.current_action = None |
ba219325 | 266 | self.machine = KeyMachine(self) |
b17aed6a | 267 | |
e55b29bb | 268 | super(Key, self).__init__(**kwargs) |
be27763f | 269 | |
e55b29bb | 270 | # Kivy events |
a9324e30 IB |
271 | @mainthread |
272 | def update_state(self, value): | |
273 | self.machine_state = value | |
274 | ||
4b2d79ca | 275 | def on_key_sym(self, key, key_sym): |
e55b29bb IB |
276 | if key_sym != "": |
277 | self.configure() | |
278 | ||
279 | def on_press(self): | |
280 | self.list_actions() | |
4b2d79ca | 281 | |
e55b29bb IB |
282 | # This one cannot be in the Machine state since it would be queued to run |
283 | # *after* the loop is ended... | |
284 | def interrupt(self): | |
285 | self.current_action.interrupt() | |
286 | ||
e55b29bb | 287 | # Setters |
b86db9f1 | 288 | def set_description(self, description): |
4b2d79ca IB |
289 | if description[0] is not None: |
290 | self.description_title = str(description[0]) | |
ab47d2a1 | 291 | self.description = [] |
2e404903 | 292 | for desc in description[1 :]: |
d479af33 IB |
293 | if desc is None: |
294 | self.description.append("") | |
295 | else: | |
4b2d79ca | 296 | self.description.append(str(desc).replace(" ", " ")) |
b86db9f1 | 297 | |
b3e624bb IB |
298 | def unset_description(self): |
299 | self.description_title = "" | |
300 | self.description = [] | |
301 | ||
b86db9f1 | 302 | def set_color(self, color): |
4b2d79ca | 303 | color = [x / 255 for x in color] |
4b2d79ca | 304 | self.custom_color = color |
be27763f | 305 | |
b3e624bb IB |
306 | def unset_color(self): |
307 | self.custom_color = [0, 1, 0] | |
308 | ||
814c30c6 IB |
309 | # Helpers |
310 | @property | |
311 | def repeat_delay(self): | |
7df12958 IB |
312 | if hasattr(self, 'config') and\ |
313 | 'repeat_delay' in self.config['properties']: | |
314 | return self.config['properties']['repeat_delay'] | |
814c30c6 IB |
315 | else: |
316 | return 0 | |
317 | ||
e55b29bb | 318 | # Actions handling |
be27763f IB |
319 | def add_action(self, action_name, **arguments): |
320 | self.actions.append(Action(action_name, self, **arguments)) | |
321 | ||
b17aed6a IB |
322 | def list_actions(self, last_action_finished=False): |
323 | not_running = (not self.is_loaded_running()) | |
324 | current_action_seen = False | |
325 | action_descriptions = [] | |
326 | for action in self.actions: | |
327 | if not_running: | |
328 | state = "inactive" | |
329 | elif last_action_finished: | |
330 | state = "done" | |
331 | elif current_action_seen: | |
332 | state = "pending" | |
333 | elif action == self.current_action: | |
334 | current_action_seen = True | |
335 | state = "current" | |
336 | else: | |
337 | state = "done" | |
338 | action_descriptions.append([action.description(), state]) | |
339 | self.parent.parent.ids['ActionList'].update_list( | |
340 | self, | |
341 | action_descriptions) |