wmaker: Add keyboard shortcuts for keeping window on top or at bottom.
[wmaker-crm.git] / util / wmiv.c
blobe99e3e3fb5ae32fd875a426682c93e71e846f596
1 /*
2 * Window Maker window manager
4 * Copyright (c) 2014 Window Maker Team - David Maciejak
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #if !defined(_GNU_SOURCE)
22 #define _GNU_SOURCE
23 #endif
25 #include <X11/keysym.h>
26 #include <X11/XKBlib.h>
27 #include <X11/Xatom.h>
28 #include <X11/Xlib.h>
29 #include "wraster.h"
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <dirent.h>
34 #include <limits.h>
35 #include <unistd.h>
36 #include <sys/stat.h>
37 #include <getopt.h>
38 #include "config.h"
40 #ifdef HAVE_EXIF
41 #include <libexif/exif-data.h>
42 #endif
44 #ifdef HAVE_PTHREAD
45 #include <pthread.h>
46 #endif
48 #ifdef USE_XPM
49 extern int XpmCreatePixmapFromData(Display *, Drawable, char **, Pixmap *, Pixmap *, void *);
50 /* this is the icon from eog project
51 git.gnome.org/browse/eog
53 #include "wmiv.h"
54 #endif
56 #define DEBUG 0
57 #define FILE_SEPARATOR '/'
59 Display *dpy;
60 Window win;
61 RContext *ctx;
62 RImage *img;
63 Pixmap pix;
65 const char *APPNAME = "wmiv";
66 int APPVERSION_MAJOR = 0;
67 int APPVERSION_MINOR = 7;
68 int NEXT = 0;
69 int PREV = 1;
70 float zoom_factor = 0;
71 int max_width = 0;
72 int max_height = 0;
74 Bool fullscreen_flag = False;
75 Bool focus = False;
76 Bool back_from_fullscreen = False;
78 #ifdef HAVE_PTHREAD
79 Bool diaporama_flag = False;
80 int diaporama_delay = 5;
81 pthread_t tid = 0;
82 #endif
83 XTextProperty title_property;
84 XTextProperty icon_property;
85 unsigned current_index = 1;
86 unsigned max_index = 1;
88 RColor lightGray;
89 RColor darkGray;
90 RColor black;
91 RColor red;
93 typedef struct link link_t;
94 struct link {
95 const void *data;
96 link_t *prev;
97 link_t *next;
100 typedef struct linked_list {
101 int count;
102 link_t *first;
103 link_t *last;
104 } linked_list_t;
106 linked_list_t list;
107 link_t *current_link;
111 load_oriented_image: used to load an image and optionally
112 get its orientation if libexif is available
113 return the image on success, NULL on failure
115 RImage *load_oriented_image(RContext *context, const char *file, int index)
117 RImage *image;
118 #ifdef HAVE_EXIF
119 int orientation = 0;
120 #endif
121 image = RLoadImage(context, file, index);
122 if (!image)
123 return NULL;
124 #ifdef HAVE_EXIF
125 ExifData *exifData = exif_data_new_from_file(file);
126 if (exifData) {
127 ExifByteOrder byteOrder = exif_data_get_byte_order(exifData);
128 ExifEntry *exifEntry = exif_data_get_entry(exifData, EXIF_TAG_ORIENTATION);
129 if (exifEntry)
130 orientation = exif_get_short(exifEntry->data, byteOrder);
132 exif_data_free(exifData);
136 0th Row 0th Column
137 1 top left side
138 2 top right side
139 3 bottom right side
140 4 bottom left side
141 5 left side top
142 6 right side top
143 7 right side bottom
144 8 left side bottom
147 if (image && (orientation > 1)) {
148 RImage *tmp = NULL;
149 switch (orientation) {
150 case 2:
151 tmp = RFlipImage(image, RHorizontalFlip);
152 break;
153 case 3:
154 tmp = RRotateImage(image, 180);
155 break;
156 case 4:
157 tmp = RFlipImage(image, RVerticalFlip);
158 break;
159 case 5: {
160 RImage *tmp2;
161 tmp2 = RFlipImage(image, RVerticalFlip);
162 if (tmp2) {
163 tmp = RRotateImage(tmp2, 90);
164 RReleaseImage(tmp2);
167 break;
168 case 6:
169 tmp = RRotateImage(image, 90);
170 break;
171 case 7: {
172 RImage *tmp2;
173 tmp2 = RFlipImage(image, RVerticalFlip);
174 if (tmp2) {
175 tmp = RRotateImage(tmp2, 270);
176 RReleaseImage(tmp2);
179 break;
180 case 8:
181 tmp = RRotateImage(image, 270);
182 break;
184 if (tmp) {
185 RReleaseImage(image);
186 image = tmp;
189 #endif
190 return image;
194 change_title: used to change window title
195 return EXIT_SUCCESS on success, 1 on failure
197 int change_title(XTextProperty *prop, char *filename)
199 char *combined_title = NULL;
200 if (!asprintf(&combined_title, "%s - %u/%u - %s", APPNAME, current_index, max_index, filename))
201 if (!asprintf(&combined_title, "%s - %u/%u", APPNAME, current_index, max_index))
202 return EXIT_FAILURE;
203 XStringListToTextProperty(&combined_title, 1, prop);
204 XSetWMName(dpy, win, prop);
205 if (prop->value)
206 XFree(prop->value);
207 free(combined_title);
208 return EXIT_SUCCESS;
212 rescale_image: used to rescale the current image based on the screen size
213 return EXIT_SUCCESS on success
215 int rescale_image(void)
217 long final_width = img->width;
218 long final_height = img->height;
220 /* check if there is already a zoom factor applied */
221 if (zoom_factor != 0) {
222 final_width = img->width + (int)(img->width * zoom_factor);
223 final_height = img->height + (int)(img->height * zoom_factor);
225 if ((max_width < final_width) || (max_height < final_height)) {
226 long val = 0;
227 if (final_width > final_height) {
228 val = final_height * max_width / final_width;
229 final_width = final_width * val / final_height;
230 final_height = val;
231 if (val > max_height) {
232 val = final_width * max_height / final_height;
233 final_height = final_height * val / final_width;
234 final_width = val;
236 } else {
237 val = final_width * max_height / final_height;
238 final_height = final_height * val / final_width;
239 final_width = val;
240 if (val > max_width) {
241 val = final_height * max_width / final_width;
242 final_width = final_width * val / final_height;
243 final_height = val;
247 if ((final_width != img->width) || (final_height != img->height)) {
248 RImage *old_img = img;
249 img = RScaleImage(img, final_width, final_height);
250 if (!img) {
251 img = old_img;
252 return EXIT_FAILURE;
254 RReleaseImage(old_img);
256 if (!RConvertImage(ctx, img, &pix)) {
257 fprintf(stderr, "%s\n", RMessageForError(RErrorCode));
258 return EXIT_FAILURE;
260 return EXIT_SUCCESS;
264 maximize_image: find the best image size for the current display
265 return EXIT_SUCCESS on success
267 int maximize_image(void)
269 rescale_image();
270 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0,
271 img->width, img->height, max_width/2-img->width/2, max_height/2-img->height/2);
272 return EXIT_SUCCESS;
276 merge_with_background: merge the current image with with a checkboard background
277 return EXIT_SUCCESS on success, 1 on failure
279 int merge_with_background(RImage *i)
281 if (i) {
282 RImage *back;
283 back = RCreateImage(i->width, i->height, True);
284 if (back) {
285 int opaq = 255;
286 int x = 0, y = 0;
288 RFillImage(back, &lightGray);
289 for (x = 0; x <= i->width; x += 8) {
290 if (x/8 % 2)
291 y = 8;
292 else
293 y = 0;
294 for (; y <= i->height; y += 16)
295 ROperateRectangle(back, RAddOperation, x, y, x+8, y+8, &darkGray);
298 RCombineImagesWithOpaqueness(i, back, opaq);
299 RReleaseImage(back);
300 return EXIT_SUCCESS;
303 return EXIT_FAILURE;
307 turn_image: rotate the image by the angle passed
308 return EXIT_SUCCESS on success, EXIT_FAILURE on failure
310 int turn_image(float angle)
312 RImage *tmp;
314 if (!img)
315 return EXIT_FAILURE;
317 tmp = RRotateImage(img, angle);
318 if (!tmp)
319 return EXIT_FAILURE;
321 if (!fullscreen_flag) {
322 if (img->width != tmp->width || img->height != tmp->height)
323 XResizeWindow(dpy, win, tmp->width, tmp->height);
326 RReleaseImage(img);
327 img = tmp;
329 rescale_image();
330 if (!fullscreen_flag) {
331 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, img->width, img->height, 0, 0);
332 } else {
333 XClearWindow(dpy, win);
334 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0,
335 img->width, img->height, max_width/2-img->width/2, max_height/2-img->height/2);
338 return EXIT_SUCCESS;
342 turn_image_right: rotate the image by 90 degree
343 return EXIT_SUCCESS on success, EXIT_FAILURE on failure
345 int turn_image_right(void)
347 return turn_image(90.0);
351 turn_image_left: rotate the image by -90 degree
352 return EXIT_SUCCESS on success, 1 on failure
354 int turn_image_left(void)
356 return turn_image(-90.0);
360 draw_failed_image: create a red crossed image to indicate an error loading file
361 return the image on success, NULL on failure
364 RImage *draw_failed_image(void)
366 RImage *failed_image = NULL;
367 XWindowAttributes attr;
369 if (win && (XGetWindowAttributes(dpy, win, &attr) >= 0))
370 failed_image = RCreateImage(attr.width, attr.height, False);
371 else
372 failed_image = RCreateImage(50, 50, False);
373 if (!failed_image)
374 return NULL;
376 RFillImage(failed_image, &black);
377 ROperateLine(failed_image, RAddOperation, 0, 0, failed_image->width, failed_image->height, &red);
378 ROperateLine(failed_image, RAddOperation, 0, failed_image->height, failed_image->width, 0, &red);
380 return failed_image;
384 full_screen: sending event to the window manager to switch from/to full screen mode
385 return EXIT_SUCCESS on success, 1 on failure
387 int full_screen(void)
389 XEvent xev;
391 Atom wm_state = XInternAtom(dpy, "_NET_WM_STATE", True);
392 Atom fullscreen = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", True);
393 long mask = SubstructureNotifyMask;
395 if (fullscreen_flag) {
396 fullscreen_flag = False;
397 zoom_factor = 0;
398 back_from_fullscreen = True;
399 } else {
400 fullscreen_flag = True;
401 zoom_factor = 1000;
404 memset(&xev, 0, sizeof(xev));
405 xev.type = ClientMessage;
406 xev.xclient.display = dpy;
407 xev.xclient.window = win;
408 xev.xclient.message_type = wm_state;
409 xev.xclient.format = 32;
410 xev.xclient.data.l[0] = fullscreen_flag;
411 xev.xclient.data.l[1] = fullscreen;
413 if (!XSendEvent(dpy, DefaultRootWindow(dpy), False, mask, &xev)) {
414 fprintf(stderr, "Error: sending fullscreen event to xserver\n");
415 return EXIT_FAILURE;
417 return EXIT_SUCCESS;
421 zoom_in_out: apply a zoom factor on the current image
422 arg: 1 to zoom in, 0 to zoom out
423 return EXIT_SUCCESS on success, 1 on failure
425 int zoom_in_out(int z)
427 RImage *old_img = img;
428 RImage *tmp = load_oriented_image(ctx, current_link->data, 0);
429 if (!tmp)
430 return EXIT_FAILURE;
432 if (z) {
433 zoom_factor += 0.2;
434 img = RScaleImage(tmp, tmp->width + (int)(tmp->width * zoom_factor),
435 tmp->height + (int)(tmp->height * zoom_factor));
436 if (!img) {
437 img = old_img;
438 return EXIT_FAILURE;
440 } else {
441 zoom_factor -= 0.2;
442 int new_width = tmp->width + (int) (tmp->width * zoom_factor);
443 int new_height = tmp->height + (int)(tmp->height * zoom_factor);
444 if ((new_width <= 0) || (new_height <= 0)) {
445 zoom_factor += 0.2;
446 RReleaseImage(tmp);
447 return EXIT_FAILURE;
449 img = RScaleImage(tmp, new_width, new_height);
450 if (!img) {
451 img = old_img;
452 return EXIT_FAILURE;
455 RReleaseImage(old_img);
456 RReleaseImage(tmp);
457 XFreePixmap(dpy, pix);
459 merge_with_background(img);
460 if (!RConvertImage(ctx, img, &pix)) {
461 fprintf(stderr, "%s\n", RMessageForError(RErrorCode));
462 return EXIT_FAILURE;
464 XResizeWindow(dpy, win, img->width, img->height);
465 return EXIT_SUCCESS;
469 zoom_in: transitional fct used to call zoom_in_out with zoom in flag
470 return EXIT_SUCCESS on success, 1 on failure
472 int zoom_in(void)
474 return zoom_in_out(1);
478 zoom_out: transitional fct used to call zoom_in_out with zoom out flag
479 return EXIT_SUCCESS on success, 1 on failure
481 int zoom_out(void)
483 return zoom_in_out(0);
487 change_image: load previous or next image
488 arg: way which could be PREV or NEXT constant
489 return EXIT_SUCCESS on success, 1 on failure
491 int change_image(int way)
493 if (img && current_link) {
494 int old_img_width = img->width;
495 int old_img_height = img->height;
497 RReleaseImage(img);
499 if (way == NEXT) {
500 current_link = current_link->next;
501 current_index++;
502 } else {
503 current_link = current_link->prev;
504 current_index--;
506 if (current_link == NULL) {
507 if (way == NEXT) {
508 current_link = list.first;
509 current_index = 1;
510 } else {
511 current_link = list.last;
512 current_index = max_index;
515 if (DEBUG)
516 fprintf(stderr, "current file is> %s\n", (char *)current_link->data);
517 img = load_oriented_image(ctx, current_link->data, 0);
519 if (!img) {
520 fprintf(stderr, "Error: %s %s\n", (char *)current_link->data,
521 RMessageForError(RErrorCode));
522 img = draw_failed_image();
523 } else {
524 merge_with_background(img);
526 rescale_image();
527 if (!fullscreen_flag) {
528 if ((old_img_width != img->width) || (old_img_height != img->height))
529 XResizeWindow(dpy, win, img->width, img->height);
530 else
531 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, img->width, img->height, 0, 0);
532 change_title(&title_property, (char *)current_link->data);
533 } else {
534 XClearWindow(dpy, win);
535 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0,
536 img->width, img->height, max_width/2-img->width/2, max_height/2-img->height/2);
538 return EXIT_SUCCESS;
540 return EXIT_FAILURE;
543 #ifdef HAVE_PTHREAD
545 diaporama: send a xevent to display the next image at every delay set to diaporama_delay
546 arg: not used
547 return void
549 void *diaporama(void *arg)
551 (void) arg;
553 XKeyEvent event;
554 event.display = dpy;
555 event.window = win;
556 event.root = DefaultRootWindow(dpy);
557 event.subwindow = None;
558 event.time = CurrentTime;
559 event.x = 1;
560 event.y = 1;
561 event.x_root = 1;
562 event.y_root = 1;
563 event.same_screen = True;
564 event.keycode = XKeysymToKeycode(dpy, XK_Right);
565 event.state = 0;
566 event.type = KeyPress;
568 while (diaporama_flag) {
569 int r;
570 r = XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
571 if (!r)
572 fprintf(stderr, "Error sending event\n");
573 XFlush(dpy);
574 /* default sleep time between moving to next image */
575 sleep(diaporama_delay);
577 tid = 0;
578 return arg;
580 #endif
583 linked_list_init: init the linked list
585 void linked_list_init(linked_list_t *list)
587 list->first = list->last = 0;
588 list->count = 0;
592 linked_list_add: add an element to the linked list
593 return EXIT_SUCCESS on success, 1 otherwise
595 int linked_list_add(linked_list_t *list, const void *data)
597 link_t *link;
599 /* calloc sets the "next" field to zero. */
600 link = calloc(1, sizeof(link_t));
601 if (!link) {
602 fprintf(stderr, "Error: memory allocation failed\n");
603 return EXIT_FAILURE;
605 link->data = data;
606 if (list->last) {
607 /* Join the two final links together. */
608 list->last->next = link;
609 link->prev = list->last;
610 list->last = link;
611 } else {
612 list->first = link;
613 list->last = link;
615 list->count++;
616 return EXIT_SUCCESS;
620 linked_list_free: deallocate the whole linked list
622 void linked_list_free(linked_list_t *list)
624 link_t *link;
625 link_t *next;
626 for (link = list->first; link; link = next) {
627 /* Store the next value so that we don't access freed memory. */
628 next = link->next;
629 if (link->data)
630 free((char *)link->data);
631 free(link);
636 connect_dir: list and sort by name all files from a given directory
637 arg: the directory path that contains images, the linked list where to add the new file refs
638 return: the first argument of the list or NULL on failure
640 link_t *connect_dir(char *dirpath, linked_list_t *li)
642 struct dirent **dir;
643 int dv, idx;
644 char path[PATH_MAX] = "";
646 if (!dirpath)
647 return NULL;
649 dv = scandir(dirpath, &dir, 0, alphasort);
650 if (dv < 0) {
651 /* maybe it's a file */
652 struct stat stDirInfo;
653 if (lstat(dirpath, &stDirInfo) == 0) {
654 linked_list_add(li, strdup(dirpath));
655 return li->first;
656 } else {
657 return NULL;
660 for (idx = 0; idx < dv; idx++) {
661 struct stat stDirInfo;
662 if (dirpath[strlen(dirpath)-1] == FILE_SEPARATOR)
663 snprintf(path, PATH_MAX, "%s%s", dirpath, dir[idx]->d_name);
664 else
665 snprintf(path, PATH_MAX, "%s%c%s", dirpath, FILE_SEPARATOR, dir[idx]->d_name);
667 free(dir[idx]);
668 if ((lstat(path, &stDirInfo) == 0) && !S_ISDIR(stDirInfo.st_mode))
669 linked_list_add(li, strdup(path));
671 free(dir);
672 return li->first;
676 main
678 int main(int argc, char **argv)
680 int option = -1;
681 RContextAttributes attr;
682 XEvent e;
683 KeySym keysym;
684 char *reading_filename = "";
685 int screen, file_i;
686 int quit = 0;
687 XClassHint *class_hints;
688 XSizeHints *size_hints;
689 XWMHints *win_hints;
690 #ifdef USE_XPM
691 Pixmap icon_pixmap, icon_shape;
692 #endif
693 class_hints = XAllocClassHint();
694 if (!class_hints) {
695 fprintf(stderr, "Error: failure allocating memory\n");
696 return EXIT_FAILURE;
698 class_hints->res_name = (char *)APPNAME;
699 class_hints->res_class = "default";
701 /* init colors */
702 lightGray.red = lightGray.green = lightGray.blue = 211;
703 darkGray.red = darkGray.green = darkGray.blue = 169;
704 lightGray.alpha = darkGray.alpha = 1;
705 black.red = black.green = black.blue = 0;
706 red.red = 255;
707 red.green = red.blue = 0;
709 static struct option long_options[] = {
710 {"version", no_argument, 0, 'v'},
711 {"help", no_argument, 0, 'h'},
712 {0, 0, 0, 0}
714 int option_index = 0;
716 option = getopt_long (argc, argv, "hv", long_options, &option_index);
717 if (option != -1) {
718 switch (option) {
719 case 'h':
720 printf("Usage: %s [image(s)|directory]\n"
721 "Options:\n"
722 " -h, --help print this help text\n"
723 " -v, --version print version\n"
724 "Keys:\n"
725 " [+] zoom in\n"
726 " [-] zoom out\n"
727 " [Esc] actual size\n"
728 #ifdef HAVE_PTHREAD
729 " [D] launch diaporama mode\n"
730 #endif
731 " [L] rotate image on the left\n"
732 " [Q] quit\n"
733 " [R] rotate image on the right\n"
734 " [â–¸] next image\n"
735 " [â—‚] previous image\n"
736 " [â–´] first image\n"
737 " [â–¾] last image\n",
738 argv[0]);
739 return EXIT_SUCCESS;
740 case 'v':
741 fprintf(stderr, "%s version %d.%d\n", APPNAME, APPVERSION_MAJOR, APPVERSION_MINOR);
742 return EXIT_SUCCESS;
743 case '?':
744 return EXIT_FAILURE;
748 linked_list_init(&list);
750 dpy = XOpenDisplay(NULL);
751 if (!dpy) {
752 fprintf(stderr, "Error: can't open display");
753 linked_list_free(&list);
754 return EXIT_FAILURE;
757 screen = DefaultScreen(dpy);
758 max_width = DisplayWidth(dpy, screen);
759 max_height = DisplayHeight(dpy, screen);
761 attr.flags = RC_RenderMode | RC_ColorsPerChannel;
762 attr.render_mode = RDitheredRendering;
763 attr.colors_per_channel = 4;
764 ctx = RCreateContext(dpy, DefaultScreen(dpy), &attr);
766 if (argc < 2) {
767 argv[1] = ".";
768 argc = 2;
771 for (file_i = 1; file_i < argc; file_i++) {
772 current_link = connect_dir(argv[file_i], &list);
773 if (current_link) {
774 reading_filename = (char *)current_link->data;
775 max_index = list.count;
779 img = load_oriented_image(ctx, reading_filename, 0);
781 if (!img) {
782 fprintf(stderr, "Error: %s %s\n", reading_filename, RMessageForError(RErrorCode));
783 img = draw_failed_image();
784 if (!current_link)
785 return EXIT_FAILURE;
788 merge_with_background(img);
789 rescale_image();
791 if (DEBUG)
792 fprintf(stderr, "display size: %dx%d\n", max_width, max_height);
794 win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
795 img->width, img->height, 0, 0, BlackPixel(dpy, screen));
796 XSelectInput(dpy, win, KeyPressMask|StructureNotifyMask|ExposureMask|ButtonPressMask|FocusChangeMask);
798 size_hints = XAllocSizeHints();
799 if (!size_hints) {
800 fprintf(stderr, "Error: failure allocating memory\n");
801 return EXIT_FAILURE;
803 size_hints->width = img->width;
804 size_hints->height = img->height;
806 Atom delWindow = XInternAtom(dpy, "WM_DELETE_WINDOW", 0);
807 XSetWMProtocols(dpy, win, &delWindow, 1);
808 change_title(&title_property, reading_filename);
810 win_hints = XAllocWMHints();
811 if (win_hints) {
812 win_hints->flags = StateHint|InputHint|WindowGroupHint;
814 #ifdef USE_XPM
815 if ((XpmCreatePixmapFromData(dpy, win, wmiv_xpm, &icon_pixmap, &icon_shape, NULL)) == 0) {
816 win_hints->flags |= IconPixmapHint|IconMaskHint|IconPositionHint;
817 win_hints->icon_pixmap = icon_pixmap;
818 win_hints->icon_mask = icon_shape;
819 win_hints->icon_x = 0;
820 win_hints->icon_y = 0;
822 #endif
823 win_hints->initial_state = NormalState;
824 win_hints->input = True;
825 win_hints->window_group = win;
826 XStringListToTextProperty((char **)&APPNAME, 1, &icon_property);
827 XSetWMProperties(dpy, win, NULL, &icon_property, argv, argc, size_hints, win_hints, class_hints);
828 if (icon_property.value)
829 XFree(icon_property.value);
830 XFree(win_hints);
831 XFree(class_hints);
832 XFree(size_hints);
835 XMapWindow(dpy, win);
836 XFlush(dpy);
837 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, img->width, img->height, 0, 0);
839 while (!quit) {
840 XNextEvent(dpy, &e);
841 if (e.type == ClientMessage) {
842 if (e.xclient.data.l[0] == delWindow)
843 quit = 1;
844 break;
846 if (e.type == FocusIn) {
847 focus = True;
848 continue;
850 if (e.type == FocusOut) {
851 focus = False;
852 continue;
854 if (!fullscreen_flag && (e.type == Expose)) {
855 XExposeEvent xev = e.xexpose;
856 if (xev.count == 0)
857 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, img->width, img->height, 0, 0);
858 continue;
860 if (!fullscreen_flag && e.type == ConfigureNotify) {
861 XConfigureEvent xce = e.xconfigure;
862 if (xce.width != img->width || xce.height != img->height) {
863 RImage *old_img = img;
864 img = load_oriented_image(ctx, current_link->data, 0);
865 if (!img) {
866 /* keep the old img and window size */
867 img = old_img;
868 XResizeWindow(dpy, win, img->width, img->height);
869 } else {
870 RImage *tmp2;
871 if (!back_from_fullscreen)
872 /* manually resized window */
873 tmp2 = RScaleImage(img, xce.width, xce.height);
874 else {
875 /* back from fullscreen mode, maybe img was rotated */
876 tmp2 = img;
877 back_from_fullscreen = False;
878 XClearWindow(dpy, win);
880 merge_with_background(tmp2);
881 if (RConvertImage(ctx, tmp2, &pix)) {
882 RReleaseImage(old_img);
883 img = RCloneImage(tmp2);
884 RReleaseImage(tmp2);
885 change_title(&title_property, (char *)current_link->data);
886 XSync(dpy, True);
887 XResizeWindow(dpy, win, img->width, img->height);
888 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0,
889 img->width, img->height, 0, 0);
894 continue;
896 if (fullscreen_flag && e.type == ConfigureNotify) {
897 maximize_image();
898 continue;
900 if (e.type == ButtonPress) {
901 switch (e.xbutton.button) {
902 case Button1: {
903 if (focus) {
904 if (img && (e.xbutton.x > img->width/2))
905 change_image(NEXT);
906 else
907 change_image(PREV);
910 break;
911 case Button4:
912 zoom_in();
913 break;
914 case Button5:
915 zoom_out();
916 break;
917 case 8:
918 change_image(PREV);
919 break;
920 case 9:
921 change_image(NEXT);
922 break;
924 continue;
926 if (e.type == KeyPress) {
927 keysym = XkbKeycodeToKeysym(dpy, e.xkey.keycode, 0, e.xkey.state & ShiftMask?1:0);
928 #ifdef HAVE_PTHREAD
929 if (keysym != XK_Right)
930 diaporama_flag = False;
931 #endif
932 switch (keysym) {
933 case XK_Right:
934 change_image(NEXT);
935 break;
936 case XK_Left:
937 change_image(PREV);
938 break;
939 case XK_Up:
940 if (current_link) {
941 current_link = list.last;
942 change_image(NEXT);
944 break;
945 case XK_Down:
946 if (current_link) {
947 current_link = list.first;
948 change_image(PREV);
950 break;
951 #ifdef HAVE_PTHREAD
952 case XK_F5:
953 case XK_d:
954 if (!tid) {
955 if (current_link && !diaporama_flag) {
956 diaporama_flag = True;
957 pthread_create(&tid, NULL, &diaporama, NULL);
958 } else {
959 fprintf(stderr, "Can't use diaporama mode\n");
962 break;
963 #endif
964 case XK_q:
965 quit = 1;
966 break;
967 case XK_Escape:
968 if (!fullscreen_flag) {
969 zoom_factor = -0.2;
970 /* zoom_in will increase the zoom factor by 0.2 */
971 zoom_in();
972 } else {
973 /* we are in fullscreen mode already, want to return to normal size */
974 full_screen();
976 break;
977 case XK_plus:
978 zoom_in();
979 break;
980 case XK_minus:
981 zoom_out();
982 break;
983 case XK_F11:
984 case XK_f:
985 full_screen();
986 break;
987 case XK_r:
988 turn_image_right();
989 break;
990 case XK_l:
991 turn_image_left();
992 break;
998 if (img)
999 RReleaseImage(img);
1000 if (pix)
1001 XFreePixmap(dpy, pix);
1002 #ifdef USE_XPM
1003 if (icon_pixmap)
1004 XFreePixmap(dpy, icon_pixmap);
1005 if (icon_shape)
1006 XFreePixmap(dpy, icon_shape);
1007 #endif
1008 linked_list_free(&list);
1009 RDestroyContext(ctx);
1010 RShutdown();
1011 XCloseDisplay(dpy);
1012 return EXIT_SUCCESS;