|
| 1 | +/* |
| 2 | + * Copyright 2021-2022 by Nedim Sabic Sabic |
| 3 | + * https://www.fibratus.io |
| 4 | + * All Rights Reserved. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package systray |
| 20 | + |
| 21 | +import ( |
| 22 | + "errors" |
| 23 | + "fmt" |
| 24 | + "github.com/rabbitstack/fibratus/pkg/alertsender" |
| 25 | + "github.com/rabbitstack/fibratus/pkg/sys" |
| 26 | + "golang.org/x/sys/windows" |
| 27 | + "os" |
| 28 | + "path/filepath" |
| 29 | + "unsafe" |
| 30 | +) |
| 31 | + |
| 32 | +var ( |
| 33 | + // ErrSystrayIconRegisterClass is raised when the systray window class fails o register |
| 34 | + ErrSystrayIconRegisterClass = errors.New("unable to register systray icon window class") |
| 35 | + // ErrSystrayIconWindow is raised when the systray icon window can't be created |
| 36 | + ErrSystrayIconWindow = errors.New("unable to create systray icon window") |
| 37 | + |
| 38 | + className = windows.StringToUTF16Ptr("fibratus") |
| 39 | +) |
| 40 | + |
| 41 | +// systray interops with the status area |
| 42 | +// to show balloon notifications with the |
| 43 | +// desired title and text. Both, regular |
| 44 | +// and balloon icons are also rendered when |
| 45 | +// displaying the notification message. |
| 46 | +type systray struct { |
| 47 | + wnd sys.Hwnd // systray icon window handle |
| 48 | + systrayIcon *sys.SystrayIcon |
| 49 | + config Config |
| 50 | +} |
| 51 | + |
| 52 | +func init() { |
| 53 | + alertsender.Register(alertsender.Systray, makeSender) |
| 54 | +} |
| 55 | + |
| 56 | +// makeSender constructs a new instance of the systray alert sender. |
| 57 | +func makeSender(config alertsender.Config) (alertsender.Sender, error) { |
| 58 | + c, ok := config.Sender.(Config) |
| 59 | + if !ok { |
| 60 | + return nil, alertsender.ErrInvalidConfig(alertsender.Systray) |
| 61 | + } |
| 62 | + |
| 63 | + if !c.Enabled { |
| 64 | + return &systray{}, nil |
| 65 | + } |
| 66 | + |
| 67 | + var mod windows.Handle |
| 68 | + err := windows.GetModuleHandleEx(0, nil, &mod) |
| 69 | + if err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + // register notification icon window class |
| 73 | + var wc sys.WndClassEx |
| 74 | + wc.Size = uint32(unsafe.Sizeof(wc)) |
| 75 | + wc.Instance = mod |
| 76 | + wc.WndProc = windows.NewCallback(wndProc) |
| 77 | + wc.ClassName = className |
| 78 | + err = sys.RegisterClass(&wc) |
| 79 | + if err != nil { |
| 80 | + return nil, errors.Join(ErrSystrayIconRegisterClass, err) |
| 81 | + } |
| 82 | + // create the notification icon window |
| 83 | + hwnd, err := sys.CreateWindowEx( |
| 84 | + 0, |
| 85 | + className, |
| 86 | + className, |
| 87 | + sys.WindowStyleOverlapped, |
| 88 | + sys.CwUseDefault, |
| 89 | + sys.CwUseDefault, |
| 90 | + 100, |
| 91 | + 100, |
| 92 | + 0, |
| 93 | + 0, |
| 94 | + mod, |
| 95 | + 0, |
| 96 | + ) |
| 97 | + if err != nil { |
| 98 | + return nil, errors.Join(ErrSystrayIconWindow, err) |
| 99 | + } |
| 100 | + |
| 101 | + systrayIcon, err := sys.NewSystrayIcon(hwnd) |
| 102 | + if err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + // find the icon in the same directory where the binary is loaded |
| 106 | + exe, err := os.Executable() |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + ico, err := sys.LoadImage( |
| 111 | + mod, |
| 112 | + windows.StringToUTF16Ptr(filepath.Join(filepath.Dir(exe), "fibratus.ico")), |
| 113 | + 1, // load icon |
| 114 | + 0, |
| 115 | + 0, |
| 116 | + sys.LoadResourceDefaultSize|sys.LoadResourceFromFile, |
| 117 | + ) |
| 118 | + if err != nil { |
| 119 | + // load stock informational system icon |
| 120 | + var icon sys.ShStockIcon |
| 121 | + icon.Size = uint32(unsafe.Sizeof(icon)) |
| 122 | + err := sys.SHGetStockIconInfo(79, 0x000000100, &icon) |
| 123 | + if err != nil { |
| 124 | + return nil, fmt.Errorf("unable to load systray icon resource: %v", err) |
| 125 | + } |
| 126 | + ico = windows.Handle(icon.Icon) |
| 127 | + } |
| 128 | + |
| 129 | + // set systray icon and tooltip |
| 130 | + if err := systrayIcon.SetIcon(sys.Hicon(ico)); err != nil { |
| 131 | + return nil, fmt.Errorf("unable to set systray icon: %v", err) |
| 132 | + } |
| 133 | + if err := systrayIcon.SetTooltip("Fibratus"); err != nil { |
| 134 | + return nil, fmt.Errorf("unable to set systray icon tooltip: %v", err) |
| 135 | + } |
| 136 | + s := &systray{ |
| 137 | + wnd: hwnd, |
| 138 | + systrayIcon: systrayIcon, |
| 139 | + config: c, |
| 140 | + } |
| 141 | + return s, nil |
| 142 | +} |
| 143 | + |
| 144 | +func (s systray) Send(alert alertsender.Alert) error { |
| 145 | + if s.systrayIcon == nil { |
| 146 | + return nil |
| 147 | + } |
| 148 | + return s.systrayIcon.ShowBalloonNotification(alert.Title, alert.Text, s.config.Sound, s.config.QuietMode) |
| 149 | +} |
| 150 | + |
| 151 | +func (s systray) Type() alertsender.Type { return alertsender.Systray } |
| 152 | +func (s systray) SupportsMarkdown() bool { return false } |
| 153 | + |
| 154 | +func (s systray) Shutdown() error { |
| 155 | + if s.wnd.IsValid() { |
| 156 | + s.wnd.Destroy() |
| 157 | + } |
| 158 | + if s.systrayIcon != nil { |
| 159 | + return s.systrayIcon.Delete() |
| 160 | + } |
| 161 | + return nil |
| 162 | +} |
| 163 | + |
| 164 | +func wndProc(hwnd uintptr, msg uint32, wparam, lparam uintptr) uintptr { |
| 165 | + return sys.DefWindowProc(hwnd, msg, wparam, lparam) |
| 166 | +} |
0 commit comments