1# -*- coding: utf-8 -*-
2
3import obspython as obs
4import asyncio
5import websockets
6import threading
7
8hotkey_id_first = None
9hotkey_id_prev = None
10hotkey_id_next = None
11hotkey_id_last = None
12hotkey_id_black = None
13hotkey_id_white = None
14
15HOTKEY_NAME_FIRST = 'powerpoint_slides.first'
16HOTKEY_NAME_PREV = 'powerpoint_slides.previous'
17HOTKEY_NAME_NEXT = 'powerpoint_slides.next'
18HOTKEY_NAME_LAST = 'powerpoint_slides.last'
19HOTKEY_NAME_BLACK = 'powerpoint_slides.black'
20HOTKEY_NAME_WHITE = 'powerpoint_slides.white'
21
22HOTKEY_DESC_FIRST = 'First PowerPoint slide'
23HOTKEY_DESC_PREV = 'Previous PowerPoint slide'
24HOTKEY_DESC_NEXT = 'Next PowerPoint slide'
25HOTKEY_DESC_LAST = 'Last PowerPoint slide'
26HOTKEY_DESC_BLACK = 'Black PowerPoint slide'
27HOTKEY_DESC_WHITE = 'White PowerPoint slide'
28
29global cmd
30cmd = ""
31
32async def communicate():
33 global cmd
34 async with websockets.connect("ws://10.0.0.93:5678", ping_interval=None) as websocket:
35 while True:
36 if cmd:
37 await websocket.send("{\"action\": \"" + cmd + "\"}")
38 cmd = ""
39 await asyncio.sleep(0.05)
40
41def run_ws():
42 asyncio.set_event_loop(asyncio.new_event_loop())
43 asyncio.get_event_loop().run_until_complete(communicate())
44
45
46
47#------------------------------------------------------------
48# global functions for script plugins
49
50def script_load(settings):
51 global hotkey_id_first
52 global hotkey_id_prev
53 global hotkey_id_next
54 global hotkey_id_last
55 global hotkey_id_black
56 global hotkey_id_white
57
58 hotkey_id_first = register_and_load_hotkey(settings, HOTKEY_NAME_FIRST, HOTKEY_DESC_FIRST, first_slide)
59 hotkey_id_prev = register_and_load_hotkey(settings, HOTKEY_NAME_PREV, HOTKEY_DESC_PREV, prev_slide)
60 hotkey_id_next = register_and_load_hotkey(settings, HOTKEY_NAME_NEXT, HOTKEY_DESC_NEXT, next_slide)
61 hotkey_id_last = register_and_load_hotkey(settings, HOTKEY_NAME_LAST, HOTKEY_DESC_LAST, last_slide)
62 hotkey_id_black = register_and_load_hotkey(settings, HOTKEY_NAME_BLACK, HOTKEY_DESC_BLACK, black)
63 hotkey_id_white = register_and_load_hotkey(settings, HOTKEY_NAME_WHITE, HOTKEY_DESC_WHITE, white)
64 ws_daemon = threading.Thread(name="ws_daemon", target=run_ws)
65 ws_daemon.setDaemon(True)
66 ws_daemon.start()
67 print("Started websocket client")
68
69def script_unload():
70 obs.obs_hotkey_unregister(first_slide)
71 obs.obs_hotkey_unregister(prev_slide)
72 obs.obs_hotkey_unregister(next_slide)
73 obs.obs_hotkey_unregister(last_slide)
74 obs.obs_hotkey_unregister(black)
75 obs.obs_hotkey_unregister(white)
76
77def script_save(settings):
78 save_hotkey(settings, HOTKEY_NAME_FIRST, hotkey_id_first)
79 save_hotkey(settings, HOTKEY_NAME_PREV, hotkey_id_prev)
80 save_hotkey(settings, HOTKEY_NAME_NEXT, hotkey_id_next)
81 save_hotkey(settings, HOTKEY_NAME_LAST, hotkey_id_last)
82 save_hotkey(settings, HOTKEY_NAME_BLACK, hotkey_id_black)
83 save_hotkey(settings, HOTKEY_NAME_WHITE, hotkey_id_white)
84
85def script_description():
86 return "ppt-control client\nHotkeys for controlling PowerPoint slides using websockets"
87
88def script_defaults(settings):
89 obs.obs_data_set_default_int(settings, 'port', 5678)
90
91def script_properties():
92 props = obs.obs_properties_create()
93
94 obs.obs_properties_add_int(props, "port", "Websocket port: ", 0, 9999, 1)
95 return props
96
97def script_update(settings):
98 global port
99 port = obs.obs_data_get_int(settings, "port")
100
101def register_and_load_hotkey(settings, name, description, callback):
102 hotkey_id = obs.obs_hotkey_register_frontend(name, description, callback)
103 hotkey_save_array = obs.obs_data_get_array(settings, name)
104 obs.obs_hotkey_load(hotkey_id, hotkey_save_array)
105 obs.obs_data_array_release(hotkey_save_array)
106
107 return hotkey_id
108
109def save_hotkey(settings, name, hotkey_id):
110 hotkey_save_array = obs.obs_hotkey_save(hotkey_id)
111 obs.obs_data_set_array(settings, name, hotkey_save_array)
112 obs.obs_data_array_release(hotkey_save_array)
113
114#-------------------------------------
115
116def first_slide(pressed):
117 if pressed:
118 global cmd
119 cmd = "first"
120
121def prev_slide(pressed):
122 if pressed:
123 global cmd
124 cmd = "prev"
125
126def next_slide(pressed):
127 if pressed:
128 global cmd
129 cmd = "next"
130
131def last_slide(pressed):
132 if pressed:
133 global cmd
134 cmd = "last"
135
136def black(pressed):
137 if pressed:
138 global cmd
139 cmd = "black"
140
141def white(pressed):
142 if pressed:
143 global cmd
144 cmd = "white"