Skip to content
Snippets Groups Projects
winwidget.c 27.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
    /* winwidget.c
    
    Copyright (C) 1999-2003 Tom Gilbert.
    
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to
    deal in the Software without restriction, including without limitation the
    rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
    sell copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    
    The above copyright notice and this permission notice shall be included in
    all copies of the Software and its documentation and acknowledgment shall be
    given in the documentation and software packages that this Software was
    used.
    
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
    THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
    IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    
    */
    
    #include "feh.h"
    #include "filelist.h"
    #include "winwidget.h"
    #include "options.h"
    
    static void winwidget_unregister(winwidget win);
    static void winwidget_register(winwidget win);
    static winwidget winwidget_allocate(void);
    
    static char bm_no_data[] = { 0,0,0,0, 0,0,0,0 };
    int window_num = 0;             /* For window list */
    winwidget *windows = NULL;      /* List of windows to loop though */
    
    static winwidget
    winwidget_allocate(void)
    {
      winwidget ret = NULL;
    
      D_ENTER(4);
      ret = emalloc(sizeof(_winwidget));
    
      ret->win = 0;
      ret->w = 0;
      ret->h = 0;
      ret->full_screen = 0;
      ret->im_w = 0;
      ret->im_h = 0;
      ret->im_angle = 0;
      ret->bg_pmap = 0;
      ret->bg_pmap_cache = 0;
      ret->im = NULL;
      ret->name = NULL;
      ret->file = NULL;
      ret->type = WIN_TYPE_UNSET;
      ret->visible = 0;
      ret->caption_entry = 0;
    
      /* Zoom stuff */
      ret->mode = MODE_NORMAL;
    
      ret->gc = None;
    
      /* New stuff */
      ret->im_x = 0;
      ret->im_y = 0;
      ret->zoom = 1.0;
    
      ret->click_offset_x = 0;
      ret->click_offset_y = 0;
      ret->im_click_offset_x = 0;
      ret->im_click_offset_y = 0;
      ret->has_rotated = 0;
    
      D_RETURN(4, ret);
    }
    
    winwidget
    winwidget_create_from_image(Imlib_Image im,
                                char *name,
                                char type)
    {
      winwidget ret = NULL;
    
      D_ENTER(4);
    
      if (im == NULL)
        D_RETURN(4, NULL);
    
      ret = winwidget_allocate();
      ret->type = type;
    
      ret->im = im;
      ret->w = ret->im_w = gib_imlib_image_get_width(ret->im);
      ret->h = ret->im_h = gib_imlib_image_get_height(ret->im);
    
      if (name)
        ret->name = estrdup(name);
      else
        ret->name = estrdup(PACKAGE);
    
      if (opt.full_screen)
        ret->full_screen = True;
      winwidget_create_window(ret, ret->w, ret->h);
      winwidget_render_image(ret, 1, 1);
    
      D_RETURN(4, ret);
    }
    
    winwidget
    winwidget_create_from_file(gib_list * list,
                               char *name,
                               char type)
    {
      winwidget ret = NULL;
      feh_file *file = FEH_FILE(list->data);
    
      D_ENTER(4);
    
      if (!file || !file->filename)
        D_RETURN(4, NULL);
    
      ret = winwidget_allocate();
      ret->file = list;
      ret->type = type;
      if (name)
        ret->name = estrdup(name);
      else
        ret->name = estrdup(file->filename);
    
      if (winwidget_loadimage(ret, file) == 0) {
        winwidget_destroy(ret);
        D_RETURN(4, NULL);
      }
    
      if (!ret->win) {
        ret->w = ret->im_w = gib_imlib_image_get_width(ret->im);
        ret->h = ret->im_h = gib_imlib_image_get_height(ret->im);
        D(3,
          ("image is %dx%d pixels, format %s\n", ret->w, ret->h,
           gib_imlib_image_format(ret->im)));
        if (opt.full_screen)
          ret->full_screen = True;
        winwidget_create_window(ret, ret->w, ret->h);
        winwidget_render_image(ret, 1, 1);
      }
    
      D_RETURN(4, ret);
    }
    
    void
    winwidget_create_window(winwidget ret,
                            int w,
                            int h)
    {
      XSetWindowAttributes attr;
      XClassHint *xch;
      MWMHints mwmhints;
      Atom prop = None;
      int x = 0;
      int y = 0;
    
      D_ENTER(4);
    
      if (ret->full_screen) {
        w = scr->width;
        h = scr->height;
    
    #ifdef HAVE_LIBXINERAMA
        if (opt.xinerama && xinerama_screens) {
          w = xinerama_screens[xinerama_screen].width;
          h = xinerama_screens[xinerama_screen].height;
        }
    #endif /* HAVE_LIBXINERAMA */
      } else if (opt.geom_flags) {
        if (opt.geom_flags & WidthValue) {
          w = opt.geom_w;
        }
        if (opt.geom_flags & HeightValue) {
          h = opt.geom_h;
        }
        if (opt.geom_flags & XValue) {
          if (opt.geom_flags & XNegative) {
            x = scr->width - opt.geom_x;
          } else {
            x = opt.geom_x;
          }
        }
        if (opt.geom_flags & YValue) {
          if (opt.geom_flags & YNegative) {
            y = scr->height - opt.geom_y;
          } else {
            y = opt.geom_y;
          }
        }
      } else if (opt.screen_clip) {
        if (w > scr->width)
          w = scr->width;
        if (h > scr->height)
          h = scr->height;
    
    #ifdef HAVE_LIBXINERAMA
        if (opt.xinerama && xinerama_screens) {
          if (w > xinerama_screens[xinerama_screen].width)
            w = xinerama_screens[xinerama_screen].width;
          if (h > xinerama_screens[xinerama_screen].height)
            h = xinerama_screens[xinerama_screen].height;
        }
    #endif /* HAVE_LIBXINERAMA */
      }
    
      ret->x = x;
      ret->y = y;
      ret->w = w;
      ret->h = h;
      ret->visible = False;
    
      attr.backing_store = NotUseful;
      attr.override_redirect = False;
      attr.colormap = cm;
      attr.border_pixel = 0;
      attr.background_pixel = 0;
      attr.save_under = False;
      attr.event_mask =
        StructureNotifyMask | ButtonPressMask | ButtonReleaseMask |
        PointerMotionMask | EnterWindowMask | LeaveWindowMask | KeyPressMask |
        KeyReleaseMask | ButtonMotionMask | ExposureMask | FocusChangeMask |
        PropertyChangeMask | VisibilityChangeMask;
    
      if (opt.borderless || ret->full_screen) {
        prop = XInternAtom(disp, "_MOTIF_WM_HINTS", True);
        if (prop == None) {
          weprintf("Window Manager does not support MWM hints. "
                   "To get a borderless window I have to bypass your wm.");
          attr.override_redirect = True;
          mwmhints.flags = 0;
        } else {
          mwmhints.flags = MWM_HINTS_DECORATIONS;
          mwmhints.decorations = 0;
        }
      } else
        mwmhints.flags = 0;
    
      ret->win =
        XCreateWindow(disp, DefaultRootWindow(disp), x, y, w, h, 0, depth,
                      InputOutput, vis,
                      CWOverrideRedirect | CWSaveUnder | CWBackingStore |
                      CWColormap | CWBackPixel | CWBorderPixel | CWEventMask,
                      &attr);
    
      if (mwmhints.flags) {
        XChangeProperty(disp, ret->win, prop, prop, 32, PropModeReplace,
                        (unsigned char *) &mwmhints, PROP_MWM_HINTS_ELEMENTS);
      }
    
      XSetWMProtocols(disp, ret->win, &wmDeleteWindow, 1);
      winwidget_update_title(ret);
      xch = XAllocClassHint();
      xch->res_name = "feh";
      xch->res_class = "feh";
      XSetClassHint(disp, ret->win, xch);
      XFree(xch);
    
      /* Size hints */
      if (ret->full_screen || opt.geom_flags) {
        XSizeHints xsz;
    
        xsz.flags = USPosition;
        xsz.x = x;
        xsz.y = y;
        XSetWMNormalHints(disp, ret->win, &xsz);
        XMoveWindow(disp, ret->win, x, y);
      }
      if (ret->full_screen && opt.hide_pointer) {
        Cursor no_ptr;
        XColor black, dummy;
        Pixmap bm_no;
        bm_no = XCreateBitmapFromData(disp, ret->win, bm_no_data, 8, 8);
        XAllocNamedColor(disp, DefaultColormapOfScreen(DefaultScreenOfDisplay(disp)), "black", &black, &dummy);
        
        no_ptr = XCreatePixmapCursor(disp, bm_no, bm_no, &black, &black, 0, 0);
        XDefineCursor(disp, ret->win, no_ptr);
      }
    
      /* set the icon name property */
      XSetIconName(disp, ret->win, "feh");
      /* set the command hint */
      XSetCommand(disp, ret->win, cmdargv, cmdargc);
    
      winwidget_register(ret);
      D_RETURN_(4);
    }
    
    void
    winwidget_update_title(winwidget ret)
    {
      char *name;
    
      D_ENTER(4);
      D(4, ("winwid->name = %s\n", ret->name));
      name = ret->name ? ret->name : "feh";
      XStoreName(disp, ret->win, name);
      XSetIconName(disp, ret->win, name);
      /* XFlush(disp); */
      D_RETURN_(4);
    }
    
    void
    winwidget_setup_pixmaps(winwidget winwid)
    {
      D_ENTER(4);
    
      if (winwid->full_screen) {
        if (!(winwid->bg_pmap)) {
          if (winwid->gc == None) {
            XGCValues gcval;
    
            gcval.foreground = BlackPixel(disp, DefaultScreen(disp));
            winwid->gc = XCreateGC(disp, winwid->win, GCForeground, &gcval);
          }
          winwid->bg_pmap =
            XCreatePixmap(disp, winwid->win, scr->width, scr->height, depth);
        }
        XFillRectangle(disp, winwid->bg_pmap, winwid->gc, 0, 0, scr->width,
                       scr->height);
      } else {
        if (!winwid->bg_pmap || winwid->had_resize) {
          D(4, ("recreating background pixmap (%dx%d)\n", winwid->w, winwid->h));
          if (winwid->bg_pmap)
            XFreePixmap(disp, winwid->bg_pmap);
    
          if (winwid->w == 0)
            winwid->w = 1;
          if (winwid->h == 0)
            winwid->h = 1;
          winwid->bg_pmap =
            XCreatePixmap(disp, winwid->win, winwid->w, winwid->h, depth);
          winwid->had_resize = 0;
        }
      }
      D_RETURN_(4);
    }
    
    void
    winwidget_render_image(winwidget winwid,
                           int resize,
                           int alias)
    {
      int sx, sy, sw, sh, dx, dy, dw, dh;
      int calc_w, calc_h;
    
      D_ENTER(4);
    
      if (!winwid->full_screen && resize) {
        winwidget_resize(winwid, winwid->im_w, winwid->im_h);
        winwidget_reset_image(winwid);
      }
    
      /* bounds checks for panning */
      if (winwid->im_x > winwid->w)
        winwid->im_x = winwid->w;
      if (winwid->im_y > winwid->h)
        winwid->im_y = winwid->h;
    
      winwidget_setup_pixmaps(winwid);
    
      if (!winwid->full_screen
          && ((gib_imlib_image_has_alpha(winwid->im)) || (opt.geom_flags)
              || (winwid->im_x || winwid->im_y) || (winwid->zoom != 1.0)
              || (winwid->w > winwid->im_w || winwid->h > winwid->im_h)
              || (winwid->has_rotated)))
        feh_draw_checks(winwid);
    
      if (!winwid->full_screen && opt.scale_down
          && ((winwid->w < winwid->im_w) || (winwid->h < winwid->im_h))) {
        D(2, ("scaling down image\n"));
    
        feh_calc_needed_zoom(&(winwid->zoom), winwid->im_w, winwid->im_h,
                             winwid->w, winwid->h);
        winwidget_resize(winwid, winwid->im_w * winwid->zoom,
                         winwid->im_h * winwid->zoom);
      }
    
      if (resize && (winwid->full_screen || opt.geom_flags)) {
        int smaller;                /* Is the image smaller than screen? */
        int max_w, max_h;
    
        if (winwid->full_screen) {
          max_w = scr->width;
          max_h = scr->height;
    #ifdef HAVE_LIBXINERAMA
          if (opt.xinerama && xinerama_screens) {
            max_w = xinerama_screens[xinerama_screen].width;
            max_h = xinerama_screens[xinerama_screen].height;
          }
    #endif /* HAVE_LIBXINERAMA */
        } else {
          if (opt.geom_flags & WidthValue) {
            max_w = opt.geom_w;
          }
          if (opt.geom_flags & HeightValue) {
            max_h = opt.geom_h;
          }
        }
    
        D(4, ("Calculating for fullscreen/fixed geom render\n"));
        smaller = ((winwid->im_w < max_w) && (winwid->im_h < max_h));
    
        if (!smaller || opt.auto_zoom) {
          double ratio = 0.0;
    
          /* Image is larger than the screen (so wants shrinking), or it's
             smaller but wants expanding to fill it */
          ratio =
            feh_calc_needed_zoom(&(winwid->zoom), winwid->im_w, winwid->im_h,
                                 max_w, max_h);
    
          /* contributed by Jens Laas <jens.laas@data.slu.se>
           * What it does:
           * zooms images by a fixed amount but never larger than the screen.
           *
           * Why:
           * This is nice if you got a collection of images where some
           * are small and can stand a small zoom. Large images are unaffected.
           *
           * When does it work, and how?
           * You have to be in fullscreen mode _and_ have auto-zoom turned on.
           *   "feh -FZ --zoom 130 imagefile" will do the trick.
           *        -zoom percent - the new switch.
           *                        100 = orignal size,
           *                        130 is 30% larger.
           */
          if (opt.default_zoom) {
            double old_zoom = winwid->zoom;
    
            winwid->zoom = 0.01 * opt.default_zoom;
            if ((winwid->im_h * winwid->zoom) > max_h)
              winwid->zoom = old_zoom;
            if ((winwid->im_w * winwid->zoom) > max_w)
              winwid->zoom = old_zoom;
    
            winwid->im_x = ((int) (max_w - (winwid->im_w * winwid->zoom))) >> 1;
            winwid->im_y = ((int) (max_h - (winwid->im_h * winwid->zoom))) >> 1;
          } else {
            if (ratio > 1.0) {
              /* height is the factor */
              winwid->im_x = 0;
              winwid->im_y = ((int) (max_h - (winwid->im_h * winwid->zoom))) >> 1;
            } else {
              /* width is the factor */
              winwid->im_x = ((int) (max_w - (winwid->im_w * winwid->zoom))) >> 1;
              winwid->im_y = 0;
            }
          }
        } else {
          /* my modification to jens hack, allow --zoom without auto-zoom mode */
          if(opt.default_zoom) {
            winwid->zoom = 0.01 * opt.default_zoom;
          } else {
            winwid->zoom = 1.0;
          }
          /* Just center the image in the window */
          winwid->im_x = (int)(max_w - (winwid->im_w * winwid->zoom)) >> 1;
          winwid->im_y = (int)(max_h - (winwid->im_h * winwid->zoom)) >> 1;
        }
      }
    
      /* Now we ensure only to render the area we're looking at */
      dx = winwid->im_x;
      dy = winwid->im_y;
      if (dx < 0)
        dx = 0;
      if (dy < 0)
        dy = 0;
    
      if (winwid->im_x < 0)
        sx = 0 - (winwid->im_x / winwid->zoom);
      else
        sx = 0;
    
      if (winwid->im_y < 0)
        sy = 0 - (winwid->im_y / winwid->zoom);
      else
        sy = 0;
      
      calc_w = winwid->im_w * winwid->zoom;
      calc_h = winwid->im_h * winwid->zoom;
      dw = (winwid->w - winwid->im_x);
      dh = (winwid->h - winwid->im_y);
      if (calc_w < dw)
        dw = calc_w;
      if (calc_h < dh)
        dh = calc_h;
      if (dw > winwid->w)
        dw = winwid->w;
      if (dh > winwid->h)
        dh = winwid->h;
    
      sw = dw / winwid->zoom;
      sh = dh / winwid->zoom;
    
      D(5,
        ("sx: %d sy: %d sw: %d sh: %d dx: %d dy: %d dw: %d dh: %d zoom: %f\n", sx,
         sy, sw, sh, dx, dy, dw, dh, winwid->zoom));
    
      D(5, ("winwidget_render(): winwid->im_angle = %f\n", winwid->im_angle));
      if (winwid->has_rotated)
        gib_imlib_render_image_part_on_drawable_at_size_with_rotation(winwid->
                                                                      bg_pmap,
                                                                      winwid->im,
                                                                      sx, sy, sw,
                                                                      sh, dx, dy,
                                                                      dw, dh,
                                                                      winwid->
                                                                      im_angle, 1,
                                                                      1, alias);
      else
        gib_imlib_render_image_part_on_drawable_at_size(winwid->bg_pmap,
                                                        winwid->im, sx, sy, sw,
                                                        sh, dx, dy, dw, dh, 1,
                                                        gib_imlib_image_has_alpha
                                                        (winwid->im), alias);
      if (opt.caption_path) {
        /* cache bg pixmap. during caption entry, multiple redraws are done
         * because the caption overlay changes - the image doesn't though, so re-
         * rendering that is a waste of time */
        if (winwid->caption_entry) {
          GC gc;
          if (winwid->bg_pmap_cache)
            XFreePixmap(disp, winwid->bg_pmap_cache);
          winwid->bg_pmap_cache = XCreatePixmap(disp,
                                                winwid->win,
                                                winwid->w,
                                                winwid->h,
                                                depth);
          gc = XCreateGC(disp, winwid->win, 0, NULL);
          XCopyArea(disp, winwid->bg_pmap, winwid->bg_pmap_cache, gc, 0, 0, winwid->w, winwid->h, 0, 0);
          XFreeGC(disp, gc);
        }
        feh_draw_caption(winwid);
      }
    
      if (opt.draw_filename)
        feh_draw_filename(winwid);
      if (opt.draw_actions)
        feh_draw_actions(winwid);
      if ((opt.mode == MODE_ZOOM) && !alias)
        feh_draw_zoom(winwid);
      XSetWindowBackgroundPixmap(disp, winwid->win, winwid->bg_pmap);
      XClearWindow(disp, winwid->win);
      D_RETURN_(4);
    }
    
    void winwidget_render_image_cached(winwidget winwid) {
      static GC gc = None;
    
      if (gc == None) {
        gc = XCreateGC(disp, winwid->win, 0, NULL);
      }
      XCopyArea(disp, winwid->bg_pmap_cache, winwid->bg_pmap, gc, 0, 0, winwid->w, winwid->h, 0, 0);
    
      if (opt.caption_path)
        feh_draw_caption(winwid);
      if (opt.draw_filename)
        feh_draw_filename(winwid);
      if (opt.draw_actions)
        feh_draw_actions(winwid);
      XSetWindowBackgroundPixmap(disp, winwid->win, winwid->bg_pmap);
      XClearWindow(disp, winwid->win);
    }
    
    double
    feh_calc_needed_zoom(double *zoom,
                         int orig_w,
                         int orig_h,
                         int dest_w,
                         int dest_h)
    {
      double ratio = 0.0;
    
      D_ENTER(4);
    
      ratio = ((double) orig_w / orig_h) / ((double) dest_w / dest_h);
    
      if (ratio > 1.0)
        *zoom = ((double) dest_w / orig_w);
      else
        *zoom = ((double) dest_h / orig_h);
    
      D_RETURN(4, ratio);
    }
    
    Pixmap
    feh_create_checks(void)
    {
      static Pixmap checks_pmap = None;
      Imlib_Image checks = NULL;
    
      D_ENTER(4);
      if (checks_pmap == None) {
        int onoff, x, y;
    
        checks = imlib_create_image(16, 16);
    
        if (!checks)
          eprintf
            ("Unable to create a teeny weeny imlib image. I detect problems");
    
        for (y = 0; y < 16; y += 8) {
          onoff = (y / 8) & 0x1;
          for (x = 0; x < 16; x += 8) {
            if (onoff)
              gib_imlib_image_fill_rectangle(checks, x, y, 8, 8, 144, 144, 144,
                                             255);
            else
              gib_imlib_image_fill_rectangle(checks, x, y, 8, 8, 100, 100, 100,
                                             255);
            onoff++;
            if (onoff == 2)
              onoff = 0;
          }
        }
        checks_pmap = XCreatePixmap(disp, root, 16, 16, depth);
        gib_imlib_render_image_on_drawable(checks_pmap, checks, 0, 0, 1, 0, 0);
        gib_imlib_free_image_and_decache(checks);
      }
      D_RETURN(4, checks_pmap);
    }
    
    void
    winwidget_clear_background(winwidget w)
    {
      D_ENTER(4);
      XSetWindowBackgroundPixmap(disp, w->win, feh_create_checks());
      /* XClearWindow(disp, w->win); */
      D_RETURN_(4);
    }
    
    void
    feh_draw_checks(winwidget win)
    {
      static GC gc = None;
      XGCValues gcval;
    
      D_ENTER(4);
      if (gc == None) {
        gcval.tile = feh_create_checks();
        gcval.fill_style = FillTiled;
        gc = XCreateGC(disp, win->win, GCTile | GCFillStyle, &gcval);
      }
      XFillRectangle(disp, win->bg_pmap, gc, 0, 0, win->w, win->h);
      D_RETURN_(4);
    }
    
    void
    winwidget_destroy_xwin(winwidget winwid)
    {
      D_ENTER(4);
      if (winwid->win) {
        winwidget_unregister(winwid);
        XDestroyWindow(disp, winwid->win);
      }
      if (winwid->bg_pmap) {
        XFreePixmap(disp, winwid->bg_pmap);
        winwid->bg_pmap = None;
      }
      D_RETURN_(4);
    }
    
    void
    winwidget_destroy(winwidget winwid)
    {
      D_ENTER(4);
      winwidget_destroy_xwin(winwid);
      if (winwid->name)
        free(winwid->name);
      if ((winwid->type == WIN_TYPE_ABOUT) && winwid->file) {
        feh_file_free(FEH_FILE(winwid->file->data));
        free(winwid->file);
      }
      if (winwid->gc)
        XFreeGC(disp, winwid->gc);
      if (winwid->im)
        gib_imlib_free_image_and_decache(winwid->im);
      free(winwid);
      D_RETURN_(4);
    }
    
    void
    winwidget_destroy_all(void)
    {
      int i;
    
      D_ENTER(4);
      /* Have to DESCEND the list here, 'cos of the way _unregister works */
      for (i = window_num - 1; i >= 0; i--)
        winwidget_destroy(windows[i]);
      D_RETURN_(4);
    }
    
    void
    winwidget_rerender_all(int resize,
                           int alias)
    {
      int i;
    
      D_ENTER(4);
      /* Have to DESCEND the list here, 'cos of the way _unregister works */
      for (i = window_num - 1; i >= 0; i--)
        winwidget_render_image(windows[i], resize, alias);
      D_RETURN_(4);
    }
    
    winwidget
    winwidget_get_first_window_of_type(unsigned int type)
    {
      int i;
    
      D_ENTER(4);
      for (i = 0; i < window_num; i++)
        if (windows[i]->type == type)
          D_RETURN(4, windows[i]);
      D_RETURN(4, NULL);
    }
    
    int
    winwidget_loadimage(winwidget winwid,
                        feh_file * file)
    {
      D_ENTER(4);
      D(4, ("filename %s\n", file->filename));
      D_RETURN(4, feh_load_image(&(winwid->im), file));
    }
    
    void
    winwidget_show(winwidget winwid)
    {
      XEvent ev;
    
      D_ENTER(4);
    
      /* feh_debug_print_winwid(winwid); */
      if (!winwid->visible) {
        XMapWindow(disp, winwid->win);
        if (opt.full_screen)
          XMoveWindow(disp, winwid->win, 0, 0);
        /* wait for the window to map */
        D(4, ("Waiting for window to map\n"));
        XMaskEvent(disp, StructureNotifyMask, &ev);
        D(4, ("Window mapped\n"));
        winwid->visible = 1;
      }
      D_RETURN_(4);
    }
    
    int
    winwidget_count(void)
    {
      D_ENTER(4);
      D_RETURN(4, window_num);
    }
    
    void 
    winwidget_move(winwidget winwid,
                   int x,
                   int y)
    {
      D_ENTER(4);
      if (winwid && ((winwid->x != x) || (winwid->y != y))) {
        winwid->x = x;
        winwid->y = y;
        winwid->x = (x > scr->width) ? scr->width : x;
        winwid->y = (y > scr->height) ? scr->height : y;
        XMoveWindow(disp, winwid->win, winwid->x, winwid->y);
        XFlush(disp);
      } else {
        D(4, ("No move actually needed\n"));
      }
      D_RETURN_(4);
    }
    
    void
    winwidget_resize(winwidget winwid,
                     int w,
                     int h)
    {
      D_ENTER(4);
      if (opt.geom_flags) {
        winwid->had_resize = 1;
        return;
      }
      if (winwid && ((winwid->w != w) || (winwid->h != h))) {
        D(4, ("Really doing a resize\n"));
        /* winwidget_clear_background(winwid); */
        if (opt.screen_clip) {
          winwid->w = (w > scr->width) ? scr->width : w;
          winwid->h = (h > scr->height) ? scr->height : h;
        }
        XResizeWindow(disp, winwid->win, winwid->w, winwid->h);
        winwid->had_resize = 1;
        XFlush(disp);
    
    #ifdef HAVE_LIBXINERAMA
        if (opt.xinerama && xinerama_screens) {
          int i;
    
          for (i = 0; i < num_xinerama_screens; i++) {
            xinerama_screen = 0;
            if (XY_IN_RECT(winwid->x, winwid->y,
                           xinerama_screens[i].x_org, xinerama_screens[i].y_org,
                           xinerama_screens[i].width, xinerama_screens[i].height)) {
              xinerama_screen = i;
              break;
            }
              
          }
        }
    #endif /* HAVE_LIBXINERAMA */
    
      } else {
        D(4, ("No resize actually needed\n"));
      }
    
      D_RETURN_(4);
    }
    
    void
    winwidget_hide(winwidget winwid)
    {
      D_ENTER(4);
      XUnmapWindow(disp, winwid->win);
      winwid->visible = 0;
      D_RETURN_(4);
    }
    
    static void
    winwidget_register(winwidget win)
    {
      D_ENTER(4);
      D(5, ("window %p\n", win));
      window_num++;
      if (windows)
        windows = erealloc(windows, window_num * sizeof(winwidget));
      else
        windows = emalloc(window_num * sizeof(winwidget));
      windows[window_num - 1] = win;
    
      XSaveContext(disp, win->win, xid_context, (XPointer) win);
      D_RETURN_(4);
    }
    
    static void
    winwidget_unregister(winwidget win)
    {
      int i, j;
    
      D_ENTER(4);
      for (i = 0; i < window_num; i++) {
        if (windows[i] == win) {
          for (j = i; j < window_num - 1; j++)
            windows[j] = windows[j + 1];
          window_num--;
          if (window_num > 0)
            windows = erealloc(windows, window_num * sizeof(winwidget));
          else {
            free(windows);
            windows = NULL;
          }
        }
      }
      XDeleteContext(disp, win->win, xid_context);
      D_RETURN_(4);
    }
    
    winwidget
    winwidget_get_from_window(Window win)
    {
      winwidget ret = NULL;
    
      D_ENTER(4);
      if (XFindContext(disp, win, xid_context, (XPointer *) & ret) != XCNOENT)
        D_RETURN(4, ret);
      D_RETURN(4, NULL);
    }
    
    void
    winwidget_rename(winwidget winwid,
                     char *newname)
    {
      D_ENTER(4);
      if (winwid->name)
        free(winwid->name);
      winwid->name = estrdup(newname);
      winwidget_update_title(winwid);
      D_RETURN_(4);
    }
    
    void
    winwidget_free_image(winwidget w)
    {
      D_ENTER(4);
      if (w->im)
        gib_imlib_free_image_and_decache(w->im);
      w->im = NULL;
      w->im_w = 0;
      w->im_h = 0;
      D_RETURN_(4);
    }
    
    void
    feh_debug_print_winwid(winwidget w)
    {
      printf("winwid_debug:\n" "winwid = %p\n" "win = %ld\n" "w = %d\n" "h = %d\n"
             "im_w = %d\n" "im_h = %d\n" "im_angle = %f\n" "type = %d\n"
             "had_resize = %d\n" "im = %p\n" "GC = %p\n" "pixmap = %ld\n"
             "name = %s\n" "file = %p\n" "mode = %d\n" "im_x = %d\n" "im_y = %d\n"
             "zoom = %f\n" "click_offset_x = %d\n" "click_offset_y = %d\n"
             "im_click_offset_x = %d\n" "im_click_offset_y = %d\n"
             "has_rotated = %d\n", w, w->win, w->w, w->h, w->im_w, w->im_h,
             w->im_angle, w->type, w->had_resize, w->im, w->gc, w->bg_pmap,
             w->name, w->file, w->mode, w->im_x, w->im_y, w->zoom,
             w->click_offset_x, w->click_offset_y, w->im_click_offset_x,
             w->im_click_offset_y, w->has_rotated);
    }
    
    void
    winwidget_reset_image(winwidget winwid)
    {
      D_ENTER(4);
      winwid->zoom = 1.0;
      winwid->im_x = 0;
      winwid->im_y = 0;
      winwid->im_angle = 0.0;
      winwid->has_rotated = 0;
      D_RETURN_(4);
    }
    
    void
    winwidget_sanitise_offsets(winwidget winwid)
    {
      int far_left, far_top;
      int min_x, max_x, max_y, min_y;
    
      D_ENTER(4);
    
      far_left = winwid->w - (winwid->im_w * winwid->zoom);
      far_top = winwid->h - (winwid->im_h * winwid->zoom);
    
      if ((winwid->im_w * winwid->zoom) > winwid->w) {
        min_x = far_left;
        max_x = 0;
      } else {
        min_x = 0;
        max_x = far_left;
      }
      if ((winwid->im_h * winwid->zoom) > winwid->h) {
        min_y = far_top;
        max_y = 0;
      } else {
        min_y = 0;
        max_y = far_top;
      }
      if (winwid->im_x > max_x)
        winwid->im_x = max_x;
      if (winwid->im_x < min_x)
        winwid->im_x = min_x;
      if (winwid->im_y > max_y)
        winwid->im_y = max_y;
      if (winwid->im_y < min_y)
        winwid->im_y = min_y;
    
      D_RETURN_(4);
    }
    
    
    void
    winwidget_size_to_image(winwidget winwid)
    {
      D_ENTER(4);
      winwidget_resize(winwid, winwid->im_w * winwid->zoom,
                       winwid->im_h * winwid->zoom);
      winwid->im_x = winwid->im_y = 0;
      winwidget_render_image(winwid, 0, 1);
      D_RETURN_(4);
    }
    
    int winwidget_get_width(winwidget winwid) {
      int rect[4];
      D_ENTER(4);
      winwidget_get_geometry(winwid, rect);
      D_RETURN(4, rect[2]);
    }
    
    int winwidget_get_height(winwidget winwid) {
      int rect[4];
      D_ENTER(4);