You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

STrayIcon.cxx 8.4KB

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