tag: Improve tag property::index support (FS#1229)
[awesome.git] / dbus.c
blob5b65acb886b6de8c0163a9ce402d18f30f5dea7b
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"
23 #include "dbus.h"
25 #include <glib.h>
27 #ifdef WITH_DBUS
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 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
47 static void
48 a_dbus_cleanup_bus(DBusConnection *dbus_connection, GSource **source)
50 if(!dbus_connection)
51 return;
53 if (*source != NULL)
54 g_source_destroy(*source);
55 *source = NULL;
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
67 static int
68 a_dbus_message_iter(DBusMessageIter *iter)
70 int nargs = 0;
74 switch(dbus_message_iter_get_arg_type(iter))
76 default:
77 lua_pushnil(globalconf.L);
78 nargs++;
79 break;
80 case DBUS_TYPE_INVALID:
81 break;
82 case DBUS_TYPE_VARIANT:
84 DBusMessageIter subiter;
85 dbus_message_iter_recurse(iter, &subiter);
86 a_dbus_message_iter(&subiter);
88 nargs++;
89 break;
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);
99 nargs++;
100 break;
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);
117 nargs++;
118 break;
119 case DBUS_TYPE_ARRAY:
121 int array_type = dbus_message_iter_get_element_type(iter);
123 if(dbus_type_is_fixed(array_type))
125 DBusMessageIter sub;
126 dbus_message_iter_recurse(iter, &sub);
128 switch(array_type)
130 int datalen = 0;
131 #define DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(type, dbustype) \
132 case dbustype: \
134 const type *data; \
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); \
143 break;
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
152 case DBUS_TYPE_BYTE:
154 const char *c;
155 dbus_message_iter_get_fixed_array(&sub, &c, &datalen);
156 lua_pushlstring(globalconf.L, c, datalen);
158 break;
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);
170 break;
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);
191 else
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);
209 nargs++;
210 break;
211 case DBUS_TYPE_BOOLEAN:
213 dbus_bool_t b;
214 dbus_message_iter_get_basic(iter, &b);
215 lua_pushboolean(globalconf.L, b);
217 nargs++;
218 break;
219 case DBUS_TYPE_BYTE:
221 char c;
222 dbus_message_iter_get_basic(iter, &c);
223 lua_pushlstring(globalconf.L, &c, 1);
225 nargs++;
226 break;
227 #define DBUS_MSG_HANDLE_TYPE_NUMBER(type, dbustype) \
228 case dbustype: \
230 type ui; \
231 dbus_message_iter_get_basic(iter, &ui); \
232 lua_pushnumber(globalconf.L, ui); \
234 nargs++; \
235 break;
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:
245 char *s;
246 dbus_message_iter_get_basic(iter, &s);
247 lua_pushstring(globalconf.L, s);
249 nargs++;
250 break;
252 } while(dbus_message_iter_next(iter));
254 return nargs;
257 static bool
258 a_dbus_convert_value(lua_State *L, int idx, DBusMessageIter *iter)
260 /* i is the type name, i+1 the value */
261 size_t len;
262 const char *type = lua_tolstring(globalconf.L, idx, &len);
264 if(!type || len < 1)
265 return false;
267 switch(*type)
269 case DBUS_TYPE_ARRAY:
271 DBusMessageIter subiter;
272 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
273 type + 1,
274 &subiter);
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");
282 return false;
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);
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");
356 break;
357 case DBUS_MESSAGE_TYPE_METHOD_CALL:
358 lua_pushliteral(globalconf.L, "method_call");
359 break;
360 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
361 lua_pushliteral(globalconf.L, "method_return");
362 break;
363 case DBUS_MESSAGE_TYPE_ERROR:
364 lua_pushliteral(globalconf.L, "error");
365 break;
366 default:
367 lua_pushliteral(globalconf.L, "unknown");
368 break;
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");
386 else
387 lua_pushliteral(globalconf.L, "session");
388 lua_setfield(globalconf.L, -2, "bus");
390 /* + 1 for the table above */
391 DBusMessageIter iter;
392 int nargs = 1;
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)));
401 /* emit signals */
402 if(sigfound)
403 signal_object_emit(globalconf.L, &dbus_signals, NONULL(interface), nargs);
405 else
407 signal_t *sig = signal_array_getbyid(&dbus_signals,
408 a_strhash((const unsigned char *) NONULL(interface)));
409 if(sig)
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);
425 if(n % 2 != 0)
427 luaA_warn(globalconf.L,
428 "your D-Bus signal handling method returned wrong number of arguments");
429 /* Restore stack */
430 lua_settop(globalconf.L, old_top);
431 return;
434 /* i is negative */
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");
440 /* Restore stack */
441 lua_settop(globalconf.L, old_top);
442 return;
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);
453 /* Restore stack */
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
461 static void
462 a_dbus_process_requests_on_bus(DBusConnection *dbus_connection, GSource **source)
464 DBusMessage *msg;
465 int nmsg = 0;
467 while(true)
469 dbus_connection_read_write(dbus_connection, 0);
471 if(!(msg = dbus_connection_pop_message(dbus_connection)))
472 break;
474 if(dbus_message_is_signal(msg, DBUS_INTERFACE_LOCAL, "Disconnected"))
476 a_dbus_cleanup_bus(dbus_connection, source);
477 dbus_message_unref(msg);
478 return;
480 else
481 a_dbus_process_request(dbus_connection, msg);
483 dbus_message_unref(msg);
485 nmsg++;
488 if(nmsg)
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)
496 static gboolean
497 a_dbus_process_requests_session(gpointer data)
499 a_dbus_process_requests_on_bus(dbus_connection_session, &session_source);
500 return TRUE;
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)
507 static gboolean
508 a_dbus_process_requests_system(gpointer data)
510 a_dbus_process_requests_on_bus(dbus_connection_system, &system_source);
511 return TRUE;
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.
520 static bool
521 a_dbus_request_name(DBusConnection *dbus_connection, const char *name)
523 DBusError err;
525 if(!dbus_connection)
526 return false;
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);
536 return false;
539 switch(ret)
541 case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
542 return true;
543 case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
544 warn("already primary D-Bus name owner for %s", name);
545 return true;
547 return false;
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.
556 static bool
557 a_dbus_release_name(DBusConnection *dbus_connection, const char *name)
559 DBusError err;
561 if(!dbus_connection)
562 return false;
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);
572 return false;
575 switch(ret)
577 case DBUS_RELEASE_NAME_REPLY_NOT_OWNER:
578 warn("not primary D-Bus name owner for %s", name);
579 return false;
580 case DBUS_RELEASE_NAME_REPLY_NON_EXISTENT:
581 warn("non existent D-Bus name: %s", name);
582 return false;
584 return true;
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)
597 int fd;
598 DBusConnection *dbus_connection;
599 DBusError err;
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);
610 else
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);
623 else
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
635 void
636 a_dbus_init(void)
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
646 void
647 a_dbus_cleanup(void)
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;
664 return NULL;
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.
670 * \luastack
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.
675 static int
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));
682 return 1;
685 /** Release a D-Bus name.
686 * \param L The Lua VM state.
687 * \return The number of elements pushed on stack.
688 * \luastack
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.
693 static int
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));
700 return 1;
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.
706 * \luastack
707 * \lparam A string indicating if we are using system or session bus.
708 * \lparam A string with the name of the match rule.
710 static int
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);
717 if(dbus_connection)
719 dbus_bus_add_match(dbus_connection, name, NULL);
720 dbus_connection_flush(dbus_connection);
723 return 0;
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.
730 * \luastack
731 * \lparam A string indicating if we are using system or session bus.
732 * \lparam A string with the name of the match rule.
734 static int
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);
741 if(dbus_connection)
743 dbus_bus_remove_match(dbus_connection, name, NULL);
744 dbus_connection_flush(dbus_connection);
747 return 0;
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.
753 * \luastack
754 * \lparam A string with the interface name.
755 * \lparam The function to call.
757 static int
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));
764 if(sig)
765 luaA_warn(L, "cannot add signal %s on D-Bus, already existing", name);
766 else
768 signal_add(&dbus_signals, name);
769 signal_connect(&dbus_signals, name, luaA_object_ref(L, 2));
771 return 0;
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.
777 * \luastack
778 * \lparam A string with the interface name.
779 * \lparam The function to call.
781 static int
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);
789 return 0;
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 },
800 { "__index", luaA_default_index },
801 { "__newindex", luaA_default_newindex },
802 { NULL, NULL }
805 #else /* WITH_DBUS */
807 /** Empty stub if dbus is not enabled */
808 void
809 a_dbus_init(void)
813 /** Empty stub if dbus is not enabled */
814 void
815 a_dbus_cleanup(void)
819 #endif
820 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80