diff options
Diffstat (limited to 'helpers/mapping.py')
-rw-r--r-- | helpers/mapping.py | 281 |
1 files changed, 116 insertions, 165 deletions
diff --git a/helpers/mapping.py b/helpers/mapping.py index 58aaf3d..4a1cd98 100644 --- a/helpers/mapping.py +++ b/helpers/mapping.py | |||
@@ -1,175 +1,51 @@ | |||
1 | from kivy.uix.relativelayout import RelativeLayout | ||
2 | from kivy.properties import NumericProperty | ||
3 | from kivy.core.window import Window | ||
4 | |||
1 | import threading | 5 | import threading |
2 | import pygame | 6 | import pygame |
3 | from .key import * | 7 | import yaml |
4 | 8 | ||
5 | class Mapping: | 9 | from .lock import * |
6 | WIDTH = 903 | 10 | from .music_file import * |
7 | HEIGHT = 298 | 11 | |
8 | SIZE = WIDTH, HEIGHT | 12 | class Mapping(RelativeLayout): |
9 | 13 | expected_keys = NumericProperty(0) | |
10 | ROW_POSITIONS = { | 14 | |
11 | 'first': 0, | 15 | def __init__(self, **kwargs): |
12 | 'second': 50, | 16 | self.key_config, self.channel_number, self.open_files = self.parse_config() |
13 | 'third': 100, | 17 | super(Mapping, self).__init__(**kwargs) |
14 | 'fourth': 150, | 18 | self._keyboard = Window.request_keyboard(self._keyboard_closed, self) |
15 | 'fifth': 200, | 19 | self._keyboard.bind(on_key_down=self._on_keyboard_down) |
16 | 'sixth': 250, | ||
17 | } | ||
18 | |||
19 | KEYS = [ | ||
20 | (pygame.K_ESCAPE, 'ESC', 'first', 0, {}), | ||
21 | |||
22 | (pygame.K_F1, 'F1', 'first', 100, {}), | ||
23 | (pygame.K_F2, 'F2', 'first', 150, {}), | ||
24 | (pygame.K_F3, 'F3', 'first', 200, {}), | ||
25 | (pygame.K_F4, 'F4', 'first', 250, {}), | ||
26 | |||
27 | (pygame.K_F5, 'F5', 'first', 325, {}), | ||
28 | (pygame.K_F6, 'F6', 'first', 375, {}), | ||
29 | (pygame.K_F7, 'F7', 'first', 425, {}), | ||
30 | (pygame.K_F8, 'F8', 'first', 475, {}), | ||
31 | |||
32 | (pygame.K_F9, 'F9', 'first', 550, {}), | ||
33 | (pygame.K_F10, 'F10', 'first', 600, {}), | ||
34 | (pygame.K_F11, 'F11', 'first', 650, {}), | ||
35 | (pygame.K_F12, 'F12', 'first', 700, {}), | ||
36 | |||
37 | |||
38 | (178, '²', 'second', 0, {}), | ||
39 | (pygame.K_AMPERSAND, '&', 'second', 50, {}), | ||
40 | (233, 'é', 'second', 100, {}), | ||
41 | (pygame.K_QUOTEDBL, '"', 'second', 150, {}), | ||
42 | (pygame.K_QUOTE, "'", 'second', 200, {}), | ||
43 | (pygame.K_LEFTPAREN, '(', 'second', 250, {}), | ||
44 | (pygame.K_MINUS, '-', 'second', 300, {}), | ||
45 | (232, 'è', 'second', 350, {}), | ||
46 | (pygame.K_UNDERSCORE, '_', 'second', 400, {}), | ||
47 | (231, 'ç', 'second', 450, {}), | ||
48 | (224, 'à', 'second', 500, {}), | ||
49 | (pygame.K_RIGHTPAREN, ')', 'second', 550, {}), | ||
50 | (pygame.K_EQUALS, '=', 'second', 600, {}), | ||
51 | |||
52 | (pygame.K_BACKSPACE, '<-', 'second', 650, { 'width': 98 }), | ||
53 | |||
54 | |||
55 | (pygame.K_TAB, 'tab', 'third', 0, { 'width' : 73 }), | ||
56 | (pygame.K_a, 'a', 'third', 75, {}), | ||
57 | (pygame.K_z, 'z', 'third', 125, {}), | ||
58 | (pygame.K_e, 'e', 'third', 175, {}), | ||
59 | (pygame.K_r, 'r', 'third', 225, {}), | ||
60 | (pygame.K_t, 't', 'third', 275, {}), | ||
61 | (pygame.K_y, 'y', 'third', 325, {}), | ||
62 | (pygame.K_u, 'u', 'third', 375, {}), | ||
63 | (pygame.K_i, 'i', 'third', 425, {}), | ||
64 | (pygame.K_o, 'o', 'third', 475, {}), | ||
65 | (pygame.K_p, 'p', 'third', 525, {}), | ||
66 | (pygame.K_CARET, '^', 'third', 575, {}), | ||
67 | (pygame.K_DOLLAR, '$', 'third', 625, {}), | ||
68 | |||
69 | (pygame.K_RETURN, 'Enter', 'third', 692, { 'width': 56, 'height': 98 }), | ||
70 | |||
71 | (pygame.K_CAPSLOCK, 'CAPS', 'fourth', 0, { 'width': 88, 'disabled': True }), | ||
72 | |||
73 | (pygame.K_q, 'q', 'fourth', 90, {}), | ||
74 | (pygame.K_s, 's', 'fourth', 140, {}), | ||
75 | (pygame.K_d, 'd', 'fourth', 190, {}), | ||
76 | (pygame.K_f, 'f', 'fourth', 240, {}), | ||
77 | (pygame.K_g, 'g', 'fourth', 290, {}), | ||
78 | (pygame.K_h, 'h', 'fourth', 340, {}), | ||
79 | (pygame.K_j, 'j', 'fourth', 390, {}), | ||
80 | (pygame.K_k, 'k', 'fourth', 440, {}), | ||
81 | (pygame.K_l, 'l', 'fourth', 490, {}), | ||
82 | (pygame.K_m, 'm', 'fourth', 540, {}), | ||
83 | (249, 'ù', 'fourth', 590, {}), | ||
84 | (pygame.K_ASTERISK, '*', 'fourth', 640, {}), | ||
85 | |||
86 | |||
87 | (pygame.K_LSHIFT, 'LShift', 'fifth', 0, { 'width': 63, 'disabled': True }), | ||
88 | |||
89 | (pygame.K_LESS, '<', 'fifth', 65, {}), | ||
90 | (pygame.K_w, 'w', 'fifth', 115, {}), | ||
91 | (pygame.K_x, 'x', 'fifth', 165, {}), | ||
92 | (pygame.K_c, 'c', 'fifth', 215, {}), | ||
93 | (pygame.K_v, 'v', 'fifth', 265, {}), | ||
94 | (pygame.K_b, 'b', 'fifth', 315, {}), | ||
95 | (pygame.K_n, 'n', 'fifth', 365, {}), | ||
96 | (pygame.K_COMMA, ',', 'fifth', 415, {}), | ||
97 | (pygame.K_SEMICOLON, ';', 'fifth', 465, {}), | ||
98 | (pygame.K_COLON, ':', 'fifth', 515, {}), | ||
99 | (pygame.K_EXCLAIM, '!', 'fifth', 565, {}), | ||
100 | |||
101 | (pygame.K_RSHIFT, 'RShift', 'fifth', 615, { 'width': 133, 'disabled': True }), | ||
102 | |||
103 | (pygame.K_LCTRL, 'LCtrl', 'sixth', 0, { 'width': 63, 'disabled': True }), | ||
104 | (pygame.K_LSUPER, 'LSuper', 'sixth', 115, { 'disabled': True }), | ||
105 | (pygame.K_LALT, 'LAlt', 'sixth', 165, { 'disabled': True }), | ||
106 | (pygame.K_SPACE, 'Espace', 'sixth', 215, { 'width': 248 }), | ||
107 | (pygame.K_MODE, 'AltGr', 'sixth', 465, { 'disabled': True }), | ||
108 | (314, 'Compose', 'sixth', 515, { 'disabled': True }), | ||
109 | (pygame.K_RCTRL, 'RCtrl', 'sixth', 565, { 'width': 63, 'disabled': True }), | ||
110 | |||
111 | |||
112 | (pygame.K_INSERT, 'ins', 'second', 755, {}), | ||
113 | (pygame.K_HOME, 'home', 'second', 805, {}), | ||
114 | (pygame.K_PAGEUP, 'pg_u', 'second', 855, {}), | ||
115 | (pygame.K_DELETE, 'del', 'third', 755, {}), | ||
116 | (pygame.K_END, 'end', 'third', 805, {}), | ||
117 | (pygame.K_PAGEDOWN, 'pg_d', 'third', 855, {}), | ||
118 | |||
119 | |||
120 | (pygame.K_UP, 'up', 'fifth', 805, {}), | ||
121 | (pygame.K_DOWN, 'down', 'sixth', 805, {}), | ||
122 | (pygame.K_LEFT, 'left', 'sixth', 755, {}), | ||
123 | (pygame.K_RIGHT, 'right', 'sixth', 855, {}), | ||
124 | ] | ||
125 | |||
126 | def __init__(self, screen, draw_lock): | ||
127 | self.draw_lock = draw_lock | ||
128 | self.screen = screen | ||
129 | self.background = pygame.Surface(self.SIZE).convert() | ||
130 | self.background.fill((250, 250, 250)) | ||
131 | self.keys = {} | ||
132 | self.running = [] | 20 | self.running = [] |
133 | for key in self.KEYS: | ||
134 | if key[2] in self.ROW_POSITIONS: | ||
135 | position = self.ROW_POSITIONS[key[2]] | ||
136 | else: | ||
137 | position = key[2] | ||
138 | self.keys[key[0]] = Key(self, | ||
139 | self.draw_lock, | ||
140 | key[0], key[1], position, key[3], | ||
141 | **key[4]) | ||
142 | |||
143 | def draw(self): | ||
144 | for key_name in self.keys: | ||
145 | key = self.keys[key_name] | ||
146 | should_redraw_key = key.draw(self.background) | ||
147 | |||
148 | if should_redraw_key: | ||
149 | threading.Thread(name = "MSPollRedraw", target = key.poll_redraw, args = [self.background]).start() | ||
150 | self.blit() | ||
151 | |||
152 | def blit(self): | ||
153 | self.draw_lock.acquire() | ||
154 | self.screen.blit(self.background, (5, 5)) | ||
155 | pygame.display.flip() | ||
156 | self.draw_lock.release() | ||
157 | |||
158 | def find_by_key_num(self, key_num): | ||
159 | if key_num in self.keys: | ||
160 | return self.keys[key_num] | ||
161 | return None | ||
162 | 21 | ||
163 | def find_by_collidepoint(self, position): | 22 | |
164 | for key in self.keys: | 23 | pygame.mixer.init(frequency = 44100) |
165 | if self.keys[key].collidepoint(position): | 24 | pygame.mixer.set_num_channels(self.channel_number) |
166 | return self.keys[key] | 25 | |
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) | ||
32 | if key is not None: | ||
33 | threading.Thread(name = "MSKeyAction", target=key.do_actions).start() | ||
34 | return True | ||
35 | |||
36 | def find_by_key_code(self, key_code): | ||
37 | if "Key_" + str(key_code[0]) in self.ids: | ||
38 | return self.ids["Key_" + str(key_code[0])] | ||
167 | return None | 39 | return None |
168 | 40 | ||
169 | def find_by_unicode(self, key_sym): | 41 | def find_by_unicode(self, key_sym): |
170 | for key in self.keys: | 42 | for key in self.children: |
171 | if self.keys[key].key_sym == key_sym: | 43 | if not type(key).__name__ == "Key": |
172 | return self.keys[key] | 44 | continue |
45 | print(key.key_sym, key_sym) | ||
46 | if key.key_sym == key_sym: | ||
47 | print("found") | ||
48 | return key | ||
173 | return None | 49 | return None |
174 | 50 | ||
175 | def stop_all_running(self): | 51 | def stop_all_running(self): |
@@ -185,3 +61,78 @@ class Mapping: | |||
185 | if (key, start_time) in self.running: | 61 | if (key, start_time) in self.running: |
186 | self.running.remove((key, start_time)) | 62 | self.running.remove((key, start_time)) |
187 | 63 | ||
64 | def parse_config(self): | ||
65 | stream = open("config.yml", "r") | ||
66 | config = yaml.load(stream) | ||
67 | stream.close() | ||
68 | |||
69 | aliases = config['aliases'] | ||
70 | seen_files = {} | ||
71 | |||
72 | file_lock = Lock("file") | ||
73 | |||
74 | channel_id = 0 | ||
75 | |||
76 | key_properties = {} | ||
77 | |||
78 | for key in config['key_properties']: | ||
79 | if key not in key_properties: | ||
80 | key_properties[key] = { | ||
81 | "actions": [], | ||
82 | "properties": config['key_properties'][key], | ||
83 | "files": [] | ||
84 | } | ||
85 | |||
86 | for mapped_key in config['keys']: | ||
87 | if mapped_key not in key_properties: | ||
88 | key_properties[mapped_key] = { | ||
89 | "actions": [], | ||
90 | "properties": {}, | ||
91 | "files": [] | ||
92 | } | ||
93 | for action in config['keys'][mapped_key]: | ||
94 | action_name = list(action)[0] | ||
95 | action_args = {} | ||
96 | if action[action_name] is None: | ||
97 | action[action_name] = [] | ||
98 | |||
99 | if 'include' in action[action_name]: | ||
100 | included = action[action_name]['include'] | ||
101 | del(action[action_name]['include']) | ||
102 | |||
103 | if isinstance(included, str): | ||
104 | action[action_name].update(aliases[included], **action[action_name]) | ||
105 | else: | ||
106 | for included_ in included: | ||
107 | action[action_name].update(aliases[included_], **action[action_name]) | ||
108 | |||
109 | for argument in action[action_name]: | ||
110 | if argument == 'file': | ||
111 | filename = action[action_name]['file'] | ||
112 | if filename not in seen_files: | ||
113 | if filename in config['music_properties']: | ||
114 | seen_files[filename] = MusicFile( | ||
115 | filename, | ||
116 | file_lock, | ||
117 | channel_id, | ||
118 | **config['music_properties'][filename]) | ||
119 | else: | ||
120 | seen_files[filename] = MusicFile( | ||
121 | filename, | ||
122 | file_lock, | ||
123 | channel_id) | ||
124 | channel_id = channel_id + 1 | ||
125 | |||
126 | if filename not in key_properties[mapped_key]['files']: | ||
127 | key_properties[mapped_key]['files'].append(seen_files[filename]) | ||
128 | |||
129 | action_args['music'] = seen_files[filename] | ||
130 | |||
131 | else: | ||
132 | action_args[argument] = action[action_name][argument] | ||
133 | |||
134 | key_properties[mapped_key]['actions'].append([action_name, action_args]) | ||
135 | |||
136 | return (key_properties, channel_id + 1, seen_files) | ||
137 | |||
138 | |||