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 <taaparthur@noreply.codeberg.org>
This commit is contained in:
NRK 2022-10-19 14:46:22 +02:00
parent aa56aa2303
commit b11384a694
5 changed files with 51 additions and 57 deletions

66
image.c
View file

@ -23,6 +23,7 @@
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -92,7 +93,7 @@ void img_init(img_t *img, win_t *win)
img_change_gamma(img, options->gamma); img_change_gamma(img, options->gamma);
img->ss.on = options->slideshow > 0; 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 #if HAVE_LIBEXIF
@ -160,8 +161,8 @@ static bool img_load_gif(img_t *img, const fileinfo_t *file)
GifRowType *rows = NULL; GifRowType *rows = NULL;
GifRecordType rec; GifRecordType rec;
ColorMapObject *cmap; ColorMapObject *cmap;
DATA32 bgpixel = 0, *data, *ptr; uint32_t bgpixel = 0, *data, *ptr;
DATA32 *prev_frame = NULL; uint32_t *prev_frame = NULL;
Imlib_Image im; Imlib_Image im;
int i, j, bg, r, g, b; int i, j, bg, r, g, b;
int x, y, w, h, sw, sh; 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 disposal = 0, prev_disposal = 0;
unsigned int delay = 0; unsigned int delay = 0;
bool err = false; bool err = false;
multi_img_t *m = &img->multi;
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;
#if defined(GIFLIB_MAJOR) && GIFLIB_MAJOR >= 5 #if defined(GIFLIB_MAJOR) && GIFLIB_MAJOR >= 5
gif = DGifOpenFileName(file->path, NULL); gif = DGifOpenFileName(file->path, NULL);
@ -194,6 +189,7 @@ static bool img_load_gif(img_t *img, const fileinfo_t *file)
sh = gif->SHeight; sh = gif->SHeight;
px = py = pw = ph = 0; px = py = pw = ph = 0;
m->length = m->cnt = m->sel = 0;
do { do {
if (DGifGetRecordType(gif, &rec) == GIF_ERROR) { if (DGifGetRecordType(gif, &rec) == GIF_ERROR) {
err = true; err = true;
@ -227,9 +223,9 @@ static bool img_load_gif(img_t *img, const fileinfo_t *file)
w = gif->Image.Width; w = gif->Image.Width;
h = gif->Image.Height; h = gif->Image.Height;
rows = emalloc(h * sizeof(GifRowType)); rows = emalloc(h * sizeof(*rows));
for (i = 0; i < h; i++) for (i = 0; i < h; i++)
rows[i] = emalloc(w * sizeof(GifPixelType)); rows[i] = emalloc(w * sizeof(*rows[i]));
if (gif->Image.Interlace) { if (gif->Image.Interlace) {
for (i = 0; i < 4; i++) { for (i = 0; i < 4; i++) {
for (j = intoffset[i]; j < h; j += intjump[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); 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; cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap;
/* if bg > cmap->ColorCount, it is transparent black already */ /* if bg > cmap->ColorCount, it is transparent black already */
if (cmap && bg >= 0 && bg < cmap->ColorCount) { 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; prev_disposal = disposal;
px = x, py = y, pw = w, ph = h; px = x, py = y, pw = w, ph = h;
if (img->multi.cnt == img->multi.cap) { assert(m->cnt <= m->cap);
img->multi.cap *= 2; if (m->cnt == m->cap) {
img->multi.frames = erealloc(img->multi.frames, m->cap = m->cap == 0 ? 16 : (m->cap * 2);
img->multi.cap * sizeof(img_frame_t)); m->frames = erealloc(m->frames, m->cap * sizeof(*m->frames));
} }
img->multi.frames[img->multi.cnt].im = im; m->frames[m->cnt].im = im;
delay = img->multi.framedelay > 0 ? img->multi.framedelay : delay; delay = m->framedelay > 0 ? m->framedelay : delay;
img->multi.frames[img->multi.cnt].delay = delay > 0 ? delay : DEF_GIF_DELAY; m->frames[m->cnt].delay = delay > 0 ? delay : DEF_GIF_DELAY;
img->multi.length += img->multi.frames[img->multi.cnt].delay; m->length += m->frames[m->cnt].delay;
img->multi.cnt++; m->cnt++;
} }
} while (rec != TERMINATE_RECORD_TYPE); } 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 long flags;
unsigned int delay; unsigned int delay;
bool err = false; bool err = false;
multi_img_t *m = &img->multi;
if ((webp_file = fopen(file->path, "rb")) == NULL) { if ((webp_file = fopen(file->path, "rb")) == NULL) {
error(0, errno, "%s: Error opening webp image", file->name); 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->w = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH);
img->h = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT); img->h = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT);
if (info.frame_count > img->multi.cap) { if (info.frame_count > m->cap) {
img->multi.cap = info.frame_count; m->cap = info.frame_count;
img->multi.frames = erealloc(img->multi.frames, m->frames = erealloc(m->frames, m->cap * sizeof(*m->frames));
img->multi.cap * sizeof(img_frame_t));
} }
/* Load and decode frames (also works on images with only 1 frame) */ /* 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)) { while (WebPAnimDecoderGetNext(dec, &buf, &ts)) {
im = imlib_create_image_using_copied_data( 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_context_set_image(im);
imlib_image_set_format("webp"); imlib_image_set_format("webp");
/* Get an iterator of this frame - used for frame info (duration, etc.) */ /* 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); imlib_image_set_has_alpha((flags & ALPHA_FLAG) == ALPHA_FLAG);
/* Store info for this frame */ /* 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; delay = iter.duration > 0 ? iter.duration : DEF_WEBP_DELAY;
img->multi.frames[img->multi.cnt].delay = delay; m->frames[m->cnt].delay = delay;
img->multi.length += img->multi.frames[img->multi.cnt].delay; m->length += m->frames[m->cnt].delay;
img->multi.cnt++; m->cnt++;
} }
WebPDemuxReleaseIterator(&iter); WebPDemuxReleaseIterator(&iter);
@ -619,8 +615,8 @@ void img_render(img_t *img)
if (img->alpha) { if (img->alpha) {
int i, c, r; int i, c, r;
DATA32 col[2] = { 0xFF666666, 0xFF999999 }; uint32_t col[2] = { 0xFF666666, 0xFF999999 };
DATA32 * data = imlib_image_get_data(); uint32_t *data = imlib_image_get_data();
for (r = 0; r < dh; r++) { for (r = 0; r < dh; r++) {
i = r * dw; i = r * dw;

18
main.c
View file

@ -62,17 +62,16 @@ tns_t tns;
win_t win; win_t win;
appmode_t mode; appmode_t mode;
const XButtonEvent *xbutton_ev;
fileinfo_t *files; fileinfo_t *files;
int filecnt, fileidx; int filecnt, fileidx;
int alternate; int alternate;
int markcnt; int markcnt;
int markidx; int markidx;
int prefix; int prefix;
static bool extprefix; bool title_dirty;
const XButtonEvent *xbutton_ev;
static bool extprefix;
static bool resized = false; static bool resized = false;
static struct { static struct {
@ -90,8 +89,6 @@ static struct {
extcmd_t f; extcmd_t f;
} wintitle; } wintitle;
bool title_dirty;
static struct { static struct {
timeout_f handler; timeout_f handler;
struct timeval when; struct timeval when;
@ -104,9 +101,10 @@ static struct {
{ clear_resize }, { clear_resize },
}; };
/************************** /*
function implementations * function implementations
**************************/ */
static void cleanup(void) static void cleanup(void)
{ {
img_close(&img, false); img_close(&img, false);
@ -891,7 +889,7 @@ int main(int argc, char *argv[])
} }
r_closedir(&dir); r_closedir(&dir);
if (fileidx - start > 1) if (fileidx - start > 1)
qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp); qsort(files + start, fileidx - start, sizeof(*files), fncmp);
} }
} }

