change codename
[awesome.git] / dbus.c
blob371b4ca1a38e53831a752da180c0370874a3f94e
1 /*
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.
22 #include "dbus.h"
23 #include "client.h"
25 #ifdef WITH_DBUS
27 #include <ev.h>
28 #include <dbus/dbus.h>
29 #include <unistd.h>
30 #include <fcntl.h>
32 #include "event.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 };
39 static void
40 a_dbus_cleanup_bus(DBusConnection *dbus_connection, ev_io *dbusio)
42 if(!dbus_connection)
43 return;
45 if(dbusio->fd >= 0)
47 ev_ref(EV_DEFAULT_UC);
48 ev_io_stop(EV_DEFAULT_UC_ dbusio);
49 dbusio->fd = -1;
52 /* This is a shared connection owned by libdbus
53 * Do not close it, only unref
55 dbus_connection_unref(dbus_connection);
58 static int
59 a_dbus_message_iter(DBusMessageIter *iter)
61 int nargs = 0;
65 switch(dbus_message_iter_get_arg_type(iter))
67 default:
68 lua_pushnil(globalconf.L);
69 nargs++;
70 break;
71 case DBUS_TYPE_INVALID:
72 break;
73 case DBUS_TYPE_VARIANT:
75 DBusMessageIter subiter;
76 dbus_message_iter_recurse(iter, &subiter);
77 a_dbus_message_iter(&subiter);
79 nargs++;
80 break;
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);
90 nargs++;
91 break;
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);
108 nargs++;
109 break;
110 case DBUS_TYPE_ARRAY:
112 int array_type = dbus_message_iter_get_element_type(iter);
114 if(dbus_type_is_fixed(array_type))
116 DBusMessageIter sub;
117 dbus_message_iter_recurse(iter, &sub);
119 switch(array_type)
121 int datalen = 0;
122 #define DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(type, dbustype) \
123 case dbustype: \
125 const type *data; \
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); \
134 break;
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
143 case DBUS_TYPE_BYTE:
145 const char *c;
146 dbus_message_iter_get_fixed_array(&sub, &c, &datalen);
147 lua_pushlstring(globalconf.L, c, datalen);
149 break;
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);
161 break;
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);
182 else
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);
200 nargs++;
201 break;
202 case DBUS_TYPE_BOOLEAN:
204 dbus_bool_t b;
205 dbus_message_iter_get_basic(iter, &b);
206 lua_pushboolean(globalconf.L, b);
208 nargs++;
209 break;
210 case DBUS_TYPE_BYTE:
212 char c;
213 dbus_message_iter_get_basic(iter, &c);
214 lua_pushlstring(globalconf.L, &c, 1);
216 nargs++;
217 break;
218 #define DBUS_MSG_HANDLE_TYPE_NUMBER(type, dbustype) \
219 case dbustype: \
221 type ui; \
222 dbus_message_iter_get_basic(iter, &ui); \
223 lua_pushnumber(globalconf.L, ui); \
225 nargs++; \
226 break;
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:
236 char *s;
237 dbus_message_iter_get_basic(iter, &s);
238 lua_pushstring(globalconf.L, s);
240 nargs++;
241 break;
243 } while(dbus_message_iter_next(iter));
245 return nargs;
248 static void
249 a_dbus_process_request(DBusConnection *dbus_connection, DBusMessage *msg)
251 if(globalconf.hooks.dbus == LUA_REFNIL)
252 return;
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");
260 break;
261 case DBUS_MESSAGE_TYPE_METHOD_CALL:
262 lua_pushliteral(globalconf.L, "method_call");
263 break;
264 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
265 lua_pushliteral(globalconf.L, "method_return");
266 break;
267 case DBUS_MESSAGE_TYPE_ERROR:
268 lua_pushliteral(globalconf.L, "error");
269 break;
270 default:
271 lua_pushliteral(globalconf.L, "unknown");
272 break;
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");
291 else
292 lua_pushliteral(globalconf.L, "session");
293 lua_setfield(globalconf.L, -2, "bus");
295 /* + 1 for the table above */
296 DBusMessageIter iter;
297 int nargs = 1;
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);
304 else
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);
314 /* i is negative */
315 for(int i = n; i < 0; i += 2)
317 /* i is the type name, i+1 the value */
318 size_t len;
319 const char *type = lua_tolstring(globalconf.L, i, &len);
321 if(!type || len != 1)
322 break;
324 switch(*type)
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);
331 break;
332 #define DBUS_MSG_RETURN_HANDLE_TYPE_STRING(dbustype) \
333 case dbustype: \
334 if((s = lua_tostring(globalconf.L, i + 1))) \
335 dbus_message_iter_append_basic(&iter, dbustype, &s); \
336 break;
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) \
341 case dbustype: \
343 type num = lua_tonumber(globalconf.L, i + 1); \
344 dbus_message_iter_append_basic(&iter, dbustype, &num); \
346 break;
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);
366 static void
367 a_dbus_process_requests_on_bus(DBusConnection *dbus_connection, ev_io *dbusio)
369 DBusMessage *msg;
370 int nmsg = 0;
372 while(true)
374 dbus_connection_read_write(dbus_connection, 0);
376 if(!(msg = dbus_connection_pop_message(dbus_connection)))
377 break;
379 if(dbus_message_is_signal(msg, DBUS_INTERFACE_LOCAL, "Disconnected"))
381 a_dbus_cleanup_bus(dbus_connection, dbusio);
382 dbus_message_unref(msg);
383 return;
385 else
386 a_dbus_process_request(dbus_connection, msg);
388 dbus_message_unref(msg);
390 nmsg++;
393 if(nmsg)
394 dbus_connection_flush(dbus_connection);
396 awesome_refresh();
399 static void
400 a_dbus_process_requests_session(EV_P_ ev_io *w, int revents)
402 a_dbus_process_requests_on_bus(dbus_connection_session, w);
405 static void
406 a_dbus_process_requests_system(EV_P_ ev_io *w, int revents)
408 a_dbus_process_requests_on_bus(dbus_connection_system, w);
411 static bool
412 a_dbus_request_name(DBusConnection *dbus_connection, const char *name)
414 DBusError err;
416 if(!dbus_connection)
417 return false;
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);
427 return false;
430 switch(ret)
432 case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
433 return true;
434 case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
435 warn("already primary D-Bus name owner for %s", name);
436 return true;
438 return false;
441 static bool
442 a_dbus_release_name(DBusConnection *dbus_connection, const char *name)
444 DBusError err;
446 if(!dbus_connection)
447 return false;
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);
457 return false;
460 switch(ret)
462 case DBUS_RELEASE_NAME_REPLY_NOT_OWNER:
463 warn("not primary D-Bus name owner for %s", name);
464 return false;
465 case DBUS_RELEASE_NAME_REPLY_NON_EXISTENT:
466 warn("non existent D-Bus name: %s", name);
467 return false;
469 return true;
472 static DBusConnection *
473 a_dbus_connect(DBusBusType type, const char *type_name,
474 ev_io *dbusio, void *cb)
476 int fd;
477 DBusConnection *dbus_connection;
478 DBusError err;
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);
489 else
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);
500 else
502 warn("cannot get D-Bus connection file descriptor");
503 a_dbus_cleanup_bus(dbus_connection, dbusio);
507 return dbus_connection;
510 void
511 a_dbus_init(void)
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);
519 void
520 a_dbus_cleanup(void)
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))
531 case A_TK_SYSTEM:
532 return dbus_connection_system;
533 case A_TK_SESSION:
534 return dbus_connection_session;
535 default:
536 return NULL;
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.
543 * \luastack
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.
549 static int
550 luaA_dbus_request_name(lua_State *L)
552 size_t len;
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));
557 return 1;
560 /** Release a D-Bus name.
561 * \param L The Lua VM state.
562 * \return The number of elements pushed on stack.
563 * \luastack
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.
568 static int
569 luaA_dbus_release_name(lua_State *L)
571 size_t len;
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));
576 return 1;
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.
582 * \luastack
583 * \lparam A string indicating if we are using system or session bus.
584 * \lparam A string with the name of the match rule.
586 static int
587 luaA_dbus_add_match(lua_State *L)
589 size_t len;
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);
594 if(dbus_connection)
596 dbus_bus_add_match(dbus_connection, name, NULL);
597 dbus_connection_flush(dbus_connection);
600 return 0;
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.
607 * \luastack
608 * \lparam A string indicating if we are using system or session bus.
609 * \lparam A string with the name of the match rule.
611 static int
612 luaA_dbus_remove_match(lua_State *L)
614 size_t len;
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);
619 if(dbus_connection)
621 dbus_bus_remove_match(dbus_connection, name, NULL);
622 dbus_connection_flush(dbus_connection);
625 return 0;
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 },
634 { NULL, NULL }
637 #else /* HAVE_DBUS */
639 void
640 a_dbus_init(void)
644 void
645 a_dbus_cleanup(void)
649 #endif
650 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80