Trying to use an xlib GXxor

Alan Corey alan01346 at gmail.com
Mon Feb 6 18:12:41 UTC 2017


OK, line spins inside a box until ctrl-c in the controlling terminal.
I wanted to get a sense of how fast or how much CPU it was going to
take, this takes under 4% CPU on a Raspberry Pi 3B.  I only got my
first xlib program working a couple days ago.

My makefile has paths for both a Pi with Raspbian and OpenBSD I think:

spi: spi.c
        gcc -O -Wall -g -o spi -I/usr/X11R6/include/ -I/usr/include \
        -L/usr/X11R6/lib/ -lX11 spi.c

/*
  My first recreational X activity. Based on Tronche's tutorial.
*/

#include <X11/Xlib.h> // Every Xlib program must include this
#include <assert.h>   // I include this to test return values the lazy way
#include <unistd.h>   // So we got the profile for 10 seconds
#include <stdio.h>
#include <stdlib.h> // for exit
#include <stdint.h> // for int16_t
#include <time.h>  // for nanosleep

#define WWIDTH 320
#define WHEIGHT 240

Display *dpy;
int blackcolor,whitecolor;
Window w;
GC gc;
XEvent e; // a struct
uint16_t npoints;
uint16_t ends[WWIDTH+WHEIGHT][4]; // endpoints of lines
useconds_t us = 1000;  // how long to leave a line on screen


void doloop(void) {  // ctrl-c in terminal to stop
  int i = 0;
  while(1) {
    XDrawLine(dpy,w,gc,ends[i][0],ends[i][1],ends[i][2],ends[i][3]);
    XFlush(dpy);
    usleep(us);
    XDrawLine(dpy,w,gc,ends[i][0],ends[i][1],ends[i][2],ends[i][3]);
    XFlush(dpy);
    i++;
    if (i > npoints)
      i = 0;
  }
}

/*
  Filling ends[][] with endpoints of lines.  Start with a horizontal line,
  turn it 1 pixel CW, save the endpoints, going just inside WWIDTH.  At the
  top work across staying 1 pixel inside WHEIGHT.  At the right side work
  down to the midpoint.  The line effectively flips over.  I expected it to
  rotate CW, not CCW.  Oh well.
*/
void fillarray(void) {  // define endpoints of lines
  uint16_t yh = WHEIGHT/2;
  uint16_t x,y;
  int i,j;
  y = yh;
  for (i=0; i<yh; i++) {  // up the left side
    ends[i][0] = 1;  // x
    ends[i][1] = yh+i; // y
    ends[i][2] = WWIDTH - 1; // inside right side
    ends[i][3] = yh-i; // progresssing down lower right side
  }
  j = i+1;  // position in array
  for (x=1; x<WWIDTH; x++) {  // across the top
    ends[j][0] = x;
    ends[j][1] = WHEIGHT -1;
    ends[j][2] = WWIDTH - x;  // working right-left in lr corner
    ends[j][3] = 1;
    j++;
  }
  for (y=0; y<yh; y++) {  // down the right
    ends[j][0] = WWIDTH-1;
    ends[j][1] = WHEIGHT-y;
    ends[j][2] = 1;
    ends[j][3] = y;
    j++;
  }
  npoints = j-1;
}

int main(void) {
  fillarray();
  dpy = XOpenDisplay(NULL);
  assert(dpy);  // be sure we've got dpy
  blackcolor = BlackPixel(dpy, DefaultScreen(dpy));
  whitecolor = WhitePixel(dpy, DefaultScreen(dpy));
  w = XCreateSimpleWindow(dpy,DefaultRootWindow(dpy),0,0,WWIDTH,WHEIGHT,0,\
    blackcolor,whitecolor); XSelectInput(dpy,w,StructureNotifyMask); //
  XMapWindow(dpy,w); gc = XCreateGC(dpy,w,0,NULL);
  XSetFunction(dpy,gc,GXxor); // sets xor mode
  XSetForeground(dpy,gc,whitecolor);
  // MapNotify wait loop, wait for a MapNotify event before drawing
  // confirms that XMapWindow worked.
  for (;;) {
    XNextEvent(dpy,&e);  // fetch an event
    if (e.type == MapNotify)  // done when we find one
      break;
  }
  doloop();
  return 0;
}


More information about the xorg mailing list