Newer
Older
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
/*
CS 349 Code Examples: X Windows and XLib
eventloop.min.cpp Demos events by displaying mouse motion events
to console (minimal version)
- - - - - - - - - - - - - - - - - - - - - -
See associated makefile for compiling instructions
*/
#include <cstdlib>
#include <iostream>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
using namespace std;
Display* display;
Window window;
int main( int argc, char *argv[] ) {
display = XOpenDisplay("");
if (display == NULL) exit (-1);
int screennum = DefaultScreen(display);
long background = WhitePixel(display, screennum);
long foreground = BlackPixel(display, screennum);
window = XCreateSimpleWindow(display, DefaultRootWindow(display),
10, 10, 300, 200, 2, foreground, background);
XSelectInput(display, window,
PointerMotionMask | KeyPressMask); // select events
XMapRaised(display, window);
XFlush(display);
XEvent event; // save the event here
while ( true ) { // event loop until 'exit'
XNextEvent( display, &event ); // wait for next event
cout << event.type << " ";
switch ( event.type ) {
case MotionNotify: // mouse movement
cout << event.xmotion.x << ","
<< event.xmotion.y << endl;
break;
case KeyPress: // any keypress
exit(0);
break;
}
}
XCloseDisplay(display);
}