Last active
July 13, 2018 14:55
-
-
Save twaik/3ea63ce326d2874caaefa745201ef79f to your computer and use it in GitHub Desktop.
Drawing pixmaps using libX11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include <unistd.h> | |
#include <string.h> | |
#include <math.h> | |
#include <sys/ipc.h> | |
#include <sys/shm.h> | |
#include <X11/Xlib.h> | |
#include <X11/extensions/XShm.h> | |
XImage *CreatePixmapImage(Display *display, Visual *visual, void ** data, int width, int height) { | |
static XShmSegmentInfo shminfo; | |
XImage *image = XShmCreateImage(display, visual, 24, ZPixmap, 0, &shminfo, width, height); | |
shminfo.shmid = shmget(IPC_PRIVATE, image->bytes_per_line * image->height,IPC_CREAT|0777); | |
shminfo.shmaddr = image->data = *data = shmat(shminfo.shmid, 0, 0); | |
shminfo.readOnly = False; | |
XShmAttach(display, &shminfo); | |
return image; | |
} | |
void redrawImage(unsigned char *image, int width, int height) { | |
int i, j; | |
static double time = 0; | |
//unsigned char *image32=(unsigned char *)malloc(width*height*4); | |
unsigned char *p=image; | |
double ttime = sin(time)*256; | |
for(i=0; i<width; i++) | |
{ | |
for(j=0; j<height; j++) | |
{ | |
*p++=i%256; // blue | |
*p++=j%256; // green | |
*p++=ttime; // red | |
p++; | |
} | |
} | |
time+=0.06; | |
if (time >= M_PI) time = 0; | |
} | |
void processEvent(Display *display, Window window, Visual* visual, int width, int height) { | |
XEvent ev; | |
XImage *ximage; | |
if (XPending(display)) { | |
XNextEvent(display, &ev); | |
switch(ev.type) { | |
case Expose: break; | |
case ButtonPress: | |
case KeyPress: exit(0); break; | |
} | |
} | |
} | |
int main(int argc, char **argv) | |
{ | |
int width=512, height=512; | |
Display *display=XOpenDisplay(NULL); | |
Visual *visual=DefaultVisual(display, 0); | |
Window window=XCreateSimpleWindow(display, RootWindow(display, 0), 0, 0, width, height, 1, 0, 0); | |
XImage *ximage; | |
uint8_t *image; | |
if(visual->class!=TrueColor) | |
{ | |
fprintf(stderr, "Cannot handle non true color visual ...\n"); | |
exit(1); | |
} | |
XSelectInput(display, window, ButtonPressMask|ExposureMask|KeyPressMask); | |
XMapWindow(display, window); | |
ximage = CreatePixmapImage(display, visual, (void**) &image, width, height); | |
while(1) | |
{ | |
processEvent(display, window, visual, width, height); | |
redrawImage(image, width, height); | |
XPutImage(display, window, DefaultGC(display, 0), ximage, 0, 0, 0, 0, width, height); | |
usleep(10000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment