summaryrefslogtreecommitdiff
path: root/src/keylogger.c
blob: dae2177f3ea21250189ce941da2a364a49285da8 (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
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/XInput.h>

#define INVALID_EVENT_TYPE  -1

static int key_press_type = INVALID_EVENT_TYPE;
static int key_release_type = INVALID_EVENT_TYPE;

static void
_press(const char *symbol)
{
   fprintf(stdout, ".%s\n", symbol);
   fflush(stdout);
}

static void
_release(const char *symbol)
{
   fprintf(stdout, ",%s\n", symbol);
   fflush(stdout);
}

int main(void) {

  int i;
  int number = 0;
  int run = 1;
  int default_screen;

  Display * default_display;
  Window root_window;
  XEvent e;
  XDeviceKeyEvent * key;
  XInputClassInfo       *ip;
  XEventClass           event_list[20];

  default_display = XOpenDisplay(getenv("DISPLAY"));
  default_screen = DefaultScreen(default_display);
  root_window = RootWindow(default_display, default_screen);

  int num = 0;

  XDeviceInfo *infos = XListInputDevices(default_display, &num);

  //find all devices that are doing input
  for (int i = 0; i < num; ++i) {
     XDevice * device;

     if (infos[i].id == IsXKeyboard) continue;
     if (infos[i].id == IsXPointer) continue;
     if (infos[i].id == IsXExtensionKeyboard) continue;
     if (infos[i].id == IsXExtensionPointer) continue;


     switch(infos[i].inputclassinfo->class) {
        case KeyClass:
          device = XOpenDevice(default_display, infos[i].id);

          if(!device){
            fprintf(stderr, "unable to open device\n");
            return 0;
          }

          DeviceKeyPress(device, key_press_type, event_list[number]); number++;
          DeviceKeyRelease(device, key_release_type, event_list[number]); number++;

        break;
     }
  }

  /* important */
  if(XSelectExtensionEvent(default_display, root_window, event_list, number)) {
    fprintf(stderr, "error selecting extended events\n");
    return 0;
  }

  while(run) {
    XNextEvent(default_display, &e);
    key = (XDeviceKeyEvent *) &e;
    const char *symbol;

    symbol = XKeysymToString(XKeycodeToKeysym(default_display, key->keycode, 0));

    if (key->type == key_press_type)
      {
         _press(symbol);
      }
    else if (key->type == key_release_type)
      {
         _release(symbol);
      }
  }

  /* Close the connection to the X server */
  XCloseDisplay(default_display);

  return 0;
}