/*
* Simple Xlib application drawing a box in a window.
* gcc input.c -o output -lX11
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
int
main(int argc, char **argv)
{
Display *display;
Window window;
XEvent event;
char *msg = "Hello, World!";
int s;
/* Open connection with the server. */
display = XOpenDisplay(NULL);
if(display == NULL){
fprintf(stderr, "Cannot open display\n");
exit(1);
}
s = DefaultScreen(display);
/* Create window. */
window = XCreateSimpleWindow(display, RootWindow(display, s), 10, 10,
200, 200, 1, BlackPixel(display, s), WhitePixel(display, s));
/* Select kind of events we are interested in. */
XSelectInput(display, window, ExposureMask | KeyPressMask);
/* Map (show) the window. */
XMapWindow(display, window);
/* Event loop. */
for(;;){
XNextEvent(display, &event);
/* Draw or redraw the window. */
if(event.type == Expose){
XFillRectangle(display, window, DefaultGC(display, s),
20, 20, 10, 10);
XDrawString(display, window, DefaultGC(display, s), 50,
50, msg, strlen(msg));
}
/* Exit on key press. */
if(event.type == KeyPress)
break;
}
/* Close connection to server. */
XCloseDisplay(display);
return 0;
}