diff --git a/src/add-ons/input_server/filters/shortcut_catcher/KeyCommandMap.cpp b/src/add-ons/input_server/filters/shortcut_catcher/KeyCommandMap.cpp index 007ea767d1..cc499af9be 100644 --- a/src/add-ons/input_server/filters/shortcut_catcher/KeyCommandMap.cpp +++ b/src/add-ons/input_server/filters/shortcut_catcher/KeyCommandMap.cpp @@ -5,61 +5,60 @@ * Authors: * Jeremy Friesner */ - + #include "KeyCommandMap.h" - #include - -#include -#include -#include -#include -#include -#include #include +#include +#include +#include +#include +#include +#include - -#include "ShortcutsFilterConstants.h" #include "BitFieldTesters.h" #include "CommandActuators.h" +#include "ShortcutsFilterConstants.h" + #define FILE_UPDATED 'fiUp' + class hks { public: - hks(int32 key, BitFieldTester* t, CommandActuator* act, const BMessage& a) + hks(int32 key, BitFieldTester* tester, CommandActuator* actuator, + const BMessage& actuatorMessage) : fKey(key), - fTester(t), - fActuator(act), - fActuatorMessage(a) + fTester(tester), + fActuator(actuator), + fActuatorMessage(actuatorMessage) { - // empty } - - ~hks() + ~hks() { delete fActuator; delete fTester; } - int32 GetKey() const {return fKey;} - bool DoModifiersMatch(uint32 bits) const - { - return fTester->IsMatching(bits); - } - const BMessage& GetActuatorMsg() const {return fActuatorMessage;} - CommandActuator* GetActuator() {return fActuator;} + int32 GetKey() const + { return fKey; } + bool DoModifiersMatch(uint32 bits) const + { return fTester->IsMatching(bits); } + const BMessage& GetActuatorMsg() const + { return fActuatorMessage; } + CommandActuator* GetActuator() + { return fActuator; } private: - int32 fKey; - BitFieldTester* fTester; - CommandActuator* fActuator; - const BMessage fActuatorMessage; + int32 fKey; + BitFieldTester* fTester; + CommandActuator* fActuator; + const BMessage fActuatorMessage; }; @@ -73,7 +72,7 @@ KeyCommandMap::KeyCommandMap(const char* file) BEntry fileEntry(fFileName); if (fileEntry.InitCheck() == B_NO_ERROR) { node_ref nref; - + if (fileEntry.GetNodeRef(&nref) == B_NO_ERROR) watch_node(&nref, B_WATCH_STAT, this); } @@ -82,7 +81,8 @@ KeyCommandMap::KeyCommandMap(const char* file) PostMessage(&msg); fPort = create_port(1, SHORTCUTS_CATCHER_PORT_NAME); - _PutMessageToPort(); // advertise our BMessenger to the world + _PutMessageToPort(); + // advertise our BMessenger to the world } @@ -90,66 +90,65 @@ KeyCommandMap::~KeyCommandMap() { if (fPort >= 0) close_port(fPort); - - for (int i = fInjects.CountItems() - 1; i >= 0; i--) + + for (int i = fInjects.CountItems() - 1; i >= 0; i--) delete (BMessage*)fInjects.ItemAt(i); - - stop_watching(this); // don't know if this is necessary, but it can't hurt + + stop_watching(this); + // don't know if this is necessary, but it can't hurt _DeleteHKSList(fSpecs); delete [] fFileName; } void -KeyCommandMap::MouseMoved(const BMessage* mm) +KeyCommandMap::MouseMoved(const BMessage* mouseMoved) { // Save the mouse state for later... - fLastMouseMessage = *mm; + fLastMouseMessage = *mouseMoved; } filter_result -KeyCommandMap::KeyEvent(const BMessage* keyMsg, BList* outlist, - const BMessenger& sendTo) +KeyCommandMap::KeyEvent(const BMessage* keyMsg, BList* outlist, + const BMessenger& sendTo) { + filter_result result = B_DISPATCH_MESSAGE; uint32 modifiers; - filter_result ret = B_DISPATCH_MESSAGE; // default: pass it on + int32 key; - if (keyMsg->FindInt32("modifiers", (int32*) &modifiers) == B_NO_ERROR) { - int32 key; - if (keyMsg->FindInt32("key", &key) == B_NO_ERROR) { - if (fSyncSpecs.Lock()) { - if (fSpecs != NULL) { - int num = fSpecs->CountItems(); - - for (int i = 0; i < num; i++) { - hks* next = (hks*) fSpecs->ItemAt(i); - - if ((key == next->GetKey()) - && (next->DoModifiersMatch(modifiers))) { - void* asyncData = NULL; - ret = next->GetActuator()-> - KeyEvent(keyMsg, outlist, &asyncData, &fLastMouseMessage); - - if (asyncData) { - BMessage newMsg(*keyMsg); - newMsg.AddMessage("act", &next->GetActuatorMsg()); - newMsg.AddPointer("adata", asyncData); - sendTo.SendMessage(&newMsg); - } - } + if (keyMsg->FindInt32("modifiers", (int32*) &modifiers) == B_OK + && keyMsg->FindInt32("key", &key) == B_OK + && fSyncSpecs.Lock()) { + if (fSpecs != NULL) { + int count = fSpecs->CountItems(); + + for (int i = 0; i < count; i++) { + hks* next = (hks*) fSpecs->ItemAt(i); + + if (key == next->GetKey() + && next->DoModifiersMatch(modifiers)) { + void* asyncData = NULL; + result = next->GetActuator()->KeyEvent(keyMsg, outlist, + &asyncData, &fLastMouseMessage); + + if (asyncData != NULL) { + BMessage newMsg(*keyMsg); + newMsg.AddMessage("act", &next->GetActuatorMsg()); + newMsg.AddPointer("adata", asyncData); + sendTo.SendMessage(&newMsg); } } - fSyncSpecs.Unlock(); } } + fSyncSpecs.Unlock(); } - return ret; + return result; } void -KeyCommandMap::DrainInjectedEvents(const BMessage* keyMsg, BList* outlist, +KeyCommandMap::DrainInjectedEvents(const BMessage* keyMsg, BList* outlist, const BMessenger& sendTo) { BList temp; @@ -159,28 +158,28 @@ KeyCommandMap::DrainInjectedEvents(const BMessage* keyMsg, BList* outlist, fSyncSpecs.Unlock(); } - int is = temp.CountItems(); - for (int i = 0; i < is; i++) { + int count = temp.CountItems(); + for (int i = 0; i < count; i++) { BMessage* msg = (BMessage*)temp.ItemAt(i); - BArchivable* arc = instantiate_object(msg); - - if (arc) { - CommandActuator* act = dynamic_cast(arc); - - if (act) { + + BArchivable* archive = instantiate_object(msg); + if (archive != NULL) { + CommandActuator* actuator = dynamic_cast(archive); + if (actuator != NULL) { BMessage newMsg(*keyMsg); newMsg.what = B_KEY_DOWN; + void* asyncData = NULL; - (void) act->KeyEvent(&newMsg, outlist, &asyncData, + actuator->KeyEvent(&newMsg, outlist, &asyncData, &fLastMouseMessage); - - if (asyncData) { + + if (asyncData != NULL) { newMsg.AddMessage("act", msg); newMsg.AddPointer("adata", asyncData); sendTo.SendMessage(&newMsg); } } - delete arc; + delete archive; } delete msg; } @@ -190,81 +189,66 @@ KeyCommandMap::DrainInjectedEvents(const BMessage* keyMsg, BList* outlist, void KeyCommandMap::MessageReceived(BMessage* msg) { - switch(msg->what) { + switch (msg->what) { case EXECUTE_COMMAND: { BMessage subMsg; - if (msg->FindMessage("act", &subMsg) == B_NO_ERROR) { if (fSyncSpecs.Lock()) { fInjects.AddItem(new BMessage(subMsg)); fSyncSpecs.Unlock(); - - // This evil hack forces input_server to call Filter() on + + // This evil hack forces input_server to call Filter() on // us so we can process the injected event. BPoint lmp; status_t err = fLastMouseMessage.FindPoint("where", &lmp); - if (err == B_NO_ERROR) + if (err == B_NO_ERROR) set_mouse_position((int32)lmp.x, (int32)lmp.y); } } - break; + break; } case REPLENISH_MESSENGER: _PutMessageToPort(); - break; + break; case B_NODE_MONITOR: case FILE_UPDATED: { BMessage fileMsg; BFile file(fFileName, B_READ_ONLY); - if ((file.InitCheck() == B_NO_ERROR) - && (fileMsg.Unflatten(&file) == B_NO_ERROR)) { + if (file.InitCheck() == B_OK && fileMsg.Unflatten(&file) == B_OK) { BList* newList = new BList; - + // whatever this is set to will be deleted below. // defaults to no deletion BList* oldList = NULL; int i = 0; BMessage msg; - while (fileMsg.FindMessage("spec", i++, &msg) == B_NO_ERROR) { + while (fileMsg.FindMessage("spec", i++, &msg) == B_OK) { uint32 key; BMessage testerMsg; BMessage actMsg; - if ((msg.FindInt32("key", (int32*) &key) == B_NO_ERROR) - && ((msg.FindMessage("act", &actMsg)) == B_NO_ERROR) - && ((msg.FindMessage("modtester", &testerMsg)) - == B_NO_ERROR)) { - - BArchivable* arcObj = instantiate_object(&testerMsg); - if (arcObj) { - BitFieldTester* tester = - dynamic_cast(arcObj); - - if (tester) { - BArchivable* barcObj = - instantiate_object(&actMsg); - - if (barcObj) { - CommandActuator* act = - dynamic_cast(barcObj); - - if (act) - newList->AddItem( - new hks(key, tester, act, actMsg)); - else { - delete barcObj; - delete tester; - } - } else - delete tester; - } else - delete arcObj; - } + if (msg.FindInt32("key", (int32*) &key) == B_OK + && msg.FindMessage("act", &actMsg) == B_OK + && msg.FindMessage("modtester", &testerMsg) == B_OK) { + BArchivable* archive = instantiate_object(&testerMsg); + if (BitFieldTester* tester + = dynamic_cast(archive)) { + archive = instantiate_object(&actMsg); + if (CommandActuator* actuator + = dynamic_cast(archive)) { + newList->AddItem( + new hks(key, tester, actuator, actMsg)); + } else { + delete archive; + delete tester; + } + } else + delete archive; } } @@ -285,28 +269,30 @@ KeyCommandMap::MessageReceived(BMessage* msg) } -// Deletes an HKS-filled BList and its contents. -void KeyCommandMap::_DeleteHKSList(BList* l) +//! Deletes an HKS-filled BList and its contents. +void +KeyCommandMap::_DeleteHKSList(BList* l) { if (l != NULL) { int num = l->CountItems(); - for (int i = 0; i < num; i++) + for (int i = 0; i < num; i++) delete ((hks*) l->ItemAt(i)); delete l; } } -void KeyCommandMap::_PutMessageToPort() +void +KeyCommandMap::_PutMessageToPort() { if (fPort >= 0) { - BMessenger toMe(this); - BMessage m; - m.AddMessenger("target", toMe); + BMessage message; + message.AddMessenger("target", this); - char buf[2048]; - ssize_t fs = m.FlattenedSize(); - if ((fs <= sizeof(buf)) && (m.Flatten(buf, fs) == B_NO_ERROR)) - write_port_etc(fPort, 0, buf, fs, B_TIMEOUT, 250000); + char buffer[2048]; + ssize_t size = message.FlattenedSize(); + if (size <= (ssize_t)sizeof(buffer) + && message.Flatten(buffer, size) == B_OK) + write_port_etc(fPort, 0, buffer, size, B_TIMEOUT, 250000); } }