feat/data-dnd e05cb15

hotplug.go RawHistoryFolder

1package drm
2
3import (
4	"context"
5	"errors"
6	"fmt"
7	"sync"
8
9	"github.com/bnema/neferwl/internal/adapters/fdwait"
10	"github.com/bnema/purego"
11	"golang.org/x/sys/unix"
12)
13
14// Hotplug reports connector changes through a udev netlink monitor on the
15// drm subsystem (the kernel sends HOTPLUG=1 on the card). libudev is loaded
16// with purego.
17
18var (
19	udevOnce sync.Once
20	udevErr  error
21
22	udevNew           func() uintptr
23	udevUnref         func(uintptr) uintptr
24	monitorNew        func(udev uintptr, name string) uintptr
25	monitorUnref      func(uintptr) uintptr
26	monitorFilter     func(mon uintptr, subsystem string, devtype uintptr) int32
27	monitorEnable     func(uintptr) int32
28	monitorFD         func(uintptr) int32
29	monitorReceive    func(uintptr) uintptr
30	deviceUnref       func(uintptr) uintptr
31	devicePropertyVal func(dev uintptr, key string) string
32)
33
34func loadUdev() error {
35	udevOnce.Do(func() {
36		lib, err := purego.Dlopen("libudev.so.1", purego.RTLD_NOW|purego.RTLD_LOCAL)
37		if err != nil {
38			udevErr = fmt.Errorf("open libudev.so.1: %w", err)
39			return
40		}
41		defer func() {
42			if r := recover(); r != nil {
43				udevErr = fmt.Errorf("libudev: %v", r)
44			}
45		}()
46		reg := func(fn any, name string) { purego.RegisterLibFunc(fn, lib, "udev_"+name) }
47		reg(&udevNew, "new")
48		reg(&udevUnref, "unref")
49		reg(&monitorNew, "monitor_new_from_netlink")
50		reg(&monitorUnref, "monitor_unref")
51		reg(&monitorFilter, "monitor_filter_add_match_subsystem_devtype")
52		reg(&monitorEnable, "monitor_enable_receiving")
53		reg(&monitorFD, "monitor_get_fd")
54		reg(&monitorReceive, "monitor_receive_device")
55		reg(&deviceUnref, "device_unref")
56		reg(&devicePropertyVal, "device_get_property_value")
57	})
58	return udevErr
59}
60
61// WatchHotplug sends on changed after every DRM hotplug event until ctx
62// ends. Events that arrive while a send is pending are merged.
63func WatchHotplug(ctx context.Context, changed chan<- struct{}) error {
64	if err := loadUdev(); err != nil {
65		return err
66	}
67	udev := udevNew()
68	if udev == 0 {
69		return errors.New("udev_new failed")
70	}
71	defer udevUnref(udev)
72	mon := monitorNew(udev, "udev")
73	if mon == 0 {
74		return errors.New("udev_monitor_new_from_netlink failed")
75	}
76	defer monitorUnref(mon)
77	if monitorFilter(mon, "drm", 0) < 0 || monitorEnable(mon) < 0 {
78		return errors.New("udev monitor setup failed")
79	}
80	fd := monitorFD(mon)
81	waiter, err := fdwait.New()
82	if err != nil {
83		return fmt.Errorf("udev waiter: %w", err)
84	}
85	defer waiter.Close()
86	waiter.StopOn(ctx)
87	if err := waiter.Pin(fd); err != nil {
88		return fmt.Errorf("udev waiter: %w", err)
89	}
90	fds := []unix.PollFd{{Fd: fd, Events: unix.POLLIN}}
91	for ctx.Err() == nil {
92		n, err := waiter.Poll(fds, -1)
93		if err != nil && !errors.Is(err, unix.EINTR) {
94			return fmt.Errorf("udev poll: %w", err)
95		}
96		if n <= 0 {
97			continue
98		}
99		dev := monitorReceive(mon)
100		if dev == 0 {
101			continue
102		}
103		hot := devicePropertyVal(dev, "HOTPLUG") == "1"
104		deviceUnref(dev)
105		if !hot {
106			continue
107		}
108		select {
109		case changed <- struct{}{}:
110		default:
111		}
112	}
113	return nil
114}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 25

Tags 10

↑↓ moveenter openesc close