Commit Graph

212 Commits

Author SHA1 Message Date
David Gowers 10a6228538 set autoreload timeout based on the latest event (#459)
currently the autoreload feature of nsxiv is a bit unreliable because we
try to load at the very first event we received. however, the writer
might not be done writing and so we might try to load a truncated image
(and fail).

in the following ascii diagram, function S represents sleep and `+` sign
represents writes by the writer. because we set the sleep (of 10ms) at
the first event, subsequent writes by the writer doesn't influence our
reload logic:

       S(10)                   load()
nsxiv        |                       |
writer       +           +           +           + (done)
time(ms):   00          05          10          15

after this patch, (assuming function T (re)sets a timeout), we will keep
(re)setting a timeout on new events giving the writer more time to
finish:

       T(10)       T(10)       T(10)       T(10)                   load()
nsxiv        |           |           |           |                       |
writer       +           +           +           + (done)
time(ms):   00          05          10          15          20          25

while this patch makes things significantly more robust, the problem
here is inherently unsolvable since there's no way to tell whether the
writer is done writing or not.

for example, if user does something like `curl 'some.png' > test.png`
then curl might stop for a second or two in the middle of writing due to
internet issues - which will make nsxiv drop the image.

this patch also increases the autoreload delay from 10ms to now 128ms
instead to decrease chances of false failures.

ref: 6ae2df6ed5
partially-fixes: https://codeberg.org/nsxiv/nsxiv/issues/456
commit-message-by: NRK
Reviewed-on: https://codeberg.org/nsxiv/nsxiv/pulls/459
Reviewed-by: eylles <eylles@noreply.codeberg.org>
2023-08-28 16:31:09 +06:00
NRK c1b36b8f09 add some upgrade comments (#457)
these document improvements and/or removal of unnecessary code for when
we will require a higher minimum version of Imlib2.

all the comments have been prefixed with "UPGRADE: " for easy grepping.

Reviewed-on: https://codeberg.org/nsxiv/nsxiv/pulls/457
Reviewed-by: explosion-mental <explosion-mental@noreply.codeberg.org>
2023-07-08 07:05:57 +00:00
NRK 9b122c82fa add compiled features into --version output (#462)
a lot of application which allow selecting features at build time seem
to output the build config with `--version` or similar (e.g ffmpeg).

aside from giving the user information about the feature set the binary
was compiled with (in case the user didn't compile it themselves, e.g on
a binary distro) it can also (possibly) help when submitting bug
reports.

Reviewed-on: https://codeberg.org/nsxiv/nsxiv/pulls/462
Reviewed-by: explosion-mental <explosion-mental@noreply.codeberg.org>
2023-07-08 06:53:57 +00:00
NRK 0e1bc3c045 set a default delay if delay is 0 (#445)
gif spec [0] doesn't mention what to do when "Delay Time" is 0.

apng spec [1] states:

| If the the value of the numerator is 0 the decoder should render the
| next frame as quickly as possible, though viewers may impose a
| reasonable lower bound.

webp spec [2]:

| the interpretation of frame duration of 0 (and often <= 10) is
| implementation defined.

so it seems that it's safe to set a default delay for 0 delay frames,
which is what the older gif and webp loaders were already doing. do the
same for the imlib2 multi-frame loader as well.

[0]: https://www.w3.org/Graphics/GIF/spec-gif89a.txt
[1]: https://wiki.mozilla.org/APNG_Specification
[2]: https://developers.google.com/speed/webp/docs/riff_container#animation

Reviewed-on: https://codeberg.org/nsxiv/nsxiv/pulls/445
Reviewed-by: eylles <eylles@noreply.codeberg.org>
2023-05-23 11:36:41 +00:00
NRK d0ec8716d7 fix: calling imlib2 with color modifier being NULL (#440)
the multiframe loaders sets the color modifier to NULL but doesn't
restore it before returning. this results in a imlib2 developer warning
if you try to change brightness/contrast on a multiframe image (which
doesn't have alpha).

ensure that the color modifier is restored before returning under all
paths.

Reported-by: Madhu

Reviewed-on: https://codeberg.org/nsxiv/nsxiv/pulls/440
Reviewed-by: eylles <eylles@noreply.codeberg.org>
2023-05-18 15:06:44 +00:00
NRK 4b67816eae fix: loading old frames due to multi-frame cache (#437)
by default imlib2 doesn't check the file's timestamp to avoid disk
activity when loading from cache. however, this ends up breaking our
autoreload functionality on multi-frame images.

the reason why single frame images weren't broken was because
`img_load()` calls `imlib_image_set_changes_on_disk()`, which tells
imlib2 to check the timestamp before loading from cache.

do the same thing for the multi-frame loader as well.

additionally add a comment to img_load() explaining what's going on.

Closes: https://codeberg.org/nsxiv/nsxiv/issues/436

Reviewed-on: https://codeberg.org/nsxiv/nsxiv/pulls/437
Reviewed-by: eylles <eylles@noreply.codeberg.org>
2023-05-18 15:05:45 +00:00
NRK f2f4903de4 apply clang-format
minus the bogus changes

Co-authored-by: Berke Kocaoğlu <berke.kocaoglu@metu.edu.tr>
2023-03-11 18:37:04 +06:00
NRK 2434e83807 make some changes prepping for clang-format
Co-authored-by: Berke Kocaoğlu <berke.kocaoglu@metu.edu.tr>
2023-03-11 18:36:26 +06:00
NRK a051ad210d update copyright year 2023-01-28 16:29:36 +06:00
NRK 6ffc64a04e img_t: rename alpha -> alpha_layer for consistency 2023-01-28 10:23:11 +00:00
NRK 1f788a318b add cli flag --alpha-layer
now that we have long-opts, we don't have to worry about exhausting the
alphabet list for short-opts. so adding a cli flag to set/unset the
checker background makes sense.

ref: https://codeberg.org/nsxiv/nsxiv/issues/404
2023-01-28 10:23:11 +00:00
NRK 76c2b81b60 add support for multi-frame images via imlib2 (#373)
this will be a massive change compared to the usual stuff. however the
gains will be worth it:

* we gain lots of additional animated image support.
* and we'll gain _even_ more format support as imlib2 adds them, without needing
  any change in our code-base.
* about ~300 LoC will be purged once we remove our internal gif and webp loader.

as for when to remove the internal loaders, a good time might be when debian
upgrades their imlib2, currently it seems to be at v1.7.5, which doesn't support
animated images.

as of now, nsxiv will continue to build with the internal gif/webp loaders
(assuming they were enabled in config.mk) if imlib2 version is below 1.8.0 and
will print out a deprecation notice.

and if imlib2 version supports multi-frame then it will simply ignore the
internal loaders and use the imlib2 one.

in other words, users shouldn't need to do anything on their side. everything
that previously functioned will continue to function regardless of the user's
imlib2 version (though they might see the annoying deprecation notice if the
imlib2 version doesn't support multi-frame images).

known issue:

* image loading performance can be noticeably worse in
  imlib2 versions below 1.9.0

Closes: https://codeberg.org/nsxiv/nsxiv/issues/301
Closes: https://codeberg.org/nsxiv/nsxiv/issues/300
Reviewed-on: https://codeberg.org/nsxiv/nsxiv/pulls/373
Reviewed-by: TAAPArthur <taaparthur@noreply.codeberg.org>
2023-01-08 10:02:56 +00:00
Berke Kocaoğlu 95bc9b463b add brightness and contrast (#396)
* Imlib2 supports modifying gamma, brightness and contrast directly
  while sxiv only supports gamma. Makes sense to extend it to brightness
  and contrast as well.

* Since color corrections need to be aware of each other, they have been
  refactored into one centralized function.

* This also makes the code more hackable as it makes it easier to add
  more color correction functions without them interfering with each
  other.

Co-authored-by: 0ion9 <finticemo@gmail.com>
Co-authored-by: NRK <nrk@disroot.org>
Reviewed-on: https://codeberg.org/nsxiv/nsxiv/pulls/396
Reviewed-by: NRK <nrk@disroot.org>
Reviewed-by: TAAPArthur <taaparthur@noreply.codeberg.org>
Co-authored-by: Berke Kocaoğlu <kberke@metu.edu.tr>
Co-committed-by: Berke Kocaoğlu <kberke@metu.edu.tr>
2022-12-22 11:21:40 +00:00
NRK 3140638042 fix: wrong length on animated webp (#381)
the length member needed to be zero-ed before we started decoding.
this was causing unintended behavior of playing an animated webp longer
than it should under slideshow mode.

Reviewed-on: https://codeberg.org/nsxiv/nsxiv/pulls/381
Reviewed-by: Berke Kocaoğlu <berke.kocaoglu@metu.edu.tr>
2022-10-25 20:34:19 +02:00
NRK e197429489 img_open: explicitly decode image data immediately (#380)
the way `imlib_load_image()` works, is that it only does a lightweight
signature/metadata check. it doesn't actually decode the image. which
means that a file that has valid metadata but invalid content would get
loaded successfully.

`imlib_image_get_data_for_reading_only()` basically forces imlib to
decode the data, and thus reveal any malformed images so we can reject
it (see commit f0266187).

however, this is a spurious way of achieving the goal at hand. imlib2
already offers an `_immediately` variant which decodes the data
immediately. so just use that instead of spuriously using the "get_data"
function to force a decode.

Reviewed-on: https://codeberg.org/nsxiv/nsxiv/pulls/380
Reviewed-by: Berke Kocaoğlu <berke.kocaoglu@metu.edu.tr>
2022-10-21 08:02:54 +02:00
NRK b11384a694 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>
2022-10-19 14:46:22 +02:00
NRK e356add07c rename: aa -> anti_alias 2022-09-10 19:43:14 +06:00
NRK 88a480c938 allow disabling anti-aliasing via cli flag
simply running nsxiv with `--anti-alias` will enable anti-aliasing, and
running it with `--anti-alias=no` will disable it.

the cli flag will overwrite the config.h default.

Closes: https://codeberg.org/nsxiv/nsxiv/issues/349
2022-09-10 19:43:07 +06:00
explosion-mental 0f0c49a630 code-style: don't indent switch cases (#358)
The suckless coding style [^0] and the linux coding style [^1] both
recommends not indenting switch cases. And it helps out people with
lower resolution monitors.

[^0]: https://suckless.org/coding_style/
[^1]: https://www.kernel.org/doc/html/v5.10/process/coding-style.html#indentation

Co-authored-by: explosion-mental <explosion0mental@gmail.com>
Reviewed-on: https://codeberg.org/nsxiv/nsxiv/pulls/358
Reviewed-by: NRK <nrk@disroot.org>
Co-authored-by: explosion-mental <explosion-mental@noreply.codeberg.org>
Co-committed-by: explosion-mental <explosion-mental@noreply.codeberg.org>
2022-08-16 10:54:31 +02:00
NRK becf1cc858 fix: build failure when _SC_PHYS_PAGES is not defined
_SC_PHYS_PAGES isn't POSIX and might not be defined. in such case, just
return back `CACHE_SIZE_FALLBACK`.

NOTE: POSIX says the `names` in `sysconf()` are "symbolic constants" not
necessarily macros. So we might end up returning the fallback in some
cases where `_SC_PHYS_PAGES` *was* available, but not defined as a
macro. which is not ideal, but nothing fatal.

in practice, this shouldn't be an issue since most systems seems to
define them to be macros, i've checked Glibc, Musl, OpenBSD, FreeBSD and
Haiku.

also add a (useful) comment on `config.h` describing the effect higher
cache size has.

Closes: https://codeberg.org/nsxiv/nsxiv/issues/354
2022-08-11 03:58:26 +02:00
NRK c131b1ed83 fix: -Wsign-compare warnings (#336)
mixing signed and unsigned types in comparison can end up having
unintended results. for example:

	if (-1 < 1U)
		printf("true\n");
	else
		printf("false\n");

previously we silenced these warnings, instead just fix them properly
via necessary casting, and in cases where the value cannot be negative
(e.g width/height members) make them unsigned.

Reviewed-on: https://codeberg.org/nsxiv/nsxiv/pulls/336
Reviewed-by: explosion-mental <explosion-mental@noreply.codeberg.org>
2022-07-15 22:46:23 +02:00
NRK 7c59cc7edc fix build failure due to missing <stdio.h> (#337)
the following combination currently fails to build:

	$ make HAVE_LIBFONTS=0 HAVE_LIBWEBP=1

this is probably because one of the font library was including <stdio.h>

Reviewed-on: https://codeberg.org/nsxiv/nsxiv/pulls/337
Reviewed-by: Berke Kocaoğlu <berke.kocaoglu@metu.edu.tr>
Reviewed-by: TAAPArthur <taaparthur@noreply.codeberg.org>
Co-authored-by: NRK <nrk@disroot.org>
Co-committed-by: NRK <nrk@disroot.org>
2022-07-03 08:16:56 +02:00
NRK 6d5a04005d code-style: cleanup includes
* rm unused include <sys/types.h>
* move <sys/time.h> to main.c, it's the only file that needs it.
* move TV_* macros to main.c
* let *.c files explicitly include what they need instead of including
  them at nsxiv.h
2022-06-28 21:28:06 +02:00
NRK 5c3a796e55 [ci]: slience some clang-tidy warnings
the warnings on r_readdir(), img_load_gif() and strcpy seems to be false
positives. the warning about fmt being unused is valid, but not worth
fixing with additional #ifdef guards.

use `assert` to silence the false positive cases when possible,
otherwise use a NOLINT comment with an explanation.
2022-06-25 08:27:01 +02:00
NRK b28449e10c fix: don't use reserved identifiers
identifiers beginning with an underscore is reserved by the C standard.
2022-06-25 08:27:01 +02:00
NRK 810a9651a3 reduce calls to win-title
rather than calling the script unconditionally per redraw, we now have
a `title_dirty` flag and keep track of when any of the relavent
information changes.

Co-authored-by: Arthur Williams <taaparthur@gmail.com>
Partially fixes: https://github.com/nsxiv/nsxiv/issues/258
2022-06-02 08:09:51 +00:00
N-R-K 29c6b1456e
code-style: reduce some unnecessary if-elses (#261)
also change the condition inside img_frame_animate() to check for
positive value rather than comparing against 0.
2022-04-27 01:25:11 +00:00
N-R-K f05165a77a
don't quit if imlib_create_image() fails (#248)
...simply print an error msg and try (slower) fallback.

also adds a useful comment explaining why we're doing manual blending.
2022-04-12 17:05:59 +00:00
N-R-K bda70867ac
add config.h option for top statusbar (#231)
Closes: https://github.com/nsxiv/nsxiv/issues/230
Co-authored-by: mamg22 <45301823+mamg22@users.noreply.github.com>
2022-02-26 16:38:53 +00:00
NRK 9cdeeab9b8 update copyright year 2022-02-13 19:35:58 +03:00
Nick Hanley 2ac44709bd
Add keybind to scroll to image center (#203)
There are keybinds for scrolling to the edges of an image but there's no way back to the center. This is particularly annoying while zooming.
2022-01-15 18:51:31 -04:00
NRK 5a3fb3b812 fix -Wmaybe-uninitialized warning 2022-01-06 17:53:02 +00:00
NRK 90bec70e7f fix -Wshadow related warnings
fixes all -Wshadow related warnings (on gcc). this would allow us to use
`-Wshadow` in github workflow (https://github.com/nsxiv/nsxiv/pull/195).

i've thought about adding `-Wshadow` to our Makefile as well, but
decided against it to keep the Makefile CFLAGS barebore/minimal.
2022-01-06 17:53:02 +00:00
N-R-K e839638156
fix: jpeg exif orientation on Imlib2 v1.7.5 (#188)
since Imlib2 v1.7.5, it is capable of parsing exif data on jpeg files
and auto orienting them. this caused nsxiv to rotate the image twice.

Closes: https://github.com/nsxiv/nsxiv/issues/187
2022-01-02 02:28:19 +06:00
MahouShoujoMivutilde e777adf985
Allow setting cache size based on memory percentage (#184)
The problem:

1. For the most people imlib2's default 4MiB is unreasonably low;

2. Hardcoding cache size to ~256MiB has performance benefits and doesn't
   increase RAM usage too much on relatively modern systems;

3. But we can't do that, because that would be detrimental to low spec systems
   that (apparently) not (?) building nsxiv from source, as been discussed
   #171

Solution:

Calculate cache size based on total memory.

Default is set as 3%, which means:
* ~245MiB for 8GiB
* ~30MiB for 1GiB
* and so on

CACHE_SIZE_LIMIT (256MiB by default) sets the highest possible value. And in
case we cannot determine the total memory (e.g since _SC_PHYS_PAGES isn't
POSIX) use CACHE_SIZE_FALLBACK (32MiB by default) instead.

Co-authored-by: NRK <nrk@disroot.org>
2022-01-01 14:55:59 +06:00
NRK 61da0a8ca9 refactor: move shared code into a function 2021-12-12 17:58:17 +06:00
NRK b0b16f1d0f img_load_webp: close the file handler and use errno 2021-12-12 17:58:17 +06:00
NRK 22ca3b567d img_load_webp: simplify multi-frame allocation
C89 standard specifies:
If ptr is a null pointer, the realloc function behaves like the malloc
function for the specified size.
2021-12-12 17:58:17 +06:00
NRK 3b6db44267 img_load_webp: remove unnecessary casting 2021-12-12 17:58:17 +06:00
NRK eccd7de532 remove unneeded animated-webp hacks
now that imlib2 (v1.7.5) is able to load the first frame of an
animated-webp file, we no longer need the `is_webp` check to bypass
imlib2.

ref: https://phab.enlightenment.org/T8964
2021-12-12 17:58:17 +06:00
N-R-K 36a4847bcf
fix: compile error (#182)
* fix compile error

* use variable instead of macro

* Revert "use variable instead of macro"

This reverts commit a14ef0b231c50e49906761010a4d4231ce4e3e36.

* use local variable instead of macro

* Revert "use local variable instead of macro"

This reverts commit 7e049d55d94f5c003d90e1a10187356f6a7f54b2.
2021-12-09 22:03:18 +03:00
N-R-K 79b8fefcc4
bring back zoom_levels (#156)
this still keeps the shorter zoom logic, but adds back the zoom_levels
array so that stay close to sxiv.

for users who would like to have the zoom step behavior see:
https://github.com/nsxiv/nsxiv/pull/156#issuecomment-975182631
2021-12-01 12:27:17 +00:00
mamg22 127abbea77
fix crash when zooming out on small images (#178)
When rendering images, destination image width and height may become
zero due to float-to-int conversion after zoom calculation, later
crashing when creating an image using those dimensions. This sets
a minimum of 1 to both variables.

Closes #82
2021-12-01 05:52:09 +06:00
NRK 470f50f9f8 fix: img_load_gif: out-of-bound access
according to imlib2's gif loader, bg should already be transparent black
if bg > cmap->ColorCount
2021-11-26 15:36:05 +06:00
NRK ca7368aa89 Revert "fix: out-of-bounds access when bg not in color map"
with this patch certain gif images will fail to play. one other problem
here is that it suddenly breaks the loop without free-ing data and rows,
leading to a memory leak.

regardless, this needs to be investigated further.

here's an example image where this happens:
https://i.postimg.cc/SQf1TJJg/awoo-study.gif

This reverts commit cca7834e67.
2021-11-26 15:36:05 +06:00
N-R-K 0639047dde allow configuring imlib2's cache size (#171)
by default imlib2 uses a 4mb cache, which is quite small. this allows
users who have more memory to spare to set a bigger cache size and avoid
reloading an already viewed image if it fits into the cache.

Co-authored-by: Berke Kocaoğlu <berke.kocaoglu@metu.edu.tr>
2021-11-26 03:09:46 +06:00
N-R-K c6275374b0
mark functions and vars as static (#146)
the goal here to mark functions and variables not used outside the
translation unit as static. main reason for this is cleanliness. however
as a side-effect this can help compilers optimize better as it now has
guarantee that a certain function won't be called outside of that
translation unit.

one other side-effect of this is that accessing these vars/function from
config.h is now different.

if one wants to access a static var/func from different translation unit
in config.h, he would have to create a wrapper function under the right
ifdef. for static functions one would also need to forward declare it.
here's a dummy example of accessing the function `run_key_handler` from
config.h under _MAPPINGS_CONFIG

```
static void run_key_handler(const char *, unsigned);
bool send_with_ctrl(arg_t key) {
	run_key_handler(XKeysymToString(key), ControlMask);
	return false;
}
```
2021-11-20 03:51:49 +00:00
N-R-K 850bc788c3
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


Co-authored-by: uidops <uidops@protonmail.com>
Co-authored-by: Arthur Williams <taaparthur@gmail.com>
2021-10-29 02:00:53 +06:00
N-R-K 03eb664e89
remove unnecessary animated webp related check
this code snippet was introduced in
2703809a23
but does not seem to be needed.

from what i can tell this is some sort of hack that might've been needed
back when we didn't bypass imlib2 when loading webp.
2021-10-28 16:45:26 +06:00
eylles 5b3221cfa6
update copyright notice (#139) 2021-10-28 16:41:16 +06:00