Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type checking for g_variant_get calls #44

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 88 additions & 60 deletions dbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
extern int set_screen_xbacklight_value (int backlight);

GDBusConnection *get_dbus_message_bus (int bus_type) {

GDBusConnection *connection;
GError *error = NULL;

connection = g_bus_get_sync (bus_type, NULL, &error);
if (error) {
g_warning ("Failed to get session bus: %s", error->message);
Expand All @@ -51,37 +51,37 @@ GDBusConnection *get_dbus_message_bus (int bus_type) {
}

return connection;

}

int get_screensaver_active() {

GDBusMessage *message, *reply;
GDBusConnection *connection;
GError *error;
GVariant *body;
GVariant *body;
gint32 value;

connection = get_dbus_message_bus (G_BUS_TYPE_SESSION);

message = g_dbus_message_new_method_call (
GS_DBUS_SERVICE,
GS_DBUS_PATH,
GS_DBUS_INTERFACE,
"GetActive");
"GetActive");

if (message == NULL) {
g_warning ("Failed to allocate the dbus message");
return -1;
}

g_dbus_message_set_body (
message,
message,
g_variant_new ("(b)", TRUE)
);
);

error = NULL;

reply = g_dbus_connection_send_message_with_reply_sync (
connection,
message,
Expand All @@ -100,10 +100,17 @@ int get_screensaver_active() {
g_warning ("unable to flush message queue: %s", error->message);
g_clear_error (&error);
}

body = g_dbus_message_get_body (reply);

if (!g_variant_check_format_string(body, "(&s)", FALSE))
{
g_warning ("variant return type is unexpected");
return -1;
}

g_variant_get (body, "(&s)", &value);

g_object_unref (connection);
g_object_unref (message);
g_object_unref (reply);
Expand All @@ -117,27 +124,27 @@ int set_keyboard_brightness_value (int brightness) {
GDBusMessage *message, *reply;
GDBusConnection *connection;
GError *error;

connection = get_dbus_message_bus (G_BUS_TYPE_SYSTEM);

message = g_dbus_message_new_method_call (
UP_DBUS_SERVICE,
UP_DBUS_PATH,
UP_DBUS_INTERFACE,
"SetBrightness");
"SetBrightness");

if (message == NULL) {
g_warning ("Failed to allocate the dbus message");
return -1;
}

g_dbus_message_set_body (
message,
message,
g_variant_new ("(i)", brightness)
);
);

error = NULL;

reply = g_dbus_connection_send_message_with_reply_sync (
connection,
message,
Expand All @@ -156,7 +163,7 @@ int set_keyboard_brightness_value (int brightness) {
g_warning ("unable to flush message queue: %s", error->message);
g_clear_error (&error);
}

g_object_unref (reply);
g_object_unref (connection);
g_object_unref (message);
Expand All @@ -169,29 +176,29 @@ int dbus_get_screen_backlight_value() {
GDBusMessage *message, *reply;
GDBusConnection *connection;
GError *error;
GVariant *body;
GVariant *body;
gint32 value;

connection = get_dbus_message_bus(G_BUS_TYPE_SESSION);

message = g_dbus_message_new_method_call (
SD_DBUS_SERVICE,
SD_DBUS_PATH,
SD_DBUS_INTERFACE,
"GetPercentage");
"GetPercentage");

if (message == NULL) {
g_warning ("Failed to allocate the dbus message");
return -1;
}

g_warning ("before message_get_body");
g_dbus_message_set_body (
message,
g_variant_new ("(y)", NULL));
message,
g_variant_new ("(y)", NULL));

error = NULL;

reply = g_dbus_connection_send_message_with_reply_sync (
connection,
message,
Expand All @@ -210,10 +217,17 @@ int dbus_get_screen_backlight_value() {
g_warning ("unable to flush message queue: %s", error->message);
g_clear_error (&error);
}

body = g_dbus_message_get_body (reply);

if (!g_variant_check_format_string(body, "(u)", FALSE))
{
g_warning ("variant return type is unexpected");
return -1;
}

g_variant_get (body, "(u)", &value);

g_object_unref (connection);
g_object_unref (message);
g_object_unref (reply);
Expand All @@ -227,28 +241,28 @@ int dbus_set_screen_backlight_value_gnome (int backlight) {
GDBusMessage *message, *reply;
GDBusConnection *connection;
GError *error;
GVariant *body;
GVariant *body;
gint32 value;

connection = get_dbus_message_bus (G_BUS_TYPE_SESSION);

message = g_dbus_message_new_method_call (
SD_DBUS_SERVICE,
SD_DBUS_PATH,
SD_DBUS_INTERFACE,
"SetPercentage");
"SetPercentage");

if (message == NULL) {
g_warning ("Failed to allocate the dbus message");
return -1;
}

g_dbus_message_set_body (
message,
g_variant_new ("(u)", backlight));
message,
g_variant_new ("(u)", backlight));

error = NULL;

reply = g_dbus_connection_send_message_with_reply_sync (
connection,
message,
Expand All @@ -267,44 +281,51 @@ int dbus_set_screen_backlight_value_gnome (int backlight) {
g_warning ("unable to flush message queue: %s", error->message);
g_clear_error (&error);
}

body = g_dbus_message_get_body (reply);

if (!g_variant_check_format_string(body, "(u)", FALSE))
{
g_warning ("variant return type is unexpected");
return -1;
}

g_variant_get (body, "(u)", &value);

g_object_unref (reply);
g_object_unref (connection);
g_object_unref (message);

return value;
return value;
}

int dbus_set_screen_backlight_value_kde (int backlight) {

GDBusMessage *message, *reply;
GDBusConnection *connection;
GError *error;
GVariant *body;
GVariant *body;
gint32 value;

connection = get_dbus_message_bus (G_BUS_TYPE_SESSION);

message = g_dbus_message_new_method_call (
KDE_DBUS_SERVICE,
KDE_DBUS_PATH,
KDE_DBUS_INTERFACE,
"SetBrightness");
"SetBrightness");

if (message == NULL) {
g_warning ("Failed to allocate the dbus message");
return -1;
}

g_dbus_message_set_body (
message,
g_variant_new ("(u)", backlight));
message,
g_variant_new ("(u)", backlight));

error = NULL;

reply = g_dbus_connection_send_message_with_reply_sync (
connection,
message,
Expand All @@ -323,27 +344,34 @@ int dbus_set_screen_backlight_value_kde (int backlight) {
g_warning ("unable to flush message queue: %s", error->message);
g_clear_error (&error);
}

body = g_dbus_message_get_body (reply);

if (!g_variant_check_format_string(body, "(u)", FALSE))
{
g_warning ("variant return type is unexpected");
return -1;
}

g_variant_get (body, "(u)", &value);

g_object_unref (reply);
g_object_unref (connection);
g_object_unref (message);

return value;
return value;

}


int dbus_set_screen_backlight_value (int backlight, int backend) {

int ret=-1;
int ret=-1;

if (backend == 0) ret = dbus_set_screen_backlight_value_gnome(backlight);
if (backend == 1) ret = dbus_set_screen_backlight_value_kde(backlight);
if (backend == 2) ret = set_screen_xbacklight_value(backlight);

if (backend == 0) ret = dbus_set_screen_backlight_value_gnome(backlight);
if (backend == 1) ret = dbus_set_screen_backlight_value_kde(backlight);
if (backend == 2) ret = set_screen_xbacklight_value(backlight);
return ret;

return ret;

}