/
main.cpp
76 lines (61 loc) · 1.58 KB
/
main.cpp
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
/*
Simple Xlib application drawing a box in a window.
*/
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
Display *display;
int screen;
Window window;
XEvent event;
/* open connection with the server */
display = XOpenDisplay(NULL);
if (!display)
{
printf("Cannot open display\n");
exit(EXIT_FAILURE);
}
screen = DefaultScreen(display);
/* create window */
window = XCreateSimpleWindow(display, RootWindow(display, screen),
10, 10, 100, 100, 1, BlackPixel(display, screen),
WhitePixel(display, screen));
/* select kind of events we are interested in */
XSelectInput(display, window, ExposureMask | KeyPressMask);
/* map (show) the window */
XMapWindow(display, window);
int i = 0;
char buffer[127];
XWindowAttributes winInfos;
/* event loop */
while (1)
{
XNextEvent(display, &event);
/* draw or redraw the window */
//if (event.type == Expose)
{
XGetWindowAttributes(display, window, &winInfos);
XFillRectangle(
display, window, DefaultGC(display, screen), 20, 20, 10, 10);
sprintf(buffer, "width=%d", winInfos.width);
XDrawString(
display, window, DefaultGC(display, screen),
winInfos.width / 2, 10,
buffer, strlen(buffer));
sprintf(buffer, "height=%d", winInfos.height);
XDrawString(
display, window, DefaultGC(display, screen),
0, winInfos.height / 2,
buffer, strlen(buffer));
}
/* exit on key press */
if (event.type == KeyPress)
break;
}
/* close connection to server */
XCloseDisplay(display);
return EXIT_SUCCESS;
}