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>
34 static DBusConnection
*dbus_connection_session
= NULL
;
35 static DBusConnection
*dbus_connection_system
= NULL
;
36 ev_io dbusio_ses
= { .fd
= -1 };
37 ev_io dbusio_sys
= { .fd
= -1 };
40 a_dbus_cleanup_bus(DBusConnection
*dbus_connection
, ev_io
*dbusio
)
47 ev_ref(EV_DEFAULT_UC
);
48 ev_io_stop(EV_DEFAULT_UC_ dbusio
);
52 /* This is a shared connection owned by libdbus
53 * Do not close it, only unref
55 dbus_connection_unref(dbus_connection
);
59 a_dbus_message_iter(DBusMessageIter
*iter
)
65 switch(dbus_message_iter_get_arg_type(iter
))
68 lua_pushnil(globalconf
.L
);
71 case DBUS_TYPE_INVALID
:
73 case DBUS_TYPE_VARIANT
:
75 DBusMessageIter subiter
;
76 dbus_message_iter_recurse(iter
, &subiter
);
77 a_dbus_message_iter(&subiter
);
81 case DBUS_TYPE_DICT_ENTRY
:
83 DBusMessageIter subiter
;
85 /* initialize a sub iterator */
86 dbus_message_iter_recurse(iter
, &subiter
);
87 /* create a new table to store the dict */
88 a_dbus_message_iter(&subiter
);
92 case DBUS_TYPE_STRUCT
:
94 DBusMessageIter subiter
;
95 /* initialize a sub iterator */
96 dbus_message_iter_recurse(iter
, &subiter
);
98 int n
= a_dbus_message_iter(&subiter
);
100 /* create a new table to store all the value */
101 lua_createtable(globalconf
.L
, n
, 0);
102 /* move the table before array elements */
103 lua_insert(globalconf
.L
, - n
- 1);
105 for(int i
= n
; i
> 0; i
--)
106 lua_rawseti(globalconf
.L
, - i
- 1, i
);
110 case DBUS_TYPE_ARRAY
:
112 int array_type
= dbus_message_iter_get_element_type(iter
);
114 if(dbus_type_is_fixed(array_type
))
117 dbus_message_iter_recurse(iter
, &sub
);
122 #define DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(type, dbustype) \
126 dbus_message_iter_get_fixed_array(&sub, &data, &datalen); \
127 lua_createtable(globalconf.L, datalen, 0); \
128 for(int i = 0; i < datalen; i++) \
130 lua_pushnumber(globalconf.L, data[i]); \
131 lua_rawseti(globalconf.L, -2, i + 1); \
135 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(int16_t, DBUS_TYPE_INT16
)
136 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(uint16_t, DBUS_TYPE_UINT16
)
137 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(int32_t, DBUS_TYPE_INT32
)
138 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(uint32_t, DBUS_TYPE_UINT32
)
139 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(int64_t, DBUS_TYPE_INT64
)
140 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(uint64_t, DBUS_TYPE_UINT64
)
141 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(double, DBUS_TYPE_DOUBLE
)
142 #undef DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER
146 dbus_message_iter_get_fixed_array(&sub
, &c
, &datalen
);
147 lua_pushlstring(globalconf
.L
, c
, datalen
);
150 case DBUS_TYPE_BOOLEAN
:
152 const dbus_bool_t
*b
;
153 dbus_message_iter_get_fixed_array(&sub
, &b
, &datalen
);
154 lua_createtable(globalconf
.L
, datalen
, 0);
155 for(int i
= 0; i
< datalen
; i
++)
157 lua_pushboolean(globalconf
.L
, b
[i
]);
158 lua_rawseti(globalconf
.L
, -2, i
+ 1);
164 else if(array_type
== DBUS_TYPE_DICT_ENTRY
)
166 DBusMessageIter subiter
;
167 /* initialize a sub iterator */
168 dbus_message_iter_recurse(iter
, &subiter
);
170 /* get the keys and the values
171 * n is the number of entry in dict */
172 int n
= a_dbus_message_iter(&subiter
);
174 /* create a new table to store all the value */
175 lua_createtable(globalconf
.L
, n
, 0);
176 /* move the table before array elements */
177 lua_insert(globalconf
.L
, - (n
* 2) - 1);
179 for(int i
= 0; i
< n
; i
++)
180 lua_rawset(globalconf
.L
, - (n
* 2) - 1 + i
* 2);
184 DBusMessageIter subiter
;
185 /* prepare to dig into the array*/
186 dbus_message_iter_recurse(iter
, &subiter
);
188 /* now iterate over every element of the array */
189 int n
= a_dbus_message_iter(&subiter
);
191 /* create a new table to store all the value */
192 lua_createtable(globalconf
.L
, n
, 0);
193 /* move the table before array elements */
194 lua_insert(globalconf
.L
, - n
- 1);
196 for(int i
= n
; i
> 0; i
--)
197 lua_rawseti(globalconf
.L
, - i
- 1, i
);
202 case DBUS_TYPE_BOOLEAN
:
205 dbus_message_iter_get_basic(iter
, &b
);
206 lua_pushboolean(globalconf
.L
, b
);
213 dbus_message_iter_get_basic(iter
, &c
);
214 lua_pushlstring(globalconf
.L
, &c
, 1);
218 #define DBUS_MSG_HANDLE_TYPE_NUMBER(type, dbustype) \
222 dbus_message_iter_get_basic(iter, &ui); \
223 lua_pushnumber(globalconf.L, ui); \
227 DBUS_MSG_HANDLE_TYPE_NUMBER(int16_t, DBUS_TYPE_INT16
)
228 DBUS_MSG_HANDLE_TYPE_NUMBER(uint16_t, DBUS_TYPE_UINT16
)
229 DBUS_MSG_HANDLE_TYPE_NUMBER(int32_t, DBUS_TYPE_INT32
)
230 DBUS_MSG_HANDLE_TYPE_NUMBER(uint32_t, DBUS_TYPE_UINT32
)
231 DBUS_MSG_HANDLE_TYPE_NUMBER(int64_t, DBUS_TYPE_INT64
)
232 DBUS_MSG_HANDLE_TYPE_NUMBER(uint64_t, DBUS_TYPE_UINT64
)
233 #undef DBUS_MSG_HANDLE_TYPE_NUMBER
234 case DBUS_TYPE_STRING
:
237 dbus_message_iter_get_basic(iter
, &s
);
238 lua_pushstring(globalconf
.L
, s
);
243 } while(dbus_message_iter_next(iter
));
249 a_dbus_process_request(DBusConnection
*dbus_connection
, DBusMessage
*msg
)
251 if(globalconf
.hooks
.dbus
== LUA_REFNIL
)
254 lua_createtable(globalconf
.L
, 0, 5);
256 switch(dbus_message_get_type(msg
))
258 case DBUS_MESSAGE_TYPE_SIGNAL
:
259 lua_pushliteral(globalconf
.L
, "signal");
261 case DBUS_MESSAGE_TYPE_METHOD_CALL
:
262 lua_pushliteral(globalconf
.L
, "method_call");
264 case DBUS_MESSAGE_TYPE_METHOD_RETURN
:
265 lua_pushliteral(globalconf
.L
, "method_return");
267 case DBUS_MESSAGE_TYPE_ERROR
:
268 lua_pushliteral(globalconf
.L
, "error");
271 lua_pushliteral(globalconf
.L
, "unknown");
275 lua_setfield(globalconf
.L
, -2, "type");
277 const char *s
= dbus_message_get_interface(msg
);
278 lua_pushstring(globalconf
.L
, NONULL(s
));
279 lua_setfield(globalconf
.L
, -2, "interface");
281 s
= dbus_message_get_path(msg
);
282 lua_pushstring(globalconf
.L
, NONULL(s
));
283 lua_setfield(globalconf
.L
, -2, "path");
285 s
= dbus_message_get_member(msg
);
286 lua_pushstring(globalconf
.L
, NONULL(s
));
287 lua_setfield(globalconf
.L
, -2, "member");
289 if(dbus_connection
== dbus_connection_system
)
290 lua_pushliteral(globalconf
.L
, "system");
292 lua_pushliteral(globalconf
.L
, "session");
293 lua_setfield(globalconf
.L
, -2, "bus");
295 /* + 1 for the table above */
296 DBusMessageIter iter
;
299 if(dbus_message_iter_init(msg
, &iter
))
300 nargs
+= a_dbus_message_iter(&iter
);
302 if(dbus_message_get_no_reply(msg
))
303 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.dbus
, nargs
, 0);
306 int n
= lua_gettop(globalconf
.L
) - nargs
;
307 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.dbus
, nargs
, LUA_MULTRET
);
308 n
-= lua_gettop(globalconf
.L
);
310 DBusMessage
*reply
= dbus_message_new_method_return(msg
);
312 dbus_message_iter_init_append(reply
, &iter
);
315 for(int i
= n
; i
< 0; i
+= 2)
317 /* i is the type name, i+1 the value */
319 const char *type
= lua_tolstring(globalconf
.L
, i
, &len
);
321 if(!type
|| len
!= 1)
326 case DBUS_TYPE_BOOLEAN
:
328 dbus_bool_t b
= lua_toboolean(globalconf
.L
, i
+ 1);
329 dbus_message_iter_append_basic(&iter
, DBUS_TYPE_BOOLEAN
, &b
);
332 #define DBUS_MSG_RETURN_HANDLE_TYPE_STRING(dbustype) \
334 if((s = lua_tostring(globalconf.L, i + 1))) \
335 dbus_message_iter_append_basic(&iter, dbustype, &s); \
337 DBUS_MSG_RETURN_HANDLE_TYPE_STRING(DBUS_TYPE_STRING
)
338 DBUS_MSG_RETURN_HANDLE_TYPE_STRING(DBUS_TYPE_BYTE
)
339 #undef DBUS_MSG_RETURN_HANDLE_TYPE_STRING
340 #define DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(type, dbustype) \
343 type num = lua_tonumber(globalconf.L, i + 1); \
344 dbus_message_iter_append_basic(&iter, dbustype, &num); \
347 DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(int16_t, DBUS_TYPE_INT16
)
348 DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(uint16_t, DBUS_TYPE_UINT16
)
349 DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(int32_t, DBUS_TYPE_INT32
)
350 DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(uint32_t, DBUS_TYPE_UINT32
)
351 DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(int64_t, DBUS_TYPE_INT64
)
352 DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(uint64_t, DBUS_TYPE_UINT64
)
353 DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(double, DBUS_TYPE_DOUBLE
)
354 #undef DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER
357 lua_remove(globalconf
.L
, i
);
358 lua_remove(globalconf
.L
, i
+ 1);
361 dbus_connection_send(dbus_connection
, reply
, NULL
);
362 dbus_message_unref(reply
);
367 a_dbus_process_requests_on_bus(DBusConnection
*dbus_connection
, ev_io
*dbusio
)
374 dbus_connection_read_write(dbus_connection
, 0);
376 if(!(msg
= dbus_connection_pop_message(dbus_connection
)))
379 if(dbus_message_is_signal(msg
, DBUS_INTERFACE_LOCAL
, "Disconnected"))
381 a_dbus_cleanup_bus(dbus_connection
, dbusio
);
382 dbus_message_unref(msg
);
386 a_dbus_process_request(dbus_connection
, msg
);
388 dbus_message_unref(msg
);
394 dbus_connection_flush(dbus_connection
);
400 a_dbus_process_requests_session(EV_P_ ev_io
*w
, int revents
)
402 a_dbus_process_requests_on_bus(dbus_connection_session
, w
);
406 a_dbus_process_requests_system(EV_P_ ev_io
*w
, int revents
)
408 a_dbus_process_requests_on_bus(dbus_connection_system
, w
);
412 a_dbus_request_name(DBusConnection
*dbus_connection
, const char *name
)
419 dbus_error_init(&err
);
421 int ret
= dbus_bus_request_name(dbus_connection
, name
, 0, &err
);
423 if(dbus_error_is_set(&err
))
425 warn("failed to request D-Bus name: %s", err
.message
);
426 dbus_error_free(&err
);
432 case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
:
434 case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER
:
435 warn("already primary D-Bus name owner for %s", name
);
442 a_dbus_release_name(DBusConnection
*dbus_connection
, const char *name
)
449 dbus_error_init(&err
);
451 int ret
= dbus_bus_release_name(dbus_connection
, name
, &err
);
453 if(dbus_error_is_set(&err
))
455 warn("failed to release D-Bus name: %s", err
.message
);
456 dbus_error_free(&err
);
462 case DBUS_RELEASE_NAME_REPLY_NOT_OWNER
:
463 warn("not primary D-Bus name owner for %s", name
);
465 case DBUS_RELEASE_NAME_REPLY_NON_EXISTENT
:
466 warn("non existent D-Bus name: %s", name
);
472 static DBusConnection
*
473 a_dbus_connect(DBusBusType type
, const char *type_name
,
474 ev_io
*dbusio
, void *cb
)
477 DBusConnection
*dbus_connection
;
480 dbus_error_init(&err
);
482 dbus_connection
= dbus_bus_get(type
, &err
);
483 if(dbus_error_is_set(&err
))
485 warn("D-Bus session bus %s failed: %s", type_name
, err
.message
);
486 dbus_connection
= NULL
;
487 dbus_error_free(&err
);
491 dbus_connection_set_exit_on_disconnect(dbus_connection
, false);
492 if(dbus_connection_get_unix_fd(dbus_connection
, &fd
))
494 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
496 ev_io_init(dbusio
, cb
, fd
, EV_READ
);
497 ev_io_start(EV_DEFAULT_UC_ dbusio
);
498 ev_unref(EV_DEFAULT_UC
);
502 warn("cannot get D-Bus connection file descriptor");
503 a_dbus_cleanup_bus(dbus_connection
, dbusio
);
507 return dbus_connection
;
513 dbus_connection_session
= a_dbus_connect(DBUS_BUS_SESSION
, "session",
514 &dbusio_ses
, a_dbus_process_requests_session
);
515 dbus_connection_system
= a_dbus_connect(DBUS_BUS_SYSTEM
, "system",
516 &dbusio_sys
, a_dbus_process_requests_system
);
522 a_dbus_cleanup_bus(dbus_connection_session
, &dbusio_ses
);
523 a_dbus_cleanup_bus(dbus_connection_system
, &dbusio_sys
);
526 static DBusConnection
*
527 a_dbus_bus_getbyname(const char *name
, size_t len
)
529 switch(a_tokenize(name
, len
))
532 return dbus_connection_system
;
534 return dbus_connection_session
;
540 /** Register a D-Bus name to receive message from.
541 * \param L The Lua VM state.
542 * \return The number of elements pushed on stack.
544 * \lparam A string indicating if we are using system or session bus.
545 * \lparam A string with the name of the D-Bus name to register. Note that
546 * org.awesome is registered by default.
547 * \lreturn True if everything worked fine, false otherwise.
550 luaA_dbus_request_name(lua_State
*L
)
553 const char *bus
= luaL_checklstring(L
, 1, &len
);
554 const char *name
= luaL_checkstring(L
, 2);
555 DBusConnection
*dbus_connection
= a_dbus_bus_getbyname(bus
, len
);
556 lua_pushboolean(L
, a_dbus_request_name(dbus_connection
, name
));
560 /** Release a D-Bus name.
561 * \param L The Lua VM state.
562 * \return The number of elements pushed on stack.
564 * \lparam A string indicating if we are using system or session bus.
565 * \lparam A string with the name of the D-Bus name to unregister.
566 * \lreturn True if everything worked fine, false otherwise.
569 luaA_dbus_release_name(lua_State
*L
)
572 const char *bus
= luaL_checklstring(L
, 1, &len
);
573 const char *name
= luaL_checkstring(L
, 2);
574 DBusConnection
*dbus_connection
= a_dbus_bus_getbyname(bus
, len
);
575 lua_pushboolean(L
, a_dbus_release_name(dbus_connection
, name
));
579 /** Add a match rule to match messages going through the message bus.
580 * \param L The Lua VM state.
581 * \return The number of elements pushed on stack.
583 * \lparam A string indicating if we are using system or session bus.
584 * \lparam A string with the name of the match rule.
587 luaA_dbus_add_match(lua_State
*L
)
590 const char *bus
= luaL_checklstring(L
, 1, &len
);
591 const char *name
= luaL_checkstring(L
, 2);
592 DBusConnection
*dbus_connection
= a_dbus_bus_getbyname(bus
, len
);
596 dbus_bus_add_match(dbus_connection
, name
, NULL
);
597 dbus_connection_flush(dbus_connection
);
603 /** Remove a previously added match rule "by value"
604 * (the most recently-added identical rule gets removed).
605 * \param L The Lua VM state.
606 * \return The number of elements pushed on stack.
608 * \lparam A string indicating if we are using system or session bus.
609 * \lparam A string with the name of the match rule.
612 luaA_dbus_remove_match(lua_State
*L
)
615 const char *bus
= luaL_checklstring(L
, 1, &len
);
616 const char *name
= luaL_checkstring(L
, 2);
617 DBusConnection
*dbus_connection
= a_dbus_bus_getbyname(bus
, len
);
621 dbus_bus_remove_match(dbus_connection
, name
, NULL
);
622 dbus_connection_flush(dbus_connection
);
628 const struct luaL_reg awesome_dbus_lib
[] =
630 { "request_name", luaA_dbus_request_name
},
631 { "release_name", luaA_dbus_release_name
},
632 { "add_match", luaA_dbus_add_match
},
633 { "remove_match", luaA_dbus_remove_match
},
637 #else /* HAVE_DBUS */
650 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80