Skip to content

Commit c891420

Browse files
Merge pull request #22 from BlankSourceCode/prevent-zero-onload
Prevent zero size when loading
2 parents 23b27d9 + 75df67e commit c891420

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Prevent uploading ljsocket.lua into this repo by accident
2+
ljsocket.lua

obs-zoom-to-mouse.lua

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ local monitor_override_sy = 0
6464
local monitor_override_dw = 0
6565
local monitor_override_dh = 0
6666
local debug_logs = false
67+
local is_obs_loaded = false
68+
local is_script_loaded = false
6769

6870
local ZoomState = {
6971
None = 0,
@@ -74,7 +76,9 @@ local ZoomState = {
7476
local zoom_state = ZoomState.None
7577

7678
local version = obs.obs_get_version_string()
77-
local major = tonumber(version:match("(%d+%.%d+)")) or 0
79+
local m1, m2 = version:match("(%d+%.%d+)%.(%d+)")
80+
local major = tonumber(m1) or 0
81+
local minor = tonumber(m2) or 0
7882

7983
-- Define the mouse cursor functions for each platform
8084
if ffi.os == "Windows" then
@@ -589,12 +593,14 @@ function refresh_sceneitem(find_newest)
589593
end
590594

591595
if source_width == 0 or source_height == 0 then
592-
log("ERROR: Something went wrong determining source size." ..
593-
" Try using the 'Set manual source position' option and adding override values")
594-
595-
if monitor_info ~= nil then
596+
if monitor_info ~= nil and monitor_info.width > 0 and monitor_info.height > 0 then
597+
log("WARNING: Something went wrong determining source size.\n" ..
598+
" Using source size from info: " .. monitor_info.width .. ", " .. monitor_info.height)
596599
source_width = monitor_info.width
597600
source_height = monitor_info.height
601+
else
602+
log("ERROR: Something went wrong determining source size.\n" ..
603+
" Try using the 'Set manual source position' option and adding override values")
598604
end
599605
else
600606
log("Using source size: " .. source_width .. ", " .. source_height)
@@ -999,16 +1005,34 @@ end
9991005

10001006
function on_frontend_event(event)
10011007
if event == obs.OBS_FRONTEND_EVENT_SCENE_CHANGED then
1002-
log("Scene changed")
1008+
log("OBS Scene changed")
10031009
-- If the scene changes we attempt to find a new source with the same name in this new scene
10041010
-- TODO: There probably needs to be a way for users to specify what source they want to use in each scene
1011+
-- Scene change can happen before OBS has completely loaded, so we check for that here
1012+
if is_obs_loaded then
1013+
refresh_sceneitem(true)
1014+
end
1015+
elseif event == obs.OBS_FRONTEND_EVENT_FINISHED_LOADING then
1016+
log("OBS Loaded")
1017+
-- Once loaded we perform our initial lookup
1018+
is_obs_loaded = true
1019+
monitor_info = get_monitor_info(source)
10051020
refresh_sceneitem(true)
1021+
elseif event == obs.OBS_FRONTEND_EVENT_SCRIPTING_SHUTDOWN then
1022+
log("OBS Shutting down")
1023+
-- Add a fail-safe for unloading the script during shutdown
1024+
if is_script_loaded then
1025+
script_unload()
1026+
end
10061027
end
10071028
end
10081029

10091030
function on_update_transform()
10101031
-- Update the crop/size settings based on whatever the source in the current scene looks like
1011-
refresh_sceneitem(true)
1032+
if is_obs_loaded then
1033+
refresh_sceneitem(true)
1034+
end
1035+
10121036
return true
10131037
end
10141038

@@ -1064,7 +1088,8 @@ function log_current_settings()
10641088
debug_logs = debug_logs
10651089
}
10661090

1067-
log("OBS Version: " .. string.format("%.1f", major))
1091+
log("OBS Version: " .. string.format("%.1f", major) .. "." .. minor)
1092+
log("Platform: " .. ffi.os)
10681093
log("Current settings:")
10691094
log(format_table(settings))
10701095
end
@@ -1250,11 +1275,16 @@ function script_load(settings)
12501275
log("ERROR: Could not get X11 Display for Linux\n" ..
12511276
"Mouse position will be incorrect.")
12521277
end
1278+
1279+
source_name = ""
1280+
is_script_loaded = true
12531281
end
12541282

12551283
function script_unload()
1284+
is_script_loaded = false
1285+
12561286
-- Clean up the memory usage
1257-
if major > 29.0 then -- 29.0 seems to crash if you do this, so we ignore it as the script is closing anyway
1287+
if major > 29.1 or (major == 29.1 and minor > 2) then -- 29.1.2 and below seems to crash if you do this, so we ignore it as the script is closing anyway
12581288
local transitions = obs.obs_frontend_get_transitions()
12591289
if transitions ~= nil then
12601290
for i, s in pairs(transitions) do
@@ -1272,6 +1302,8 @@ function script_unload()
12721302

12731303
if x11_lib ~= nil and x11_display ~= nil then
12741304
x11_lib.XCloseDisplay(x11_display)
1305+
x11_display = nil
1306+
x11_lib = nil
12751307
end
12761308
end
12771309

@@ -1348,7 +1380,7 @@ function script_update(settings)
13481380
debug_logs = obs.obs_data_get_bool(settings, "debug_logs")
13491381

13501382
-- Only do the expensive refresh if the user selected a new source
1351-
if source_name ~= old_source_name then
1383+
if source_name ~= old_source_name and is_obs_loaded then
13521384
refresh_sceneitem(true)
13531385
end
13541386

@@ -1363,7 +1395,9 @@ function script_update(settings)
13631395
monitor_override_sy ~= old_sy or
13641396
monitor_override_w ~= old_dw or
13651397
monitor_override_h ~= old_dh then
1366-
monitor_info = get_monitor_info(source)
1398+
if is_obs_loaded then
1399+
monitor_info = get_monitor_info(source)
1400+
end
13671401
end
13681402
end
13691403

@@ -1384,4 +1418,4 @@ function populate_zoom_sources(list)
13841418

13851419
obs.source_list_release(sources)
13861420
end
1387-
end
1421+
end

0 commit comments

Comments
 (0)