-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.v
More file actions
352 lines (321 loc) · 8.86 KB
/
Copy pathmain.v
File metadata and controls
352 lines (321 loc) · 8.86 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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
module main
import cmd
import ipc
import lib.gtk
import lib.inotify
import os
import providers
import vars
import widgets.bar
import widgets.combo
import widgets.label
import widgets.systray
import widgets.workspaces
struct AppData {
mut:
config Config
refs []voidptr
app &C.GtkApplication = unsafe { nil }
store &vars.VarStore = unsafe { nil }
gen &vars.Generation = unsafe { nil }
lua_rt &cmd.LuaRuntime = unsafe { nil }
reload_timer_id u32
}
fn content_fn(container &C.GtkWidget, mon cmd.MonitorInfo, data voidptr) {
cd := unsafe { &ContentData(data) }
store := unsafe { &vars.VarStore(cd.store) }
gen := unsafe { &vars.Generation(cd.gen) }
left_box := C.gtk_box_new(gtk.gtk_orientation_horizontal, 0)
center_box := C.gtk_box_new(gtk.gtk_orientation_horizontal, 0)
right_box := C.gtk_box_new(gtk.gtk_orientation_horizontal, 0)
for w in cd.left {
widget := make_widget(w, mon, store, cd.shell, gen, cd.lua_rt)
if widget != unsafe { nil } {
C.gtk_box_pack_start(left_box, widget, 0, 0, 0)
}
}
for w in cd.center {
widget := make_widget(w, mon, store, cd.shell, gen, cd.lua_rt)
if widget != unsafe { nil } {
C.gtk_box_pack_start(center_box, widget, 0, 0, 0)
}
}
for w in cd.right {
widget := make_widget(w, mon, store, cd.shell, gen, cd.lua_rt)
if widget != unsafe { nil } {
C.gtk_box_pack_start(right_box, widget, 0, 0, 0)
}
}
C.gtk_box_pack_start(container, left_box, 0, 0, 0)
C.gtk_box_set_center_widget(container, center_box)
C.gtk_box_pack_end(container, right_box, 0, 0, 0)
}
fn make_widget(desc WidgetDesc, mon cmd.MonitorInfo, store &vars.VarStore, shell []string, gen &vars.Generation, lua_rt voidptr) &C.GtkWidget {
new_self := cmd.clone_self_with_monitor(lua_rt, desc.self_ref, mon)
on_click := desc.on_click.with_self_ref(new_self)
on_right_click := desc.on_right_click.with_self_ref(new_self)
on_middle_click := desc.on_middle_click.with_self_ref(new_self)
on_drag := desc.on_drag.with_self_ref(new_self)
on_drop := desc.on_drop.with_self_ref(new_self)
return match desc.kind {
'label' {
label.make_widget(desc.text, desc.var_name, store, gen, on_click, on_right_click,
on_middle_click, on_drag, desc.drag_enabled, on_drop, shell, lua_rt,
desc.format_ref, new_self)
}
'workspaces' {
workspaces.make_widget(desc.active_color, mon.name, on_click, on_right_click,
on_middle_click, on_drop, shell, gen, lua_rt)
}
'systray' {
systray.make_widget(desc.icon_size)
}
'combo' {
combo.make_widget(desc.items, desc.var_name, store, gen, desc.on_change, shell,
lua_rt)
}
else {
eprintln('barf: unknown widget type: ${desc.kind}')
unsafe { nil }
}
}
}
fn setup(mut ad AppData) {
cfg := ad.config
default_shell := if cfg.shell.len > 0 { cfg.shell } else { ['sh', '-c'] }
cmd.bind_store(ad.lua_rt, voidptr(ad.store))
for gv in global_var_descs() {
providers.start_poll(gv.name, cmd.Command{ kind: .shell, str_val: gv.command },
gv.interval, ['sh', '-c'], ad.store, ad.gen, voidptr(ad.lua_rt))
}
for p in cfg.polls {
if p.value != '' {
unsafe {
mut store := ad.store
if p.value_is_json {
store.set_json(p.name, p.value)
} else {
store.set(p.name, p.value)
}
}
}
if p.command.is_set() {
shell := if p.shell.len > 0 { p.shell } else { default_shell }
providers.start_poll(p.name, p.command, p.interval, shell, ad.store, ad.gen,
voidptr(ad.lua_rt))
}
if p.listen_shell != '' {
shell := if p.shell.len > 0 { p.shell } else { default_shell }
providers.start_listen(p.name, p.listen_shell, shell, p.listen_override, ad.store,
ad.gen, voidptr(ad.lua_rt))
}
}
mut content_refs := []&ContentData{}
for desc in cfg.bars {
cd := &ContentData{
left: desc.left
center: desc.center
right: desc.right
store: voidptr(ad.store)
gen: voidptr(ad.gen)
shell: default_shell
lua_rt: voidptr(ad.lua_rt)
}
content_refs << cd
mut anchors := desc.anchors.map(fn (s string) bar.Anchor {
return match s {
'left' { bar.Anchor.left }
'right' { bar.Anchor.right }
'bottom' { bar.Anchor.bottom }
else { bar.Anchor.top }
}
})
if anchors.len == 0 {
anchors = [bar.Anchor.left, .right, .top]
}
event_refs := bar.create(ad.app, bar.BarConfig{
height: desc.height
self_ref: desc.self_ref
anchors: anchors
monitors: desc.monitors
content: content_fn
content_data: voidptr(cd)
font_family: desc.font_family
font_size: desc.font_size
bg_color: desc.bg_color
fg_color: desc.fg_color
on_scroll: desc.on_scroll
on_click: desc.on_click
shell: default_shell
lua_rt: voidptr(ad.lua_rt)
})
for r in event_refs {
ad.refs << r
}
}
_ = content_refs
}
struct IpcSetRequest {
name string
value string
store voidptr
}
fn ipc_set_apply(data voidptr) int {
req := unsafe { &IpcSetRequest(data) }
unsafe {
mut store := &vars.VarStore(req.store)
store.set(req.name, req.value)
}
return 0
}
fn ipc_set_var(name string, value string, data voidptr) {
req := &IpcSetRequest{
name: name
value: value
store: data
}
C.g_idle_add(voidptr(ipc_set_apply), voidptr(req))
}
fn do_reload(data voidptr) int {
mut ad := unsafe { &AppData(data) }
ad.reload_timer_id = 0
ad.gen.value++
mut node := C.gtk_application_get_windows(ad.app)
mut windows := []&C.GtkWidget{}
for node != unsafe { nil } {
windows << unsafe { &C.GtkWidget(node.data) }
node = node.next
}
for w in windows {
C.gtk_widget_destroy(w)
}
unsafe {
mut store := ad.store
store.clear()
}
if ad.lua_rt != unsafe { nil } {
unsafe {
mut rt := ad.lua_rt
rt.close()
}
}
config, lua_state := load_config()
ad.config = config
if lua_state != unsafe { nil } {
ad.lua_rt = cmd.new_runtime(lua_state)
} else {
ad.lua_rt = unsafe { nil }
}
ad.refs = []
setup(mut ad)
return 0
}
fn on_monitor_changed(display voidptr, monitor voidptr, data voidptr) {
mut ad := unsafe { &AppData(data) }
// bump gen before destroying so idle_update callbacks see stale gen and bail
ad.gen.value++
// destroy windows immediately to avoid wayland error
mut node := C.gtk_application_get_windows(ad.app)
mut windows := []&C.GtkWidget{}
for node != unsafe { nil } {
windows << unsafe { &C.GtkWidget(node.data) }
node = node.next
}
for w in windows {
C.gtk_widget_destroy(w)
}
// debounce
if ad.reload_timer_id != 0 {
C.g_source_remove(ad.reload_timer_id)
}
ad.reload_timer_id = C.g_timeout_add(200, voidptr(do_reload), data)
}
fn on_activate(app &C.GtkApplication, data voidptr) {
mut ad := unsafe { &AppData(data) }
unsafe {
ad.app = app
}
// keep the app alive across moments when every window is destroyed
// (e.g. monitor-removed on DPMS off), otherwise GtkApplication exits
C.g_application_hold(app)
setup(mut ad)
display := C.gdk_display_get_default()
C.g_signal_connect_data(display, c'monitor-added', voidptr(on_monitor_changed), data,
unsafe { nil }, 0)
C.g_signal_connect_data(display, c'monitor-removed', voidptr(on_monitor_changed), data,
unsafe { nil }, 0)
}
fn watch_config(dir string, data voidptr) {
fd := C.inotify_init1(inotify.in_cloexec)
if fd < 0 {
eprintln('barf: inotify_init1 failed')
return
}
wd := C.inotify_add_watch(fd, dir.str,
inotify.in_close_write | inotify.in_moved_to | inotify.in_create)
if wd < 0 {
eprintln('barf: inotify_add_watch failed for ${dir}')
C.close(fd)
return
}
buf := []u8{len: 4096}
mut pending := false
for {
mut rs := C.fd_set{}
C.FD_ZERO(&rs)
C.FD_SET(fd, &rs)
mut tv := C.timeval{
tv_sec: 0
tv_usec: 200000
}
n := C.select(fd + 1, &rs, unsafe { nil }, unsafe { nil }, &tv)
if n > 0 && C.FD_ISSET(fd, &rs) != 0 {
C.read(fd, buf.data, usize(buf.len))
pending = true
} else if n == 0 && pending {
C.g_idle_add(voidptr(do_reload), data)
pending = false
}
}
}
fn main() {
if os.args.len < 2 {
eprintln('usage: barf daemon | update <name>=<value> | get <name> | state')
exit(1)
}
match os.args[1] {
'daemon' {}
'update', 'get', 'state' {
ipc.run_client(os.args[1..]) or {
eprintln('barf: ${err}')
exit(1)
}
return
}
else {
eprintln('barf: unknown command: ${os.args[1]}')
eprintln('usage: barf daemon | update <name>=<value> | get <name> | state')
exit(1)
}
}
config, lua_state := load_config()
mut lua_rt := unsafe { &cmd.LuaRuntime(nil) }
if lua_state != unsafe { nil } {
lua_rt = cmd.new_runtime(lua_state)
}
store := &vars.VarStore{}
gen := &vars.Generation{}
mut ad := &AppData{
config: config
store: store
gen: gen
lua_rt: lua_rt
}
spawn watch_config(config_dir(), voidptr(ad))
spawn ipc.serve(store, ipc_set_var, voidptr(store))
app := C.gtk_application_new(c'io.barf', 0)
C.g_signal_connect_data(app, c'activate', voidptr(on_activate), voidptr(ad), unsafe { nil }, 0)
status := C.g_application_run(app, 0, unsafe { nil })
C.g_object_unref(app)
exit(status)
}