Finish code to connect to IBUS daemon

This commit is contained in:
Kovid Goyal
2018-07-09 09:22:36 +05:30
parent 5b7c697dfe
commit dca81589a7
4 changed files with 173 additions and 19 deletions

97
glfw/dbus_glfw.c vendored
View File

@@ -29,13 +29,17 @@
#include "dbus_glfw.h"
static inline void
report_error(DBusError *err, const char *msg) {
const char *prefix = msg ? msg : "DBUS error occurred";
_glfwInputError(GLFW_PLATFORM_ERROR, "%s: %s", prefix, err->message);
report_error(DBusError *err, const char *fmt, ...) {
static char buf[1024];
va_list args;
va_start(args, fmt);
int n = vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
snprintf(buf + n, sizeof(buf), ". DBUS error: %s", err->message);
_glfwInputError(GLFW_PLATFORM_ERROR, "%s", buf);
dbus_error_free(err);
}
GLFWbool
glfw_dbus_init(_GLFWDBUSData *dbus) {
DBusError err;
@@ -43,7 +47,7 @@ glfw_dbus_init(_GLFWDBUSData *dbus) {
dbus_error_init(&err);
dbus->session_conn = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
if (dbus_error_is_set(&err)) {
report_error(&err, "Failed to connect to DBUS system bus");
report_error(&err, "Failed to connect to DBUS session bus");
return GLFW_FALSE;
}
}
@@ -57,7 +61,15 @@ glfw_dbus_connect_to(const char *path, const char* err_msg) {
DBusConnection *ans = dbus_connection_open_private(path, &err);
if (!ans) {
report_error(&err, err_msg);
return NULL;
}
dbus_error_free(&err);
dbus_connection_flush(ans);
if (!dbus_bus_register(ans, &err)) {
report_error(&err, err_msg);
return NULL;
}
dbus_connection_flush(ans);
return ans;
}
@@ -75,3 +87,78 @@ glfw_dbus_close_connection(DBusConnection *conn) {
dbus_connection_close(conn);
dbus_connection_unref(conn);
}
static GLFWbool
call_void_method(DBusConnection *conn, const char *node, const char *path, const char *interface, const char *method, va_list ap) {
GLFWbool retval = GLFW_FALSE;
if (conn) {
DBusMessage *msg = dbus_message_new_method_call(node, path, interface, method);
if (msg) {
int firstarg = va_arg(ap, int);
if ((firstarg == DBUS_TYPE_INVALID) || dbus_message_append_args_valist(msg, firstarg, ap)) {
if (dbus_connection_send(conn, msg, NULL)) {
dbus_connection_flush(conn);
retval = GLFW_TRUE;
}
}
dbus_message_unref(msg);
}
}
return retval;
}
GLFWbool
glfw_dbus_call_void_method(DBusConnection *conn, const char *node, const char *path, const char *interface, const char *method, ...) {
GLFWbool retval;
va_list ap;
va_start(ap, method);
retval = call_void_method(conn, node, path, interface, method, ap);
va_end(ap);
return retval;
}
static GLFWbool
call_method(DBusConnection *conn, const char *node, const char *path, const char *interface, const char *method, va_list ap) {
GLFWbool retval = GLFW_FALSE;
if (conn) {
DBusMessage *msg = dbus_message_new_method_call(node, path, interface, method);
if (msg) {
int firstarg = va_arg(ap, int);
if ((firstarg == DBUS_TYPE_INVALID) || dbus_message_append_args_valist(msg, firstarg, ap)) {
DBusError err;
dbus_error_init(&err);
DBusMessage *reply = dbus_connection_send_with_reply_and_block(conn, msg, 300, &err);
if (reply) {
firstarg = va_arg(ap, int);
dbus_error_free(&err);
if ((firstarg == DBUS_TYPE_INVALID) || dbus_message_get_args_valist(reply, &err, firstarg, ap)) {
retval = GLFW_TRUE;
} else {
report_error(&err, "Failed to get reply args from DBUS method: %s on node: %s and interface: %s", method, node, interface);
}
dbus_message_unref(reply);
} else {
report_error(&err, "Failed to call DBUS method: %s on node: %s and interface: %s", method, node, interface);
}
}
dbus_message_unref(msg);
}
}
return retval;
}
GLFWbool
glfw_dbus_call_method(DBusConnection *conn, const char *node, const char *path, const char *interface, const char *method, ...) {
GLFWbool retval;
va_list ap;
va_start(ap, method);
retval = call_method(conn, node, path, interface, method, ap);
va_end(ap);
return retval;
}