-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement touchpad relative input mode
- Loading branch information
Xtr126
committed
Nov 20, 2023
1 parent
e6ccf6b
commit 4cd7684
Showing
8 changed files
with
328 additions
and
99 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
#include "mouse_cursor.h" | ||
#include <thread> | ||
#include <jni.h> | ||
#include <vector> | ||
#include <iostream> | ||
#include <cstdio> | ||
#include <cstring> | ||
#include <dirent.h> | ||
#include <unistd.h> | ||
#include <fcntl.h> | ||
#include <sys/poll.h> | ||
#include <linux/uinput.h> | ||
#include <linux/input.h> | ||
#include <linux/input-event-codes.h> | ||
#include <cstdlib> | ||
#include "evdev_common.h" | ||
|
||
using std::string; | ||
|
||
std::vector<string> ListInputDevices() { | ||
const string input_directory = "/dev/input"; | ||
std::vector<string> filenames; | ||
struct DIR * directory = opendir(input_directory.c_str()); | ||
|
||
struct dirent *entry; | ||
while ((entry = readdir(directory))) { | ||
if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { | ||
filenames.push_back(input_directory + "/" + entry->d_name); | ||
} | ||
} | ||
return filenames; | ||
} | ||
|
||
std::vector<int> scanTouchpadDevices() { | ||
std::vector<string> evdevNames = ListInputDevices(); | ||
std::vector<int> touchpadDeviceFds; | ||
|
||
printf("I: Searching for touchpad devices...\n"); | ||
for (auto & evdev : evdevNames) { | ||
int device_fd = open(evdev.c_str(), O_RDWR); | ||
if (device_fd < 0) { | ||
perror("opening device"); | ||
} | ||
|
||
// Get device name | ||
char dev_name[24]; | ||
if(!ioctl(device_fd, EVIOCGNAME(sizeof(dev_name) - 1), &dev_name)) { | ||
perror("get device name"); | ||
close(device_fd); | ||
continue; | ||
} | ||
|
||
if(!HasSpecificAbs(device_fd, ABS_X) || !HasSpecificAbs(device_fd, ABS_Y)) { | ||
printf("%s does not have ABS_X or ABS_Y events\n", dev_name); | ||
printf("%s: Not a touch device\n", dev_name); | ||
close(device_fd); | ||
continue; | ||
} | ||
|
||
if (!HasInputProp(device_fd, INPUT_PROP_POINTER)) { | ||
printf("%s does not have INPUT_PROP_POINTER\n", dev_name); | ||
printf("%s: Not a touchpad device\n", dev_name); | ||
close(device_fd); | ||
continue; | ||
} | ||
|
||
|
||
// Check for virtual devices | ||
if (x_virtual_tablet == dev_name || x_virtual_mouse == dev_name) { | ||
printf("skipping %s\n", dev_name); | ||
close(device_fd); | ||
continue; | ||
} | ||
else if (x_virtual_touch == dev_name) { | ||
touchpadDeviceFds.clear(); | ||
return touchpadDeviceFds; | ||
} | ||
|
||
printf(" adding touchpad device...\n", evdev.c_str()); | ||
ioctl(device_fd, EVIOCGRAB, (void *)1); | ||
|
||
touchpadDeviceFds.push_back(device_fd); | ||
} | ||
return touchpadDeviceFds; | ||
} | ||
|
||
bool HasSpecificAbs(int device_fd, unsigned int abs) { | ||
unsigned long nchar = KEY_MAX / 8 + 1; | ||
unsigned char bits[nchar]; | ||
// Get the bit fields of available abs events. | ||
ioctl(device_fd, EVIOCGBIT(EV_ABS, sizeof(bits)), &bits); | ||
return bits[abs/8] & (1 << (abs % 8)); | ||
} | ||
|
||
bool HasSpecificKey(int device_fd, unsigned int key) { | ||
unsigned long nchar = KEY_MAX / 8 + 1; | ||
unsigned char bits[nchar]; | ||
// Get the bit fields of available keys. | ||
ioctl(device_fd, EVIOCGBIT(EV_KEY, sizeof(bits)), &bits); | ||
return bits[key/8] & (1 << (key % 8)); | ||
} | ||
|
||
bool HasInputProp(int device_fd, unsigned int input_prop) { | ||
unsigned long nchar = INPUT_PROP_MAX / 8 + 1; | ||
unsigned char bits[nchar]; | ||
// Get the bit fields of available keys. | ||
ioctl(device_fd, EVIOCGPROP(sizeof(bits)), &bits); | ||
return bits[input_prop/8] & (1 << (input_prop % 8)); | ||
} | ||
|
||
bool HasEventType(int device_fd, unsigned int type) { | ||
unsigned long evbit = 0; | ||
// Get the bit field of available event types. | ||
ioctl(device_fd, EVIOCGBIT(0, sizeof(evbit)), &evbit); | ||
return evbit & (1 << type); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using std::string; | ||
|
||
const char *x_virtual_touch = "x-virtual-touch"; | ||
const char *x_virtual_mouse = "x-virtual-mouse"; | ||
|
||
std::vector<string> ListInputDevices(); | ||
|
||
std::vector<int> scanTouchpadDevices(); | ||
|
||
bool HasSpecificAbs(int device_fd, unsigned int abs); | ||
|
||
bool HasSpecificKey(int device_fd, unsigned int key); | ||
|
||
bool HasInputProp(int device_fd, unsigned int input_prop); | ||
|
||
bool HasEventType(int device_fd, unsigned int type); |
This file contains 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
Oops, something went wrong.