-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
1526 lines (1214 loc) · 51.6 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from PIL import Image
import wx
import wx.lib.delayedresult
import requests
import time
import importlib.resources
import datetime
import http.client
import urllib
import semver
import textwrap
import pyperclip
import traceback
import os
import time
import webbrowser
import subprocess
import platform
import glob
import rtmidi
import math
import traceback
import mouse
from rtmidi.midiutil import open_midiport
import threading
import queue
import tomlkit
import copy
import sys
import re
import appdirs
from enum import Enum
from pointercc.core import Window, Box, make_box
from pointercc.version import version
if sys.platform == "win32":
import pointercc.win32 as core_platform
elif sys.platform == "darwin":
import pointercc.darwin as core_platform
else:
raise NotImplemnted(f'Platform {sys.platform} not supported')
app_name = "pointer-cc"
app_author = "smatting"
app_url = "https://github.com/smatting/pointer-cc/"
app_email = "[email protected]"
url_latest = "https://raw.githubusercontent.com/smatting/pointer-cc/main/latest_release.txt"
InternalCommand = Enum('InternalCommand', ['QUIT', 'CHANGE_MIDI_PORT', 'CHANGE_MIDI_CHANNEL', 'UPDATE_WINDOW', 'RELOAD_ALL_CONFIGS'])
class Bijection:
def __init__(self, a_name, b_name, atob_pairs):
self._d_atob = dict(atob_pairs)
self._d_btoa = dict([(b, a) for (a, b) in atob_pairs])
setattr(self, a_name, self._a)
setattr(self, b_name, self._b)
def _a(self, b):
return self._d_btoa.get(b)
def _b(self, a):
return self._d_atob.get(a)
def datadir():
return appdirs.user_data_dir(app_name, app_author)
def userfile(p):
return os.path.join(datadir(), p)
def configfile():
return userfile('config.txt')
def initialize_config():
os.makedirs(datadir(), exist_ok=True)
cf = configfile()
if not os.path.exists(cf):
with open(cf, 'w') as f:
conf = default_config_toml()
f.write(conf.as_string())
ControlType = Enum('ControlType', ['WHEEL', 'DRAG','CLICK'])
class ConfigError(Exception):
def __init__(self, msg):
self.msg = msg
super(ConfigError, self).__init__(self.msg)
def in_context(f, context):
try:
return f()
except ConfigError as ce:
ce.msg = ce.msg + f", {context}"
raise ce
control_type_bij = Bijection('str', 'enum', [('wheel', ControlType.WHEEL), ('drag', ControlType.DRAG), ('click', ControlType.CLICK)])
def squared_dist(p1, p2):
x1, y1 = p1
x2, y2 = p2
return (x1 - x2)**2 + (y1 - y2)**2
class Control:
def __init__(self, type_, i, x, y, speed, m, time_resolution):
self.type_ = type_
self.i = i
self.x = x
self.y = y
self.speed = speed
self.m = m
self.time_resolution = time_resolution
def __eq__(self, other):
return self.i == other.i
def __str__(self):
type_str = control_type_bij.str(self.type_)
return f'{self.i}({type_str})'
@staticmethod
def parse(d, i, default, context):
try:
x_s = expect_value(d, "x")
x = expect_int(x_s, "x")
y_s = expect_value(d, "y")
y = expect_int(y_s, "y")
default_type = control_type_bij.enum(default['type'])
type_= maybe(d.get('type'), control_type_bij.enum, default_type)
m = maybe(d.get('m'), lambda s: expect_float(s, 'm'), 1.0)
if type_ == ControlType.WHEEL:
d = default['wheel']
default_speed = d['speed']
default_time_resolution = d['time_resolution']
else:
d = default['drag']
default_speed = d['speed']
default_time_resolution = 100
time_resolution = maybe(d.get('time_resolution'), lambda s: expect_int(s, 'time_resolution'), default_time_resolution)
speed = maybe(d.get('speed'), lambda s: expect_float(s, 'speed'), default_speed)
return Control(type_, i, x, y, speed, m, time_resolution)
except ConfigError as ce:
ce.msg = ce.msg + f", {context}"
raise ce
def maybe(mv, f, default):
if mv is None:
return default
else:
return f(mv)
class Instrument:
def __init__(self, pattern, box, controls):
self.pattern = pattern
self.box = box
self.controls = controls
@staticmethod
def load(path, instrument_context):
try:
controls = []
with open(path, 'r') as f:
d = tomlkit.load(f)
dimensions = expect_value(d, 'dimensions')
width_s = expect_value(dimensions, 'width', 'dimensions')
width = expect_int(width_s, 'dimensions.width')
height_s = expect_value(dimensions, 'height', 'dimensions')
height = expect_int(height_s, 'dimensions.height')
box = Box(0, width, 0, height)
default = expect_value(d, 'default')
type_s = expect_value(default, 'type', 'default')
default_wheel = expect_value(default, 'wheel', 'default')
expect_value(default_wheel, 'speed', 'default.wheel')
expect_value(default_wheel, 'time_resolution', 'default.wheel')
default_drag = expect_value(default, 'drag', 'default')
expect_value(default_drag, 'speed', 'default.drag')
controls_unparsed = expect_value(d, 'controls')
for control_id, v in controls_unparsed.items():
context = f"in control \"{control_id}\""
c = Control.parse(v, control_id, default, context)
controls.append(c)
window = expect_value(d, 'window')
pattern = expect_value(window, 'contains', 'window')
return Instrument(pattern, box, controls)
except tomlkit.exceptions.TOMLKitError as tk:
msg = f'Not a valid TOML file: {tk}'
raise ConfigError(msg + f" in \"{instrument_context}\"")
except ConfigError as ce:
raise ConfigError(ce.msg + f" in \"{instrument_context}\"")
def find_closest_control(self, mx, my):
c_best = None
d_best = math.inf
for c in self.controls:
d = math.pow(c.x - mx, 2.0) + math.pow(c.y - my, 2.0)
if d < d_best:
c_best = c
d_best = d
return c_best
def overlaps(p1, p2):
if p1[0] > p2[0]:
p1, p2 = p2, p1
return p2[0] <= p1[1]
def find_overlapping(spans, p):
for p_span in spans:
if overlaps(p_span, p):
return spans[p_span]
return None
def color_diff(c1, c2):
return max(abs(c1[0] - c2[0]), abs(c1[1] - c2[1]), abs(c1[2] - c2[2]))
def find_markings(im, marker_color, threshold, update_percent):
boxes = []
spans_last = {}
for y in range(0, im.height):
update_percent(int(100*y//im.height))
xmin = None
xmax = None
spans = {}
for x in range(0, im.width):
color = im.getpixel((x, y))
diff = color_diff(marker_color, color)
in_marker = diff <= threshold
if in_marker:
if xmin is None:
xmin = x
xmax = x
if (not in_marker and xmax is not None) or (in_marker and x == im.width - 1):
b = find_overlapping(spans_last, (xmin, xmax))
if b is not None:
b.ymax = y
b.xmin = min(b.xmin, xmin)
b.xmax = max(b.xmax, xmax)
else:
b = Box(xmin, xmax, y, y)
boxes.append(b)
spans[(xmin, xmax)] = b
xmin = None
xmax = None
spans_last = spans
return boxes
def analyze(self, filename, marker_color, threshold):
d = {}
im = Image.open(filename)
dimensions = {}
d['dimensions'] = dimensions
dimensions['width'] = im.width
dimensions['height'] = im.height
def update_percent(i):
wx.CallAfter(self.update_progress, i)
controls = {}
d['controls'] = controls
markings = find_markings(im, marker_color, threshold, update_percent)
for i, box in enumerate(markings):
c = {}
x, y = box.center()
c['x'] = x
c['y'] = y
controls[f'c{i+1}'] = c
return d
def toml_instrument_config(extract_result, window_contains, control_type):
doc = tomlkit.document()
doc.add(tomlkit.comment(f'The is pointer-cc instrument configuration file. Please edit it to change it, then pick "Reload Config" from the menu for your changes to take effect. See the pointer-cc documentation for details: {app_url}.'))
window = tomlkit.table()
window.add('contains', window_contains)
doc.add('window', window)
dimensions = tomlkit.table()
dimensions.add('width', extract_result['dimensions']['width'])
dimensions.add('height', extract_result['dimensions']['height'])
doc.add('dimensions', dimensions)
default_control = tomlkit.table()
default_control.add('type', control_type)
default_drag = tomlkit.table()
default_drag.add('speed', 1.0)
default_control.add('drag', default_drag)
default_wheel = tomlkit.table()
default_wheel.add('speed', 1.0)
default_wheel.add('time_resolution', 100)
default_control.add('wheel', default_wheel)
doc.add('default', default_control)
controls = tomlkit.table()
for cid, c in extract_result['controls'].items():
control = tomlkit.table()
control.add('x', c['x'])
control.add('y', c['y'])
control.add('m', 1.0)
controls.add(cid, control)
doc.add('controls', controls)
return doc
midi_type_bij = Bijection('midi', 'display', [(0x80, "NOTEOFF"), (0x90, "NOTEON"), (0xA0, "KPRESS"), (0xB0, "CC"), (0xC0, "PROG"), (0xC0, "PROG"), (0xD0, "CHPRESS"), (0xE0, "PBEND"), (0xF0, "SYSEX")])
def fmt_hex(i):
s = hex(i)[2:].upper()
prefix = "".join(((2 - len(s)) * ["0"]))
return prefix + s
def fmt_midi(msg):
if len(msg) == 0:
return None
else:
parts = []
t = midi_type_bij.display(msg[0] & 0xf0)
if t is not None:
chan = (msg[0] & 0x0f) + 1
parts.append(f'Ch.{chan}')
parts.append(t)
else:
parts.append(fmt_hex(msg[0]))
parts = parts + [str(i) for i in msg[1:]]
return ' '.join(parts)
class Dispatcher(threading.Thread):
def __init__(self, midiin, queue, frame):
super(Dispatcher, self).__init__()
self.midiin = midiin
self.queue = queue
self.frame = frame
self.port_name = None
self.midi_channel= 0
self.controllers = {}
self.polling = None
self.config = default_config()
self.instruments = {}
self.reload_all_configs()
def reload_all_configs(self):
try:
config = load_config()
except ConfigError as e:
msg = f'Configuration error: {e}'
wx.CallAfter(self.frame.show_error, msg)
else:
instruments, inst_exceptions = load_instruments()
self.config = config
self.set_instruments(instruments)
if config.preferred_midi_port is not None:
self.queue.put((InternalCommand.CHANGE_MIDI_PORT, config.preferred_midi_port, False))
if config.preferred_midi_channel is not None:
self.queue.put((InternalCommand.CHANGE_MIDI_CHANNEL, config.preferred_midi_channel))
self.stop_window_polling()
self.start_window_polling()
if len(inst_exceptions) > 0:
msgs = []
for e in inst_exceptions:
msgs.append(str(e))
msg = 'Configuration error: ' + ' '.join(msgs)
wx.CallAfter(self.frame.show_error, msg)
def get_active_controller(self):
if len(self.controllers) == 0:
return None
elif len(self.controllers) == 1:
return list(self.controllers.values())[0]
else:
x, y = mouse.get_position()
def sort_key(controller):
box = controller.window.box
key_contains = 0 if box.contains_point(x, y) else 1
key_distance = squared_dist(box.center(), (x, y))
tpl = (key_contains, key_distance)
return tpl
controllers = list(self.controllers.values())
controllers.sort(key=sort_key)
if len(controllers) > 0:
return controllers[0]
def __call__(self, event, data=None):
try:
message, deltatime = event
ch = message[0] & 0x0f
ignore = False
if self.midi_channel != 0:
if ch != self.midi_channel - 1:
return
controller = self.get_active_controller()
midi_msg_text_parts = []
m = fmt_midi(message)
if m is not None:
midi_msg_text_parts.append(m)
# note off(?)
if message[0] & 0xf0 == 0x80:
pass
ctrl_info_parts = []
if controller:
if controller.current_control is not None:
ctrl_info_parts.append(str(controller.current_control))
if controller.freewheeling:
ctrl_info_parts.append('(freewheeling)')
if message[0] & 0xf0 == 0xb0:
for binding in self.config.bindings:
if binding.cc == message[1]:
c = cmd_str.str(binding.command)
midi_msg_text_parts.append(f'({c})')
if binding.command == Command.PAN_X:
x_normed = message[2] / 127.0
controller.pan_x(x_normed)
elif binding.command == Command.PAN_X_INV:
x_normed = message[2] / 127.0
controller.pan_x(1.0 - x_normed)
elif binding.command == Command.PAN_Y:
y_normed = message[2] / 127.0
controller.pan_y(y_normed)
elif binding.command == Command.PAN_Y_INV:
y_normed = message[2] / 127.0
controller.pan_y(1.0 - y_normed)
elif binding.command == Command.ADJUST_CONTROL:
cc = message[2]
status = controller.turn(cc)
if status is not None:
ctrl_info_parts.append(status)
elif binding.command == Command.FREEWHEEL:
controller.freewheel()
ctrl_info = ' '.join(ctrl_info_parts)
midi_msg_text = ' '.join(midi_msg_text_parts)
wx.CallAfter(self.frame.update_view, midi_msg_text, ctrl_info)
except Exception as e:
traceback.print_exception(e)
def set_instruments(self, instruments):
if len(instruments) == 0:
msg = "No instruments configured, please read the docs"
else:
msg = f'{len(instruments)} instruments configured'
self.instruments = instruments
wx.CallAfter(self.frame.set_window_text, msg)
def start_window_polling(self):
self.polling = WindowPolling(self.queue, list(self.instruments.keys()))
self.polling.start()
def stop_window_polling(self):
if self.polling:
self.polling.event.set()
self.polling.join()
def run(self):
self.midiin.set_callback(self)
running = True
while running:
item = self.queue.get()
cmd = item[0]
if cmd == InternalCommand.QUIT:
running = False
self.stop_window_polling()
elif cmd == InternalCommand.CHANGE_MIDI_CHANNEL:
self.midi_channel = item[1]
if self.midiin.is_port_open() and self.port_name is not None:
self.config.set_preferred_midi(self.port_name, self.midi_channel)
elif cmd == InternalCommand.CHANGE_MIDI_PORT:
port_name = item[1]
triggered_by_user = item[2]
(success, exc) = open_midi_port(self.midiin, port_name)
if success:
self.port_name = port_name
self.config.set_preferred_midi(port_name, self.midi_channel)
self.midiin.set_callback(self)
wx.CallAfter(self.frame.set_midi_selection, self.port_name, self.midi_channel)
else:
if triggered_by_user:
msg = f"Could not open MIDI port \"{port_name}\""
wx.CallAfter(self.frame.show_error, msg)
# TODO:
# wx.CallAfter(self.frame.set_midi_selection, None, None)
elif cmd == InternalCommand.UPDATE_WINDOW:
name = item[1]
window = item[2]
if window is None:
if name in self.controllers:
del self.controllers[name]
else:
self.controllers[name] = InstrumentController(self.instruments[window.pattern], window)
c = self.get_active_controller()
if c is None:
self.frame.set_window_text('No window found')
else:
self.frame.set_window_text(c.window.name)
elif cmd == InternalCommand.RELOAD_ALL_CONFIGS:
self.reload_all_configs()
def unit_affine():
return Affine(1.0, 1.0, 0.0, 0.0)
# affine transform that can be scaling and translation
class Affine:
def __init__(self, sx, sy, dx, dy):
self.sx = sx
self.sy = sy
self.dx = dx
self.dy = dy
def inverse(self):
small = 1e-2
if abs(self.sx) < small or abs(self.sy) < small:
return unit_affine()
sx_inv = 1.0 / self.sx
sy_inv = 1.0 / self.sy
return Affine(sx_inv, sy_inv, - sx_inv * self.dx, - sy_inv * self.dy)
def multiply_right(self, other):
sx = self.sx * other.sx
sy = self.sy * other.sy
dx = sx * other.dx + self.dx
dy = sy * other.dy + self.dy
self.sx = sx
self.sy = sy
self.dx = dx
self.dy = dy
def apply(self, x, y):
x_ = self.sx * x + self.dx
y_ = self.sy * y + self.dy
return (x_, y_)
def screen_to_window(window_box):
b = window_box
return Affine(1.0, 1.0, -b.xmin, -b.ymin)
def window_to_screen(window_box):
return screen_to_window(window_box).inverse()
def model_to_window(window_box, model_box):
if model_box.width == 0 or model_box.height == 0:
return unit_affine()
sx = window_box.width / model_box.width
sy = window_box.height / model_box.height
s = min(sx, sy)
excess_x = window_box.width - s * model_box.width
excess_y = window_box.height - s * model_box.height
return Affine(s, s, excess_x / 2.0, excess_y)
def window_to_model(window_box, model_box):
return model_to_window(window_box, model_box).inverse()
class InstrumentController:
def __init__(self, instrument, window):
self.instrument = instrument
self.set_window(window)
self.mx = 0.0
self.my = 0.0
self.current_control = None
self.freewheeling = False
self.freewheeling_direction = None
self.last_cc = None
self.last_control = None
self.last_control_accum = 0.0
self.last_t = None
self.dragging = False
self.click_sm = ClickStateMachine(1.0, 2)
def set_window(self, window):
window_box = window.box
t = window_to_model(window_box, self.instrument.box)
s2w = screen_to_window(window_box)
t.multiply_right(s2w)
self.screen_to_instrument = t
self.instrument_to_screen = self.screen_to_instrument.inverse()
self.window = window
def pan_x(self, x_normed):
self.mx = self.instrument.box.width * x_normed
self.current_control = self.move_pointer_to_closest()
def pan_y(self, y_normed):
self.my = self.instrument.box.height * y_normed
self.current_control = self.move_pointer_to_closest()
def turn(self, cc):
status = None
screen_x, screen_y = mouse.get_position()
if not self.dragging:
self.mx, self.my = self.screen_to_instrument.apply(screen_x, screen_y)
self.current_control = self.instrument.find_closest_control(self.mx, self.my)
if self.last_control is not None:
if self.current_control != self.last_control:
self.click_sm.reset()
self.last_cc = cc
self.last_control_accum = 0.0
self.last_t = None
if self.last_cc is None:
self.last_cc = cc
delta = cc - self.last_cc
t = time.time()
if self.freewheeling:
if self.freewheeling_direction is None:
self.freewheeling_direction = delta > 0
elif self.freewheeling_direction != (delta > 0):
self.freewheeling = False
self.freewheeling_direction = None
else:
if self.current_control is not None:
m = self.current_control.m
speed = self.current_control.speed * self.current_control.m
self.last_control_accum += delta * speed
if self.current_control.type_ == ControlType.WHEEL:
if self.last_t is None:
self.last_t = t
else:
time_delta = t - self.last_t
time_delta_res = 1.0 / self.current_control.time_resolution
if time_delta > time_delta_res:
if time_delta < 5 * time_delta_res:
mouse.wheel(self.last_control_accum)
status = f'wheel! {"+" if self.last_control_accum > 0 else ""}{self.last_control_accum:.2f} (x{speed:.2f})'
self.last_control_accum = 0
self.last_t = t
elif self.current_control.type_ == ControlType.DRAG:
if self.dragging:
pass
else:
mouse.press()
self.dragging = True
k_whole = int(self.last_control_accum)
mouse.move(screen_x, screen_y - k_whole)
status = f'drag! {"+" if k_whole > 0 else ""}{k_whole} (x{speed:.2f})'
self.last_control_accum -= k_whole
elif self.current_control.type_ == ControlType.CLICK:
is_click = self.click_sm.on_cc(cc, time.time())
if is_click:
mouse.click()
status = f'click! (x{speed:.2f})'
self.last_control = self.current_control
self.last_cc = cc
return status
def freewheel(self):
self.freewheeling = True
self.freewheeling_direction = None
def move_pointer_to_closest(self):
c = self.instrument.find_closest_control(self.mx, self.my)
if self.dragging:
mouse.release()
self.dragging = False
x, y = self.instrument_to_screen.apply(c.x, c.y)
mouse.move(int(x), int(y))
return c
class AddInstrumentDialog(wx.Dialog):
def __init__(self, parent, title, queue):
wx.Frame.__init__(self, parent, title=title)
self.queue = queue
self.extract_result = None
topbottommargin = 25
intermargin = 25
smallmargin = 4
horizontal_margin = 20
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.AddSpacer(topbottommargin)
introText = wx.StaticText(self)
t = textwrap.dedent('''\
Here you can create a instrument configuration .txt file for your VST / Software Instrument.
After you've created it you need to edit the .txt file with a text editor to change the details
of each control, e.g. speed multiplier etc. Please read the pointer documentation on how to do this.
''')
introText.SetLabelMarkup(t)
sizer.Add(introText, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, border=horizontal_margin)
controlPosLabel = wx.StaticText(self)
controlPosLabel.SetLabelMarkup('<b>Control positions</b>')
controlPosDescr = wx.StaticText(self)
controlPosDescr.SetLabelMarkup('<i>Take a screenshot of the instrument window. Crop the screen to contents, but\nexclude all window decoration, e.g. the window title bar or borders.\nThen mark the controls by drawing rectangles in the marker color with any image app (e.g. GIMP).</i>')
# cpLabel = wx.StaticText(self, label="Marker color #FF00FF, rgb(255,0,255)")
self.cpLabel = wx.StaticText(self)
self.colorPickerCtrl = wx.ColourPickerCtrl(self)
self.colorPickerCtrl.SetColour(wx.Colour(255, 0, 255))
self.colorPickerCtrl.Bind(wx.EVT_COLOURPICKER_CHANGED, self.on_color_changed)
self.set_color_text()
cpSizer = wx.BoxSizer(wx.HORIZONTAL)
cpSizer.Add(self.colorPickerCtrl, 0)
cpSizer.Add(self.cpLabel, wx.SizerFlags().Bottom().Border(wx.LEFT, borderinpixels=smallmargin))
sizer.Add(controlPosLabel, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, border=horizontal_margin)
sizer.AddSpacer(smallmargin)
sizer.Add(controlPosDescr, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, border=horizontal_margin)
sizer.AddSpacer(smallmargin)
sizer.Add(cpSizer, 0, wx.LEFT | wx.RIGHT, border=horizontal_margin)
sizer.AddSpacer(smallmargin)
self.colorThreshold = wx.TextCtrl(self, value="30")
colorThresholdSizer = wx.BoxSizer(wx.HORIZONTAL)
colorThresholdSizer.Add(self.colorThreshold)
thresholdLabel = wx.StaticText(self, label="Marker color threshold (0-255)")
colorThresholdSizer.Add(thresholdLabel, 0, wx.LEFT, border=smallmargin)
sizer.Add(colorThresholdSizer, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, border=horizontal_margin)
self.selectScreenshot = wx.FilePickerCtrl(self, style=wx.FLP_OPEN, message="Select a cropped screenshot of instrument", wildcard="*.png")
self.selectScreenshot.Bind(wx.EVT_FILEPICKER_CHANGED, self.on_screenshot_selected)
filepicker_set_button_label(self.selectScreenshot, 'Analyze Screenshot')
self.analyzeText = wx.StaticText(self, label="(no positions extracted yet)")
analyzeSizer = wx.BoxSizer(wx.HORIZONTAL)
analyzeSizer.Add(self.selectScreenshot)
analyzeSizer.Add(self.analyzeText, 0, wx.LEFT, border=smallmargin)
sizer.Add(analyzeSizer, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, border=horizontal_margin)
sizer.AddSpacer(intermargin)
windowPatternLabel = wx.StaticText(self)
windowPatternLabel.SetLabelMarkup('<b>Window Pattern</b>')
windowPatternDescr = wx.StaticText(self, style=wx.LB_MULTIPLE)
windowPatternDescr.SetLabelMarkup('<i>What string is always contained in the instrument\'s window title (usually the name)? This is needed to detect its window.</i>')
self.window_pattern_ctrl = wx.TextCtrl(self)
sizer.Add(windowPatternLabel, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, border=horizontal_margin)
sizer.AddSpacer(smallmargin)
sizer.Add(windowPatternDescr, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, border=horizontal_margin)
sizer.AddSpacer(smallmargin)
sizer.Add(self.window_pattern_ctrl, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, border=horizontal_margin)
sizer.AddSpacer(intermargin)
mouseControlLabel = wx.StaticText(self)
mouseControlLabel.SetLabelMarkup('<b>Mouse control</b>')
mouseControlDescr = wx.StaticText(self)
mouseControlDescr.SetLabelMarkup('<i>How does the mouse adjust controls?</i>')
sizer.Add(mouseControlLabel, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, border=horizontal_margin)
sizer.AddSpacer(smallmargin)
sizer.Add(mouseControlDescr, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, border=horizontal_margin)
sizer.AddSpacer(smallmargin)
choices = [
"mouse drag up and down",
"mouse wheel"
]
self.mousectrl_combo = wx.ComboBox(self, id=wx.ID_ANY, value="mouse wheel", choices=choices, style=wx.CB_READONLY)
sizer.Add(self.mousectrl_combo, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, border=horizontal_margin)
sizer.AddSpacer(intermargin)
filenameDescr = wx.StaticText(self)
filenameDescr.SetLabelMarkup('<i>Choose instrument configuration save file. The filename must start with "inst-" and end with ".txt".</i>')
self.chooseSaveFile = wx.FilePickerCtrl(self, style=wx.FLP_SAVE, message="Save instrument text file")
filepicker_set_button_label(self.chooseSaveFile, 'Save Instrument')
self.chooseSaveFile.SetInitialDirectory(datadir())
self.chooseSaveFile.SetPath(userfile('inst-renameme.txt'))
self.chooseSaveFile.Bind(wx.EVT_FILEPICKER_CHANGED, self.on_save_file_picked)
sizer.Add(filenameDescr, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, border=horizontal_margin)
sizer.AddSpacer(smallmargin)
sizer.Add(self.chooseSaveFile, 0, wx.CENTER | wx.LEFT | wx.RIGHT, border=horizontal_margin)
sizer.AddSpacer(topbottommargin)
self.progress = None
self.SetSizer(sizer)
self.Fit()
def on_color_changed(self, _):
self.set_color_text()
def set_color_text(self):
c = self.colorPickerCtrl.GetColour()
r, g, b, _ = c.Get()
msg = 'Marker color: #' + fmt_hex(r) + fmt_hex(g) + fmt_hex(b) + ", " + f'rgb({r},{g},{b})'
self.cpLabel.SetLabel(msg)
def on_extract_done(self, delayed_result, **kwargs):
if self.progress:
self.progress.Destroy()
self.progress = None
self.set_extract_result(delayed_result.get(), kwargs['path'])
def update_progress(self, i):
if self.progress:
self.progress.Update(i)
def on_screenshot_selected(self, event):
v = self.colorThreshold.GetValue()
msg = None
try:
v_int = int(v)
except Exception as e:
msg = f'Color threshold {v} is not an integer'
if not (0 <= v_int and v_int <= 255):
msg = f'Color threshold {v} is not in range 0-255'
if msg:
wx.CallAfter(self.frame.show_error, msg)
return
path = self.selectScreenshot.GetPath()
marker_color = self.colorPickerCtrl.GetColour().Get()
self.progress = wx.ProgressDialog("In Progress", message="Analyzing screenshot...")
wx.lib.delayedresult.startWorker(self.on_extract_done, workerFn=analyze, ckwargs=dict(path=path), wargs=[self, path, marker_color, v_int])
def set_extract_result(self, extract_result, path):
self.extract_result = extract_result
dim = extract_result['dimensions']
width = dim['width']
height = dim['height']
n = len(extract_result['controls'])
filename = os.path.basename(path)
msg = f'{n} controls found in {filename}, {width}x{height}.'
self.analyzeText.SetLabel(msg)
def on_save_file_picked(self, event):
path = self.chooseSaveFile.GetPath()
problems = []
if os.path.dirname(path) != datadir():
problems.append('Instrument file is not chosen in the configuration directory')
if not re.match('^inst-(.*)\.txt$', os.path.basename(path)):
problems.append('Instrument file is not named in format inst-{name}.txt . It must start with "inst-" and end in ".txt".')
if self.extract_result is None:
problems.append('Screenshot analysis is missing')
window_contains = self.window_pattern_ctrl.GetValue()
if len(window_contains) == 0:
problems.append('Window title pattern is missing')
if len(problems) > 0:
message = "Did not save the instrument because:\n"
message += '\n'.join([f"{i+1}: {p}" for i, p in enumerate(problems)])
dlg = wx.MessageDialog(None, message, 'Instrument not saved', wx.OK | wx.ICON_ERROR)
dlg.ShowModal()
dlg.Destroy()
return
mouse_control = control_type_bij.str([ControlType.DRAG, ControlType.WHEEL][self.mousectrl_combo.GetCurrentSelection()])
doc = toml_instrument_config(self.extract_result, window_contains, mouse_control)
with open(path, 'w') as f:
f.write(doc.as_string())
self.queue.put((InternalCommand.RELOAD_ALL_CONFIGS, None))
message = f'"{os.path.basename(path)}" was successfully saved to the configuration directory.\nTo further adjust it you need to open it with a text editor.\nPlease read the documentation on the details of the instrument configuration file. Please "Reload Config" in the menu after you\'ve changed config files to take effect.'
dlg = wx.MessageDialog(None, message, f'Instrument successfully saved', wx.OK | wx.CANCEL)
dlg.SetOKLabel("Open configuration directory")
response = dlg.ShowModal()
dlg.Destroy()
if response == wx.ID_OK:
open_directory(datadir())
self.EndModal(0)
class AboutWindow(wx.Frame):
def __init__(self, parent):
super(AboutWindow, self).__init__(parent)
msg = f'pointer-cc, Version {version}\nby Stefan Matting\nPlease send feedback to [email protected] or the github site.'
sizer = wx.BoxSizer(wx.VERTICAL)
margin_outside = 20
sizer.AddSpacer(40)
self.version_label = wx.StaticText(self, label=msg, style=wx.ALIGN_CENTRE_HORIZONTAL)
sizer.Add(self.version_label, 0, wx.LEFT | wx.RIGHT, margin_outside)
sizer.AddSpacer(10)
self.go_website = wx.Button(self, label='Go to website')
self.go_website.Bind(wx.EVT_BUTTON, self.on_go_website)
sizer.Add(self.go_website, 0, wx.LEFT | wx.RIGHT, margin_outside)
sizer.AddSpacer(10)
self.copy_email = wx.Button(self, label='Copy email address to clipboard')
self.copy_email.Bind(wx.EVT_BUTTON, self.on_copy_email)
sizer.Add(self.copy_email, 0, wx.LEFT | wx.RIGHT, margin_outside)
sizer.AddSpacer(10)
self.close_button = wx.Button(self, label='Close window')
self.close_button.Bind(wx.EVT_BUTTON, self.on_close)
sizer.Add(self.close_button, 0, wx.LEFT | wx.RIGHT, margin_outside)
sizer.AddSpacer(40)
self.SetSizer(sizer)
sizer.SetMinSize((300, 0))
sizer.Fit(self)
def on_go_website(self, event):
webbrowser.open(app_url)
def on_copy_email(self, event):
pyperclip.copy(app_email)
def on_close(self, event):
self.Destroy()
class MainWindow(wx.Frame):
def __init__(self, parent, title, q, ports):
wx.Frame.__init__(self, parent, title=title)
self.panel = wx.Panel(self, wx.ID_ANY)
self.Bind(wx.EVT_CLOSE, self.on_close)
self.queue = q
self.ports = ports
files = importlib.resources.files('pointercc')
with files.joinpath('resources/logo-small.png').open('rb') as f:
png = wx.Image(f, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
logo = wx.StaticBitmap(self.panel, -1, png, (10, 10), (png.GetWidth(), png.GetHeight()))
self.version_label = wx.StaticText(self.panel, label=f'', style=wx.ALIGN_CENTRE_HORIZONTAL)
value = ""
self.port_dropdown = wx.ComboBox(self.panel, id=wx.ID_ANY, value=value, choices=self.ports, style=wx.CB_READONLY)
self.Bind(wx.EVT_COMBOBOX, self.handle_midi_port_choice, self.port_dropdown)
channel_choices = ["All"] + [f"Ch. {i}" for i in range(1, 17)]
value = ""
# value = channel_choices[config.preferred_midi_channel]
self.channel_dropdown = wx.ComboBox(self.panel, id=wx.ID_ANY, value=value, choices=channel_choices, style=wx.CB_READONLY)
self.Bind(wx.EVT_COMBOBOX, self.handle_midi_channel_choice, self.channel_dropdown)
self.window_text_ctrl = wx.TextCtrl(self.panel, style=wx.TE_READONLY)