Ejemplo n.º 1
0
/*  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;
}
Ejemplo n.º 2
0
/*  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"));

		CreatePopupMenus();	/* Create popup menus */  
		LoadAppIcon();		/* Load App Icon */
		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
		return TRUE;
		
	case WM_COMMAND:
		if ( (LOWORD(wParam) >= IDM_CONNECTMENU) && (LOWORD(wParam) < IDM_CONNECTMENU + MAX_CONFIGS) ) {
			StartOpenVPN(LOWORD(wParam) - IDM_CONNECTMENU);
		}
		if ( (LOWORD(wParam) >= IDM_DISCONNECTMENU) && (LOWORD(wParam) < IDM_DISCONNECTMENU + MAX_CONFIGS) ) {
			StopOpenVPN(LOWORD(wParam) - IDM_DISCONNECTMENU);
		}
		if ( (LOWORD(wParam) >= IDM_STATUSMENU) && (LOWORD(wParam) < IDM_STATUSMENU + MAX_CONFIGS) ) {
			ShowWindow(o.cnn[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(LOWORD(wParam) - IDM_PASSPHRASEMENU);
		}
#endif
		if (LOWORD(wParam) == IDM_PROXY) {
			ShowProxySettingsDialog();
		}
		if (LOWORD(wParam) == IDM_ABOUT) {
			DialogBox(o.hInstance, (LPCTSTR)IDD_ABOUTDIALOG, NULL, (DLGPROC)AboutDialogFunc);
		}
		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:
		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_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.cnn[i].connect_status == CONNECTED)
					SuspendOpenVPN(i);
				}

				/* Wait for all connections to suspend */
				for (i=0; i<10; i++, Sleep(500))
				if (CountConnectedState(SUSPENDING) == 0) break;
			}
			return FALSE;

		case PBT_APMRESUMESUSPEND:
		case PBT_APMRESUMECRITICAL:
			for (i=0; i<o.num_configs; i++)
			{
				/* Restart suspend connections */
				if (o.cnn[i].connect_status == SUSPENDED)
				StartOpenVPN(i);

				/* If some connection never reached SUSPENDED state */
				if (o.cnn[i].connect_status == SUSPENDING)
				StopOpenVPN(i);
			}
			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;
}