nsxiv1449bfc
code-style: fix consistency issues all over the codebase (#94)
* remove duplicate comment * remove empty tabs and blank lines * move macros and globals ontop * comment to seprate function implementation * fix alignment * switch to *argv[] similar to other suckless code * kill all empty last lines * append comment to endif * reuse existing ARRLEN macro * comment fall through * use while (true) everywhere
+45 −68
11 files changed
- M autoreload_inotify.c +6 −7
- M autoreload_nop.c +0 −1
- M commands.c +3 −5
- M commands.lst +0 −1
- M config.def.h +1 −1
- M image.c +2 −1
- M main.c +10 −8
- M nsxiv.h +0 −7
- M thumbs.c +2 −4
- M util.c +2 −3
- M window.c +19 −30
| @@ -24,6 +24,11 @@ | |||
| 24 | 24 | #include <unistd.h> | |
| 25 | 25 | #include <sys/inotify.h> | |
| 26 | 26 | ||
| 27 | + | union { | |
| 28 | + | char d[4096]; /* aligned buffer */ | |
| 29 | + | struct inotify_event e; | |
| 30 | + | } buf; | |
| 31 | + | ||
| 27 | 32 | void arl_init(arl_t *arl) | |
| 28 | 33 | { | |
| 29 | 34 | arl->fd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK); | |
| @@ -76,18 +81,13 @@ void arl_setup(arl_t *arl, const char *filepath) | |||
| 76 | 81 | } | |
| 77 | 82 | } | |
| 78 | 83 | ||
| 79 | − | union { | |
| 80 | − | char d[4096]; /* aligned buffer */ | |
| 81 | − | struct inotify_event e; | |
| 82 | − | } buf; | |
| 83 | − | ||
| 84 | 84 | bool arl_handle(arl_t *arl) | |
| 85 | 85 | { | |
| 86 | 86 | bool reload = false; | |
| 87 | 87 | char *ptr; | |
| 88 | 88 | const struct inotify_event *e; | |
| 89 | 89 | ||
| 90 | − | for (;;) { | |
| 90 | + | while (true) { | |
| 91 | 91 | ssize_t len = read(arl->fd, buf.d, sizeof(buf.d)); | |
| 92 | 92 | ||
| 93 | 93 | if (len == -1) { | |
| @@ -109,4 +109,3 @@ bool arl_handle(arl_t *arl) | |||
| 109 | 109 | } | |
| 110 | 110 | return reload; | |
| 111 | 111 | } | |
| 112 | − | ||
| @@ -39,4 +39,3 @@ bool arl_handle(arl_t *arl) | |||
| 39 | 39 | (void) arl; | |
| 40 | 40 | return false; | |
| 41 | 41 | } | |
| 42 | − | ||
| @@ -233,7 +233,7 @@ bool cg_navigate_marked(arg_t n) | |||
| 233 | 233 | { | |
| 234 | 234 | int d, i; | |
| 235 | 235 | int new = fileidx; | |
| 236 | − | ||
| 236 | + | ||
| 237 | 237 | if (prefix > 0) | |
| 238 | 238 | n *= prefix; | |
| 239 | 239 | d = n > 0 ? 1 : -1; | |
| @@ -337,14 +337,13 @@ bool ci_drag(arg_t mode) | |||
| 337 | 337 | ||
| 338 | 338 | if ((int)(img.w * img.zoom) <= win.w && (int)(img.h * img.zoom) <= win.h) | |
| 339 | 339 | return false; | |
| 340 | − | ||
| 341 | − | win_set_cursor(&win, CURSOR_DRAG); | |
| 342 | 340 | ||
| 341 | + | win_set_cursor(&win, CURSOR_DRAG); | |
| 343 | 342 | win_cursor_pos(&win, &x, &y); | |
| 344 | 343 | ox = x; | |
| 345 | 344 | oy = y; | |
| 346 | 345 | ||
| 347 | − | for (;;) { | |
| 346 | + | while (true) { | |
| 348 | 347 | if (mode == DRAG_ABSOLUTE) { | |
| 349 | 348 | px = MIN(MAX(0.0, x - win.w*0.1), win.w*0.8) / (win.w*0.8) | |
| 350 | 349 | * (win.w - img.w * img.zoom); | |
| @@ -449,4 +448,3 @@ bool ct_reload_all(arg_t _) | |||
| 449 | 448 | const cmd_t cmds[CMD_COUNT] = { | |
| 450 | 449 | #include "commands.lst" | |
| 451 | 450 | }; | |
| 452 | − | ||
| @@ -34,4 +34,3 @@ I_CMD(slideshow) | |||
| 34 | 34 | ||
| 35 | 35 | T_CMD(move_sel) | |
| 36 | 36 | T_CMD(reload_all) | |
| 37 | − | ||
| @@ -63,7 +63,7 @@ static const int THUMB_SIZE = 3; | |||
| 63 | 63 | #endif | |
| 64 | 64 | #ifdef _MAPPINGS_CONFIG | |
| 65 | 65 | ||
| 66 | − | /* Following modifiers (NumLock | CapsLock) will be ignored when processing keybindings */ | |
| 66 | + | /* following modifiers (NumLock | CapsLock) will be ignored when processing keybindings */ | |
| 67 | 67 | static const int ignore_mask = Mod2Mask | LockMask; | |
| 68 | 68 | ||
| 69 | 69 | /* abort the keyhandler */ | |
| @@ -89,6 +89,7 @@ void exif_auto_orientate(const fileinfo_t *file) | |||
| 89 | 89 | switch (orientation) { | |
| 90 | 90 | case 5: | |
| 91 | 91 | imlib_image_orientate(1); | |
| 92 | + | /* fall through */ | |
| 92 | 93 | case 2: | |
| 93 | 94 | imlib_image_flip_vertical(); | |
| 94 | 95 | break; | |
| @@ -97,6 +98,7 @@ void exif_auto_orientate(const fileinfo_t *file) | |||
| 97 | 98 | break; | |
| 98 | 99 | case 7: | |
| 99 | 100 | imlib_image_orientate(1); | |
| 101 | + | /* fall through */ | |
| 100 | 102 | case 4: | |
| 101 | 103 | imlib_image_flip_horizontal(); | |
| 102 | 104 | break; | |
| @@ -916,4 +918,3 @@ bool img_frame_animate(img_t *img) | |||
| 916 | 918 | img->dirty = true; | |
| 917 | 919 | return true; | |
| 918 | 920 | } | |
| 919 | − | ||
| @@ -34,6 +34,9 @@ | |||
| 34 | 34 | #include <X11/keysym.h> | |
| 35 | 35 | #include <X11/XF86keysym.h> | |
| 36 | 36 | ||
| 37 | + | #define MODMASK(mask) ((mask) & ~ignore_mask) | |
| 38 | + | #define BAR_SEP " " | |
| 39 | + | ||
| 37 | 40 | typedef struct { | |
| 38 | 41 | struct timeval when; | |
| 39 | 42 | bool active; | |
| @@ -93,6 +96,9 @@ cursor_t imgcursor[3] = { | |||
| 93 | 96 | CURSOR_ARROW, CURSOR_ARROW, CURSOR_ARROW | |
| 94 | 97 | }; | |
| 95 | 98 | ||
| 99 | + | /************************** | |
| 100 | + | function implementations | |
| 101 | + | **************************/ | |
| 96 | 102 | void cleanup(void) | |
| 97 | 103 | { | |
| 98 | 104 | img_close(&img, false); | |
| @@ -346,8 +352,6 @@ void bar_put(win_bar_t *bar, const char *fmt, ...) | |||
| 346 | 352 | va_end(ap); | |
| 347 | 353 | } | |
| 348 | 354 | ||
| 349 | − | #define BAR_SEP " " | |
| 350 | − | ||
| 351 | 355 | void update_info(void) | |
| 352 | 356 | { | |
| 353 | 357 | unsigned int i, fn, fw; | |
| @@ -554,7 +558,7 @@ void run_key_handler(const char *key, unsigned int mask) | |||
| 554 | 558 | for (f = i = 0; f < fcnt; i++) { | |
| 555 | 559 | if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) { | |
| 556 | 560 | if (stat(files[i].path, &st) != 0 || | |
| 557 | − | memcmp(&oldst[f].st_mtime, &st.st_mtime, sizeof(st.st_mtime)) != 0) | |
| 561 | + | memcmp(&oldst[f].st_mtime, &st.st_mtime, sizeof(st.st_mtime)) != 0) | |
| 558 | 562 | { | |
| 559 | 563 | if (tns.thumbs != NULL) { | |
| 560 | 564 | tns_unload(&tns, i); | |
| @@ -582,8 +586,6 @@ end: | |||
| 582 | 586 | redraw(); | |
| 583 | 587 | } | |
| 584 | 588 | ||
| 585 | − | #define MODMASK(mask) ((mask) & ~ignore_mask) | |
| 586 | − | ||
| 587 | 589 | void on_keypress(XKeyEvent *kev) | |
| 588 | 590 | { | |
| 589 | 591 | int i; | |
| @@ -607,7 +609,7 @@ void on_keypress(XKeyEvent *kev) | |||
| 607 | 609 | handle_key_handler(false); | |
| 608 | 610 | } else if (extprefix) { | |
| 609 | 611 | run_key_handler(XKeysymToString(ksym), kev->state & ~sh); | |
| 610 | − | extprefix = False; | |
| 612 | + | extprefix = false; | |
| 611 | 613 | } else if (key >= '0' && key <= '9') { | |
| 612 | 614 | /* number prefix for commands */ | |
| 613 | 615 | prefix = prefix * 10 + (int) (key - '0'); | |
| @@ -675,7 +677,7 @@ void on_buttonpress(XButtonEvent *bev) | |||
| 675 | 677 | bool on = !(files[sel].flags & FF_MARK); | |
| 676 | 678 | XEvent e; | |
| 677 | 679 | ||
| 678 | − | for (;;) { | |
| 680 | + | while (true) { | |
| 679 | 681 | if (sel >= 0 && mark_image(sel, on)) | |
| 680 | 682 | redraw(); | |
| 681 | 683 | XMaskEvent(win.env.dpy, | |
| @@ -836,7 +838,7 @@ void setup_signal(int sig, void (*handler)(int sig)) | |||
| 836 | 838 | error(EXIT_FAILURE, errno, "signal %d", sig); | |
| 837 | 839 | } | |
| 838 | 840 | ||
| 839 | − | int main(int argc, char **argv) | |
| 841 | + | int main(int argc, char *argv[]) | |
| 840 | 842 | { | |
| 841 | 843 | int i, start; | |
| 842 | 844 | size_t n; | |
| @@ -33,15 +33,9 @@ | |||
| 33 | 33 | */ | |
| 34 | 34 | #define CLEANUP | |
| 35 | 35 | ||
| 36 | − | #ifndef MIN | |
| 37 | 36 | #define MIN(a,b) ((a) < (b) ? (a) : (b)) | |
| 38 | − | #endif | |
| 39 | − | #ifndef MAX | |
| 40 | 37 | #define MAX(a,b) ((a) > (b) ? (a) : (b)) | |
| 41 | − | #endif | |
| 42 | − | #ifndef ABS | |
| 43 | 38 | #define ABS(a) ((a) > 0 ? (a) : -(a)) | |
| 44 | − | #endif | |
| 45 | 39 | ||
| 46 | 40 | #define ARRLEN(a) (sizeof(a) / sizeof((a)[0])) | |
| 47 | 41 | ||
| @@ -465,4 +459,3 @@ void win_set_cursor(win_t*, cursor_t); | |||
| 465 | 459 | void win_cursor_pos(win_t*, int*, int*); | |
| 466 | 460 | ||
| 467 | 461 | #endif /* NSXIV_H */ | |
| 468 | − | ||
| @@ -143,9 +143,7 @@ void tns_clean_cache(tns_t *tns) | |||
| 143 | 143 | r_closedir(&dir); | |
| 144 | 144 | } | |
| 145 | 145 | ||
| 146 | − | ||
| 147 | − | void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel, | |
| 148 | − | win_t *win) | |
| 146 | + | void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel, win_t *win) | |
| 149 | 147 | { | |
| 150 | 148 | int len; | |
| 151 | 149 | const char *homedir, *dsuffix = ""; | |
| @@ -320,7 +318,7 @@ bool tns_load(tns_t *tns, int n, bool force, bool cache_only) | |||
| 320 | 318 | } | |
| 321 | 319 | exif_data_unref(ed); | |
| 322 | 320 | } | |
| 323 | − | #endif | |
| 321 | + | #endif /* HAVE_LIBEXIF */ | |
| 324 | 322 | } | |
| 325 | 323 | } | |
| 326 | 324 | ||
| @@ -30,7 +30,7 @@ const char *progname; | |||
| 30 | 30 | void* emalloc(size_t size) | |
| 31 | 31 | { | |
| 32 | 32 | void *ptr; | |
| 33 | − | ||
| 33 | + | ||
| 34 | 34 | ptr = malloc(size); | |
| 35 | 35 | if (ptr == NULL) | |
| 36 | 36 | error(EXIT_FAILURE, errno, NULL); | |
| @@ -172,7 +172,7 @@ char* r_readdir(r_dir_t *rdir, bool skip_dotfiles) | |||
| 172 | 172 | } | |
| 173 | 173 | return filename; | |
| 174 | 174 | } | |
| 175 | − | ||
| 175 | + | ||
| 176 | 176 | if (rdir->recursive && rdir->stlen > 0) { | |
| 177 | 177 | /* open next subdirectory */ | |
| 178 | 178 | closedir(rdir->dir); | |
| @@ -210,4 +210,3 @@ int r_mkdir(char *path) | |||
| 210 | 210 | } | |
| 211 | 211 | return 0; | |
| 212 | 212 | } | |
| 213 | − | ||
| @@ -33,10 +33,15 @@ | |||
| 33 | 33 | #include "utf8.h" | |
| 34 | 34 | static XftFont *font; | |
| 35 | 35 | static double fontsize; | |
| 36 | + | #define TEXTWIDTH(win, text, len) \ | |
| 37 | + | win_draw_text(win, NULL, NULL, 0, 0, text, len, 0) | |
| 36 | 38 | #endif | |
| 37 | 39 | ||
| 38 | 40 | #define RES_CLASS "Nsxiv" | |
| 39 | 41 | ||
| 42 | + | #define INIT_ATOM_(atom) \ | |
| 43 | + | atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False); | |
| 44 | + | ||
| 40 | 45 | enum { | |
| 41 | 46 | H_TEXT_PAD = 5, | |
| 42 | 47 | V_TEXT_PAD = 1 | |
| @@ -98,9 +103,6 @@ const char* win_res(XrmDatabase db, const char *name, const char *def) | |||
| 98 | 103 | } | |
| 99 | 104 | } | |
| 100 | 105 | ||
| 101 | − | #define INIT_ATOM_(atom) \ | |
| 102 | − | atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False); | |
| 103 | − | ||
| 104 | 106 | void win_init(win_t *win) | |
| 105 | 107 | { | |
| 106 | 108 | win_env_t *e; | |
| @@ -194,7 +196,7 @@ void win_open(win_t *win) | |||
| 194 | 196 | XSizeHints sizehints; | |
| 195 | 197 | XWMHints hints; | |
| 196 | 198 | pid_t pid; | |
| 197 | − | char hostname[255]; | |
| 199 | + | char hostname[256]; | |
| 198 | 200 | ||
| 199 | 201 | e = &win->env; | |
| 200 | 202 | parent = options->embed != 0 ? options->embed : RootWindow(e->dpy, e->scr); | |
| @@ -229,30 +231,26 @@ void win_open(win_t *win) | |||
| 229 | 231 | if ((gmask & YNegative) != 0) { | |
| 230 | 232 | win->y += e->scrh - win->h; | |
| 231 | 233 | sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity | |
| 232 | − | ? SouthEastGravity : SouthWestGravity; | |
| 234 | + | ? SouthEastGravity : SouthWestGravity; | |
| 233 | 235 | } | |
| 234 | 236 | sizehints.flags |= USPosition; | |
| 235 | 237 | } else { | |
| 236 | 238 | win->y = 0; | |
| 237 | 239 | } | |
| 238 | 240 | ||
| 239 | − | win->xwin = XCreateWindow(e->dpy, parent, | |
| 240 | − | win->x, win->y, win->w, win->h, 0, | |
| 241 | + | win->xwin = XCreateWindow(e->dpy, parent, win->x, win->y, win->w, win->h, 0, | |
| 241 | 242 | e->depth, InputOutput, e->vis, 0, NULL); | |
| 242 | 243 | if (win->xwin == None) | |
| 243 | 244 | error(EXIT_FAILURE, 0, "Error creating X window"); | |
| 244 | 245 | ||
| 245 | 246 | /* set the _NET_WM_PID */ | |
| 246 | 247 | pid = getpid(); | |
| 247 | − | XChangeProperty(e->dpy, win->xwin, | |
| 248 | − | atoms[ATOM__NET_WM_PID], XA_CARDINAL, sizeof(pid_t) * 8, | |
| 249 | − | PropModeReplace, (unsigned char *) &pid, 1); | |
| 250 | − | ||
| 251 | − | /* set the _NET_WM_PID */ | |
| 252 | − | if (gethostname(hostname, sizeof(hostname)) == 0) { | |
| 248 | + | XChangeProperty(e->dpy, win->xwin, atoms[ATOM__NET_WM_PID], XA_CARDINAL, | |
| 249 | + | sizeof(pid_t) * 8, PropModeReplace, (unsigned char *) &pid, 1); | |
| 250 | + | if (gethostname(hostname, ARRLEN(hostname)) == 0) { | |
| 253 | 251 | XTextProperty tp; | |
| 254 | 252 | tp.value = (unsigned char *)hostname; | |
| 255 | − | tp.nitems = strnlen(hostname, sizeof(hostname)); | |
| 253 | + | tp.nitems = strnlen(hostname, ARRLEN(hostname)); | |
| 256 | 254 | tp.encoding = XA_STRING; | |
| 257 | 255 | tp.format = 8; | |
| 258 | 256 | XSetWMClientMachine(e->dpy, win->xwin, &tp); | |
| @@ -266,11 +264,9 @@ void win_open(win_t *win) | |||
| 266 | 264 | if (i != CURSOR_NONE) | |
| 267 | 265 | cursors[i].icon = XCreateFontCursor(e->dpy, cursors[i].name); | |
| 268 | 266 | } | |
| 269 | − | if (XAllocNamedColor(e->dpy, e->cmap, "black", | |
| 270 | − | &col, &col) == 0) | |
| 271 | − | { | |
| 267 | + | if (XAllocNamedColor(e->dpy, e->cmap, "black", &col, &col) == 0) | |
| 272 | 268 | error(EXIT_FAILURE, 0, "Error allocating color 'black'"); | |
| 273 | − | } | |
| 269 | + | ||
| 274 | 270 | none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8); | |
| 275 | 271 | *cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0); | |
| 276 | 272 | ||
| @@ -288,8 +284,7 @@ void win_open(win_t *win) | |||
| 288 | 284 | for (c = icons[i].data[j] >> 4; c >= 0; c--) | |
| 289 | 285 | icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F]; | |
| 290 | 286 | } | |
| 291 | − | XChangeProperty(e->dpy, win->xwin, | |
| 292 | − | atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32, | |
| 287 | + | XChangeProperty(e->dpy, win->xwin, atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32, | |
| 293 | 288 | i == 0 ? PropModeReplace : PropModeAppend, | |
| 294 | 289 | (unsigned char *) icon_data, n); | |
| 295 | 290 | } | |
| @@ -320,12 +315,11 @@ void win_open(win_t *win) | |||
| 320 | 315 | ||
| 321 | 316 | win->buf.w = e->scrw; | |
| 322 | 317 | win->buf.h = e->scrh; | |
| 323 | − | win->buf.pm = XCreatePixmap(e->dpy, win->xwin, | |
| 324 | − | win->buf.w, win->buf.h, e->depth); | |
| 318 | + | win->buf.pm = XCreatePixmap(e->dpy, win->xwin, win->buf.w, win->buf.h, e->depth); | |
| 319 | + | ||
| 325 | 320 | XSetForeground(e->dpy, gc, win->win_bg); | |
| 326 | 321 | XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h); | |
| 327 | 322 | XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm); | |
| 328 | − | ||
| 329 | 323 | XMapWindow(e->dpy, win->xwin); | |
| 330 | 324 | XFlush(e->dpy); | |
| 331 | 325 | ||
| @@ -409,9 +403,6 @@ void win_clear(win_t *win) | |||
| 409 | 403 | } | |
| 410 | 404 | ||
| 411 | 405 | #if HAVE_LIBFONTS | |
| 412 | − | #define TEXTWIDTH(win, text, len) \ | |
| 413 | − | win_draw_text(win, NULL, NULL, 0, 0, text, len, 0) | |
| 414 | − | ||
| 415 | 406 | int win_draw_text(win_t *win, XftDraw *d, const XftColor *color, int x, int y, | |
| 416 | 407 | char *text, int len, int w) | |
| 417 | 408 | { | |
| @@ -459,8 +450,7 @@ void win_draw_bar(win_t *win) | |||
| 459 | 450 | e = &win->env; | |
| 460 | 451 | y = win->h + font->ascent + V_TEXT_PAD; | |
| 461 | 452 | w = win->w - 2*H_TEXT_PAD; | |
| 462 | − | d = XftDrawCreate(e->dpy, win->buf.pm, e->vis, | |
| 463 | − | e->cmap); | |
| 453 | + | d = XftDrawCreate(e->dpy, win->buf.pm, e->vis, e->cmap); | |
| 464 | 454 | ||
| 465 | 455 | XSetForeground(e->dpy, gc, win->bar_bg.pixel); | |
| 466 | 456 | XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h); | |
| @@ -522,7 +512,7 @@ void win_set_title(win_t *win, const char *path) | |||
| 522 | 512 | return; | |
| 523 | 513 | ||
| 524 | 514 | snprintf(title, title_max, "%s%s", options->title_prefix, | |
| 525 | − | (options->title_suffixmode == SUFFIX_BASENAME) ? basename : path); | |
| 515 | + | options->title_suffixmode == SUFFIX_BASENAME ? basename : path); | |
| 526 | 516 | if (options->title_suffixmode == SUFFIX_EMPTY) | |
| 527 | 517 | *(title+strlen(options->title_prefix)) = '\0'; | |
| 528 | 518 | ||
| @@ -551,4 +541,3 @@ void win_cursor_pos(win_t *win, int *x, int *y) | |||
| 551 | 541 | if (!XQueryPointer(win->env.dpy, win->xwin, &w, &w, &i, &i, x, y, &ui)) | |
| 552 | 542 | *x = *y = 0; | |
| 553 | 543 | } | |
| 554 | − | ||