client: _really_ honor size hints
[awesome.git] / dbus.c
blobab3f019702d26f23b78d6cec2a1038ca0d9a57f3
1 /*
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.
22 #include "config.h"
24 #include "dbus.h"
26 #ifdef WITH_DBUS
28 #include <ev.h>
29 #include <dbus/dbus.h>
30 #include <unistd.h>
31 #include <fcntl.h>
33 #include "event.h"
34 #include "luaa.h"
36 static DBusConnection *dbus_connection_session = NULL;
37 static DBusConnection *dbus_connection_system = NULL;
38 ev_io dbusio_ses = { .fd = -1 };
39 ev_io dbusio_sys = { .fd = -1 };
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
47 static void
48 a_dbus_cleanup_bus(DBusConnection *dbus_connection, ev_io *dbusio)
50 if(!dbus_connection)
51 return;
53 if(dbusio->fd >= 0)
55 ev_ref(EV_DEFAULT_UC);
56 ev_io_stop(EV_DEFAULT_UC_ dbusio);
57 dbusio->fd = -1;
60 /* This is a shared connection owned by libdbus
61 * Do not close it, only unref
63 dbus_connection_unref(dbus_connection);
66 /** Iterate through the D-Bus messages counting each or traverse each sub message.
67 * \param iter The D-Bus message iterator pointer
68 * \return The number of arguments in the iterator
70 static int
71 a_dbus_message_iter(DBusMessageIter *iter)
73 int nargs = 0;
77 switch(dbus_message_iter_get_arg_type(iter))
79 default:
80 lua_pushnil(globalconf.L);
81 nargs++;
82 break;
83 case DBUS_TYPE_INVALID:
84 break;
85 case DBUS_TYPE_VARIANT:
87 DBusMessageIter subiter;
88 dbus_message_iter_recurse(iter, &subiter);
89 a_dbus_message_iter(&subiter);
91 nargs++;
92 break;
93 case DBUS_TYPE_DICT_ENTRY:
95 DBusMessageIter subiter;
97 /* initialize a sub iterator */
98 dbus_message_iter_recurse(iter, &subiter);
99 /* create a new table to store the dict */
100 a_dbus_message_iter(&subiter);
102 nargs++;
103 break;
104 case DBUS_TYPE_STRUCT:
106 DBusMessageIter subiter;
107 /* initialize a sub iterator */
108 dbus_message_iter_recurse(iter, &subiter);
110 int n = a_dbus_message_iter(&subiter);
112 /* create a new table to store all the value */
113 lua_createtable(globalconf.L, n, 0);
114 /* move the table before array elements */
115 lua_insert(globalconf.L, - n - 1);
117 for(int i = n; i > 0; i--)
118 lua_rawseti(globalconf.L, - i - 1, i);
120 nargs++;
121 break;
122 case DBUS_TYPE_ARRAY:
124 int array_type = dbus_message_iter_get_element_type(iter);
126 if(dbus_type_is_fixed(array_type))
128 DBusMessageIter sub;
129 dbus_message_iter_recurse(iter, &sub);
131 switch(array_type)
133 int datalen = 0;
134 #define DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(type, dbustype) \
135 case dbustype: \
137 const type *data; \
138 dbus_message_iter_get_fixed_array(&sub, &data, &datalen); \
139 lua_createtable(globalconf.L, datalen, 0); \
140 for(int i = 0; i < datalen; i++) \
142 lua_pushnumber(globalconf.L, data[i]); \
143 lua_rawseti(globalconf.L, -2, i + 1); \
146 break;
147 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(int16_t, DBUS_TYPE_INT16)
148 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(uint16_t, DBUS_TYPE_UINT16)
149 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(int32_t, DBUS_TYPE_INT32)
150 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(uint32_t, DBUS_TYPE_UINT32)
151 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(int64_t, DBUS_TYPE_INT64)
152 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(uint64_t, DBUS_TYPE_UINT64)
153 DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(double, DBUS_TYPE_DOUBLE)
154 #undef DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER
155 case DBUS_TYPE_BYTE:
157 const char *c;
158 dbus_message_iter_get_fixed_array(&sub, &c, &datalen);
159 lua_pushlstring(globalconf.L, c, datalen);
161 break;
162 case DBUS_TYPE_BOOLEAN:
164 const dbus_bool_t *b;
165 dbus_message_iter_get_fixed_array(&sub, &b, &datalen);
166 lua_createtable(globalconf.L, datalen, 0);
167 for(int i = 0; i < datalen; i++)
169 lua_pushboolean(globalconf.L, b[i]);
170 lua_rawseti(globalconf.L, -2, i + 1);
173 break;
176 else if(array_type == DBUS_TYPE_DICT_ENTRY)
178 DBusMessageIter subiter;
179 /* initialize a sub iterator */
180 dbus_message_iter_recurse(iter, &subiter);
182 /* get the keys and the values
183 * n is the number of entry in dict */
184 int n = a_dbus_message_iter(&subiter);
186 /* create a new table to store all the value */
187 lua_createtable(globalconf.L, n, 0);
188 /* move the table before array elements */
189 lua_insert(globalconf.L, - (n * 2) - 1);
191 for(int i = 0; i < n; i ++)
192 lua_rawset(globalconf.L, - (n * 2) - 1 + i * 2);
194 else
196 DBusMessageIter subiter;
197 /* prepare to dig into the array*/
198 dbus_message_iter_recurse(iter, &subiter);
200 /* now iterate over every element of the array */
201 int n = a_dbus_message_iter(&subiter);
203 /* create a new table to store all the value */
204 lua_createtable(globalconf.L, n, 0);
205 /* move the table before array elements */
206 lua_insert(globalconf.L, - n - 1);
208 for(int i = n; i > 0; i--)
209 lua_rawseti(globalconf.L, - i - 1, i);
212 nargs++;
213 break;
214 case DBUS_TYPE_BOOLEAN:
216 dbus_bool_t b;
217 dbus_message_iter_get_basic(iter, &b);
218 lua_pushboolean(globalconf.L, b);
220 nargs++;
221 break;
222 case DBUS_TYPE_BYTE:
224 char c;
225 dbus_message_iter_get_basic(iter, &c);
226 lua_pushlstring(globalconf.L, &c, 1);
228 nargs++;
229 break;
230 #define DBUS_MSG_HANDLE_TYPE_NUMBER(type, dbustype) \
231 case dbustype: \
233 type ui; \
234 dbus_message_iter_get_basic(iter, &ui); \
235 lua_pushnumber(globalconf.L, ui); \
237 nargs++; \
238 break;
239 DBUS_MSG_HANDLE_TYPE_NUMBER(int16_t, DBUS_TYPE_INT16)
240 DBUS_MSG_HANDLE_TYPE_NUMBER(uint16_t, DBUS_TYPE_UINT16)
241 DBUS_MSG_HANDLE_TYPE_NUMBER(int32_t, DBUS_TYPE_INT32)
242 DBUS_MSG_HANDLE_TYPE_NUMBER(uint32_t, DBUS_TYPE_UINT32)
243 DBUS_MSG_HANDLE_TYPE_NUMBER(int64_t, DBUS_TYPE_INT64)
244 DBUS_MSG_HANDLE_TYPE_NUMBER(uint64_t, DBUS_TYPE_UINT64)
245 #undef DBUS_MSG_HANDLE_TYPE_NUMBER
246 case DBUS_TYPE_STRING:
248 char *s;
249 dbus_message_iter_get_basic(iter, &s);
250 lua_pushstring(globalconf.L, s);
252 nargs++;
253 break;
255 } while(dbus_message_iter_next(iter));
257 return nargs;
260 static bool
261 a_dbus_convert_value(lua_State *L, int idx, DBusMessageIter *iter)
263 /* i is the type name, i+1 the value */
264 size_t len;
265 const char *type = lua_tolstring(globalconf.L, idx, &len);
267 if(!type || len < 1)
268 return false;
270 switch(*type)
272 case DBUS_TYPE_ARRAY:
274 DBusMessageIter subiter;
275 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
276 type + 1,
277 &subiter);
279 int arraylen = lua_objlen(L, idx + 1);
281 if(arraylen % 2 != 0)
282 luaL_error(globalconf.L,
283 "your D-Bus signal handling method returned wrong number of arguments");
285 /* Push the array */
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))
293 return false;
294 lua_pop(L, 2);
297 /* Remove the array */
298 lua_pop(L, 1);
300 dbus_message_iter_close_container(iter, &subiter);
302 break;
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);
308 break;
309 #define DBUS_MSG_RETURN_HANDLE_TYPE_STRING(dbustype) \
310 case dbustype: \
312 const char *s = lua_tostring(globalconf.L, idx + 1); \
313 if(s) \
314 dbus_message_iter_append_basic(iter, dbustype, &s); \
316 break;
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) \
321 case dbustype: \
323 type num = lua_tonumber(globalconf.L, idx + 1); \
324 dbus_message_iter_append_basic(iter, dbustype, &num); \
326 break;
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
337 return true;
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.
344 static void
345 a_dbus_process_request(DBusConnection *dbus_connection, DBusMessage *msg)
347 const char *interface = dbus_message_get_interface(msg);
349 lua_createtable(globalconf.L, 0, 5);
351 switch(dbus_message_get_type(msg))
353 case DBUS_MESSAGE_TYPE_SIGNAL:
354 lua_pushliteral(globalconf.L, "signal");
355 break;
356 case DBUS_MESSAGE_TYPE_METHOD_CALL:
357 lua_pushliteral(globalconf.L, "method_call");
358 break;
359 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
360 lua_pushliteral(globalconf.L, "method_return");
361 break;
362 case DBUS_MESSAGE_TYPE_ERROR:
363 lua_pushliteral(globalconf.L, "error");
364 break;
365 default:
366 lua_pushliteral(globalconf.L, "unknown");
367 break;
370 lua_setfield(globalconf.L, -2, "type");
372 lua_pushstring(globalconf.L, interface);
373 lua_setfield(globalconf.L, -2, "interface");
375 const char *s = dbus_message_get_path(msg);
376 lua_pushstring(globalconf.L, s);
377 lua_setfield(globalconf.L, -2, "path");
379 s = dbus_message_get_member(msg);
380 lua_pushstring(globalconf.L, s);
381 lua_setfield(globalconf.L, -2, "member");
383 if(dbus_connection == dbus_connection_system)
384 lua_pushliteral(globalconf.L, "system");
385 else
386 lua_pushliteral(globalconf.L, "session");
387 lua_setfield(globalconf.L, -2, "bus");
389 /* + 1 for the table above */
390 DBusMessageIter iter;
391 int nargs = 1;
393 if(dbus_message_iter_init(msg, &iter))
394 nargs += a_dbus_message_iter(&iter);
396 if(dbus_message_get_no_reply(msg))
397 /* emit signals */
398 signal_object_emit(globalconf.L, &dbus_signals, NONULL(interface), nargs);
399 else
401 signal_t *sig = signal_array_getbyid(&dbus_signals,
402 a_strhash((const unsigned char *) NONULL(interface)));
403 if(sig)
405 /* there can be only ONE handler to send reply */
406 void *func = (void *) sig->sigfuncs.tab[0];
408 int n = lua_gettop(globalconf.L) - nargs;
410 luaA_object_push(globalconf.L, (void *) func);
411 luaA_dofunction(globalconf.L, nargs, LUA_MULTRET);
413 n -= lua_gettop(globalconf.L);
415 DBusMessage *reply = dbus_message_new_method_return(msg);
417 dbus_message_iter_init_append(reply, &iter);
419 if(n % 2 != 0)
420 luaL_error(globalconf.L,
421 "your D-Bus signal handling method returned wrong number of arguments");
423 /* i is negative */
424 for(int i = n; i < 0; i += 2)
426 if(!a_dbus_convert_value(globalconf.L, i, &iter))
427 luaL_error(globalconf.L, "your D-Bus signal handling method returned bad data");
429 lua_remove(globalconf.L, i);
430 lua_remove(globalconf.L, i + 1);
433 dbus_connection_send(dbus_connection, reply, NULL);
434 dbus_message_unref(reply);
439 /** Attempt to process all the requests in the D-Bus connection.
440 * \param dbus_connection The D-Bus connection to process from
441 * \param dbusio The D-Bus event watcher
443 static void
444 a_dbus_process_requests_on_bus(DBusConnection *dbus_connection, ev_io *dbusio)
446 DBusMessage *msg;
447 int nmsg = 0;
449 while(true)
451 dbus_connection_read_write(dbus_connection, 0);
453 if(!(msg = dbus_connection_pop_message(dbus_connection)))
454 break;
456 if(dbus_message_is_signal(msg, DBUS_INTERFACE_LOCAL, "Disconnected"))
458 a_dbus_cleanup_bus(dbus_connection, dbusio);
459 dbus_message_unref(msg);
460 return;
462 else
463 a_dbus_process_request(dbus_connection, msg);
465 dbus_message_unref(msg);
467 nmsg++;
470 if(nmsg)
471 dbus_connection_flush(dbus_connection);
474 /** Foreword D-Bus process session requests on too the correct function.
475 * \param w The D-Bus event watcher
476 * \param revents (not used)
478 static void
479 a_dbus_process_requests_session(EV_P_ ev_io *w, int revents)
481 a_dbus_process_requests_on_bus(dbus_connection_session, w);
484 /** Foreword D-Bus process system requests on too the correct function.
485 * \param w The D-Bus event watcher
486 * \param revents (not used)
488 static void
489 a_dbus_process_requests_system(EV_P_ ev_io *w, int revents)
491 a_dbus_process_requests_on_bus(dbus_connection_system, w);
494 /** Attempt to request a D-Bus name
495 * \param dbus_connection The application's connection to D-Bus
496 * \param name The D-Bus connection name to be requested
497 * \return true if the name is primary owner or the name is already
498 * the owner, otherwise false.
500 static bool
501 a_dbus_request_name(DBusConnection *dbus_connection, const char *name)
503 DBusError err;
505 if(!dbus_connection)
506 return false;
508 dbus_error_init(&err);
510 int ret = dbus_bus_request_name(dbus_connection, name, 0, &err);
512 if(dbus_error_is_set(&err))
514 warn("failed to request D-Bus name: %s", err.message);
515 dbus_error_free(&err);
516 return false;
519 switch(ret)
521 case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
522 return true;
523 case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
524 warn("already primary D-Bus name owner for %s", name);
525 return true;
527 return false;
530 /** Attempt to release the D-Bus name owner
531 * \param dbus_connection The application's connection to the D-Bus
532 * \param name The name to be released
533 * \return True on success. False if the name is not
534 * the owner, or does not exist.
536 static bool
537 a_dbus_release_name(DBusConnection *dbus_connection, const char *name)
539 DBusError err;
541 if(!dbus_connection)
542 return false;
544 dbus_error_init(&err);
546 int ret = dbus_bus_release_name(dbus_connection, name, &err);
548 if(dbus_error_is_set(&err))
550 warn("failed to release D-Bus name: %s", err.message);
551 dbus_error_free(&err);
552 return false;
555 switch(ret)
557 case DBUS_RELEASE_NAME_REPLY_NOT_OWNER:
558 warn("not primary D-Bus name owner for %s", name);
559 return false;
560 case DBUS_RELEASE_NAME_REPLY_NON_EXISTENT:
561 warn("non existent D-Bus name: %s", name);
562 return false;
564 return true;
567 /** Attempt to create a new connection to D-Bus
568 * \param type The bus type to use when connecting to D-Bus
569 * \param type_name The bus type name eg: "session" or "system"
570 * \param dbusio The D-Bus event watcher
571 * \param cb Function callback to use when processing requests
572 * \return The requested D-Bus connection on success, NULL on failure.
574 static DBusConnection *
575 a_dbus_connect(DBusBusType type, const char *type_name,
576 ev_io *dbusio, void *cb)
578 int fd;
579 DBusConnection *dbus_connection;
580 DBusError err;
582 dbus_error_init(&err);
584 dbus_connection = dbus_bus_get(type, &err);
585 if(dbus_error_is_set(&err))
587 warn("D-Bus session bus %s failed: %s", type_name, err.message);
588 dbus_connection = NULL;
589 dbus_error_free(&err);
591 else
593 dbus_connection_set_exit_on_disconnect(dbus_connection, false);
594 if(dbus_connection_get_unix_fd(dbus_connection, &fd))
596 fcntl(fd, F_SETFD, FD_CLOEXEC);
598 ev_io_init(dbusio, cb, fd, EV_READ);
599 ev_io_start(EV_DEFAULT_UC_ dbusio);
600 ev_unref(EV_DEFAULT_UC);
602 else
604 warn("cannot get D-Bus connection file descriptor");
605 a_dbus_cleanup_bus(dbus_connection, dbusio);
609 return dbus_connection;
612 /** Initialize the D-Bus session and system
614 void
615 a_dbus_init(void)
617 dbus_connection_session = a_dbus_connect(DBUS_BUS_SESSION, "session",
618 &dbusio_ses, a_dbus_process_requests_session);
619 dbus_connection_system = a_dbus_connect(DBUS_BUS_SYSTEM, "system",
620 &dbusio_sys, a_dbus_process_requests_system);
623 /** Cleanup the D-Bus session and system
625 void
626 a_dbus_cleanup(void)
628 a_dbus_cleanup_bus(dbus_connection_session, &dbusio_ses);
629 a_dbus_cleanup_bus(dbus_connection_system, &dbusio_sys);
632 /** Retrieve the D-Bus bus by it's name
633 * \param name The name of the bus
634 * \param len The length of the name variable
635 * \return The corresponding D-Bus connection
637 static DBusConnection *
638 a_dbus_bus_getbyname(const char *name, size_t len)
640 switch(a_tokenize(name, len))
642 case A_TK_SYSTEM:
643 return dbus_connection_system;
644 case A_TK_SESSION:
645 return dbus_connection_session;
646 default:
647 return NULL;
651 /** Register a D-Bus name to receive message from.
652 * \param L The Lua VM state.
653 * \return The number of elements pushed on stack.
654 * \luastack
655 * \lparam A string indicating if we are using system or session bus.
656 * \lparam A string with the name of the D-Bus name to register.
657 * \lreturn True if everything worked fine, false otherwise.
659 static int
660 luaA_dbus_request_name(lua_State *L)
662 size_t len;
663 const char *bus = luaL_checklstring(L, 1, &len);
664 const char *name = luaL_checkstring(L, 2);
665 DBusConnection *dbus_connection = a_dbus_bus_getbyname(bus, len);
666 lua_pushboolean(L, a_dbus_request_name(dbus_connection, name));
667 return 1;
670 /** Release a D-Bus name.
671 * \param L The Lua VM state.
672 * \return The number of elements pushed on stack.
673 * \luastack
674 * \lparam A string indicating if we are using system or session bus.
675 * \lparam A string with the name of the D-Bus name to unregister.
676 * \lreturn True if everything worked fine, false otherwise.
678 static int
679 luaA_dbus_release_name(lua_State *L)
681 size_t len;
682 const char *bus = luaL_checklstring(L, 1, &len);
683 const char *name = luaL_checkstring(L, 2);
684 DBusConnection *dbus_connection = a_dbus_bus_getbyname(bus, len);
685 lua_pushboolean(L, a_dbus_release_name(dbus_connection, name));
686 return 1;
689 /** Add a match rule to match messages going through the message bus.
690 * \param L The Lua VM state.
691 * \return The number of elements pushed on stack.
692 * \luastack
693 * \lparam A string indicating if we are using system or session bus.
694 * \lparam A string with the name of the match rule.
696 static int
697 luaA_dbus_add_match(lua_State *L)
699 size_t len;
700 const char *bus = luaL_checklstring(L, 1, &len);
701 const char *name = luaL_checkstring(L, 2);
702 DBusConnection *dbus_connection = a_dbus_bus_getbyname(bus, len);
704 if(dbus_connection)
706 dbus_bus_add_match(dbus_connection, name, NULL);
707 dbus_connection_flush(dbus_connection);
710 return 0;
713 /** Remove a previously added match rule "by value"
714 * (the most recently-added identical rule gets removed).
715 * \param L The Lua VM state.
716 * \return The number of elements pushed on stack.
717 * \luastack
718 * \lparam A string indicating if we are using system or session bus.
719 * \lparam A string with the name of the match rule.
721 static int
722 luaA_dbus_remove_match(lua_State *L)
724 size_t len;
725 const char *bus = luaL_checklstring(L, 1, &len);
726 const char *name = luaL_checkstring(L, 2);
727 DBusConnection *dbus_connection = a_dbus_bus_getbyname(bus, len);
729 if(dbus_connection)
731 dbus_bus_remove_match(dbus_connection, name, NULL);
732 dbus_connection_flush(dbus_connection);
735 return 0;
738 /** Add a signal receiver on the D-Bus.
739 * \param L The Lua VM state.
740 * \return The number of elements pushed on stack.
741 * \luastack
742 * \lparam A string with the interface name.
743 * \lparam The function to call.
745 static int
746 luaA_dbus_add_signal(lua_State *L)
748 const char *name = luaL_checkstring(L, 1);
749 luaA_checkfunction(L, 2);
750 signal_t *sig = signal_array_getbyid(&dbus_signals,
751 a_strhash((const unsigned char *) name));
752 if(sig)
753 luaA_warn(L, "cannot add signal %s on D-Bus, already existing", name);
754 else
755 signal_add(&dbus_signals, name, luaA_object_ref(L, 2));
756 return 0;
759 /** Add a signal receiver on the D-Bus.
760 * \param L The Lua VM state.
761 * \return The number of elements pushed on stack.
762 * \luastack
763 * \lparam A string with the interface name.
764 * \lparam The function to call.
766 static int
767 luaA_dbus_remove_signal(lua_State *L)
769 const char *name = luaL_checkstring(L, 1);
770 luaA_checkfunction(L, 2);
771 const void *func = lua_topointer(L, 2);
772 signal_remove(&dbus_signals, name, func);
773 luaA_object_unref(L, (void *) func);
774 return 0;
777 const struct luaL_reg awesome_dbus_lib[] =
779 { "request_name", luaA_dbus_request_name },
780 { "release_name", luaA_dbus_release_name },
781 { "add_match", luaA_dbus_add_match },
782 { "remove_match", luaA_dbus_remove_match },
783 { "add_signal", luaA_dbus_add_signal },
784 { "remove_signal", luaA_dbus_remove_signal },
785 { NULL, NULL }
788 #else /* WITH_DBUS */
790 /** Empty stub if dbus is not enabled */
791 void
792 a_dbus_init(void)
796 /** Empty stub if dbus is not enabled */
797 void
798 a_dbus_cleanup(void)
802 #endif
803 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80