scripts

misc scripts and tools
git clone git://git.2f30.org/scripts
Log | Files | Refs

see.c (2108B)


      1 #include <err.h>
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include <X11/Xlib.h>
      5 #include <cairo/cairo.h>
      6 #include <cairo/cairo-xlib.h>
      7 
      8 /* draw a png image inside an xlib window using cairo */
      9 
     10 int
     11 main(int argc, char *argv[])
     12 {
     13 	Display *dpy;
     14 	Window root, win;
     15 	XEvent e;
     16 	int scr;
     17 	cairo_surface_t *cs, *img;
     18 	cairo_t *c;
     19 	int winw, winh;
     20 	int imgw, imgh;
     21 	char *imgpath;
     22 
     23 	imgpath = argv[1];
     24 	if (imgpath == NULL) {
     25 		fprintf(stderr, "usage: see image.png\n");
     26 		exit(1);
     27 	}
     28 
     29 	/* load image and get dimensions */
     30 	img = cairo_image_surface_create_from_png(imgpath);
     31 	if (cairo_surface_status(img) != CAIRO_STATUS_SUCCESS)
     32 		errx(1, "load image");
     33 	imgw = cairo_image_surface_get_width(img);
     34 	imgh = cairo_image_surface_get_height(img);
     35 
     36 	/* init screen and get dimensions */
     37 	dpy = XOpenDisplay(NULL);
     38 	if (dpy == NULL)
     39 		errx(1, "open display");
     40 	scr = DefaultScreen(dpy);
     41 	winw = DisplayWidth(dpy, scr);
     42 	winh = DisplayHeight(dpy, scr);
     43 
     44 	/* resize window if img is smaller */
     45 #define MIN(x, y) ((x) < (y) ? (x) : (y))
     46 	winw = MIN(winw, imgw);
     47 	winh = MIN(winh, imgh);
     48 #undef MIN
     49 
     50 	/* create window */
     51 	root = RootWindow(dpy, scr);
     52 	win = XCreateSimpleWindow(dpy, root, 0, 0, winw, winh, 0,
     53 	    BlackPixel(dpy, scr), BlackPixel(dpy, scr));
     54 
     55 	/* register for events */
     56 	XSelectInput(dpy, win, ExposureMask | KeyPressMask);
     57 
     58 	/* name window */
     59 	XStoreName(dpy, win, imgpath);
     60 
     61 	/* draw window */
     62 	XMapWindow(dpy, win);
     63 
     64 	/* create surface in window */
     65 	cs = cairo_xlib_surface_create(dpy, win, DefaultVisual(dpy, scr),
     66 	    winw, winh);
     67 	c = cairo_create(cs);
     68 
     69 	/* put image on surface centered */
     70 	cairo_set_source_surface(c, img,
     71 	    (winw - imgw) / 2, (winh - imgh) / 2);
     72 
     73 	while (1) {
     74 		XNextEvent(dpy, &e);
     75 
     76 		switch (e.type) {
     77 		case Expose:
     78 			/* redraw if damaged */
     79 			if (e.xexpose.count < 1)
     80 				cairo_paint(c);
     81 			break;
     82 		case KeyPress:
     83 			/* quit on q pressed */
     84 			if (XLookupKeysym(&(e.xkey), 0) ==
     85 			    XStringToKeysym("q"))
     86 				goto out;
     87 			break;
     88 		}
     89 	}
     90 
     91 out:
     92 	/* free */
     93 	cairo_destroy(c);
     94 	cairo_surface_destroy(cs);
     95 	cairo_surface_destroy(img);
     96 	XCloseDisplay(dpy);
     97 
     98 	return 0;
     99 }