nsxiv12a94cc
Manual event handling in main.c
+142 −155
1 file changed
- M main.c +142 −155
| @@ -32,17 +32,10 @@ | |||
| 32 | 32 | #include "util.h" | |
| 33 | 33 | #include "window.h" | |
| 34 | 34 | ||
| 35 | − | void on_keypress(XEvent*); | |
| 36 | − | void on_buttonpress(XEvent*); | |
| 37 | − | void on_buttonrelease(XEvent*); | |
| 38 | − | void on_motionnotify(XEvent*); | |
| 39 | − | void on_configurenotify(XEvent*); | |
| 40 | − | ||
| 41 | 35 | void update_title(); | |
| 42 | 36 | void check_append(const char*); | |
| 43 | 37 | void read_dir_rec(const char*); | |
| 44 | − | ||
| 45 | − | static void (*handler[LASTEvent])(XEvent*); | |
| 38 | + | void run(); | |
| 46 | 39 | ||
| 47 | 40 | img_t img; | |
| 48 | 41 | win_t win; | |
| @@ -52,11 +45,6 @@ win_t win; | |||
| 52 | 45 | const char **filenames; | |
| 53 | 46 | int filecnt, fileidx; | |
| 54 | 47 | ||
| 55 | − | unsigned char timeout; | |
| 56 | − | ||
| 57 | − | int mox; | |
| 58 | − | int moy; | |
| 59 | − | ||
| 60 | 48 | #define TITLE_LEN 256 | |
| 61 | 49 | char win_title[TITLE_LEN]; | |
| 62 | 50 | ||
| @@ -69,39 +57,6 @@ void cleanup() { | |||
| 69 | 57 | } | |
| 70 | 58 | } | |
| 71 | 59 | ||
| 72 | − | void run() { | |
| 73 | − | int xfd; | |
| 74 | − | fd_set fds; | |
| 75 | − | struct timeval t; | |
| 76 | − | XEvent ev; | |
| 77 | − | ||
| 78 | − | handler[KeyPress] = on_keypress; | |
| 79 | − | handler[ButtonPress] = on_buttonpress; | |
| 80 | − | handler[ButtonRelease] = on_buttonrelease; | |
| 81 | − | handler[MotionNotify] = on_motionnotify; | |
| 82 | − | handler[ConfigureNotify] = on_configurenotify; | |
| 83 | − | ||
| 84 | − | timeout = 0; | |
| 85 | − | ||
| 86 | − | while (1) { | |
| 87 | − | if (timeout) { | |
| 88 | − | t.tv_sec = 0; | |
| 89 | − | t.tv_usec = 250; | |
| 90 | − | xfd = ConnectionNumber(win.env.dpy); | |
| 91 | − | FD_ZERO(&fds); | |
| 92 | − | FD_SET(xfd, &fds); | |
| 93 | − | ||
| 94 | − | if (!XPending(win.env.dpy) && !select(xfd + 1, &fds, 0, 0, &t)) { | |
| 95 | − | img_render(&img, &win); | |
| 96 | − | timeout = 0; | |
| 97 | − | } | |
| 98 | − | } | |
| 99 | − | ||
| 100 | − | if (!XNextEvent(win.env.dpy, &ev) && handler[ev.type]) | |
| 101 | − | handler[ev.type](&ev); | |
| 102 | − | } | |
| 103 | − | } | |
| 104 | − | ||
| 105 | 60 | int main(int argc, char **argv) { | |
| 106 | 61 | int i; | |
| 107 | 62 | const char *filename; | |
| @@ -158,15 +113,102 @@ int main(int argc, char **argv) { | |||
| 158 | 113 | return 0; | |
| 159 | 114 | } | |
| 160 | 115 | ||
| 161 | − | void on_keypress(XEvent *ev) { | |
| 116 | + | void update_title() { | |
| 117 | + | int n; | |
| 118 | + | ||
| 119 | + | n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> %s", fileidx + 1, | |
| 120 | + | filecnt, (int) (img.zoom * 100.0), filenames[fileidx]); | |
| 121 | + | ||
| 122 | + | if (n >= TITLE_LEN) { | |
| 123 | + | win_title[TITLE_LEN - 2] = '.'; | |
| 124 | + | win_title[TITLE_LEN - 3] = '.'; | |
| 125 | + | win_title[TITLE_LEN - 4] = '.'; | |
| 126 | + | } | |
| 127 | + | ||
| 128 | + | win_set_title(&win, win_title); | |
| 129 | + | } | |
| 130 | + | ||
| 131 | + | void check_append(const char *filename) { | |
| 132 | + | if (!filename) | |
| 133 | + | return; | |
| 134 | + | ||
| 135 | + | if (img_check(filename)) { | |
| 136 | + | if (fileidx == filecnt) { | |
| 137 | + | filecnt *= 2; | |
| 138 | + | filenames = (const char**) s_realloc(filenames, | |
| 139 | + | filecnt * sizeof(const char*)); | |
| 140 | + | } | |
| 141 | + | filenames[fileidx++] = filename; | |
| 142 | + | } | |
| 143 | + | } | |
| 144 | + | ||
| 145 | + | void read_dir_rec(const char *dirname) { | |
| 146 | + | char *filename; | |
| 147 | + | const char **dirnames; | |
| 148 | + | int dircnt, diridx; | |
| 149 | + | unsigned char first; | |
| 150 | + | size_t len; | |
| 151 | + | DIR *dir; | |
| 152 | + | struct dirent *dentry; | |
| 153 | + | struct stat fstats; | |
| 154 | + | ||
| 155 | + | if (!dirname) | |
| 156 | + | return; | |
| 157 | + | ||
| 158 | + | dircnt = DNAME_CNT; | |
| 159 | + | diridx = first = 1; | |
| 160 | + | dirnames = (const char**) s_malloc(dircnt * sizeof(const char*)); | |
| 161 | + | dirnames[0] = dirname; | |
| 162 | + | ||
| 163 | + | while (diridx > 0) { | |
| 164 | + | dirname = dirnames[--diridx]; | |
| 165 | + | if (!(dir = opendir(dirname))) | |
| 166 | + | die("could not open directory: %s", dirname); | |
| 167 | + | while ((dentry = readdir(dir))) { | |
| 168 | + | if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, "..")) | |
| 169 | + | continue; | |
| 170 | + | len = strlen(dirname) + strlen(dentry->d_name) + 2; | |
| 171 | + | filename = (char*) s_malloc(len * sizeof(char)); | |
| 172 | + | snprintf(filename, len, "%s/%s", dirname, dentry->d_name); | |
| 173 | + | if (stat(filename, &fstats)) { | |
| 174 | + | warn("could not stat file: %s", filename); | |
| 175 | + | free(filename); | |
| 176 | + | } else if (S_ISDIR(fstats.st_mode)) { | |
| 177 | + | if (diridx == dircnt) { | |
| 178 | + | dircnt *= 2; | |
| 179 | + | dirnames = (const char**) s_realloc(dirnames, | |
| 180 | + | dircnt * sizeof(const char*)); | |
| 181 | + | } | |
| 182 | + | dirnames[diridx++] = filename; | |
| 183 | + | } else { | |
| 184 | + | check_append(filename); | |
| 185 | + | } | |
| 186 | + | } | |
| 187 | + | closedir(dir); | |
| 188 | + | if (!first) | |
| 189 | + | free((void*) dirname); | |
| 190 | + | else | |
| 191 | + | first = 0; | |
| 192 | + | } | |
| 193 | + | ||
| 194 | + | free(dirnames); | |
| 195 | + | } | |
| 196 | + | ||
| 197 | + | ||
| 198 | + | /* event handling */ | |
| 199 | + | ||
| 200 | + | unsigned char timeout; | |
| 201 | + | int mox, moy; | |
| 202 | + | ||
| 203 | + | void on_keypress(XKeyEvent *kev) { | |
| 162 | 204 | char key; | |
| 163 | 205 | KeySym ksym; | |
| 164 | 206 | int changed; | |
| 165 | 207 | ||
| 166 | − | if (!ev) | |
| 208 | + | if (!kev) | |
| 167 | 209 | return; | |
| 168 | 210 | ||
| 169 | − | XLookupString(&ev->xkey, &key, 1, &ksym, NULL); | |
| 211 | + | XLookupString(kev, &key, 1, &ksym, NULL); | |
| 170 | 212 | changed = 0; | |
| 171 | 213 | ||
| 172 | 214 | switch (ksym) { | |
| @@ -271,17 +313,17 @@ void on_keypress(XEvent *ev) { | |||
| 271 | 313 | } | |
| 272 | 314 | } | |
| 273 | 315 | ||
| 274 | − | void on_buttonpress(XEvent *ev) { | |
| 316 | + | void on_buttonpress(XButtonEvent *bev) { | |
| 275 | 317 | int changed; | |
| 276 | 318 | unsigned int mask; | |
| 277 | 319 | ||
| 278 | − | if (!ev) | |
| 320 | + | if (!bev) | |
| 279 | 321 | return; | |
| 280 | 322 | ||
| 281 | − | mask = CLEANMASK(ev->xbutton.state); | |
| 323 | + | mask = CLEANMASK(bev->state); | |
| 282 | 324 | changed = 0; | |
| 283 | 325 | ||
| 284 | − | switch (ev->xbutton.button) { | |
| 326 | + | switch (bev->button) { | |
| 285 | 327 | case Button1: | |
| 286 | 328 | if (fileidx + 1 < filecnt) { | |
| 287 | 329 | img_load(&img, filenames[++fileidx]); | |
| @@ -289,8 +331,8 @@ void on_buttonpress(XEvent *ev) { | |||
| 289 | 331 | } | |
| 290 | 332 | break; | |
| 291 | 333 | case Button2: | |
| 292 | − | mox = ev->xbutton.x; | |
| 293 | − | moy = ev->xbutton.y; | |
| 334 | + | mox = bev->x; | |
| 335 | + | moy = bev->y; | |
| 294 | 336 | win_set_cursor(&win, CURSOR_HAND); | |
| 295 | 337 | break; | |
| 296 | 338 | case Button3: | |
| @@ -330,118 +372,63 @@ void on_buttonpress(XEvent *ev) { | |||
| 330 | 372 | } | |
| 331 | 373 | } | |
| 332 | 374 | ||
| 333 | − | void on_buttonrelease(XEvent *ev) { | |
| 334 | − | if (!ev) | |
| 375 | + | void on_motionnotify(XMotionEvent *mev) { | |
| 376 | + | if (!mev) | |
| 335 | 377 | return; | |
| 336 | 378 | ||
| 337 | − | if (ev->xbutton.button == Button2) | |
| 338 | − | win_set_cursor(&win, CURSOR_ARROW); | |
| 339 | − | } | |
| 340 | − | ||
| 341 | − | void on_motionnotify(XEvent *ev) { | |
| 342 | − | XMotionEvent *m; | |
| 343 | − | ||
| 344 | − | if (!ev) | |
| 345 | − | return; | |
| 346 | − | ||
| 347 | − | m = &ev->xmotion; | |
| 348 | − | ||
| 349 | − | if (m->x >= 0 && m->x <= win.w && m->y >= 0 && m->y <= win.h) { | |
| 350 | − | if (img_move(&img, &win, m->x - mox, m->y - moy)) | |
| 379 | + | if (mev->x >= 0 && mev->x <= win.w && mev->y >= 0 && mev->y <= win.h) { | |
| 380 | + | if (img_move(&img, &win, mev->x - mox, mev->y - moy)) | |
| 351 | 381 | timeout = 1; | |
| 352 | 382 | ||
| 353 | − | mox = m->x; | |
| 354 | − | moy = m->y; | |
| 383 | + | mox = mev->x; | |
| 384 | + | moy = mev->y; | |
| 355 | 385 | } | |
| 356 | 386 | } | |
| 357 | 387 | ||
| 358 | − | void on_configurenotify(XEvent *ev) { | |
| 359 | − | if (!ev) | |
| 360 | − | return; | |
| 361 | − | ||
| 362 | − | if (win_configure(&win, &ev->xconfigure)) { | |
| 363 | − | img.checkpan = 1; | |
| 364 | − | timeout = 1; | |
| 365 | − | } | |
| 366 | − | } | |
| 367 | − | ||
| 368 | − | void update_title() { | |
| 369 | − | int n; | |
| 370 | − | ||
| 371 | − | n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> %s", fileidx + 1, | |
| 372 | − | filecnt, (int) (img.zoom * 100.0), filenames[fileidx]); | |
| 373 | − | ||
| 374 | − | if (n >= TITLE_LEN) { | |
| 375 | − | win_title[TITLE_LEN - 2] = '.'; | |
| 376 | − | win_title[TITLE_LEN - 3] = '.'; | |
| 377 | − | win_title[TITLE_LEN - 4] = '.'; | |
| 378 | − | } | |
| 379 | − | ||
| 380 | − | win_set_title(&win, win_title); | |
| 381 | − | } | |
| 388 | + | void run() { | |
| 389 | + | int xfd; | |
| 390 | + | fd_set fds; | |
| 391 | + | struct timeval t; | |
| 392 | + | XEvent ev; | |
| 382 | 393 | ||
| 383 | − | void check_append(const char *filename) { | |
| 384 | − | if (!filename) | |
| 385 | − | return; | |
| 394 | + | timeout = 0; | |
| 386 | 395 | ||
| 387 | − | if (img_check(filename)) { | |
| 388 | − | if (fileidx == filecnt) { | |
| 389 | − | filecnt *= 2; | |
| 390 | − | filenames = (const char**) s_realloc(filenames, | |
| 391 | − | filecnt * sizeof(const char*)); | |
| 396 | + | while (1) { | |
| 397 | + | if (timeout) { | |
| 398 | + | t.tv_sec = 0; | |
| 399 | + | t.tv_usec = 250; | |
| 400 | + | xfd = ConnectionNumber(win.env.dpy); | |
| 401 | + | FD_ZERO(&fds); | |
| 402 | + | FD_SET(xfd, &fds); | |
| 403 | + | ||
| 404 | + | if (!XPending(win.env.dpy) && !select(xfd + 1, &fds, 0, 0, &t)) { | |
| 405 | + | img_render(&img, &win); | |
| 406 | + | timeout = 0; | |
| 407 | + | } | |
| 392 | 408 | } | |
| 393 | − | filenames[fileidx++] = filename; | |
| 394 | − | } | |
| 395 | − | } | |
| 396 | 409 | ||
| 397 | − | void read_dir_rec(const char *dirname) { | |
| 398 | − | char *filename; | |
| 399 | − | const char **dirnames; | |
| 400 | − | int dircnt, diridx; | |
| 401 | − | unsigned char first; | |
| 402 | − | size_t len; | |
| 403 | − | DIR *dir; | |
| 404 | − | struct dirent *dentry; | |
| 405 | − | struct stat fstats; | |
| 406 | − | ||
| 407 | − | if (!dirname) | |
| 408 | − | return; | |
| 409 | − | ||
| 410 | − | dircnt = DNAME_CNT; | |
| 411 | − | diridx = first = 1; | |
| 412 | − | dirnames = (const char**) s_malloc(dircnt * sizeof(const char*)); | |
| 413 | − | dirnames[0] = dirname; | |
| 414 | − | ||
| 415 | − | while (diridx > 0) { | |
| 416 | − | dirname = dirnames[--diridx]; | |
| 417 | − | if (!(dir = opendir(dirname))) | |
| 418 | − | die("could not open directory: %s", dirname); | |
| 419 | − | while ((dentry = readdir(dir))) { | |
| 420 | − | if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, "..")) | |
| 421 | − | continue; | |
| 422 | − | len = strlen(dirname) + strlen(dentry->d_name) + 2; | |
| 423 | − | filename = (char*) s_malloc(len * sizeof(char)); | |
| 424 | − | snprintf(filename, len, "%s/%s", dirname, dentry->d_name); | |
| 425 | − | if (stat(filename, &fstats)) { | |
| 426 | − | warn("could not stat file: %s", filename); | |
| 427 | − | free(filename); | |
| 428 | − | } else if (S_ISDIR(fstats.st_mode)) { | |
| 429 | − | if (diridx == dircnt) { | |
| 430 | − | dircnt *= 2; | |
| 431 | − | dirnames = (const char**) s_realloc(dirnames, | |
| 432 | − | dircnt * sizeof(const char*)); | |
| 433 | − | } | |
| 434 | − | dirnames[diridx++] = filename; | |
| 435 | − | } else { | |
| 436 | − | check_append(filename); | |
| 410 | + | if (!XNextEvent(win.env.dpy, &ev)) { | |
| 411 | + | switch (ev.type) { | |
| 412 | + | case KeyPress: | |
| 413 | + | on_keypress(&ev.xkey); | |
| 414 | + | break; | |
| 415 | + | case ButtonPress: | |
| 416 | + | on_buttonpress(&ev.xbutton); | |
| 417 | + | break; | |
| 418 | + | case ButtonRelease: | |
| 419 | + | if (ev.xbutton.button == Button2) | |
| 420 | + | win_set_cursor(&win, CURSOR_ARROW); | |
| 421 | + | break; | |
| 422 | + | case MotionNotify: | |
| 423 | + | on_motionnotify(&ev.xmotion); | |
| 424 | + | break; | |
| 425 | + | case ConfigureNotify: | |
| 426 | + | if (win_configure(&win, &ev.xconfigure)) { | |
| 427 | + | img.checkpan = 1; | |
| 428 | + | timeout = 1; | |
| 429 | + | } | |
| 430 | + | break; | |
| 437 | 431 | } | |
| 438 | 432 | } | |
| 439 | − | closedir(dir); | |
| 440 | − | if (!first) | |
| 441 | − | free((void*) dirname); | |
| 442 | − | else | |
| 443 | − | first = 0; | |
| 444 | 433 | } | |
| 445 | − | ||
| 446 | − | free(dirnames); | |
| 447 | 434 | } | |