Skip to content

Commit aefd485

Browse files
Added xmove_window test program
1 parent 000a80f commit aefd485

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed

test/xmove_window.c

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*
2+
* Simple test program to create an X Window and move it with a keyboard.
3+
*/
4+
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <X11/Xlib.h>
8+
#include <X11/Xutil.h>
9+
10+
static int xPos = 0;
11+
static int yPos = 0;
12+
13+
static void
14+
eventLoop(Display *dpy, Window win);
15+
16+
static Window create_window(
17+
Display* dpy,
18+
XVisualInfo* vis_info,
19+
int x,
20+
int y,
21+
int width,
22+
int height);
23+
24+
int
25+
main(int argc, char **argv)
26+
{
27+
Display *dpy;
28+
XVisualInfo template;
29+
XVisualInfo* vis_info;
30+
Window window;
31+
int nitems;
32+
33+
/* Open display */
34+
if (!(dpy = XOpenDisplay(NULL))) {
35+
fprintf(stderr, "Could not open display\n");
36+
exit(1);
37+
}
38+
39+
template.class = TrueColor;
40+
template.depth = 24;
41+
vis_info = XGetVisualInfo(dpy, VisualClassMask | VisualDepthMask,
42+
&template, &nitems);
43+
44+
if (nitems < 1 || vis_info == NULL) {
45+
fprintf(stderr, "No conforming visual exists\n");
46+
exit(1);
47+
}
48+
49+
fprintf(stderr,
50+
"found a %d-bit visual (visual ID = 0x%x)\n",
51+
vis_info->depth,
52+
(unsigned int)vis_info->visualid);
53+
54+
window = create_window(dpy, vis_info,
55+
10, 10, 100, 100);
56+
eventLoop(dpy, window);
57+
}
58+
59+
60+
static void
61+
eventLoop(Display *dpy, Window win)
62+
{
63+
XEvent event, next_event;
64+
65+
while (1) {
66+
XNextEvent(dpy, &event);
67+
switch(event.type) {
68+
case VisibilityNotify:
69+
{
70+
XVisibilityEvent *xve = (XVisibilityEvent*) &event;
71+
fprintf(stderr,
72+
"Visibility event: visibility = %d\n",
73+
xve->state);
74+
break;
75+
}
76+
case Expose:
77+
{
78+
XExposeEvent *xee = (XExposeEvent*) &event;
79+
fprintf(stderr, "Expose event: (%d, %d) %dx%d\n",
80+
xee->x, xee->y, xee->width, xee->height);
81+
break;
82+
}
83+
case ConfigureNotify:
84+
{
85+
XConfigureEvent *xce = (XConfigureEvent*) &event;
86+
fprintf(stderr, "ConfigureNotify event\n");
87+
break;
88+
}
89+
90+
case KeyPress:
91+
{
92+
int i, j;
93+
char text[3];
94+
95+
fprintf(stderr, "KeyPress event: ");
96+
i = XLookupString((XKeyEvent *)&event,
97+
text,
98+
sizeof(text),
99+
NULL,
100+
NULL);
101+
if (i == 1) {
102+
fprintf(stderr, "key = 0x%x ('%c')", text[0], text[0]);
103+
switch (text[0]) {
104+
case '-':
105+
xPos -= 10;
106+
yPos -= 10;
107+
fprintf(stderr, "move to: %d, %d\n", xPos, yPos);
108+
XMoveWindow(dpy, win, xPos, yPos);
109+
break;
110+
111+
case '+':
112+
case '=':
113+
xPos += 10;
114+
yPos += 10;
115+
fprintf(stderr, "move to: %d, %d\n", xPos, yPos);
116+
XMoveWindow(dpy, win, xPos, yPos);
117+
break;
118+
119+
case '\033':
120+
fprintf(stderr, "\n");
121+
exit(0);
122+
}
123+
}
124+
fprintf(stderr, "\n");
125+
break;
126+
}
127+
case MappingNotify:
128+
XRefreshKeyboardMapping((XMappingEvent *)&event);
129+
break;
130+
}
131+
}
132+
}
133+
134+
135+
static Window
136+
create_window(
137+
Display* dpy,
138+
XVisualInfo* vis_info,
139+
int x,
140+
int y,
141+
int width,
142+
int height)
143+
{
144+
Window window;
145+
XSetWindowAttributes wattrs;
146+
XEvent event;
147+
Colormap cmap;
148+
149+
xPos = x;
150+
yPos = y;
151+
152+
cmap = XCreateColormap(dpy, RootWindow(dpy, vis_info->screen),
153+
vis_info->visual, AllocNone);
154+
155+
/* Create the output window. */
156+
wattrs.background_pixel = 0;
157+
wattrs.border_pixel = 0;
158+
wattrs.override_redirect = True;
159+
wattrs.colormap = cmap;
160+
wattrs.event_mask = VisibilityChangeMask | ExposureMask | StructureNotifyMask | KeyPressMask;
161+
window = XCreateWindow(dpy, RootWindow(dpy, vis_info->screen),
162+
x, y, width, height, 1,
163+
vis_info->depth, InputOutput, vis_info->visual,
164+
CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &wattrs);
165+
XMapWindow(dpy, window );
166+
167+
return window;
168+
}

0 commit comments

Comments
 (0)