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 | |
| 25 | #include <rfb/LogWriter.h> |
| 26 | #include <rfb/Configuration.h> |
| 27 | #include <rfb_win32/LaunchProcess.h> |
| 28 | #include <rfb_win32/TrayIcon.h> |
| 29 | #include <rfb_win32/AboutDialog.h> |
| 30 | #include <rfb_win32/MsgBox.h> |
| 31 | #include <rfb_win32/Service.h> |
| 32 | #include <rfb_win32/CurrentUser.h> |
| 33 | #include <winvnc/ControlPanel.h> |
| 34 | |
| 35 | using namespace rfb; |
| 36 | using namespace win32; |
| 37 | using namespace winvnc; |
| 38 | |
| 39 | static LogWriter vlog("STrayIcon"); |
| 40 | |
| 41 | BoolParameter STrayIconThread::disableOptions("DisableOptions", "Disable the Options entry in the VNC Server tray menu.", false); |
| 42 | BoolParameter STrayIconThread::disableClose("DisableClose", "Disable the Close entry in the VNC Server tray menu.", false); |
| 43 | |
| 44 | |
| 45 | // |
| 46 | // -=- AboutDialog global values |
| 47 | // |
| 48 | |
| 49 | const WORD rfb::win32::AboutDialog::DialogId = IDD_ABOUT; |
| 50 | const WORD rfb::win32::AboutDialog::Copyright = IDC_COPYRIGHT; |
| 51 | const WORD rfb::win32::AboutDialog::Version = IDC_VERSION; |
| 52 | const WORD rfb::win32::AboutDialog::BuildTime = IDC_BUILDTIME; |
| 53 | const WORD rfb::win32::AboutDialog::Description = IDC_DESCRIPTION; |
| 54 | |
| 55 | |
| 56 | // |
| 57 | // -=- Internal tray icon class |
| 58 | // |
| 59 | |
| 60 | const UINT WM_SET_TOOLTIP = WM_USER + 1; |
| 61 | |
| 62 | |
| 63 | class winvnc::STrayIcon : public TrayIcon { |
| 64 | public: |
| 65 | STrayIcon(STrayIconThread& t) : thread(t), |
| 66 | vncConfig(_T("vncconfig.exe"), isServiceProcess() ? _T("-noconsole -service") : _T("-noconsole")), |
| 67 | vncConnect(_T("winvnc4.exe"), _T("-noconsole -connect")) { |
| 68 | |
| 69 | // *** |
| 70 | SetWindowText(getHandle(), _T("winvnc::IPC_Interface")); |
| 71 | // *** |
| 72 | |
| 73 | SetTimer(getHandle(), 1, 3000, 0); |
| 74 | PostMessage(getHandle(), WM_TIMER, 1, 0); |
| 75 | PostMessage(getHandle(), WM_SET_TOOLTIP, 0, 0); |
| 76 | CPanel = new ControlPanel(getHandle()); |
| 77 | } |
| 78 | |
| 79 | virtual LRESULT processMessage(UINT msg, WPARAM wParam, LPARAM lParam) { |
| 80 | switch(msg) { |
| 81 | |
| 82 | case WM_USER: |
| 83 | { |
| 84 | bool userKnown = CurrentUserToken().canImpersonate(); |
| 85 | bool allowOptions = !STrayIconThread::disableOptions && userKnown; |
| 86 | bool allowClose = !STrayIconThread::disableClose && userKnown; |
| 87 | |
| 88 | switch (lParam) { |
| 89 | case WM_LBUTTONDBLCLK: |
| 90 | SendMessage(getHandle(), WM_COMMAND, ID_CONTR0L_PANEL, 0); |
| 91 | break; |
| 92 | case WM_RBUTTONUP: |
| 93 | HMENU menu = LoadMenu(GetModuleHandle(0), MAKEINTRESOURCE(thread.menu)); |
| 94 | HMENU trayMenu = GetSubMenu(menu, 0); |
| 95 | |
| 96 | |
| 97 | // Default item is Options, if available, or About if not |
| 98 | SetMenuDefaultItem(trayMenu, ID_CONTR0L_PANEL, FALSE); |
| 99 | |
| 100 | // Enable/disable options as required |
| 101 | EnableMenuItem(trayMenu, ID_OPTIONS, (!allowOptions ? MF_GRAYED : MF_ENABLED) | MF_BYCOMMAND); |
| 102 | EnableMenuItem(trayMenu, ID_CONNECT, (!userKnown ? MF_GRAYED : MF_ENABLED) | MF_BYCOMMAND); |
| 103 | EnableMenuItem(trayMenu, ID_CLOSE, (!allowClose ? MF_GRAYED : MF_ENABLED) | MF_BYCOMMAND); |
| 104 | |
| 105 | thread.server.getClientsInfo(&LCInfo); |
| 106 | CheckMenuItem(trayMenu, ID_DISABLE_NEW_CLIENTS, (LCInfo.getDisable() ? MF_CHECKED : MF_UNCHECKED) | MF_BYCOMMAND); |
| 107 | |
| 108 | // SetForegroundWindow is required, otherwise Windows ignores the |
| 109 | // TrackPopupMenu because the window isn't the foreground one, on |
| 110 | // some older Windows versions... |
| 111 | SetForegroundWindow(getHandle()); |
| 112 | |
| 113 | // Display the menu |
| 114 | POINT pos; |
| 115 | GetCursorPos(&pos); |
| 116 | TrackPopupMenu(trayMenu, 0, pos.x, pos.y, 0, getHandle(), 0); |
| 117 | |
| 118 | break; |
| 119 | } |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | // Handle tray icon menu commands |
| 124 | case WM_COMMAND: |
| 125 | switch (LOWORD(wParam)) { |
| 126 | case ID_CONTR0L_PANEL: |
| 127 | CPanel->showDialog(); |
| 128 | break; |
| 129 | case ID_DISABLE_NEW_CLIENTS: |
| 130 | { |
| 131 | thread.server.getClientsInfo(&LCInfo); |
| 132 | LCInfo.setDisable(!LCInfo.getDisable()); |
| 133 | thread.server.setClientsStatus(&LCInfo); |
| 134 | CPanel->UpdateListView(&LCInfo); |
| 135 | } |
| 136 | break; |
| 137 | case ID_OPTIONS: |
| 138 | { |
| 139 | CurrentUserToken token; |
| 140 | if (token.canImpersonate()) |
| 141 | vncConfig.start(isServiceProcess() ? (HANDLE)token : INVALID_HANDLE_VALUE); |
| 142 | else |
| 143 | vlog.error("Options: unknown current user"); |
| 144 | } |
| 145 | break; |
| 146 | case ID_CONNECT: |
| 147 | { |
| 148 | CurrentUserToken token; |
| 149 | if (token.canImpersonate()) |
| 150 | vncConnect.start(isServiceProcess() ? (HANDLE)token : INVALID_HANDLE_VALUE); |
| 151 | else |
| 152 | vlog.error("Options: unknown current user"); |
| 153 | } |
| 154 | break; |
| 155 | case ID_DISCONNECT: |
| 156 | thread.server.disconnectClients("tray menu disconnect"); |
| 157 | break; |
| 158 | case ID_CLOSE: |
| 159 | if (MsgBox(0, _T("Are you sure you want to close the server?"), |
| 160 | MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON2) == IDYES) { |
| 161 | if (isServiceProcess()) { |
| 162 | ImpersonateCurrentUser icu; |
| 163 | try { |
| 164 | rfb::win32::stopService(VNCServerService::Name); |
| 165 | } catch (rdr::Exception& e) { |
| 166 | MsgBox(0, TStr(e.str()), MB_ICONERROR | MB_OK); |
| 167 | } |
| 168 | } else { |
| 169 | thread.server.stop(); |
| 170 | } |
| 171 | } |
| 172 | break; |
| 173 | case ID_ABOUT: |
| 174 | AboutDialog::instance.showDialog(); |
| 175 | break; |
| 176 | } |
| 177 | return 0; |
| 178 | |
| 179 | // Handle commands send by other processes |
| 180 | case WM_COPYDATA: |
| 181 | { |
| 182 | COPYDATASTRUCT* command = (COPYDATASTRUCT*)lParam; |
| 183 | switch (command->dwData) { |
| 184 | case 1: |
| 185 | { |
| 186 | CharArray viewer = new char[command->cbData + 1]; |
| 187 | memcpy(viewer.buf, command->lpData, command->cbData); |
| 188 | viewer.buf[command->cbData] = 0; |
| 189 | return thread.server.addNewClient(viewer.buf) ? 1 : 0; |
| 190 | } |
| 191 | case 2: |
| 192 | return thread.server.disconnectClients("IPC disconnect") ? 1 : 0; |
| 193 | case 3: |
| 194 | thread.server.setClientsStatus((rfb::ListConnInfo *)command->cbData); |
| 195 | case 4: |
| 196 | thread.server.getClientsInfo(&LCInfo); |
| 197 | CPanel->UpdateListView(&LCInfo); |
| 198 | break; |
| 199 | }; |
| 200 | }; |
| 201 | break; |
| 202 | |
| 203 | case WM_CLOSE: |
| 204 | PostQuitMessage(0); |
| 205 | break; |
| 206 | |
| 207 | case WM_TIMER: |
| 208 | if (rfb::win32::desktopChangeRequired()) { |
| 209 | SendMessage(getHandle(), WM_CLOSE, 0, 0); |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | thread.server.getClientsInfo(&LCInfo); |
| 214 | CPanel->UpdateListView(&LCInfo); |
| 215 | |
| 216 | setIcon(thread.server.isServerInUse() ? |
| 217 | (!LCInfo.getDisable() ? thread.activeIcon : thread.dis_activeIcon) : |
| 218 | (!LCInfo.getDisable() ? thread.inactiveIcon : thread.dis_inactiveIcon)); |
| 219 | |
| 220 | return 0; |
| 221 | |
| 222 | case WM_SET_TOOLTIP: |
| 223 | { |
| 224 | rfb::Lock l(thread.lock); |
| 225 | if (thread.toolTip.buf) |
| 226 | setToolTip(thread.toolTip.buf); |
| 227 | } |
| 228 | return 0; |
| 229 | |
| 230 | } |
| 231 | |
| 232 | return TrayIcon::processMessage(msg, wParam, lParam); |
| 233 | } |
| 234 | |
| 235 | protected: |
| 236 | LaunchProcess vncConfig; |
| 237 | LaunchProcess vncConnect; |
| 238 | STrayIconThread& thread; |
| 239 | ControlPanel * CPanel; |
| 240 | rfb::ListConnInfo LCInfo; |
| 241 | }; |
| 242 | |
| 243 | |
| 244 | STrayIconThread::STrayIconThread(VNCServerWin32& sm, UINT inactiveIcon_, UINT activeIcon_, |
| 245 | UINT dis_inactiveIcon_, UINT dis_activeIcon_, UINT menu_) |
| 246 | : Thread("TrayIcon"), server(sm), inactiveIcon(inactiveIcon_), activeIcon(activeIcon_), |
| 247 | dis_inactiveIcon(dis_inactiveIcon_), dis_activeIcon(dis_activeIcon_),menu(menu_), |
| 248 | windowHandle(0), runTrayIcon(true) { |
| 249 | start(); |
| 250 | } |
| 251 | |
| 252 | |
| 253 | void STrayIconThread::run() { |
| 254 | while (runTrayIcon) { |
| 255 | if (rfb::win32::desktopChangeRequired() && |
| 256 | !rfb::win32::changeDesktop()) |
| 257 | Sleep(2000); |
| 258 | |
| 259 | STrayIcon icon(*this); |
| 260 | windowHandle = icon.getHandle(); |
| 261 | |
| 262 | MSG msg; |
| 263 | while (runTrayIcon && ::GetMessage(&msg, 0, 0, 0) > 0) { |
| 264 | TranslateMessage(&msg); |
| 265 | DispatchMessage(&msg); |
| 266 | } |
| 267 | |
| 268 | windowHandle = 0; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | void STrayIconThread::setToolTip(const TCHAR* text) { |
| 273 | if (!windowHandle) return; |
| 274 | Lock l(lock); |
| 275 | delete [] toolTip.buf; |
| 276 | toolTip.buf = tstrDup(text); |
| 277 | PostMessage(windowHandle, WM_SET_TOOLTIP, 0, 0); |
| 278 | } |
| 279 | |
| 280 | |