-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdriver.lua
278 lines (262 loc) · 10.2 KB
/
driver.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
-------------
-- Globals --
-------------
do
EC = {}
OPC = {}
PRESETS = {
["None"] = {
icon = "default"
},
["Google"] = {
url = "https://www.google.com",
icon = "google"
},
["Google Calendar"] = {
url = "https://www.google.com/calendar",
icon = "calendar"
},
["Google News"] = {
url = "https://news.google.com",
icon = "news"
},
["Weather.com"] = {
url = "https://weather.com",
icon = "weather"
}
}
g_debugMode = 0
g_DbgPrint = nil
g_AutoRename = false
g_lastName = "WebView"
end
----------------------------------------------------------------------------
--Function Name : OnDriverInit
--Description : Function invoked when a driver is loaded or being updated.
----------------------------------------------------------------------------
function OnDriverInit()
C4:UpdateProperty("Driver Name", C4:GetDriverConfigInfo("name"))
C4:UpdateProperty("Driver Version", C4:GetDriverConfigInfo("version"))
C4:AllowExecute(true)
end
------------------------------------------------------------------------------------------------
--Function Name : OnDriverLateInit
--Description : Function that serves as a callback into a project after the project is loaded.
------------------------------------------------------------------------------------------------
function OnDriverLateInit()
for k,v in pairs(Properties) do OnPropertyChanged(k) end
PersistData = PersistData or {}
if (PersistData.URL ~= nil) then C4:UpdateProperty("URL", PersistData.URL) end
C4:SetTimer(3000, function(timer)
C4:SendToProxy(5001, "URL_CHANGED", {url = Properties["URL"]})
setPresetIcon(Properties["Presets"])
if (g_AutoRename) then setPresetName(Properties["Presets"]) end
end)
end
-----------------------------------------------------------------------------------------------------------------------------
--Function Name : OnDriverDestroyed
--Description : Function called when a driver is deleted from a project, updated within a project or Director is shut down.
-----------------------------------------------------------------------------------------------------------------------------
function OnDriverDestroyed()
if (g_DbgPrint ~= nil) then g_DbgPrint:Cancel() end
end
----------------------------------------------------------------------------
--Function Name : OnPropertyChanged
--Parameters : strProperty(str)
--Description : Function called by Director when a property changes value.
----------------------------------------------------------------------------
function OnPropertyChanged(strProperty)
Dbg("OnPropertyChanged: " .. strProperty .. " (" .. Properties[strProperty] .. ")")
local propertyValue = Properties[strProperty]
if (propertyValue == nil) then propertyValue = '' end
local strProperty = string.upper(strProperty)
strProperty = string.gsub(strProperty, "%s+", "_")
local success, ret
if (OPC and OPC[strProperty] and type(OPC[strProperty]) == "function") then
success, ret = pcall(OPC[strProperty], propertyValue)
end
if (success == true) then
return (ret)
elseif (success == false) then
print ("OnPropertyChanged Lua error: ", strProperty, ret)
end
end
-------------------------------------------------------------------------
--Function Name : OPC.DEBUG_MODE
--Parameters : strProperty(str)
--Description : Function called when Debug Mode property changes value.
-------------------------------------------------------------------------
function OPC.DEBUG_MODE(strProperty)
if (strProperty == "Off") then
if (g_DbgPrint ~= nil) then g_DbgPrint:Cancel() end
g_debugMode = 0
print ("Debug Mode: Off")
else
g_debugMode = 1
print ("Debug Mode: On for 8 hours")
g_DbgPrint = C4:SetTimer(28800000, function(timer)
C4:UpdateProperty("Debug Mode", "Off")
timer:Cancel()
end, false)
end
end
------------------------------------------------------------------
--Function Name : OPC.URL
--Parameters : strProperty(str)
--Description : Function called when URL property changes value.
------------------------------------------------------------------
function OPC.URL(strProperty)
C4:SendToProxy(5001, "URL_CHANGED", {url = strProperty})
PersistData.URL = strProperty
end
----------------------------------------------------------------------
--Function Name : OPC.PRESETS
--Parameters : strProperty(str)
--Description : Function called when Presets property changes value.
----------------------------------------------------------------------
function OPC.PRESETS(strProperty)
setPresetIcon(strProperty)
setPresetURL(strProperty)
if (g_AutoRename) then setPresetName(Properties["Presets"]) end
end
---------------------------------------------------------------------------------
--Function Name : OPC.AUTO_RENAME_DRIVER
--Parameters : strProperty(str)
--Description : Function called when Auto Rename Driver property changes value.
---------------------------------------------------------------------------------
function OPC.AUTO_RENAME_DRIVER(strProperty)
if (strProperty == "On") then
g_AutoRename = true
setPresetName(Properties["Presets"])
else
g_AutoRename = false
end
end
-----------------------------------------------------------------------------------------------------
--Function Name : ExecuteCommand
--Parameters : strCommand(str), tParams(table)
--Description : Function called by Director when a command is received for this DriverWorks driver.
-----------------------------------------------------------------------------------------------------
function ExecuteCommand(strCommand, tParams)
tParams = tParams or {}
Dbg("ExecuteCommand: " .. strCommand .. " (" .. formatParams(tParams) .. ")")
if (strCommand == 'LUA_ACTION') then
if (tParams.ACTION) then
strCommand = tParams.ACTION
tParams.ACTION = nil
end
end
local strCommand = string.upper(strCommand)
strCommand = string.gsub(strCommand, "%s+", "_")
local success, ret
if (EC and EC[strCommand] and type(EC[strCommand]) == "function") then
success, ret = pcall(EC[strCommand], tParams)
end
if (success == true) then
return (ret)
elseif (success == false) then
print ("ExecuteCommand Lua error: ", strCommand, ret)
end
end
----------------------------------------------------------------------------
--Function Name : EC.SET_URL
--Parameters : tParams(table)
--Description : Function called when "Set URL" ExecuteCommand is received.
----------------------------------------------------------------------------
function EC.SET_URL(tParams)
Dbg("[EC] Set URL (" .. formatParams(tParams) .. ")")
if (tParams.URL ~= nil) then
C4:SendToProxy(5001, "URL_CHANGED", {url = tParams.URL})
C4:UpdateProperty("URL", tParams.URL)
end
end
-------------------------------------------------------------------------------
--Function Name : EC.SET_PRESET
--Parameters : tParams(table)
--Description : Function called when "Set Preset" ExecuteCommand is received.
-------------------------------------------------------------------------------
function EC.SET_PRESET(tParams)
Dbg("[EC] Set PRESET (" .. formatParams(tParams) .. ")")
if (tParams.PRESET ~= nil) then
local strPreset = tParams.PRESET
setPresetIcon(Properties[strPreset])
setPresetURL(Properties[strPreset])
if (g_AutoRename) then setPresetName(Properties["Presets"]) end
end
end
----------------------------------------------------------------------------------------------
--Function Name : setPresetIcon
--Parameters : strPreset(str)
--Description : Function called to set the Icon for the UI button based on Preset Selection.
----------------------------------------------------------------------------------------------
function setPresetIcon(strPreset)
if (strPreset ~= nil) then
local strIcon = PRESETS[strPreset]["icon"]
C4:SendToProxy(5001, "ICON_CHANGED", {icon = strIcon})
end
end
---------------------------------------------------------------------------------------------
--Function Name : setPresetURL
--Parameters : strPreset(str)
--Description : Function called to set the URL for the UI button based on Preset Selection.
---------------------------------------------------------------------------------------------
function setPresetURL(strPreset)
if (strPreset ~= "None") then
local newUrl = PRESETS[strPreset]["url"]
C4:SendToProxy(5001, "URL_CHANGED", {url = newUrl})
C4:UpdateProperty("URL", newUrl)
end
end
-------------------------------------------------------------------------------------------
--Function Name : setPresetURL
--Parameters : strPreset(str)
--Description : Function called to set the Name for the Driver based on Preset Selection.
-------------------------------------------------------------------------------------------
function setPresetName(strPreset)
if (g_AutoRename == false) then return end
local lastName = g_lastName or "WebView"
local newName = strPreset
local proxyId = C4:GetProxyDevices()
if (strPreset ~= "None") then
if (newName ~= lastName) then
Dbg("Current Driver Name: " .. lastName .. " | Renaming to: " ..newName)
C4:RenameDevice(proxyId, newName)
g_lastName = newName
else
Dbg("Not renaming device, already same name: " .. newName)
end
else
if (newName ~= lastName) then
Dbg("Current Driver Name: " .. lastName .. " | Renaming to: WebView")
C4:RenameDevice(proxyId, "WebView")
g_lastName = "WebView"
else
Dbg("Not renaming device, already same name: WebView")
end
end
end
---------------------------------------------------------------------------------------------
--Function Name : Dbg
--Parameters : strDebugText(str)
--Description : Function called when debug information is to be printed/logged (if enabled)
---------------------------------------------------------------------------------------------
function Dbg(strDebugText)
if (g_debugMode == 1) then print(strDebugText) end
end
---------------------------------------------------------
--Function Name : formatParams
--Parameters : tParams(table)
--Description : Function called to format table params.
---------------------------------------------------------
function formatParams(tParams)
tParams = tParams or {}
local out = {}
for k,v in pairs(tParams) do
if (type(v) == "string") then
table.insert(out, k .. " = \"" .. v .. "\"")
else
table.insert(out, k .. " = " .. tostring(v))
end
end
return "{" .. table.concat(out, ", ") .. "}"
end