nsxiv850bc78
code-style: general cleanups (#137)
* tns_clean_cache: remove unused function arg * remove malloc casting * improve consistency use sizeof(T) at the end * avoid comparing integers of different signedness * use Window type for embed and parent * remove unnecessary comparisons * remove cpp style comments * improve consistency: remove comma from the end of enumerator list * Removed useless _IMAGE_CONFIG defines * consistency: use the same order as snprintf * Resolve c89 warnings
+42 −45
9 files changed
- M commands.c +0 −2
- M image.c +7 −9
- M main.c +12 −10
- M nsxiv.h +4 −4
- M options.c +0 −1
- M thumbs.c +4 −4
- M utf8.h +4 −4
- M util.c +1 −1
- M window.c +10 −10
| @@ -18,8 +18,6 @@ | |||
| 18 | 18 | */ | |
| 19 | 19 | ||
| 20 | 20 | #include "nsxiv.h" | |
| 21 | − | #define _IMAGE_CONFIG | |
| 22 | − | #include "config.h" | |
| 23 | 21 | ||
| 24 | 22 | #include <stdlib.h> | |
| 25 | 23 | #include <string.h> | |
| @@ -135,8 +135,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file) | |||
| 135 | 135 | ||
| 136 | 136 | if (img->multi.cap == 0) { | |
| 137 | 137 | img->multi.cap = 8; | |
| 138 | − | img->multi.frames = (img_frame_t*) | |
| 139 | − | emalloc(sizeof(img_frame_t) * img->multi.cap); | |
| 138 | + | img->multi.frames = emalloc(img->multi.cap * sizeof(img_frame_t)); | |
| 140 | 139 | } | |
| 141 | 140 | img->multi.cnt = img->multi.sel = 0; | |
| 142 | 141 | img->multi.length = 0; | |
| @@ -188,9 +187,9 @@ bool img_load_gif(img_t *img, const fileinfo_t *file) | |||
| 188 | 187 | w = gif->Image.Width; | |
| 189 | 188 | h = gif->Image.Height; | |
| 190 | 189 | ||
| 191 | − | rows = (GifRowType*) emalloc(h * sizeof(GifRowType)); | |
| 190 | + | rows = emalloc(h * sizeof(GifRowType)); | |
| 192 | 191 | for (i = 0; i < h; i++) | |
| 193 | − | rows[i] = (GifRowType) emalloc(w * sizeof(GifPixelType)); | |
| 192 | + | rows[i] = emalloc(w * sizeof(GifPixelType)); | |
| 194 | 193 | if (gif->Image.Interlace) { | |
| 195 | 194 | for (i = 0; i < 4; i++) { | |
| 196 | 195 | for (j = intoffset[i]; j < h; j += intjump[i]) | |
| @@ -201,7 +200,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file) | |||
| 201 | 200 | DGifGetLine(gif, rows[i], w); | |
| 202 | 201 | } | |
| 203 | 202 | ||
| 204 | − | ptr = data = (DATA32*) emalloc(sizeof(DATA32) * sw * sh); | |
| 203 | + | ptr = data = emalloc(sw * sh * sizeof(DATA32)); | |
| 205 | 204 | cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap; | |
| 206 | 205 | if (bg >= cmap->ColorCount) { | |
| 207 | 206 | err = true; | |
| @@ -258,8 +257,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file) | |||
| 258 | 257 | ||
| 259 | 258 | if (img->multi.cnt == img->multi.cap) { | |
| 260 | 259 | img->multi.cap *= 2; | |
| 261 | − | img->multi.frames = (img_frame_t*) | |
| 262 | − | erealloc(img->multi.frames, | |
| 260 | + | img->multi.frames = erealloc(img->multi.frames, | |
| 263 | 261 | img->multi.cap * sizeof(img_frame_t)); | |
| 264 | 262 | } | |
| 265 | 263 | img->multi.frames[img->multi.cnt].im = im; | |
| @@ -322,7 +320,6 @@ bool img_load_webp(const fileinfo_t *file, Imlib_Image *fframe, img_t *img) | |||
| 322 | 320 | { | |
| 323 | 321 | FILE *webp_file; | |
| 324 | 322 | WebPData data; | |
| 325 | − | data.bytes = NULL; | |
| 326 | 323 | ||
| 327 | 324 | Imlib_Image im = NULL; | |
| 328 | 325 | struct WebPAnimDecoderOptions opts; | |
| @@ -335,6 +332,7 @@ bool img_load_webp(const fileinfo_t *file, Imlib_Image *fframe, img_t *img) | |||
| 335 | 332 | unsigned long flags; | |
| 336 | 333 | unsigned int delay; | |
| 337 | 334 | bool err = false; | |
| 335 | + | data.bytes = NULL; | |
| 338 | 336 | ||
| 339 | 337 | if ((err = fframe == NULL && img == NULL)) | |
| 340 | 338 | goto fail; | |
| @@ -863,7 +861,7 @@ bool img_change_gamma(img_t *img, int d) | |||
| 863 | 861 | ||
| 864 | 862 | if (img->gamma != gamma) { | |
| 865 | 863 | imlib_reset_color_modifier(); | |
| 866 | − | if (gamma != 0) { | |
| 864 | + | if (gamma) { | |
| 867 | 865 | range = gamma <= 0 ? 1.0 : GAMMA_MAX - 1.0; | |
| 868 | 866 | imlib_modify_color_modifier_gamma(1.0 + gamma * (range / GAMMA_RANGE)); | |
| 869 | 867 | } | |
| @@ -169,7 +169,7 @@ void remove_file(int n, bool manual) | |||
| 169 | 169 | ||
| 170 | 170 | void set_timeout(timeout_f handler, int time, bool overwrite) | |
| 171 | 171 | { | |
| 172 | − | int i; | |
| 172 | + | unsigned int i; | |
| 173 | 173 | ||
| 174 | 174 | for (i = 0; i < ARRLEN(timeouts); i++) { | |
| 175 | 175 | if (timeouts[i].handler == handler) { | |
| @@ -185,7 +185,7 @@ void set_timeout(timeout_f handler, int time, bool overwrite) | |||
| 185 | 185 | ||
| 186 | 186 | void reset_timeout(timeout_f handler) | |
| 187 | 187 | { | |
| 188 | − | int i; | |
| 188 | + | unsigned int i; | |
| 189 | 189 | ||
| 190 | 190 | for (i = 0; i < ARRLEN(timeouts); i++) { | |
| 191 | 191 | if (timeouts[i].handler == handler) { | |
| @@ -234,7 +234,7 @@ void open_info(void) | |||
| 234 | 234 | int pfd[2]; | |
| 235 | 235 | char w[12], h[12]; | |
| 236 | 236 | ||
| 237 | − | if (info.f.err != 0 || info.fd >= 0 || win.bar.h == 0) | |
| 237 | + | if (info.f.err || info.fd >= 0 || win.bar.h == 0) | |
| 238 | 238 | return; | |
| 239 | 239 | win.bar.l.buf[0] = '\0'; | |
| 240 | 240 | if (pipe(pfd) < 0) | |
| @@ -379,7 +379,7 @@ void update_info(void) | |||
| 379 | 379 | else | |
| 380 | 380 | bar_put(r, "%ds" BAR_SEP, img.ss.delay / 10); | |
| 381 | 381 | } | |
| 382 | − | if (img.gamma != 0) | |
| 382 | + | if (img.gamma) | |
| 383 | 383 | bar_put(r, "G%+d" BAR_SEP, img.gamma); | |
| 384 | 384 | bar_put(r, "%3d%%" BAR_SEP, (int) (img.zoom * 100.0)); | |
| 385 | 385 | if (img.multi.cnt > 0) { | |
| @@ -423,7 +423,8 @@ void redraw(void) | |||
| 423 | 423 | ||
| 424 | 424 | void reset_cursor(void) | |
| 425 | 425 | { | |
| 426 | − | int c, i; | |
| 426 | + | int c; | |
| 427 | + | unsigned int i; | |
| 427 | 428 | cursor_t cursor = CURSOR_NONE; | |
| 428 | 429 | ||
| 429 | 430 | if (mode == MODE_IMAGE) { | |
| @@ -499,7 +500,7 @@ void run_key_handler(const char *key, unsigned int mask) | |||
| 499 | 500 | struct stat *oldst, st; | |
| 500 | 501 | XEvent dump; | |
| 501 | 502 | ||
| 502 | − | if (keyhandler.f.err != 0) { | |
| 503 | + | if (keyhandler.f.err) { | |
| 503 | 504 | if (!keyhandler.warned) { | |
| 504 | 505 | error(0, keyhandler.f.err, "%s", keyhandler.f.cmd); | |
| 505 | 506 | keyhandler.warned = true; | |
| @@ -586,7 +587,7 @@ end: | |||
| 586 | 587 | ||
| 587 | 588 | void on_keypress(XKeyEvent *kev) | |
| 588 | 589 | { | |
| 589 | − | int i; | |
| 590 | + | unsigned int i; | |
| 590 | 591 | unsigned int sh = 0; | |
| 591 | 592 | KeySym ksym, shksym; | |
| 592 | 593 | char dummy, key; | |
| @@ -629,7 +630,8 @@ void on_keypress(XKeyEvent *kev) | |||
| 629 | 630 | ||
| 630 | 631 | void on_buttonpress(XButtonEvent *bev) | |
| 631 | 632 | { | |
| 632 | − | int i, sel; | |
| 633 | + | int sel; | |
| 634 | + | unsigned int i; | |
| 633 | 635 | bool dirty = false; | |
| 634 | 636 | static Time firstclick; | |
| 635 | 637 | ||
| @@ -855,7 +857,7 @@ int main(int argc, char *argv[]) | |||
| 855 | 857 | ||
| 856 | 858 | if (options->clean_cache) { | |
| 857 | 859 | tns_init(&tns, NULL, NULL, NULL, NULL); | |
| 858 | − | tns_clean_cache(&tns); | |
| 860 | + | tns_clean_cache(); | |
| 859 | 861 | exit(EXIT_SUCCESS); | |
| 860 | 862 | } | |
| 861 | 863 | ||
| @@ -929,7 +931,7 @@ int main(int argc, char *argv[]) | |||
| 929 | 931 | const char *s = "/nsxiv/exec/"; | |
| 930 | 932 | ||
| 931 | 933 | for (i = 0; i < ARRLEN(cmd); i++) { | |
| 932 | − | n = strlen(homedir) + strlen(dsuffix) + strlen(name[i]) + strlen(s) + 1; | |
| 934 | + | n = strlen(homedir) + strlen(dsuffix) + strlen(s) + strlen(name[i]) + 1; | |
| 933 | 935 | cmd[i]->cmd = emalloc(n); | |
| 934 | 936 | snprintf(cmd[i]->cmd, n, "%s%s%s%s", homedir, dsuffix, s, name[i]); | |
| 935 | 937 | if (access(cmd[i]->cmd, X_OK) != 0) | |
| @@ -119,7 +119,7 @@ typedef enum { | |||
| 119 | 119 | typedef enum { | |
| 120 | 120 | SUFFIX_EMPTY, | |
| 121 | 121 | SUFFIX_BASENAME, | |
| 122 | − | SUFFIX_FULLPATH, | |
| 122 | + | SUFFIX_FULLPATH | |
| 123 | 123 | } suffixmode_t; | |
| 124 | 124 | ||
| 125 | 125 | typedef struct { | |
| @@ -198,7 +198,7 @@ typedef struct { | |||
| 198 | 198 | int cnt; | |
| 199 | 199 | int sel; | |
| 200 | 200 | bool animate; | |
| 201 | − | int framedelay; | |
| 201 | + | unsigned int framedelay; | |
| 202 | 202 | int length; | |
| 203 | 203 | } multi_img_t; | |
| 204 | 204 | ||
| @@ -272,7 +272,7 @@ struct opt { | |||
| 272 | 272 | /* window: */ | |
| 273 | 273 | bool fullscreen; | |
| 274 | 274 | bool hide_bar; | |
| 275 | − | long embed; | |
| 275 | + | Window embed; /* unsigned long */ | |
| 276 | 276 | char *geometry; | |
| 277 | 277 | char *res_name; | |
| 278 | 278 | const char *title_prefix; | |
| @@ -324,7 +324,7 @@ struct tns { | |||
| 324 | 324 | bool dirty; | |
| 325 | 325 | }; | |
| 326 | 326 | ||
| 327 | − | void tns_clean_cache(tns_t*); | |
| 327 | + | void tns_clean_cache(void); | |
| 328 | 328 | void tns_init(tns_t*, fileinfo_t*, const int*, int*, win_t*); | |
| 329 | 329 | CLEANUP void tns_free(tns_t*); | |
| 330 | 330 | bool tns_load(tns_t*, int, bool, bool); | |
| @@ -18,7 +18,6 @@ | |||
| 18 | 18 | */ | |
| 19 | 19 | ||
| 20 | 20 | #include "nsxiv.h" | |
| 21 | − | #define _IMAGE_CONFIG | |
| 22 | 21 | #define _TITLE_CONFIG | |
| 23 | 22 | #include "config.h" | |
| 24 | 23 | #include "version.h" | |
| @@ -49,7 +49,7 @@ char* tns_cache_filepath(const char *filepath) | |||
| 49 | 49 | if (strncmp(filepath, cache_dir, strlen(cache_dir)) != 0) { | |
| 50 | 50 | /* don't cache images inside the cache directory! */ | |
| 51 | 51 | len = strlen(cache_dir) + strlen(filepath) + 2; | |
| 52 | − | cfile = (char*) emalloc(len); | |
| 52 | + | cfile = emalloc(len); | |
| 53 | 53 | snprintf(cfile, len, "%s/%s", cache_dir, filepath + 1); | |
| 54 | 54 | } | |
| 55 | 55 | return cfile; | |
| @@ -120,7 +120,7 @@ end: | |||
| 120 | 120 | } | |
| 121 | 121 | } | |
| 122 | 122 | ||
| 123 | − | void tns_clean_cache(tns_t *tns) | |
| 123 | + | void tns_clean_cache(void) | |
| 124 | 124 | { | |
| 125 | 125 | int dirlen; | |
| 126 | 126 | char *cfile, *filename; | |
| @@ -150,7 +150,7 @@ void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel, win_t *wi | |||
| 150 | 150 | const char *homedir, *dsuffix = ""; | |
| 151 | 151 | ||
| 152 | 152 | if (cnt != NULL && *cnt > 0) { | |
| 153 | − | tns->thumbs = (thumb_t*) emalloc(*cnt * sizeof(thumb_t)); | |
| 153 | + | tns->thumbs = emalloc(*cnt * sizeof(thumb_t)); | |
| 154 | 154 | memset(tns->thumbs, 0, *cnt * sizeof(thumb_t)); | |
| 155 | 155 | } else { | |
| 156 | 156 | tns->thumbs = NULL; | |
| @@ -174,7 +174,7 @@ void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel, win_t *wi | |||
| 174 | 174 | const char *s = "/nsxiv"; | |
| 175 | 175 | free(cache_dir); | |
| 176 | 176 | len = strlen(homedir) + strlen(dsuffix) + strlen(s) + 1; | |
| 177 | − | cache_dir = (char*) emalloc(len); | |
| 177 | + | cache_dir = emalloc(len); | |
| 178 | 178 | snprintf(cache_dir, len, "%s%s%s", homedir, dsuffix, s); | |
| 179 | 179 | } else { | |
| 180 | 180 | error(0, 0, "Cache directory not found"); | |
| @@ -53,13 +53,13 @@ utf8_decode(void *buf, uint32_t *c, int *e) | |||
| 53 | 53 | *c >>= shiftc[len]; | |
| 54 | 54 | ||
| 55 | 55 | /* Accumulate the various error conditions. */ | |
| 56 | − | *e = (*c < mins[len]) << 6; // non-canonical encoding | |
| 57 | − | *e |= ((*c >> 11) == 0x1b) << 7; // surrogate half? | |
| 58 | − | *e |= (*c > 0x10FFFF) << 8; // out of range? | |
| 56 | + | *e = (*c < mins[len]) << 6; /* non-canonical encoding */ | |
| 57 | + | *e |= ((*c >> 11) == 0x1b) << 7; /* surrogate half? */ | |
| 58 | + | *e |= (*c > 0x10FFFF) << 8; /* out of range? */ | |
| 59 | 59 | *e |= (s[1] & 0xc0) >> 2; | |
| 60 | 60 | *e |= (s[2] & 0xc0) >> 4; | |
| 61 | 61 | *e |= (s[3] ) >> 6; | |
| 62 | − | *e ^= 0x2a; // top two bits of each tail byte correct? | |
| 62 | + | *e ^= 0x2a; /* top two bits of each tail byte correct? */ | |
| 63 | 63 | *e >>= shifte[len]; | |
| 64 | 64 | ||
| 65 | 65 | return next; | |
| @@ -82,7 +82,7 @@ void error(int eval, int err, const char* fmt, ...) | |||
| 82 | 82 | void size_readable(float *size, const char **unit) | |
| 83 | 83 | { | |
| 84 | 84 | const char *units[] = { "", "K", "M", "G" }; | |
| 85 | − | int i; | |
| 85 | + | unsigned int i; | |
| 86 | 86 | ||
| 87 | 87 | for (i = 0; i < ARRLEN(units) && *size > 1024.0; i++) | |
| 88 | 88 | *size /= 1024.0; | |
| @@ -172,7 +172,7 @@ void win_init(win_t *win) | |||
| 172 | 172 | void win_open(win_t *win) | |
| 173 | 173 | { | |
| 174 | 174 | int c, i, j, n; | |
| 175 | − | long parent; | |
| 175 | + | Window parent; | |
| 176 | 176 | win_env_t *e; | |
| 177 | 177 | XClassHint classhint; | |
| 178 | 178 | unsigned long *icon_data; | |
| @@ -188,7 +188,7 @@ void win_open(win_t *win) | |||
| 188 | 188 | XSetWindowAttributes attrs; | |
| 189 | 189 | ||
| 190 | 190 | e = &win->env; | |
| 191 | − | parent = options->embed != 0 ? options->embed : RootWindow(e->dpy, e->scr); | |
| 191 | + | parent = options->embed ? options->embed : RootWindow(e->dpy, e->scr); | |
| 192 | 192 | ||
| 193 | 193 | sizehints.flags = PWinGravity; | |
| 194 | 194 | sizehints.win_gravity = NorthWestGravity; | |
| @@ -199,16 +199,16 @@ void win_open(win_t *win) | |||
| 199 | 199 | else | |
| 200 | 200 | gmask = XParseGeometry(options->geometry, &win->x, &win->y, | |
| 201 | 201 | &win->w, &win->h); | |
| 202 | − | if ((gmask & WidthValue) != 0) | |
| 202 | + | if (gmask & WidthValue) | |
| 203 | 203 | sizehints.flags |= USSize; | |
| 204 | 204 | else | |
| 205 | 205 | win->w = WIN_WIDTH; | |
| 206 | − | if ((gmask & HeightValue) != 0) | |
| 206 | + | if (gmask & HeightValue) | |
| 207 | 207 | sizehints.flags |= USSize; | |
| 208 | 208 | else | |
| 209 | 209 | win->h = WIN_HEIGHT; | |
| 210 | − | if ((gmask & XValue) != 0) { | |
| 211 | − | if ((gmask & XNegative) != 0) { | |
| 210 | + | if (gmask & XValue) { | |
| 211 | + | if (gmask & XNegative) { | |
| 212 | 212 | win->x += e->scrw - win->w; | |
| 213 | 213 | sizehints.win_gravity = NorthEastGravity; | |
| 214 | 214 | } | |
| @@ -216,8 +216,8 @@ void win_open(win_t *win) | |||
| 216 | 216 | } else { | |
| 217 | 217 | win->x = 0; | |
| 218 | 218 | } | |
| 219 | − | if ((gmask & YValue) != 0) { | |
| 220 | − | if ((gmask & YNegative) != 0) { | |
| 219 | + | if (gmask & YValue) { | |
| 220 | + | if (gmask & YNegative) { | |
| 221 | 221 | win->y += e->scrh - win->h; | |
| 222 | 222 | sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity | |
| 223 | 223 | ? SouthEastGravity : SouthWestGravity; | |
| @@ -322,7 +322,7 @@ void win_open(win_t *win) | |||
| 322 | 322 | ||
| 323 | 323 | CLEANUP void win_close(win_t *win) | |
| 324 | 324 | { | |
| 325 | − | int i; | |
| 325 | + | unsigned int i; | |
| 326 | 326 | ||
| 327 | 327 | for (i = 0; i < ARRLEN(cursors); i++) | |
| 328 | 328 | XFreeCursor(win->env.dpy, cursors[i].icon); | |
| @@ -360,7 +360,7 @@ void win_toggle_fullscreen(win_t *win) | |||
| 360 | 360 | cm->window = win->xwin; | |
| 361 | 361 | cm->message_type = atoms[ATOM__NET_WM_STATE]; | |
| 362 | 362 | cm->format = 32; | |
| 363 | − | cm->data.l[0] = 2; // toggle | |
| 363 | + | cm->data.l[0] = 2; /* toggle */ | |
| 364 | 364 | cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN]; | |
| 365 | 365 | ||
| 366 | 366 | XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False, | |