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.
26 #include <dbus/dbus.h>
32 extern awesome_t globalconf
;
35 static DBusConnection
*dbus_connection
= NULL
;
36 ev_io dbusio
= { .fd
= -1 };
38 /** Check a dbus object path format and its number of element.
39 * \param path The path.
40 * \param nelem The number of element it should have.
41 * \return true if the path is ok, false otherwise.
44 a_dbus_path_check(char **path
, int nelem
)
48 for(i
= 0; path
[i
]; i
++);
51 return (!a_strcmp(path
[0], "org") && !a_strcmp(path
[1], "awesome"));
55 a_dbus_process_request_do(DBusMessage
*msg
)
61 if(!dbus_message_get_path_decomposed(msg
, &path
))
66 if(!a_dbus_path_check(path
, 2))
69 if(!dbus_message_iter_init(msg
, &iter
))
71 dbus_error_free(&err
);
74 else if(DBUS_TYPE_STRING
!= dbus_message_iter_get_arg_type(&iter
))
76 dbus_error_free(&err
);
80 dbus_message_iter_get_basic(&iter
, &cmd
);
82 luaA_dostring(globalconf
.L
, cmd
);
85 for(i
= 0; path
[i
]; i
++)
91 a_dbus_process_requests(EV_P_ ev_io
*w
, int revents
)
96 if(!dbus_connection
&& !a_dbus_init())
101 dbus_connection_read_write(dbus_connection
, 0);
103 if(!(msg
= dbus_connection_pop_message(dbus_connection
)))
106 if(dbus_message_is_signal(msg
, DBUS_INTERFACE_LOCAL
, "Disconnected"))
109 dbus_message_unref(msg
);
112 else if(dbus_message_is_method_call(msg
, "org.awesome", "do"))
113 a_dbus_process_request_do(msg
);
115 dbus_message_unref(msg
);
121 dbus_connection_flush(dbus_connection
);
130 dbus_error_init(&err
);
132 dbus_connection
= dbus_bus_get(DBUS_BUS_SESSION
, &err
);
133 if(dbus_error_is_set(&err
))
135 warn("DBus system bus connection failed: %s", err
.message
);
136 dbus_connection
= NULL
;
137 dbus_error_free(&err
);
141 dbus_connection_set_exit_on_disconnect(dbus_connection
, FALSE
);
143 ret
= dbus_bus_request_name(dbus_connection
, "org.awesome", 0, &err
);
145 if(dbus_error_is_set(&err
))
147 warn("failed to request DBus name: %s", err
.message
);
152 if(ret
!= DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
)
154 warn("not primary DBus name owner");
159 if(!dbus_connection_get_unix_fd(dbus_connection
, &fd
))
161 warn("cannot get DBus connection file descriptor");
166 ev_io_init(&dbusio
, a_dbus_process_requests
, fd
, EV_READ
);
167 ev_io_start(EV_DEFAULT_UC_
&dbusio
);
168 ev_unref(EV_DEFAULT_UC
);
178 dbus_error_free(&err
);
180 if (dbusio
.fd
>= 0) {
181 ev_ref(EV_DEFAULT_UC
);
182 ev_io_stop(EV_DEFAULT_UC_
&dbusio
);
186 /* This is a shared connection owned by libdbus
187 * Do not close it, only unref
189 dbus_connection_unref(dbus_connection
);
190 dbus_connection
= NULL
;
193 #else /* HAVE_DBUS */
210 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80