View file

@ -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 = ""; const char *homedir, *dsuffix = "";
if (cnt != NULL && *cnt > 0) if (cnt != NULL && *cnt > 0)
tns->thumbs = ecalloc(*cnt, sizeof(thumb_t)); tns->thumbs = ecalloc(*cnt, sizeof(*tns->thumbs));
else else
tns->thumbs = NULL; tns->thumbs = NULL;
tns->files = tns_files; tns->files = tns_files;

4
util.c
View file

@ -97,7 +97,7 @@ int r_opendir(r_dir_t *rdir, const char *dirname, bool recursive)
} }
rdir->stcap = 512; rdir->stcap = 512;
rdir->stack = emalloc(rdir->stcap * sizeof(char*)); rdir->stack = emalloc(rdir->stcap * sizeof(*rdir->stack));
rdir->stlen = 0; rdir->stlen = 0;
rdir->name = (char*) dirname; rdir->name = (char*) dirname;
@ -164,7 +164,7 @@ char* r_readdir(r_dir_t *rdir, bool skip_dotfiles)
if (rdir->stlen == rdir->stcap) { if (rdir->stlen == rdir->stcap) {
rdir->stcap *= 2; rdir->stcap *= 2;
rdir->stack = erealloc(rdir->stack, rdir->stack = erealloc(rdir->stack,
rdir->stcap * sizeof(char*)); rdir->stcap * sizeof(*rdir->stack));
} }
rdir->stack[rdir->stlen++] = filename; rdir->stack[rdir->stlen++] = filename;
continue; continue;

View file

@ -34,14 +34,11 @@
#if HAVE_LIBFONTS #if HAVE_LIBFONTS
#include "utf8.h" #include "utf8.h"
#define UTF8_PADDING 4 /* utf8_decode requires 4 bytes of zero padding */ #define UTF8_PADDING 4 /* utf8_decode requires 4 bytes of zero padding */
static XftFont *font;
static double fontsize;
#define TEXTWIDTH(win, text, len) \ #define TEXTWIDTH(win, text, len) \
win_draw_text(win, NULL, NULL, 0, 0, text, len, 0) 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) \ #define INIT_ATOM_(atom) \
atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False); atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False);
@ -50,6 +47,10 @@ enum {
V_TEXT_PAD = 1 V_TEXT_PAD = 1
}; };
Atom atoms[ATOM_COUNT];
static GC gc;
static int barheight;
static struct { static struct {
int name; int name;
Cursor icon; Cursor icon;
@ -58,11 +59,10 @@ static struct {
{ XC_sb_left_arrow }, { XC_sb_right_arrow } { XC_sb_left_arrow }, { XC_sb_right_arrow }
}; };
static GC gc; #if HAVE_LIBFONTS
static XftFont *font;
static int barheight; static double fontsize;
#endif
Atom atoms[ATOM_COUNT];
#if HAVE_LIBFONTS #if HAVE_LIBFONTS
static void win_init_font(const win_env_t *e, const char *fontstr) 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]; static char lbuf[512 + UTF8_PADDING], rbuf[64 + UTF8_PADDING];
#endif #endif
memset(win, 0, sizeof(win_t)); memset(win, 0, sizeof(*win));
e = &win->env; e = &win->env;
if ((e->dpy = XOpenDisplay(NULL)) == NULL) if ((e->dpy = XOpenDisplay(NULL)) == NULL)