From 1449bfc5e960a61438c60a67c28da6d7c5bf2c7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berke=20Kocao=C4=9Flu?= Date: Mon, 11 Oct 2021 06:07:18 +0300 Subject: [PATCH] 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 Co-authored-by: NRK --- autoreload_inotify.c | 13 ++++++------ autoreload_nop.c | 1 - commands.c | 8 +++----- commands.lst | 1 - config.def.h | 2 +- image.c | 3 ++- main.c | 18 ++++++++-------- nsxiv.h | 7 ------- thumbs.c | 6 ++---- util.c | 5 ++--- window.c | 49 +++++++++++++++++--------------------------- 11 files changed, 45 insertions(+), 68 deletions(-) diff --git a/autoreload_inotify.c b/autoreload_inotify.c index 7982e0d..6bcbd04 100644 --- a/autoreload_inotify.c +++ b/autoreload_inotify.c @@ -24,6 +24,11 @@ #include #include +union { + char d[4096]; /* aligned buffer */ + struct inotify_event e; +} buf; + void arl_init(arl_t *arl) { arl->fd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK); @@ -76,18 +81,13 @@ void arl_setup(arl_t *arl, const char *filepath) } } -union { - char d[4096]; /* aligned buffer */ - struct inotify_event e; -} buf; - bool arl_handle(arl_t *arl) { bool reload = false; char *ptr; const struct inotify_event *e; - for (;;) { + while (true) { ssize_t len = read(arl->fd, buf.d, sizeof(buf.d)); if (len == -1) { @@ -109,4 +109,3 @@ bool arl_handle(arl_t *arl) } return reload; } - diff --git a/autoreload_nop.c b/autoreload_nop.c index 4f5702f..431566d 100644 --- a/autoreload_nop.c +++ b/autoreload_nop.c @@ -39,4 +39,3 @@ bool arl_handle(arl_t *arl) (void) arl; return false; } - diff --git a/commands.c b/commands.c index 8ab1247..3981f0f 100644 --- a/commands.c +++ b/commands.c @@ -233,7 +233,7 @@ bool cg_navigate_marked(arg_t n) { int d, i; int new = fileidx; - + if (prefix > 0) n *= prefix; d = n > 0 ? 1 : -1; @@ -337,14 +337,13 @@ bool ci_drag(arg_t mode) if ((int)(img.w * img.zoom) <= win.w && (int)(img.h * img.zoom) <= win.h) return false; - - win_set_cursor(&win, CURSOR_DRAG); + win_set_cursor(&win, CURSOR_DRAG); win_cursor_pos(&win, &x, &y); ox = x; oy = y; - for (;;) { + while (true) { if (mode == DRAG_ABSOLUTE) { px = MIN(MAX(0.0, x - win.w*0.1), win.w*0.8) / (win.w*0.8) * (win.w - img.w * img.zoom); @@ -449,4 +448,3 @@ bool ct_reload_all(arg_t _) const cmd_t cmds[CMD_COUNT] = { #include "commands.lst" }; - diff --git a/commands.lst b/commands.lst index 06e7d78..a97ca1c 100644 --- a/commands.lst +++ b/commands.lst @@ -34,4 +34,3 @@ I_CMD(slideshow) T_CMD(move_sel) T_CMD(reload_all) - diff --git a/config.def.h b/config.def.h index e67fcb8..1a23fc8 100644 --- a/config.def.h +++ b/config.def.h @@ -63,7 +63,7 @@ static const int THUMB_SIZE = 3; #endif #ifdef _MAPPINGS_CONFIG -/* Following modifiers (NumLock | CapsLock) will be ignored when processing keybindings */ +/* following modifiers (NumLock | CapsLock) will be ignored when processing keybindings */ static const int ignore_mask = Mod2Mask | LockMask; /* abort the keyhandler */ diff --git a/image.c b/image.c index 43bd23c..0349d12 100644 --- a/image.c +++ b/image.c @@ -89,6 +89,7 @@ void exif_auto_orientate(const fileinfo_t *file) switch (orientation) { case 5: imlib_image_orientate(1); + /* fall through */ case 2: imlib_image_flip_vertical(); break; @@ -97,6 +98,7 @@ void exif_auto_orientate(const fileinfo_t *file) break; case 7: imlib_image_orientate(1); + /* fall through */ case 4: imlib_image_flip_horizontal(); break; @@ -916,4 +918,3 @@ bool img_frame_animate(img_t *img) img->dirty = true; return true; } - diff --git a/main.c b/main.c index f3eeb0a..9fdda54 100644 --- a/main.c +++ b/main.c @@ -34,6 +34,9 @@ #include #include +#define MODMASK(mask) ((mask) & ~ignore_mask) +#define BAR_SEP " " + typedef struct { struct timeval when; bool active; @@ -93,6 +96,9 @@ cursor_t imgcursor[3] = { CURSOR_ARROW, CURSOR_ARROW, CURSOR_ARROW }; +/************************** + function implementations + **************************/ void cleanup(void) { img_close(&img, false); @@ -346,8 +352,6 @@ void bar_put(win_bar_t *bar, const char *fmt, ...) va_end(ap); } -#define BAR_SEP " " - void update_info(void) { unsigned int i, fn, fw; @@ -554,7 +558,7 @@ void run_key_handler(const char *key, unsigned int mask) for (f = i = 0; f < fcnt; i++) { if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) { if (stat(files[i].path, &st) != 0 || - memcmp(&oldst[f].st_mtime, &st.st_mtime, sizeof(st.st_mtime)) != 0) + memcmp(&oldst[f].st_mtime, &st.st_mtime, sizeof(st.st_mtime)) != 0) { if (tns.thumbs != NULL) { tns_unload(&tns, i); @@ -582,8 +586,6 @@ end: redraw(); } -#define MODMASK(mask) ((mask) & ~ignore_mask) - void on_keypress(XKeyEvent *kev) { int i; @@ -607,7 +609,7 @@ void on_keypress(XKeyEvent *kev) handle_key_handler(false); } else if (extprefix) { run_key_handler(XKeysymToString(ksym), kev->state & ~sh); - extprefix = False; + extprefix = false; } else if (key >= '0' && key <= '9') { /* number prefix for commands */ prefix = prefix * 10 + (int) (key - '0'); @@ -675,7 +677,7 @@ void on_buttonpress(XButtonEvent *bev) bool on = !(files[sel].flags & FF_MARK); XEvent e; - for (;;) { + while (true) { if (sel >= 0 && mark_image(sel, on)) redraw(); XMaskEvent(win.env.dpy, @@ -836,7 +838,7 @@ void setup_signal(int sig, void (*handler)(int sig)) error(EXIT_FAILURE, errno, "signal %d", sig); } -int main(int argc, char **argv) +int main(int argc, char *argv[]) { int i, start; size_t n; diff --git a/nsxiv.h b/nsxiv.h index 716b24d..6ae2289 100644 --- a/nsxiv.h +++ b/nsxiv.h @@ -33,15 +33,9 @@ */ #define CLEANUP -#ifndef MIN #define MIN(a,b) ((a) < (b) ? (a) : (b)) -#endif -#ifndef MAX #define MAX(a,b) ((a) > (b) ? (a) : (b)) -#endif -#ifndef ABS #define ABS(a) ((a) > 0 ? (a) : -(a)) -#endif #define ARRLEN(a) (sizeof(a) / sizeof((a)[0])) @@ -465,4 +459,3 @@ void win_set_cursor(win_t*, cursor_t); void win_cursor_pos(win_t*, int*, int*); #endif /* NSXIV_H */ - diff --git a/thumbs.c b/thumbs.c index 059ae95..c9e92b7 100644 --- a/thumbs.c +++ b/thumbs.c @@ -143,9 +143,7 @@ void tns_clean_cache(tns_t *tns) r_closedir(&dir); } - -void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel, - win_t *win) +void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel, win_t *win) { int len; const char *homedir, *dsuffix = ""; @@ -320,7 +318,7 @@ bool tns_load(tns_t *tns, int n, bool force, bool cache_only) } exif_data_unref(ed); } -#endif +#endif /* HAVE_LIBEXIF */ } } diff --git a/util.c b/util.c index fa0f0e3..6028eb7 100644 --- a/util.c +++ b/util.c @@ -30,7 +30,7 @@ const char *progname; void* emalloc(size_t size) { void *ptr; - + ptr = malloc(size); if (ptr == NULL) error(EXIT_FAILURE, errno, NULL); @@ -172,7 +172,7 @@ char* r_readdir(r_dir_t *rdir, bool skip_dotfiles) } return filename; } - + if (rdir->recursive && rdir->stlen > 0) { /* open next subdirectory */ closedir(rdir->dir); @@ -210,4 +210,3 @@ int r_mkdir(char *path) } return 0; } - diff --git a/window.c b/window.c index 5d33bfa..9f22fba 100644 --- a/window.c +++ b/window.c @@ -33,10 +33,15 @@ #include "utf8.h" static XftFont *font; static double fontsize; +#define TEXTWIDTH(win, text, len) \ + win_draw_text(win, NULL, NULL, 0, 0, text, len, 0) #endif #define RES_CLASS "Nsxiv" +#define INIT_ATOM_(atom) \ + atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False); + enum { H_TEXT_PAD = 5, V_TEXT_PAD = 1 @@ -98,9 +103,6 @@ const char* win_res(XrmDatabase db, const char *name, const char *def) } } -#define INIT_ATOM_(atom) \ - atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False); - void win_init(win_t *win) { win_env_t *e; @@ -194,7 +196,7 @@ void win_open(win_t *win) XSizeHints sizehints; XWMHints hints; pid_t pid; - char hostname[255]; + char hostname[256]; e = &win->env; parent = options->embed != 0 ? options->embed : RootWindow(e->dpy, e->scr); @@ -229,30 +231,26 @@ void win_open(win_t *win) if ((gmask & YNegative) != 0) { win->y += e->scrh - win->h; sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity - ? SouthEastGravity : SouthWestGravity; + ? SouthEastGravity : SouthWestGravity; } sizehints.flags |= USPosition; } else { win->y = 0; } - win->xwin = XCreateWindow(e->dpy, parent, - win->x, win->y, win->w, win->h, 0, + win->xwin = XCreateWindow(e->dpy, parent, win->x, win->y, win->w, win->h, 0, e->depth, InputOutput, e->vis, 0, NULL); if (win->xwin == None) error(EXIT_FAILURE, 0, "Error creating X window"); /* set the _NET_WM_PID */ pid = getpid(); - XChangeProperty(e->dpy, win->xwin, - atoms[ATOM__NET_WM_PID], XA_CARDINAL, sizeof(pid_t) * 8, - PropModeReplace, (unsigned char *) &pid, 1); - - /* set the _NET_WM_PID */ - if (gethostname(hostname, sizeof(hostname)) == 0) { + XChangeProperty(e->dpy, win->xwin, atoms[ATOM__NET_WM_PID], XA_CARDINAL, + sizeof(pid_t) * 8, PropModeReplace, (unsigned char *) &pid, 1); + if (gethostname(hostname, ARRLEN(hostname)) == 0) { XTextProperty tp; tp.value = (unsigned char *)hostname; - tp.nitems = strnlen(hostname, sizeof(hostname)); + tp.nitems = strnlen(hostname, ARRLEN(hostname)); tp.encoding = XA_STRING; tp.format = 8; XSetWMClientMachine(e->dpy, win->xwin, &tp); @@ -266,11 +264,9 @@ void win_open(win_t *win) if (i != CURSOR_NONE) cursors[i].icon = XCreateFontCursor(e->dpy, cursors[i].name); } - if (XAllocNamedColor(e->dpy, e->cmap, "black", - &col, &col) == 0) - { + if (XAllocNamedColor(e->dpy, e->cmap, "black", &col, &col) == 0) error(EXIT_FAILURE, 0, "Error allocating color 'black'"); - } + none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8); *cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0); @@ -288,8 +284,7 @@ void win_open(win_t *win) for (c = icons[i].data[j] >> 4; c >= 0; c--) icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F]; } - XChangeProperty(e->dpy, win->xwin, - atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32, + XChangeProperty(e->dpy, win->xwin, atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32, i == 0 ? PropModeReplace : PropModeAppend, (unsigned char *) icon_data, n); } @@ -320,12 +315,11 @@ void win_open(win_t *win) win->buf.w = e->scrw; win->buf.h = e->scrh; - win->buf.pm = XCreatePixmap(e->dpy, win->xwin, - win->buf.w, win->buf.h, e->depth); + win->buf.pm = XCreatePixmap(e->dpy, win->xwin, win->buf.w, win->buf.h, e->depth); + XSetForeground(e->dpy, gc, win->win_bg); XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h); XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm); - XMapWindow(e->dpy, win->xwin); XFlush(e->dpy); @@ -409,9 +403,6 @@ void win_clear(win_t *win) } #if HAVE_LIBFONTS -#define TEXTWIDTH(win, text, len) \ - win_draw_text(win, NULL, NULL, 0, 0, text, len, 0) - int win_draw_text(win_t *win, XftDraw *d, const XftColor *color, int x, int y, char *text, int len, int w) { @@ -459,8 +450,7 @@ void win_draw_bar(win_t *win) e = &win->env; y = win->h + font->ascent + V_TEXT_PAD; w = win->w - 2*H_TEXT_PAD; - d = XftDrawCreate(e->dpy, win->buf.pm, e->vis, - e->cmap); + d = XftDrawCreate(e->dpy, win->buf.pm, e->vis, e->cmap); XSetForeground(e->dpy, gc, win->bar_bg.pixel); 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) return; snprintf(title, title_max, "%s%s", options->title_prefix, - (options->title_suffixmode == SUFFIX_BASENAME) ? basename : path); + options->title_suffixmode == SUFFIX_BASENAME ? basename : path); if (options->title_suffixmode == SUFFIX_EMPTY) *(title+strlen(options->title_prefix)) = '\0'; @@ -551,4 +541,3 @@ void win_cursor_pos(win_t *win, int *x, int *y) if (!XQueryPointer(win->env.dpy, win->xwin, &w, &w, &i, &i, x, y, &ui)) *x = *y = 0; } -