]>
Commit | Line | Data |
---|---|---|
4b2d79ca | 1 | from kivy.uix.relativelayout import RelativeLayout |
30d8796f | 2 | from kivy.properties import NumericProperty, ListProperty |
4b2d79ca | 3 | from kivy.core.window import Window |
30d8796f | 4 | from kivy.clock import Clock |
4b2d79ca | 5 | |
be27763f | 6 | import threading |
4b2d79ca | 7 | import yaml |
b68b4e8f | 8 | import sys |
4b2d79ca | 9 | |
4b2d79ca | 10 | from .music_file import * |
9b9dd12a | 11 | from . import yml_file |
4b2d79ca IB |
12 | |
13 | class Mapping(RelativeLayout): | |
14 | expected_keys = NumericProperty(0) | |
30d8796f | 15 | ready_color = ListProperty([1, 165/255, 0, 1]) |
4b2d79ca IB |
16 | |
17 | def __init__(self, **kwargs): | |
29597680 | 18 | self.key_config, self.open_files = self.parse_config() |
4b2d79ca IB |
19 | super(Mapping, self).__init__(**kwargs) |
20 | self._keyboard = Window.request_keyboard(self._keyboard_closed, self) | |
21 | self._keyboard.bind(on_key_down=self._on_keyboard_down) | |
be27763f | 22 | self.running = [] |
30d8796f | 23 | Clock.schedule_interval(self.not_all_keys_ready, 1) |
be27763f | 24 | |
4b2d79ca | 25 | |
4b2d79ca IB |
26 | def _keyboard_closed(self): |
27 | self._keyboard.unbind(on_key_down=self._on_keyboard_down) | |
28 | self._keyboard = None | |
29 | ||
30 | def _on_keyboard_down(self, keyboard, keycode, text, modifiers): | |
31 | key = self.find_by_key_code(keycode) | |
b68b4e8f | 32 | if len(modifiers) == 0 and key is not None: |
4b2d79ca | 33 | threading.Thread(name = "MSKeyAction", target=key.do_actions).start() |
b68b4e8f IB |
34 | elif 'ctrl' in modifiers and (keycode[0] == 113 or keycode[0] == '99'): |
35 | for thread in threading.enumerate(): | |
36 | if thread.getName()[0:2] != "MS": | |
37 | continue | |
38 | thread.join() | |
39 | ||
b68b4e8f | 40 | sys.exit() |
4b2d79ca IB |
41 | return True |
42 | ||
43 | def find_by_key_code(self, key_code): | |
44 | if "Key_" + str(key_code[0]) in self.ids: | |
45 | return self.ids["Key_" + str(key_code[0])] | |
be27763f IB |
46 | return None |
47 | ||
48 | def find_by_unicode(self, key_sym): | |
4b2d79ca IB |
49 | for key in self.children: |
50 | if not type(key).__name__ == "Key": | |
51 | continue | |
52 | print(key.key_sym, key_sym) | |
53 | if key.key_sym == key_sym: | |
54 | print("found") | |
55 | return key | |
be27763f IB |
56 | return None |
57 | ||
30d8796f IB |
58 | def not_all_keys_ready(self, dt): |
59 | for key in self.children: | |
60 | if not type(key).__name__ == "Key": | |
61 | continue | |
62 | if not key.is_key_ready: | |
63 | return True | |
64 | self.ready_color = [0, 1, 0, 1] | |
65 | return False | |
66 | ||
be27763f | 67 | def stop_all_running(self): |
0deb82a5 | 68 | running = self.running |
be27763f | 69 | self.running = [] |
0deb82a5 IB |
70 | for (key, start_time) in running: |
71 | key.interrupt_action() | |
be27763f IB |
72 | |
73 | def start_running(self, key, start_time): | |
74 | self.running.append((key, start_time)) | |
75 | ||
76 | def keep_running(self, key, start_time): | |
77 | return (key, start_time) in self.running | |
78 | ||
79 | def finished_running(self, key, start_time): | |
80 | if (key, start_time) in self.running: | |
81 | self.running.remove((key, start_time)) | |
82 | ||
4b2d79ca | 83 | def parse_config(self): |
9b9dd12a | 84 | stream = open(yml_file(), "r") |
4b2d79ca IB |
85 | config = yaml.load(stream) |
86 | stream.close() | |
87 | ||
88 | aliases = config['aliases'] | |
89 | seen_files = {} | |
90 | ||
4b2d79ca IB |
91 | key_properties = {} |
92 | ||
93 | for key in config['key_properties']: | |
94 | if key not in key_properties: | |
e5edd8b9 IB |
95 | key_prop = config['key_properties'][key] |
96 | if 'include' in key_prop: | |
97 | included = key_prop['include'] | |
98 | del(key_prop['include']) | |
99 | ||
100 | if isinstance(included, str): | |
101 | key_prop.update(aliases[included], **key_prop) | |
102 | else: | |
103 | for included_ in included: | |
104 | key_prop.update(aliases[included_], **key_prop) | |
105 | ||
4b2d79ca IB |
106 | key_properties[key] = { |
107 | "actions": [], | |
e5edd8b9 | 108 | "properties": key_prop, |
4b2d79ca IB |
109 | "files": [] |
110 | } | |
111 | ||
112 | for mapped_key in config['keys']: | |
113 | if mapped_key not in key_properties: | |
114 | key_properties[mapped_key] = { | |
115 | "actions": [], | |
116 | "properties": {}, | |
117 | "files": [] | |
118 | } | |
119 | for action in config['keys'][mapped_key]: | |
120 | action_name = list(action)[0] | |
121 | action_args = {} | |
122 | if action[action_name] is None: | |
123 | action[action_name] = [] | |
124 | ||
125 | if 'include' in action[action_name]: | |
126 | included = action[action_name]['include'] | |
127 | del(action[action_name]['include']) | |
128 | ||
129 | if isinstance(included, str): | |
130 | action[action_name].update(aliases[included], **action[action_name]) | |
131 | else: | |
132 | for included_ in included: | |
133 | action[action_name].update(aliases[included_], **action[action_name]) | |
134 | ||
135 | for argument in action[action_name]: | |
136 | if argument == 'file': | |
137 | filename = action[action_name]['file'] | |
138 | if filename not in seen_files: | |
139 | if filename in config['music_properties']: | |
140 | seen_files[filename] = MusicFile( | |
141 | filename, | |
4b2d79ca IB |
142 | **config['music_properties'][filename]) |
143 | else: | |
144 | seen_files[filename] = MusicFile( | |
29597680 | 145 | filename) |
4b2d79ca IB |
146 | |
147 | if filename not in key_properties[mapped_key]['files']: | |
148 | key_properties[mapped_key]['files'].append(seen_files[filename]) | |
149 | ||
150 | action_args['music'] = seen_files[filename] | |
151 | ||
152 | else: | |
153 | action_args[argument] = action[action_name][argument] | |
154 | ||
155 | key_properties[mapped_key]['actions'].append([action_name, action_args]) | |
156 | ||
29597680 | 157 | return (key_properties, seen_files) |
4b2d79ca IB |
158 | |
159 |