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 <nrk@disroot.org>
This commit is contained in:
Berke Kocaoğlu 2021-10-11 06:07:18 +03:00 committed by GitHub
parent 675db4bbb6
commit 1449bfc5e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 45 additions and 68 deletions

View File

@ -24,6 +24,11 @@
#include <unistd.h> #include <unistd.h>
#include <sys/inotify.h> #include <sys/inotify.h>
union {
char d[4096]; /* aligned buffer */
struct inotify_event e;
} buf;
void arl_init(arl_t *arl) void arl_init(arl_t *arl)
{ {
arl->fd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK); 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 arl_handle(arl_t *arl)
{ {
bool reload = false; bool reload = false;
char *ptr; char *ptr;
const struct inotify_event *e; const struct inotify_event *e;
for (;;) { while (true) {
ssize_t len = read(arl->fd, buf.d, sizeof(buf.d)); ssize_t len = read(arl->fd, buf.d, sizeof(buf.d));
if (len == -1) { if (len == -1) {
@ -109,4 +109,3 @@ bool arl_handle(arl_t *arl)
} }
return reload; return reload;
} }

View File

@ -39,4 +39,3 @@ bool arl_handle(arl_t *arl)
(void) arl; (void) arl;
return false; return false;
} }

View File

@ -233,7 +233,7 @@ bool cg_navigate_marked(arg_t n)
{ {
int d, i; int d, i;
int new = fileidx; int new = fileidx;
if (prefix > 0) if (prefix > 0)
n *= prefix; n *= prefix;
d = n > 0 ? 1 : -1; 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) if ((int)(img.w * img.zoom) <= win.w && (int)(img.h * img.zoom) <= win.h)
return false; return false;
win_set_cursor(&win, CURSOR_DRAG);
win_set_cursor(&win, CURSOR_DRAG);
win_cursor_pos(&win, &x, &y); win_cursor_pos(&win, &x, &y);
ox = x; ox = x;
oy = y; oy = y;
for (;;) { while (true) {
if (mode == DRAG_ABSOLUTE) { if (mode == DRAG_ABSOLUTE) {
px = MIN(MAX(0.0, x - win.w*0.1), win.w*0.8) / (win.w*0.8) px = MIN(MAX(0.0, x - win.w*0.1), win.w*0.8) / (win.w*0.8)
* (win.w - img.w * img.zoom); * (win.w - img.w * img.zoom);
@ -449,4 +448,3 @@ bool ct_reload_all(arg_t _)
const cmd_t cmds[CMD_COUNT] = { const cmd_t cmds[CMD_COUNT] = {
#include "commands.lst" #include "commands.lst"
}; };

View File

@ -34,4 +34,3 @@ I_CMD(slideshow)
T_CMD(move_sel) T_CMD(move_sel)
T_CMD(reload_all) T_CMD(reload_all)

View File

@ -63,7 +63,7 @@ static const int THUMB_SIZE = 3;
#endif #endif
#ifdef _MAPPINGS_CONFIG #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; static const int ignore_mask = Mod2Mask | LockMask;
/* abort the keyhandler */ /* abort the keyhandler */

View File

@ -89,6 +89,7 @@ void exif_auto_orientate(const fileinfo_t *file)
switch (orientation) { switch (orientation) {
case 5: case 5:
imlib_image_orientate(1); imlib_image_orientate(1);
/* fall through */
case 2: case 2:
imlib_image_flip_vertical(); imlib_image_flip_vertical();
break; break;
@ -97,6 +98,7 @@ void exif_auto_orientate(const fileinfo_t *file)
break; break;
case 7: case 7:
imlib_image_orientate(1); imlib_image_orientate(1);
/* fall through */
case 4: case 4:
imlib_image_flip_horizontal(); imlib_image_flip_horizontal();
break; break;
@ -916,4 +918,3 @@ bool img_frame_animate(img_t *img)
img->dirty = true; img->dirty = true;
return true; return true;
} }

18
main.c
View File

@ -34,6 +34,9 @@
#include <X11/keysym.h> #include <X11/keysym.h>
#include <X11/XF86keysym.h> #include <X11/XF86keysym.h>
#define MODMASK(mask) ((mask) & ~ignore_mask)
#define BAR_SEP " "
typedef struct { typedef struct {
struct timeval when; struct timeval when;
bool active; bool active;
@ -93,6 +96,9 @@ cursor_t imgcursor[3] = {
CURSOR_ARROW, CURSOR_ARROW, CURSOR_ARROW CURSOR_ARROW, CURSOR_ARROW, CURSOR_ARROW
}; };
/**************************
function implementations
**************************/
void cleanup(void) void cleanup(void)
{ {
img_close(&img, false); img_close(&img, false);
@ -346,8 +352,6 @@ void bar_put(win_bar_t *bar, const char *fmt, ...)
va_end(ap); va_end(ap);
} }
#define BAR_SEP " "
void update_info(void) void update_info(void)
{ {
unsigned int i, fn, fw; 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++) { for (f = i = 0; f < fcnt; i++) {
if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) { if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
if (stat(files[i].path, &st) != 0 || 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) { if (tns.thumbs != NULL) {
tns_unload(&tns, i); tns_unload(&tns, i);
@ -582,8 +586,6 @@ end:
redraw(); redraw();
} }
#define MODMASK(mask) ((mask) & ~ignore_mask)
void on_keypress(XKeyEvent *kev) void on_keypress(XKeyEvent *kev)
{ {
int i; int i;
@ -607,7 +609,7 @@ void on_keypress(XKeyEvent *kev)
handle_key_handler(false); handle_key_handler(false);
} else if (extprefix) { } else if (extprefix) {
run_key_handler(XKeysymToString(ksym), kev->state & ~sh); run_key_handler(XKeysymToString(ksym), kev->state & ~sh);
extprefix = False; extprefix = false;
} else if (key >= '0' && key <= '9') { } else if (key >= '0' && key <= '9') {
/* number prefix for commands */ /* number prefix for commands */
prefix = prefix * 10 + (int) (key - '0'); prefix = prefix * 10 + (int) (key - '0');
@ -675,7 +677,7 @@ void on_buttonpress(XButtonEvent *bev)
bool on = !(files[sel].flags & FF_MARK); bool on = !(files[sel].flags & FF_MARK);
XEvent e; XEvent e;
for (;;) { while (true) {
if (sel >= 0 && mark_image(sel, on)) if (sel >= 0 && mark_image(sel, on))
redraw(); redraw();
XMaskEvent(win.env.dpy, XMaskEvent(win.env.dpy,
@ -836,7 +838,7 @@ void setup_signal(int sig, void (*handler)(int sig))
error(EXIT_FAILURE, errno, "signal %d", sig); error(EXIT_FAILURE, errno, "signal %d", sig);
} }
int main(int argc, char **argv) int main(int argc, char *argv[])
{ {
int i, start; int i, start;
size_t n; size_t n;

View File

@ -33,15 +33,9 @@
*/ */
#define CLEANUP #define CLEANUP
#ifndef MIN
#define MIN(a,b) ((a) < (b) ? (a) : (b)) #define MIN(a,b) ((a) < (b) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a,b) ((a) > (b) ? (a) : (b)) #define MAX(a,b) ((a) > (b) ? (a) : (b))
#endif
#ifndef ABS
#define ABS(a) ((a) > 0 ? (a) : -(a)) #define ABS(a) ((a) > 0 ? (a) : -(a))
#endif
#define ARRLEN(a) (sizeof(a) / sizeof((a)[0])) #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*); void win_cursor_pos(win_t*, int*, int*);
#endif /* NSXIV_H */ #endif /* NSXIV_H */

View File

@ -143,9 +143,7 @@ void tns_clean_cache(tns_t *tns)
r_closedir(&dir); 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; int len;
const char *homedir, *dsuffix = ""; 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); exif_data_unref(ed);
} }
#endif #endif /* HAVE_LIBEXIF */
} }
} }

5
util.c
View File

@ -30,7 +30,7 @@ const char *progname;
void* emalloc(size_t size) void* emalloc(size_t size)
{ {
void *ptr; void *ptr;
ptr = malloc(size); ptr = malloc(size);
if (ptr == NULL) if (ptr == NULL)
error(EXIT_FAILURE, errno, NULL); error(EXIT_FAILURE, errno, NULL);
@ -172,7 +172,7 @@ char* r_readdir(r_dir_t *rdir, bool skip_dotfiles)
} }
return filename; return filename;
} }
if (rdir->recursive && rdir->stlen > 0) { if (rdir->recursive && rdir->stlen > 0) {
/* open next subdirectory */ /* open next subdirectory */
closedir(rdir->dir); closedir(rdir->dir);
@ -210,4 +210,3 @@ int r_mkdir(char *path)
} }
return 0; return 0;
} }

View File

@ -33,10 +33,15 @@
#include "utf8.h" #include "utf8.h"
static XftFont *font; static XftFont *font;
static double fontsize; static double fontsize;
#define TEXTWIDTH(win, text, len) \
win_draw_text(win, NULL, NULL, 0, 0, text, len, 0)
#endif #endif
#define RES_CLASS "Nsxiv" #define RES_CLASS "Nsxiv"
#define INIT_ATOM_(atom) \
atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False);
enum { enum {
H_TEXT_PAD = 5, H_TEXT_PAD = 5,
V_TEXT_PAD = 1 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) void win_init(win_t *win)
{ {
win_env_t *e; win_env_t *e;
@ -194,7 +196,7 @@ void win_open(win_t *win)
XSizeHints sizehints; XSizeHints sizehints;
XWMHints hints; XWMHints hints;
pid_t pid; pid_t pid;
char hostname[255]; char hostname[256];
e = &win->env; e = &win->env;
parent = options->embed != 0 ? options->embed : RootWindow(e->dpy, e->scr); 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) { if ((gmask & YNegative) != 0) {
win->y += e->scrh - win->h; win->y += e->scrh - win->h;
sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
? SouthEastGravity : SouthWestGravity; ? SouthEastGravity : SouthWestGravity;
} }
sizehints.flags |= USPosition; sizehints.flags |= USPosition;
} else { } else {
win->y = 0; win->y = 0;
} }
win->xwin = XCreateWindow(e->dpy, parent, win->xwin = XCreateWindow(e->dpy, parent, win->x, win->y, win->w, win->h, 0,
win->x, win->y, win->w, win->h, 0,
e->depth, InputOutput, e->vis, 0, NULL); e->depth, InputOutput, e->vis, 0, NULL);
if (win->xwin == None) if (win->xwin == None)
error(EXIT_FAILURE, 0, "Error creating X window"); error(EXIT_FAILURE, 0, "Error creating X window");
/* set the _NET_WM_PID */ /* set the _NET_WM_PID */
pid = getpid(); pid = getpid();
XChangeProperty(e->dpy, win->xwin, XChangeProperty(e->dpy, win->xwin, atoms[ATOM__NET_WM_PID], XA_CARDINAL,
atoms[ATOM__NET_WM_PID], XA_CARDINAL, sizeof(pid_t) * 8, sizeof(pid_t) * 8, PropModeReplace, (unsigned char *) &pid, 1);
PropModeReplace, (unsigned char *) &pid, 1); if (gethostname(hostname, ARRLEN(hostname)) == 0) {
/* set the _NET_WM_PID */
if (gethostname(hostname, sizeof(hostname)) == 0) {
XTextProperty tp; XTextProperty tp;
tp.value = (unsigned char *)hostname; tp.value = (unsigned char *)hostname;
tp.nitems = strnlen(hostname, sizeof(hostname)); tp.nitems = strnlen(hostname, ARRLEN(hostname));
tp.encoding = XA_STRING; tp.encoding = XA_STRING;
tp.format = 8; tp.format = 8;
XSetWMClientMachine(e->dpy, win->xwin, &tp); XSetWMClientMachine(e->dpy, win->xwin, &tp);
@ -266,11 +264,9 @@ void win_open(win_t *win)
if (i != CURSOR_NONE) if (i != CURSOR_NONE)
cursors[i].icon = XCreateFontCursor(e->dpy, cursors[i].name); cursors[i].icon = XCreateFontCursor(e->dpy, cursors[i].name);
} }
if (XAllocNamedColor(e->dpy, e->cmap, "black", if (XAllocNamedColor(e->dpy, e->cmap, "black", &col, &col) == 0)
&col, &col) == 0)
{
error(EXIT_FAILURE, 0, "Error allocating color 'black'"); error(EXIT_FAILURE, 0, "Error allocating color 'black'");
}
none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8); none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
*cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0); *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--) for (c = icons[i].data[j] >> 4; c >= 0; c--)
icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F]; icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F];
} }
XChangeProperty(e->dpy, win->xwin, XChangeProperty(e->dpy, win->xwin, atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32,
atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32,
i == 0 ? PropModeReplace : PropModeAppend, i == 0 ? PropModeReplace : PropModeAppend,
(unsigned char *) icon_data, n); (unsigned char *) icon_data, n);
} }
@ -320,12 +315,11 @@ void win_open(win_t *win)
win->buf.w = e->scrw; win->buf.w = e->scrw;
win->buf.h = e->scrh; win->buf.h = e->scrh;
win->buf.pm = XCreatePixmap(e->dpy, win->xwin, win->buf.pm = XCreatePixmap(e->dpy, win->xwin, win->buf.w, win->buf.h, e->depth);
win->buf.w, win->buf.h, e->depth);
XSetForeground(e->dpy, gc, win->win_bg); XSetForeground(e->dpy, gc, win->win_bg);
XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h); XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm); XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
XMapWindow(e->dpy, win->xwin); XMapWindow(e->dpy, win->xwin);
XFlush(e->dpy); XFlush(e->dpy);
@ -409,9 +403,6 @@ void win_clear(win_t *win)
} }
#if HAVE_LIBFONTS #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, int win_draw_text(win_t *win, XftDraw *d, const XftColor *color, int x, int y,
char *text, int len, int w) char *text, int len, int w)
{ {
@ -459,8 +450,7 @@ void win_draw_bar(win_t *win)
e = &win->env; e = &win->env;
y = win->h + font->ascent + V_TEXT_PAD; y = win->h + font->ascent + V_TEXT_PAD;
w = win->w - 2*H_TEXT_PAD; w = win->w - 2*H_TEXT_PAD;
d = XftDrawCreate(e->dpy, win->buf.pm, e->vis, d = XftDrawCreate(e->dpy, win->buf.pm, e->vis, e->cmap);
e->cmap);
XSetForeground(e->dpy, gc, win->bar_bg.pixel); XSetForeground(e->dpy, gc, win->bar_bg.pixel);
XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h); 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; return;
snprintf(title, title_max, "%s%s", options->title_prefix, 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) if (options->title_suffixmode == SUFFIX_EMPTY)
*(title+strlen(options->title_prefix)) = '\0'; *(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)) if (!XQueryPointer(win->env.dpy, win->xwin, &w, &w, &i, &i, x, y, &ui))
*x = *y = 0; *x = *y = 0;
} }