Skip to content
Snippets Groups Projects
keyevents.c 13.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
    /* keyevents.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 "thumbnail.h"
    #include "filelist.h"
    #include "winwidget.h"
    #include "options.h"
    
    void
    feh_event_invoke_action(winwidget winwid, char *action)
    {
      D_ENTER(4);
      D(4, ("action is '%s'\n", action));
      D(4, ("winwid is '%p'\n", winwid));
      if (action)
      {
         if (opt.slideshow)
         {
            feh_action_run(FEH_FILE(winwid->file->data),action);
            slideshow_change_image(winwid, SLIDE_NEXT);
         }
         else if ((winwid->type == WIN_TYPE_SINGLE)
                  || (winwid->type == WIN_TYPE_THUMBNAIL_VIEWER))
         {
            feh_action_run(FEH_FILE(winwid->file->data),action);
            winwidget_destroy(winwid);
         }
         else if (winwid->type == WIN_TYPE_THUMBNAIL)
         {
           printf("actions from the main thumb window aren't currentl supported!\n");
           printf("For now, open the image to perform the action on it.\n");
         }
      }
      D_RETURN_(4);
    }
    
    void
    feh_event_handle_keypress(XEvent * ev)
    {
       int len;
       char kbuf[20];
       KeySym keysym;
       XKeyEvent *kev;
       winwidget winwid = NULL;
       int curr_screen = 0;
       feh_menu_item *selected_item;
       feh_menu *selected_menu;
    
       D_ENTER(4);
    
       winwid = winwidget_get_from_window(ev->xkey.window);
       
       /* nuke dupe events, unless we're typing text */
       if (winwid && !winwid->caption_entry) {
         while (XCheckTypedWindowEvent(disp, ev->xkey.window, KeyPress, ev));
       }
    
       kev = (XKeyEvent *) ev;
       len = XLookupString(&ev->xkey, (char *) kbuf, sizeof(kbuf), &keysym, NULL);
    
       /* menus are showing, so this is a menu control keypress */
       if (ev->xbutton.window == menu_cover) {
         selected_item = feh_menu_find_selected_r(menu_root, &selected_menu);
         switch (keysym) {
           case XK_Escape:
             feh_menu_hide(menu_root, True);
             break;
           case XK_Left:
             feh_menu_select_parent(selected_menu, selected_item);
             break;
           case XK_Down:
             feh_menu_select_next(selected_menu, selected_item);
             break;
           case XK_Up:
             feh_menu_select_prev(selected_menu, selected_item);
             break;
           case XK_Right:
             feh_menu_select_submenu(selected_menu, selected_item);
             break;
           case XK_Return:
             feh_menu_item_activate(selected_menu, selected_item);
             break;
           default:
             break;
         }
         if (len <= 0 || len > (int) sizeof(kbuf))
            D_RETURN_(4);
         kbuf[len] = '\0';
    
         switch (*kbuf)
         {
           case 'h':
             feh_menu_select_parent(selected_menu, selected_item);
             break;
           case 'j':
             feh_menu_select_next(selected_menu, selected_item);
             break;
           case 'k':
             feh_menu_select_prev(selected_menu, selected_item);
             break;
           case 'l':
             feh_menu_select_submenu(selected_menu, selected_item);
             break;
           case ' ':
             feh_menu_item_activate(selected_menu, selected_item);
             break;
           default:
             break;
         }
         
         D_RETURN_(4);
       }
    
       if (winwid == NULL)
          D_RETURN_(4);
    
       if (winwid->caption_entry) {
         switch(keysym) {
           case XK_Return:
             if (kev->state & ControlMask) {
               /* insert actual newline */
               ESTRAPPEND(FEH_FILE(winwid->file->data)->caption, "\n");
               winwidget_render_image_cached(winwid);
             } else {
               /* finish caption entry, write to captions file */
               FILE *fp;
               char *caption_filename;
               caption_filename = build_caption_filename(FEH_FILE(winwid->file->data));
               winwid->caption_entry = 0;
               winwidget_render_image_cached(winwid);
               XFreePixmap(disp, winwid->bg_pmap_cache);
               winwid->bg_pmap_cache = 0;
               fp = fopen(caption_filename, "w");
               if (!fp) {
                 weprintf("couldn't write to captions file %s:", caption_filename);
                 D_RETURN_(4);
               }
               fprintf(fp, "%s", FEH_FILE(winwid->file->data)->caption);
               free(caption_filename);
               fclose(fp);
             }
             break;
           case XK_Escape:
             /* cancel, revert caption */
             winwid->caption_entry = 0;
             free(FEH_FILE(winwid->file->data)->caption);
             FEH_FILE(winwid->file->data)->caption = NULL;
             winwidget_render_image_cached(winwid);
             XFreePixmap(disp, winwid->bg_pmap_cache);
             winwid->bg_pmap_cache = 0;
             break;
           case XK_BackSpace:
             /* backspace */
             ESTRTRUNC(FEH_FILE(winwid->file->data)->caption, 1);
             winwidget_render_image_cached(winwid);
             break;
           default:
             if(isascii(keysym)) {
               /* append to caption */
               ESTRAPPEND_CHAR(FEH_FILE(winwid->file->data)->caption, keysym);
               winwidget_render_image_cached(winwid);
             }
             break;
         }
         D_RETURN_(4);
       }
       
       
       switch (keysym)
       {
         case XK_Left:
            if (opt.slideshow)
               slideshow_change_image(winwid, SLIDE_PREV);
            break;
         case XK_Right:
            if (opt.slideshow)
               slideshow_change_image(winwid, SLIDE_NEXT);
            break;
         case XK_Page_Up:
            if (opt.slideshow)
               slideshow_change_image(winwid, SLIDE_JUMP_BACK);
            break;
         case XK_Escape:
            winwidget_destroy_all();
            break;
         case XK_Page_Down:
            if (opt.slideshow)
               slideshow_change_image(winwid, SLIDE_JUMP_FWD);
            break;
         case XK_Delete:
            /* Holding ctrl gets you a filesystem deletion and removal from the * 
               filelist. Just DEL gets you filelist removal only. */
            if (kev->state & ControlMask)
            {
               if (winwid->type == WIN_TYPE_THUMBNAIL_VIEWER)
                  feh_thumbnail_mark_removed(FEH_FILE(winwid->file->data), 1);
               feh_filelist_image_remove(winwid, 1);
            }
            else
            {
               if (winwid->type == WIN_TYPE_THUMBNAIL_VIEWER)
                  feh_thumbnail_mark_removed(FEH_FILE(winwid->file->data), 0);
               feh_filelist_image_remove(winwid, 0);
            }
            break;
         case XK_Home:
         case XK_KP_Home:
            if (opt.slideshow)
               slideshow_change_image(winwid, SLIDE_FIRST);
            break;
         case XK_End:
         case XK_KP_End:
            if (opt.slideshow)
               slideshow_change_image(winwid, SLIDE_LAST);
            break;
         case XK_Tab:
    	if (opt.draw_actions) 
            {
    	   opt.draw_actions = 0;
    	   winwidget_rerender_all(0, 1);
            }
    	else
            {
    	   opt.draw_actions = 1;
    	   winwidget_rerender_all(0, 1);
            }
    	break;
         case XK_Return:
             feh_event_invoke_action(winwid,opt.actions[0]);
             break;
         case XK_0:
             feh_event_invoke_action(winwid,opt.actions[0]);
             break;
         case XK_1:
             feh_event_invoke_action(winwid,opt.actions[1]);
             break;
         case XK_2:
             feh_event_invoke_action(winwid,opt.actions[2]);
             break;
         case XK_3:
             feh_event_invoke_action(winwid,opt.actions[3]);
             break;
         case XK_4:
             feh_event_invoke_action(winwid,opt.actions[4]);
             break;
         case XK_5:
             feh_event_invoke_action(winwid,opt.actions[5]);
             break;
         case XK_6:
             feh_event_invoke_action(winwid,opt.actions[6]);
             break;
         case XK_7:
             feh_event_invoke_action(winwid,opt.actions[7]);
             break;
         case XK_8:
             feh_event_invoke_action(winwid,opt.actions[8]);
             break;
         case XK_9:
             feh_event_invoke_action(winwid,opt.actions[9]);
             break;
         case XK_KP_Left:
            winwid->im_x = winwid->im_x - 10;
            winwidget_render_image(winwid, 0, 0);
            break;
         case XK_KP_Right:
            winwid->im_x = winwid->im_x + 10;
            winwidget_render_image(winwid, 0, 0);
            break;
         case XK_KP_Up:
            winwid->im_y = winwid->im_y - 10;
            winwidget_render_image(winwid, 0, 0);
            break;
         case XK_KP_Down:
            winwid->im_y = winwid->im_y + 10;
            winwidget_render_image(winwid, 0, 0);
            break;
         case XK_KP_Add:
            winwid->zoom = winwid->zoom * 1.25;
            winwidget_render_image(winwid, 0, 0);
            break;
         case XK_KP_Subtract:
            winwid->zoom = winwid->zoom * 0.75;
            winwidget_render_image(winwid, 0, 0);
    	break;
         case XK_KP_Multiply:
            winwid->zoom = 1;
            winwidget_render_image(winwid, 0, 0);
    	break;
         case XK_KP_Divide:
            feh_calc_needed_zoom(&winwid->zoom, winwid->im_w, winwid->im_h, winwid->w, winwid->h);
            winwidget_render_image(winwid, 0, 0);
    	break;
         default:
            break;
       }
    
       if (len <= 0 || len > (int) sizeof(kbuf))
          D_RETURN_(4);
       kbuf[len] = '\0';
    
       switch (*kbuf)
       {
         case 'n':
         case 'N':
         case ' ':
            if (opt.slideshow)
               slideshow_change_image(winwid, SLIDE_NEXT);
            break;
         case 'p':
         case 'P':
         case '\b':
            if (opt.slideshow)
               slideshow_change_image(winwid, SLIDE_PREV);
            break;
         case 'q':
         case 'Q':
            winwidget_destroy_all();
            break;
         case 'c':
         case 'C':
            if (opt.caption_path)
              winwid->caption_entry = 1;
              winwidget_render_image(winwid, 0, 0);
            break;
         case 'r':
         case 'R':
            feh_reload_image(winwid, 0, 0);
            break;
         case 'h':
         case 'H':
            slideshow_pause_toggle(winwid);
            break;
         case 's':
         case 'S':
            slideshow_save_image(winwid);
            break;
         case 'f':
         case 'F':
            feh_save_filelist();
            break;
         case 'w':
         case 'W':
            winwidget_size_to_image(winwid);
            break;
         case 'm':
         case 'M':
            winwidget_show_menu(winwid);
            break;
         case 'x':
         case 'X':
            winwidget_destroy(winwid);
            break;
         case '>':
            feh_edit_inplace_orient(winwid, 1);
            break;
         case '<':
            feh_edit_inplace_orient(winwid, 3);
            break;
         case 'v':
         case 'V':
    #ifdef HAVE_LIBXINERAMA
            if (opt.xinerama && xinerama_screens) {
              int i, rect[4];
    
              /* FIXME: this doesn't do what it should;  XGetGeometry always
               * returns x,y == 0,0.  I think that's due to the hints being passed
               * (or more specifically, a missing hint) to X in winwidget_create
               */
              winwidget_get_geometry(winwid, rect);
              /* printf("window: (%d, %d)\n", rect[0], rect[1]);
              printf("found %d screens.\n", num_xinerama_screens); */
              for (i = 0; i < num_xinerama_screens; i++) {
                xinerama_screen = 0;
                /* printf("%d: [%d, %d, %d, %d] (%d, %d)\n",
                       i,
                       xinerama_screens[i].x_org, xinerama_screens[i].y_org,
                       xinerama_screens[i].width, xinerama_screens[i].height,
                       rect[0], rect[1]);*/
                if (XY_IN_RECT(rect[0], rect[1],
                      xinerama_screens[i].x_org, xinerama_screens[i].y_org,
                      xinerama_screens[i].width, xinerama_screens[i].height)) {
                  curr_screen = xinerama_screen = i;
                  break;
                }
              }
            }
    #endif /* HAVE_LIBXINERAMA */
            winwid->full_screen = !winwid->full_screen;
            winwidget_destroy_xwin(winwid);
            winwidget_create_window(winwid, winwid->im_w, winwid->im_h);
            winwidget_render_image(winwid, 1, 1);
            winwidget_show(winwid);
    #ifdef HAVE_LIBXINERAMA
            /* if we have xinerama and we're using it, then full screen the window
             * on the head that the window was active on */
            if (winwid->full_screen == TRUE &&
                opt.xinerama && xinerama_screens) {
              xinerama_screen = curr_screen;
              winwidget_move(winwid,
                             xinerama_screens[curr_screen].x_org,
                             xinerama_screens[curr_screen].y_org);
            }
    #endif /* HAVE_LIBXINERAMA */
    	   case '=':
         case '+':
            if (opt.reload < SLIDESHOW_RELOAD_MAX)
               opt.reload++;
            else if (opt.verbose)
               weprintf("Cannot set RELOAD higher than %d seconds.", opt.reload);
            break;
         case '-':
         case '_':
            if (opt.reload > 1)
               opt.reload--;
            else if (opt.verbose)
               weprintf("Cannot set RELOAD lower than 1 second.");
            break;
         default:
            break;
       }
       D_RETURN_(4);
    }