forked from jpf/Zoom.spoon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.lua
524 lines (483 loc) · 17.3 KB
/
init.lua
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
--[[
--------------------------------------------------------
-- Instantiate Spoon
--------------------------------------------------------
]]
local obj = {}
obj.__index = obj
--[[
--------------------------------------------------------
-- Spoon Metadata
--------------------------------------------------------
]]
obj.name = 'Expanded Zoom Spoon'
obj.version = '3.0'
obj.author = 'Luke Brooks'
obj.license = 'MIT'
obj.homepage = 'https://github.com/luke-brooks/Zoom.spoon'
obj.adaptedFrom = {
author = 'Joel Franusic',
homepage = 'https://github.com/jpf/Zoom.spoon'
}
--[[
--------------------------------------------------------
-- Zoom State Machine
--------------------------------------------------------
]]
APP_STATES = {
CLOSED = 'closed',
RUNNING = 'running',
MEETING = 'meeting',
SHARING = 'sharing'
}
-- via: https://github.com/kyleconroy/lua-state-machine/
local machine = dofile(hs.spoons.resourcePath('statemachine.lua'))
Zoom_State = machine.create({
initial = 'closed',
events = {{
name = 'start',
from = APP_STATES.CLOSED,
to = APP_STATES.RUNNING
}, {
name = 'startMeeting',
from = APP_STATES.RUNNING,
to = APP_STATES.MEETING
}, {
name = 'startShare',
from = APP_STATES.MEETING,
to = APP_STATES.SHARING
}, {
name = 'endShare',
from = APP_STATES.SHARING,
to = APP_STATES.MEETING
}, {
name = 'endMeeting',
from = APP_STATES.MEETING,
to = APP_STATES.RUNNING
}, {
name = 'stop',
from = APP_STATES.RUNNING,
to = APP_STATES.CLOSED
}},
callbacks = {
onstatechange = function(self, event, from, to)
changeName = 'from-' .. from .. '-to-' .. to
_printInfo(string.format('internal state machine transition %s', changeName))
if (obj.transitionCallbackFunction ~= nil) then
obj.transitionCallbackFunction(changeName)
end
end
}
})
--[[
--------------------------------------------------------
-- Object Properties & Constants
--------------------------------------------------------
]]
ZOOM_APP_NAME = 'zoom.us'
ZOOM_APP_INSTANCE = nil
local DEBUG_MODE = false
obj.audio = {}
obj.video = {}
obj.share = {}
obj.chat = {}
obj.participants = {}
obj.stateCallbackFunction = nil
obj.transitionCallbackFunction = nil
local INPUT_STATES = {
OFF = 'off',
MUTED = 'muted',
UNMUTED = 'unmuted'
}
local WINDOW_TITLES = {
MEETING = 'Zoom Meeting',
WEBINAR = 'Zoom Webinar',
MAIN = 'Zoom',
SHARING = 'zoom share',
CHAT = 'Chat',
PARTICIPANTS = 'Participants'
-- ??? BREAKOUT = 'Zoom Room'
}
local MENU_ITEMS = {
-- Meeting Menu Items
MEETING = {
TOP = 'Meeting',
INVITE = 'Invite',
UNMUTE_AUDIO = 'Unmute audio',
MUTE_AUDIO = 'Mute audio',
START_VIDEO = 'Start video',
STOP_VIDEO = 'Stop video',
STOP_SHARE = 'Stop Share'
},
-- View Menu Items
VIEW = {
TOP = 'View',
SHOW_CHAT = 'Show Chat',
CLOSE_CHAT = 'Close Chat',
SHOW_PARTICIPANTS = 'Show Manage Participants',
CLOSE_PARTICIPANTS = 'Close Manage Participants',
SHOW_SHARE_CONTROLS = 'Show Floating Meeting Controls'
}
}
--[[
--------------------------------------------------------
-- App & Zoom Event Watchers
--------------------------------------------------------
]]
zoom_state_watcher = nil
-- Watches all application events
-- & instantiates the zoom_state_watcher if Zoom was not open during hs config load
app_watcher = hs.application.watcher.new(function(appName, eventType, appObject)
_printInfo(string.format('App Name %s', appName))
_printInfo(string.format('examining external events %s', eventType))
if (appName == ZOOM_APP_NAME) then
_start_zoom_state_watcher(appObject)
_printInfo(string.format('zoom state machine current %s', Zoom_State.current))
end
end)
function _start_zoom_state_watcher(tempZoomObject)
_printInfo(string.format('zoom_state_watcher status %s', zoom_state_watcher))
if (zoom_state_watcher == nil and tempZoomObject ~= nil) then
_printInfo('instantiating zoom_state_watcher')
ZOOM_APP_INSTANCE = tempZoomObject
zoom_state_watcher = ZOOM_APP_INSTANCE:newWatcher(function(element, event, zoom_state_watcher, userData)
_printInfo(string.format('zoom state watcher events %s', event))
_printInfo(string.format('zoom state watcher element %s', element))
_determineZoomState()
end, {
name = ZOOM_APP_NAME
})
-- these events get frickin hammered on screen share, breakout rooms, & just normal usage
-- hs.uielement.watcher.windowMoved,
-- hs.uielement.watcher.windowResized,
-- a more aggressive state watcher will help keep the state & mute statuses accurate
-- but more aggressive seems to be harming performance, need to refine
zoom_state_watcher:start({hs.uielement.watcher.applicationActivated,
hs.uielement.watcher.applicationDeactivated, hs.uielement.watcher.applicationHidden,
hs.uielement.watcher.applicationShown, hs.uielement.watcher.mainWindowChanged,
hs.uielement.watcher.focusedWindowChanged, hs.uielement.watcher.focusedElementChanged,
hs.uielement.watcher.windowMinimized, hs.uielement.watcher.windowUnminimized,
hs.uielement.watcher.windowCreated, hs.uielement.watcher.titleChanged,
hs.uielement.watcher.elementDestroyed})
end
end
function _stop_zoom_state_watcher()
if (zoom_state_watcher ~= nil) then
zoom_state_watcher:stop()
Zoom_State:stop()
zoom_state_watcher = nil
end
end
--[[
--------------------------------------------------------
-- Trying to deprecate
--------------------------------------------------------
]]
-- alternative to the func below: hs.timer.delayed.new(0.2, function() end)
-- Pauses script execution
local clock = os.clock
function _pause(n) -- 'n' in seconds, can be decimal for partial seconds
local t0 = clock()
while clock() - t0 <= n do
end
end
--[[
--------------------------------------------------------
-- Internal Functions
--------------------------------------------------------
]]
function _printInfo(msg)
if (DEBUG_MODE == true) then
hs.printf('Zoom.spoon: ' .. msg)
end
end
-- Returns the Zoom hs.application object
function _getZoomInstance()
if (ZOOM_APP_INSTANCE ~= nil) then
return ZOOM_APP_INSTANCE
else
-- nesting like this for processing efficiency by avoiding unnecessary hs.application.get()
local app = hs.application.get(ZOOM_APP_NAME)
if (app ~= nil) then
ZOOM_APP_INSTANCE = app
return ZOOM_APP_INSTANCE
else
_printInfo('unable to retrieve Zoom App Instance')
return nil
end
end
end
-- Adjusts state to a proper value
-- mostly needed when hs config load happens while Zoom is already running
function _handleImproperState()
if (Zoom_State.current == APP_STATES.CLOSED) then
Zoom_State:start()
end
if (Zoom_State.current == APP_STATES.MEETING) then
Zoom_State:endMeeting()
end
end
-- Determines Zoom state by examining open windows & their titles
-- Returns highest priority hs.window object for current Zoom state
function _determineZoomState(triggerChange)
local app = _getZoomInstance()
if (app ~= nil and Zoom_State ~= nil) then
-- https://stackoverflow.com/a/66003880/8677309
-- using this default nonsense until i can streamline the _change function
local defaultTrigger = true
triggerChange = triggerChange or (triggerChange == nil and defaultTrigger)
local priorityWindow = nil
local meetingWindow = app:findWindow(WINDOW_TITLES.MEETING)
local sharingWindow = app:findWindow(WINDOW_TITLES.SHARING)
local webinarWindow = app:findWindow(WINDOW_TITLES.WEBINAR)
local mainWindow = app:findWindow(WINDOW_TITLES.MAIN)
if (meetingWindow ~= nil) then
_printInfo('set state to meeting')
_handleImproperState()
Zoom_State:startMeeting() -- this will not move from 'closed' to 'meeting' needs to go to 'running' first
priorityWindow = meetingWindow
elseif (sharingWindow ~= nil) then
_printInfo('set state to sharing')
_handleImproperState()
Zoom_State:startShare()
priorityWindow = sharingWindow
elseif (webinarWindow ~= nil) then
_printInfo('set state to webinar')
_handleImproperState()
Zoom_State:startMeeting()
priorityWindow = webinarWindow
elseif (mainWindow ~= nil) then
_printInfo('set state to running')
_handleImproperState()
Zoom_State:start()
priorityWindow = app:findWindow(WINDOW_TITLES.MAIN)
end
if (priorityWindow ~= nil) then
_printInfo('checking audio/video status')
obj.audio:status() -- idk if these status calls are needed, the results are never used
obj.video:status()
if (triggerChange) then _change() end -- this _change is slowing down my Zoom:focus() func
else
Zoom_State:stop()
end
return priorityWindow
end
return nil
end
-- Checks for the presence of a given menu item
function _check(tbl)
local app = _getZoomInstance()
if (app ~= nil) then
-- this seems to be a bottleneck, likely due to the menus taking forever to sift through
-- the chrome itemMenu took like 30 seconds to compile lol
return app:findMenuItem(tbl) ~= nil
end
end
-- Performs a "click" action on a given menu item
function _click(tbl)
local app = _getZoomInstance()
if (app ~= nil) then
_printInfo('clicking menu item')
return app:selectMenuItem(tbl)
end
end
-- Triggers the user-defined callback function
function _change()
if (obj.stateCallbackFunction ~= nil) then
_printInfo('triggering callback')
-- this pause is going to be very difficult to remove...
_pause(0.5) -- it would be nice to not have to delay. this also slows down non-audio status updates
obj.stateCallbackFunction(Zoom_State.current, obj.audio:status(), obj.video:status())
end
end
-- Process window title string
function _processWindowTitle(title, target)
return title:sub(1, #target)
end
-- Examines the current muted status of either audio or video
function _determineInputStatus(muteItem, unmuteItem)
local result = INPUT_STATES.OFF
if _check({MENU_ITEMS.MEETING.TOP, muteItem}) then
result = INPUT_STATES.UNMUTED
elseif _check({MENU_ITEMS.MEETING.TOP, unmuteItem}) then
result = INPUT_STATES.MUTED
end
return result
end
-- Performs a menu click to change a muted status
-- & triggers user-defined callback if click was successful
function _changeInputSetting(menuItem)
if _click({MENU_ITEMS.MEETING.TOP, menuItem}) then
_change()
end
end
-- Examines the current muted status of either audio or video
-- & performs a click to change to the opposite
function _toggleInputSetting(muteItem, unmuteItem)
local current = _determineInputStatus(muteItem, unmuteItem)
local itemToClick = muteItem
if current == INPUT_STATES.MUTED then
itemToClick = unmuteItem
end
_changeInputSetting(itemToClick)
end
--[[
--------------------------------------------------------
-- External API
--------------------------------------------------------
]]
function obj:start()
_printInfo('starting app watcher in spoon')
_start_zoom_state_watcher(_getZoomInstance())
app_watcher:start()
_determineZoomState()
end
function obj:stop()
app_watcher:stop()
_stop_zoom_state_watcher()
end
function obj:debug()
DEBUG_MODE = true
end
function obj:inMeeting()
return Zoom_State:is('meeting') or Zoom_State:is('sharing')
end
function obj:leaveMeeting()
local app = _getZoomInstance()
if (app ~= nil) then
local meetingWindow = app:findWindow(WINDOW_TITLES.MEETING)
if (meetingWindow ~= nil) then
_printInfo('ending meeting')
meetingWindow:close() -- this opens leave/end submenu
hs.eventtap.keyStroke({}, 'return') -- triggers end meeting option of submenu
end
end
end
-- Focuses on Zoom window, prioritising order: Meeting -> Sharing -> Webinar -> Main
-- Returns the hs.window object on successful focus
function obj:focus()
local window = _determineZoomState(false)
if (window ~= nil) then
return window:focus()
end
return nil
end
-- new feature: delete join zoom meeting tabs from chrome when going from-running-to-meeting
-------------------
-- spoon.Zoom.audio
-------------------
function obj.audio:status()
_printInfo('toggling audio')
return _determineInputStatus(MENU_ITEMS.MEETING.MUTE_AUDIO, MENU_ITEMS.MEETING.UNMUTE_AUDIO)
end
function obj.audio:mute()
return _changeInputSetting(MENU_ITEMS.MEETING.MUTE_AUDIO)
end
function obj.audio:unmute()
return _changeInputSetting(MENU_ITEMS.MEETING.UNMUTE_AUDIO)
end
function obj.audio:toggleMute()
return _toggleInputSetting(MENU_ITEMS.MEETING.MUTE_AUDIO, MENU_ITEMS.MEETING.UNMUTE_AUDIO)
end
-------------------
-- spoon.Zoom.video
-------------------
function obj.video:status()
return _determineInputStatus(MENU_ITEMS.MEETING.STOP_VIDEO, MENU_ITEMS.MEETING.START_VIDEO)
end
function obj.video:mute()
return _changeInputSetting(MENU_ITEMS.MEETING.STOP_VIDEO)
end
function obj.video:unmute()
return _changeInputSetting(MENU_ITEMS.MEETING.START_VIDEO)
end
function obj.video:toggleMute()
return _toggleInputSetting(MENU_ITEMS.MEETING.STOP_VIDEO, MENU_ITEMS.MEETING.START_VIDEO)
end
-------------------
-- spoon.Zoom.chat
-------------------
function obj.chat:open()
return _click({MENU_ITEMS.VIEW.TOP, MENU_ITEMS.VIEW.SHOW_CHAT})
end
function obj.chat:close()
return _click({MENU_ITEMS.VIEW.TOP, MENU_ITEMS.VIEW.CLOSE_CHAT})
end
-- function obj.chat:focus()
-- -- this needs either cleaned up or deleted...
-- local result = _click({MENU_ITEMS.VIEW.TOP, MENU_ITEMS.VIEW.SHOW_CHAT})
-- local app = _getZoomInstance()
-- if (app ~= nil) then
-- local chatWindow = app:findWindow(WINDOW_TITLES.CHAT)
-- if (chatWindow ~= nil) then
-- -- TODO: fucking zoom main window always steals focus...
-- return chatWindow:focus()
-- end
-- end
-- return result
-- end
-------------------
-- spoon.Zoom.participants
-------------------
-- TODO: get participant count
function obj.participants:open()
return _click({MENU_ITEMS.VIEW.TOP, MENU_ITEMS.VIEW.SHOW_PARTICIPANTS})
end
function obj.participants:close()
return _click({MENU_ITEMS.VIEW.TOP, MENU_ITEMS.VIEW.CLOSE_PARTICIPANTS})
end
-- function obj.participants:focus()
-- -- this needs either cleaned up or deleted...
-- local result = _click({MENU_ITEMS.VIEW.TOP, MENU_ITEMS.VIEW.SHOW_PARTICIPANTS})
-- local app = _getZoomInstance()
-- if (app ~= nil) then
-- local participantsWindow = app:findWindow(WINDOW_TITLES.PARTICIPANTS)
-- if (participantsWindow ~= nil) then
-- -- TODO: fucking zoom main window always steals focus...
-- return participantsWindow:focus()
-- end
-- end
-- return result
-- end
-------------------
-- spoon.Zoom.share
-------------------
function obj.share:stop()
return _click({MENU_ITEMS.MEETING.TOP, MENU_ITEMS.MEETING.STOP_SHARE})
end
function obj.share:showControls()
return _click({MENU_ITEMS.VIEW.TOP, MENU_ITEMS.VIEW.SHOW_SHARE_CONTROLS})
end
--[[
--------------------------------------------------------
-- User-Defined Callback Functions
--------------------------------------------------------
]]--
---------------------------------------------------------------------------------------------
-- Registers a function to be called whenever Zoom's state is changed or examined
-- Parameters:
-- func - A function in the form function(currentState, audioStatus, videoStatus)
-- currentState = a string representing the current state of the Zoom State Machine
-- audioStatus = a string representing the current Zoom Audio state
-- videoStatus = a string representing the current Zoom Video state
---------------------------------------------------------------------------------------------
function obj:setStatusCallback(func)
_printInfo('setting zoom state callback')
self.stateCallbackFunction = func
end
---------------------------------------------------------------------------------------------
-- Registers a function to be called whenever a Zoom state transition occurs
-- Parameters:
-- func - A function in the form function(stateTransition)
-- stateTransition = a string representing the state transition in the form: 'from-running-to-meeting'
---------------------------------------------------------------------------------------------
function obj:setTransitionCallback(func)
_printInfo('setting zoom transition callback')
self.transitionCallbackFunction = func
end
--[[
--------------------------------------------------------
-- Spoon Delivery
--------------------------------------------------------
]]
return obj