forked from TrilbyWhite/Iocane
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiocane.c
138 lines (128 loc) · 4.2 KB
/
iocane.c
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <X11/Xlib.h>
#define MAXLINE 100
#define MAXSYMLEN 12
typedef struct { KeyCode key; char *command;} Key;
static Display *dpy;
static int scr, sw, sh;
static Window root;
static Bool running = True;
static void press(int arg) {
XEvent ev;
memset(&ev, 0x00, sizeof(ev));
usleep(100000);
ev.type = ButtonPress;
ev.xbutton.button = arg;
ev.xbutton.same_screen = True;
XQueryPointer(dpy,root,&ev.xbutton.root,&ev.xbutton.window,&ev.xbutton.x_root,&ev.xbutton.y_root,&ev.xbutton.x,&ev.xbutton.y,&ev.xbutton.state);
ev.xbutton.subwindow = ev.xbutton.window;
while(ev.xbutton.subwindow) {
ev.xbutton.window = ev.xbutton.subwindow;
XQueryPointer(dpy,ev.xbutton.window,&ev.xbutton.root,&ev.xbutton.subwindow,&ev.xbutton.x_root,&ev.xbutton.y_root,&ev.xbutton.x,&ev.xbutton.y,&ev.xbutton.state);
}
XSendEvent(dpy,PointerWindow,True,0xfff,&ev);
XFlush(dpy);
usleep(100000);
ev.type = ButtonRelease;
ev.xbutton.state = 0x400;
XSendEvent(dpy,PointerWindow, True, 0xfff, &ev);
XFlush(dpy);
}
static void command(char *line) {
if (line[0] == '\0' || line[0] == '\n' || line[0] == '#') return;
int x=0, y=0;
sscanf(line,"%*s %d %d",&x,&y);
char *arg1 = strchr(line,' ') + 1;
if (line[0] == 'p') XWarpPointer(dpy,None,root,0,0,0,0,sw,sh);
else if (line[0] == 'b') press(x);
else if (line[0] == 'm') XWarpPointer(dpy,None,None,0,0,0,0,x,y);
else if (line[0] == 'c') XDefineCursor(dpy,root,XCreateFontCursor(dpy,x));
else if (line[0] == 'q') running = False; /* only relevant in interactive mode */
else if (line[0] == 's') { sleep(x); usleep(y*1000); }
else XWarpPointer(dpy,None,root,0,0,0,0,x,y);
XFlush(dpy);
}
static void scriptmode(int argc, const char **argv) {
int i,len;
char *line = (char *) calloc(MAXLINE+1,sizeof(char));
char *p = line;
for (i = 1; i < argc; i++) {
if (argv[i][0] == ':')
command((p=line));
else {
if (p > line) {*p=' '; p++;}
len = strlen(argv[i]);
strncpy(p,argv[i],len);
*(p+=len) = '\0';
}
}
command(line);
free(line);
}
static void stdinmode(int argc,const char **argv) {
FILE *input = NULL;
if (argc == 3) input=fopen(argv[2],"r");
if (input == NULL) input = stdin;
char *line = (char *) calloc(MAXLINE+1,sizeof(char));
while ( (fgets(line,MAXLINE,input)) != NULL ) command(line);
free(line);
if (input != stdin) fclose(input);
}
int main(int argc, const char **argv) {
if (!(dpy=XOpenDisplay(0x0))) return 1;
scr = DefaultScreen(dpy);
root = RootWindow(dpy,scr);
sw = DisplayWidth(dpy,scr);
sh = DisplayHeight(dpy,scr);
if (argc > 1) {
if (argv[1][0] == '-' && argv[1][1] == 'h')
printf("IOCANE: Copyright 2012, Jesse McClure\nSee `man iocane` for commands and examples.\n");
if (argv[1][0] == '-' && argv[1][1] == '\0')
stdinmode(argc,argv);
else scriptmode(argc,argv);
XCloseDisplay(dpy);
return 0;
}
/* no args -> interactive mode: */
Key *keys = NULL;
char *line = (char *) calloc(MAXLINE+MAXSYMLEN+2,sizeof(char));
char keystring[MAXSYMLEN];
KeySym keysym;
chdir(getenv("HOME"));
FILE *rcfile = fopen(".iocanerc","r");
if (rcfile == NULL) fopen("/usr/share/iocane/iocanerc","r");
if (rcfile == NULL) {
fprintf(stderr,"IOCANE: no iocanerc file found.\n");
XCloseDisplay(dpy);
return 0;
}
int i = 0;
while (fgets(line,MAXLINE+MAXSYMLEN+2,rcfile) != NULL) {
if (line[0] == '#' || line[0] == '\n') continue;
strncpy(keystring,line,MAXSYMLEN); *strchr(keystring,' ') = '\0';
if ( (keysym=XStringToKeysym(keystring)) == NoSymbol ) continue;
keys = realloc(keys,(i+1) * sizeof(Key));
keys[i].key = XKeysymToKeycode(dpy,keysym);
keys[i].command = (char *) calloc(strlen(line) - strlen(keystring),sizeof(char));
strcpy(keys[i].command,strchr(line,' ')+1);
XGrabKey(dpy,keys[i].key,0,root,True,GrabModeAsync,GrabModeAsync);
XGrabKey(dpy,keys[i++].key,LockMask,root,True,GrabModeAsync,GrabModeAsync);
}
int keycount = i;
free(line);
fclose(rcfile);
XEvent ev;
XKeyEvent *e;
while (running && !XNextEvent(dpy,&ev)) if (ev.type == KeyPress) {
e = &ev.xkey;
for (i = 0; i < keycount; i++)
if (e->keycode == keys[i].key && keys[i].command)
command(keys[i].command);
}
for (i = 0; i < keycount; i++) free(keys[i].command);
free(keys);
XCloseDisplay(dpy);
return 0;
}