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.3KB

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