blob: b13f3f19aea7a2fb2af6478ed3a794d0688dcdf1 [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:
Pierre Ossmanb1cd6ca2015-03-03 16:37:43 +010066 STrayIcon(STrayIconThread& t) :
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000067 vncConfig(_T("vncconfig.exe"), isServiceProcess() ? _T("-noconsole -service") : _T("-noconsole")),
Pierre Ossmanb1cd6ca2015-03-03 16:37:43 +010068 vncConnect(_T("winvnc4.exe"), _T("-noconsole -connect")), thread(t) {
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000069
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:
Samuel Mannehed60c41932014-02-07 14:53:24 +0000139 vncConfig.start(INVALID_HANDLE_VALUE);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000140 break;
141 case ID_CONNECT:
Samuel Mannehed60c41932014-02-07 14:53:24 +0000142 vncConnect.start(INVALID_HANDLE_VALUE);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000143 break;
144 case ID_DISCONNECT:
145 thread.server.disconnectClients("tray menu disconnect");
146 break;
147 case ID_CLOSE:
148 if (MsgBox(0, _T("Are you sure you want to close the server?"),
149 MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON2) == IDYES) {
150 if (isServiceProcess()) {
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000151 try {
152 rfb::win32::stopService(VNCServerService::Name);
153 } catch (rdr::Exception& e) {
154 MsgBox(0, TStr(e.str()), MB_ICONERROR | MB_OK);
155 }
156 } else {
157 thread.server.stop();
158 }
159 }
160 break;
161 case ID_ABOUT:
162 AboutDialog::instance.showDialog();
163 break;
164 }
165 return 0;
166
167 // Handle commands send by other processes
168 case WM_COPYDATA:
169 {
170 COPYDATASTRUCT* command = (COPYDATASTRUCT*)lParam;
171 switch (command->dwData) {
172 case 1:
173 {
Adam Tkac934f63c2009-10-12 15:54:59 +0000174 CharArray viewer(command->cbData + 1);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000175 memcpy(viewer.buf, command->lpData, command->cbData);
176 viewer.buf[command->cbData] = 0;
177 return thread.server.addNewClient(viewer.buf) ? 1 : 0;
178 }
179 case 2:
180 return thread.server.disconnectClients("IPC disconnect") ? 1 : 0;
181 case 3:
182 thread.server.setClientsStatus((rfb::ListConnInfo *)command->cbData);
183 case 4:
184 thread.server.getClientsInfo(&LCInfo);
185 CPanel->UpdateListView(&LCInfo);
186 break;
187 };
188 };
189 break;
190
191 case WM_CLOSE:
192 PostQuitMessage(0);
193 break;
194
195 case WM_TIMER:
196 if (rfb::win32::desktopChangeRequired()) {
197 SendMessage(getHandle(), WM_CLOSE, 0, 0);
198 return 0;
199 }
200
201 thread.server.getClientsInfo(&LCInfo);
202 CPanel->UpdateListView(&LCInfo);
203
204 setIcon(thread.server.isServerInUse() ?
205 (!LCInfo.getDisable() ? thread.activeIcon : thread.dis_activeIcon) :
206 (!LCInfo.getDisable() ? thread.inactiveIcon : thread.dis_inactiveIcon));
207
208 return 0;
209
210 case WM_SET_TOOLTIP:
211 {
212 rfb::Lock l(thread.lock);
213 if (thread.toolTip.buf)
214 setToolTip(thread.toolTip.buf);
215 }
216 return 0;
217
218 }
219
220 return TrayIcon::processMessage(msg, wParam, lParam);
221 }
222
223protected:
224 LaunchProcess vncConfig;
225 LaunchProcess vncConnect;
226 STrayIconThread& thread;
227 ControlPanel * CPanel;
228 rfb::ListConnInfo LCInfo;
229};
230
231
232STrayIconThread::STrayIconThread(VNCServerWin32& sm, UINT inactiveIcon_, UINT activeIcon_,
233 UINT dis_inactiveIcon_, UINT dis_activeIcon_, UINT menu_)
Pierre Ossmanb1cd6ca2015-03-03 16:37:43 +0100234: Thread("TrayIcon"), windowHandle(0), server(sm),
235 inactiveIcon(inactiveIcon_), activeIcon(activeIcon_),
236 dis_inactiveIcon(dis_inactiveIcon_), dis_activeIcon(dis_activeIcon_),
237 menu(menu_), runTrayIcon(true) {
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000238 start();
239}
240
241
242void STrayIconThread::run() {
243 while (runTrayIcon) {
244 if (rfb::win32::desktopChangeRequired() &&
245 !rfb::win32::changeDesktop())
246 Sleep(2000);
247
248 STrayIcon icon(*this);
249 windowHandle = icon.getHandle();
250
251 MSG msg;
252 while (runTrayIcon && ::GetMessage(&msg, 0, 0, 0) > 0) {
253 TranslateMessage(&msg);
254 DispatchMessage(&msg);
255 }
256
257 windowHandle = 0;
258 }
259}
260
261void STrayIconThread::setToolTip(const TCHAR* text) {
262 if (!windowHandle) return;
263 Lock l(lock);
264 delete [] toolTip.buf;
265 toolTip.buf = tstrDup(text);
266 PostMessage(windowHandle, WM_SET_TOOLTIP, 0, 0);
267}
268
Adam Tkac934f63c2009-10-12 15:54:59 +0000269}
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000270