-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
208 lines (149 loc) · 4.72 KB
/
main.lua
File metadata and controls
208 lines (149 loc) · 4.72 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
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
function HandleShellSignals()
-- if we are talking to a shell in a pty (we are in xterm or terminal mode) then
-- there are signals that we must propgate to the pty
if shell ~= nil
then
if process.SIGWINCH ~= nil and process.sigcheck(process.SIGWINCH)
then
shell:ptysize(term:width(), term:length() - settings.steal_lines)
end
if process.SIGINT ~= nil and process.sigcheck(process.SIGINT)
then
shell:write("\x03",1)
end
end
end
function HandleExitedChildProcesses()
-- if any child processes have exited, then collect them here
if process.collect ~= nil
then
process.collect()
else
-- old function call, will go away eventually
process.childExited(-1)
end
end
function ApplicationSetup()
-- assume our output device, whatever it is, can support unicode UTF8
terminal.utf8(3)
-- load some initial settings defaults
SettingsInit()
-- load any modules that extend functionality
LoadModules()
process.configure("mdwe security=untrusted")
display:init()
onclicks:init()
--DataSockAdd(settings.datasock)
Out=OpenOutput(settings)
if settings.output == "term"
then
if strutil.strlen(settings.foreground) > 0
then
settings.term_foreground=TranslateColorName(settings.foreground)
end
if strutil.strlen(settings.background) > 0
then
settings.term_background=string.upper(TranslateColorName(settings.background))
end
end
last_time=0
end
function UpdateDisplay()
last_time=now
str=display:substitute_values(settings)
str=TranslateColorStrings(settings, str)
str=terminal.format(str)
display_update_required=false
-- dwm uses the 'name' value of the root window as it's input, so we have to set that
if settings.output == "dwm"
then
os.execute("xsetroot -name '"..str.."'")
-- for other 'bar' programs we write to standard out
else
Out:writeln(str)
Out:flush()
end
lookup_counter=lookup_counter+1
end
-- this function reads data from the next active datastream and processes it
function ProcessStreams()
local S, str
S=poll_streams:select(100)
-- io.stderr:write("SELECT: "..tostring(S).."\n")
if S ~= nil
then
-- if we are running as a bar within a terminal or xterm then we
-- need to pass keystrokes through to the shell that's being displayed
-- along with our bar on that terminal
if S==stdio
then
shell:write(stdio:getch(), 1)
elseif S==shell
then
-- if we are running as a bar within a terminal then we read
-- bytes from the shell running in that termina and transfer
-- them to the screen. There are two special cases SHELL_CLOSED
-- and SHELL_CLS (clear screen) which must be handled here
shell_result=TerminalReadFromPty()
if shell_result==SHELL_CLOSED then return false end
if shell_result==SHELL_CLS then display_update_required=true end
-- activity coming from lemonbar or dzen or other 'bar' program
elseif S==Out
then
-- read from the bar program
str=S:readln()
if str == nil
then
-- if bar program closes, reopen it
poll_streams:delete(Out)
Out:close()
Out=OpenOutput(settings)
else
ProcessBarProgramOutput(str)
end
-- our updater process has input
elseif S==updater:get_stream()
then
updater:process_input()
-- our listening datasocket has recieved a connection, accept a new client who will
-- send us messages
elseif S==datasock:get_stream()
then
S=datasock:accept()
poll_streams:add(S)
-- anything else must be coming from a client program that has connected to our datasock
elseif KvLineRead(S) == false
then
poll_streams:delete(S)
S:close()
end
end
return true
end
function BarmaidMainLoop()
local S
while true
do
now=time.secs()
-- if we get a sigpipe, we ignore it, we don't want to be shut down by this signal
if process.SIGPIPE ~= nil then process.sigwatch(process.SIGPIPE) end
-- if we are talking to a shell in a pty and
-- if we have a recent enough libUseful-lua to support signals, then
-- watch for sigwinch (signal for 'window size changed') and sig int (ctrl-c)
if shell ~= nil
then
if process.SIGWINCH ~= nil then process.sigwatch(process.SIGWINCH) end
if process.SIGINT ~= nil then process.sigwatch(process.SIGINT) end
end
if now ~= last_time then display_update_required=true end
if display_update_required == true then UpdateDisplay() end
if updater:required(now) == true then updater:launch() end
if ProcessStreams() ~= true then break end
HandleShellSignals()
HandleExitedChildProcesses()
end
end
-- MAIN STARTS HERE
ApplicationSetup()
BarmaidMainLoop()
if settings.ypos=="bottom" then term:clear() end