refactored a bit more to clean up WinMain()

git-svn-id: http://apu.dw.local/svnusers/JoshHoltrop/pppc/trunk@49 8131a0b2-b21c-1c47-bd6a-f003126495bd
This commit is contained in:
joshholtrop 2009-08-19 15:39:34 +00:00
parent 05ff66a313
commit 6714dab3e2
2 changed files with 69 additions and 50 deletions

112
pppc.cc
View File

@ -11,11 +11,7 @@
#include "pppc.h" #include "pppc.h"
using namespace std; using namespace std;
#define APP_NAME "Parallel Port Power Controller" static LRESULT WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
#define WM_ICON_NOTIFY (WM_APP + 42)
LRESULT WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void addMenuItem(HMENU hMenu, CHAR * itemstr, WORD id);
int WinMain( int WinMain(
HINSTANCE hInstance, HINSTANCE hInstance,
@ -23,57 +19,25 @@ int WinMain(
LPSTR lpCmdLine, LPSTR lpCmdLine,
int nCmdShow) int nCmdShow)
{ {
/* parse command line into an array like sane operating systems supply */
vector<string> args = parseCmdLine(lpCmdLine); vector<string> args = parseCmdLine(lpCmdLine);
PPPC myPPPC(hInstance); const char * config_file = NULL;
/* process command line arguments */
BOOL bRet; for (int sz = args.size(), i = 0; i < sz; i++)
MSG msg;
bool running = true;
while(running)
{ {
bRet = GetMessage(&msg, NULL, 0, 0); if (args[i][0] != '-')
if (bRet == -1 || bRet == 0)
{ {
break; config_file = args[i].c_str();
}
switch (msg.message)
{
case WM_PAINT:
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;
case WM_COMMAND:
switch (LOWORD(msg.wParam)) /* menu entry index */
{
case 0: /* exit */
running = false;
break;
}
break;
break;
} }
} }
return bRet; PPPC myPPPC(hInstance, config_file);
return myPPPC.mainLoop();
} }
LRESULT WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) static LRESULT WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{ {
switch (message) switch (message)
{ {
@ -82,10 +46,10 @@ LRESULT WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
case WM_CREATE: case WM_CREATE:
break; break;
case WM_CLOSE: case WM_CLOSE:
DestroyWindow(hWnd); ::DestroyWindow(hWnd);
break; break;
case WM_DESTROY: case WM_DESTROY:
PostQuitMessage(0); ::PostQuitMessage(0);
break; break;
case WM_ICON_NOTIFY: case WM_ICON_NOTIFY:
if (wParam == 0) if (wParam == 0)
@ -102,7 +66,7 @@ LRESULT WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
// BUGFIX: See "PRB: Menus for Notification Icons Don't Work Correctly" // BUGFIX: See "PRB: Menus for Notification Icons Don't Work Correctly"
::PostMessage(hWnd, WM_NULL, 0, 0); ::PostMessage(hWnd, WM_NULL, 0, 0);
DestroyMenu(hMenu); ::DestroyMenu(hMenu);
} }
} }
break; break;
@ -185,6 +149,54 @@ PPPC::PPPC(HINSTANCE hInstance, const char * config_file)
} }
BOOL PPPC::mainLoop()
{
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:
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;
case WM_COMMAND:
switch (LOWORD(msg.wParam)) /* menu entry index */
{
case 0: /* exit */
running = false;
break;
}
break;
break;
}
}
return bRet;
}
PPPC::~PPPC() PPPC::~PPPC()
{ {
/* delete the tray icon */ /* delete the tray icon */

7
pppc.h
View File

@ -2,15 +2,22 @@
#ifndef PPPC_H #ifndef PPPC_H
#define PPPC_H #define PPPC_H
#define APP_NAME "Parallel Port Power Controller"
#define WM_ICON_NOTIFY (WM_APP + 42)
class PPPC class PPPC
{ {
public: public:
PPPC(HINSTANCE hInstance, const char * config_file = NULL); PPPC(HINSTANCE hInstance, const char * config_file = NULL);
~PPPC(); ~PPPC();
BOOL mainLoop();
protected: protected:
HWND m_hWnd; HWND m_hWnd;
NOTIFYICONDATA m_nid; NOTIFYICONDATA m_nid;
}; };
/* non class-member functions */
void addMenuItem(HMENU hMenu, CHAR * itemstr, WORD id);
#endif #endif