2 * dbus.c - awesome dbus support
4 * Copyright © 2008 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.
28 #include <dbus/dbus.h>
35 static DBusConnection
*dbus_connection_session
= NULL
;
36 static DBusConnection
*dbus_connection_system
= NULL
;
37 ev_io dbusio_ses
= { .fd
= -1 };
38 ev_io dbusio_sys
= { .fd
= -1 };
40 static signal_array_t dbus_signals
;
43 a_dbus_cleanup_bus(DBusConnection
*dbus_connection
, ev_io
*dbusio
)
50 ev_ref(EV_DEFAULT_UC
);
51 ev_io_stop(EV_DEFAULT_UC_ dbusio
);
55 /* This is a shared connection owned by libdbus
56 * Do not close it, only unref
58 dbus_connection_unref(dbus_connection
);
62 a_dbus_message_iter(DBusMessageIter
*iter
)
68 switch(dbus_message_iter_get_arg_type(iter
))
71 lua_pushnil(globalconf
.L
);
74 case DBUS_TYPE_INVALID
:
76 case DBUS_TYPE_VARIANT
:
78 DBusMessageIter subiter
;
79 dbus_message_iter_recurse(iter
, &subiter
);
80 a_dbus_message_iter(&subiter
);
84 case DBUS_TYPE_DICT_ENTRY
:
86 DBusMessageIter subiter
;
88 /* initialize a sub iterator */
89 dbus_message_iter_recurse(iter
, &subiter
);
90 /* create a new table to store the dict */
91 a_dbus_message_iter(&subiter
);
95 case DBUS_TYPE_STRUCT
:
97 DBusMessageIter subiter
;
98 /* initialize a sub iterator */
99 dbus_message_iter_recurse(iter
, &subiter
);
101 int n
= a_dbus_message_iter(&subiter
);
103 /* create a new table to store all the value */
104 lua_createtable(globalconf
.L
, n
, 0);
105 /* move the table before array elements */
106 lua_insert(globalconf
.L
, - n
- 1);
108 for(int i
= n
; i
> 0; i
--)
109 lua_rawseti(globalconf
.L
, - i
- 1, i
);
113 case DBUS_TYPE_ARRAY
:
115 int array_type
= dbus_message_iter_get_element_type(iter
);
117 if(dbus_type_is_fixed(array_type
))
120 dbus_message_iter_recurse(iter
, &sub
);
125 #define DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(type, dbustype) \
129 dbus_message_iter_get_fixed_array(&sub, &data, &datalen); \
130 lua_createtable(globalconf.L, datalen, 0); \
131 for(int i = 0; i < datalen; i++) \
133 lua_pushnumber(globalconf.L, data[i]); \
134 lua_rawseti(globalconf.L, -2, i + 1); \
138 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(int16_t, DBUS_TYPE_INT16
)
139 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(uint16_t, DBUS_TYPE_UINT16
)
140 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(int32_t, DBUS_TYPE_INT32
)
141 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(uint32_t, DBUS_TYPE_UINT32
)
142 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(int64_t, DBUS_TYPE_INT64
)
143 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(uint64_t, DBUS_TYPE_UINT64
)
144 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(double, DBUS_TYPE_DOUBLE
)
145 #undef DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER
149 dbus_message_iter_get_fixed_array(&sub
, &c
, &datalen
);
150 lua_pushlstring(globalconf
.L
, c
, datalen
);
153 case DBUS_TYPE_BOOLEAN
:
155 const dbus_bool_t
*b
;
156 dbus_message_iter_get_fixed_array(&sub
, &b
, &datalen
);
157 lua_createtable(globalconf
.L
, datalen
, 0);
158 for(int i
= 0; i
< datalen
; i
++)
160 lua_pushboolean(globalconf
.L
, b
[i
]);
161 lua_rawseti(globalconf
.L
, -2, i
+ 1);
167 else if(array_type
== DBUS_TYPE_DICT_ENTRY
)
169 DBusMessageIter subiter
;
170 /* initialize a sub iterator */
171 dbus_message_iter_recurse(iter
, &subiter
);
173 /* get the keys and the values
174 * n is the number of entry in dict */
175 int n
= a_dbus_message_iter(&subiter
);
177 /* create a new table to store all the value */
178 lua_createtable(globalconf
.L
, n
, 0);
179 /* move the table before array elements */
180 lua_insert(globalconf
.L
, - (n
* 2) - 1);
182 for(int i
= 0; i
< n
; i
++)
183 lua_rawset(globalconf
.L
, - (n
* 2) - 1 + i
* 2);
187 DBusMessageIter subiter
;
188 /* prepare to dig into the array*/
189 dbus_message_iter_recurse(iter
, &subiter
);
191 /* now iterate over every element of the array */
192 int n
= a_dbus_message_iter(&subiter
);
194 /* create a new table to store all the value */
195 lua_createtable(globalconf
.L
, n
, 0);
196 /* move the table before array elements */
197 lua_insert(globalconf
.L
, - n
- 1);
199 for(int i
= n
; i
> 0; i
--)
200 lua_rawseti(globalconf
.L
, - i
- 1, i
);
205 case DBUS_TYPE_BOOLEAN
:
208 dbus_message_iter_get_basic(iter
, &b
);
209 lua_pushboolean(globalconf
.L
, b
);
216 dbus_message_iter_get_basic(iter
, &c
);
217 lua_pushlstring(globalconf
.L
, &c
, 1);
221 #define DBUS_MSG_HANDLE_TYPE_NUMBER(type, dbustype) \
225 dbus_message_iter_get_basic(iter, &ui); \
226 lua_pushnumber(globalconf.L, ui); \
230 DBUS_MSG_HANDLE_TYPE_NUMBER(int16_t, DBUS_TYPE_INT16
)
231 DBUS_MSG_HANDLE_TYPE_NUMBER(uint16_t, DBUS_TYPE_UINT16
)
232 DBUS_MSG_HANDLE_TYPE_NUMBER(int32_t, DBUS_TYPE_INT32
)
233 DBUS_MSG_HANDLE_TYPE_NUMBER(uint32_t, DBUS_TYPE_UINT32
)
234 DBUS_MSG_HANDLE_TYPE_NUMBER(int64_t, DBUS_TYPE_INT64
)
235 DBUS_MSG_HANDLE_TYPE_NUMBER(uint64_t, DBUS_TYPE_UINT64
)
236 #undef DBUS_MSG_HANDLE_TYPE_NUMBER
237 case DBUS_TYPE_STRING
:
240 dbus_message_iter_get_basic(iter
, &s
);
241 lua_pushstring(globalconf
.L
, s
);
246 } while(dbus_message_iter_next(iter
));
252 a_dbus_process_request(DBusConnection
*dbus_connection
, DBusMessage
*msg
)
254 const char *interface
= dbus_message_get_interface(msg
);
256 lua_createtable(globalconf
.L
, 0, 5);
258 switch(dbus_message_get_type(msg
))
260 case DBUS_MESSAGE_TYPE_SIGNAL
:
261 lua_pushliteral(globalconf
.L
, "signal");
263 case DBUS_MESSAGE_TYPE_METHOD_CALL
:
264 lua_pushliteral(globalconf
.L
, "method_call");
266 case DBUS_MESSAGE_TYPE_METHOD_RETURN
:
267 lua_pushliteral(globalconf
.L
, "method_return");
269 case DBUS_MESSAGE_TYPE_ERROR
:
270 lua_pushliteral(globalconf
.L
, "error");
273 lua_pushliteral(globalconf
.L
, "unknown");
277 lua_setfield(globalconf
.L
, -2, "type");
279 lua_pushstring(globalconf
.L
, NONULL(interface
));
280 lua_setfield(globalconf
.L
, -2, "interface");
282 const char *s
= dbus_message_get_path(msg
);
283 lua_pushstring(globalconf
.L
, NONULL(s
));
284 lua_setfield(globalconf
.L
, -2, "path");
286 s
= dbus_message_get_member(msg
);
287 lua_pushstring(globalconf
.L
, NONULL(s
));
288 lua_setfield(globalconf
.L
, -2, "member");
290 if(dbus_connection
== dbus_connection_system
)
291 lua_pushliteral(globalconf
.L
, "system");
293 lua_pushliteral(globalconf
.L
, "session");
294 lua_setfield(globalconf
.L
, -2, "bus");
296 /* + 1 for the table above */
297 DBusMessageIter iter
;
300 if(dbus_message_iter_init(msg
, &iter
))
301 nargs
+= a_dbus_message_iter(&iter
);
303 if(dbus_message_get_no_reply(msg
))
305 signal_object_emit(globalconf
.L
, &dbus_signals
, interface
, nargs
);
308 signal_t
*sig
= signal_array_getbyid(&dbus_signals
,
309 a_strhash((const unsigned char *) interface
));
312 /* there can be only ONE handler to send reply */
313 void *func
= (void *) sig
->sigfuncs
.tab
[0];
315 int n
= lua_gettop(globalconf
.L
) - nargs
;
317 luaA_object_push(globalconf
.L
, (void *) func
);
318 luaA_dofunction(globalconf
.L
, nargs
, LUA_MULTRET
);
320 n
-= lua_gettop(globalconf
.L
);
322 DBusMessage
*reply
= dbus_message_new_method_return(msg
);
324 dbus_message_iter_init_append(reply
, &iter
);
327 for(int i
= n
; i
< 0; i
+= 2)
329 /* i is the type name, i+1 the value */
331 const char *type
= lua_tolstring(globalconf
.L
, i
, &len
);
333 if(!type
|| len
!= 1)
335 luaA_warn(globalconf
.L
,
336 "your D-Bus signal handling method returned bad data");
342 case DBUS_TYPE_BOOLEAN
:
344 dbus_bool_t b
= lua_toboolean(globalconf
.L
, i
+ 1);
345 dbus_message_iter_append_basic(&iter
, DBUS_TYPE_BOOLEAN
, &b
);
348 #define DBUS_MSG_RETURN_HANDLE_TYPE_STRING(dbustype) \
350 if((s = lua_tostring(globalconf.L, i + 1))) \
351 dbus_message_iter_append_basic(&iter, dbustype, &s); \
353 DBUS_MSG_RETURN_HANDLE_TYPE_STRING(DBUS_TYPE_STRING
)
354 DBUS_MSG_RETURN_HANDLE_TYPE_STRING(DBUS_TYPE_BYTE
)
355 #undef DBUS_MSG_RETURN_HANDLE_TYPE_STRING
356 #define DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(type, dbustype) \
359 type num = lua_tonumber(globalconf.L, i + 1); \
360 dbus_message_iter_append_basic(&iter, dbustype, &num); \
363 DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(int16_t, DBUS_TYPE_INT16
)
364 DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(uint16_t, DBUS_TYPE_UINT16
)
365 DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(int32_t, DBUS_TYPE_INT32
)
366 DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(uint32_t, DBUS_TYPE_UINT32
)
367 DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(int64_t, DBUS_TYPE_INT64
)
368 DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(uint64_t, DBUS_TYPE_UINT64
)
369 DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(double, DBUS_TYPE_DOUBLE
)
370 #undef DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER
373 lua_remove(globalconf
.L
, i
);
374 lua_remove(globalconf
.L
, i
+ 1);
377 dbus_connection_send(dbus_connection
, reply
, NULL
);
378 dbus_message_unref(reply
);
384 a_dbus_process_requests_on_bus(DBusConnection
*dbus_connection
, ev_io
*dbusio
)
391 dbus_connection_read_write(dbus_connection
, 0);
393 if(!(msg
= dbus_connection_pop_message(dbus_connection
)))
396 if(dbus_message_is_signal(msg
, DBUS_INTERFACE_LOCAL
, "Disconnected"))
398 a_dbus_cleanup_bus(dbus_connection
, dbusio
);
399 dbus_message_unref(msg
);
403 a_dbus_process_request(dbus_connection
, msg
);
405 dbus_message_unref(msg
);
411 dbus_connection_flush(dbus_connection
);
415 a_dbus_process_requests_session(EV_P_ ev_io
*w
, int revents
)
417 a_dbus_process_requests_on_bus(dbus_connection_session
, w
);
421 a_dbus_process_requests_system(EV_P_ ev_io
*w
, int revents
)
423 a_dbus_process_requests_on_bus(dbus_connection_system
, w
);
427 a_dbus_request_name(DBusConnection
*dbus_connection
, const char *name
)
434 dbus_error_init(&err
);
436 int ret
= dbus_bus_request_name(dbus_connection
, name
, 0, &err
);
438 if(dbus_error_is_set(&err
))
440 warn("failed to request D-Bus name: %s", err
.message
);
441 dbus_error_free(&err
);
447 case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
:
449 case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER
:
450 warn("already primary D-Bus name owner for %s", name
);
457 a_dbus_release_name(DBusConnection
*dbus_connection
, const char *name
)
464 dbus_error_init(&err
);
466 int ret
= dbus_bus_release_name(dbus_connection
, name
, &err
);
468 if(dbus_error_is_set(&err
))
470 warn("failed to release D-Bus name: %s", err
.message
);
471 dbus_error_free(&err
);
477 case DBUS_RELEASE_NAME_REPLY_NOT_OWNER
:
478 warn("not primary D-Bus name owner for %s", name
);
480 case DBUS_RELEASE_NAME_REPLY_NON_EXISTENT
:
481 warn("non existent D-Bus name: %s", name
);
487 static DBusConnection
*
488 a_dbus_connect(DBusBusType type
, const char *type_name
,
489 ev_io
*dbusio
, void *cb
)
492 DBusConnection
*dbus_connection
;
495 dbus_error_init(&err
);
497 dbus_connection
= dbus_bus_get(type
, &err
);
498 if(dbus_error_is_set(&err
))
500 warn("D-Bus session bus %s failed: %s", type_name
, err
.message
);
501 dbus_connection
= NULL
;
502 dbus_error_free(&err
);
506 dbus_connection_set_exit_on_disconnect(dbus_connection
, false);
507 if(dbus_connection_get_unix_fd(dbus_connection
, &fd
))
509 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
511 ev_io_init(dbusio
, cb
, fd
, EV_READ
);
512 ev_io_start(EV_DEFAULT_UC_ dbusio
);
513 ev_unref(EV_DEFAULT_UC
);
517 warn("cannot get D-Bus connection file descriptor");
518 a_dbus_cleanup_bus(dbus_connection
, dbusio
);
522 return dbus_connection
;
528 dbus_connection_session
= a_dbus_connect(DBUS_BUS_SESSION
, "session",
529 &dbusio_ses
, a_dbus_process_requests_session
);
530 dbus_connection_system
= a_dbus_connect(DBUS_BUS_SYSTEM
, "system",
531 &dbusio_sys
, a_dbus_process_requests_system
);
537 a_dbus_cleanup_bus(dbus_connection_session
, &dbusio_ses
);
538 a_dbus_cleanup_bus(dbus_connection_system
, &dbusio_sys
);
541 static DBusConnection
*
542 a_dbus_bus_getbyname(const char *name
, size_t len
)
544 switch(a_tokenize(name
, len
))
547 return dbus_connection_system
;
549 return dbus_connection_session
;
555 /** Register a D-Bus name to receive message from.
556 * \param L The Lua VM state.
557 * \return The number of elements pushed on stack.
559 * \lparam A string indicating if we are using system or session bus.
560 * \lparam A string with the name of the D-Bus name to register.
561 * \lreturn True if everything worked fine, false otherwise.
564 luaA_dbus_request_name(lua_State
*L
)
567 const char *bus
= luaL_checklstring(L
, 1, &len
);
568 const char *name
= luaL_checkstring(L
, 2);
569 DBusConnection
*dbus_connection
= a_dbus_bus_getbyname(bus
, len
);
570 lua_pushboolean(L
, a_dbus_request_name(dbus_connection
, name
));
574 /** Release a D-Bus name.
575 * \param L The Lua VM state.
576 * \return The number of elements pushed on stack.
578 * \lparam A string indicating if we are using system or session bus.
579 * \lparam A string with the name of the D-Bus name to unregister.
580 * \lreturn True if everything worked fine, false otherwise.
583 luaA_dbus_release_name(lua_State
*L
)
586 const char *bus
= luaL_checklstring(L
, 1, &len
);
587 const char *name
= luaL_checkstring(L
, 2);
588 DBusConnection
*dbus_connection
= a_dbus_bus_getbyname(bus
, len
);
589 lua_pushboolean(L
, a_dbus_release_name(dbus_connection
, name
));
593 /** Add a match rule to match messages going through the message bus.
594 * \param L The Lua VM state.
595 * \return The number of elements pushed on stack.
597 * \lparam A string indicating if we are using system or session bus.
598 * \lparam A string with the name of the match rule.
601 luaA_dbus_add_match(lua_State
*L
)
604 const char *bus
= luaL_checklstring(L
, 1, &len
);
605 const char *name
= luaL_checkstring(L
, 2);
606 DBusConnection
*dbus_connection
= a_dbus_bus_getbyname(bus
, len
);
610 dbus_bus_add_match(dbus_connection
, name
, NULL
);
611 dbus_connection_flush(dbus_connection
);
617 /** Remove a previously added match rule "by value"
618 * (the most recently-added identical rule gets removed).
619 * \param L The Lua VM state.
620 * \return The number of elements pushed on stack.
622 * \lparam A string indicating if we are using system or session bus.
623 * \lparam A string with the name of the match rule.
626 luaA_dbus_remove_match(lua_State
*L
)
629 const char *bus
= luaL_checklstring(L
, 1, &len
);
630 const char *name
= luaL_checkstring(L
, 2);
631 DBusConnection
*dbus_connection
= a_dbus_bus_getbyname(bus
, len
);
635 dbus_bus_remove_match(dbus_connection
, name
, NULL
);
636 dbus_connection_flush(dbus_connection
);
642 /** Add a signal receiver on the D-Bus.
643 * \param L The Lua VM state.
644 * \return The number of elements pushed on stack.
646 * \lparam A string with the interface name.
647 * \lparam The function to call.
650 luaA_dbus_add_signal(lua_State
*L
)
652 const char *name
= luaL_checkstring(L
, 1);
653 luaA_checkfunction(L
, 2);
654 signal_t
*sig
= signal_array_getbyid(&dbus_signals
,
655 a_strhash((const unsigned char *) name
));
657 luaA_warn(L
, "cannot add signal %s on D-Bus, already existing", name
);
659 signal_add(&dbus_signals
, name
, luaA_object_ref(L
, 2));
663 /** Add a signal receiver on the D-Bus.
664 * \param L The Lua VM state.
665 * \return The number of elements pushed on stack.
667 * \lparam A string with the interface name.
668 * \lparam The function to call.
671 luaA_dbus_remove_signal(lua_State
*L
)
673 const char *name
= luaL_checkstring(L
, 1);
674 luaA_checkfunction(L
, 2);
675 const void *func
= lua_topointer(L
, 2);
676 signal_remove(&dbus_signals
, name
, func
);
677 luaA_object_unref(L
, (void *) func
);
681 const struct luaL_reg awesome_dbus_lib
[] =
683 { "request_name", luaA_dbus_request_name
},
684 { "release_name", luaA_dbus_release_name
},
685 { "add_match", luaA_dbus_add_match
},
686 { "remove_match", luaA_dbus_remove_match
},
687 { "add_signal", luaA_dbus_add_signal
},
688 { "remove_signal", luaA_dbus_remove_signal
},
692 #else /* HAVE_DBUS */
705 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80