XLIB

From Teknologisk videncenter
Jump to: navigation, search

X11lib C example:

Note: You need X11 installed and the package libx11-dev Compile with:

 gcc sourcefile.c -o programname -lX11
// heth@mercantec.dk 2019
//Ihatov: WTFPL 2017
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#define WIDTH  512
#define HEIGHT 400
void drawpixel(Display* di, Window wi, GC gc, int x, int y, int color)
{
        XSetForeground(di, gc, color);
        XDrawPoint(di, wi, gc, x, y);
}
int main()
{
        //Open Display
        int xp,yp;
        int cou=0;
        Display *di = XOpenDisplay(getenv("DISPLAY"));
        if (di == NULL) {
                printf("Couldn't open display.\n");
                return -1;
        }

        //Create Window
        int const x = 0, y = 0, width = WIDTH, height = HEIGHT, border_width = 1;
        int sc    = DefaultScreen(di);
        Window ro = DefaultRootWindow(di);
        Window wi = XCreateSimpleWindow(di, ro, x, y, width, height, border_width,
                                BlackPixel(di, sc), WhitePixel(di, sc));
        XMapWindow(di, wi); //Make window visible
        XStoreName(di, wi, "X11 - Window sample"); // Set window title

        //Prepare the window for drawing
        GC gc = XCreateGC(di, ro, 0, NULL);

        //Select what events the window will listen to
        //Exposure generate event on empty screen - run once
        XSelectInput(di, wi, KeyPressMask | ExposureMask);
        XEvent ev;
        printf("Press a key to advance....\n<ESC> to quit.\n");

        int quit = 0;
        while (!quit) {
                //int a = XNextEvent(di, &ev);
                XNextEvent(di, &ev);
                cou++;
                for (xp=0;xp<WIDTH-1;xp++) {
                 for (yp=0; yp<HEIGHT-1;yp++) {
                  switch(cou%6) {
                    case 0: drawpixel(di,wi,gc,xp,yp,0x00ff00);
                           break;
                   case 1: drawpixel(di,wi,gc,xp,yp,0xff0000);
                           break;
                   case 2: drawpixel(di,wi,gc,xp,yp,0x0000ff);
                           break;
                   case 3: drawpixel(di,wi,gc,xp,yp,xp*yp*4);
                           break;
                   case 4: drawpixel(di,wi,gc,xp,yp,(xp<<12) | yp);
                           break;
                   case 5: drawpixel(di,wi,gc,xp,yp,(yp<<12) | xp);
                           break;
                   default: drawpixel(di,wi,gc,xp,yp,0xf0f0f0);
                  }
                 }
                }
                if (ev.type == KeyPress)
                        // Escape = quit program
                        printf("Keycode = %i, %c\n", ev.xkey.keycode ,ev.xkey.keycode );
                        if ( ev.xkey.keycode == 0x09 ) {
                                quit=1;
                                break;
                }
                if (ev.type == Expose) {
                        drawpixel(di, wi, gc, 10, 10, 0x00ff00); //green
                }
        }
        XFreeGC(di, gc);
        XDestroyWindow(di, wi);
        XCloseDisplay(di);
        return 0;
}

Links