Created
October 24, 2020 21:50
-
-
Save saivert/0017032242d77b1827738d9ffc0187d3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-2.0 | |
// This is garbage code | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <sys/sysmacros.h> | |
#include <systemd/sd-bus.h> | |
static sd_bus * bus; | |
static int ConnectBus(void) | |
{ | |
int r; | |
/* Connect to the system bus */ | |
r = sd_bus_open_system(&bus); | |
if (r < 0) { | |
fprintf(stderr, "Failed to connect to system bus: %s\n", strerror(-r)); | |
return 0; | |
} | |
return 1; | |
} | |
static void DisconnectBus(void) | |
{ | |
sd_bus_unref(bus); | |
} | |
static int TakeControl(void) | |
{ | |
sd_bus_error error = SD_BUS_ERROR_NULL; | |
sd_bus_message *m = NULL; | |
int r; | |
/* Issue the method call and store the respons message in m */ | |
r = sd_bus_call_method(bus, | |
"org.freedesktop.login1", /* service to contact */ | |
"/org/freedesktop/login1/session/auto", /* object path */ | |
"org.freedesktop.login1.Session", /* interface name */ | |
"TakeControl", /* method name */ | |
&error, /* object to return error in */ | |
&m, /* return message on success */ | |
"b", /* input signature */ | |
0); /* first argument */ | |
if (r < 0) { | |
fprintf(stderr, "Failed to issue method call: %s\n", error.message); | |
return 0; | |
} | |
sd_bus_error_free(&error); | |
sd_bus_message_unref(m); | |
return 1; | |
} | |
const int TakeDevice(int maj, int min) | |
{ | |
sd_bus_error error = SD_BUS_ERROR_NULL; | |
sd_bus_message *m = NULL; | |
const int fd; | |
int r; | |
/* Issue the method call and store the respons message in m */ | |
r = sd_bus_call_method(bus, | |
"org.freedesktop.login1", /* service to contact */ | |
"/org/freedesktop/login1/session/auto", /* object path */ | |
"org.freedesktop.login1.Session", /* interface name */ | |
"TakeDevice", /* method name */ | |
&error, /* object to return error in */ | |
&m, /* return message on success */ | |
"uu", /* input signature */ | |
maj, /* first argument */ | |
min); /* second argument */ | |
if (r < 0) { | |
fprintf(stderr, "Failed to issue method call: %s\n", error.message); | |
return 0; | |
} | |
/* Parse the response message */ | |
r = sd_bus_message_read(m, "h", &fd); | |
if (r < 0) { | |
fprintf(stderr, "Failed to parse response message: %s\n", strerror(-r)); | |
return 0; | |
} | |
sd_bus_error_free(&error); | |
sd_bus_message_unref(m); | |
return fd; | |
} | |
int main(void) | |
{ | |
struct stat s; | |
stat("/dev/input/event2", &s); | |
printf("maj = %x, min = %x\n", major(s.st_rdev), minor(s.st_rdev)); | |
if (ConnectBus()) { | |
if (TakeControl()) | |
TakeDevice(major(s.st_rdev), minor(s.st_rdev)); | |
DisconnectBus(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment