eventloop.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //go:build windows
  2. package wintray
  3. import (
  4. "fmt"
  5. "log/slog"
  6. "sync"
  7. "unsafe"
  8. "golang.org/x/sys/windows"
  9. )
  10. var (
  11. quitOnce sync.Once
  12. )
  13. func (t *winTray) Run() {
  14. nativeLoop()
  15. }
  16. func nativeLoop() {
  17. // Main message pump.
  18. slog.Debug("starting event handling loop")
  19. m := &struct {
  20. WindowHandle windows.Handle
  21. Message uint32
  22. Wparam uintptr
  23. Lparam uintptr
  24. Time uint32
  25. Pt point
  26. LPrivate uint32
  27. }{}
  28. for {
  29. ret, _, err := pGetMessage.Call(uintptr(unsafe.Pointer(m)), 0, 0, 0)
  30. // If the function retrieves a message other than WM_QUIT, the return value is nonzero.
  31. // If the function retrieves the WM_QUIT message, the return value is zero.
  32. // If there is an error, the return value is -1
  33. // https://msdn.microsoft.com/en-us/library/windows/desktop/ms644936(v=vs.85).aspx
  34. switch int32(ret) {
  35. case -1:
  36. slog.Error(fmt.Sprintf("get message failure: %v", err))
  37. return
  38. case 0:
  39. return
  40. default:
  41. pTranslateMessage.Call(uintptr(unsafe.Pointer(m))) //nolint:errcheck
  42. pDispatchMessage.Call(uintptr(unsafe.Pointer(m))) //nolint:errcheck
  43. }
  44. }
  45. }
  46. // WindowProc callback function that processes messages sent to a window.
  47. // https://msdn.microsoft.com/en-us/library/windows/desktop/ms633573(v=vs.85).aspx
  48. func (t *winTray) wndProc(hWnd windows.Handle, message uint32, wParam, lParam uintptr) (lResult uintptr) {
  49. const (
  50. WM_RBUTTONUP = 0x0205
  51. WM_LBUTTONUP = 0x0202
  52. WM_COMMAND = 0x0111
  53. WM_ENDSESSION = 0x0016
  54. WM_CLOSE = 0x0010
  55. WM_DESTROY = 0x0002
  56. WM_MOUSEMOVE = 0x0200
  57. WM_LBUTTONDOWN = 0x0201
  58. )
  59. switch message {
  60. case WM_COMMAND:
  61. menuItemId := int32(wParam)
  62. // https://docs.microsoft.com/en-us/windows/win32/menurc/wm-command#menus
  63. switch menuItemId {
  64. case quitMenuID:
  65. select {
  66. case t.callbacks.Quit <- struct{}{}:
  67. // should not happen but in case not listening
  68. default:
  69. slog.Error("no listener on Quit")
  70. }
  71. case updateMenuID:
  72. select {
  73. case t.callbacks.Update <- struct{}{}:
  74. // should not happen but in case not listening
  75. default:
  76. slog.Error("no listener on Update")
  77. }
  78. case diagLogsMenuID:
  79. select {
  80. case t.callbacks.ShowLogs <- struct{}{}:
  81. // should not happen but in case not listening
  82. default:
  83. slog.Error("no listener on ShowLogs")
  84. }
  85. default:
  86. slog.Debug(fmt.Sprintf("Unexpected menu item id: %d", menuItemId))
  87. }
  88. case WM_CLOSE:
  89. boolRet, _, err := pDestroyWindow.Call(uintptr(t.window))
  90. if boolRet == 0 {
  91. slog.Error(fmt.Sprintf("failed to destroy window: %s", err))
  92. }
  93. err = t.wcex.unregister()
  94. if err != nil {
  95. slog.Error(fmt.Sprintf("failed to uregister windo %s", err))
  96. }
  97. case WM_DESTROY:
  98. // same as WM_ENDSESSION, but throws 0 exit code after all
  99. defer pPostQuitMessage.Call(uintptr(int32(0))) //nolint:errcheck
  100. fallthrough
  101. case WM_ENDSESSION:
  102. t.muNID.Lock()
  103. if t.nid != nil {
  104. err := t.nid.delete()
  105. if err != nil {
  106. slog.Error(fmt.Sprintf("failed to delete nid: %s", err))
  107. }
  108. }
  109. t.muNID.Unlock()
  110. case t.wmSystrayMessage:
  111. switch lParam {
  112. case WM_MOUSEMOVE, WM_LBUTTONDOWN:
  113. // Ignore these...
  114. case WM_RBUTTONUP, WM_LBUTTONUP:
  115. err := t.showMenu()
  116. if err != nil {
  117. slog.Error(fmt.Sprintf("failed to show menu: %s", err))
  118. }
  119. case 0x405: // TODO - how is this magic value derived for the notification left click
  120. if t.pendingUpdate {
  121. select {
  122. case t.callbacks.Update <- struct{}{}:
  123. // should not happen but in case not listening
  124. default:
  125. slog.Error("no listener on Update")
  126. }
  127. } else {
  128. select {
  129. case t.callbacks.DoFirstUse <- struct{}{}:
  130. // should not happen but in case not listening
  131. default:
  132. slog.Error("no listener on DoFirstUse")
  133. }
  134. }
  135. case 0x404: // Middle click or close notification
  136. // slog.Debug("doing nothing on close of first time notification")
  137. default:
  138. // 0x402 also seems common - what is it?
  139. slog.Debug(fmt.Sprintf("unmanaged app message, lParm: 0x%x", lParam))
  140. }
  141. case t.wmTaskbarCreated: // on explorer.exe restarts
  142. t.muNID.Lock()
  143. err := t.nid.add()
  144. if err != nil {
  145. slog.Error(fmt.Sprintf("failed to refresh the taskbar on explorer restart: %s", err))
  146. }
  147. t.muNID.Unlock()
  148. default:
  149. // Calls the default window procedure to provide default processing for any window messages that an application does not process.
  150. // https://msdn.microsoft.com/en-us/library/windows/desktop/ms633572(v=vs.85).aspx
  151. lResult, _, _ = pDefWindowProc.Call(
  152. uintptr(hWnd),
  153. uintptr(message),
  154. wParam,
  155. lParam,
  156. )
  157. }
  158. return
  159. }
  160. func (t *winTray) Quit() {
  161. quitOnce.Do(quit)
  162. }
  163. func quit() {
  164. boolRet, _, err := pPostMessage.Call(
  165. uintptr(wt.window),
  166. WM_CLOSE,
  167. 0,
  168. 0,
  169. )
  170. if boolRet == 0 {
  171. slog.Error(fmt.Sprintf("failed to post close message on shutdown %s", err))
  172. }
  173. }