pppc/pppc.cc
2009-07-29 22:16:46 +00:00

147 lines
4.0 KiB
C++

#include <windows.h>
#include <string.h>
#include <iostream>
#include "session_notification.h"
#include "pport.h"
#include "resources.h"
using namespace std;
#define APP_NAME "Parallel Port Power Controller"
#define WM_ICON_NOTIFY (WM_APP + 42)
LRESULT WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
static HINSTANCE m_hInstance;
int WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
m_hInstance = hInstance;
/* Load the application icon */
HICON icon = ::LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
if (icon == 0)
{
cerr << "Warning: LoadIcon(): " << GetLastError() << endl;
}
/* Create the TrayIconClass class */
WNDCLASSEX wcex;
memset(&wcex, 0, sizeof(wcex));
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wcex.lpfnWndProc = (WNDPROC)WindowProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = icon;
wcex.hCursor = 0;
wcex.hbrBackground = 0;
wcex.lpszMenuName = 0;
wcex.lpszClassName = "TrayIconClass";
wcex.hIconSm = 0;
RegisterClassEx(&wcex);
/* Create the tray icon window */
HWND hWnd = ::CreateWindow("TrayIconClass", "", WS_POPUP,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL, 0,
hInstance, 0);
if (hWnd == NULL)
{
cerr << "Warning: CreateWindow(): " << GetLastError() << endl;
}
/* Create the notify icon data */
NOTIFYICONDATA nid;
memset(&nid, 0, sizeof(nid));
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hWnd;
nid.uID = 0;
nid.hIcon = icon;
nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
nid.uCallbackMessage = WM_ICON_NOTIFY;
strncpy(nid.szTip, APP_NAME, sizeof(nid.szTip));
BOOL bResult = Shell_NotifyIcon(NIM_ADD, &nid);
if (bResult == 0)
{
cerr << "Warning: Shell_NotifyIcon(): " << GetLastError() << endl;
}
/* begin capturing session notifications */
session_notification_enable(hWnd);
BOOL bRet;
MSG msg;
bool running = true;
while(running)
{
bRet = GetMessage(&msg, NULL, 0, 0);
if (bRet == -1 || bRet == 0)
{
break;
}
switch (msg.message)
{
case WM_PAINT:
cerr << "WM_PAINT" << endl;
break;
case WM_COMMAND:
cerr << "WM_COMMAND" << endl;
break;
case WM_CLOSE:
case WM_DESTROY:
case WM_QUIT:
running = false;
break;
case WM_WTSSESSION_CHANGE:
if (msg.wParam == WTS_SESSION_LOGON
|| msg.wParam == WTS_SESSION_UNLOCK)
{
pport_write(0xFF);
}
if (msg.wParam == WTS_SESSION_LOGOFF
|| msg.wParam == WTS_SESSION_LOCK)
{
pport_write(0x0);
}
break;
}
}
/* stop listening for session notifications */
session_notification_disable(hWnd);
return bRet;
}
LRESULT WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_NCCREATE:
return 1;
case WM_CREATE:
break;
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_ICON_NOTIFY:
cerr << "WM_ICON_NOTIFY" << endl;
break;
}
return 0;
}