2 * dbus.c - awesome dbus support
4 * Copyright © 2008-2009 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <dbus/dbus.h>
36 static DBusConnection
*dbus_connection_session
= NULL
;
37 static DBusConnection
*dbus_connection_system
= NULL
;
38 static GSource
*session_source
= NULL
;
39 static GSource
*system_source
= NULL
;
41 static signal_array_t dbus_signals
;
43 /** Clean up the D-Bus connection data members
44 * \param dbus_connection The D-Bus connection to clean up
45 * \param dbusio The D-Bus event watcher
48 a_dbus_cleanup_bus(DBusConnection
*dbus_connection
, GSource
**source
)
54 g_source_destroy(*source
);
57 /* This is a shared connection owned by libdbus
58 * Do not close it, only unref
60 dbus_connection_unref(dbus_connection
);
63 /** Iterate through the D-Bus messages counting each or traverse each sub message.
64 * \param iter The D-Bus message iterator pointer
65 * \return The number of arguments in the iterator
68 a_dbus_message_iter(DBusMessageIter
*iter
)
74 switch(dbus_message_iter_get_arg_type(iter
))
77 lua_pushnil(globalconf
.L
);
80 case DBUS_TYPE_INVALID
:
82 case DBUS_TYPE_VARIANT
:
84 DBusMessageIter subiter
;
85 dbus_message_iter_recurse(iter
, &subiter
);
86 a_dbus_message_iter(&subiter
);
90 case DBUS_TYPE_DICT_ENTRY
:
92 DBusMessageIter subiter
;
94 /* initialize a sub iterator */
95 dbus_message_iter_recurse(iter
, &subiter
);
96 /* create a new table to store the dict */
97 a_dbus_message_iter(&subiter
);
101 case DBUS_TYPE_STRUCT
:
103 DBusMessageIter subiter
;
104 /* initialize a sub iterator */
105 dbus_message_iter_recurse(iter
, &subiter
);
107 int n
= a_dbus_message_iter(&subiter
);
109 /* create a new table to store all the value */
110 lua_createtable(globalconf
.L
, n
, 0);
111 /* move the table before array elements */
112 lua_insert(globalconf
.L
, - n
- 1);
114 for(int i
= n
; i
> 0; i
--)
115 lua_rawseti(globalconf
.L
, - i
- 1, i
);
119 case DBUS_TYPE_ARRAY
:
121 int array_type
= dbus_message_iter_get_element_type(iter
);
123 if(dbus_type_is_fixed(array_type
))
126 dbus_message_iter_recurse(iter
, &sub
);
131 #define DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(type, dbustype) \
135 dbus_message_iter_get_fixed_array(&sub, &data, &datalen); \
136 lua_createtable(globalconf.L, datalen, 0); \
137 for(int i = 0; i < datalen; i++) \
139 lua_pushnumber(globalconf.L, data[i]); \
140 lua_rawseti(globalconf.L, -2, i + 1); \
144 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(int16_t, DBUS_TYPE_INT16
)
145 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(uint16_t, DBUS_TYPE_UINT16
)
146 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(int32_t, DBUS_TYPE_INT32
)
147 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(uint32_t, DBUS_TYPE_UINT32
)
148 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(int64_t, DBUS_TYPE_INT64
)
149 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(uint64_t, DBUS_TYPE_UINT64
)
150 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(double, DBUS_TYPE_DOUBLE
)
151 #undef DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER
155 dbus_message_iter_get_fixed_array(&sub
, &c
, &datalen
);
156 lua_pushlstring(globalconf
.L
, c
, datalen
);
159 case DBUS_TYPE_BOOLEAN
:
161 const dbus_bool_t
*b
;
162 dbus_message_iter_get_fixed_array(&sub
, &b
, &datalen
);
163 lua_createtable(globalconf
.L
, datalen
, 0);
164 for(int i
= 0; i
< datalen
; i
++)
166 lua_pushboolean(globalconf
.L
, b
[i
]);
167 lua_rawseti(globalconf
.L
, -2, i
+ 1);
173 else if(array_type
== DBUS_TYPE_DICT_ENTRY
)
175 DBusMessageIter subiter
;
176 /* initialize a sub iterator */
177 dbus_message_iter_recurse(iter
, &subiter
);
179 /* get the keys and the values
180 * n is the number of entry in dict */
181 int n
= a_dbus_message_iter(&subiter
);
183 /* create a new table to store all the value */
184 lua_createtable(globalconf
.L
, n
, 0);
185 /* move the table before array elements */
186 lua_insert(globalconf
.L
, - (n
* 2) - 1);
188 for(int i
= 0; i
< n
; i
++)
189 lua_rawset(globalconf
.L
, - (n
* 2) - 1 + i
* 2);
193 DBusMessageIter subiter
;
194 /* prepare to dig into the array*/
195 dbus_message_iter_recurse(iter
, &subiter
);
197 /* now iterate over every element of the array */
198 int n
= a_dbus_message_iter(&subiter
);
200 /* create a new table to store all the value */
201 lua_createtable(globalconf
.L
, n
, 0);
202 /* move the table before array elements */
203 lua_insert(globalconf
.L
, - n
- 1);
205 for(int i
= n
; i
> 0; i
--)
206 lua_rawseti(globalconf
.L
, - i
- 1, i
);
211 case DBUS_TYPE_BOOLEAN
:
214 dbus_message_iter_get_basic(iter
, &b
);
215 lua_pushboolean(globalconf
.L
, b
);
222 dbus_message_iter_get_basic(iter
, &c
);
223 lua_pushlstring(globalconf
.L
, &c
, 1);
227 #define DBUS_MSG_HANDLE_TYPE_NUMBER(type, dbustype) \
231 dbus_message_iter_get_basic(iter, &ui); \
232 lua_pushnumber(globalconf.L, ui); \
236 DBUS_MSG_HANDLE_TYPE_NUMBER(int16_t, DBUS_TYPE_INT16
)
237 DBUS_MSG_HANDLE_TYPE_NUMBER(uint16_t, DBUS_TYPE_UINT16
)
238 DBUS_MSG_HANDLE_TYPE_NUMBER(int32_t, DBUS_TYPE_INT32
)
239 DBUS_MSG_HANDLE_TYPE_NUMBER(uint32_t, DBUS_TYPE_UINT32
)
240 DBUS_MSG_HANDLE_TYPE_NUMBER(int64_t, DBUS_TYPE_INT64
)
241 DBUS_MSG_HANDLE_TYPE_NUMBER(uint64_t, DBUS_TYPE_UINT64
)
242 #undef DBUS_MSG_HANDLE_TYPE_NUMBER
243 case DBUS_TYPE_STRING
:
246 dbus_message_iter_get_basic(iter
, &s
);
247 lua_pushstring(globalconf
.L
, s
);
252 } while(dbus_message_iter_next(iter
));
258 a_dbus_convert_value(lua_State
*L
, int idx
, DBusMessageIter
*iter
)
260 /* i is the type name, i+1 the value */
262 const char *type
= lua_tolstring(globalconf
.L
, idx
, &len
);
269 case DBUS_TYPE_ARRAY
:
271 DBusMessageIter subiter
;
272 dbus_message_iter_open_container(iter
, DBUS_TYPE_ARRAY
,
276 int arraylen
= luaA_rawlen(L
, idx
+ 1);
278 if(arraylen
% 2 != 0)
280 luaA_warn(globalconf
.L
,
281 "your D-Bus signal handling method returned wrong number of arguments");
286 lua_pushvalue(L
, idx
+ 1);
288 for(int i
= 1; i
< arraylen
; i
+= 2)
290 lua_rawgeti(L
, -1, i
);
291 lua_rawgeti(L
, -2, i
+ 1);
292 if(!a_dbus_convert_value(L
, -2, &subiter
))
297 /* Remove the array */
300 dbus_message_iter_close_container(iter
, &subiter
);
303 case DBUS_TYPE_BOOLEAN
:
305 dbus_bool_t b
= lua_toboolean(globalconf
.L
, idx
+ 1);
306 dbus_message_iter_append_basic(iter
, DBUS_TYPE_BOOLEAN
, &b
);
309 #define DBUS_MSG_RETURN_HANDLE_TYPE_STRING(dbustype) \
312 const char *s = lua_tostring(globalconf.L, idx + 1); \
314 dbus_message_iter_append_basic(iter, dbustype, &s); \
317 DBUS_MSG_RETURN_HANDLE_TYPE_STRING(DBUS_TYPE_STRING
)
318 DBUS_MSG_RETURN_HANDLE_TYPE_STRING(DBUS_TYPE_BYTE
)
319 #undef DBUS_MSG_RETURN_HANDLE_TYPE_STRING
320 #define DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(type, dbustype) \
323 type num = lua_tonumber(globalconf.L, idx + 1); \
324 dbus_message_iter_append_basic(iter, dbustype, &num); \
327 DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(int16_t, DBUS_TYPE_INT16
)
328 DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(uint16_t, DBUS_TYPE_UINT16
)
329 DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(int32_t, DBUS_TYPE_INT32
)
330 DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(uint32_t, DBUS_TYPE_UINT32
)
331 DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(int64_t, DBUS_TYPE_INT64
)
332 DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(uint64_t, DBUS_TYPE_UINT64
)
333 DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(double, DBUS_TYPE_DOUBLE
)
334 #undef DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER
340 /** Process a single request from D-Bus
341 * \param dbus_connection The connection to the D-Bus server.
342 * \param msg The D-Bus message request being sent to the D-Bus connection.
345 a_dbus_process_request(DBusConnection
*dbus_connection
, DBusMessage
*msg
)
347 const char *interface
= dbus_message_get_interface(msg
);
348 int old_top
= lua_gettop(globalconf
.L
);
350 lua_createtable(globalconf
.L
, 0, 5);
352 switch(dbus_message_get_type(msg
))
354 case DBUS_MESSAGE_TYPE_SIGNAL
:
355 lua_pushliteral(globalconf
.L
, "signal");
357 case DBUS_MESSAGE_TYPE_METHOD_CALL
:
358 lua_pushliteral(globalconf
.L
, "method_call");
360 case DBUS_MESSAGE_TYPE_METHOD_RETURN
:
361 lua_pushliteral(globalconf
.L
, "method_return");
363 case DBUS_MESSAGE_TYPE_ERROR
:
364 lua_pushliteral(globalconf
.L
, "error");
367 lua_pushliteral(globalconf
.L
, "unknown");
371 lua_setfield(globalconf
.L
, -2, "type");
373 lua_pushstring(globalconf
.L
, interface
);
374 lua_setfield(globalconf
.L
, -2, "interface");
376 const char *s
= dbus_message_get_path(msg
);
377 lua_pushstring(globalconf
.L
, s
);
378 lua_setfield(globalconf
.L
, -2, "path");
380 s
= dbus_message_get_member(msg
);
381 lua_pushstring(globalconf
.L
, s
);
382 lua_setfield(globalconf
.L
, -2, "member");
384 if(dbus_connection
== dbus_connection_system
)
385 lua_pushliteral(globalconf
.L
, "system");
387 lua_pushliteral(globalconf
.L
, "session");
388 lua_setfield(globalconf
.L
, -2, "bus");
390 /* + 1 for the table above */
391 DBusMessageIter iter
;
394 if(dbus_message_iter_init(msg
, &iter
))
395 nargs
+= a_dbus_message_iter(&iter
);
397 if(dbus_message_get_no_reply(msg
))
399 signal_t
*sigfound
= signal_array_getbyid(&dbus_signals
,
400 a_strhash((const unsigned char *) NONULL(interface
)));
403 signal_object_emit(globalconf
.L
, &dbus_signals
, NONULL(interface
), nargs
);
407 signal_t
*sig
= signal_array_getbyid(&dbus_signals
,
408 a_strhash((const unsigned char *) NONULL(interface
)));
411 /* there can be only ONE handler to send reply */
412 void *func
= (void *) sig
->sigfuncs
.tab
[0];
414 int n
= lua_gettop(globalconf
.L
) - nargs
;
416 luaA_object_push(globalconf
.L
, (void *) func
);
417 luaA_dofunction(globalconf
.L
, nargs
, LUA_MULTRET
);
419 n
-= lua_gettop(globalconf
.L
);
421 DBusMessage
*reply
= dbus_message_new_method_return(msg
);
423 dbus_message_iter_init_append(reply
, &iter
);
427 luaA_warn(globalconf
.L
,
428 "your D-Bus signal handling method returned wrong number of arguments");
430 lua_settop(globalconf
.L
, old_top
);
435 for(int i
= n
; i
< 0; i
+= 2)
437 if(!a_dbus_convert_value(globalconf
.L
, i
, &iter
))
439 luaA_warn(globalconf
.L
, "your D-Bus signal handling method returned bad data");
441 lua_settop(globalconf
.L
, old_top
);
445 lua_remove(globalconf
.L
, i
);
446 lua_remove(globalconf
.L
, i
+ 1);
449 dbus_connection_send(dbus_connection
, reply
, NULL
);
450 dbus_message_unref(reply
);
454 lua_settop(globalconf
.L
, old_top
);
457 /** Attempt to process all the requests in the D-Bus connection.
458 * \param dbus_connection The D-Bus connection to process from
459 * \param dbusio The D-Bus event watcher
462 a_dbus_process_requests_on_bus(DBusConnection
*dbus_connection
, GSource
**source
)
469 dbus_connection_read_write(dbus_connection
, 0);
471 if(!(msg
= dbus_connection_pop_message(dbus_connection
)))
474 if(dbus_message_is_signal(msg
, DBUS_INTERFACE_LOCAL
, "Disconnected"))
476 a_dbus_cleanup_bus(dbus_connection
, source
);
477 dbus_message_unref(msg
);
481 a_dbus_process_request(dbus_connection
, msg
);
483 dbus_message_unref(msg
);
489 dbus_connection_flush(dbus_connection
);
492 /** Foreword D-Bus process session requests on too the correct function.
493 * \param w The D-Bus event watcher
494 * \param revents (not used)
497 a_dbus_process_requests_session(gpointer data
)
499 a_dbus_process_requests_on_bus(dbus_connection_session
, &session_source
);
503 /** Foreword D-Bus process system requests on too the correct function.
504 * \param w The D-Bus event watcher
505 * \param revents (not used)
508 a_dbus_process_requests_system(gpointer data
)
510 a_dbus_process_requests_on_bus(dbus_connection_system
, &system_source
);
514 /** Attempt to request a D-Bus name
515 * \param dbus_connection The application's connection to D-Bus
516 * \param name The D-Bus connection name to be requested
517 * \return true if the name is primary owner or the name is already
518 * the owner, otherwise false.
521 a_dbus_request_name(DBusConnection
*dbus_connection
, const char *name
)
528 dbus_error_init(&err
);
530 int ret
= dbus_bus_request_name(dbus_connection
, name
, 0, &err
);
532 if(dbus_error_is_set(&err
))
534 warn("failed to request D-Bus name: %s", err
.message
);
535 dbus_error_free(&err
);
541 case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
:
543 case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER
:
544 warn("already primary D-Bus name owner for %s", name
);
550 /** Attempt to release the D-Bus name owner
551 * \param dbus_connection The application's connection to the D-Bus
552 * \param name The name to be released
553 * \return True on success. False if the name is not
554 * the owner, or does not exist.
557 a_dbus_release_name(DBusConnection
*dbus_connection
, const char *name
)
564 dbus_error_init(&err
);
566 int ret
= dbus_bus_release_name(dbus_connection
, name
, &err
);
568 if(dbus_error_is_set(&err
))
570 warn("failed to release D-Bus name: %s", err
.message
);
571 dbus_error_free(&err
);
577 case DBUS_RELEASE_NAME_REPLY_NOT_OWNER
:
578 warn("not primary D-Bus name owner for %s", name
);
580 case DBUS_RELEASE_NAME_REPLY_NON_EXISTENT
:
581 warn("non existent D-Bus name: %s", name
);
587 /** Attempt to create a new connection to D-Bus
588 * \param type The bus type to use when connecting to D-Bus
589 * \param type_name The bus type name eg: "session" or "system"
590 * \param cb Function callback to use when processing requests
591 * \param source A new GSource that will be used for watching the dbus connection.
592 * \return The requested D-Bus connection on success, NULL on failure.
594 static DBusConnection
*
595 a_dbus_connect(DBusBusType type
, const char *type_name
, GSourceFunc cb
, GSource
**source
)
598 DBusConnection
*dbus_connection
;
601 dbus_error_init(&err
);
603 dbus_connection
= dbus_bus_get(type
, &err
);
604 if(dbus_error_is_set(&err
))
606 warn("D-Bus session bus %s failed: %s", type_name
, err
.message
);
607 dbus_connection
= NULL
;
608 dbus_error_free(&err
);
612 dbus_connection_set_exit_on_disconnect(dbus_connection
, false);
613 if(dbus_connection_get_unix_fd(dbus_connection
, &fd
))
615 GIOChannel
*channel
= g_io_channel_unix_new(fd
);
616 *source
= g_io_create_watch(channel
, G_IO_IN
);
617 g_io_channel_unref(channel
);
618 g_source_set_callback(*source
, cb
, NULL
, NULL
);
619 g_source_attach(*source
, NULL
);
621 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
625 warn("cannot get D-Bus connection file descriptor");
626 a_dbus_cleanup_bus(dbus_connection
, source
);
630 return dbus_connection
;
633 /** Initialize the D-Bus session and system
638 dbus_connection_session
= a_dbus_connect(DBUS_BUS_SESSION
, "session",
639 a_dbus_process_requests_session
, &session_source
);
640 dbus_connection_system
= a_dbus_connect(DBUS_BUS_SYSTEM
, "system",
641 a_dbus_process_requests_system
, &system_source
);
644 /** Cleanup the D-Bus session and system
649 a_dbus_cleanup_bus(dbus_connection_session
, &session_source
);
650 a_dbus_cleanup_bus(dbus_connection_system
, &system_source
);
653 /** Retrieve the D-Bus bus by it's name
654 * \param name The name of the bus
655 * \return The corresponding D-Bus connection
657 static DBusConnection
*
658 a_dbus_bus_getbyname(const char *name
)
660 if(A_STREQ(name
, "system"))
661 return dbus_connection_system
;
662 if(A_STREQ(name
, "session"))
663 return dbus_connection_session
;
667 /** Register a D-Bus name to receive message from.
668 * \param L The Lua VM state.
669 * \return The number of elements pushed on stack.
671 * \lparam A string indicating if we are using system or session bus.
672 * \lparam A string with the name of the D-Bus name to register.
673 * \lreturn True if everything worked fine, false otherwise.
676 luaA_dbus_request_name(lua_State
*L
)
678 const char *bus
= luaL_checkstring(L
, 1);
679 const char *name
= luaL_checkstring(L
, 2);
680 DBusConnection
*dbus_connection
= a_dbus_bus_getbyname(bus
);
681 lua_pushboolean(L
, a_dbus_request_name(dbus_connection
, name
));
685 /** Release a D-Bus name.
686 * \param L The Lua VM state.
687 * \return The number of elements pushed on stack.
689 * \lparam A string indicating if we are using system or session bus.
690 * \lparam A string with the name of the D-Bus name to unregister.
691 * \lreturn True if everything worked fine, false otherwise.
694 luaA_dbus_release_name(lua_State
*L
)
696 const char *bus
= luaL_checkstring(L
, 1);
697 const char *name
= luaL_checkstring(L
, 2);
698 DBusConnection
*dbus_connection
= a_dbus_bus_getbyname(bus
);
699 lua_pushboolean(L
, a_dbus_release_name(dbus_connection
, name
));
703 /** Add a match rule to match messages going through the message bus.
704 * \param L The Lua VM state.
705 * \return The number of elements pushed on stack.
707 * \lparam A string indicating if we are using system or session bus.
708 * \lparam A string with the name of the match rule.
711 luaA_dbus_add_match(lua_State
*L
)
713 const char *bus
= luaL_checkstring(L
, 1);
714 const char *name
= luaL_checkstring(L
, 2);
715 DBusConnection
*dbus_connection
= a_dbus_bus_getbyname(bus
);
719 dbus_bus_add_match(dbus_connection
, name
, NULL
);
720 dbus_connection_flush(dbus_connection
);
726 /** Remove a previously added match rule "by value"
727 * (the most recently-added identical rule gets removed).
728 * \param L The Lua VM state.
729 * \return The number of elements pushed on stack.
731 * \lparam A string indicating if we are using system or session bus.
732 * \lparam A string with the name of the match rule.
735 luaA_dbus_remove_match(lua_State
*L
)
737 const char *bus
= luaL_checkstring(L
, 1);
738 const char *name
= luaL_checkstring(L
, 2);
739 DBusConnection
*dbus_connection
= a_dbus_bus_getbyname(bus
);
743 dbus_bus_remove_match(dbus_connection
, name
, NULL
);
744 dbus_connection_flush(dbus_connection
);
750 /** Add a signal receiver on the D-Bus.
751 * \param L The Lua VM state.
752 * \return The number of elements pushed on stack.
754 * \lparam A string with the interface name.
755 * \lparam The function to call.
758 luaA_dbus_connect_signal(lua_State
*L
)
760 const char *name
= luaL_checkstring(L
, 1);
761 luaA_checkfunction(L
, 2);
762 signal_t
*sig
= signal_array_getbyid(&dbus_signals
,
763 a_strhash((const unsigned char *) name
));
765 luaA_warn(L
, "cannot add signal %s on D-Bus, already existing", name
);
768 signal_add(&dbus_signals
, name
);
769 signal_connect(&dbus_signals
, name
, luaA_object_ref(L
, 2));
774 /** Add a signal receiver on the D-Bus.
775 * \param L The Lua VM state.
776 * \return The number of elements pushed on stack.
778 * \lparam A string with the interface name.
779 * \lparam The function to call.
782 luaA_dbus_disconnect_signal(lua_State
*L
)
784 const char *name
= luaL_checkstring(L
, 1);
785 luaA_checkfunction(L
, 2);
786 const void *func
= lua_topointer(L
, 2);
787 signal_disconnect(&dbus_signals
, name
, func
);
788 luaA_object_unref(L
, func
);
792 const struct luaL_Reg awesome_dbus_lib
[] =
794 { "request_name", luaA_dbus_request_name
},
795 { "release_name", luaA_dbus_release_name
},
796 { "add_match", luaA_dbus_add_match
},
797 { "remove_match", luaA_dbus_remove_match
},
798 { "connect_signal", luaA_dbus_connect_signal
},
799 { "disconnect_signal", luaA_dbus_disconnect_signal
},
803 #else /* WITH_DBUS */
805 /** Empty stub if dbus is not enabled */
811 /** Empty stub if dbus is not enabled */
818 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80