Difference between revisions of "XLIB"

From Teknologisk videncenter
Jump to: navigation, search
(Created page with "=Links= *[https://tronche.com/gui/x/xlib/ xlib manual] Category:CCategory:LinuxCategory:X11")
 
m
Line 1: Line 1:
 +
X11lib C example:
 +
<source lang=c>
 +
// 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, "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;
 +
        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;
 +
}
 +
</source>
 
=Links=
 
=Links=
 
*[https://tronche.com/gui/x/xlib/ xlib manual]
 
*[https://tronche.com/gui/x/xlib/ xlib manual]
 
[[Category:C]][[Category:Linux]][[Category:X11]]
 
[[Category:C]][[Category:Linux]][[Category:X11]]

Revision as of 09:29, 5 December 2019

X11lib C example:

// 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, "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;
        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