blob: 0b10fa5b61c650c4f0e2224fee846a566fa1789e [file] [log] [blame]
Constantin Kaplinsky9eede292006-05-11 10:30:11 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00003 * 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>
Constantin Kaplinsky9eede292006-05-11 10:30:11 +000022#include <winvnc/VNCServerService.h>
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000023#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>
Constantin Kaplinsky9eede292006-05-11 10:30:11 +000030#include <rfb_win32/MsgBox.h>
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000031#include <rfb_win32/Service.h>
32#include <rfb_win32/CurrentUser.h>
Oleg Sheikinf5049ad2005-07-01 12:41:15 +000033#include <winvnc/ControlPanel.h>
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000034
35using namespace rfb;
36using namespace win32;
37using namespace winvnc;
38
39static LogWriter vlog("STrayIcon");
40
41BoolParameter STrayIconThread::disableOptions("DisableOptions", "Disable the Options entry in the VNC Server tray menu.", false);
Constantin Kaplinsky9eede292006-05-11 10:30:11 +000042BoolParameter STrayIconThread::disableClose("DisableClose", "Disable the Close entry in the VNC Server tray menu.", false);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000043
44
45//
46// -=- AboutDialog global values
47//
48
49const WORD rfb::win32::AboutDialog::DialogId = IDD_ABOUT;
50const WORD rfb::win32::AboutDialog::Copyright = IDC_COPYRIGHT;
51const WORD rfb::win32::AboutDialog::Version = IDC_VERSION;
52const WORD rfb::win32::AboutDialog::BuildTime = IDC_BUILDTIME;
53const WORD rfb::win32::AboutDialog::Description = IDC_DESCRIPTION;
54
55
56//
57// -=- Internal tray icon class
58//
59
60const UINT WM_SET_TOOLTIP = WM_USER + 1;
61
62
63class winvnc::STrayIcon : public TrayIcon {
64public:
65 STrayIcon(STrayIconThread& t) : thread(t),
66 vncConfig(_T("vncconfig.exe"), isServiceProcess() ? _T("-noconsole -service") : _T("-noconsole")),
Constantin Kaplinsky9eede292006-05-11 10:30:11 +000067 vncConnect(_T("winvnc4.exe"), _T("-noconsole -connect")) {
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000068
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);
Oleg Sheikin641f7e52005-11-22 18:04:10 +000076 CPanel = new ControlPanel(getHandle());
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000077 }
78
79 virtual LRESULT processMessage(UINT msg, WPARAM wParam, LPARAM lParam) {
80 switch(msg) {
81
82 case WM_USER:
83 {
Constantin Kaplinsky9eede292006-05-11 10:30:11 +000084 bool userKnown = CurrentUserToken().canImpersonate();
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000085 bool allowOptions = !STrayIconThread::disableOptions && userKnown;
Constantin Kaplinsky9eede292006-05-11 10:30:11 +000086 bool allowClose = !STrayIconThread::disableClose && userKnown;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000087
88 switch (lParam) {
89 case WM_LBUTTONDBLCLK:
Oleg Sheikinf5049ad2005-07-01 12:41:15 +000090 SendMessage(getHandle(), WM_COMMAND, ID_CONTR0L_PANEL, 0);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000091 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
Oleg Sheikinf5049ad2005-07-01 12:41:15 +000098 SetMenuDefaultItem(trayMenu, ID_CONTR0L_PANEL, FALSE);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000099
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);
Constantin Kaplinsky9eede292006-05-11 10:30:11 +0000103 EnableMenuItem(trayMenu, ID_CLOSE, (!allowClose ? MF_GRAYED : MF_ENABLED) | MF_BYCOMMAND);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000104
Oleg Sheikind87a7a72006-01-12 15:27:04 +0000105 thread.server.getClientsInfo(&LCInfo);
106 CheckMenuItem(trayMenu, ID_DISABLE_NEW_CLIENTS, (LCInfo.getDisable() ? MF_CHECKED : MF_UNCHECKED) | MF_BYCOMMAND);
107
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000108 // 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);
Oleg Sheikind87a7a72006-01-12 15:27:04 +0000117
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000118 break;
Oleg Sheikind87a7a72006-01-12 15:27:04 +0000119 }
120 return 0;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000121 }
122
123 // Handle tray icon menu commands
124 case WM_COMMAND:
125 switch (LOWORD(wParam)) {
Oleg Sheikind87a7a72006-01-12 15:27:04 +0000126 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;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000137 case ID_OPTIONS:
138 {
139 CurrentUserToken token;
Constantin Kaplinsky9eede292006-05-11 10:30:11 +0000140 if (token.canImpersonate())
141 vncConfig.start(isServiceProcess() ? (HANDLE)token : INVALID_HANDLE_VALUE);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000142 else
143 vlog.error("Options: unknown current user");
144 }
145 break;
146 case ID_CONNECT:
147 {
148 CurrentUserToken token;
Constantin Kaplinsky9eede292006-05-11 10:30:11 +0000149 if (token.canImpersonate())
150 vncConnect.start(isServiceProcess() ? (HANDLE)token : INVALID_HANDLE_VALUE);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000151 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:
Constantin Kaplinsky9eede292006-05-11 10:30:11 +0000159 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 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000172 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;
Constantin Kaplinsky9eede292006-05-11 10:30:11 +0000189 return thread.server.addNewClient(viewer.buf) ? 1 : 0;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000190 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000191 case 2:
Constantin Kaplinsky9eede292006-05-11 10:30:11 +0000192 return thread.server.disconnectClients("IPC disconnect") ? 1 : 0;
Oleg Sheikin4b0304f2005-12-09 10:59:12 +0000193 case 3:
194 thread.server.setClientsStatus((rfb::ListConnInfo *)command->cbData);
Oleg Sheikind87a7a72006-01-12 15:27:04 +0000195 case 4:
Constantin Kaplinskyc14a07a2005-12-09 14:54:28 +0000196 thread.server.getClientsInfo(&LCInfo);
197 CPanel->UpdateListView(&LCInfo);
Oleg Sheikin4b0304f2005-12-09 10:59:12 +0000198 break;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000199 };
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 }
Oleg Sheikin641f7e52005-11-22 18:04:10 +0000212
Oleg Sheikinff43bfd2005-12-07 08:02:52 +0000213 thread.server.getClientsInfo(&LCInfo);
Oleg Sheikin5c642e92005-12-22 20:57:58 +0000214 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));
Oleg Sheikin641f7e52005-11-22 18:04:10 +0000219
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000220 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
235protected:
236 LaunchProcess vncConfig;
237 LaunchProcess vncConnect;
238 STrayIconThread& thread;
Oleg Sheikinf5049ad2005-07-01 12:41:15 +0000239 ControlPanel * CPanel;
Oleg Sheikin641f7e52005-11-22 18:04:10 +0000240 rfb::ListConnInfo LCInfo;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000241};
242
243
Oleg Sheikin5c642e92005-12-22 20:57:58 +0000244STrayIconThread::STrayIconThread(VNCServerWin32& sm, UINT inactiveIcon_, UINT activeIcon_,
245 UINT dis_inactiveIcon_, UINT dis_activeIcon_, UINT menu_)
Constantin Kaplinsky9eede292006-05-11 10:30:11 +0000246: Thread("TrayIcon"), server(sm), inactiveIcon(inactiveIcon_), activeIcon(activeIcon_),
Oleg Sheikin5c642e92005-12-22 20:57:58 +0000247 dis_inactiveIcon(dis_inactiveIcon_), dis_activeIcon(dis_activeIcon_),menu(menu_),
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000248 windowHandle(0), runTrayIcon(true) {
249 start();
250}
251
252
253void 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
272void 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