void init_x_and_imlib(void) { disp = XOpenDisplay(NULL); if (!disp) eprintf("Can't open X display. It *is* running, yeah?"); vis = DefaultVisual(disp, DefaultScreen(disp)); depth = DefaultDepth(disp, DefaultScreen(disp)); cm = DefaultColormap(disp, DefaultScreen(disp)); root = RootWindow(disp, DefaultScreen(disp)); scr = ScreenOfDisplay(disp, DefaultScreen(disp)); xid_context = XUniqueContext(); #ifdef HAVE_LIBXINERAMA init_xinerama(); #endif /* HAVE_LIBXINERAMA */ imlib_context_set_display(disp); imlib_context_set_visual(vis); imlib_context_set_colormap(cm); imlib_context_set_color_modifier(NULL); imlib_context_set_progress_function(NULL); imlib_context_set_operation(IMLIB_OP_COPY); wmDeleteWindow = XInternAtom(disp, "WM_DELETE_WINDOW", False); /* Initialise random numbers */ srand(getpid() * time(NULL) % ((unsigned int) -1)); return; }
int main(int argc, char **argv) { Imlib_Image *im = NULL; char *file = NULL; int no = 1; const char *display_name = getenv("DISPLAY"); if (argc < 2) return 1; file = argv[no]; if (display_name == NULL) display_name = ":0"; disp = XOpenDisplay(display_name); if (disp == NULL) { fprintf(stderr, "Can't open display %s\n", display_name); return 1; } vis = DefaultVisual(disp, DefaultScreen(disp)); depth = DefaultDepth(disp, DefaultScreen(disp)); cm = DefaultColormap(disp, DefaultScreen(disp)); win = XCreateSimpleWindow(disp, DefaultRootWindow(disp), 0, 0, 10, 10, 0, 0, 0); XSelectInput(disp, win, ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | PointerMotionMask); imlib_context_set_display(disp); imlib_context_set_visual(vis); imlib_context_set_colormap(cm); imlib_context_set_progress_function(progress); imlib_context_set_progress_granularity(10); imlib_context_set_drawable(win); im = imlib_load_image(file); while (!im) { no++; if (no == argc) { fprintf(stderr, "Image format not available\n"); exit(0); } file = argv[no]; image_width = 0; im = imlib_load_image(file); imlib_context_set_image(im); } if (!im) { fprintf(stderr, "Image format not available\n"); exit(0); } for (;;) { int x, y, b, count, fdsize, xfd, timeout = 0; XEvent ev; static int zoom_mode = 0, zx, zy; static double zoom = 1.0; struct timeval tval; fd_set fdset; double t1; XFlush(disp); XNextEvent(disp, &ev); switch (ev.type) { case ButtonPress: b = ev.xbutton.button; x = ev.xbutton.x; y = ev.xbutton.y; if (b == 3) { zoom_mode = 1; zx = x; zy = y; imlib_context_set_drawable(pm); imlib_context_set_image(bg_im); imlib_context_set_anti_alias(0); imlib_context_set_dither(0); imlib_context_set_blend(0); imlib_render_image_part_on_drawable_at_size (0, 0, image_width, image_height, 0, 0, image_width, image_height); XSetWindowBackgroundPixmap(disp, win, pm); XClearWindow(disp, win); } break; case ButtonRelease: b = ev.xbutton.button; x = ev.xbutton.x; y = ev.xbutton.y; if (b == 3) zoom_mode = 0; if (b == 1) { no++; if (no == argc) no = argc - 1; file = argv[no]; image_width = 0; zoom = 1.0; zoom_mode = 0; imlib_context_set_image(im); imlib_free_image_and_decache(); im = imlib_load_image(file); while (!im) { no++; if (no == argc) exit(0); file = argv[no]; image_width = 0; im = imlib_load_image(file); } imlib_context_set_image(im); } if (b == 2) { no--; if (no == 0) no = 1; file = argv[no]; image_width = 0; zoom = 1.0; zoom_mode = 0; imlib_context_set_image(im); imlib_free_image_and_decache(); im = imlib_load_image(file); while (!im) { no--; if (no == 0) no = 1; file = argv[no]; image_width = 0; im = imlib_load_image(file); } imlib_context_set_image(im); } break; case MotionNotify: while (XCheckTypedWindowEvent(disp, win, MotionNotify, &ev)); x = ev.xmotion.x; y = ev.xmotion.y; if (zoom_mode) { int sx, sy, sw, sh, dx, dy, dw, dh; zoom = ((double)x - (double)zx) / 32.0; if (zoom < 0) zoom = 1.0 + ((zoom * 32.0) / ((double)(zx + 1))); else zoom += 1.0; if (zoom <= 0.0001) zoom = 0.0001; if (zoom > 1.0) { dx = 0; dy = 0; dw = image_width; dh = image_height; sx = zx - (zx / zoom); sy = zy - (zy / zoom); sw = image_width / zoom; sh = image_height / zoom; } else { dx = zx - (zx * zoom); dy = zy - (zy * zoom); dw = image_width * zoom; dh = image_height * zoom; sx = 0; sy = 0; sw = image_width; sh = image_height; } imlib_context_set_anti_alias(0); imlib_context_set_dither(0); imlib_context_set_blend(0); imlib_context_set_image(bg_im); imlib_render_image_part_on_drawable_at_size (sx, sy, sw, sh, dx, dy, dw, dh); XSetWindowBackgroundPixmap(disp, win, pm); XClearWindow(disp, win); XFlush(disp); timeout = 1; } default: break; } t1 = 0.2; tval.tv_sec = (long)t1; tval.tv_usec = (long)((t1 - ((double)tval.tv_sec)) * 1000000); xfd = ConnectionNumber(disp); fdsize = xfd + 1; FD_ZERO(&fdset); FD_SET(xfd, &fdset); if (timeout) count = select(fdsize, &fdset, NULL, NULL, &tval); else count = select(fdsize, &fdset, NULL, NULL, NULL); if (count < 0) { if ((errno == ENOMEM) || (errno == EINVAL) || (errno == EBADF)) exit(1); } else { if ((count == 0) && (timeout)) { int sx, sy, sw, sh, dx, dy, dw, dh; if (zoom > 1.0) { dx = 0; dy = 0; dw = image_width; dh = image_height; sx = zx - (zx / zoom); sy = zy - (zy / zoom); sw = image_width / zoom; sh = image_height / zoom; } else { dx = zx - (zx * zoom); dy = zy - (zy * zoom); dw = image_width * zoom; dh = image_height * zoom; sx = 0; sy = 0; sw = image_width; sh = image_height; } imlib_context_set_anti_alias(1); imlib_context_set_dither(1); imlib_context_set_blend(0); imlib_context_set_image(bg_im); imlib_render_image_part_on_drawable_at_size (sx, sy, sw, sh, dx, dy, dw, dh); XSetWindowBackgroundPixmap(disp, win, pm); XClearWindow(disp, win); XFlush(disp); timeout = 0; } } } return 0; }
int gib_imlib_load_image(Imlib_Image * im, char *filename) { Imlib_Load_Error err; imlib_context_set_progress_function(NULL); if (!filename) return (0); *im = imlib_load_image_with_error_return(filename, &err); if ((err) || (!im)) { /* Check error code */ switch (err) { case IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST: gib_weprintf("%s - File does not exist", filename); break; case IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY: gib_weprintf("%s - Directory specified for image filename", filename); break; case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ: gib_weprintf("%s - No read access to directory", filename); break; case IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT: gib_weprintf("%s - No Imlib2 loader for that file format", filename); break; case IMLIB_LOAD_ERROR_PATH_TOO_LONG: gib_weprintf("%s - Path specified is too long", filename); break; case IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT: gib_weprintf("%s - Path component does not exist", filename); break; case IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY: gib_weprintf("%s - Path component is not a directory", filename); break; case IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE: gib_weprintf("%s - Path points outside address space", filename); break; case IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS: gib_weprintf("%s - Too many levels of symbolic links", filename); break; case IMLIB_LOAD_ERROR_OUT_OF_MEMORY: gib_eprintf("While loading %s - Out of memory", filename); break; case IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS: gib_eprintf("While loading %s - Out of file descriptors", filename); break; case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE: gib_weprintf("%s - Cannot write to directory", filename); break; case IMLIB_LOAD_ERROR_OUT_OF_DISK_SPACE: gib_weprintf("%s - Cannot write - out of disk space", filename); break; case IMLIB_LOAD_ERROR_UNKNOWN: default: gib_weprintf ("While loading %s - Unknown error. Attempting to continue", filename); break; } return (0); } return (1); }