-
Notifications
You must be signed in to change notification settings - Fork 12
/
setup-gnome.cpp
151 lines (129 loc) · 6.62 KB
/
setup-gnome.cpp
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
// Copyright (c) 2020 TUXEDO Computers GmbH <tux@tuxedocomputers.com>
//
// This file is part of TUXEDO Touchpad Switch.
//
// This file is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// TUXEDO Touchpad Switch is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with TUXEDO Touchpad Switch. If not, see <https://www.gnu.org/licenses/>.
#include "setup-gnome.h"
#include <iostream>
#include <sys/file.h>
#include <gio/gio.h>
#include "touchpad-control.h"
using std::cerr;
using std::endl;
static int lockfile;
static GSettings *touchpad_settings = NULL;
static GDBusProxy *session_manager_properties = NULL;
static GDBusProxy *display_config_properties = NULL;
static void send_events_handler(GSettings *settings, const char* key, __attribute__((unused)) gpointer user_data) {
const gchar *send_events_string = g_settings_get_string(settings, key);
if (!send_events_string) {
cerr << "send_events_handler(...): g_settings_get_string(...) failed." << endl;
return;
}
int enabled = 0;
if (send_events_string[0] == 'e') {
enabled = 1;
}
if (set_touchpad_state(enabled)) {
cerr << "send_events_handler(...): set_touchpad_state(...) failed." << endl;
}
}
static void session_manager_properties_changed_handler(__attribute__((unused)) GDBusProxy *proxy, GVariant *changed_properties, __attribute__((unused)) GStrv invalidated_properties, gpointer user_data) {
if (g_variant_is_of_type(changed_properties, G_VARIANT_TYPE_VARDICT)) {
GVariantDict changed_properties_dict;
gboolean sessionIsActive;
g_variant_dict_init (&changed_properties_dict, changed_properties);
if (g_variant_dict_lookup (&changed_properties_dict, "SessionIsActive", "b", &sessionIsActive)) {
if (sessionIsActive) {
if (flock(lockfile, LOCK_EX)) {
cerr << "properties_changed_handler(...): flock(...) failed." << endl;
}
send_events_handler((GSettings *)user_data, "send-events", NULL);
}
else {
if (set_touchpad_state(1)) {
cerr << "properties_changed_handler(...): set_touchpad_state(...) failed." << endl;
}
if (flock(lockfile, LOCK_UN)) {
cerr << "properties_changed_handler(...): flock(...) failed." << endl;
}
}
}
}
}
static void display_config_properties_changed_handler(__attribute__((unused)) GDBusProxy *proxy, GVariant *changed_properties, __attribute__((unused)) GStrv invalidated_properties, gpointer user_data) {
if (g_variant_is_of_type(changed_properties, G_VARIANT_TYPE_VARDICT)) {
GVariantDict changed_properties_dict;
gint32 powerSaveMode;
g_variant_dict_init (&changed_properties_dict, changed_properties);
if (g_variant_dict_lookup (&changed_properties_dict, "PowerSaveMode", "i", &powerSaveMode)) {
if (powerSaveMode == 0) {
send_events_handler((GSettings *)user_data, "send-events", NULL);
}
}
}
}
int setup_gnome(int lockfile_arg) {
lockfile = lockfile_arg;
// get a new glib settings context to read the touchpad configuration of the current user
touchpad_settings = g_settings_new("org.gnome.desktop.peripherals.touchpad");
if (!touchpad_settings) {
cerr << "setup_gnome(...): g_settings_new(...) failed." << endl;
return EXIT_FAILURE;
}
// sync on config change
if (g_signal_connect(touchpad_settings, "changed::send-events", G_CALLBACK(send_events_handler), NULL) < 1) {
cerr << "setup_gnome(...): g_signal_connect(...) failed." << endl;
return EXIT_FAILURE;
}
// sync on xsession change
session_manager_properties = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION,
G_DBUS_PROXY_FLAGS_NONE, NULL,
"org.gnome.SessionManager",
"/org/gnome/SessionManager",
"org.gnome.SessionManager",
NULL, NULL);
if (session_manager_properties == NULL) {
cerr << "setup_gnome(...): g_dbus_proxy_new_for_bus_sync(...) failed." << endl;
return EXIT_FAILURE;
}
if (g_signal_connect(session_manager_properties, "g-properties-changed", G_CALLBACK(session_manager_properties_changed_handler), touchpad_settings) < 1) {
cerr << "setup_gnome(...): g_signal_connect(...) failed." << endl;
return EXIT_FAILURE;
}
// sync on wakeup
display_config_properties = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION,
G_DBUS_PROXY_FLAGS_NONE, NULL,
"org.gnome.Mutter.DisplayConfig",
"/org/gnome/Mutter/DisplayConfig",
"org.gnome.Mutter.DisplayConfig",
NULL, NULL);
if (display_config_properties == NULL) {
cerr << "setup_gnome(...): g_dbus_proxy_new_for_bus_sync(...) failed." << endl;
return EXIT_FAILURE;
}
if (g_signal_connect(display_config_properties, "g-properties-changed", G_CALLBACK(display_config_properties_changed_handler), touchpad_settings) < 1) {
cerr << "setup_gnome(...): g_signal_connect(...) failed." << endl;
return EXIT_FAILURE;
}
// sync on start
// also ensures that "send-events" setting is accessed at least once, which is required for the GSettings singal handling to be correctly initialize
send_events_handler(touchpad_settings, "send-events", NULL);
return EXIT_SUCCESS;
}
void clean_gnome() {
g_clear_object(&session_manager_properties);
g_clear_object(&display_config_properties);
g_clear_object(&touchpad_settings);
}