]>
Commit | Line | Data |
---|---|---|
1 | from kivy.uix.relativelayout import RelativeLayout | |
2 | from kivy.properties import NumericProperty, ListProperty | |
3 | from kivy.core.window import Window | |
4 | from kivy.clock import Clock | |
5 | ||
6 | import threading | |
7 | import yaml | |
8 | import sys | |
9 | ||
10 | from .music_file import * | |
11 | from .mixer import Mixer | |
12 | from . import Config, gain, error_print | |
13 | from .action import Action | |
14 | ||
15 | class Mapping(RelativeLayout): | |
16 | expected_keys = NumericProperty(0) | |
17 | master_volume = NumericProperty(100) | |
18 | ready_color = ListProperty([1, 165/255, 0, 1]) | |
19 | ||
20 | def __init__(self, **kwargs): | |
21 | if Config.builtin_mixing: | |
22 | self.mixer = Mixer() | |
23 | else: | |
24 | self.mixer = None | |
25 | ||
26 | try: | |
27 | self.key_config, self.open_files = self.parse_config() | |
28 | except Exception as e: | |
29 | error_print("Error while loading configuration: {}".format(e)) | |
30 | sys.exit() | |
31 | ||
32 | super(Mapping, self).__init__(**kwargs) | |
33 | self._keyboard = Window.request_keyboard(self._keyboard_closed, self) | |
34 | self._keyboard.bind(on_key_down=self._on_keyboard_down) | |
35 | self.running = [] | |
36 | self.wait_ids = {} | |
37 | Clock.schedule_interval(self.not_all_keys_ready, 1) | |
38 | ||
39 | @property | |
40 | def master_gain(self): | |
41 | return gain(self.master_volume) | |
42 | ||
43 | def set_master_volume(self, value, delta=False, fade=0): | |
44 | [db_gain, self.master_volume] = gain( | |
45 | value + int(delta) * self.master_volume, | |
46 | self.master_volume) | |
47 | ||
48 | for music in self.open_files.values(): | |
49 | music.set_gain_with_effect(db_gain, fade=fade) | |
50 | ||
51 | def add_wait_id(self, wait_id, action_or_wait): | |
52 | self.wait_ids[wait_id] = action_or_wait | |
53 | ||
54 | def interrupt_wait(self, wait_id): | |
55 | if wait_id in self.wait_ids: | |
56 | action_or_wait = self.wait_ids[wait_id] | |
57 | del(self.wait_ids[wait_id]) | |
58 | if isinstance(action_or_wait, Action): | |
59 | action_or_wait.interrupt() | |
60 | else: | |
61 | action_or_wait.set() | |
62 | ||
63 | def _keyboard_closed(self): | |
64 | self._keyboard.unbind(on_key_down=self._on_keyboard_down) | |
65 | self._keyboard = None | |
66 | ||
67 | def _on_keyboard_down(self, keyboard, keycode, text, modifiers): | |
68 | key = self.find_by_key_code(keycode) | |
69 | if len(modifiers) == 0 and key is not None: | |
70 | threading.Thread(name="MSKeyAction", target=key.do_actions).start() | |
71 | elif 'ctrl' in modifiers and (keycode[0] == 113 or keycode[0] == '99'): | |
72 | for thread in threading.enumerate(): | |
73 | if thread.getName()[0:2] != "MS": | |
74 | continue | |
75 | thread.join() | |
76 | ||
77 | sys.exit() | |
78 | return True | |
79 | ||
80 | def find_by_key_code(self, key_code): | |
81 | if "Key_" + str(key_code[0]) in self.ids: | |
82 | return self.ids["Key_" + str(key_code[0])] | |
83 | return None | |
84 | ||
85 | def not_all_keys_ready(self, dt): | |
86 | for key in self.children: | |
87 | if not type(key).__name__ == "Key": | |
88 | continue | |
89 | if not key.is_key_ready: | |
90 | return True | |
91 | self.ready_color = [0, 1, 0, 1] | |
92 | return False | |
93 | ||
94 | def stop_all_running(self): | |
95 | running = self.running | |
96 | self.running = [] | |
97 | for (key, start_time) in running: | |
98 | key.interrupt_action() | |
99 | ||
100 | def start_running(self, key, start_time): | |
101 | self.running.append((key, start_time)) | |
102 | ||
103 | def keep_running(self, key, start_time): | |
104 | return (key, start_time) in self.running | |
105 | ||
106 | def finished_running(self, key, start_time): | |
107 | if (key, start_time) in self.running: | |
108 | self.running.remove((key, start_time)) | |
109 | ||
110 | def parse_config(self): | |
111 | stream = open(Config.yml_file, "r") | |
112 | try: | |
113 | config = yaml.load(stream) | |
114 | except Exception as e: | |
115 | error_print("Error while loading config file: {}".format(e)) | |
116 | sys.exit() | |
117 | stream.close() | |
118 | ||
119 | aliases = config['aliases'] | |
120 | seen_files = {} | |
121 | ||
122 | key_properties = {} | |
123 | ||
124 | for key in config['key_properties']: | |
125 | if key not in key_properties: | |
126 | key_prop = config['key_properties'][key] | |
127 | if 'include' in key_prop: | |
128 | included = key_prop['include'] | |
129 | del(key_prop['include']) | |
130 | ||
131 | if isinstance(included, str): | |
132 | key_prop.update(aliases[included], **key_prop) | |
133 | else: | |
134 | for included_ in included: | |
135 | key_prop.update(aliases[included_], **key_prop) | |
136 | ||
137 | key_properties[key] = { | |
138 | "actions": [], | |
139 | "properties": key_prop, | |
140 | "files": [] | |
141 | } | |
142 | ||
143 | for mapped_key in config['keys']: | |
144 | if mapped_key not in key_properties: | |
145 | key_properties[mapped_key] = { | |
146 | "actions": [], | |
147 | "properties": {}, | |
148 | "files": [] | |
149 | } | |
150 | for action in config['keys'][mapped_key]: | |
151 | action_name = list(action)[0] | |
152 | action_args = {} | |
153 | if action[action_name] is None: | |
154 | action[action_name] = [] | |
155 | ||
156 | if 'include' in action[action_name]: | |
157 | included = action[action_name]['include'] | |
158 | del(action[action_name]['include']) | |
159 | ||
160 | if isinstance(included, str): | |
161 | action[action_name].update( | |
162 | aliases[included], | |
163 | **action[action_name]) | |
164 | else: | |
165 | for included_ in included: | |
166 | action[action_name].update( | |
167 | aliases[included_], | |
168 | **action[action_name]) | |
169 | ||
170 | for argument in action[action_name]: | |
171 | if argument == 'file': | |
172 | filename = action[action_name]['file'] | |
173 | if filename not in seen_files: | |
174 | if filename in config['music_properties']: | |
175 | seen_files[filename] = MusicFile( | |
176 | filename, | |
177 | self, | |
178 | **config['music_properties'][filename]) | |
179 | else: | |
180 | seen_files[filename] = MusicFile( | |
181 | self, | |
182 | filename) | |
183 | ||
184 | if filename not in key_properties[mapped_key]['files']: | |
185 | key_properties[mapped_key]['files'] \ | |
186 | .append(seen_files[filename]) | |
187 | ||
188 | action_args['music'] = seen_files[filename] | |
189 | ||
190 | else: | |
191 | action_args[argument] = action[action_name][argument] | |
192 | ||
193 | key_properties[mapped_key]['actions'] \ | |
194 | .append([action_name, action_args]) | |
195 | ||
196 | return (key_properties, seen_files) | |
197 | ||
198 |