From b11384a6942cdf3c6b6f55e0dcc0a438eff7158a Mon Sep 17 00:00:00 2001 From: NRK Date: Wed, 19 Oct 2022 14:46:22 +0200 Subject: [PATCH] code-style: misc changes (#374) * ensure static variables comes after non-static ones * remove depreciated DATA32 type * prefer `sizeof(expression)` over `sizeof(Type)`. * silence a -Wsign warning * {gif,webp} loader: use a pointer to reduce code-noise * gif loader: allocate in one place Reviewed-on: https://codeberg.org/nsxiv/nsxiv/pulls/374 Reviewed-by: TAAPArthur --- image.c | 66 ++++++++++++++++++++++++++------------------------------ main.c | 18 +++++++--------- thumbs.c | 2 +- util.c | 4 ++-- window.c | 18 ++++++++-------- 5 files changed, 51 insertions(+), 57 deletions(-) diff --git a/image.c b/image.c index 512a837..4c9e393 100644 --- a/image.c +++ b/image.c @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -92,7 +93,7 @@ void img_init(img_t *img, win_t *win) img_change_gamma(img, options->gamma); img->ss.on = options->slideshow > 0; - img->ss.delay = options->slideshow > 0 ? options->slideshow : SLIDESHOW_DELAY * 10; + img->ss.delay = options->slideshow > 0 ? options->slideshow : SLIDESHOW_DELAY * 10u; } #if HAVE_LIBEXIF @@ -160,8 +161,8 @@ static bool img_load_gif(img_t *img, const fileinfo_t *file) GifRowType *rows = NULL; GifRecordType rec; ColorMapObject *cmap; - DATA32 bgpixel = 0, *data, *ptr; - DATA32 *prev_frame = NULL; + uint32_t bgpixel = 0, *data, *ptr; + uint32_t *prev_frame = NULL; Imlib_Image im; int i, j, bg, r, g, b; int x, y, w, h, sw, sh; @@ -172,13 +173,7 @@ static bool img_load_gif(img_t *img, const fileinfo_t *file) unsigned int disposal = 0, prev_disposal = 0; unsigned int delay = 0; bool err = false; - - if (img->multi.cap == 0) { - img->multi.cap = 8; - img->multi.frames = emalloc(img->multi.cap * sizeof(img_frame_t)); - } - img->multi.cnt = img->multi.sel = 0; - img->multi.length = 0; + multi_img_t *m = &img->multi; #if defined(GIFLIB_MAJOR) && GIFLIB_MAJOR >= 5 gif = DGifOpenFileName(file->path, NULL); @@ -194,6 +189,7 @@ static bool img_load_gif(img_t *img, const fileinfo_t *file) sh = gif->SHeight; px = py = pw = ph = 0; + m->length = m->cnt = m->sel = 0; do { if (DGifGetRecordType(gif, &rec) == GIF_ERROR) { err = true; @@ -227,9 +223,9 @@ static bool img_load_gif(img_t *img, const fileinfo_t *file) w = gif->Image.Width; h = gif->Image.Height; - rows = emalloc(h * sizeof(GifRowType)); + rows = emalloc(h * sizeof(*rows)); for (i = 0; i < h; i++) - rows[i] = emalloc(w * sizeof(GifPixelType)); + rows[i] = emalloc(w * sizeof(*rows[i])); if (gif->Image.Interlace) { for (i = 0; i < 4; i++) { for (j = intoffset[i]; j < h; j += intjump[i]) @@ -240,7 +236,7 @@ static bool img_load_gif(img_t *img, const fileinfo_t *file) DGifGetLine(gif, rows[i], w); } - ptr = data = emalloc(sw * sh * sizeof(DATA32)); + ptr = data = emalloc(sw * sh * sizeof(*data)); cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap; /* if bg > cmap->ColorCount, it is transparent black already */ if (cmap && bg >= 0 && bg < cmap->ColorCount) { @@ -295,16 +291,16 @@ static bool img_load_gif(img_t *img, const fileinfo_t *file) prev_disposal = disposal; px = x, py = y, pw = w, ph = h; - if (img->multi.cnt == img->multi.cap) { - img->multi.cap *= 2; - img->multi.frames = erealloc(img->multi.frames, - img->multi.cap * sizeof(img_frame_t)); + assert(m->cnt <= m->cap); + if (m->cnt == m->cap) { + m->cap = m->cap == 0 ? 16 : (m->cap * 2); + m->frames = erealloc(m->frames, m->cap * sizeof(*m->frames)); } - img->multi.frames[img->multi.cnt].im = im; - delay = img->multi.framedelay > 0 ? img->multi.framedelay : delay; - img->multi.frames[img->multi.cnt].delay = delay > 0 ? delay : DEF_GIF_DELAY; - img->multi.length += img->multi.frames[img->multi.cnt].delay; - img->multi.cnt++; + m->frames[m->cnt].im = im; + delay = m->framedelay > 0 ? m->framedelay : delay; + m->frames[m->cnt].delay = delay > 0 ? delay : DEF_GIF_DELAY; + m->length += m->frames[m->cnt].delay; + m->cnt++; } } while (rec != TERMINATE_RECORD_TYPE); @@ -340,6 +336,7 @@ static bool img_load_webp(img_t *img, const fileinfo_t *file) unsigned long flags; unsigned int delay; bool err = false; + multi_img_t *m = &img->multi; if ((webp_file = fopen(file->path, "rb")) == NULL) { error(0, errno, "%s: Error opening webp image", file->name); @@ -375,28 +372,27 @@ static bool img_load_webp(img_t *img, const fileinfo_t *file) img->w = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH); img->h = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT); - if (info.frame_count > img->multi.cap) { - img->multi.cap = info.frame_count; - img->multi.frames = erealloc(img->multi.frames, - img->multi.cap * sizeof(img_frame_t)); + if (info.frame_count > m->cap) { + m->cap = info.frame_count; + m->frames = erealloc(m->frames, m->cap * sizeof(*m->frames)); } /* Load and decode frames (also works on images with only 1 frame) */ - img->multi.cnt = img->multi.sel = 0; + m->cnt = m->sel = 0; while (WebPAnimDecoderGetNext(dec, &buf, &ts)) { im = imlib_create_image_using_copied_data( - info.canvas_width, info.canvas_height, (DATA32*)buf); + info.canvas_width, info.canvas_height, (uint32_t *)buf); imlib_context_set_image(im); imlib_image_set_format("webp"); /* Get an iterator of this frame - used for frame info (duration, etc.) */ - WebPDemuxGetFrame(demux, img->multi.cnt+1, &iter); + WebPDemuxGetFrame(demux, m->cnt+1, &iter); imlib_image_set_has_alpha((flags & ALPHA_FLAG) == ALPHA_FLAG); /* Store info for this frame */ - img->multi.frames[img->multi.cnt].im = im; + m->frames[m->cnt].im = im; delay = iter.duration > 0 ? iter.duration : DEF_WEBP_DELAY; - img->multi.frames[img->multi.cnt].delay = delay; - img->multi.length += img->multi.frames[img->multi.cnt].delay; - img->multi.cnt++; + m->frames[m->cnt].delay = delay; + m->length += m->frames[m->cnt].delay; + m->cnt++; } WebPDemuxReleaseIterator(&iter); @@ -619,8 +615,8 @@ void img_render(img_t *img) if (img->alpha) { int i, c, r; - DATA32 col[2] = { 0xFF666666, 0xFF999999 }; - DATA32 * data = imlib_image_get_data(); + uint32_t col[2] = { 0xFF666666, 0xFF999999 }; + uint32_t *data = imlib_image_get_data(); for (r = 0; r < dh; r++) { i = r * dw; diff --git a/main.c b/main.c index 8754dae..1c8dd7b 100644 --- a/main.c +++ b/main.c @@ -62,17 +62,16 @@ tns_t tns; win_t win; appmode_t mode; -const XButtonEvent *xbutton_ev; - fileinfo_t *files; int filecnt, fileidx; int alternate; int markcnt; int markidx; - int prefix; -static bool extprefix; +bool title_dirty; +const XButtonEvent *xbutton_ev; +static bool extprefix; static bool resized = false; static struct { @@ -90,8 +89,6 @@ static struct { extcmd_t f; } wintitle; -bool title_dirty; - static struct { timeout_f handler; struct timeval when; @@ -104,9 +101,10 @@ static struct { { clear_resize }, }; -/************************** - function implementations - **************************/ +/* + * function implementations + */ + static void cleanup(void) { img_close(&img, false); @@ -891,7 +889,7 @@ int main(int argc, char *argv[]) } r_closedir(&dir); if (fileidx - start > 1) - qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp); + qsort(files + start, fileidx - start, sizeof(*files), fncmp); } } diff --git a/thumbs.c b/thumbs.c index 40a3d93..c9e97c4 100644 --- a/thumbs.c +++ b/thumbs.c @@ -145,7 +145,7 @@ void tns_init(tns_t *tns, fileinfo_t *tns_files, const int *cnt, int *sel, win_t const char *homedir, *dsuffix = ""; if (cnt != NULL && *cnt > 0) - tns->thumbs = ecalloc(*cnt, sizeof(thumb_t)); + tns->thumbs = ecalloc(*cnt, sizeof(*tns->thumbs)); else tns->thumbs = NULL; tns->files = tns_files; diff --git a/util.c b/util.c index 8b164ed..9172e85 100644 --- a/util.c +++ b/util.c @@ -97,7 +97,7 @@ int r_opendir(r_dir_t *rdir, const char *dirname, bool recursive) } rdir->stcap = 512; - rdir->stack = emalloc(rdir->stcap * sizeof(char*)); + rdir->stack = emalloc(rdir->stcap * sizeof(*rdir->stack)); rdir->stlen = 0; rdir->name = (char*) dirname; @@ -164,7 +164,7 @@ char* r_readdir(r_dir_t *rdir, bool skip_dotfiles) if (rdir->stlen == rdir->stcap) { rdir->stcap *= 2; rdir->stack = erealloc(rdir->stack, - rdir->stcap * sizeof(char*)); + rdir->stcap * sizeof(*rdir->stack)); } rdir->stack[rdir->stlen++] = filename; continue; diff --git a/window.c b/window.c index e385be7..136b452 100644 --- a/window.c +++ b/window.c @@ -34,14 +34,11 @@ #if HAVE_LIBFONTS #include "utf8.h" #define UTF8_PADDING 4 /* utf8_decode requires 4 bytes of zero padding */ -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); @@ -50,6 +47,10 @@ enum { V_TEXT_PAD = 1 }; +Atom atoms[ATOM_COUNT]; + +static GC gc; +static int barheight; static struct { int name; Cursor icon; @@ -58,11 +59,10 @@ static struct { { XC_sb_left_arrow }, { XC_sb_right_arrow } }; -static GC gc; - -static int barheight; - -Atom atoms[ATOM_COUNT]; +#if HAVE_LIBFONTS +static XftFont *font; +static double fontsize; +#endif #if HAVE_LIBFONTS static void win_init_font(const win_env_t *e, const char *fontstr) @@ -116,7 +116,7 @@ void win_init(win_t *win) static char lbuf[512 + UTF8_PADDING], rbuf[64 + UTF8_PADDING]; #endif - memset(win, 0, sizeof(win_t)); + memset(win, 0, sizeof(*win)); e = &win->env; if ((e->dpy = XOpenDisplay(NULL)) == NULL)