Skip to content

Commit fe99e82

Browse files
committed
mod_xrandr: use local scope where appropriate (fixes #212)
1 parent c41baad commit fe99e82

File tree

3 files changed

+35
-34
lines changed

3 files changed

+35
-34
lines changed

mod_xrandr/cfg_xrandr.lua

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
-- physical screen that a workspace wants to be on, is part of the C class
33
-- WGroupWS. For "full screen workspaces" and scratchpads, we only keep this
44
-- information in a temporary list.
5-
InitialOutputs={}
5+
local InitialOutputs={}
66

7-
function getInitialOutputs(ws)
7+
local function getInitialOutputs(ws)
88
if obj_is(ws, "WGroupCW") or is_scratchpad(ws) then
99
return InitialOutputs[ws:name()]
1010
elseif obj_is(ws, "WGroupWS") then
@@ -14,22 +14,22 @@ function getInitialOutputs(ws)
1414
end
1515
end
1616

17-
function setInitialOutputs(ws, outputs)
17+
local function setInitialOutputs(ws, outputs)
1818
if obj_is(ws, "WGroupCW") or is_scratchpad(ws) then
1919
InitialOutputs[ws:name()] = outputs
2020
elseif obj_is(ws, "WGroupWS") then
2121
WGroupWS.set_initial_outputs(ws, outputs)
2222
end
2323
end
2424

25-
function nilOrEmpty(t)
25+
local function nilOrEmpty(t)
2626
return not t or empty(t)
2727
end
2828

2929
function mod_xrandr.workspace_added(ws)
3030
if nilOrEmpty(getInitialOutputs(ws)) then
31-
outputs = mod_xrandr.get_outputs(ws:screen_of(ws))
32-
outputKeys = {}
31+
local outputs = mod_xrandr.get_outputs(ws:screen_of(ws))
32+
local outputKeys = {}
3333
for k,v in pairs(outputs) do
3434
table.insert(outputKeys, k)
3535
end
@@ -38,7 +38,7 @@ function mod_xrandr.workspace_added(ws)
3838
return true
3939
end
4040

41-
function for_all_workspaces_do(fn)
41+
local function for_all_workspaces_do(fn)
4242
local workspaces={}
4343
notioncore.region_i(function(scr)
4444
scr:managed_i(function(ws)
@@ -62,15 +62,17 @@ function mod_xrandr.screenmanagedchanged(tab)
6262
end
6363
end
6464

65-
screen_managed_changed_hook = notioncore.get_hook('screen_managed_changed_hook')
65+
local screen_managed_changed_hook = notioncore.get_hook('screen_managed_changed_hook')
6666
if screen_managed_changed_hook then
6767
screen_managed_changed_hook:add(mod_xrandr.screenmanagedchanged)
6868
end
6969

70-
post_layout_setup_hook = notioncore.get_hook('ioncore_post_layout_setup_hook')
71-
post_layout_setup_hook:add(mod_xrandr.workspaces_added)
70+
local post_layout_setup_hook = notioncore.get_hook('ioncore_post_layout_setup_hook')
71+
if post_layout_setup_hook then
72+
post_layout_setup_hook:add(mod_xrandr.workspaces_added)
73+
end
7274

73-
function add_safe(t, key, value)
75+
local function add_safe(t, key, value)
7476
if t[key] == nil then
7577
t[key] = {}
7678
end
@@ -80,10 +82,10 @@ end
8082

8183
-- parameter: list of output names
8284
-- returns: map from screen name to screen
83-
function candidate_screens_for_output(max_screen_id, all_outputs, outputname)
85+
local function candidate_screens_for_output(max_screen_id, all_outputs, outputname)
8486
local retval = {}
8587

86-
function addIfContainsOutput(screen)
88+
local function addIfContainsOutput(screen)
8789
local outputs_within_screen = mod_xrandr.get_outputs_within(all_outputs, screen)
8890
if screen:id() <= max_screen_id and outputs_within_screen[outputname] ~= nil then
8991
retval[screen:name()] = screen
@@ -97,7 +99,7 @@ end
9799

98100
-- parameter: maximum screen id, list of all output names, list of output names for which we want the screens
99101
-- returns: map from screen name to screen
100-
function candidate_screens_for_outputs(max_screen_id, all_outputs, outputnames)
102+
local function candidate_screens_for_outputs(max_screen_id, all_outputs, outputnames)
101103
local result = {}
102104

103105
if outputnames == nil then return result end
@@ -111,30 +113,30 @@ function candidate_screens_for_outputs(max_screen_id, all_outputs, outputnames)
111113
return result;
112114
end
113115

114-
function firstValue(t)
116+
local function firstValue(t)
115117
local key, value = next(t)
116118
return value
117119
end
118120

119-
function firstKey(t)
121+
local function firstKey(t)
120122
local key, value = next(t)
121123
return key
122124
end
123125

124-
function empty(t)
126+
local function empty(t)
125127
return not next(t)
126128
end
127129

128-
function singleton(t)
130+
local function singleton(t)
129131
local first = next(t)
130132
return first and not next(t, first)
131133
end
132134

133-
function is_scratchpad(ws)
135+
local function is_scratchpad(ws)
134136
return package.loaded["mod_sp"] and mod_sp.is_scratchpad(ws)
135137
end
136138

137-
function find_scratchpad(screen)
139+
local function find_scratchpad(screen)
138140
local sp
139141
screen:managed_i(function(ws)
140142
if is_scratchpad(ws) then
@@ -147,7 +149,7 @@ function find_scratchpad(screen)
147149
return sp
148150
end
149151

150-
function move_if_needed(workspace, screen_id)
152+
local function move_if_needed(workspace, screen_id)
151153
local screen = notioncore.find_screen_id(screen_id)
152154

153155
if workspace:screen_of() ~= screen then
@@ -173,21 +175,22 @@ end
173175
-- Arrange the workspaces over the first number_of_screens screens
174176
function mod_xrandr.rearrangeworkspaces(max_screen_id)
175177
-- for each screen id, which workspaces should be on that screen
176-
new_mapping = {}
178+
local new_mapping = {}
177179
-- workspaces that want to be on an output that's currently not on any screen
178-
orphans = {}
180+
local orphans = {}
179181
-- workspaces that want to be on multiple available outputs
180-
wanderers = {}
182+
local wanderers = {}
181183

182184
local all_outputs = mod_xrandr.get_all_outputs()
183185

184186
-- When moving a "full screen workspace" to another screen, we seem to lose
185187
-- its placeholder and thereby the possibility to return it from full
186188
-- screen later. Let's therefore try to close any full screen workspace
187189
-- before rearranging.
188-
full_screen_workspaces={}
190+
local full_screen_workspaces={}
189191
for_all_workspaces_do(function(ws)
190-
if obj_is(ws, "WGroupCW") then table.insert(full_screen_workspaces, ws)
192+
if obj_is(ws, "WGroupCW") then
193+
table.insert(full_screen_workspaces, ws)
191194
end
192195
return true
193196
end)
@@ -197,7 +200,7 @@ function mod_xrandr.rearrangeworkspaces(max_screen_id)
197200

198201
-- round one: divide workspaces in directly assignable,
199202
-- orphans and wanderers
200-
function roundone(workspace)
203+
for_all_workspaces_do(function(workspace)
201204
local screens = candidate_screens_for_outputs(max_screen_id, all_outputs, getInitialOutputs(workspace))
202205
if nilOrEmpty(screens) then
203206
table.insert(orphans, workspace)
@@ -207,8 +210,7 @@ function mod_xrandr.rearrangeworkspaces(max_screen_id)
207210
wanderers[workspace] = screens
208211
end
209212
return true
210-
end
211-
for_all_workspaces_do(roundone)
213+
end)
212214

213215
for workspace,screens in pairs(wanderers) do
214216
-- TODO add to screen with least # of workspaces instead of just the
@@ -255,8 +257,7 @@ function mod_xrandr.screenlayoutupdated()
255257
notioncore.profiling_stop()
256258
end
257259

258-
randr_screen_change_notify_hook = notioncore.get_hook('randr_screen_change_notify')
259-
260+
local randr_screen_change_notify_hook = notioncore.get_hook('randr_screen_change_notify')
260261
if randr_screen_change_notify_hook then
261262
randr_screen_change_notify_hook:add(mod_xrandr.screenlayoutupdated)
262263
end

mod_xrandr/mod_xrandr.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ if not package.loaded["mod_xinerama"] then
1414
dopath("mod_xinerama")
1515
end
1616

17-
function filter(t, predicate)
17+
local function filter(t, predicate)
1818
local result = {}
1919
for k,v in pairs(t) do
2020
if predicate(v) then

mod_xrandr/test_xrandr.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ local secondscreen = {
1010
id = function() return 1 end
1111
}
1212

13-
notioncore = {
13+
local notioncore = {
1414
load_module = function() return 1 end,
1515
get_hook = function() return { add = function() end; } end,
1616
region_i = function(callback, type)
@@ -19,7 +19,7 @@ notioncore = {
1919
end
2020
}
2121

22-
function dopath()
22+
local function dopath()
2323
end
2424

2525
local all_outputs = { VGA1 = { x=0, y=0, w=1680, h=1050, name='VGA1' }, LVDS1 = { x=1680, y=0, w=1920, h=1080, name='LVDS1' } }

0 commit comments

Comments
 (0)