demo / network-computer
Public
Copy of github.com/askrobots/network-computer (MIT): a desk you reach from anything. The original lives on GitHub.
GitStable demo
README: say this is a copy and where the original is
cde82c2
13 hours, 39 minutes ago
History
keyboard.go · 2.4 KB · 80 lines
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "log" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "regexp" |
| 8 | "runtime" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | var layoutRe = regexp.MustCompile(`^[a-z]{2,3}(:[a-z0-9_-]{1,32})?$`) |
| 13 | |
| 14 | // setKeyboard applies a client's keyboard layout (Linux: nc-keyboard, which |
| 15 | // records it on the desk and applies it to every keyboard now). |
| 16 | func setKeyboard(layout string) { |
| 17 | if !layoutRe.MatchString(layout) { |
| 18 | log.Printf("keyboard: ignoring bad layout %q", layout) |
| 19 | return |
| 20 | } |
| 21 | if runtime.GOOS != "linux" { |
| 22 | return // macOS/Windows hosts use their own configured layout |
| 23 | } |
| 24 | out, err := exec.Command("nc-keyboard", layout).CombinedOutput() |
| 25 | if err != nil { |
| 26 | log.Printf("keyboard: nc-keyboard %s: %v %s", layout, err, strings.TrimSpace(string(out))) |
| 27 | return |
| 28 | } |
| 29 | log.Printf("keyboard: %s", strings.TrimSpace(string(out))) |
| 30 | } |
| 31 | |
| 32 | // runInputHook lets provisioning adjust newly created input devices (the |
| 33 | // desk's keyboard layout). Optional: absent on hosts without it. |
| 34 | func runInputHook() { |
| 35 | if runtime.GOOS != "linux" { |
| 36 | return |
| 37 | } |
| 38 | p, err := exec.LookPath("nc-input-hook") |
| 39 | if err != nil { |
| 40 | return |
| 41 | } |
| 42 | if out, err := exec.Command(p).CombinedOutput(); err != nil { |
| 43 | log.Printf("input hook: %v %s", err, strings.TrimSpace(string(out))) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | var tzRe = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9_+-]*(/[A-Za-z0-9_+-]+){0,2}$`) |
| 48 | |
| 49 | // setTimezone makes the desk's clock the client's: the taskbar clock, the |
| 50 | // dashboard, and what voice takes "at 3" to mean. Linux desks only. |
| 51 | func setTimezone(tz string) { |
| 52 | if runtime.GOOS != "linux" || !tzRe.MatchString(tz) || strings.Contains(tz, "..") { |
| 53 | return |
| 54 | } |
| 55 | if _, err := os.Stat("/usr/share/zoneinfo/" + tz); err != nil { |
| 56 | log.Printf("timezone: unknown %q", tz) |
| 57 | return |
| 58 | } |
| 59 | if cur, _ := os.Readlink("/etc/localtime"); strings.HasSuffix(cur, "/"+tz) { |
| 60 | return |
| 61 | } |
| 62 | if out, err := exec.Command("timedatectl", "set-timezone", tz).CombinedOutput(); err != nil { |
| 63 | log.Printf("timezone: %v %s", err, strings.TrimSpace(string(out))) |
| 64 | return |
| 65 | } |
| 66 | log.Printf("timezone: %s (from the client)", tz) |
| 67 | // the taskbar clock reads the zone when it starts |
| 68 | exec.Command("nc-as-user", "xfce4-panel", "-r").Start() |
| 69 | } |
| 70 | |
| 71 | // launch opens (or closes) the desk's search and launch bar, for the client's |
| 72 | // 🔍 button. Linux desks only: nc-launch comes with provisioning. |
| 73 | func launch() { |
| 74 | if runtime.GOOS != "linux" { |
| 75 | return |
| 76 | } |
| 77 | if out, err := exec.Command("nc-launch").CombinedOutput(); err != nil { |
| 78 | log.Printf("launch: %v %s", err, strings.TrimSpace(string(out))) |
| 79 | } |
| 80 | } |