-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqlab_mimic.py
452 lines (366 loc) · 17 KB
/
qlab_mimic.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
# This file is a derivation of work on - and as such shares the same
# licence as - Linux Show Player
#
# Linux Show Player:
# Copyright 2012-2022 Francesco Ceruti <[email protected]>
#
# This file:
# Copyright 2022 s0600204
#
# Linux Show Player 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 3 of the License, or
# (at your option) any later version.
#
# Linux Show Player 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.
#
# You should have received a copy of the GNU General Public License
# along with Linux Show Player. If not, see <http://www.gnu.org/licenses/>.
from json import JSONEncoder
import logging
import time
from uuid import uuid4
from liblo import SLIP_DOUBLE
# pylint: disable=import-error
from lisp.core.plugin import Plugin
from lisp.core.util import get_lan_ip
from lisp.plugins.list_layout.layout import ListLayout
from lisp.ui.settings.app_configuration import AppConfigurationDialog
from lisp.ui.ui_utils import translate
from .cues_handler import CuesHandler, CUE_STATE_CHANGES
from .osc_tcp_server import OscTcpServer
from .service_announcer import QLabServiceAnnouncer
from .settings import QlabMimicSettings
from .utility import client_id_string, join_path, QlabStatus, split_path
logger = logging.getLogger(__name__) # pylint: disable=invalid-name
QLAB_VERSION = '4.3'
QLAB_TCP_PORT = 53000
MESSAGE_RECV_TIMEOUT = 0.1 # seconds
class QlabMimic(Plugin):
"""LiSP pretends to be QLab for the purposes of basic OSC control"""
Name = 'QLab Mimic'
Authors = ('s0600204',)
Description = 'LiSP pretends to be QLab for the purposes of basic OSC control.'
def __init__(self, app):
super().__init__(app)
self._session_name = None
self._session_uuid = None
self._connected_clients = {}
self._last_messages = {}
self._encoder = JSONEncoder(separators=(',', ':'))
self._cues_message_handler = CuesHandler(self)
self._server = OscTcpServer(QLAB_TCP_PORT)
self._server.register_method(self._handle_always_reply, '/alwaysReply')
self._server.register_method(self._handle_workspaces, '/workspaces')
self._server.start()
self._server.new_message.connect(self._generic_handler)
self._server_announcer = QLabServiceAnnouncer(QLAB_TCP_PORT)
AppConfigurationDialog.registerSettingsPage(
'plugins.qlab_mimic', QlabMimicSettings, self.Config)
self.Config.updated.connect(self._actualize_config)
self._actualize_config(self.Config)
def _actualize_config(self, _):
if self.Config.get("service_announcement", True):
self._server_announcer.start()
else:
self._server_announcer.stop()
def _on_session_initialised(self, session):
self._session_name = session.name()
self._session_uuid = str(uuid4())
self.app.cue_model.item_added.connect(self._on_cue_added)
self.app.layout.model.item_moved.connect(self._on_cue_moved)
self.app.cue_model.item_removed.connect(self._on_cue_removed)
if isinstance(self.app.layout, ListLayout):
self.app.layout.view.listView.currentItemChanged.connect(self._emit_playback_head_updated)
self._cues_message_handler.register_cuelists(self.app.layout)
def _pre_session_deinitialisation(self, _):
self._emit_workspace_disconnect()
self._session_name = None
self._session_uuid = None
self.app.cue_model.item_added.disconnect(self._on_cue_added)
self.app.layout.model.item_moved.disconnect(self._on_cue_moved)
self.app.cue_model.item_removed.disconnect(self._on_cue_removed)
if isinstance(self.app.layout, ListLayout):
self.app.layout.view.listView.currentItemChanged.disconnect(self._emit_playback_head_updated)
self._cues_message_handler.deregister_cuelists()
@property
def server(self):
return self._server
def terminate(self):
self._server.stop()
def finalize(self):
logger.debug('Shutting down QLab server')
self.terminate()
self._server_announcer.terminate()
def send_reply(self, src, path, status, data=None, send_id=True):
client_id = client_id_string(src)
if data is None and (client_id not in self._connected_clients or not self._connected_clients[client_id][2]):
return
src.set_slip_enabled(SLIP_DOUBLE)
response = {
'address': path,
'status': status.value,
}
if send_id and self._session_uuid:
response['workspace_id'] = self._session_uuid
if status is QlabStatus.Ok and data is not None:
response['data'] = data
response = self._encoder.encode(response)
self._server.send(src, '/reply' + path, response)
def send_update(self, path, args=[], always_send=False):
path[0:0] = ['update', 'workspace', self._session_uuid]
path = join_path(path)
to_prune = []
for client_id, client in self._connected_clients.items():
if client[1] or always_send:
client[0].set_slip_enabled(SLIP_DOUBLE)
if not self._server.send(client[0], path, *args):
to_prune.append(client_id)
for client_id in to_prune:
logger.debug(f"Unable to update client at '{client_id}'. Removing from list of connected clients.")
del self._connected_clients[client_id]
def _generic_handler(self, original_path, args, types, src, user_data):
if (src.url in self._last_messages
and original_path == self._last_messages[src.url][0]
and time.time() < self._last_messages[src.url][1] + MESSAGE_RECV_TIMEOUT
):
logger.debug(
f"Duplicate message received too soon after the last, ignoring. "\
f"({src.hostname} :: {original_path})"
)
return
self._last_messages[src.url] = [original_path, time.time()]
path = split_path(original_path)
handlers = {
'cue': self._handle_cue,
'cue_id': self._handle_cue,
'disconnect': self._handle_disconnect,
'go': self._handle_go,
'stop': self._handle_stop,
'updates': self._handle_updates,
'version': self._handle_version,
'workspace': self._handle_workspace,
}
if path[0] not in handlers:
self.send_reply(src, original_path, QlabStatus.NotOk)
return
handlers.get(
path[0], lambda *_: None
)(
original_path, args, types, src, user_data
)
def _handle_always_reply(self, original_path, args, types, src, user_data):
client_id = client_id_string(src)
if client_id not in self._connected_clients:
# No point in sending a reply, as we don't recognise the client
return
self._connected_clients[client_id][2] = bool(args[0])
self.send_reply(src, original_path, QlabStatus.Ok)
def _handle_connect(self, original_path, args, types, src, user_data):
client_id = client_id_string(src)
if client_id not in self._connected_clients:
self._connected_clients[client_id] = [src, False, False]
self.send_reply(src, original_path, QlabStatus.Ok, 'ok')
def _handle_cue(self, original_path, args, types, src, user_data):
path = split_path(original_path)
if path[0] == 'workspace':
del path[0:2]
return_path = join_path(path)
if path[0] == 'cue':
status, data = self._cues_message_handler.by_cue_number(path, args, self.app.layout)
else:
status, data = self._cues_message_handler.by_cue_id(path, args, self.app.cue_model)
self.send_reply(src, return_path, status, data)
def _handle_cuelists(self, path, args, types, src, user_data):
cuelists = self._cues_message_handler.get_cuelists()
self.send_reply(src, path, QlabStatus.Ok, cuelists)
def _handle_disconnect(self, original_path, args, types, src, user_data):
client_id = client_id_string(src)
if client_id in self._connected_clients:
self.send_reply(src, original_path, QlabStatus.Ok)
del self._connected_clients[client_id]
else:
logger.warn(client_id + " not recognised (disconnect)")
def _handle_doubleGoWindowRemaining(self, path, args, types, src, user_data):
self.send_reply(src, path, QlabStatus.Ok, 0)
def _handle_go(self, path, args, types, src, user_data):
self.app.layout.go()
self.send_reply(src, path, QlabStatus.Ok)
def _handle_panic(self, path, args, types, src, user_data):
self.app.layout.interrupt_all()
self.send_reply(src, path, QlabStatus.Ok)
def _handle_pause(self, path, args, types, src, user_data):
self.app.layout.pause_all()
self.send_reply(src, path, QlabStatus.Ok)
def _handle_resume(self, path, args, types, src, user_data):
self.app.layout.resume_all()
self.send_reply(src, path, QlabStatus.Ok)
def _handle_runningCues(self, path, args, types, src, user_data):
self._cues_message_handler.get_currently_playing(False)
self.send_reply(src, path, QlabStatus.Ok, cues)
def _handle_runningOrPausedCues(self, path, args, types, src, user_data):
cues = self._cues_message_handler.get_currently_playing(True)
self.send_reply(src, path, QlabStatus.Ok, cues)
def _handle_select(self, path, args, types, src, user_data):
'''
/workspace/<id>/select/{previous|next}
'''
if not isinstance(self.app.layout, ListLayout):
self.send_reply(src, path, QlabStatus.NotOk)
return
action = split_path(path)[3]
actions = {
'next': lambda current: current + 1,
'previous': lambda current: current - 1,
}
if action not in actions:
self.send_reply(src, path, QlabStatus.NotOk)
return
self.app.layout.set_standby_index(
actions.get(action)(self.app.layout.standby_index())
)
self.send_reply(src, path, QlabStatus.Ok)
def _handle_selectId(self, path, args, types, src, user_data):
'''
/workspace/<id>/select_id/<cue_id>
'''
if not isinstance(self.app.layout, ListLayout):
self.send_reply(src, path, QlabStatus.NotOk)
return
cue_id = split_path(path)[3]
cue = self.app.layout.cue_model.get(cue_id)
if cue is None or cue.index < 0:
self.send_reply(src, path, QlabStatus.NotOk)
return
self.app.layout.set_standby_index(cue.index)
self.send_reply(src, path, QlabStatus.Ok)
def _handle_selectionIsPlayhead(self, path, args, types, src, user_data):
if isinstance(self.app.layout, ListLayout):
if args:
self.app.layout.selection_mode = not bool(args[0])
self.send_reply(src, path, QlabStatus.Ok)
else:
self.send_reply(src, path, QlabStatus.Ok, int(not self.app.layout.selection_mode))
else:
if args:
self.send_reply(src, path, QlabStatus.NotOk)
else:
self.send_reply(src, path, QlabStatus.Ok, 0)
def _handle_showMode(self, path, args, types, src, user_data):
if args:
# We don't support changing this setting
self.send_reply(src, path, QlabStatus.NotOk)
else:
self.send_reply(src, path, QlabStatus.Ok, 1)
def _handle_stop(self, path, args, types, src, user_data):
self.app.layout.stop_all()
self.send_reply(src, path, QlabStatus.Ok)
def _handle_thump(self, path, args, types, src, user_data):
self.send_reply(src, path, QlabStatus.Ok, 'thump')
def _handle_updates(self, original_path, args, types, src, user_data):
client_id = client_id_string(src)
if client_id not in self._connected_clients:
# No point in sending a reply, as we don't recognise the client
return
self._connected_clients[client_id][1] = bool(args[0])
self.send_reply(src, original_path, QlabStatus.Ok)
def _handle_version(self, original_path, args, types, src, user_data):
self.send_reply(src, original_path, QlabStatus.Ok, QLAB_VERSION)
def _handle_workspace(self, original_path, args, types, src, user_data):
path = split_path(original_path)
# If wrong workspace
if path[1] != self._session_uuid and path[1] != self._session_name:
self.send_reply(src, original_path, QlabStatus.NotOk, send_id=False)
return
handlers = {
'connect': self._handle_connect,
'cue': self._handle_cue,
'cue_id': self._handle_cue,
'cueLists': self._handle_cuelists,
'disconnect': self._handle_disconnect,
'doubleGoWindowRemaining': self._handle_doubleGoWindowRemaining,
'go': self._handle_go,
'panic': self._handle_panic,
'pause': self._handle_pause,
'resume': self._handle_resume,
'runningCues': self._handle_runningCues,
'runningOrPausedCues': self._handle_runningOrPausedCues,
'select': self._handle_select,
'select_id': self._handle_selectId,
'selectionIsPlayhead': self._handle_selectionIsPlayhead,
'showMode': self._handle_showMode,
'stop': self._handle_stop,
'thump': self._handle_thump,
'updates': self._handle_updates,
}
if path[2] not in handlers:
self.send_reply(src, original_path, QlabStatus.NotOk)
return
handlers.get(
path[2], lambda *_: None
)(
original_path, args, types, src, user_data
)
def _handle_workspaces(self, path, args, types, src, user_data):
if not self._session_uuid:
self.send_reply(src, path, QlabStatus.NotOk, send_id=False)
return
workspaces = [{
'uniqueID': self._session_uuid,
'displayName': self._session_name,
'hasPasscode': False,
'version': QLAB_VERSION,
}]
self.send_reply(src, path, QlabStatus.Ok, workspaces, send_id=False)
def _on_cue_added(self, cue):
self.emit_workspace_updated()
# Emit that the parent cue has been changed
self.emit_cue_updated(self._cues_message_handler.cue_parent(cue))
# Set listeners for when a cue has been edited...
cue.properties_changed.connect(self.emit_cue_updated)
# ...and when it changes state
for state_change in CUE_STATE_CHANGES:
cue.__getattribute__(state_change).connect(self.emit_cue_updated)
def _on_cue_removed(self, cue):
self.emit_workspace_updated()
# Emit that the parent cue has been changed
self.emit_cue_updated(self._cues_message_handler.cue_parent(cue))
# Remove listeners for when a cue has been edited...
cue.properties_changed.disconnect(self.emit_cue_updated)
# ...and when it changes state
for state_change in CUE_STATE_CHANGES:
cue.__getattribute__(state_change).disconnect(self.emit_cue_updated)
def _on_cue_moved(self, old_index, new_index):
cue = self.app.layout.model.item(new_index)
self.emit_workspace_updated()
# Emit that the parent cue(s) have been changed
self.emit_cue_updated(self._cues_message_handler.cue_parent(cue))
if not isinstance(self.app.layout, ListLayout):
# In case cue has been moved from one page to another
old_page_num = self.app.layout.to_3d_index(old_index)[0]
new_page_num = self.app.layout.to_3d_index(new_index)[0]
if old_page_num != new_page_num:
self.emit_cue_updated(self._cues_message_handler.cuelist(old_page_num))
def emit_cue_updated(self, cue):
'''Sent if the cue or its state has changed'''
self.send_update(['cue_id', cue.id])
def _emit_workspace_disconnect(self):
'''Sent to tell clients that they need to disconnect
e.g. because the workspace or application is being closed.
'''
self.send_update(['disconnect'], always_send=True)
def emit_workspace_updated(self, *_):
'''Sent if the cue lists for the workspace need reloading
For instance when a cue is added, removed, or other aspects of a workspace are updated.
'''
self.send_update([])
def _emit_playback_head_updated(self, selected, _):
'''Sent if the the selected cue has changed'''
cuelists = self._cues_message_handler.get_cuelists()
self.send_update(
['cueList', cuelists[0]['uniqueID'], 'playbackPosition'],
[selected.cue.id] if selected else []
)