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

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