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>
34 extern awesome_t globalconf
;
37 static DBusConnection
*dbus_connection
= NULL
;
38 ev_io dbusio
= { .fd
= -1 };
40 /** Check a dbus object path format and its number of element.
41 * \param path The path.
42 * \param nelem The number of element it should have.
43 * \return true if the path is ok, false otherwise.
46 a_dbus_path_check(char **path
, int nelem
)
50 for(i
= 0; path
[i
]; i
++);
53 return (!a_strcmp(path
[0], "org") && !a_strcmp(path
[1], "awesome"));
57 a_dbus_process_request_do(DBusMessage
*msg
)
63 if(!dbus_message_get_path_decomposed(msg
, &path
))
68 if(!a_dbus_path_check(path
, 2))
71 if(!dbus_message_iter_init(msg
, &iter
))
73 dbus_error_free(&err
);
76 else if(DBUS_TYPE_STRING
!= dbus_message_iter_get_arg_type(&iter
))
78 dbus_error_free(&err
);
82 dbus_message_iter_get_basic(&iter
, &cmd
);
84 luaA_dostring(globalconf
.L
, cmd
);
87 for(i
= 0; path
[i
]; i
++)
93 a_dbus_process_requests(EV_P_ ev_io
*w
, int revents
)
98 if(!dbus_connection
&& !a_dbus_init())
103 dbus_connection_read_write(dbus_connection
, 0);
105 if(!(msg
= dbus_connection_pop_message(dbus_connection
)))
108 if(dbus_message_is_signal(msg
, DBUS_INTERFACE_LOCAL
, "Disconnected"))
111 dbus_message_unref(msg
);
114 else if(dbus_message_is_method_call(msg
, "org.awesome", "do"))
115 a_dbus_process_request_do(msg
);
117 dbus_message_unref(msg
);
123 dbus_connection_flush(dbus_connection
);
132 dbus_error_init(&err
);
134 dbus_connection
= dbus_bus_get(DBUS_BUS_SESSION
, &err
);
135 if(dbus_error_is_set(&err
))
137 warn("DBus system bus connection failed: %s", err
.message
);
138 dbus_connection
= NULL
;
139 dbus_error_free(&err
);
143 dbus_connection_set_exit_on_disconnect(dbus_connection
, FALSE
);
145 ret
= dbus_bus_request_name(dbus_connection
, "org.awesome", 0, &err
);
147 if(dbus_error_is_set(&err
))
149 warn("failed to request DBus name: %s", err
.message
);
154 if(ret
!= DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
)
156 warn("not primary DBus name owner");
161 if(!dbus_connection_get_unix_fd(dbus_connection
, &fd
))
163 warn("cannot get DBus connection file descriptor");
168 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
170 ev_io_init(&dbusio
, a_dbus_process_requests
, fd
, EV_READ
);
171 ev_io_start(EV_DEFAULT_UC_
&dbusio
);
172 ev_unref(EV_DEFAULT_UC
);
182 dbus_error_free(&err
);
184 if (dbusio
.fd
>= 0) {
185 ev_ref(EV_DEFAULT_UC
);
186 ev_io_stop(EV_DEFAULT_UC_
&dbusio
);
190 /* This is a shared connection owned by libdbus
191 * Do not close it, only unref
193 dbus_connection_unref(dbus_connection
);
194 dbus_connection
= NULL
;
197 #else /* HAVE_DBUS */
214 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80