nsxiv5cb6d12
Refactored main.c
+93 −91
2 files changed
| @@ -1,6 +1,6 @@ | |||
| 1 | 1 | all: sxiv | |
| 2 | 2 | ||
| 3 | − | VERSION=git-20110408 | |
| 3 | + | VERSION=git-20110411 | |
| 4 | 4 | ||
| 5 | 5 | CC?=gcc | |
| 6 | 6 | PREFIX?=/usr/local | |
| @@ -45,8 +45,6 @@ typedef enum { | |||
| 45 | 45 | MODE_THUMBS | |
| 46 | 46 | } appmode_t; | |
| 47 | 47 | ||
| 48 | − | void update_title(); | |
| 49 | − | int check_append(const char*); | |
| 50 | 48 | void run(); | |
| 51 | 49 | ||
| 52 | 50 | appmode_t mode; | |
| @@ -92,6 +90,57 @@ int load_image(int new) { | |||
| 92 | 90 | return ret; | |
| 93 | 91 | } | |
| 94 | 92 | ||
| 93 | + | void update_title() { | |
| 94 | + | int n; | |
| 95 | + | float size; | |
| 96 | + | const char *unit; | |
| 97 | + | ||
| 98 | + | if (mode == MODE_THUMBS) { | |
| 99 | + | n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] %s", | |
| 100 | + | tns.cnt ? tns.sel + 1 : 0, tns.cnt, | |
| 101 | + | tns.cnt ? filenames[tns.sel] : ""); | |
| 102 | + | } else { | |
| 103 | + | if (img.im) { | |
| 104 | + | size = filesize; | |
| 105 | + | size_readable(&size, &unit); | |
| 106 | + | n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> (%.2f%s) %s", | |
| 107 | + | fileidx + 1, filecnt, (int) (img.zoom * 100.0), size, unit, | |
| 108 | + | filenames[fileidx]); | |
| 109 | + | } else { | |
| 110 | + | n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] invalid: %s", | |
| 111 | + | fileidx + 1, filecnt, filenames[fileidx]); | |
| 112 | + | } | |
| 113 | + | } | |
| 114 | + | ||
| 115 | + | if (n >= TITLE_LEN) { | |
| 116 | + | win_title[TITLE_LEN - 2] = '.'; | |
| 117 | + | win_title[TITLE_LEN - 3] = '.'; | |
| 118 | + | win_title[TITLE_LEN - 4] = '.'; | |
| 119 | + | } | |
| 120 | + | ||
| 121 | + | win_set_title(&win, win_title); | |
| 122 | + | } | |
| 123 | + | ||
| 124 | + | int check_append(const char *filename) { | |
| 125 | + | if (!filename) | |
| 126 | + | return 0; | |
| 127 | + | ||
| 128 | + | if (access(filename, R_OK)) { | |
| 129 | + | warn("could not open file: %s", filename); | |
| 130 | + | return 0; | |
| 131 | + | } else if (options->all || img_check(filename)) { | |
| 132 | + | if (fileidx == filecnt) { | |
| 133 | + | filecnt *= 2; | |
| 134 | + | filenames = (const char**) s_realloc(filenames, | |
| 135 | + | filecnt * sizeof(const char*)); | |
| 136 | + | } | |
| 137 | + | filenames[fileidx++] = filename; | |
| 138 | + | return 1; | |
| 139 | + | } else { | |
| 140 | + | return 0; | |
| 141 | + | } | |
| 142 | + | } | |
| 143 | + | ||
| 95 | 144 | int fncmp(const void *a, const void *b) { | |
| 96 | 145 | return strcoll(*((char* const*) a), *((char* const*) b)); | |
| 97 | 146 | } | |
| @@ -185,57 +234,6 @@ int main(int argc, char **argv) { | |||
| 185 | 234 | return 0; | |
| 186 | 235 | } | |
| 187 | 236 | ||
| 188 | − | void update_title() { | |
| 189 | − | int n; | |
| 190 | − | float size; | |
| 191 | − | const char *unit; | |
| 192 | − | ||
| 193 | − | if (mode == MODE_THUMBS) { | |
| 194 | − | n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] %s", | |
| 195 | − | tns.cnt ? tns.sel + 1 : 0, tns.cnt, | |
| 196 | − | tns.cnt ? filenames[tns.sel] : ""); | |
| 197 | − | } else { | |
| 198 | − | if (img.im) { | |
| 199 | − | size = filesize; | |
| 200 | − | size_readable(&size, &unit); | |
| 201 | − | n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> (%.2f%s) %s", | |
| 202 | − | fileidx + 1, filecnt, (int) (img.zoom * 100.0), size, unit, | |
| 203 | − | filenames[fileidx]); | |
| 204 | − | } else { | |
| 205 | − | n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] invalid: %s", | |
| 206 | − | fileidx + 1, filecnt, filenames[fileidx]); | |
| 207 | − | } | |
| 208 | − | } | |
| 209 | − | ||
| 210 | − | if (n >= TITLE_LEN) { | |
| 211 | − | win_title[TITLE_LEN - 2] = '.'; | |
| 212 | − | win_title[TITLE_LEN - 3] = '.'; | |
| 213 | − | win_title[TITLE_LEN - 4] = '.'; | |
| 214 | − | } | |
| 215 | − | ||
| 216 | − | win_set_title(&win, win_title); | |
| 217 | − | } | |
| 218 | − | ||
| 219 | − | int check_append(const char *filename) { | |
| 220 | − | if (!filename) | |
| 221 | − | return 0; | |
| 222 | − | ||
| 223 | − | if (access(filename, R_OK)) { | |
| 224 | − | warn("could not open file: %s", filename); | |
| 225 | − | return 0; | |
| 226 | − | } else if (options->all || img_check(filename)) { | |
| 227 | − | if (fileidx == filecnt) { | |
| 228 | − | filecnt *= 2; | |
| 229 | − | filenames = (const char**) s_realloc(filenames, | |
| 230 | − | filecnt * sizeof(const char*)); | |
| 231 | − | } | |
| 232 | − | filenames[fileidx++] = filename; | |
| 233 | − | return 1; | |
| 234 | − | } else { | |
| 235 | − | return 0; | |
| 236 | − | } | |
| 237 | − | } | |
| 238 | − | ||
| 239 | 237 | #if EXT_COMMANDS | |
| 240 | 238 | int run_command(const char *cline, Bool reload) { | |
| 241 | 239 | int fncnt, fnlen; | |
| @@ -294,6 +292,29 @@ int run_command(const char *cline, Bool reload) { | |||
| 294 | 292 | } | |
| 295 | 293 | #endif /* EXT_COMMANDS */ | |
| 296 | 294 | ||
| 295 | + | void remove_file(int n) { | |
| 296 | + | if (n < 0 || n >= filecnt) | |
| 297 | + | return; | |
| 298 | + | ||
| 299 | + | if (filecnt == 1) { | |
| 300 | + | cleanup(); | |
| 301 | + | exit(0); | |
| 302 | + | } | |
| 303 | + | ||
| 304 | + | if (n + 1 < filecnt) | |
| 305 | + | memmove(filenames + n, filenames + n + 1, (filecnt - n - 1) * | |
| 306 | + | sizeof(const char*)); | |
| 307 | + | if (n + 1 < tns.cnt) { | |
| 308 | + | memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) * | |
| 309 | + | sizeof(thumb_t)); | |
| 310 | + | memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t)); | |
| 311 | + | } | |
| 312 | + | ||
| 313 | + | --filecnt; | |
| 314 | + | if (n < tns.cnt) | |
| 315 | + | --tns.cnt; | |
| 316 | + | } | |
| 317 | + | ||
| 297 | 318 | ||
| 298 | 319 | /* event handling */ | |
| 299 | 320 | ||
| @@ -322,7 +343,7 @@ void redraw() { | |||
| 322 | 343 | } | |
| 323 | 344 | ||
| 324 | 345 | void on_keypress(XKeyEvent *kev) { | |
| 325 | − | int i, x, y; | |
| 346 | + | int x, y; | |
| 326 | 347 | unsigned int w, h; | |
| 327 | 348 | char key; | |
| 328 | 349 | KeySym ksym; | |
| @@ -337,10 +358,10 @@ void on_keypress(XKeyEvent *kev) { | |||
| 337 | 358 | #if EXT_COMMANDS | |
| 338 | 359 | /* external commands from commands.h */ | |
| 339 | 360 | if (CLEANMASK(kev->state) & ControlMask) { | |
| 340 | − | for (i = 0; i < LEN(commands); ++i) { | |
| 341 | − | if (commands[i].ksym == ksym) { | |
| 361 | + | for (x = 0; x < LEN(commands); ++x) { | |
| 362 | + | if (commands[x].ksym == ksym) { | |
| 342 | 363 | win_set_cursor(&win, CURSOR_WATCH); | |
| 343 | − | if (run_command(commands[i].cmdline, commands[i].reload)) { | |
| 364 | + | if (run_command(commands[x].cmdline, commands[x].reload)) { | |
| 344 | 365 | if (mode == MODE_NORMAL) { | |
| 345 | 366 | img_close(&img, 1); | |
| 346 | 367 | load_image(fileidx); | |
| @@ -465,6 +486,10 @@ void on_keypress(XKeyEvent *kev) { | |||
| 465 | 486 | img.alpha ^= 1; | |
| 466 | 487 | changed = 1; | |
| 467 | 488 | break; | |
| 489 | + | case XK_D: | |
| 490 | + | remove_file(fileidx); | |
| 491 | + | changed = load_image(fileidx >= filecnt ? filecnt - 1 : fileidx); | |
| 492 | + | break; | |
| 468 | 493 | case XK_r: | |
| 469 | 494 | changed = load_image(fileidx); | |
| 470 | 495 | break; | |
| @@ -507,6 +532,16 @@ void on_keypress(XKeyEvent *kev) { | |||
| 507 | 532 | tns.sel = tns.cnt - 1; | |
| 508 | 533 | changed = tns.dirty = 1; | |
| 509 | 534 | } | |
| 535 | + | ||
| 536 | + | /* miscellaneous */ | |
| 537 | + | case XK_D: | |
| 538 | + | if (tns.sel < tns.cnt) { | |
| 539 | + | remove_file(tns.sel); | |
| 540 | + | changed = tns.dirty = 1; | |
| 541 | + | if (tns.sel >= tns.cnt) | |
| 542 | + | tns.sel = tns.cnt - 1; | |
| 543 | + | } | |
| 544 | + | break; | |
| 510 | 545 | } | |
| 511 | 546 | } | |
| 512 | 547 | ||
| @@ -515,43 +550,10 @@ void on_keypress(XKeyEvent *kev) { | |||
| 515 | 550 | case XK_q: | |
| 516 | 551 | cleanup(); | |
| 517 | 552 | exit(0); | |
| 518 | − | ||
| 519 | 553 | case XK_f: | |
| 520 | 554 | win_toggle_fullscreen(&win); | |
| 521 | 555 | /* render on next configurenotify */ | |
| 522 | 556 | break; | |
| 523 | − | ||
| 524 | − | case XK_D: | |
| 525 | − | if (mode == MODE_THUMBS) { | |
| 526 | − | if (tns.sel >= tns.cnt) | |
| 527 | − | break; | |
| 528 | − | i = tns.sel; | |
| 529 | − | } else { | |
| 530 | − | i = fileidx; | |
| 531 | − | } | |
| 532 | − | if (filecnt == 1) { | |
| 533 | − | cleanup(); | |
| 534 | − | exit(0); | |
| 535 | − | } | |
| 536 | − | if (i + 1 < filecnt) | |
| 537 | − | memmove(filenames + i, filenames + i + 1, (filecnt - i - 1) * | |
| 538 | − | sizeof(const char*)); | |
| 539 | − | else if (fileidx) | |
| 540 | − | fileidx--; | |
| 541 | − | if (i + 1 < tns.cnt) { | |
| 542 | − | memmove(tns.thumbs + i, tns.thumbs + i + 1, (tns.cnt - i - 1) * | |
| 543 | − | sizeof(thumb_t)); | |
| 544 | − | memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t)); | |
| 545 | − | } else if (tns.sel) { | |
| 546 | − | tns.sel--; | |
| 547 | − | } | |
| 548 | − | filecnt--; | |
| 549 | − | if (mode == MODE_NORMAL) | |
| 550 | − | load_image(fileidx); | |
| 551 | − | if (i < tns.cnt) | |
| 552 | − | tns.cnt--; | |
| 553 | − | changed = tns.dirty = 1; | |
| 554 | − | break; | |
| 555 | 557 | } | |
| 556 | 558 | ||
| 557 | 559 | if (changed) | |