summaryrefslogtreecommitdiff
path: root/libdecor/src/desktop-settings.c
blob: 28067ac3ecd9f960b3428911289fa670b205f563 (plain)
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
/*
 * Copyright © 2019 Christian Rauch
 * Copyright © 2024 Colin Kinloch
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "desktop-settings.h"
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include "config.h"

static bool
get_cursor_settings_from_env(char **theme, int *size)
{
	char *env_xtheme;
	char *env_xsize;

	env_xtheme = getenv("XCURSOR_THEME");
	if (env_xtheme != NULL)
		*theme = strdup(env_xtheme);

	env_xsize = getenv("XCURSOR_SIZE");
	if (env_xsize != NULL)
		*size = atoi(env_xsize);

	return env_xtheme != NULL && env_xsize != NULL;
}

#ifdef HAS_DBUS
#include <dbus/dbus.h>

static DBusMessage *
get_setting_sync(DBusConnection *const connection,
		 const char *key,
		 const char *value)
{
	DBusError error;
	dbus_bool_t success;
	DBusMessage *message;
	DBusMessage *reply;

	message = dbus_message_new_method_call(
		"org.freedesktop.portal.Desktop",
		"/org/freedesktop/portal/desktop",
		"org.freedesktop.portal.Settings",
		"Read");

	success = dbus_message_append_args(message,
		DBUS_TYPE_STRING, &key,
		DBUS_TYPE_STRING, &value,
		DBUS_TYPE_INVALID);

	if (!success)
		return NULL;

	dbus_error_init(&error);

	reply = dbus_connection_send_with_reply_and_block(
			     connection,
			     message,
			     DBUS_TIMEOUT_USE_DEFAULT,
			     &error);

	dbus_message_unref(message);

	if (dbus_error_is_set(&error)) {
		dbus_error_free(&error);
		return NULL;
	}

	dbus_error_free(&error);
	return reply;
}

static bool
parse_type(DBusMessage *const reply,
	   const int type,
	   void *value)
{
	DBusMessageIter iter[3];

	dbus_message_iter_init(reply, &iter[0]);
	if (dbus_message_iter_get_arg_type(&iter[0]) != DBUS_TYPE_VARIANT)
		return false;

	dbus_message_iter_recurse(&iter[0], &iter[1]);
	if (dbus_message_iter_get_arg_type(&iter[1]) != DBUS_TYPE_VARIANT)
		return false;

	dbus_message_iter_recurse(&iter[1], &iter[2]);
	if (dbus_message_iter_get_arg_type(&iter[2]) != type)
		return false;

	dbus_message_iter_get_basic(&iter[2], value);

	return true;
}

bool
libdecor_get_cursor_settings(char **theme, int *size)
{
	static const char name[] = "org.gnome.desktop.interface";
	static const char key_theme[] = "cursor-theme";
	static const char key_size[] = "cursor-size";

	DBusError error;
	DBusConnection *connection;
	DBusMessage *reply;
	const char *value_theme = NULL;

	dbus_error_init(&error);

	connection = dbus_bus_get(DBUS_BUS_SESSION, &error);

	if (dbus_error_is_set(&error))
		goto fallback;

	reply = get_setting_sync(connection, name, key_theme);
	if (!reply)
		goto fallback;

	if (!parse_type(reply, DBUS_TYPE_STRING, &value_theme)) {
		dbus_message_unref(reply);
		goto fallback;
	}

	*theme = strdup(value_theme);

	dbus_message_unref(reply);

	reply = get_setting_sync(connection, name, key_size);
	if (!reply)
		goto fallback;

	if (!parse_type(reply, DBUS_TYPE_INT32, size)) {
		dbus_message_unref(reply);
		goto fallback;
	}

	dbus_message_unref(reply);

	return true;

fallback:
	return get_cursor_settings_from_env(theme, size);
}

enum libdecor_color_scheme
libdecor_get_color_scheme()
{
	static const char name[] = "org.freedesktop.appearance";
	static const char key_color_scheme[] = "color-scheme";
	uint32_t color = 0;

	DBusError error;
	DBusConnection *connection;
	DBusMessage *reply;

	dbus_error_init(&error);

	connection = dbus_bus_get(DBUS_BUS_SESSION, &error);

	if (dbus_error_is_set(&error))
		return 0;

	reply = get_setting_sync(connection, name, key_color_scheme);
	if (!reply)
		return 0;

	if (!parse_type(reply, DBUS_TYPE_UINT32, &color)) {
		dbus_message_unref(reply);
		return 0;
	}

	dbus_message_unref(reply);

	return color;
}
#else
bool
libdecor_get_cursor_settings(char **theme, int *size)
{
	return get_cursor_settings_from_env(theme, size);
}

uint32_t
libdecor_get_color_scheme()
{
	return LIBDECOR_COLOR_SCHEME_DEFAULT;
}
#endif