Krolyxon
Commit 75e3693

Branches

Refs →

nsxivevents.c

567 lines · 11.5 KB · C

1/* sxiv: events.c
2 * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#define _GENERAL_CONFIG
20#define _MAPPINGS_CONFIG
21
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
25#include <sys/time.h>
26#include <sys/wait.h>
27#include <X11/keysym.h>
28#include <X11/Xutil.h>
29
30#include "events.h"
31#include "image.h"
32#include "thumbs.h"
33#include "types.h"
34#include "util.h"
35#include "window.h"
36#include "config.h"
37
38/* timeouts in milliseconds: */
39enum {
40 TO_WIN_RESIZE = 75,
41 TO_CURSOR_HIDE = 1500,
42 TO_THUMBS_LOAD = 200
43};
44
45void cleanup();
46void remove_file(int, unsigned char);
47void load_image(int);
48void update_title();
49
50extern appmode_t mode;
51extern img_t img;
52extern tns_t tns;
53extern win_t win;
54
55extern char **filenames;
56extern int filecnt, fileidx;
57
58int timo_cursor;
59int timo_redraw;
60
61int run_command(const char *cline, Bool reload) {
62 int fncnt, fnlen;
63 char *cn, *cmdline;
64 const char *co, *fname;
65 pid_t pid;
66 int ret, status;
67
68 if (!cline || !*cline)
69 return 0;
70
71 fncnt = 0;
72 co = cline - 1;
73 while ((co = strchr(co + 1, '#')))
74 fncnt++;
75
76 if (!fncnt)
77 return 0;
78
79 ret = 0;
80 fname = filenames[mode == MODE_NORMAL ? fileidx : tns.sel];
81 fnlen = strlen(fname);
82 cn = cmdline = (char*) s_malloc((strlen(cline) + fncnt * (fnlen + 2)) *
83 sizeof(char));
84
85 /* replace all '#' with filename */
86 for (co = cline; *co; co++) {
87 if (*co == '#') {
88 *cn++ = '"';
89 strcpy(cn, fname);
90 cn += fnlen;
91 *cn++ = '"';
92 } else {
93 *cn++ = *co;
94 }
95 }
96 *cn = '\0';
97
98 if ((pid = fork()) == 0) {
99 execlp("/bin/sh", "/bin/sh", "-c", cmdline, NULL);
100 warn("could not exec: /bin/sh");
101 exit(1);
102 } else if (pid < 0) {
103 warn("could not fork. command line was: %s", cmdline);
104 } else if (reload) {
105 waitpid(pid, &status, 0);
106 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
107 ret = 1;
108 else
109 warn("child exited with non-zero return value: %d. command line was: %s",
110 WEXITSTATUS(status), cmdline);
111 }
112
113 free(cmdline);
114 return ret;
115}
116
117void redraw() {
118 if (mode == MODE_NORMAL) {
119 img_render(&img, &win);
120 if (timo_cursor)
121 win_set_cursor(&win, CURSOR_ARROW);
122 else
123 win_set_cursor(&win, CURSOR_NONE);
124 } else {
125 tns_render(&tns, &win);
126 }
127 update_title();
128 timo_redraw = 0;
129}
130
131void on_keypress(XKeyEvent *kev) {
132 int i;
133 KeySym ksym;
134 char key;
135
136 if (!kev)
137 return;
138
139 XLookupString(kev, &key, 1, &ksym, NULL);
140
141 if (EXT_COMMANDS && (CLEANMASK(kev->state) & ControlMask)) {
142 for (i = 0; i < LEN(commands); i++) {
143 if (commands[i].ksym == ksym) {
144 win_set_cursor(&win, CURSOR_WATCH);
145 if (run_command(commands[i].cmdline, commands[i].reload)) {
146 if (mode == MODE_NORMAL) {
147 if (fileidx < tns.cnt)
148 tns_load(&tns, fileidx, filenames[fileidx], 1);
149 img_close(&img, 1);
150 load_image(fileidx);
151 } else {
152 if (!tns_load(&tns, tns.sel, filenames[tns.sel], 0)) {
153 remove_file(tns.sel, 0);
154 tns.dirty = 1;
155 if (tns.sel >= tns.cnt)
156 tns.sel = tns.cnt - 1;
157 }
158 }
159 redraw();
160 }
161 if (mode == MODE_THUMBS)
162 win_set_cursor(&win, CURSOR_ARROW);
163 else if (!timo_cursor)
164 win_set_cursor(&win, CURSOR_NONE);
165 return;
166 }
167 }
168 }
169
170 for (i = 0; i < LEN(keys); i++) {
171 if (ksym == keys[i].ksym && keys[i].handler) {
172 if (keys[i].handler(keys[i].arg))
173 redraw();
174 return;
175 }
176 }
177}
178
179void on_buttonpress(XButtonEvent *bev) {
180 int i, sel;
181
182 if (!bev)
183 return;
184
185 if (mode == MODE_NORMAL) {
186 win_set_cursor(&win, CURSOR_ARROW);
187 timo_cursor = TO_CURSOR_HIDE;
188
189 for (i = 0; i < LEN(buttons); i++) {
190 if (CLEANMASK(bev->state) == CLEANMASK(buttons[i].mod) &&
191 bev->button == buttons[i].button && buttons[i].handler)
192 {
193 if (buttons[i].handler(buttons[i].arg))
194 redraw();
195 return;
196 }
197 }
198 } else {
199 /* thumbnail mode (hard-coded) */
200 switch (bev->button) {
201 case Button1:
202 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
203 if (sel == tns.sel) {
204 load_image(tns.sel);
205 mode = MODE_NORMAL;
206 timo_cursor = TO_CURSOR_HIDE;
207 } else {
208 tns_highlight(&tns, &win, tns.sel, False);
209 tns_highlight(&tns, &win, sel, True);
210 tns.sel = sel;
211 }
212 redraw();
213 break;
214 }
215 break;
216 case Button4:
217 case Button5:
218 if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN))
219 redraw();
220 break;
221 }
222 }
223}
224
225void run() {
226 int xfd, timeout;
227 fd_set fds;
228 struct timeval tt, t0, t1;
229 XEvent ev;
230
231 timo_cursor = mode == MODE_NORMAL ? TO_CURSOR_HIDE : 0;
232
233 redraw();
234
235 while (1) {
236 if (mode == MODE_THUMBS && tns.cnt < filecnt) {
237 /* load thumbnails */
238 win_set_cursor(&win, CURSOR_WATCH);
239 gettimeofday(&t0, 0);
240
241 while (tns.cnt < filecnt && !XPending(win.env.dpy)) {
242 if (tns_load(&tns, tns.cnt, filenames[tns.cnt], 0))
243 tns.cnt++;
244 else
245 remove_file(tns.cnt, 0);
246 gettimeofday(&t1, 0);
247 if (TIMEDIFF(&t1, &t0) >= TO_THUMBS_LOAD)
248 break;
249 }
250 if (tns.cnt == filecnt)
251 win_set_cursor(&win, CURSOR_ARROW);
252 if (!XPending(win.env.dpy)) {
253 redraw();
254 continue;
255 } else {
256 timo_redraw = TO_THUMBS_LOAD;
257 }
258 } else if (timo_cursor || timo_redraw) {
259 /* check active timeouts */
260 gettimeofday(&t0, 0);
261 timeout = MIN(timo_cursor + 1, timo_redraw + 1);
262 MSEC_TO_TIMEVAL(timeout, &tt);
263 xfd = ConnectionNumber(win.env.dpy);
264 FD_ZERO(&fds);
265 FD_SET(xfd, &fds);
266
267 if (!XPending(win.env.dpy))
268 select(xfd + 1, &fds, 0, 0, &tt);
269 gettimeofday(&t1, 0);
270 timeout = MIN(TIMEDIFF(&t1, &t0), timeout);
271
272 /* timeouts fired? */
273 if (timo_cursor) {
274 timo_cursor = MAX(0, timo_cursor - timeout);
275 if (!timo_cursor)
276 win_set_cursor(&win, CURSOR_NONE);
277 }
278 if (timo_redraw) {
279 timo_redraw = MAX(0, timo_redraw - timeout);
280 if (!timo_redraw)
281 redraw();
282 }
283 if ((timo_cursor || timo_redraw) && !XPending(win.env.dpy))
284 continue;
285 }
286
287 if (!XNextEvent(win.env.dpy, &ev)) {
288 /* handle events */
289 switch (ev.type) {
290 case ButtonPress:
291 on_buttonpress(&ev.xbutton);
292 break;
293 case ClientMessage:
294 if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
295 return;
296 break;
297 case ConfigureNotify:
298 if (win_configure(&win, &ev.xconfigure)) {
299 timo_redraw = TO_WIN_RESIZE;
300 if (mode == MODE_NORMAL)
301 img.checkpan = 1;
302 else
303 tns.dirty = 1;
304 }
305 break;
306 case KeyPress:
307 on_keypress(&ev.xkey);
308 break;
309 case MotionNotify:
310 if (!timo_cursor)
311 win_set_cursor(&win, CURSOR_ARROW);
312 timo_cursor = TO_CURSOR_HIDE;
313 break;
314 }
315 }
316 }
317}
318
319
320/* handler functions for key and button mappings: */
321
322int quit(arg_t a) {
323 cleanup();
324 exit(0);
325}
326
327int reload(arg_t a) {
328 if (mode == MODE_NORMAL) {
329 load_image(fileidx);
330 return 1;
331 } else {
332 return 0;
333 }
334}
335
336int toggle_fullscreen(arg_t a) {
337 win_toggle_fullscreen(&win);
338 if (mode == MODE_NORMAL)
339 img.checkpan = 1;
340 else
341 tns.dirty = 1;
342 timo_redraw = TO_WIN_RESIZE;
343 return 0;
344}
345
346int toggle_antialias(arg_t a) {
347 if (mode == MODE_NORMAL) {
348 img_toggle_antialias(&img);
349 return 1;
350 } else {
351 return 0;
352 }
353}
354
355int toggle_alpha(arg_t a) {
356 if (mode == MODE_NORMAL) {
357 img.alpha ^= 1;
358 return 1;
359 } else {
360 return 0;
361 }
362}
363
364int switch_mode(arg_t a) {
365 if (mode == MODE_NORMAL) {
366 if (!tns.thumbs)
367 tns_init(&tns, filecnt);
368 img_close(&img, 0);
369 win_set_cursor(&win, CURSOR_ARROW);
370 timo_cursor = 0;
371 tns.sel = fileidx;
372 tns.dirty = 1;
373 mode = MODE_THUMBS;
374 } else {
375 timo_cursor = TO_CURSOR_HIDE;
376 load_image(tns.sel);
377 mode = MODE_NORMAL;
378 }
379 return 1;
380}
381
382int navigate(arg_t n) {
383 if (mode == MODE_NORMAL) {
384 n += fileidx;
385 if (n < 0)
386 n = 0;
387 if (n >= filecnt)
388 n = filecnt - 1;
389
390 if (n != fileidx) {
391 load_image(n);
392 return 1;
393 }
394 }
395 return 0;
396}
397
398int first(arg_t a) {
399 if (mode == MODE_NORMAL && fileidx != 0) {
400 load_image(0);
401 return 1;
402 } else if (mode == MODE_THUMBS && tns.sel != 0) {
403 tns.sel = 0;
404 tns.dirty = 1;
405 return 1;
406 } else {
407 return 0;
408 }
409}
410
411int last(arg_t a) {
412 if (mode == MODE_NORMAL && fileidx != filecnt - 1) {
413 load_image(filecnt - 1);
414 return 1;
415 } else if (mode == MODE_THUMBS && tns.sel != tns.cnt - 1) {
416 tns.sel = tns.cnt - 1;
417 tns.dirty = 1;
418 return 1;
419 } else {
420 return 0;
421 }
422}
423
424int remove_image(arg_t a) {
425 if (mode == MODE_NORMAL) {
426 remove_file(fileidx, 1);
427 load_image(fileidx >= filecnt ? filecnt - 1 : fileidx);
428 return 1;
429 } else if (tns.sel < tns.cnt) {
430 remove_file(tns.sel, 1);
431 tns.dirty = 1;
432 if (tns.sel >= tns.cnt)
433 tns.sel = tns.cnt - 1;
434 return 1;
435 } else {
436 return 0;
437 }
438}
439
440int move(arg_t dir) {
441 if (mode == MODE_NORMAL)
442 return img_pan(&img, &win, dir, 0);
443 else
444 return tns_move_selection(&tns, &win, dir);
445}
446
447int pan_screen(arg_t dir) {
448 if (mode == MODE_NORMAL)
449 return img_pan(&img, &win, dir, 1);
450 else
451 return 0;
452}
453
454int pan_edge(arg_t dir) {
455 if (mode == MODE_NORMAL)
456 return img_pan_edge(&img, &win, dir);
457 else
458 return 0;
459}
460
461/* Xlib helper function for drag() */
462Bool ismnotify(Display *d, XEvent *e, XPointer a) {
463 return e != NULL && e->type == MotionNotify;
464}
465
466int drag(arg_t a) {
467 int dx = 0, dy = 0, i, ox, oy, x, y;
468 unsigned int ui;
469 Bool dragging = True, next = False;
470 XEvent e;
471 Window w;
472
473 if (mode != MODE_NORMAL)
474 return 0;
475 if (!XQueryPointer(win.env.dpy, win.xwin, &w, &w, &i, &i, &ox, &oy, &ui))
476 return 0;
477
478 win_set_cursor(&win, CURSOR_HAND);
479
480 while (dragging) {
481 if (!next)
482 XMaskEvent(win.env.dpy,
483 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
484 switch (e.type) {
485 case ButtonPress:
486 case ButtonRelease:
487 dragging = False;
488 break;
489 case MotionNotify:
490 x = e.xmotion.x;
491 y = e.xmotion.y;
492 if (x >= 0 && x <= win.w && y >= 0 && y <= win.h) {
493 dx += x - ox;
494 dy += y - oy;
495 }
496 ox = x;
497 oy = y;
498 break;
499 }
500 if (dragging)
501 next = XCheckIfEvent(win.env.dpy, &e, ismnotify, None);
502 if ((!dragging || !next) && (dx != 0 || dy != 0)) {
503 if (img_move(&img, &win, dx, dy))
504 img_render(&img, &win);
505 dx = dy = 0;
506 }
507 }
508
509 win_set_cursor(&win, CURSOR_ARROW);
510 timo_cursor = TO_CURSOR_HIDE;
511 timo_redraw = 0;
512
513 return 0;
514}
515
516int rotate(arg_t dir) {
517 if (mode == MODE_NORMAL) {
518 if (dir == DIR_LEFT) {
519 img_rotate_left(&img, &win);
520 return 1;
521 } else if (dir == DIR_RIGHT) {
522 img_rotate_right(&img, &win);
523 return 1;
524 }
525 }
526 return 0;
527}
528
529int zoom(arg_t scale) {
530 if (mode != MODE_NORMAL)
531 return 0;
532 if (scale > 0)
533 return img_zoom_in(&img, &win);
534 else if (scale < 0)
535 return img_zoom_out(&img, &win);
536 else
537 return img_zoom(&img, &win, 1.0);
538}
539
540int fit_to_win(arg_t ret) {
541 if (mode == MODE_NORMAL) {
542 if ((ret = img_fit_win(&img, &win)))
543 img_center(&img, &win);
544 return ret;
545 } else {
546 return 0;
547 }
548}
549
550int fit_to_img(arg_t ret) {
551 int x, y;
552 unsigned int w, h;
553
554 if (mode == MODE_NORMAL) {
555 x = MAX(0, win.x + img.x);
556 y = MAX(0, win.y + img.y);
557 w = img.w * img.zoom;
558 h = img.h * img.zoom;
559 if ((ret = win_moveresize(&win, x, y, w, h))) {
560 img.x = x - win.x;
561 img.y = y - win.y;
562 }
563 return ret;
564 } else {
565 return 0;
566 }
567}