瀏覽代碼

Hello, Rust!

Don't look at the C code, please, it's quite... working, but, y' know.
Lucas Stadler 13 年之前
父節點
當前提交
93e18df3c3
共有 3 個文件被更改,包括 142 次插入0 次删除
  1. 11 0
      rust/Makefile
  2. 24 0
      rust/keylog.rs
  3. 107 0
      rust/keylog_xlib.c

+ 11 - 0
rust/Makefile

@ -0,0 +1,11 @@
1
keylog: keylog.rs libkeylog_xlib.so
2
	rustc -L rust-xlib -L . keylog.rs
3
4
libkeylog_xlib.so: keylog_xlib.c
5
	clang -fPIC -c keylog_xlib.c
6
	clang -shared -Wl,-soname,libkeylog_xlib.so -o libkeylog_xlib.so keylog_xlib.o
7
8
clean-keylog:
9
	rm -f keylog libkeylog_xlib.so keylog_xlib.o
10
11
clean: clean-keylog

+ 24 - 0
rust/keylog.rs

@ -0,0 +1,24 @@
1
extern mod xlib;
2
3
use XlibT = xlib::xlib;
4
use Xlib = xlib::xlib::bindgen;
5
6
extern mod xlib_helpers;
7
use xh = xlib_helpers::xlib_helpers;
8
9
extern mod keylog_xlib {
10
	fn print_next_event(display: *XlibT::Display);
11
}
12
13
fn main() {
14
	let display = Xlib::XOpenDisplay(ptr::null());
15
	
16
	loop { keylog_xlib::print_next_event(display); }
17
18
	/*do xevents(KeyPressMask | FocusChangeMask) |&ev| {
19
		match ev.type {
20
			KeyPress => (),
21
			_ => ()
22
		}
23
	}*/
24
}

+ 107 - 0
rust/keylog_xlib.c

@ -0,0 +1,107 @@
1
#include <time.h>
2
#include <ctype.h>
3
#include <stdio.h>
4
#include <stdlib.h>
5
#include <string.h>
6
7
#include <X11/Xlib.h>
8
#include <X11/Xutil.h>
9
#define IsTTYFunctionKey(keysym) (((keysym >= 0xff08) && (keysym <= 0xff1b)) || keysym == 0xffff)
10
11
/* internal state */
12
Window focused_window = None;
13
14
char *rfc3339_format_time(struct tm *time) {
15
	char *rfc3339 = malloc(sizeof(char) * 100);
16
	strftime(rfc3339, 100, "%Y-%m-%d %H:%M:%S", time);
17
	int tzd_sec = abs(timezone) / 60 / 60;
18
	int tzd_mon = abs(timezone) % 60;
19
	sprintf(rfc3339 + strlen(rfc3339), "%+03d:%02d", tzd_sec, tzd_mon);
20
	return rfc3339;
21
}
22
23
char *rfc3339_now_str() {
24
	time_t now_t = time(NULL);
25
	struct tm *now = gmtime(&now_t);
26
	return rfc3339_format_time(now);
27
}
28
29
Window wparent(Display *display, Window window) {
30
	Window parent;
31
	Window root;
32
	Window *children;
33
	unsigned int nchildren;
34
	XQueryTree(display, window, &root, &parent, &children, &nchildren);
35
	return parent;
36
}
37
38
XTextProperty *get_window_name(Display *display, Window window) {
39
	XTextProperty *wm_name_prop = malloc(sizeof(XTextProperty));
40
	Atom net_wm_name = XInternAtom(display, "_NET_WM_NAME", False);
41
	XGetTextProperty(display, window, wm_name_prop, net_wm_name);
42
	if (wm_name_prop->value == NULL) {
43
		XGetTextProperty(display, wparent(display, window), wm_name_prop, net_wm_name);
44
	}
45
	return wm_name_prop;
46
}
47
48
void print_next_event(Display *display) {
49
	XEvent ev;
50
	KeyCode kc= -1;
51
	KeySym ksym;
52
	char *ksymname = NULL;
53
	char *kname = malloc(sizeof(char) * 2);
54
	
55
	//printf("hi there, nice to see you.\n");
56
	
57
	if (focused_window == None) {
58
		//printf("no focus, let's look for it.\n");
59
		int r = 0;
60
		XGetInputFocus(display, &focused_window, &r);
61
		//printf("got focus\n");
62
		if (focused_window != None) {
63
			XSelectInput(display, focused_window, KeyPressMask | FocusChangeMask | PropertyChangeMask);
64
			//printf("selected input");
65
		} else {
66
			
67
		}
68
		return;
69
	}
70
71
	XNextEvent(display, &ev);
72
	if(ev.xany.type == FocusOut) {
73
		focused_window = None;
74
	} else if (ev.xany.type == PropertyNotify) {
75
		Atom _NET_WM_NAME = XInternAtom(display, "_NET_WM_NAME", False);
76
		if (ev.xproperty.atom == _NET_WM_NAME) {
77
			XTextProperty *wm_name_prop = get_window_name(display, focused_window);
78
			printf("focus '%s' %s\n", wm_name_prop->value, rfc3339_now_str());
79
			free(wm_name_prop);
80
		}
81
	} else if (ev.xany.type == KeyPress) {
82
		/* XLookupString  handle keyboard input events in Latin-1 */
83
		XLookupString(&ev.xkey, kname, 2, &ksym, 0);
84
85
		/* Find out string representation */
86
		if(ksym == NoSymbol) {
87
			ksymname = "NoSymbol";
88
		} else {
89
			if (!(ksymname = XKeysymToString (ksym))) {
90
				ksymname = "(no name)";
91
			}
92
			kc = XKeysymToKeycode(display, ksym);
93
		}
94
	}
95
	
96
	if (ksymname != NULL) {
97
		const int non_printable = IsKeypadKey(ksym) || IsTTYFunctionKey(ksym) || IsFunctionKey(ksym) || IsCursorKey(ksym) || IsModifierKey(ksym);
98
		printf("key ");
99
		if (non_printable) {
100
			printf("%s", ksymname);
101
		} else {
102
			printf("'%s'", kname);
103
		}
104
		printf(" %s\n", rfc3339_now_str());
105
		fflush(stdout);
106
	}
107
}