-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtuxedo-touchpad-switch.cpp
More file actions
127 lines (107 loc) · 3.94 KB
/
Copy pathtuxedo-touchpad-switch.cpp
File metadata and controls
127 lines (107 loc) · 3.94 KB
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
// 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 <iostream>
#include <cstdlib>
#include <csignal>
#include <fcntl.h>
#include <unistd.h>
#include <sys/file.h>
#include <gio/gio.h>
#include "touchpad-control.h"
#include "setup-gnome.h"
using std::cout;
using std::cerr;
using std::endl;
static int lockfile = -1;
static void gracefull_exit(int signum = 0) {
int result = EXIT_SUCCESS;
clean_gnome();
if (signum < 0) {
result = EXIT_FAILURE;
}
if (set_touchpad_state(1) != EXIT_SUCCESS) {
cerr << "gracefull_exit(...): set_touchpad_state(...) failed." << endl;
result = EXIT_FAILURE;
}
if (lockfile >= 0) {
if (flock(lockfile, LOCK_UN)) {
cerr << "gracefull_exit(...): flock(...) failed." << endl;
result = EXIT_FAILURE;
}
if (close(lockfile)) {
cerr << "gracefull_exit(...): close(...) failed." << endl;
result = EXIT_FAILURE;
}
}
exit(result);
}
int main() {
struct sigaction sigaction_gracefull_exit;
sigaction_gracefull_exit.sa_handler = gracefull_exit;
sigemptyset(&sigaction_gracefull_exit.sa_mask);
sigaction_gracefull_exit.sa_flags = 0;
if (sigaction(SIGINT, &sigaction_gracefull_exit, nullptr)) {
cerr << "main(...): sigaction(...) failed." << endl;
gracefull_exit(-EXIT_FAILURE);
}
if (sigaction(SIGTERM, &sigaction_gracefull_exit, nullptr)) {
cerr << "main(...): sigaction(...) failed." << endl;
gracefull_exit(-EXIT_FAILURE);
}
if (sigaction(SIGHUP, &sigaction_gracefull_exit, nullptr)) {
cerr << "main(...): sigaction(...) failed." << endl;
gracefull_exit(-EXIT_FAILURE);
}
lockfile = open("/etc/tuxedo-touchpad-switch-lockfile", O_RDONLY);
if (lockfile == -1) {
cerr << "main(...): open(...) failed." << endl;
gracefull_exit(-EXIT_FAILURE);
}
if (flock(lockfile, LOCK_EX) == -1) {
cerr << "main(...): flock(...) failed." << endl;
gracefull_exit(-EXIT_FAILURE);
}
char *xdg_current_desktop = getenv("XDG_CURRENT_DESKTOP");
char *xdg_session_type = getenv("XDG_SESSION_TYPE");
if (!xdg_current_desktop || !xdg_session_type) {
cout << "Your desktop environment or session type could not be determined." << endl;
gracefull_exit(SIGTERM);
}
else if (strstr(xdg_current_desktop, "GNOME") &&
strstr(xdg_session_type, "x11")) {
int ret = setup_gnome(lockfile);
if (ret != EXIT_SUCCESS) {
cerr << "main(...): setup_gnome(...) failed." << endl;
gracefull_exit(-ret);
}
}
else {
cout << "Your desktop environment is not supported." << endl;
gracefull_exit(SIGTERM);
}
// start empty glib mainloop, required for glib signals to be catched
GMainLoop *app = g_main_loop_new(NULL, TRUE);
if (!app) {
cerr << "main(...): g_main_loop_new(...) failed." << endl;
gracefull_exit(-EXIT_FAILURE);
}
g_main_loop_run(app);
// g_main_loop_run only returns on error
g_clear_object(&app);
cerr << "main(...): g_main_loop_run(...) failed." << endl;
gracefull_exit(-EXIT_FAILURE);
}