Hit any key to quit from this program.
/* compile using cc -I/usr/include/X11R5 -L/usr/lib/X11R5 -o demo demo.c -lX11 */
#include <stdio.h>
#include <X11/Xlib.h>
/* This program draws a red line and some text in a chosen font.
*
*/
Display *display;
Window window;
XSetWindowAttributes attributes;
XGCValues gr_values;
XFontStruct *fontinfo;
GC gr_context;
Visual *visual;
int depth;
int screen;
XEvent event;
XColor color, dummy;
main (argc, argv)
char *argv[];
int argc;
{
display = XOpenDisplay(NULL);
screen = DefaultScreen(display);
visual = DefaultVisual(display,screen);
depth = DefaultDepth(display,screen);
attributes.background_pixel = XWhitePixel(display,screen);
window = XCreateWindow( display,XRootWindow(display,screen),
200, 200, 350, 200, 5, depth, InputOutput,
visual ,CWBackPixel, &attributes);
XSelectInput(display,window,ExposureMask | KeyPressMask) ;
fontinfo = XLoadQueryFont(display,"6x10");
XAllocNamedColor(display, DefaultColormap(display, screen),"red",
&color,&dummy);
gr_values.font = fontinfo->fid;
gr_values.foreground = color.pixel;
gr_context=XCreateGC(display,window,GCFont+GCForeground, &gr_values);
XFlush(display);
XMapWindow(display,window);
XFlush(display);
while(1){
XNextEvent(display,&event);
switch(event.type){
case Expose:
XDrawLine(display,window,gr_context,0,0, 100, 100);
XDrawString(display,window,gr_context,100,100,"hello",5);
break;
case KeyPress:
XCloseDisplay(display);
exit(0);
}
}
}
Here is a program that uses more of the aforementioned routines.
#include <X11/cursorfont.h>
#include <stdio.h>
#include <X11/Xlib.h>
main (argc, argv)
char *argv[];
int argc;
{
Display *display;
Window win1;
XEvent event;
XSetWindowAttributes attributes;
Cursor cursor_shape;
XFontStruct *fontinfo;
GC gr_context1, gr_context2;
XGCValues gr_values;
int screen;
int i;
display = XOpenDisplay(NULL);
screen = XDefaultScreen(display);
attributes.background_pixel = XWhitePixel(display,screen);
attributes.border_pixel = XBlackPixel(display,screen);
win1= XCreateWindow( display,XRootWindow(display,screen),0,200,
XDisplayWidth(display,screen)-400,
XDisplayHeight(display,screen)-400,5, 6,
InputOutput, XDefaultVisual(display,screen),
CWBackPixel| CWBorderPixel, &attributes);
XSelectInput(display,win1,ExposureMask | KeyPressMask) ;
gr_values.function = GXcopy;
gr_values.plane_mask = AllPlanes;
gr_values.foreground = BlackPixel(display,screen);
gr_values.background = WhitePixel(display,screen);
gr_context1=XCreateGC(display,win1,
GCFunction | GCPlaneMask | GCForeground | GCBackground,
&gr_values);
gr_values.function = GXxor;
gr_values.foreground = WhitePixel(display,screen);
gr_values.background = BlackPixel(display,screen);
gr_context2=XCreateGC(display,win1,
GCFunction | GCPlaneMask | GCForeground | GCBackground,
&gr_values);
fontinfo = XLoadQueryFont(display,"6x10");
cursor_shape=XCreateFontCursor(display,XC_heart);
XDefineCursor(display,win1,cursor_shape);
XSetFont(display,gr_context1,fontinfo->fid);
XSetFont(display,gr_context2,fontinfo->fid);
XMapWindow(display,win1);
while(1){
XNextEvent(display,&event);
switch(event.type){
case Expose:
XClearWindow(display,win1);
XDrawString(display,win1,gr_context1,50,50,"Hello",5);
XDrawImageString(display,win1,gr_context2,20,20,"Hello",5);
XFillRectangle(display,win1,gr_context1,150,150,111,111);
XFillRectangle(display,win1,gr_context2,200,180,111,111);
break;
case KeyPress:
XCloseDisplay(display);
exit(0);
}
}
}