blob: fa483ee0c274fb1d257ae24b007d7ae2b0edaeeb [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
Pierre Ossman338e73a2016-07-07 15:35:13 +020025#include <os/Mutex.h>
26#include <os/Thread.h>
27
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000028#include <rfb/LogWriter.h>
29#include <rfb/Configuration.h>
Pierre Ossman338e73a2016-07-07 15:35:13 +020030
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000031#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 Ossman338e73a2016-07-07 15:35:13 +020037
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000038#include <winvnc/ControlPanel.h>
39
40using namespace rfb;
41using namespace win32;
42using namespace winvnc;
43
44static LogWriter vlog("STrayIcon");
45
46BoolParameter STrayIconThread::disableOptions("DisableOptions", "Disable the Options entry in the VNC Server tray menu.", false);
47BoolParameter STrayIconThread::disableClose("DisableClose", "Disable the Close entry in the VNC Server tray menu.", false);
48
49
50//
51// -=- AboutDialog global values
52//
53
54const WORD rfb::win32::AboutDialog::DialogId = IDD_ABOUT;
55const WORD rfb::win32::AboutDialog::Copyright = IDC_COPYRIGHT;
56const WORD rfb::win32::AboutDialog::Version = IDC_VERSION;
57const WORD rfb::win32::AboutDialog::BuildTime = IDC_BUILDTIME;
58const WORD rfb::win32::AboutDialog::Description = IDC_DESCRIPTION;
59
60
61//
62// -=- Internal tray icon class
63//
64
65const UINT WM_SET_TOOLTIP = WM_USER + 1;
66
Adam Tkac934f63c2009-10-12 15:54:59 +000067namespace winvnc {
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000068
Adam Tkac934f63c2009-10-12 15:54:59 +000069class STrayIcon : public TrayIcon {
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000070public:
Pierre Ossmanb1cd6ca2015-03-03 16:37:43 +010071 STrayIcon(STrayIconThread& t) :
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000072 vncConfig(_T("vncconfig.exe"), isServiceProcess() ? _T("-noconsole -service") : _T("-noconsole")),
Pierre Ossmanb1cd6ca2015-03-03 16:37:43 +010073 vncConnect(_T("winvnc4.exe"), _T("-noconsole -connect")), thread(t) {
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000074
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 Mannehed60c41932014-02-07 14:53:24 +0000144 vncConfig.start(INVALID_HANDLE_VALUE);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000145 break;
146 case ID_CONNECT:
Samuel Mannehed60c41932014-02-07 14:53:24 +0000147 vncConnect.start(INVALID_HANDLE_VALUE);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000148 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 Kaplinsky729598c2006-05-25 05:12:25 +0000156 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 Tkac934f63c2009-10-12 15:54:59 +0000179 CharArray viewer(command->cbData + 1);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000180 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 Ossman025326d2018-10-08 16:03:01 +0200187 thread.server.setClientsStatus((ListConnInfo *)command->lpData);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000188 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 Ossman338e73a2016-07-07 15:35:13 +0200217 os::AutoMutex a(thread.lock);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000218 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
228protected:
229 LaunchProcess vncConfig;
230 LaunchProcess vncConnect;
231 STrayIconThread& thread;
232 ControlPanel * CPanel;
Pierre Ossman025326d2018-10-08 16:03:01 +0200233 ListConnInfo LCInfo;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000234};
235
236
237STrayIconThread::STrayIconThread(VNCServerWin32& sm, UINT inactiveIcon_, UINT activeIcon_,
238 UINT dis_inactiveIcon_, UINT dis_activeIcon_, UINT menu_)
Pierre Ossman338e73a2016-07-07 15:35:13 +0200239: thread_id(-1), windowHandle(0), server(sm),
Pierre Ossmanb1cd6ca2015-03-03 16:37:43 +0100240 inactiveIcon(inactiveIcon_), activeIcon(activeIcon_),
241 dis_inactiveIcon(dis_inactiveIcon_), dis_activeIcon(dis_activeIcon_),
242 menu(menu_), runTrayIcon(true) {
Pierre Ossman338e73a2016-07-07 15:35:13 +0200243 lock = new os::Mutex;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000244 start();
Pierre Ossman338e73a2016-07-07 15:35:13 +0200245 while (thread_id == (DWORD)-1)
246 Sleep(0);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000247}
248
Pierre Ossman338e73a2016-07-07 15:35:13 +0200249STrayIconThread::~STrayIconThread() {
250 runTrayIcon = false;
251 PostThreadMessage(thread_id, WM_QUIT, 0, 0);
252 delete lock;
253}
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000254
Pierre Ossman338e73a2016-07-07 15:35:13 +0200255void STrayIconThread::worker() {
256 thread_id = GetCurrentThreadId();
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000257 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
275void STrayIconThread::setToolTip(const TCHAR* text) {
276 if (!windowHandle) return;
Pierre Ossman338e73a2016-07-07 15:35:13 +0200277 os::AutoMutex a(lock);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000278 delete [] toolTip.buf;
279 toolTip.buf = tstrDup(text);
280 PostMessage(windowHandle, WM_SET_TOOLTIP, 0, 0);
281}
282
Adam Tkac934f63c2009-10-12 15:54:59 +0000283}
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000284