forked from awesomeWM/awesome
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbus.c
More file actions
527 lines (456 loc) · 15 KB
/
Copy pathdbus.c
File metadata and controls
527 lines (456 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*
* dbus.c - awesome dbus support
*
* Copyright © 2008 Julien Danjou <julien@danjou.info>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "config.h"
#include "dbus.h"
#include "client.h"
#ifdef WITH_DBUS
#include <ev.h>
#include <dbus/dbus.h>
#include <unistd.h>
#include <fcntl.h>
#include "widget.h"
extern awesome_t globalconf;
static DBusError err;
static DBusConnection *dbus_connection = NULL;
ev_io dbusio = { .fd = -1 };
static int
a_dbus_message_iter(DBusMessageIter *iter)
{
int nargs = 0;
do
{
switch(dbus_message_iter_get_arg_type(iter))
{
case DBUS_TYPE_INVALID:
break;
case DBUS_TYPE_VARIANT:
{
DBusMessageIter subiter;
dbus_message_iter_recurse(iter, &subiter);
a_dbus_message_iter(&subiter);
}
nargs++;
break;
case DBUS_TYPE_DICT_ENTRY:
{
DBusMessageIter subiter;
/* initialize a sub iterator */
dbus_message_iter_recurse(iter, &subiter);
/* create a new table to store the dict */
a_dbus_message_iter(&subiter);
}
nargs++;
break;
case DBUS_TYPE_ARRAY:
{
int array_type = dbus_message_iter_get_element_type(iter);
/* create a new table to store all the value */
lua_newtable(globalconf.L);
if(dbus_type_is_fixed(array_type))
switch(array_type)
{
int datalen;
#define DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(type, dbustype) \
case dbustype: \
{ \
type *data; \
dbus_message_iter_get_fixed_array(iter, &data, &datalen); \
for(int i = 0; i < datalen; i++) \
{ \
lua_pushnumber(globalconf.L, data[i]); \
lua_rawseti(globalconf.L, -2, i + 1); \
} \
} \
break;
DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(int16_t, DBUS_TYPE_INT16)
DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(uint16_t, DBUS_TYPE_UINT16)
DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(int32_t, DBUS_TYPE_INT32)
DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(uint32_t, DBUS_TYPE_UINT32)
DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(int64_t, DBUS_TYPE_INT64)
DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(uint64_t, DBUS_TYPE_UINT64)
#undef DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER
}
else if(array_type == DBUS_TYPE_DICT_ENTRY)
{
DBusMessageIter subiter;
/* initialize a sub iterator */
dbus_message_iter_recurse(iter, &subiter);
/* get the keys and the values
* n is the number of entry in * dict */
int n = a_dbus_message_iter(&subiter);
for(int i = 0; i < n; i ++)
lua_rawset(globalconf.L, - (n * 2) - 1 + i * 2);
}
else
{
DBusMessageIter subiter;
/* prepare to dig into the array*/
dbus_message_iter_recurse(iter, &subiter);
/* now iterate over every element of the array */
int n = a_dbus_message_iter(&subiter);
for(int i = n; i > 0; i--)
{
lua_rawseti(globalconf.L, - i - 1, i);
}
}
}
nargs++;
break;
case DBUS_TYPE_BOOLEAN:
{
bool b;
dbus_message_iter_get_basic(iter, &b);
lua_pushboolean(globalconf.L, b);
}
nargs++;
break;
case DBUS_TYPE_BYTE:
{
char c;
dbus_message_iter_get_basic(iter, &c);
lua_pushlstring(globalconf.L, &c, 1);
}
nargs++;
break;
#define DBUS_MSG_HANDLE_TYPE_NUMBER(type, dbustype) \
case dbustype: \
{ \
type ui; \
dbus_message_iter_get_basic(iter, &ui); \
lua_pushnumber(globalconf.L, ui); \
} \
nargs++; \
break;
DBUS_MSG_HANDLE_TYPE_NUMBER(int16_t, DBUS_TYPE_INT16)
DBUS_MSG_HANDLE_TYPE_NUMBER(uint16_t, DBUS_TYPE_UINT16)
DBUS_MSG_HANDLE_TYPE_NUMBER(int32_t, DBUS_TYPE_INT32)
DBUS_MSG_HANDLE_TYPE_NUMBER(uint32_t, DBUS_TYPE_UINT32)
DBUS_MSG_HANDLE_TYPE_NUMBER(int64_t, DBUS_TYPE_INT64)
DBUS_MSG_HANDLE_TYPE_NUMBER(uint64_t, DBUS_TYPE_UINT64)
#undef DBUS_MSG_HANDLE_TYPE_NUMBER
case DBUS_TYPE_STRING:
{
char *s;
dbus_message_iter_get_basic(iter, &s);
lua_pushstring(globalconf.L, s);
}
nargs++;
break;
}
} while(dbus_message_iter_next(iter));
return nargs;
}
static void
a_dbus_process_request(DBusMessage *msg)
{
if(globalconf.hooks.dbus == LUA_REFNIL)
return;
lua_newtable(globalconf.L);
switch(dbus_message_get_type(msg))
{
case DBUS_MESSAGE_TYPE_SIGNAL:
lua_pushliteral(globalconf.L, "signal");
break;
case DBUS_MESSAGE_TYPE_METHOD_CALL:
lua_pushliteral(globalconf.L, "method_call");
break;
case DBUS_MESSAGE_TYPE_METHOD_RETURN:
lua_pushliteral(globalconf.L, "method_return");
break;
case DBUS_MESSAGE_TYPE_ERROR:
lua_pushliteral(globalconf.L, "error");
break;
default:
lua_pushliteral(globalconf.L, "unknown");
break;
}
lua_setfield(globalconf.L, -2, "type");
const char *s = dbus_message_get_interface(msg);
lua_pushstring(globalconf.L, NONULL(s));
lua_setfield(globalconf.L, -2, "interface");
s = dbus_message_get_path(msg);
lua_pushstring(globalconf.L, NONULL(s));
lua_setfield(globalconf.L, -2, "path");
s = dbus_message_get_member(msg);
lua_pushstring(globalconf.L, NONULL(s));
lua_setfield(globalconf.L, -2, "member");
/* + 1 for the table above */
DBusMessageIter iter;
int nargs = 1;
if(dbus_message_iter_init(msg, &iter))
nargs += a_dbus_message_iter(&iter);
if(dbus_message_get_no_reply(msg))
luaA_dofunction(globalconf.L, globalconf.hooks.dbus, nargs, 0);
else
{
int n = lua_gettop(globalconf.L) - nargs;
luaA_dofunction(globalconf.L, globalconf.hooks.dbus, nargs, LUA_MULTRET);
n -= lua_gettop(globalconf.L);
DBusMessage *reply = dbus_message_new_method_return(msg);
dbus_message_iter_init_append(reply, &iter);
/* i is negative */
for(int i = n; i < 0; i += 2)
{
/* i is the type name, i+1 the value */
size_t len;
const char *type = lua_tolstring(globalconf.L, i, &len);
if(!type || len != 1)
break;
switch(*type)
{
case DBUS_TYPE_BOOLEAN:
{
bool b = lua_toboolean(globalconf.L, i + 1);
dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b);
}
break;
#define DBUS_MSG_RETURN_HANDLE_TYPE_STRING(dbustype) \
case dbustype: \
if((s = lua_tostring(globalconf.L, i + 1))) \
dbus_message_iter_append_basic(&iter, dbustype, &s); \
break;
DBUS_MSG_RETURN_HANDLE_TYPE_STRING(DBUS_TYPE_STRING)
DBUS_MSG_RETURN_HANDLE_TYPE_STRING(DBUS_TYPE_BYTE)
#undef DBUS_MSG_RETURN_HANDLE_TYPE_STRING
#define DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(type, dbustype) \
case dbustype: \
{ \
type num = lua_tonumber(globalconf.L, i + 1); \
dbus_message_iter_append_basic(&iter, dbustype, &num); \
} \
break;
DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(int16_t, DBUS_TYPE_INT16)
DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(uint16_t, DBUS_TYPE_UINT16)
DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(int32_t, DBUS_TYPE_INT32)
DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(uint32_t, DBUS_TYPE_UINT32)
DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(int64_t, DBUS_TYPE_INT64)
DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(uint64_t, DBUS_TYPE_UINT64)
DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(double, DBUS_TYPE_DOUBLE)
#undef DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER
}
lua_remove(globalconf.L, i);
lua_remove(globalconf.L, i + 1);
}
dbus_connection_send(dbus_connection, reply, NULL);
dbus_message_unref(reply);
}
}
static void
a_dbus_process_requests(EV_P_ ev_io *w, int revents)
{
DBusMessage *msg;
int nmsg = 0;
if(!dbus_connection && !a_dbus_init())
return;
while(true)
{
dbus_connection_read_write(dbus_connection, 0);
if(!(msg = dbus_connection_pop_message(dbus_connection)))
break;
if(dbus_message_is_signal(msg, DBUS_INTERFACE_LOCAL, "Disconnected"))
{
a_dbus_cleanup();
dbus_message_unref(msg);
return;
}
else
a_dbus_process_request(msg);
dbus_message_unref(msg);
nmsg++;
}
if(nmsg)
dbus_connection_flush(dbus_connection);
}
static bool
a_dbus_request_name(const char *name)
{
int ret = dbus_bus_request_name(dbus_connection, name, 0, &err);
if(dbus_error_is_set(&err))
{
warn("failed to request D-Bus name: %s", err.message);
return false;
}
switch(ret)
{
case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
return true;
case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
warn("already primary D-Bus name owner for %s", name);
return true;
}
return false;
}
static bool
a_dbus_release_name(const char *name)
{
int ret = dbus_bus_release_name(dbus_connection, name, &err);
if(dbus_error_is_set(&err))
{
warn("failed to release D-Bus name: %s", err.message);
return false;
}
switch(ret)
{
case DBUS_RELEASE_NAME_REPLY_NOT_OWNER:
warn("not primary D-Bus name owner for %s", name);
return false;
case DBUS_RELEASE_NAME_REPLY_NON_EXISTENT:
warn("non existent D-Bus name: %s", name);
return false;
}
return true;
}
bool
a_dbus_init(void)
{
int fd;
dbus_error_init(&err);
dbus_connection = dbus_bus_get(DBUS_BUS_SESSION, &err);
if(dbus_error_is_set(&err))
{
warn("D-Bus system bus connection failed: %s", err.message);
dbus_connection = NULL;
dbus_error_free(&err);
return false;
}
dbus_connection_set_exit_on_disconnect(dbus_connection, FALSE);
a_dbus_request_name("org.awesome");
if(!dbus_connection_get_unix_fd(dbus_connection, &fd))
{
warn("cannot get D-Bus connection file descriptor");
a_dbus_cleanup();
return false;
}
fcntl(fd, F_SETFD, FD_CLOEXEC);
ev_io_init(&dbusio, a_dbus_process_requests, fd, EV_READ);
ev_io_start(EV_DEFAULT_UC_ &dbusio);
ev_unref(EV_DEFAULT_UC);
return true;
}
void
a_dbus_cleanup(void)
{
if(!dbus_connection)
return;
dbus_error_free(&err);
if (dbusio.fd >= 0) {
ev_ref(EV_DEFAULT_UC);
ev_io_stop(EV_DEFAULT_UC_ &dbusio);
dbusio.fd = -1;
}
/* This is a shared connection owned by libdbus
* Do not close it, only unref
*/
dbus_connection_unref(dbus_connection);
dbus_connection = NULL;
}
/** Register a D-Bus name to receive message from.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
* \luastack
* \lparam A string with the name of the D-Bus name to register. Note that
* org.awesome is registered by default.
* \lreturn True if everything worked fine, false otherwise.
*/
static int
luaA_dbus_request_name(lua_State *L)
{
const char *name = luaL_checkstring(L, 1);
lua_pushboolean(L, a_dbus_request_name(name));
return 1;
}
/** Release a D-Bus name.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
* \luastack
* \lparam A string with the name of the D-Bus name to unregister.
* \lreturn True if everything worked fine, false otherwise.
*/
static int
luaA_dbus_release_name(lua_State *L)
{
const char *name = luaL_checkstring(L, 1);
lua_pushboolean(L, a_dbus_release_name(name));
return 1;
}
/** Add a match rule to match messages going through the message bus.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
* \luastack
* \lparam A string with the name of the match rule.
*/
static int
luaA_dbus_add_match(lua_State *L)
{
const char *name = luaL_checkstring(L, 1);
dbus_bus_add_match(dbus_connection, name, NULL);
dbus_connection_flush(dbus_connection);
return 0;
}
/** Remove a previously added match rule "by value"
* (the most recently-added identical rule gets removed).
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
* \luastack
* \lparam A string with the name of the match rule.
*/
static int
luaA_dbus_remove_match(lua_State *L)
{
const char *name = luaL_checkstring(L, 1);
dbus_bus_remove_match(dbus_connection, name, NULL);
dbus_connection_flush(dbus_connection);
return 0;
}
const struct luaL_reg awesome_dbus_lib[] =
{
{ "request_name", luaA_dbus_request_name },
{ "release_name", luaA_dbus_release_name },
{ "add_match", luaA_dbus_add_match },
{ "remove_match", luaA_dbus_remove_match },
{ NULL, NULL }
};
#else /* HAVE_DBUS */
bool
a_dbus_init(void)
{
return false;
}
void
a_dbus_cleanup(void)
{
/* empty */
}
static int
luaA_donothing(lua_State *L)
{
return 0;
}
const struct luaL_reg awesome_dbus_lib[] =
{
{ "request_name", luaA_donothing },
{ "release_name", luaA_donothing },
{ "add_match", luaA_donothing },
{ "remove_match", luaA_donothing },
{ NULL, NULL }
};
#endif
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80