/* * Handle mouse clicks on tray icon */ void OnNotifyTray(LPARAM lParam) { POINT pt; switch (lParam) { case WM_RBUTTONUP: /* Recreate popup menus */ DestroyPopupMenus(); BuildFileList(); CreatePopupMenus(); GetCursorPos(&pt); SetForegroundWindow(o.hWnd); TrackPopupMenu(hMenu, TPM_RIGHTALIGN, pt.x, pt.y, 0, o.hWnd, NULL); PostMessage(o.hWnd, WM_NULL, 0, 0); break; case WM_LBUTTONDBLCLK: if (o.service_only) { /* Start or stop OpenVPN service */ if (o.service_state == service_disconnected) { MyStartService(); } else if (o.service_state == service_connected && ShowLocalizedMsgEx(MB_YESNO, _T(PACKAGE_NAME), IDS_MENU_ASK_STOP_SERVICE) == IDYES) { MyStopService(); } } else { int disconnected_conns = CountConnState(disconnected); DestroyPopupMenus(); BuildFileList(); CreatePopupMenus(); /* Start connection if only one config exist */ if (o.num_configs == 1 && o.conn[0].state == disconnected) StartOpenVPN(&o.conn[0]); else if (disconnected_conns == o.num_configs - 1) { /* Show status window if only one connection is running */ int i; for (i = 0; i < o.num_configs; i++) { if (o.conn[i].state != disconnected) { ShowWindow(o.conn[i].hwndStatus, SW_SHOW); SetForegroundWindow(o.conn[i].hwndStatus); break; } } } } break; } }
void CheckAndSetTrayIcon() { if (o.service_state == service_connected) { SetTrayIcon(connected); return; } if (CountConnState(connected) != 0) { SetTrayIcon(connected); } else { if (CountConnState(connecting) != 0 || CountConnState(reconnecting) != 0 || CountConnState(resuming) != 0 || o.service_state == service_connecting) SetTrayIcon(connecting); else SetTrayIcon(disconnected); } }
static void StopAllOpenVPN() { int i; for (i = 0; i < o.num_configs; i++) { if (o.conn[i].state != disconnected) StopOpenVPN(&o.conn[i]); } /* Wait for all connections to terminate (Max 5 sec) */ for (i = 0; i < 20; i++, Sleep(250)) { if (CountConnState(disconnected) == o.num_configs) break; } }
/* This function is called by the Windows function DispatchMessage() */ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { static UINT s_uTaskbarRestart; int i; switch (message) { case WM_CREATE: /* Save Window Handle */ o.hWnd = hwnd; s_uTaskbarRestart = RegisterWindowMessage(TEXT("TaskbarCreated")); WTSRegisterSessionNotification(hwnd, NOTIFY_FOR_THIS_SESSION); /* Load application icon */ HICON hIcon = LoadLocalizedIcon(ID_ICO_APP); if (hIcon) { SendMessage(hwnd, WM_SETICON, (WPARAM) (ICON_SMALL), (LPARAM) (hIcon)); SendMessage(hwnd, WM_SETICON, (WPARAM) (ICON_BIG), (LPARAM) (hIcon)); } CreatePopupMenus(); /* Create popup menus */ ShowTrayIcon(); if (o.allow_service[0]=='1' || o.service_only[0]=='1') CheckServiceStatus(); // Check if service is running or not if (!AutoStartConnections()) { SendMessage(hwnd, WM_CLOSE, 0, 0); break; } break; case WM_NOTIFYICONTRAY: OnNotifyTray(lParam); // Manages message from tray break; case WM_COMMAND: if ( (LOWORD(wParam) >= IDM_CONNECTMENU) && (LOWORD(wParam) < IDM_CONNECTMENU + MAX_CONFIGS) ) { StartOpenVPN(&o.conn[LOWORD(wParam) - IDM_CONNECTMENU]); } if ( (LOWORD(wParam) >= IDM_DISCONNECTMENU) && (LOWORD(wParam) < IDM_DISCONNECTMENU + MAX_CONFIGS) ) { StopOpenVPN(&o.conn[LOWORD(wParam) - IDM_DISCONNECTMENU]); } if ( (LOWORD(wParam) >= IDM_STATUSMENU) && (LOWORD(wParam) < IDM_STATUSMENU + MAX_CONFIGS) ) { ShowWindow(o.conn[LOWORD(wParam) - IDM_STATUSMENU].hwndStatus, SW_SHOW); } if ( (LOWORD(wParam) >= IDM_VIEWLOGMENU) && (LOWORD(wParam) < IDM_VIEWLOGMENU + MAX_CONFIGS) ) { ViewLog(LOWORD(wParam) - IDM_VIEWLOGMENU); } if ( (LOWORD(wParam) >= IDM_EDITMENU) && (LOWORD(wParam) < IDM_EDITMENU + MAX_CONFIGS) ) { EditConfig(LOWORD(wParam) - IDM_EDITMENU); } #ifndef DISABLE_CHANGE_PASSWORD if ( (LOWORD(wParam) >= IDM_PASSPHRASEMENU) && (LOWORD(wParam) < IDM_PASSPHRASEMENU + MAX_CONFIGS) ) { ShowChangePassphraseDialog(&o.conn[LOWORD(wParam) - IDM_PASSPHRASEMENU]); } #endif if (LOWORD(wParam) == IDM_SETTINGS) { ShowSettingsDialog(); } if (LOWORD(wParam) == IDM_CLOSE) { CloseApplication(hwnd); } if (LOWORD(wParam) == IDM_SERVICE_START) { MyStartService(); } if (LOWORD(wParam) == IDM_SERVICE_STOP) { MyStopService(); } if (LOWORD(wParam) == IDM_SERVICE_RESTART) MyReStartService(); break; case WM_CLOSE: CloseApplication(hwnd); break; case WM_DESTROY: WTSUnRegisterSessionNotification(hwnd); StopAllOpenVPN(); OnDestroyTray(); /* Remove Tray Icon and destroy menus */ PostQuitMessage (0); /* Send a WM_QUIT to the message queue */ break; case WM_QUERYENDSESSION: return(TRUE); case WM_ENDSESSION: StopAllOpenVPN(); OnDestroyTray(); break; case WM_WTSSESSION_CHANGE: switch (wParam) { case WTS_SESSION_LOCK: o.session_locked = TRUE; break; case WTS_SESSION_UNLOCK: o.session_locked = FALSE; if (CountConnState(suspended) != 0) ResumeConnections(); break; } break; case WM_POWERBROADCAST: switch (wParam) { case PBT_APMSUSPEND: if (o.disconnect_on_suspend[0] == '1') { /* Suspend running connections */ for (i=0; i<o.num_configs; i++) { if (o.conn[i].state == connected) SuspendOpenVPN(i); } /* Wait for all connections to suspend */ for (i=0; i<10; i++, Sleep(500)) if (CountConnState(suspending) == 0) break; } return FALSE; case PBT_APMRESUMESUSPEND: case PBT_APMRESUMECRITICAL: if (CountConnState(suspended) != 0 && !o.session_locked) ResumeConnections(); return FALSE; } default: /* for messages that we don't deal with */ if (message == s_uTaskbarRestart) { /* Explorer has restarted, re-register the tray icon. */ ShowTrayIcon(); CheckAndSetTrayIcon(); break; } return DefWindowProc (hwnd, message, wParam, lParam); } return 0; }
void SetTrayIcon(conn_state_t state) { TCHAR msg[500]; TCHAR msg_connected[100]; TCHAR msg_connecting[100]; int i, config = 0; BOOL first_conn; UINT icon_id; _tcsncpy(msg, LoadLocalizedString(IDS_TIP_DEFAULT), _countof(ni.szTip)); _tcsncpy(msg_connected, LoadLocalizedString(IDS_TIP_CONNECTED), _countof(msg_connected)); _tcsncpy(msg_connecting, LoadLocalizedString(IDS_TIP_CONNECTING), _countof(msg_connecting)); first_conn = TRUE; for (i = 0; i < o.num_configs; i++) { if (o.conn[i].state == connected) { /* Append connection name to Icon Tip Msg */ _tcsncat(msg, (first_conn ? msg_connected : _T(", ")), _countof(msg) - _tcslen(msg) - 1); _tcsncat(msg, o.conn[i].config_name, _countof(msg) - _tcslen(msg) - 1); first_conn = FALSE; config = i; } } first_conn = TRUE; for (i = 0; i < o.num_configs; i++) { if (o.conn[i].state == connecting || o.conn[i].state == resuming || o.conn[i].state == reconnecting) { /* Append connection name to Icon Tip Msg */ _tcsncat(msg, (first_conn ? msg_connecting : _T(", ")), _countof(msg) - _tcslen(msg) - 1); _tcsncat(msg, o.conn[i].config_name, _countof(msg) - _tcslen(msg) - 1); first_conn = FALSE; } } if (CountConnState(connected) == 1) { /* Append "Connected since and assigned IP" to message */ TCHAR time[50]; LocalizedTime(o.conn[config].connected_since, time, _countof(time)); _tcsncat(msg, LoadLocalizedString(IDS_TIP_CONNECTED_SINCE), _countof(msg) - _tcslen(msg) - 1); _tcsncat(msg, time, _countof(msg) - _tcslen(msg) - 1); if (_tcslen(o.conn[config].ip) > 0) { TCHAR *assigned_ip = LoadLocalizedString(IDS_TIP_ASSIGNED_IP, o.conn[config].ip); _tcsncat(msg, assigned_ip, _countof(msg) - _tcslen(msg) - 1); } } icon_id = ID_ICO_CONNECTING; if (state == connected) icon_id = ID_ICO_CONNECTED; else if (state == disconnected) icon_id = ID_ICO_DISCONNECTED; ni.cbSize = sizeof(ni); ni.uID = 0; ni.hWnd = o.hWnd; ni.hIcon = LoadLocalizedIcon(icon_id); ni.uFlags = NIF_MESSAGE | NIF_TIP | NIF_ICON; ni.uCallbackMessage = WM_NOTIFYICONTRAY; _tcsncpy(ni.szTip, msg, _countof(ni.szTip)); Shell_NotifyIcon(NIM_MODIFY, &ni); }