You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tuxedo-touchpad-switch/tuxedo-touchpad-switch.cpp

127 lines
3.8 KiB

4 years ago
// 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>
4 years ago
#include <fcntl.h>
4 years ago
#include <unistd.h>
#include <sys/file.h>
4 years ago
4 years ago
#include <gio/gio.h>
#include "touchpad-control.h"
#include "setup-gnome.h"
#include "setup-kde.h"
4 years ago
using std::cout;
using std::cerr;
using std::endl;
4 years ago
static int lockfile = -1;
4 years ago
static void gracefull_exit(int signum = 0) {
int result = EXIT_SUCCESS;
clean_gnome();
clean_kde();
if (signum < 0) {
result = EXIT_FAILURE;
4 years ago
}
if (set_touchpad_state(1) != EXIT_SUCCESS) {
cerr << "gracefull_exit(...): set_touchpad_state(...) failed." << endl;
result = EXIT_FAILURE;
4 years ago
}
if (lockfile >= 0) {
if (flock(lockfile, LOCK_UN)) {
cerr << "gracefull_exit(...): flock(...) failed." << endl;
result = EXIT_FAILURE;
4 years ago
}
if (close(lockfile)) {
cerr << "gracefull_exit(...): close(...) failed." << endl;
result = EXIT_FAILURE;
4 years ago
}
4 years ago
}
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);
}
4 years ago
char *xdg_current_desktop = getenv("XDG_CURRENT_DESKTOP");
if (!xdg_current_desktop) {
cout << "Your desktop environment could not be determined. Maybe running as root?" << endl;
gracefull_exit(SIGTERM);
}
else if (strstr(xdg_current_desktop, "GNOME")) {
setup_gnome(lockfile);
4 years ago
}
else if (strstr(xdg_current_desktop, "KDE")) {
setup_kde(lockfile);
4 years ago
}
else {
cout << "Your desktop environment is not supported." << endl;
gracefull_exit(SIGTERM);
}
4 years ago
// start empty glib mainloop, required for glib signals to be catched
4 years ago
GMainLoop *app = g_main_loop_new(NULL, TRUE);
if (!app) {
4 years ago
cerr << "main(...): g_main_loop_new(...) failed." << endl;
gracefull_exit(-EXIT_FAILURE);
4 years ago
}
4 years ago
4 years ago
g_main_loop_run(app);
// g_main_loop_run only returns on error
g_clear_object(&app);
4 years ago
cerr << "main(...): g_main_loop_run(...) failed." << endl;
gracefull_exit(-EXIT_FAILURE);
4 years ago
}