From 2dd4be5a6247eb967789b3ed2cca0f90d015ed93 Mon Sep 17 00:00:00 2001 From: joshholtrop Date: Wed, 19 Aug 2009 18:48:24 +0000 Subject: [PATCH] added Config::reset() to support re-reading the config file, added PPPC::event_*() stubs to handle events and PPPC::update_pins() to write to the pport git-svn-id: http://apu.dw.local/svnusers/JoshHoltrop/pppc/trunk@56 8131a0b2-b21c-1c47-bd6a-f003126495bd --- Config.cc | 13 ++++++++++++- Config.h | 1 + pppc.cc | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++---- pppc.h | 8 ++++++++ 4 files changed, 74 insertions(+), 5 deletions(-) diff --git a/Config.cc b/Config.cc index d20d517..0961c05 100644 --- a/Config.cc +++ b/Config.cc @@ -17,12 +17,19 @@ Config::Entry::Entry(const string & name) } Config::Config() +{ + reset(); +} + +void Config::reset() { port = "LPT1"; /* default to first parallel port */ + entries.clear(); /* erase any entries previously configured */ } void Config::read(const char * file) { + reset(); ifstream ifs(file); if (ifs.is_open()) { @@ -68,7 +75,11 @@ void Config::read(const char * file) { if (parts.first == "pin") { - entries[entry_num].pin = atoi(parts.second.c_str()); + int pin = atoi(parts.second.c_str()); + if (pin >= 0 && pin <= 7) + entries[entry_num].pin = pin; + else + cerr << "Bad pin #: " << parts.second << endl; } else if (parts.first == "on_start") { diff --git a/Config.h b/Config.h index d8ede6e..c120192 100644 --- a/Config.h +++ b/Config.h @@ -31,6 +31,7 @@ class Config void read(const char * file); protected: + void reset(); std::pair split(const std::string & line); Action parseAction(const std::string & str); }; diff --git a/pppc.cc b/pppc.cc index 1849bc4..89218a7 100644 --- a/pppc.cc +++ b/pppc.cc @@ -11,6 +11,11 @@ #include "pppc.h" using namespace std; +#define SET_BIT(var,position,value) \ + (var) = ((var) & (~(1 << (position)))) | ((value) << (position)) +#define GET_BIT(var,position) \ + (((var) >> (position)) & 0x1) + static LRESULT WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); int WinMain( @@ -91,11 +96,15 @@ void addMenuItem(HMENU hMenu, CHAR * itemstr, WORD id) PPPC::PPPC(HINSTANCE hInstance, const char * config_file) { + m_pins = 0u; + if (config_file != NULL) { m_config.read(config_file); } + event_start(); + /* Load the application icon */ HICON icon = ::LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1)); if (icon == 0) @@ -151,7 +160,6 @@ PPPC::PPPC(HINSTANCE hInstance, const char * config_file) /* begin capturing session notifications */ session_notification_enable(m_hWnd); - } BOOL PPPC::mainLoop() @@ -179,12 +187,12 @@ BOOL PPPC::mainLoop() if (msg.wParam == WTS_SESSION_LOGON || msg.wParam == WTS_SESSION_UNLOCK) { - pport_write(0xFF); + event_unlock(); } if (msg.wParam == WTS_SESSION_LOGOFF || msg.wParam == WTS_SESSION_LOCK) { - pport_write(0x0); + event_lock(); } break; case WM_COMMAND: @@ -195,7 +203,6 @@ BOOL PPPC::mainLoop() break; } break; - break; } } @@ -204,9 +211,51 @@ BOOL PPPC::mainLoop() PPPC::~PPPC() { + event_exit(); + /* delete the tray icon */ Shell_NotifyIcon(NIM_DELETE, &m_nid); /* stop listening for session notifications */ session_notification_disable(m_hWnd); } + +void PPPC::event_start() +{ + for (int sz = m_config.entries.size(), i = 0; i < sz; i++) + { + switch (m_config.entries[i].on_start) + { + case Config::ON: + SET_BIT(m_pins, m_config.entries[i].pin, 1); + break; + case Config::OFF: + SET_BIT(m_pins, m_config.entries[i].pin, 0); + break; + default: + break; + } + } + update_pins(); +} + +void PPPC::event_exit() +{ + update_pins(); +} + +void PPPC::event_lock() +{ + update_pins(); + m_previous_pins = m_pins; +} + +void PPPC::event_unlock() +{ + update_pins(); +} + +void PPPC::update_pins() +{ + pport_write(m_pins); +} diff --git a/pppc.h b/pppc.h index f2bafca..20ca4dd 100644 --- a/pppc.h +++ b/pppc.h @@ -15,9 +15,17 @@ class PPPC BOOL mainLoop(); protected: + void event_start(); + void event_exit(); + void event_lock(); + void event_unlock(); + void update_pins(); + Config m_config; HWND m_hWnd; NOTIFYICONDATA m_nid; + unsigned char m_pins; + unsigned char m_previous_pins; }; /* non class-member functions */