config.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package dashboard
  2. import (
  3. "dashboard/ui"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "os/signal"
  8. "syscall"
  9. "time"
  10. )
  11. const width = 1200
  12. const height = 700
  13. func Configure() {
  14. clock, window := layout()
  15. handleClock(clock)
  16. onSignal(syscall.SIGUSR1, window.ToggleVisibility)
  17. }
  18. func layout() (*ui.LabelComponent, *ui.WindowComponent) {
  19. clock := ui.Label("")
  20. w := ui.Window(width, height)
  21. w.Children(ui.VBox(30,
  22. clock,
  23. ui.VBox(10,
  24. ui.Button("Abiword", "abiword", launchfn(w, "abiword")),
  25. ui.Button("Alacritty", "Alacritty", launchfn(w, "alacritty")),
  26. ui.Button("Brave", "brave", launchfn(w, "brave")),
  27. ui.Button("Gnumeric", "gnumeric", launchfn(w, "gnumeric")),
  28. ui.Button("KeepassXC", "keepassxc", launchfn(w, "keepassxc")),
  29. ui.Button("Leafpad", "leafpad", launchfn(w, "leafpad")),
  30. ui.Button("Lutris", "lutris", launchfn(w, "lutris")),
  31. ui.Button("Thunderbird", "mozilla-thunderbird", launchfn(w, "thunderbird")),
  32. ui.Button("Spacefm", "file-manager", launchfn(w, "spacefm")),
  33. ),
  34. ui.VBox(10,
  35. ui.Button("Hexchat", "hexchat", launchfn(w, "hexchat", "--existing")),
  36. ui.Button("Signal", "signal-desktop", launchfn(w, "apulse", "signal-desktop")),
  37. ui.Button("Telegram", "telegram", launchfn(w, "telegram-desktop")),
  38. ),
  39. ui.HBox(10,
  40. ui.ImageFromIcon("audio-speakers-symbolic"),
  41. ui.HScale(0, 100, 5, func(value float64) {
  42. spawn("amixer", "sset", "-c", "HDMI", "speakers",
  43. "--", fmt.Sprintf("%d%%", int(value)))
  44. }),
  45. ui.Button("Mixer", "audio-speakers-symbolic",
  46. spawnfn("alacritty", "-e", "alsamixer", "-c", "HDMI")),
  47. ui.ImageFromIcon("audio-headphones-symbolic"),
  48. ui.HScale(0, 100, 5, func(value float64) {
  49. spawn("amixer", "sset", "-c", "Generic", "Master",
  50. "--", fmt.Sprintf("%d%%", int(value)))
  51. }),
  52. ui.Button("Mixer", "audio-headphones-symbolic",
  53. spawnfn("alacritty", "-e", "alsamixer", "-c", "Generic")),
  54. ui.Button("Mixer", "audio-input-microphone-symbolic",
  55. spawnfn("alacritty", "-e", "alsamixer", "-c", "Mini")),
  56. ),
  57. ui.HBox(10,
  58. ui.Button("On", "redshift", func() {
  59. waitFor("redshift", "-x")
  60. spawn("redshift", "-O", "3000")
  61. }),
  62. ui.Button("Off", "redshift", spawnfn("redshift", "-x")),
  63. ui.Button("Window/Region", "applets-screenshooter-symbolic",
  64. launchfn(w, "scrot", "-s")),
  65. ui.Button("1080p60", "monitor",
  66. spawnfn("xrandr", "--output", "HDMI-A-0", "--mode", "1920x1080")),
  67. ui.Button("UW100Hz", "monitor",
  68. spawnfn("xrandr", "--output", "HDMI-A-0", "--mode", "2560x1080",
  69. "--rate", "100")),
  70. ui.Button("EN/US (normal)", "keyboard",
  71. spawnfn("setxkbmap", "us")),
  72. ui.Button("EN/US (dvorak)", "keyboard",
  73. spawnfn("setxkbmap", "us",
  74. "-variant", "dvorak",
  75. "-option", "compose:ralt")),
  76. ui.Button("Reboot", "system-reboot",
  77. launchfn(w, "alacritty", "-e", "sudo", "reboot")),
  78. ui.Button("Poweroff", "system-shutdown",
  79. launchfn(w, "alacritty", "-e", "sudo", "poweroff")),
  80. ),
  81. ))
  82. return clock, w
  83. }
  84. func onSignal(sig os.Signal, fn func()) {
  85. go func() {
  86. ch := make(chan os.Signal, 1)
  87. signal.Notify(ch, sig)
  88. for {
  89. <-ch
  90. go fn()
  91. }
  92. }()
  93. }
  94. func handleClock(c *ui.LabelComponent) {
  95. go func() {
  96. for {
  97. now := time.Now()
  98. c.UpdateMarkup(formatClock(now))
  99. time.Sleep(time.Until(nextMin(now)))
  100. }
  101. }()
  102. }
  103. func nextMin(t time.Time) time.Time {
  104. y, mo, d := t.Date()
  105. h, mi, _ := t.Clock()
  106. return time.Date(y, mo, d, h, mi+1, 0, 0, t.Location())
  107. }
  108. func formatClock(t time.Time) string {
  109. return t.Format("Mon, Jan 02\n") +
  110. `<span size="300%">` + t.Format("15:04") + `</span>`
  111. }
  112. func launchfn(w *ui.WindowComponent, prog string, args ...string) func() {
  113. return func() {
  114. w.Hide()
  115. spawn(prog, args...)
  116. }
  117. }
  118. func spawnfn(prog string, args ...string) func() {
  119. return func() {
  120. spawn(prog, args...)
  121. }
  122. }
  123. func spawn(prog string, args ...string) {
  124. go start(prog, args...).Wait()
  125. }
  126. func waitFor(prog string, args ...string) {
  127. start(prog, args...).Wait()
  128. }
  129. func start(prog string, args ...string) *exec.Cmd {
  130. c := exec.Command(prog, args...)
  131. c.Start()
  132. return c
  133. }