kde-lockscreen-suspend-display/xdg/xinput_devnodes/devnodes.c

164 lines
4.0 KiB
C
Raw Normal View History

/*
2021-10-24 12:09:00 +00:00
* Loosely based on a mix of:
* - xorg-xinput
* https://github.com/freedesktop/xorg-xinput/blob/master/src/property.c::print_property_xi2
* - xkbcat
* https://github.com/anko/xkbcat
*/
#include "devnodes.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <X11/extensions/XInput2.h>
#include <X11/Xatom.h>
2021-10-24 12:09:00 +00:00
#include <X11/Xlib.h>
char* get_property_xi2(Display* dpy, int deviceid, Atom property, const char* property_name)
{
Atom act_type;
char* name;
int act_format;
unsigned long nitems, bytes_after;
unsigned char* data;
unsigned char* ptr;
int j, done = False;
char* ret = NULL;
name = XGetAtomName(dpy, property);
2021-10-16 20:37:23 +00:00
bool interested_in_property = (strcmp(name, property_name) == 0);
XFree(name);
if (!interested_in_property)
return ret;
if (XIGetProperty(dpy, deviceid, property, 0, 1000, False,
AnyPropertyType, &act_type, &act_format,
&nitems, &bytes_after, &data) == Success)
{
ptr = data;
for (j = 0; j < nitems; j++)
{
switch(act_type)
{
case XA_STRING:
if (act_format != 8)
{
//Unknown string format
done = True;
break;
}
int len = strlen((char*)ptr);
2021-10-16 20:37:23 +00:00
ret = strdup((const char*)ptr);
2021-10-16 20:37:23 +00:00
j += len; // The loop's j++ jumps over the terminating 0
ptr += len; // ptr += size below jumps over the terminating 0
break;
2021-10-16 20:37:23 +00:00
default:
// Unknown type
done = True;
break;
}
ptr += act_format/8;
if (done == True)
break;
}
XFree(data);
}
return ret;
}
XSlaves* build_slave_info(Display* disp)
{
int num_slaves;
2021-10-16 20:37:23 +00:00
XIDeviceInfo* all_slaves = XIQueryDevice(disp, XIAllDevices, &num_slaves);
XSlaves* slaves = new_xslaves(num_slaves);
2021-10-16 20:37:23 +00:00
bool fail = false;
for (int i = 0; i < num_slaves; i++)
{
2021-10-16 20:37:23 +00:00
XIDeviceInfo* current_slave = &all_slaves[i];
int nprops;
2021-10-16 20:37:23 +00:00
Atom* props = XIListProperties(disp, current_slave->deviceid, &nprops);
2021-10-16 20:37:23 +00:00
while(nprops-- && !fail)
{
2021-10-16 20:37:23 +00:00
char* devnode = get_property_xi2(disp, current_slave->deviceid, props[nprops], "Device Node");
if (current_slave->name != NULL && devnode != NULL)
{
2021-10-24 12:09:00 +00:00
int err = add_slaveinfo(slaves, current_slave->name, devnode);
if (err)
2021-10-16 20:37:23 +00:00
fail = true;
}
2021-10-16 20:37:23 +00:00
free(devnode);
}
XFree(props);
2021-10-16 20:37:23 +00:00
if (fail)
{
free_xslaves(slaves);
2021-10-24 12:09:00 +00:00
slaves = NULL;
2021-10-16 20:37:23 +00:00
break;
}
2021-10-16 20:37:23 +00:00
}
XIFreeDeviceInfo(all_slaves);
return slaves;
}
int is_xinput2_available(Display* disp)
{
int xiOpcode;
int queryEvent, queryError;
if (! XQueryExtension(disp, "XInputExtension", &xiOpcode, &queryEvent, &queryError))
2021-10-24 12:09:00 +00:00
return XINPUTEXTUNAVAIL;
int major = 2, minor = 0;
int queryResult = XIQueryVersion(disp, &major, &minor);
if (queryResult == BadRequest)
2021-10-24 12:09:00 +00:00
return XINPUT20UNAVAIL;
else if (queryResult != Success)
2021-10-24 12:09:00 +00:00
return XINPUTQUERYVERSIONFAILED;
return 0;
2021-10-16 20:37:23 +00:00
}
2021-10-24 12:09:00 +00:00
Display* connect_to_x(const char* xDisplayName, int* error)
2021-10-16 20:37:23 +00:00
{
Display* disp = XOpenDisplay(xDisplayName);
2021-10-24 12:09:00 +00:00
if (disp == NULL)
2021-10-16 20:37:23 +00:00
{
2021-10-24 12:09:00 +00:00
*error = BADDISPLAY;
2021-10-16 20:37:23 +00:00
return NULL;
}
2021-10-24 12:09:00 +00:00
int err = is_xinput2_available(disp);
if (err)
2021-10-16 20:37:23 +00:00
{
XCloseDisplay(disp);
2021-10-24 12:09:00 +00:00
*error = err;
2021-10-16 20:37:23 +00:00
return NULL;
}
return disp;
}
2021-10-24 12:09:00 +00:00
XSlaves* get_slave_info(const char* xDisplayName, int* error)
{
2021-10-24 12:09:00 +00:00
Display* disp = connect_to_x(xDisplayName, error);
if (*error)
return NULL;
2021-10-16 20:37:23 +00:00
XSlaves* s = build_slave_info(disp);
2021-10-24 12:09:00 +00:00
if (s == NULL)
*error = XSLAVEINFOQUERYFAILED;
2021-10-16 20:37:23 +00:00
XCloseDisplay(disp);
return s;
}