aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2016-06-19 01:13:41 +0200
committerIsmaël Bouya <ismael.bouya@normalesup.org>2016-06-19 01:13:41 +0200
commitba9ea93a0f52178d24a606fddc2acc5dc85b7ff2 (patch)
tree0a2b43561b84eefed07ecb83de1ecf4fad428532
parente5e6409426e3eb1918318973372568081e6d9dd6 (diff)
downloadMusicSampler-ba9ea93a0f52178d24a606fddc2acc5dc85b7ff2.tar.gz
MusicSampler-ba9ea93a0f52178d24a606fddc2acc5dc85b7ff2.tar.zst
MusicSampler-ba9ea93a0f52178d24a606fddc2acc5dc85b7ff2.zip
enhancing locks
-rw-r--r--helpers/__init__.py8
-rw-r--r--helpers/action.py5
-rw-r--r--helpers/key.py31
-rw-r--r--helpers/lock.py15
-rw-r--r--music_sampler.py20
5 files changed, 58 insertions, 21 deletions
diff --git a/helpers/__init__.py b/helpers/__init__.py
index 6935342..b1723ee 100644
--- a/helpers/__init__.py
+++ b/helpers/__init__.py
@@ -1,12 +1,10 @@
1# -*- coding: utf-8 -*- 1# -*- coding: utf-8 -*-
2import threading
3from .music_file import * 2from .music_file import *
4from .mapping import * 3from .mapping import *
5 4from .lock import *
6draw_lock = threading.RLock() 5import yaml
7 6
8def parse_config(mapping): 7def parse_config(mapping):
9 import yaml
10 stream = open("config.yml", "r") 8 stream = open("config.yml", "r")
11 config = yaml.load(stream) 9 config = yaml.load(stream)
12 stream.close() 10 stream.close()
@@ -14,7 +12,7 @@ def parse_config(mapping):
14 aliases = config['aliases'] 12 aliases = config['aliases']
15 seen_files = {} 13 seen_files = {}
16 14
17 file_lock = threading.RLock() 15 file_lock = Lock("file")
18 16
19 for mapped_key in config['keys']: 17 for mapped_key in config['keys']:
20 key = mapping.find_by_unicode(mapped_key) 18 key = mapping.find_by_unicode(mapped_key)
diff --git a/helpers/action.py b/helpers/action.py
index 8a69fae..97873a0 100644
--- a/helpers/action.py
+++ b/helpers/action.py
@@ -28,9 +28,12 @@ class Action:
28 return True 28 return True
29 29
30 def run(self): 30 def run(self):
31 print(getattr(self, self.action + "_print")(**self.arguments)) 31 print(self.description())
32 return getattr(self, self.action)(**self.arguments) 32 return getattr(self, self.action)(**self.arguments)
33 33
34 def description(self):
35 return getattr(self, self.action + "_print")(**self.arguments)
36
34 def command(self, command = "", **kwargs): 37 def command(self, command = "", **kwargs):
35 pass 38 pass
36 39
diff --git a/helpers/key.py b/helpers/key.py
index 2e4a313..6997e70 100644
--- a/helpers/key.py
+++ b/helpers/key.py
@@ -112,9 +112,32 @@ class Key:
112 112
113 self.mapping.finished_running(self, start_time) 113 self.mapping.finished_running(self, start_time)
114 114
115 def list_actions(self, surface): 115 def list_actions(self, screen):
116 # FIXME: Todo 116 action_descriptions = [action.description() for action in self.actions]
117 print("bouh", self.key_sym) 117 print("actions linked to key {}:".format(self.key_sym))
118 surface.fill((255, 0, 0)) 118 print("\t" + "\n\t".join(action_descriptions))
119 self.draw_lock.acquire()
120 surface = pygame.Surface((800, 250)).convert()
121 surface.fill((250, 250, 250))
122 if getattr(sys, 'frozen', False):
123 police = pygame.font.Font(sys._MEIPASS + "/Ubuntu-Regular.ttf", 14)
124 else:
125 police = pygame.font.Font("Ubuntu-Regular.ttf", 14)
126
127 offset = 0
128 police.set_bold(True)
129 text = police.render("actions linked to key {}:".format(self.key_sym), True, (0,0,0))
130 surface.blit(text, (0, offset))
131 offset += police.get_linesize()
132
133 police.set_bold(False)
134 for description in action_descriptions:
135 text = police.render(description, True, (0,0,0))
136 surface.blit(text, (0, offset))
137 offset += police.get_linesize()
138
139 screen.blit(surface, (10, 330))
140 pygame.display.flip()
141 self.draw_lock.release()
119 142
120 143
diff --git a/helpers/lock.py b/helpers/lock.py
new file mode 100644
index 0000000..dff8b1f
--- /dev/null
+++ b/helpers/lock.py
@@ -0,0 +1,15 @@
1import threading
2
3class Lock:
4 def __init__(self, lock_type):
5 self.type = lock_type
6 self.lock = threading.RLock()
7
8 def acquire(self, *args, **kwargs):
9 #print("acquiring lock for {}".format(self.type))
10 self.lock.acquire(*args, **kwargs)
11
12 def release(self, *args, **kwargs):
13 #print("releasing lock for {}".format(self.type))
14 self.lock.release(*args, **kwargs)
15
diff --git a/music_sampler.py b/music_sampler.py
index 9f14d56..fd03009 100644
--- a/music_sampler.py
+++ b/music_sampler.py
@@ -7,23 +7,19 @@ pygame.mixer.pre_init(frequency = 44100)
7pygame.init() 7pygame.init()
8 8
9size = width, height = 1024, 600 9size = width, height = 1024, 600
10
11helpers.draw_lock.acquire()
12screen = pygame.display.set_mode(size) 10screen = pygame.display.set_mode(size)
13mapping = helpers.Mapping(screen, helpers.draw_lock) 11screen.fill((250, 250, 250))
12
13draw_lock = helpers.Lock("draw")
14 14
15action_surface = pygame.Surface((600, 250)).convert() 15mapping = helpers.Mapping(screen, draw_lock)
16action_surface.fill((0,0,0))
17helpers.parse_config(mapping) 16helpers.parse_config(mapping)
18helpers.draw_lock.release()
19 17
20mapping.draw() 18mapping.draw()
21 19
22helpers.draw_lock.acquire() 20draw_lock.acquire()
23screen.blit(action_surface, (10, 330))
24
25pygame.display.flip() 21pygame.display.flip()
26helpers.draw_lock.release() 22draw_lock.release()
27 23
28contexts = [ 24contexts = [
29 'normal' 25 'normal'
@@ -54,7 +50,9 @@ while 1:
54 elif event.type == pygame.MOUSEBUTTONUP: 50 elif event.type == pygame.MOUSEBUTTONUP:
55 key = mapping.find_by_collidepoint(pygame.mouse.get_pos()) 51 key = mapping.find_by_collidepoint(pygame.mouse.get_pos())
56 if key is not None: 52 if key is not None:
57 key.list_actions(action_surface) 53 threading.Thread(target=key.list_actions, args = [screen]).start()
58 54
55 draw_lock.acquire()
59 pygame.display.flip() 56 pygame.display.flip()
57 draw_lock.release()
60 58