forked from dluksza/screenful
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreenful.lua
More file actions
166 lines (140 loc) · 4.2 KB
/
screenful.lua
File metadata and controls
166 lines (140 loc) · 4.2 KB
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
----------------------------------------------------------------------------
---- @author dluksza <dariusz@luksza.org>
---- @copyright 2012 dluksza
------------------------------------------------------------------------------
-- Package envronment
local naughty = require('naughty')
local card = 'card0'
local dev = '/sys/class/drm/'
local configPath = '.config/awesome/screens_db.lua'
local outputMapping = {
['DP-1'] = 'DP1',
['DP-2'] = 'DP2',
['VGA-1'] = 'VGA1',
['LVDS-1'] = 'LVDS1',
['HDMI-A-1'] = 'HDMI1',
['HDMI-A-2'] = 'HDMI2'
}
local function log(text)
naughty.notify({
title = 'screenful debug',
text = text,
ontop = true,
preset = naughty.config.presets.critical
})
local log = io.open('/tmp/log.txt', 'aw')
log:write(text)
log:flush()
log:close()
end
local function isOutputConnected(path)
local status = io.open(path .. '/status', 'r')
local value = status:read('*all')
return 'connected\n' == value
end
local function connectedOutputs(path, card)
local result = {}
local outputs = io.popen('ls -1 -d ' .. path .. '/' .. card .. '-*')
while true do
local output = outputs:read('*line')
if not output then break end
if isOutputConnected(output) then
result[output] = true
end
end
return result
end
local function getScreenId(output)
local screenId = ''
local edid = io.open(output .. '/edid', 'rb')
local id = edid:read('*all')
for i = 12, 17 do
code = id:byte(i)
if code then
screenId = screenId .. code
end
end
return screenId
end
local function getXrandrOutput(outputPath, outCard)
local regex = dev .. outCard .. '/' .. outCard .. '[-]'
local drmName = string.gsub(outputPath, regex, '')
return outputMapping[drmName]
end
local function mergeTables(table1, table2)
local result = {}
for k,v in pairs(table1) do
result[k] = v
end
for k,v in pairs(table2) do
result[k] = v
end
return result
end
local function hasConfigurationFor(screenId)
local file = io.open(configPath, 'r')
local conf = file:read('*all')
file:close()
return string.find(conf, "['\"]" .. screenId .. "['\"]")
end
local function appendConfiguration(screenId)
local file = io.open(configPath, 'a')
file:write("--\t['" .. screenId .. "'] = {\n")
file:write("--\t\t['connected'] = function ()\n")
file:write("--\t\t\treturn nil\n")
file:write("--\t\tend,\n")
file:write("--\t\t['disconnected'] = function ()\n")
file:write("--\t\t\treturn nil\n")
file:write("--\t\tend\n")
file:write("--\t}\n")
file:flush()
file:close()
end
local function setupScreen(xrandrParams)
os.execute('xrandr ' .. xrandrParams)
end
local function performConfiguredAction(screenId, action, xrandrOut)
require('screens_db')
local xrandrOpts = ''
local configuration = screens[screenId]
if configuration then
if configuration[action] then -- get xrandr options
xrandrOpts = configuration[action](xrandrOut)
end
else -- configuration not found, append configuration template
if tostring(screenId):len() ~= 0 and not hasConfigurationFor(screenId) then
naughty.notify({text = 'Append new configuration for screen id: ' .. screenId})
appendConfiguration(screenId)
end
end
if xrandrOpts:len() == 0 then -- use default configuration if specific was not found
xrandrOpts = screens['default'][action](xrandrOut)
end
setupScreen(xrandrOpts)
end
local function disableOutput(out, changedCard)
local xrandrOut = getXrandrOutput(out, changedCard)
local screenId = getScreenId(out)
performConfiguredAction(screenId, 'disconnected', xrandrOut)
naughty.notify({ text='Output ' .. xrandrOut .. ' disconnected' })
end
local function enableOutput(out, changedCard)
local xrandrOut = getXrandrOutput(out, changedCard)
local screenId = getScreenId(out)
performConfiguredAction(screenId, 'connected', xrandrOut)
end
local cardDev = dev .. card
local outputs = connectedOutputs(cardDev, card)
function updateScreens(changedCard)
local newCardDev = dev .. changedCard
local newOutputs = connectedOutputs(newCardDev, changedCard)
local mergedOutputs = mergeTables(outputs, newOutputs)
for out in pairs(mergedOutputs) do
if not outputs[out] then -- connected
enableOutput(out, changedCard)
elseif not newOutputs[out] then -- disconnected
disableOutput(out, changedCard)
end
end
outputs = newOutputs
end