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
|
#include <X11/Xlib.h> // Every Xlib program must include this
#include <assert.h> // I include this to test return values the lazy way
#include <unistd.h> // So we got the profile for 10 seconds
#include <stdio.h>
#define NIL (0) // A name for the void pointer
void main()
{
// Open the display
Display *dpy = XOpenDisplay(NIL);
// Get some colors
int blackColor = BlackPixel(dpy, DefaultScreen(dpy));
int whiteColor = WhitePixel(dpy, DefaultScreen(dpy));
// Create the windows
Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
200, 100, 0, blackColor, blackColor);
fprintf(stdout, "window = %li\n", w);
Window w2 = XCreateSimpleWindow(dpy, w, 0, 0,
50, 50, 0, blackColor, whiteColor);
fprintf(stdout, "window2 = %li\n", w2);
// We want to get MapNotify events
XSelectInput(dpy, w, ButtonPressMask | ButtonReleaseMask | KeyPressMask);
XSelectInput(dpy, w2, ButtonPressMask | ButtonReleaseMask | KeyPressMask);
// "Map" the window (that is, make it appear on the screen)
XMapWindow(dpy, w);
// Create a "Graphics Context"
GC gc = XCreateGC(dpy, w, 0, NIL);
// Tell the GC we draw using the white color
XSetForeground(dpy, gc, whiteColor);
XEvent e;
for(;;) {
XNextEvent(dpy, &e);
fprintf(stdout, "receiving event %i, on window %li\n", e.type, e.xany.window);
if (e.type == ButtonPress)
{
if( e.xany.window == w)
{
fprintf(stdout, "button press from w\n");
}
else if (e.xany.window == w2)
{
fprintf(stdout, "button press from w2\n");
}
//break;
}
else if (e.type == ButtonRelease)
{
if( e.xany.window == w)
{
fprintf(stdout, "button release from w\n");
}
else if (e.xany.window == w2)
{
fprintf(stdout, "button release from w2\n");
}
//break;
}
else if (e.type == KeyPress)
{
fprintf(stdout, "KeyPress hihih\n");
}
else
{
//break;
}
}
} |
Partager