Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | |
| 19 | // -=- WinVNC Version 4.0 Tray Icon implementation |
| 20 | |
| 21 | #include <winvnc/STrayIcon.h> |
| 22 | #include <winvnc/VNCServerService.h> |
| 23 | #include <winvnc/resource.h> |
| 24 | |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 25 | #include <os/Mutex.h> |
| 26 | #include <os/Thread.h> |
| 27 | |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 28 | #include <rfb/LogWriter.h> |
| 29 | #include <rfb/Configuration.h> |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 30 | |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 31 | #include <rfb_win32/LaunchProcess.h> |
| 32 | #include <rfb_win32/TrayIcon.h> |
| 33 | #include <rfb_win32/AboutDialog.h> |
| 34 | #include <rfb_win32/MsgBox.h> |
| 35 | #include <rfb_win32/Service.h> |
| 36 | #include <rfb_win32/CurrentUser.h> |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 37 | |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 38 | #include <winvnc/ControlPanel.h> |
| 39 | |
| 40 | using namespace rfb; |
| 41 | using namespace win32; |
| 42 | using namespace winvnc; |
| 43 | |
| 44 | static LogWriter vlog("STrayIcon"); |
| 45 | |
| 46 | BoolParameter STrayIconThread::disableOptions("DisableOptions", "Disable the Options entry in the VNC Server tray menu.", false); |
| 47 | BoolParameter STrayIconThread::disableClose("DisableClose", "Disable the Close entry in the VNC Server tray menu.", false); |
| 48 | |
| 49 | |
| 50 | // |
| 51 | // -=- AboutDialog global values |
| 52 | // |
| 53 | |
| 54 | const WORD rfb::win32::AboutDialog::DialogId = IDD_ABOUT; |
| 55 | const WORD rfb::win32::AboutDialog::Copyright = IDC_COPYRIGHT; |
| 56 | const WORD rfb::win32::AboutDialog::Version = IDC_VERSION; |
| 57 | const WORD rfb::win32::AboutDialog::BuildTime = IDC_BUILDTIME; |
| 58 | const WORD rfb::win32::AboutDialog::Description = IDC_DESCRIPTION; |
| 59 | |
| 60 | |
| 61 | // |
| 62 | // -=- Internal tray icon class |
| 63 | // |
| 64 | |
| 65 | const UINT WM_SET_TOOLTIP = WM_USER + 1; |
| 66 | |
Adam Tkac | 934f63c | 2009-10-12 15:54:59 +0000 | [diff] [blame] | 67 | namespace winvnc { |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 68 | |
Adam Tkac | 934f63c | 2009-10-12 15:54:59 +0000 | [diff] [blame] | 69 | class STrayIcon : public TrayIcon { |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 70 | public: |
Pierre Ossman | b1cd6ca | 2015-03-03 16:37:43 +0100 | [diff] [blame] | 71 | STrayIcon(STrayIconThread& t) : |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 72 | vncConfig(_T("vncconfig.exe"), isServiceProcess() ? _T("-noconsole -service") : _T("-noconsole")), |
Pierre Ossman | b1cd6ca | 2015-03-03 16:37:43 +0100 | [diff] [blame] | 73 | vncConnect(_T("winvnc4.exe"), _T("-noconsole -connect")), thread(t) { |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 74 | |
| 75 | // *** |
| 76 | SetWindowText(getHandle(), _T("winvnc::IPC_Interface")); |
| 77 | // *** |
| 78 | |
| 79 | SetTimer(getHandle(), 1, 3000, 0); |
| 80 | PostMessage(getHandle(), WM_TIMER, 1, 0); |
| 81 | PostMessage(getHandle(), WM_SET_TOOLTIP, 0, 0); |
| 82 | CPanel = new ControlPanel(getHandle()); |
| 83 | } |
| 84 | |
| 85 | virtual LRESULT processMessage(UINT msg, WPARAM wParam, LPARAM lParam) { |
| 86 | switch(msg) { |
| 87 | |
| 88 | case WM_USER: |
| 89 | { |
| 90 | bool userKnown = CurrentUserToken().canImpersonate(); |
| 91 | bool allowOptions = !STrayIconThread::disableOptions && userKnown; |
| 92 | bool allowClose = !STrayIconThread::disableClose && userKnown; |
| 93 | |
| 94 | switch (lParam) { |
| 95 | case WM_LBUTTONDBLCLK: |
| 96 | SendMessage(getHandle(), WM_COMMAND, ID_CONTR0L_PANEL, 0); |
| 97 | break; |
| 98 | case WM_RBUTTONUP: |
| 99 | HMENU menu = LoadMenu(GetModuleHandle(0), MAKEINTRESOURCE(thread.menu)); |
| 100 | HMENU trayMenu = GetSubMenu(menu, 0); |
| 101 | |
| 102 | |
| 103 | // Default item is Options, if available, or About if not |
| 104 | SetMenuDefaultItem(trayMenu, ID_CONTR0L_PANEL, FALSE); |
| 105 | |
| 106 | // Enable/disable options as required |
| 107 | EnableMenuItem(trayMenu, ID_OPTIONS, (!allowOptions ? MF_GRAYED : MF_ENABLED) | MF_BYCOMMAND); |
| 108 | EnableMenuItem(trayMenu, ID_CONNECT, (!userKnown ? MF_GRAYED : MF_ENABLED) | MF_BYCOMMAND); |
| 109 | EnableMenuItem(trayMenu, ID_CLOSE, (!allowClose ? MF_GRAYED : MF_ENABLED) | MF_BYCOMMAND); |
| 110 | |
| 111 | thread.server.getClientsInfo(&LCInfo); |
| 112 | CheckMenuItem(trayMenu, ID_DISABLE_NEW_CLIENTS, (LCInfo.getDisable() ? MF_CHECKED : MF_UNCHECKED) | MF_BYCOMMAND); |
| 113 | |
| 114 | // SetForegroundWindow is required, otherwise Windows ignores the |
| 115 | // TrackPopupMenu because the window isn't the foreground one, on |
| 116 | // some older Windows versions... |
| 117 | SetForegroundWindow(getHandle()); |
| 118 | |
| 119 | // Display the menu |
| 120 | POINT pos; |
| 121 | GetCursorPos(&pos); |
| 122 | TrackPopupMenu(trayMenu, 0, pos.x, pos.y, 0, getHandle(), 0); |
| 123 | |
| 124 | break; |
| 125 | } |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | // Handle tray icon menu commands |
| 130 | case WM_COMMAND: |
| 131 | switch (LOWORD(wParam)) { |
| 132 | case ID_CONTR0L_PANEL: |
| 133 | CPanel->showDialog(); |
| 134 | break; |
| 135 | case ID_DISABLE_NEW_CLIENTS: |
| 136 | { |
| 137 | thread.server.getClientsInfo(&LCInfo); |
| 138 | LCInfo.setDisable(!LCInfo.getDisable()); |
| 139 | thread.server.setClientsStatus(&LCInfo); |
| 140 | CPanel->UpdateListView(&LCInfo); |
| 141 | } |
| 142 | break; |
| 143 | case ID_OPTIONS: |
Samuel Mannehed | 60c4193 | 2014-02-07 14:53:24 +0000 | [diff] [blame] | 144 | vncConfig.start(INVALID_HANDLE_VALUE); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 145 | break; |
| 146 | case ID_CONNECT: |
Samuel Mannehed | 60c4193 | 2014-02-07 14:53:24 +0000 | [diff] [blame] | 147 | vncConnect.start(INVALID_HANDLE_VALUE); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 148 | break; |
| 149 | case ID_DISCONNECT: |
| 150 | thread.server.disconnectClients("tray menu disconnect"); |
| 151 | break; |
| 152 | case ID_CLOSE: |
| 153 | if (MsgBox(0, _T("Are you sure you want to close the server?"), |
| 154 | MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON2) == IDYES) { |
| 155 | if (isServiceProcess()) { |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 156 | try { |
| 157 | rfb::win32::stopService(VNCServerService::Name); |
| 158 | } catch (rdr::Exception& e) { |
| 159 | MsgBox(0, TStr(e.str()), MB_ICONERROR | MB_OK); |
| 160 | } |
| 161 | } else { |
| 162 | thread.server.stop(); |
| 163 | } |
| 164 | } |
| 165 | break; |
| 166 | case ID_ABOUT: |
| 167 | AboutDialog::instance.showDialog(); |
| 168 | break; |
| 169 | } |
| 170 | return 0; |
| 171 | |
| 172 | // Handle commands send by other processes |
| 173 | case WM_COPYDATA: |
| 174 | { |
| 175 | COPYDATASTRUCT* command = (COPYDATASTRUCT*)lParam; |
| 176 | switch (command->dwData) { |
| 177 | case 1: |
| 178 | { |
Adam Tkac | 934f63c | 2009-10-12 15:54:59 +0000 | [diff] [blame] | 179 | CharArray viewer(command->cbData + 1); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 180 | memcpy(viewer.buf, command->lpData, command->cbData); |
| 181 | viewer.buf[command->cbData] = 0; |
| 182 | return thread.server.addNewClient(viewer.buf) ? 1 : 0; |
| 183 | } |
| 184 | case 2: |
| 185 | return thread.server.disconnectClients("IPC disconnect") ? 1 : 0; |
| 186 | case 3: |
Pierre Ossman | 8526e48 | 2018-11-09 17:14:57 +0100 | [diff] [blame^] | 187 | thread.server.setClientsStatus(&CPanel->ListConnStatus); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 188 | case 4: |
| 189 | thread.server.getClientsInfo(&LCInfo); |
| 190 | CPanel->UpdateListView(&LCInfo); |
| 191 | break; |
| 192 | }; |
| 193 | }; |
| 194 | break; |
| 195 | |
| 196 | case WM_CLOSE: |
| 197 | PostQuitMessage(0); |
| 198 | break; |
| 199 | |
| 200 | case WM_TIMER: |
| 201 | if (rfb::win32::desktopChangeRequired()) { |
| 202 | SendMessage(getHandle(), WM_CLOSE, 0, 0); |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | thread.server.getClientsInfo(&LCInfo); |
| 207 | CPanel->UpdateListView(&LCInfo); |
| 208 | |
| 209 | setIcon(thread.server.isServerInUse() ? |
| 210 | (!LCInfo.getDisable() ? thread.activeIcon : thread.dis_activeIcon) : |
| 211 | (!LCInfo.getDisable() ? thread.inactiveIcon : thread.dis_inactiveIcon)); |
| 212 | |
| 213 | return 0; |
| 214 | |
| 215 | case WM_SET_TOOLTIP: |
| 216 | { |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 217 | os::AutoMutex a(thread.lock); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 218 | if (thread.toolTip.buf) |
| 219 | setToolTip(thread.toolTip.buf); |
| 220 | } |
| 221 | return 0; |
| 222 | |
| 223 | } |
| 224 | |
| 225 | return TrayIcon::processMessage(msg, wParam, lParam); |
| 226 | } |
| 227 | |
| 228 | protected: |
| 229 | LaunchProcess vncConfig; |
| 230 | LaunchProcess vncConnect; |
| 231 | STrayIconThread& thread; |
| 232 | ControlPanel * CPanel; |
Pierre Ossman | 025326d | 2018-10-08 16:03:01 +0200 | [diff] [blame] | 233 | ListConnInfo LCInfo; |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | |
| 237 | STrayIconThread::STrayIconThread(VNCServerWin32& sm, UINT inactiveIcon_, UINT activeIcon_, |
| 238 | UINT dis_inactiveIcon_, UINT dis_activeIcon_, UINT menu_) |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 239 | : thread_id(-1), windowHandle(0), server(sm), |
Pierre Ossman | b1cd6ca | 2015-03-03 16:37:43 +0100 | [diff] [blame] | 240 | inactiveIcon(inactiveIcon_), activeIcon(activeIcon_), |
| 241 | dis_inactiveIcon(dis_inactiveIcon_), dis_activeIcon(dis_activeIcon_), |
| 242 | menu(menu_), runTrayIcon(true) { |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 243 | lock = new os::Mutex; |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 244 | start(); |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 245 | while (thread_id == (DWORD)-1) |
| 246 | Sleep(0); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 249 | STrayIconThread::~STrayIconThread() { |
| 250 | runTrayIcon = false; |
| 251 | PostThreadMessage(thread_id, WM_QUIT, 0, 0); |
| 252 | delete lock; |
| 253 | } |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 254 | |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 255 | void STrayIconThread::worker() { |
| 256 | thread_id = GetCurrentThreadId(); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 257 | while (runTrayIcon) { |
| 258 | if (rfb::win32::desktopChangeRequired() && |
| 259 | !rfb::win32::changeDesktop()) |
| 260 | Sleep(2000); |
| 261 | |
| 262 | STrayIcon icon(*this); |
| 263 | windowHandle = icon.getHandle(); |
| 264 | |
| 265 | MSG msg; |
| 266 | while (runTrayIcon && ::GetMessage(&msg, 0, 0, 0) > 0) { |
| 267 | TranslateMessage(&msg); |
| 268 | DispatchMessage(&msg); |
| 269 | } |
| 270 | |
| 271 | windowHandle = 0; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | void STrayIconThread::setToolTip(const TCHAR* text) { |
| 276 | if (!windowHandle) return; |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 277 | os::AutoMutex a(lock); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 278 | delete [] toolTip.buf; |
| 279 | toolTip.buf = tstrDup(text); |
| 280 | PostMessage(windowHandle, WM_SET_TOOLTIP, 0, 0); |
| 281 | } |
| 282 | |
Adam Tkac | 934f63c | 2009-10-12 15:54:59 +0000 | [diff] [blame] | 283 | } |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 284 | |