-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathxlib_hello.c
More file actions
147 lines (121 loc) · 3.51 KB
/
xlib_hello.c
File metadata and controls
147 lines (121 loc) · 3.51 KB
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
/* Create window, log X11 events and display pressed keys.
*
* Usage:
* gcc -o xlib_hello xlib_hello.c -lX11
* ./xlib_hello
*/
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <string.h>
#include <stdio.h>
static const char *event_names[] = {
"",
"",
"KeyPress",
"KeyRelease",
"ButtonPress",
"ButtonRelease",
"MotionNotify",
"EnterNotify",
"LeaveNotify",
"FocusIn",
"FocusOut",
"KeymapNotify",
"Expose",
"GraphicsExpose",
"NoExpose",
"VisibilityNotify",
"CreateNotify",
"DestroyNotify",
"UnmapNotify",
"MapNotify",
"MapRequest",
"ReparentNotify",
"ConfigureNotify",
"ConfigureRequest",
"GravityNotify",
"ResizeRequest",
"CirculateNotify",
"CirculateRequest",
"PropertyNotify",
"SelectionClear",
"SelectionRequest",
"SelectionNotify",
"ColormapNotify",
"ClientMessage",
"MappingNotify"
};
int main(int argc, char** argv) {
Display* display = XOpenDisplay(NULL);
if (display == NULL) {
return 1;
}
int screen = DefaultScreen(display);
GC gc = DefaultGC(display, screen);
Window parent_window = DefaultRootWindow(display);
int x = 0;
int y = 0;
unsigned int width = 400;
unsigned int height = 40;
unsigned int border_width = 1;
unsigned int border_color = BlackPixel(display, screen);
unsigned int background_color = WhitePixel(display, screen);
// Create window
Window hello_window = XCreateSimpleWindow(display, parent_window,
x,
y,
width,
height,
border_width,
border_color,
background_color);
long event_mask = ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
| FocusChangeMask
;
// Select window events
XSelectInput(display, hello_window, event_mask);
// Make window visible
XMapWindow(display, hello_window);
// Set window title
XStoreName(display, hello_window, "Hello, World!");
// Get WM_DELETE_WINDOW atom
Atom wm_delete = XInternAtom(display, "WM_DELETE_WINDOW", True);
// Subscribe WM_DELETE_WINDOW message
XSetWMProtocols(display, hello_window, &wm_delete, 1);
char msg[1024] = "";
char key[32];
// Event loop
for (;;) {
// Get next event from queue
XEvent event;
XNextEvent(display, &event);
// Print event type
printf("got event: %s\n", event_names[event.type]);
// Keyboard
if (event.type == KeyPress) {
int len = XLookupString(&event.xkey, key, sizeof(key) - 1, 0, 0);
key[len] = 0;
if (strlen(msg) > 50)
msg[0] = 0;
strcat(msg, key);
strcat(msg, " ");
}
// Refresh
if (event.type == KeyPress || event.type == Expose) {
XClearWindow(display, hello_window);
XDrawString(display, hello_window, gc, 10, 20, msg, strlen(msg));
}
// Close button
if (event.type == ClientMessage) {
if (event.xclient.data.l[0] == wm_delete) {
break;
}
}
}
XCloseDisplay(display);
return 0;
}