Skip to content

Commit 79b5892

Browse files
committed
Implement internal function for nested loops.
The function returns after callback run on method call or signal receipt.
1 parent 5381b20 commit 79b5892

File tree

2 files changed

+64
-67
lines changed

2 files changed

+64
-67
lines changed

src/lib/dbus/GKDBus.cpp

Lines changed: 62 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*
33
* This file is part of GLogiK project.
44
* GLogiK, daemon to handle special features on gaming keyboards
5-
* Copyright (C) 2016-2020 Fabrice Delliaux <[email protected]>
5+
* Copyright (C) 2016-2021 Fabrice Delliaux <[email protected]>
66
*
77
* This program is free software: you can redistribute it and/or modify
88
* it under the terms of the GNU General Public License as published by
@@ -164,6 +164,66 @@ void GKDBus::checkForMessages(void) noexcept
164164
* --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
165165
*/
166166

167+
void GKDBus::checkDBusMessage(DBusConnection* connection)
168+
{
169+
const std::string object = this->getObjectFromObjectPath(toString(dbus_message_get_path(_message)));
170+
171+
for(const auto & objectPair : _DBusEvents.at(GKDBusEvents::currentBus)) {
172+
/* handle root node introspection special case */
173+
if( object != this->getRootNode() )
174+
/* object must match */
175+
if(object != objectPair.first) {
176+
#if 0 && DEBUGGING_ON
177+
LOG(DEBUG3) << "skipping " << objectPair.first << " object - not " << object;
178+
#endif
179+
continue;
180+
}
181+
182+
for(const auto & interfacePair : objectPair.second) {
183+
const char* interface = interfacePair.first.c_str();
184+
#if 0 && DEBUGGING_ON
185+
LOG(DEBUG2) << "checking " << interface << " interface";
186+
#endif
187+
188+
for(const auto & DBusEvent : interfacePair.second) { /* vector of pointers */
189+
const char* eventName = DBusEvent->eventName.c_str();
190+
#if 0 && DEBUGGING_ON
191+
LOG(DEBUG3) << "checking " << eventName << " event";
192+
#endif
193+
switch(DBusEvent->eventType) {
194+
case GKDBusEventType::GKDBUS_EVENT_METHOD:
195+
{
196+
if( dbus_message_is_method_call(_message, interface, eventName) )
197+
{
198+
#if DEBUGGING_ON
199+
LOG(DEBUG1) << "DBus " << eventName << " method called !";
200+
#endif
201+
DBusEvent->runCallback(connection, _message);
202+
return;
203+
}
204+
break;
205+
}
206+
case GKDBusEventType::GKDBUS_EVENT_SIGNAL:
207+
{
208+
if( dbus_message_is_signal(_message, interface, eventName) )
209+
{
210+
#if DEBUGGING_ON
211+
LOG(DEBUG1) << "DBus " << eventName << " signal receipted !";
212+
#endif
213+
DBusEvent->runCallback(connection, _message);
214+
return;
215+
}
216+
break;
217+
}
218+
default:
219+
throw GLogiKExcept("wrong event type");
220+
break;
221+
}
222+
}
223+
}
224+
}
225+
}
226+
167227
/* -- */
168228
/*
169229
* check if :
@@ -192,67 +252,11 @@ void GKDBus::checkForBusMessages(const BusConnection bus, DBusConnection* connec
192252
}
193253

194254
try {
195-
const std::string object = this->getObjectFromObjectPath(toString(dbus_message_get_path(_message)));
196-
197-
for(const auto & objectPair : _DBusEvents.at(GKDBusEvents::currentBus)) {
198-
/* handle root node introspection special case */
199-
if( object != this->getRootNode() )
200-
/* object must match */
201-
if(object != objectPair.first) {
202-
#if 0 && DEBUGGING_ON
203-
LOG(DEBUG3) << "skipping " << objectPair.first << " object - not " << object;
204-
#endif
205-
continue;
206-
}
207-
208-
for(const auto & interfacePair : objectPair.second) {
209-
const char* interface = interfacePair.first.c_str();
210-
#if 0 && DEBUGGING_ON
211-
LOG(DEBUG2) << "checking " << interface << " interface";
212-
#endif
213-
214-
for(const auto & DBusEvent : interfacePair.second) { /* vector of pointers */
215-
const char* eventName = DBusEvent->eventName.c_str();
216-
#if 0 && DEBUGGING_ON
217-
LOG(DEBUG3) << "checking " << eventName << " event";
218-
#endif
219-
switch(DBusEvent->eventType) {
220-
case GKDBusEventType::GKDBUS_EVENT_METHOD: {
221-
if( dbus_message_is_method_call(_message, interface, eventName) ) {
222-
#if DEBUGGING_ON
223-
LOG(DEBUG1) << "DBus " << eventName << " method called !";
224-
#endif
225-
DBusEvent->runCallback(connection, _message);
226-
throw GKDBusEventFound();
227-
}
228-
break;
229-
}
230-
case GKDBusEventType::GKDBUS_EVENT_SIGNAL: {
231-
if( dbus_message_is_signal(_message, interface, eventName) ) {
232-
#if DEBUGGING_ON
233-
LOG(DEBUG1) << "DBus " << eventName << " signal receipted !";
234-
#endif
235-
DBusEvent->runCallback(connection, _message);
236-
throw GKDBusEventFound();
237-
}
238-
break;
239-
}
240-
default:
241-
throw GLogiKExcept("wrong event type");
242-
break;
243-
}
244-
}
245-
}
246-
}
255+
this->checkDBusMessage(connection);
247256
}
248257
catch (const std::out_of_range& oor) {
249258
LOG(ERROR) << "current bus connection oor";
250259
}
251-
catch ( const GKDBusEventFound & e ) {
252-
#if 0 && DEBUGGING_ON
253-
LOG(DEBUG2) << e.what();
254-
#endif
255-
}
256260
catch ( const GLogiKExcept & e ) {
257261
LOG(ERROR) << e.what();
258262
}

src/lib/dbus/GKDBus.hpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*
33
* This file is part of GLogiK project.
44
* GLogiK, daemon to handle special features on gaming keyboards
5-
* Copyright (C) 2016-2020 Fabrice Delliaux <[email protected]>
5+
* Copyright (C) 2016-2021 Fabrice Delliaux <[email protected]>
66
*
77
* This program is free software: you can redistribute it and/or modify
88
* it under the terms of the GNU General Public License as published by
@@ -50,14 +50,6 @@
5050
namespace NSGKDBus
5151
{
5252

53-
class GKDBusEventFound
54-
: public NSGKUtils::GLogiKExcept
55-
{
56-
public:
57-
GKDBusEventFound(const std::string & msg = "event found") : GLogiKExcept(msg) {};
58-
virtual ~GKDBusEventFound( void ) throw() {};
59-
};
60-
6153
enum class ConnectionFlag : uint8_t
6254
{
6355
GKDBUS_SINGLE = 0,
@@ -113,6 +105,7 @@ class GKDBus
113105

114106
DBusMessage* _message;
115107

108+
void checkDBusMessage(DBusConnection* connection);
116109
void checkForBusMessages(const BusConnection bus, DBusConnection* connection) noexcept;
117110
void checkReleasedName(int ret) noexcept;
118111
void checkDBusError(const char* error);

0 commit comments

Comments
 (0)