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