]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blame - music_sampler/key.py
Use @mainthread decorator where necessary
[perso/Immae/Projets/Python/MusicSampler.git] / music_sampler / key.py
CommitLineData
4b2d79ca 1from kivy.uix.widget import Widget
2e404903
IB
2from kivy.properties import AliasProperty, BooleanProperty, \
3 ListProperty, StringProperty
4b2d79ca
IB
4from kivy.uix.behaviors import ButtonBehavior
5
e55b29bb 6from .action import Action
6ebe6247 7from .helpers import debug_print
be27763f 8import time
34382290 9import threading
e55b29bb 10from 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
14from kivy.clock import mainthread
15
ba219325 16class KeyMachine(Widget):
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',
e55b29bb 104 'dest': 'loaded'
34382290 105 },
e55b29bb
IB
106 ]
107
ba219325
IB
108 state = StringProperty("")
109
110 def __init__(self, key, **kwargs):
111 self.key = key
112
113 Machine(model=self, states=self.STATES,
114 transitions=self.TRANSITIONS, initial='initial',
115 ignore_invalid_triggers=True, queued=True)
116 super(KeyMachine, self).__init__(**kwargs)
117
118 # Machine states / events
119 def is_loaded_or_failed(self):
120 return self.is_loaded(allow_substates=True) or self.is_failed()
121
122 def is_loaded_inactive(self):
123 return self.is_loaded_no_config() or self.is_loaded_no_actions()
124
a9324e30 125 @mainthread
ba219325 126 def on_enter_configuring(self):
045a1b63
IB
127 self.destroy_actions()
128 self.key.unset_description()
129 self.key.unset_color()
130
ba219325
IB
131 if self.key.key_sym in self.key.parent.key_config:
132 self.key.config = self.key.parent.key_config[self.key.key_sym]
133
ba219325
IB
134 for key_action in self.key.config['actions']:
135 self.key.add_action(key_action[0], **key_action[1])
136
137 if 'description' in self.key.config['properties']:
138 self.key.set_description(self.key.config['properties']['description'])
ba219325
IB
139 if 'color' in self.key.config['properties']:
140 self.key.set_color(self.key.config['properties']['color'])
ba219325
IB
141 self.success()
142 else:
143 self.no_config()
144
145 def on_enter_loading(self):
146 if len(self.key.actions) > 0:
147 for action in self.key.actions:
148 action.load()
149 else:
150 self.no_actions()
151
045a1b63
IB
152 def destroy_actions(self):
153 for action in self.key.actions:
154 action.destroy()
155 self.key.actions = []
156
ba219325
IB
157 def run_actions(self, modifiers):
158 self.key.parent.parent.ids['KeyList'].append(self.key.key_sym)
159 debug_print("running actions for {}".format(self.key.key_sym))
160 start_time = time.time()
06ed113d 161 self.key.parent.start_running(self.key, start_time)
ba219325 162 for self.key.current_action in self.key.actions:
06ed113d 163 if self.key.parent.keep_running(self.key, start_time):
ba219325
IB
164 self.key.list_actions()
165 self.key.current_action.run(start_time)
166 self.key.list_actions(last_action_finished=True)
167
06ed113d 168 self.key.parent.finished_running(self.key, start_time)
ba219325
IB
169
170 def on_enter_loaded_protecting_repeat(self, modifiers):
814c30c6 171 if self.key.repeat_delay > 0:
ba219325 172 self.key.protecting_repeat_timer = threading.Timer(
814c30c6 173 self.key.repeat_delay,
ba219325
IB
174 self.key.repeat_protection_finished)
175 self.key.protecting_repeat_timer.start()
176 else:
177 self.key.repeat_protection_finished()
178
179 # Callbacks
a9324e30 180 @mainthread
ba219325
IB
181 def key_loaded_callback(self):
182 self.key.parent.key_loaded_callback()
183
184
185class Key(ButtonBehavior, Widget):
186
4b2d79ca 187 key_sym = StringProperty(None)
e55b29bb 188 custom_color = ListProperty([0, 1, 0])
4b2d79ca
IB
189 description_title = StringProperty("")
190 description = ListProperty([])
ba219325 191 machine_state = StringProperty("")
4b2d79ca 192
635dea02 193 def get_alias_line_cross_color(self):
ea97edb3
IB
194 if not self.is_failed() and (
195 not self.is_loaded(allow_substates=True)\
196 or self.is_loaded_running()\
197 or self.is_loaded_protecting_repeat()):
635dea02
IB
198 return [120/255, 120/255, 120/255, 1]
199 else:
200 return [0, 0, 0, 0]
201
202 def set_alias_line_cross_color(self):
203 pass
204
205 line_cross_color = AliasProperty(
206 get_alias_line_cross_color,
207 set_alias_line_cross_color,
ba219325 208 bind=['machine_state'])
635dea02 209
1094ab1a
IB
210 def get_alias_line_color(self):
211 if self.is_loaded_running():
212 return [0, 0, 0, 1]
213 else:
214 return [120/255, 120/255, 120/255, 1]
215
216 def set_alias_line_color(self):
217 pass
218
219 line_color = AliasProperty(get_alias_line_color, set_alias_line_color,
ba219325 220 bind=['machine_state'])
1094ab1a 221
e55b29bb
IB
222 def get_alias_color(self):
223 if self.is_loaded_inactive():
4b2d79ca 224 return [1, 1, 1, 1]
34382290
IB
225 elif self.is_loaded_protecting_repeat():
226 return [*self.custom_color, 100/255]
70cfb266
IB
227 elif self.is_loaded_running():
228 return [*self.custom_color, 100/255]
e55b29bb
IB
229 elif self.is_loaded(allow_substates=True):
230 return [*self.custom_color, 1]
231 elif self.is_failed():
232 return [0, 0, 0, 1]
be27763f 233 else:
e55b29bb
IB
234 return [*self.custom_color, 100/255]
235 def set_alias_color(self):
4b2d79ca
IB
236 pass
237
e55b29bb 238 color = AliasProperty(get_alias_color, set_alias_color,
ba219325
IB
239 bind=['machine_state', 'custom_color'])
240
241 def __getattr__(self, name):
242 if hasattr(self.machine, name):
243 return getattr(self.machine, name)
244 else:
245 raise AttributeError
246
247 def machine_state_changed(self, instance, machine_state):
248 self.machine_state = self.machine.state
be27763f 249
4b2d79ca 250 def __init__(self, **kwargs):
be27763f 251 self.actions = []
b17aed6a 252 self.current_action = None
ba219325
IB
253 self.machine = KeyMachine(self)
254 self.machine.bind(state=self.machine_state_changed)
b17aed6a 255
e55b29bb 256 super(Key, self).__init__(**kwargs)
be27763f 257
e55b29bb 258 # Kivy events
a9324e30
IB
259 @mainthread
260 def update_state(self, value):
261 self.machine_state = value
262
4b2d79ca 263 def on_key_sym(self, key, key_sym):
e55b29bb
IB
264 if key_sym != "":
265 self.configure()
266
267 def on_press(self):
268 self.list_actions()
4b2d79ca 269
e55b29bb
IB
270 # This one cannot be in the Machine state since it would be queued to run
271 # *after* the loop is ended...
272 def interrupt(self):
273 self.current_action.interrupt()
274
275 # Callbacks
276 def callback_action_ready(self, action, success):
277 if not success:
278 self.fail()
279 elif all(action.is_loaded_or_failed() for action in self.actions):
280 self.success()
281
282 # Setters
b86db9f1 283 def set_description(self, description):
4b2d79ca
IB
284 if description[0] is not None:
285 self.description_title = str(description[0])
ab47d2a1 286 self.description = []
2e404903 287 for desc in description[1 :]:
d479af33
IB
288 if desc is None:
289 self.description.append("")
290 else:
4b2d79ca 291 self.description.append(str(desc).replace(" ", " "))
b86db9f1 292
b3e624bb
IB
293 def unset_description(self):
294 self.description_title = ""
295 self.description = []
296
b86db9f1 297 def set_color(self, color):
4b2d79ca 298 color = [x / 255 for x in color]
4b2d79ca 299 self.custom_color = color
be27763f 300
b3e624bb
IB
301 def unset_color(self):
302 self.custom_color = [0, 1, 0]
303
814c30c6
IB
304 # Helpers
305 @property
306 def repeat_delay(self):
7df12958
IB
307 if hasattr(self, 'config') and\
308 'repeat_delay' in self.config['properties']:
309 return self.config['properties']['repeat_delay']
814c30c6
IB
310 else:
311 return 0
312
e55b29bb 313 # Actions handling
be27763f
IB
314 def add_action(self, action_name, **arguments):
315 self.actions.append(Action(action_name, self, **arguments))
316
b17aed6a
IB
317 def list_actions(self, last_action_finished=False):
318 not_running = (not self.is_loaded_running())
319 current_action_seen = False
320 action_descriptions = []
321 for action in self.actions:
322 if not_running:
323 state = "inactive"
324 elif last_action_finished:
325 state = "done"
326 elif current_action_seen:
327 state = "pending"
328 elif action == self.current_action:
329 current_action_seen = True
330 state = "current"
331 else:
332 state = "done"
333 action_descriptions.append([action.description(), state])
334 self.parent.parent.ids['ActionList'].update_list(
335 self,
336 action_descriptions)