summaryrefslogtreecommitdiff
path: root/cursorpen.c
blob: eeeff58c24d8715df366cd9baf2a3819a511a549 (plain)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/input.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <libevdev-1.0/libevdev/libevdev.h>
#include <X11/Xlib.h>
#include <X11/extensions/XTest.h>

#define SCALE 1

struct abs_range {
    int x_min;
    int x_max;
    int x_range;
    int y_min;
    int y_max;
    int y_range;
};

int click_mouse(Display *dpy, struct input_event *ev) {
    int btn;

    if (ev->code == BTN_LEFT) {
        btn = 1;
    } else if (ev->code == BTN_RIGHT) {
        btn = 2;
    } else {
        return 1;
    }

    if (ev->value < 2) {
        XTestFakeButtonEvent(dpy, btn, ev->value, CurrentTime);
    }
}

int move_mouse(Display *dpy, float posx, float posy, int screenw, int screenh) {
    float x, y;
    x = (screenw * posx) * SCALE;
    y = (screenh * posy) * SCALE;
    //x = ((screenw * posx) - (screenw*0.3)) * SCALE;
    //y = ((screenh * posy) - (screenh*0.1)) * SCALE;

    Window root = DefaultRootWindow(dpy);
    XWarpPointer(dpy, None, root, 0, 0, 0, 0, x, y);
    XFlush(dpy);
}

int handle_event(struct input_event *ev, Display *dpy, struct abs_range abs_range, float *posx, float *posy, int sw, int sh) {
    if (ev->type == EV_ABS) {
        if (ev->code == ABS_X) {
            *posx = (float) (ev->value - abs_range.x_min) / abs_range.x_range;
        } else if (ev->code == ABS_Y) {
            *posy = (float) (ev->value - abs_range.y_min) / abs_range.y_range;
        } 
    } else if (ev->type == EV_KEY) {
        click_mouse(dpy, ev);
    }
    move_mouse(dpy, *posx, *posy, sw, sh);

	return 0;
}


Display *get_resolution(int *width, int *height) {
    Display *dpy; int snum;
    if(!(dpy = XOpenDisplay(0)))
            fprintf(stderr, "cannot open display '%s'", XDisplayName(0));
    snum = DefaultScreen(dpy);
    *width = DisplayWidth(dpy, snum);
    *height = DisplayHeight(dpy, snum);
    printf("screen is %dx%d\n", *width, *height);
    return dpy;
}


XDevice *get_trackpad(Display *dpy) {
    XDevice *device; 
    XDeviceInfo *devices;

    Atom touchpad_type = 0;
    touchpad_type = XInternAtom(dpy, XI_TOUCHPAD, True);

    int ndevices = 50;
    devices = XListInputDevices(dpy, &ndevices);

    while (ndevices--) {
        if (devices[ndevices].type == touchpad_type) {
            device = XOpenDevice(dpy, devices[ndevices].id);
            if (!device) {
                fprintf(stderr, "Failed to open device '%s'.\n", devices[ndevices].name);
                break;
            }
            printf("Opened device '%s'.\n", devices[ndevices].name);
        }
    }

    XFreeDeviceList(devices);
    return device;
}

char *get_event_device(Display *dpy, XDevice *device) {
    int rc, i;
    Atom prop;
    Atom realtype;
    int realformat;
    unsigned long nitems, bytes_after;
    unsigned char *data;

    prop = XInternAtom (dpy, "Device Node", 0);

    rc = XGetDeviceProperty(dpy,
        device,
        prop,
        0, // offset
        5, // number of items
        0, // false
        AnyPropertyType,
        &realtype,
        &realformat,
        &nitems,
        &bytes_after,
        &data);

    if (rc != Success) {
        fprintf(stderr, "Failed to read property '%s'\n", "Device Node");
    }
    return data;
}

int main() {
    Display *dpy;
    XDevice *device;
    unsigned char *node;
    struct libevdev *dev;
    struct input_event ev;
    struct abs_range abs_range;
    float posx, posy;
    int sw, sh;
    int fd, rc;

    dpy = get_resolution(&sw, &sh);
    device = get_trackpad(dpy);
    node = get_event_device(dpy, device);
    printf("Reading device from %s\n", node);

    fd = open(node, O_RDONLY|O_NONBLOCK);
    if (fd < 0)
       fprintf(stderr, "error: %d %s\n", errno, strerror(errno));
    rc = libevdev_new_from_fd(fd, &dev);
    if (rc < 0)
       fprintf(stderr, "error: %d %s\n", -rc, strerror(-rc));

    abs_range.x_min = libevdev_get_abs_minimum(dev, ABS_X);
    abs_range.x_max = libevdev_get_abs_maximum(dev, ABS_X);
    abs_range.y_min = libevdev_get_abs_minimum(dev, ABS_Y);
    abs_range.y_max = libevdev_get_abs_maximum(dev, ABS_Y);

    abs_range.x_range = abs_range.x_max - abs_range.x_min;
    abs_range.y_range = abs_range.y_max - abs_range.y_min;

    do {
		struct input_event ev;
		rc = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL|LIBEVDEV_READ_FLAG_BLOCKING, &ev);
		if (rc == LIBEVDEV_READ_STATUS_SYNC) {
            // this is not needed since we dont really care about catching up with missed events
			//while (rc == LIBEVDEV_READ_STATUS_SYNC) {
				//handle_event(&ev, dpy, abs_range, &posx, &posy, sw, sh);
				//rc = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_SYNC, &ev);
			//}
		} else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
            handle_event(&ev, dpy, abs_range, &posx, &posy, sw, sh);
        }
	} while (rc == LIBEVDEV_READ_STATUS_SYNC || rc == LIBEVDEV_READ_STATUS_SUCCESS || rc == -EAGAIN);
;
    return 0;
}