Mirror of Awesome WM window manager
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dbus.c 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. /*
  2. * dbus.c - awesome dbus support
  3. *
  4. * Copyright © 2008-2009 Julien Danjou <julien@danjou.info>
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. *
  20. */
  21. #include "config.h"
  22. #include "dbus.h"
  23. #include <glib.h>
  24. #ifdef WITH_DBUS
  25. #include <dbus/dbus.h>
  26. #include <unistd.h>
  27. #include <fcntl.h>
  28. #include "event.h"
  29. #include "luaa.h"
  30. static DBusConnection *dbus_connection_session = NULL;
  31. static DBusConnection *dbus_connection_system = NULL;
  32. static GSource *session_source = NULL;
  33. static GSource *system_source = NULL;
  34. static signal_array_t dbus_signals;
  35. /** Clean up the D-Bus connection data members
  36. * \param dbus_connection The D-Bus connection to clean up
  37. * \param source The D-Bus source
  38. */
  39. static void
  40. a_dbus_cleanup_bus(DBusConnection *dbus_connection, GSource **source)
  41. {
  42. if(!dbus_connection)
  43. return;
  44. if (*source != NULL)
  45. g_source_destroy(*source);
  46. *source = NULL;
  47. /* This is a shared connection owned by libdbus
  48. * Do not close it, only unref
  49. */
  50. dbus_connection_unref(dbus_connection);
  51. }
  52. /** Iterate through the D-Bus messages counting each or traverse each sub message.
  53. * \param L The Lua VM state.
  54. * \param iter The D-Bus message iterator pointer
  55. * \return The number of arguments in the iterator
  56. */
  57. static int
  58. a_dbus_message_iter(lua_State *L, DBusMessageIter *iter)
  59. {
  60. int nargs = 0;
  61. do
  62. {
  63. switch(dbus_message_iter_get_arg_type(iter))
  64. {
  65. default:
  66. lua_pushnil(L);
  67. nargs++;
  68. break;
  69. case DBUS_TYPE_INVALID:
  70. break;
  71. case DBUS_TYPE_VARIANT:
  72. {
  73. DBusMessageIter subiter;
  74. dbus_message_iter_recurse(iter, &subiter);
  75. a_dbus_message_iter(L, &subiter);
  76. }
  77. nargs++;
  78. break;
  79. case DBUS_TYPE_DICT_ENTRY:
  80. {
  81. DBusMessageIter subiter;
  82. /* initialize a sub iterator */
  83. dbus_message_iter_recurse(iter, &subiter);
  84. /* create a new table to store the dict */
  85. a_dbus_message_iter(L, &subiter);
  86. }
  87. nargs++;
  88. break;
  89. case DBUS_TYPE_STRUCT:
  90. {
  91. DBusMessageIter subiter;
  92. /* initialize a sub iterator */
  93. dbus_message_iter_recurse(iter, &subiter);
  94. int n = a_dbus_message_iter(L, &subiter);
  95. /* create a new table to store all the value */
  96. lua_createtable(L, n, 0);
  97. /* move the table before array elements */
  98. lua_insert(L, - n - 1);
  99. for(int i = n; i > 0; i--)
  100. lua_rawseti(L, - i - 1, i);
  101. }
  102. nargs++;
  103. break;
  104. case DBUS_TYPE_ARRAY:
  105. {
  106. int array_type = dbus_message_iter_get_element_type(iter);
  107. if(dbus_type_is_fixed(array_type))
  108. {
  109. DBusMessageIter sub;
  110. dbus_message_iter_recurse(iter, &sub);
  111. switch(array_type)
  112. {
  113. int datalen = 0;
  114. #define DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(type, dbustype) \
  115. case dbustype: \
  116. { \
  117. const type *data; \
  118. dbus_message_iter_get_fixed_array(&sub, &data, &datalen); \
  119. lua_createtable(L, datalen, 0); \
  120. for(int i = 0; i < datalen; i++) \
  121. { \
  122. lua_pushnumber(L, data[i]); \
  123. lua_rawseti(L, -2, i + 1); \
  124. } \
  125. } \
  126. break;
  127. DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(int16_t, DBUS_TYPE_INT16)
  128. DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(uint16_t, DBUS_TYPE_UINT16)
  129. DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(int32_t, DBUS_TYPE_INT32)
  130. DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(uint32_t, DBUS_TYPE_UINT32)
  131. DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(int64_t, DBUS_TYPE_INT64)
  132. DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(uint64_t, DBUS_TYPE_UINT64)
  133. DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(double, DBUS_TYPE_DOUBLE)
  134. #undef DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER
  135. case DBUS_TYPE_BYTE:
  136. {
  137. const char *c;
  138. dbus_message_iter_get_fixed_array(&sub, &c, &datalen);
  139. lua_pushlstring(L, c, datalen);
  140. }
  141. break;
  142. case DBUS_TYPE_BOOLEAN:
  143. {
  144. const dbus_bool_t *b;
  145. dbus_message_iter_get_fixed_array(&sub, &b, &datalen);
  146. lua_createtable(L, datalen, 0);
  147. for(int i = 0; i < datalen; i++)
  148. {
  149. lua_pushboolean(L, b[i]);
  150. lua_rawseti(L, -2, i + 1);
  151. }
  152. }
  153. break;
  154. }
  155. }
  156. else if(array_type == DBUS_TYPE_DICT_ENTRY)
  157. {
  158. DBusMessageIter subiter;
  159. /* initialize a sub iterator */
  160. dbus_message_iter_recurse(iter, &subiter);
  161. /* get the keys and the values
  162. * n is the number of entry in dict */
  163. int n = a_dbus_message_iter(L, &subiter);
  164. /* create a new table to store all the value */
  165. lua_createtable(L, n, 0);
  166. /* move the table before array elements */
  167. lua_insert(L, - (n * 2) - 1);
  168. for(int i = 0; i < n; i ++)
  169. lua_rawset(L, - (n * 2) - 1 + i * 2);
  170. }
  171. else
  172. {
  173. DBusMessageIter subiter;
  174. /* prepare to dig into the array*/
  175. dbus_message_iter_recurse(iter, &subiter);
  176. /* now iterate over every element of the array */
  177. int n = a_dbus_message_iter(L, &subiter);
  178. /* create a new table to store all the value */
  179. lua_createtable(L, n, 0);
  180. /* move the table before array elements */
  181. lua_insert(L, - n - 1);
  182. for(int i = n; i > 0; i--)
  183. lua_rawseti(L, - i - 1, i);
  184. }
  185. }
  186. nargs++;
  187. break;
  188. case DBUS_TYPE_BOOLEAN:
  189. {
  190. dbus_bool_t b;
  191. dbus_message_iter_get_basic(iter, &b);
  192. lua_pushboolean(L, b);
  193. }
  194. nargs++;
  195. break;
  196. case DBUS_TYPE_BYTE:
  197. {
  198. char c;
  199. dbus_message_iter_get_basic(iter, &c);
  200. lua_pushlstring(L, &c, 1);
  201. }
  202. nargs++;
  203. break;
  204. #define DBUS_MSG_HANDLE_TYPE_NUMBER(type, dbustype) \
  205. case dbustype: \
  206. { \
  207. type ui; \
  208. dbus_message_iter_get_basic(iter, &ui); \
  209. lua_pushnumber(L, ui); \
  210. } \
  211. nargs++; \
  212. break;
  213. DBUS_MSG_HANDLE_TYPE_NUMBER(int16_t, DBUS_TYPE_INT16)
  214. DBUS_MSG_HANDLE_TYPE_NUMBER(uint16_t, DBUS_TYPE_UINT16)
  215. DBUS_MSG_HANDLE_TYPE_NUMBER(int32_t, DBUS_TYPE_INT32)
  216. DBUS_MSG_HANDLE_TYPE_NUMBER(uint32_t, DBUS_TYPE_UINT32)
  217. DBUS_MSG_HANDLE_TYPE_NUMBER(int64_t, DBUS_TYPE_INT64)
  218. DBUS_MSG_HANDLE_TYPE_NUMBER(uint64_t, DBUS_TYPE_UINT64)
  219. DBUS_MSG_HANDLE_TYPE_NUMBER(double, DBUS_TYPE_DOUBLE)
  220. #undef DBUS_MSG_HANDLE_TYPE_NUMBER
  221. case DBUS_TYPE_STRING:
  222. {
  223. char *s;
  224. dbus_message_iter_get_basic(iter, &s);
  225. lua_pushstring(L, s);
  226. }
  227. nargs++;
  228. break;
  229. }
  230. } while(dbus_message_iter_next(iter));
  231. return nargs;
  232. }
  233. static bool
  234. a_dbus_convert_value(lua_State *L, int idx, DBusMessageIter *iter)
  235. {
  236. /* i is the type name, i+1 the value */
  237. size_t len;
  238. const char *type = lua_tolstring(L, idx, &len);
  239. if(!type || len < 1)
  240. return false;
  241. switch(*type)
  242. {
  243. case DBUS_TYPE_ARRAY:
  244. {
  245. DBusMessageIter subiter;
  246. dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
  247. type + 1,
  248. &subiter);
  249. int arraylen = luaA_rawlen(L, idx + 1);
  250. if(arraylen % 2 != 0)
  251. {
  252. luaA_warn(L,
  253. "your D-Bus signal handling method returned wrong number of arguments");
  254. return false;
  255. }
  256. /* Push the array */
  257. lua_pushvalue(L, idx + 1);
  258. for(int i = 1; i < arraylen; i += 2)
  259. {
  260. lua_rawgeti(L, -1, i);
  261. lua_rawgeti(L, -2, i + 1);
  262. if(!a_dbus_convert_value(L, -2, &subiter))
  263. return false;
  264. lua_pop(L, 2);
  265. }
  266. /* Remove the array */
  267. lua_pop(L, 1);
  268. dbus_message_iter_close_container(iter, &subiter);
  269. }
  270. break;
  271. case DBUS_TYPE_BOOLEAN:
  272. {
  273. dbus_bool_t b = lua_toboolean(L, idx + 1);
  274. dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &b);
  275. }
  276. break;
  277. #define DBUS_MSG_RETURN_HANDLE_TYPE_STRING(dbustype) \
  278. case dbustype: \
  279. { \
  280. const char *s = lua_tostring(L, idx + 1); \
  281. if(s) \
  282. dbus_message_iter_append_basic(iter, dbustype, &s); \
  283. } \
  284. break;
  285. DBUS_MSG_RETURN_HANDLE_TYPE_STRING(DBUS_TYPE_STRING)
  286. DBUS_MSG_RETURN_HANDLE_TYPE_STRING(DBUS_TYPE_BYTE)
  287. #undef DBUS_MSG_RETURN_HANDLE_TYPE_STRING
  288. #define DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(type, dbustype) \
  289. case dbustype: \
  290. { \
  291. type num = lua_tonumber(L, idx + 1); \
  292. dbus_message_iter_append_basic(iter, dbustype, &num); \
  293. } \
  294. break;
  295. DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(int16_t, DBUS_TYPE_INT16)
  296. DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(uint16_t, DBUS_TYPE_UINT16)
  297. DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(int32_t, DBUS_TYPE_INT32)
  298. DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(uint32_t, DBUS_TYPE_UINT32)
  299. DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(int64_t, DBUS_TYPE_INT64)
  300. DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(uint64_t, DBUS_TYPE_UINT64)
  301. DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(double, DBUS_TYPE_DOUBLE)
  302. #undef DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER
  303. }
  304. return true;
  305. }
  306. /** Process a single request from D-Bus
  307. * \param dbus_connection The connection to the D-Bus server.
  308. * \param msg The D-Bus message request being sent to the D-Bus connection.
  309. */
  310. static void
  311. a_dbus_process_request(DBusConnection *dbus_connection, DBusMessage *msg)
  312. {
  313. const char *interface = dbus_message_get_interface(msg);
  314. lua_State *L = globalconf_get_lua_State();
  315. int old_top = lua_gettop(L);
  316. lua_createtable(L, 0, 5);
  317. switch(dbus_message_get_type(msg))
  318. {
  319. case DBUS_MESSAGE_TYPE_SIGNAL:
  320. lua_pushliteral(L, "signal");
  321. break;
  322. case DBUS_MESSAGE_TYPE_METHOD_CALL:
  323. lua_pushliteral(L, "method_call");
  324. break;
  325. case DBUS_MESSAGE_TYPE_METHOD_RETURN:
  326. lua_pushliteral(L, "method_return");
  327. break;
  328. case DBUS_MESSAGE_TYPE_ERROR:
  329. lua_pushliteral(L, "error");
  330. break;
  331. default:
  332. lua_pushliteral(L, "unknown");
  333. break;
  334. }
  335. lua_setfield(L, -2, "type");
  336. lua_pushstring(L, interface);
  337. lua_setfield(L, -2, "interface");
  338. const char *s = dbus_message_get_path(msg);
  339. lua_pushstring(L, s);
  340. lua_setfield(L, -2, "path");
  341. s = dbus_message_get_member(msg);
  342. lua_pushstring(L, s);
  343. lua_setfield(L, -2, "member");
  344. if(dbus_connection == dbus_connection_system)
  345. lua_pushliteral(L, "system");
  346. else
  347. lua_pushliteral(L, "session");
  348. lua_setfield(L, -2, "bus");
  349. /* + 1 for the table above */
  350. DBusMessageIter iter;
  351. int nargs = 1;
  352. if(dbus_message_iter_init(msg, &iter))
  353. nargs += a_dbus_message_iter(L, &iter);
  354. if(dbus_message_get_no_reply(msg))
  355. {
  356. signal_t *sigfound = signal_array_getbyid(&dbus_signals,
  357. a_strhash((const unsigned char *) NONULL(interface)));
  358. /* emit signals */
  359. if(sigfound)
  360. signal_object_emit(L, &dbus_signals, NONULL(interface), nargs);
  361. }
  362. else
  363. {
  364. signal_t *sig = signal_array_getbyid(&dbus_signals,
  365. a_strhash((const unsigned char *) NONULL(interface)));
  366. if(sig)
  367. {
  368. /* there can be only ONE handler to send reply */
  369. void *func = (void *) sig->sigfuncs.tab[0];
  370. int n = lua_gettop(L) - nargs;
  371. luaA_object_push(L, (void *) func);
  372. luaA_dofunction(L, nargs, LUA_MULTRET);
  373. n -= lua_gettop(L);
  374. DBusMessage *reply = dbus_message_new_method_return(msg);
  375. dbus_message_iter_init_append(reply, &iter);
  376. if(n % 2 != 0)
  377. {
  378. luaA_warn(L,
  379. "your D-Bus signal handling method returned wrong number of arguments");
  380. /* Restore stack */
  381. lua_settop(L, old_top);
  382. return;
  383. }
  384. /* i is negative */
  385. for(int i = n; i < 0; i += 2)
  386. {
  387. if(!a_dbus_convert_value(L, i, &iter))
  388. {
  389. luaA_warn(L, "your D-Bus signal handling method returned bad data");
  390. /* Restore stack */
  391. lua_settop(L, old_top);
  392. return;
  393. }
  394. lua_remove(L, i);
  395. lua_remove(L, i + 1);
  396. }
  397. dbus_connection_send(dbus_connection, reply, NULL);
  398. dbus_message_unref(reply);
  399. }
  400. }
  401. /* Restore stack */
  402. lua_settop(L, old_top);
  403. }
  404. /** Attempt to process all the requests in the D-Bus connection.
  405. * \param dbus_connection The D-Bus connection to process from
  406. * \param source The D-Bus source
  407. */
  408. static void
  409. a_dbus_process_requests_on_bus(DBusConnection *dbus_connection, GSource **source)
  410. {
  411. DBusMessage *msg;
  412. int nmsg = 0;
  413. while(true)
  414. {
  415. dbus_connection_read_write(dbus_connection, 0);
  416. if(!(msg = dbus_connection_pop_message(dbus_connection)))
  417. break;
  418. if(dbus_message_is_signal(msg, DBUS_INTERFACE_LOCAL, "Disconnected"))
  419. {
  420. a_dbus_cleanup_bus(dbus_connection, source);
  421. dbus_message_unref(msg);
  422. return;
  423. }
  424. else
  425. a_dbus_process_request(dbus_connection, msg);
  426. dbus_message_unref(msg);
  427. nmsg++;
  428. }
  429. if(nmsg)
  430. dbus_connection_flush(dbus_connection);
  431. }
  432. /** Foreword D-Bus process session requests on too the correct function.
  433. * \param data
  434. */
  435. static gboolean
  436. a_dbus_process_requests_session(gpointer data)
  437. {
  438. a_dbus_process_requests_on_bus(dbus_connection_session, &session_source);
  439. return TRUE;
  440. }
  441. /** Foreword D-Bus process system requests on too the correct function.
  442. * \param data
  443. */
  444. static gboolean
  445. a_dbus_process_requests_system(gpointer data)
  446. {
  447. a_dbus_process_requests_on_bus(dbus_connection_system, &system_source);
  448. return TRUE;
  449. }
  450. /** Attempt to request a D-Bus name
  451. * \param dbus_connection The application's connection to D-Bus
  452. * \param name The D-Bus connection name to be requested
  453. * \return true if the name is primary owner or the name is already
  454. * the owner, otherwise false.
  455. */
  456. static bool
  457. a_dbus_request_name(DBusConnection *dbus_connection, const char *name)
  458. {
  459. DBusError err;
  460. if(!dbus_connection)
  461. return false;
  462. dbus_error_init(&err);
  463. int ret = dbus_bus_request_name(dbus_connection, name, 0, &err);
  464. if(dbus_error_is_set(&err))
  465. {
  466. warn("failed to request D-Bus name: %s", err.message);
  467. dbus_error_free(&err);
  468. return false;
  469. }
  470. switch(ret)
  471. {
  472. case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
  473. return true;
  474. case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
  475. warn("already primary D-Bus name owner for %s", name);
  476. return true;
  477. }
  478. return false;
  479. }
  480. /** Attempt to release the D-Bus name owner
  481. * \param dbus_connection The application's connection to the D-Bus
  482. * \param name The name to be released
  483. * \return True on success. False if the name is not
  484. * the owner, or does not exist.
  485. */
  486. static bool
  487. a_dbus_release_name(DBusConnection *dbus_connection, const char *name)
  488. {
  489. DBusError err;
  490. if(!dbus_connection)
  491. return false;
  492. dbus_error_init(&err);
  493. int ret = dbus_bus_release_name(dbus_connection, name, &err);
  494. if(dbus_error_is_set(&err))
  495. {
  496. warn("failed to release D-Bus name: %s", err.message);
  497. dbus_error_free(&err);
  498. return false;
  499. }
  500. switch(ret)
  501. {
  502. case DBUS_RELEASE_NAME_REPLY_NOT_OWNER:
  503. warn("not primary D-Bus name owner for %s", name);
  504. return false;
  505. case DBUS_RELEASE_NAME_REPLY_NON_EXISTENT:
  506. warn("non existent D-Bus name: %s", name);
  507. return false;
  508. }
  509. return true;
  510. }
  511. /** Attempt to create a new connection to D-Bus
  512. * \param type The bus type to use when connecting to D-Bus
  513. * \param type_name The bus type name eg: "session" or "system"
  514. * \param cb Function callback to use when processing requests
  515. * \param source A new GSource that will be used for watching the dbus connection.
  516. * \return The requested D-Bus connection on success, NULL on failure.
  517. */
  518. static DBusConnection *
  519. a_dbus_connect(DBusBusType type, const char *type_name, GSourceFunc cb, GSource **source)
  520. {
  521. int fd;
  522. DBusConnection *dbus_connection;
  523. DBusError err;
  524. dbus_error_init(&err);
  525. dbus_connection = dbus_bus_get(type, &err);
  526. if(dbus_error_is_set(&err))
  527. {
  528. warn("D-Bus session bus %s failed: %s", type_name, err.message);
  529. dbus_connection = NULL;
  530. dbus_error_free(&err);
  531. }
  532. else
  533. {
  534. dbus_connection_set_exit_on_disconnect(dbus_connection, false);
  535. if(dbus_connection_get_unix_fd(dbus_connection, &fd))
  536. {
  537. GIOChannel *channel = g_io_channel_unix_new(fd);
  538. *source = g_io_create_watch(channel, G_IO_IN);
  539. g_io_channel_unref(channel);
  540. g_source_set_callback(*source, cb, NULL, NULL);
  541. g_source_attach(*source, NULL);
  542. fcntl(fd, F_SETFD, FD_CLOEXEC);
  543. }
  544. else
  545. {
  546. warn("cannot get D-Bus connection file descriptor");
  547. a_dbus_cleanup_bus(dbus_connection, source);
  548. }
  549. }
  550. return dbus_connection;
  551. }
  552. /** Initialize the D-Bus session and system
  553. */
  554. void
  555. a_dbus_init(void)
  556. {
  557. dbus_connection_session = a_dbus_connect(DBUS_BUS_SESSION, "session",
  558. a_dbus_process_requests_session, &session_source);
  559. dbus_connection_system = a_dbus_connect(DBUS_BUS_SYSTEM, "system",
  560. a_dbus_process_requests_system, &system_source);
  561. }
  562. /** Cleanup the D-Bus session and system
  563. */
  564. void
  565. a_dbus_cleanup(void)
  566. {
  567. a_dbus_cleanup_bus(dbus_connection_session, &session_source);
  568. a_dbus_cleanup_bus(dbus_connection_system, &system_source);
  569. }
  570. /** Retrieve the D-Bus bus by it's name
  571. * \param name The name of the bus
  572. * \return The corresponding D-Bus connection
  573. */
  574. static DBusConnection *
  575. a_dbus_bus_getbyname(const char *name)
  576. {
  577. if(A_STREQ(name, "system"))
  578. return dbus_connection_system;
  579. if(A_STREQ(name, "session"))
  580. return dbus_connection_session;
  581. return NULL;
  582. }
  583. /** Register a D-Bus name to receive message from.
  584. * \param L The Lua VM state.
  585. * \return The number of elements pushed on stack.
  586. * \luastack
  587. * \lparam A string indicating if we are using system or session bus.
  588. * \lparam A string with the name of the D-Bus name to register.
  589. * \lreturn True if everything worked fine, false otherwise.
  590. */
  591. static int
  592. luaA_dbus_request_name(lua_State *L)
  593. {
  594. const char *bus = luaL_checkstring(L, 1);
  595. const char *name = luaL_checkstring(L, 2);
  596. DBusConnection *dbus_connection = a_dbus_bus_getbyname(bus);
  597. lua_pushboolean(L, a_dbus_request_name(dbus_connection, name));
  598. return 1;
  599. }
  600. /** Release a D-Bus name.
  601. * \param L The Lua VM state.
  602. * \return The number of elements pushed on stack.
  603. * \luastack
  604. * \lparam A string indicating if we are using system or session bus.
  605. * \lparam A string with the name of the D-Bus name to unregister.
  606. * \lreturn True if everything worked fine, false otherwise.
  607. */
  608. static int
  609. luaA_dbus_release_name(lua_State *L)
  610. {
  611. const char *bus = luaL_checkstring(L, 1);
  612. const char *name = luaL_checkstring(L, 2);
  613. DBusConnection *dbus_connection = a_dbus_bus_getbyname(bus);
  614. lua_pushboolean(L, a_dbus_release_name(dbus_connection, name));
  615. return 1;
  616. }
  617. /** Add a match rule to match messages going through the message bus.
  618. * \param L The Lua VM state.
  619. * \return The number of elements pushed on stack.
  620. * \luastack
  621. * \lparam A string indicating if we are using system or session bus.
  622. * \lparam A string with the name of the match rule.
  623. */
  624. static int
  625. luaA_dbus_add_match(lua_State *L)
  626. {
  627. const char *bus = luaL_checkstring(L, 1);
  628. const char *name = luaL_checkstring(L, 2);
  629. DBusConnection *dbus_connection = a_dbus_bus_getbyname(bus);
  630. if(dbus_connection)
  631. {
  632. dbus_bus_add_match(dbus_connection, name, NULL);
  633. dbus_connection_flush(dbus_connection);
  634. }
  635. return 0;
  636. }
  637. /** Remove a previously added match rule "by value"
  638. * (the most recently-added identical rule gets removed).
  639. * \param L The Lua VM state.
  640. * \return The number of elements pushed on stack.
  641. * \luastack
  642. * \lparam A string indicating if we are using system or session bus.
  643. * \lparam A string with the name of the match rule.
  644. */
  645. static int
  646. luaA_dbus_remove_match(lua_State *L)
  647. {
  648. const char *bus = luaL_checkstring(L, 1);
  649. const char *name = luaL_checkstring(L, 2);
  650. DBusConnection *dbus_connection = a_dbus_bus_getbyname(bus);
  651. if(dbus_connection)
  652. {
  653. dbus_bus_remove_match(dbus_connection, name, NULL);
  654. dbus_connection_flush(dbus_connection);
  655. }
  656. return 0;
  657. }
  658. /** Add a signal receiver on the D-Bus.
  659. * \param L The Lua VM state.
  660. * \return The number of elements pushed on stack.
  661. * \luastack
  662. * \lparam A string with the interface name.
  663. * \lparam The function to call.
  664. */
  665. static int
  666. luaA_dbus_connect_signal(lua_State *L)
  667. {
  668. const char *name = luaL_checkstring(L, 1);
  669. luaA_checkfunction(L, 2);
  670. signal_t *sig = signal_array_getbyid(&dbus_signals,
  671. a_strhash((const unsigned char *) name));
  672. if(sig)
  673. luaA_warn(L, "cannot add signal %s on D-Bus, already existing", name);
  674. else
  675. {
  676. signal_add(&dbus_signals, name);
  677. signal_connect(&dbus_signals, name, luaA_object_ref(L, 2));
  678. }
  679. return 0;
  680. }
  681. /** Add a signal receiver on the D-Bus.
  682. * \param L The Lua VM state.
  683. * \return The number of elements pushed on stack.
  684. * \luastack
  685. * \lparam A string with the interface name.
  686. * \lparam The function to call.
  687. */
  688. static int
  689. luaA_dbus_disconnect_signal(lua_State *L)
  690. {
  691. const char *name = luaL_checkstring(L, 1);
  692. luaA_checkfunction(L, 2);
  693. const void *func = lua_topointer(L, 2);
  694. signal_disconnect(&dbus_signals, name, func);
  695. luaA_object_unref(L, func);
  696. return 0;
  697. }
  698. /** Emit a signal on the D-Bus.
  699. * \param L The Lua VM state.
  700. * \return The number of elements pushed on stack.
  701. * \luastack
  702. * \lparam A string indicating if we are using system or session bus.
  703. * \lparam A string with the dbus path.
  704. * \lparam A string with the dbus interface.
  705. * \lparam A string with the dbus method name.
  706. * \lparam type of 1st arg
  707. * \lparam 1st arg value
  708. * \lparam type of 2nd arg
  709. * \lparam 2nd arg value
  710. * ... etc
  711. */
  712. static int
  713. luaA_dbus_emit_signal(lua_State *L)
  714. {
  715. const char *bus_name = luaL_checkstring(L, 1);
  716. const char *path = luaL_checkstring(L, 2);
  717. const char *itface = luaL_checkstring(L, 3);
  718. const char *name = luaL_checkstring(L, 4);
  719. DBusConnection *dbus_connection = a_dbus_bus_getbyname(bus_name);
  720. DBusMessage* msg = dbus_message_new_signal(path, itface, name);
  721. DBusMessageIter iter;
  722. dbus_message_iter_init_append(msg, &iter);
  723. int top = lua_gettop(L);
  724. int nargs = top - 4;
  725. if(nargs % 2 != 0)
  726. {
  727. luaA_warn(L, "your D-Bus signal emiting method has wrong number of arguments");
  728. dbus_message_unref(msg);
  729. lua_pushboolean(L, 0);
  730. return 1;
  731. }
  732. for(int i = 5; i < top; i += 2) {
  733. if(!a_dbus_convert_value(L, i, &iter))
  734. {
  735. luaA_warn(L, "your D-Bus signal emitting method has bad argument type");
  736. dbus_message_unref(msg);
  737. lua_pushboolean(L, 0);
  738. return 1;
  739. }
  740. }
  741. dbus_connection_send(dbus_connection, msg, NULL);
  742. dbus_message_unref(msg);
  743. lua_pushboolean(L, 1);
  744. return 1;
  745. }
  746. const struct luaL_Reg awesome_dbus_lib[] =
  747. {
  748. { "request_name", luaA_dbus_request_name },
  749. { "release_name", luaA_dbus_release_name },
  750. { "add_match", luaA_dbus_add_match },
  751. { "remove_match", luaA_dbus_remove_match },
  752. { "connect_signal", luaA_dbus_connect_signal },
  753. { "disconnect_signal", luaA_dbus_disconnect_signal },
  754. { "emit_signal", luaA_dbus_emit_signal },
  755. { "__index", luaA_default_index },
  756. { "__newindex", luaA_default_newindex },
  757. { NULL, NULL }
  758. };
  759. #else /* WITH_DBUS */
  760. /** Empty stub if dbus is not enabled */
  761. void
  762. a_dbus_init(void)
  763. {
  764. }
  765. /** Empty stub if dbus is not enabled */
  766. void
  767. a_dbus_cleanup(void)
  768. {
  769. }
  770. #endif
  771. // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80