-
Notifications
You must be signed in to change notification settings - Fork 65
/
zynthian_main.py
executable file
·202 lines (161 loc) · 6.82 KB
/
zynthian_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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ******************************************************************************
# ZYNTHIAN PROJECT: Zynthian GUI
#
# Zynthian GUI Main Program
#
# Copyright (C) 2015-2023 Fernando Moyano <[email protected]>
#
# ******************************************************************************
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# For a full copy of the GNU General Public License see the LICENSE.txt file.
#
# ******************************************************************************
import sys
import signal
import ctypes
import logging
from tkinter import EventType
# Zynthian specific modules
from zyngui import zynthian_gui_config
from zyngine.zynthian_chain import *
from zyncoder.zyncore import get_lib_zyncore
from zyngui.zynthian_gui import zynthian_gui
from zyngui import zynthian_gui_keybinding
from zynlibs.zynseq import *
# ******************************************************************************
# ------------------------------------------------------------------------------
# Start Zynthian!
# ------------------------------------------------------------------------------
# ******************************************************************************
logging.info("STARTING ZYNTHIAN-UI ...")
zynthian_gui_config.zyngui = zyngui = zynthian_gui()
zyngui.create_screens()
zyngui.run_start_thread()
# ------------------------------------------------------------------------------
# Zynlib Callbacks
# ------------------------------------------------------------------------------
@ctypes.CFUNCTYPE(None, ctypes.c_ubyte, ctypes.c_int)
def zynpot_cb(i, dval):
# logging.debug("Zynpot {} Callback => {}".format(i, dval))
try:
zyngui.zynpot_lock.acquire()
zyngui.zynpot_dval[i] += dval
zyngui.zynpot_lock.release()
zyngui.zynpot_event.set()
except Exception as err:
logging.exception(err)
get_lib_zyncore().setup_zynpot_cb(zynpot_cb)
# ------------------------------------------------------------------------------
# Reparent Top Window using GTK XEmbed protocol features
# ------------------------------------------------------------------------------
def flushflush():
for i in range(1000):
print("FLUSHFLUSHFLUSHFLUSHFLUSHFLUSHFLUSH")
zynthian_gui_config.top.after(200, flushflush)
if zynthian_gui_config.wiring_layout == "EMULATOR":
top_xid = zynthian_gui_config.top.winfo_id()
print("Zynthian GUI XID: " + str(top_xid))
if len(sys.argv) > 1:
parent_xid = int(sys.argv[1])
print("Parent XID: " + str(parent_xid))
zynthian_gui_config.top.geometry('-10000-10000')
zynthian_gui_config.top.overrideredirect(True)
zynthian_gui_config.top.wm_withdraw()
flushflush()
zynthian_gui_config.top.after(
1000, zynthian_gui_config.top.wm_deiconify)
# ------------------------------------------------------------------------------
# Signal Catching
# ------------------------------------------------------------------------------
def exit_handler(signo, stack_frame):
logging.info("Catch Exit Signal ({}) ...".format(signo))
if signo == signal.SIGHUP:
exit_code = 0
elif signo == signal.SIGINT:
exit_code = 100
elif signo == signal.SIGQUIT:
exit_code = 102
elif signo == signal.SIGTERM:
exit_code = 101
else:
exit_code = 0
zyngui.exit(exit_code)
signal.signal(signal.SIGHUP, exit_handler)
signal.signal(signal.SIGINT, exit_handler)
signal.signal(signal.SIGQUIT, exit_handler)
signal.signal(signal.SIGTERM, exit_handler)
def delete_window():
exit_code = 101
zyngui.exit(exit_code)
zynthian_gui_config.top.protocol("WM_DELETE_WINDOW", delete_window)
# ------------------------------------------------------------------------------
# Key Bindings
# ------------------------------------------------------------------------------
cb_toggle = {}
def cb_keybinding(event):
"""Handle key press/release event
event : key event
keypress : True if press, False if release
"""
# Avoid TAB confusing widget focus change
if event.keycode == 23:
zynthian_gui_config.top.focus_set()
cuia = zynthian_gui_keybinding.get_key_action(event.keycode, event.state)
if cuia is not None:
# Emulate Zynswitch Push/Release with KeyPress/KeyRelease
if cuia.lower().startswith("zyn"):
if cuia[-2:] == ',T':
if event.type == EventType.KeyPress:
if event.keycode in cb_toggle:
cb_toggle[event.keycode] = not cb_toggle[event.keycode]
else:
cb_toggle[event.keycode] = True
if cb_toggle[event.keycode]:
zyngui.cuia_queue.put_nowait(cuia[:-1] + "P")
else:
zyngui.cuia_queue.put_nowait(cuia[:-1] + "R")
elif event.type == EventType.KeyPress:
zyngui.cuia_queue.put_nowait(cuia + ",P")
else:
zyngui.cuia_queue.put_nowait(cuia + ",R")
# Or normal CUIA
elif event.type == EventType.KeyPress:
zyngui.cuia_queue.put_nowait(cuia)
zynthian_gui_config.top.bind("<KeyPress>", cb_keybinding)
zynthian_gui_config.top.bind("<KeyRelease>", cb_keybinding)
# ------------------------------------------------------------------------------
# Mouse/Touch Bindings
# ------------------------------------------------------------------------------
zynthian_gui_config.top.bind("<Button-1>", zyngui.cb_touch)
zynthian_gui_config.top.bind("<ButtonRelease-1>", zyngui.cb_touch_release)
# ------------------------------------------------------------------------------
# TKinter Main Loop
# ------------------------------------------------------------------------------
# import cProfile
# cProfile.run('zynthian_gui_config.top.mainloop()')
try:
zynthian_gui_config.top.mainloop()
except Exception as e:
logging.error(f"Main Loop Exception: {e}")
if 0 <= zyngui.exit_wait_count <= 10:
logging.error("Forced exit! Trying to terminate properly...")
zyngui.exit()
sleep(2)
# ------------------------------------------------------------------------------
# Exit
# ------------------------------------------------------------------------------
logging.info("Exit with code {} ...\n\n".format(zyngui.exit_code))
sys.exit(zyngui.exit_code)
# ------------------------------------------------------------------------------