pax_global_header 0000666 0000000 0000000 00000000064 12737703411 0014517 g ustar 00root root 0000000 0000000 52 comment=751f3f9020838c6885d833c9ef72baf0cea3b69c
pqiv-2.6/ 0000775 0000000 0000000 00000000000 12737703411 0012345 5 ustar 00root root 0000000 0000000 pqiv-2.6/GNUmakefile 0000664 0000000 0000000 00000015622 12737703411 0014425 0 ustar 00root root 0000000 0000000 # pqiv Makefile
#
# Default flags, overridden by values in config.make
CFLAGS=-O2 -g
CROSS=
DESTDIR=
GTK_VERSION=0
PQIV_WARNING_FLAGS=-Wall -Wextra -Wfloat-equal -Wpointer-arith -Wcast-align -Wstrict-overflow=5 -Wwrite-strings -Waggregate-return -Wunreachable-code -Wno-unused-parameter
LDLIBS=-lm
PREFIX=/usr
MANDIR=$(PREFIX)/share/man
EXECUTABLE_EXTENSION=
PKG_CONFIG=$(CROSS)pkg-config
OBJECTS=pqiv.o lib/strnatcmp.o lib/bostree.o lib/filebuffer.o lib/config_parser.o
HEADERS=pqiv.h lib/bostree.h lib/filebuffer.h lib/strnatcmp.h
BACKENDS=gdkpixbuf
EXTRA_DEFS=
BACKENDS_BUILD=static
# Load config.make (created by configure)
ifeq ($(wildcard config.make),config.make)
include config.make
HEADERS+=config.make
endif
# pkg-config lines for the main program
LIBS_GENERAL=glib-2.0 >= 2.8 cairo >= 1.6 gio-2.0
LIBS_GTK3=gtk+-3.0 gdk-3.0
LIBS_GTK2=gtk+-2.0 >= 2.6 gdk-2.0 >= 2.8
# pkg-config libraries for the backends
LIBS_gdkpixbuf=gdk-pixbuf-2.0 >= 2.2
LIBS_poppler=poppler-glib
LIBS_spectre=libspectre
LIBS_wand=MagickWand
LIBS_libav=libavformat libavcodec libswscale libavutil
LIBS_archive_cbx=libarchive gdk-pixbuf-2.0 >= 2.2
# Disable the automated compilation of the libav backend
DISABLE_AUTOMATED_BUILD_libav=yes
# This might be required if you use mingw, and is required as of
# Aug 2014 for mxe, but IMHO shouldn't be required / is a bug in
# poppler (which does not specify this dependency). If it isn't
# or throws an error for you, please report this as a bug:
#
ifeq ($(EXECUTABLE_EXTENSION),.exe)
LDLIBS_poppler+=-llcms2 -lstdc++
endif
# If no GTK_VERSION is set, try to auto-determine, with GTK 3 preferred
ifeq ($(GTK_VERSION), 0)
ifeq ($(shell $(PKG_CONFIG) --errors-to-stdout --print-errors "$(LIBS_GTK3)"), )
LIBS=$(LIBS_GTK3)
else
LIBS=$(LIBS_GTK2)
endif
endif
ifeq ($(GTK_VERSION), 2)
LIBS=$(LIBS_GTK2)
endif
ifeq ($(GTK_VERSION), 3)
LIBS=$(LIBS_GTK3)
endif
LIBS+=$(LIBS_GENERAL)
# Add platform specific libraries
# GIo for stdin loading,
# X11 to workaround a bug, see http://stackoverflow.com/questions/18647475
ifeq ($(EXECUTABLE_EXTENSION), .exe)
LIBS+=gio-windows-2.0
else
LIBS+=gio-unix-2.0 x11
endif
# Add backend-specific libraries and objects
SHARED_OBJECTS=
SHARED_BACKENDS=
BACKENDS_INITIALIZER:=backends/initializer
define handle-backend
ifneq ($(origin LIBS_$(1)),undefined)
ifneq ($(findstring $(1), $(BACKENDS)),)
ifeq ($(BACKENDS_BUILD), shared)
ifeq ($(shell $(PKG_CONFIG) --errors-to-stdout --print-errors "$(LIBS_$(1))" 2>&1), )
SHARED_OBJECTS+=backends/pqiv-backend-$(1).so
BACKENDS_BUILD_CFLAGS_$(1):=$(shell $(PKG_CONFIG) --errors-to-stdout --print-errors --cflags "$(LIBS_$(1))" 2>&1)
BACKENDS_BUILD_LDLIBS_$(1):=$(shell $(PKG_CONFIG) --errors-to-stdout --print-errors --libs "$(LIBS_$(1))" 2>&1)
SHARED_BACKENDS+="$(1)",
endif
else
LIBS+=$(LIBS_$(1))
OBJECTS+=backends/$(1).o
LDLIBS+=$(LDLIBS_$(1))
BACKENDS_INITIALIZER:=$(BACKENDS_INITIALIZER)-$(1)
endif
endif
endif
endef
$(foreach BACKEND_C, $(wildcard backends/*.c), $(eval $(call handle-backend,$(basename $(notdir $(BACKEND_C))))))
PIXBUF_FILTER="gdkpixbuf",
ifeq ($(BACKENDS_BUILD), shared)
CFLAGS_SHARED=-fPIC
OBJECTS+=backends/shared-initializer.o
BACKENDS_BUILD_CFLAGS_shared-initializer=-DSHARED_BACKENDS='$(filter $(PIXBUF_FILTER), $(SHARED_BACKENDS)) $(filter-out $(PIXBUF_FILTER), $(SHARED_BACKENDS))'
LIBS+=gmodule-2.0
LDFLAGS_RPATH=-Wl,-rpath,'$$ORIGIN/backends',-rpath,'$$ORIGIN/../lib/pqiv',-rpath,'$(PREFIX)/lib/pqiv'
else
CFLAGS_SHARED=
OBJECTS+=$(BACKENDS_INITIALIZER).o
endif
# Add version information to builds from git
PQIV_VERSION_STRING=$(shell [ -d .git ] && (which git 2>&1 >/dev/null) && git describe --dirty --tags)
ifneq ($(PQIV_VERSION_STRING),)
PQIV_VERSION_FLAG=-DPQIV_VERSION=\"$(PQIV_VERSION_STRING)\"
endif
ifdef DEBUG
DEBUG_CFLAGS=-DDEBUG
else
DEBUG_CFLAGS=-DNDEBUG
endif
# Less verbose output
ifndef VERBOSE
SILENT_CC=@echo " CC " $@;
SILENT_CCLD=@echo " CCLD" $@;
SILENT_GEN=@echo " GEN " $@;
endif
# Assemble final compiler flags
CFLAGS_REAL=-std=gnu99 $(PQIV_WARNING_FLAGS) $(PQIV_VERSION_FLAG) $(CFLAGS) $(CFLAGS_SHARED) $(DEBUG_CFLAGS) $(EXTRA_DEFS) $(shell $(PKG_CONFIG) --cflags "$(LIBS)")
LDLIBS_REAL=$(shell $(PKG_CONFIG) --libs "$(LIBS)") $(LDLIBS)
LDFLAGS_REAL=$(LDFLAGS) $(LDFLAGS_RPATH)
all: pqiv$(EXECUTABLE_EXTENSION) $(SHARED_OBJECTS)
.PHONY: get_libs get_available_backends _build_variables clean distclean install uninstall all
.SECONDARY:
pqiv$(EXECUTABLE_EXTENSION): $(OBJECTS)
$(SILENT_CCLD) $(CROSS)$(CC) $(CPPFLAGS) -o $@ $+ $(LDLIBS_REAL) $(LDFLAGS_REAL)
ifeq ($(BACKENDS_BUILD), shared)
backends/%.o: backends/%.c $(HEADERS)
$(SILENT_CC) $(CROSS)$(CC) $(CPPFLAGS) -c -o $@ $(CFLAGS_REAL) $(BACKENDS_BUILD_CFLAGS_$*) $<
backends/pqiv-backend-%.so: backends/%.o
$(SILENT_CCLD) $(CROSS)$(CC) -shared $(CPPFLAGS) -o $@ $+ $(LDLIBS_REAL) $(LDFLAGS_REAL) $(BACKENDS_BUILD_LDLIBS_$*)
endif
%.o: %.c $(HEADERS)
$(SILENT_CC) $(CROSS)$(CC) $(CPPFLAGS) -c -o $@ $(CFLAGS_REAL) $<
$(BACKENDS_INITIALIZER).c:
@$(foreach BACKEND, $(sort $(BACKENDS)), [ -e backends/$(BACKEND).c ] || { echo; echo "Backend $(BACKEND) not found!" >&2; exit 1; };)
$(SILENT_GEN) ( \
echo '/* Auto-Generated file by Make. */'; \
echo '#include "../pqiv.h"'; \
echo "file_type_handler_t file_type_handlers[$(words $(BACKENDS)) + 1];"; \
$(foreach BACKEND, $(sort $(BACKENDS)), echo "void file_type_$(BACKEND)_initializer(file_type_handler_t *info);";) \
echo "void initialize_file_type_handlers() {"; \
echo " int i = 0;"; \
$(foreach BACKEND, $(filter gdkpixbuf, $(BACKENDS)), echo " file_type_$(BACKEND)_initializer(&file_type_handlers[i++]);";) \
$(foreach BACKEND, $(sort $(filter-out gdkpixbuf, $(BACKENDS))), echo " file_type_$(BACKEND)_initializer(&file_type_handlers[i++]);";) \
echo "}" \
) > $@
install: all
mkdir -p $(DESTDIR)$(PREFIX)/bin
install pqiv$(EXECUTABLE_EXTENSION) $(DESTDIR)$(PREFIX)/bin/pqiv$(EXECUTABLE_EXTENSION)
-mkdir -p $(DESTDIR)$(MANDIR)/man1
-install --mode=644 pqiv.1 $(DESTDIR)$(MANDIR)/man1/pqiv.1
ifeq ($(BACKENDS_BUILD), shared)
mkdir -p $(DESTDIR)$(PREFIX)/lib/pqiv
install $(SHARED_OBJECTS) $(DESTDIR)$(PREFIX)/lib/pqiv/
endif
uninstall:
rm -f $(DESTDIR)$(PREFIX)/bin/pqiv$(EXECUTABLE_EXTENSION)
rm -f $(DESTDIR)$(MANDIR)/man1/pqiv.1
ifeq ($(BACKENDS_BUILD), shared)
rm -f $(foreach SO_FILE, $(SHARED_OBJECTS), $(DESTDIR)$(PREFIX)/lib/pqiv/$(notdir $(SO_FILE)))
rmdir $(DESTDIR)$(PREFIX)/lib/pqiv
endif
clean:
rm -f pqiv$(EXECUTABLE_EXTENSION) *.o backends/*.o backends/*.so lib/*.o backends/initializer-*.c
distclean: clean
rm -f config.make
get_libs:
$(info LIBS: $(LIBS))
@true
get_available_backends:
@echo -n "BACKENDS: "; $(foreach BACKEND_C, $(wildcard backends/*.c), \
[ "$(DISABLE_AUTOMATED_BUILD_$(basename $(notdir $(BACKEND_C))))" != "yes" ] && \
[ -n "$(LIBS_$(basename $(notdir $(BACKEND_C))))" ] && \
$(PKG_CONFIG) --exists "$(LIBS_$(basename $(notdir $(BACKEND_C))))" \
&& echo -n "$(basename $(notdir $(BACKEND_C))) ";) echo
@true
pqiv-2.6/README.markdown 0000664 0000000 0000000 00000021646 12737703411 0015057 0 ustar 00root root 0000000 0000000 PQIV README
===========
About pqiv
----------
Originally, PQIV was written as a drop-in replacement for QIV
(http://spiegl.de/qiv/), as QIV was unmaintained and used imlib, a deprecated
library, which was to be removed from Gentoo Linux. The first release was not
more than a Python script, hence the name. After some month I realized that
nobody would do a better version, so I did it myself. Back then, PQIV became a
(modulo some small extras) full featured clone of QIV written in C using GTK 2
and GLIB 2.
When Debian decided to do the same step regarding imlib, a new developer stepped
in for QIV's klografx.net team and updated QIV to use GDK 2 and imlib 2. As of
May 2009, both programs are usable again and their features are likely to
diverge.
In the meantime, I have had some new ideas and learned (from my daily use)
about which features were really useful and which were merely overhead. In 2013
I decided to rewrite pqiv from scratch which these in mind, this time using the
third version of GTK and its backend, cairo. The code was tested on numerous
platforms, and is also backwards compatible with GTK 2.
Features
--------
* Command line image viewer
* Directory traversing to view whole directories
* Watch files and directories for changes
* Natural order sorting of the images
* A status bar showing information on the current image
* Transparency and animation support
* Moving, zooming, rotation, flipping
* Slideshows
* Highly customizable and scriptable (see `--actions-from-stdin` and `--bind-key`)
* Supports external image filters (e.g. `convert`)
* Preloads the next image in the background
* Fade between images
* Optional PDF/eps/ps support (useful e.g. for scientific plots)
* Optional video format support (e.g. for webm animations)
Installation
------------
Usual stuff. `./configure && make && make install`. The configure script is
optional if you only want gdk-pixbuf support and will autodetermine which
backends to build if invoked without parameters.
You'll need
* gtk+ 3.0 *or* gtk+ 2.6
* gdk-pixbuf 2.2 (included in gtk+)
* glib 2.6
* cairo 1.6
* gio 2.0
* gdk 2.8
and optionally also
* libspectre (any version, for ps/eps support)
* poppler (any version, for pdf support)
* MagickWand (any version, for additional image formats like psd)
* libarchive (for cbX comic book files)
* ffmpeg / libav (for video support, only included if explicitly compiled in)
The backends are per default linked statically into the code, so all backend
related build-time dependencies are also run-time dependencies. If you need
a shared version of the backends, for example for separate packaging of
the binaries, use the `--backends-build=shared` option. This is only supported
on Linux platforms currently.
There are experimental, nightly [static builds available for
download](http://page.mi.fu-berlin.de/pberndt/pqiv-builds/) for Windows and
Linux: 
Thanks
------
This program uses Martin Pool's natsort algorithm
Contributors
------------
Contributors to pqiv 2.x are:
* J. Paul Reed
Contributors to pqiv ≤ 1.0 were:
* Alexander Sulfrian
* Alexandros Diamantidis
* Brandon
* David Lindquist
* Hanspeter Gysin
* John Keeping
* Nir Tzachar
* Rene Saarsoo
* Tinoucas
* Yaakov
Known bugs
----------
* **The window is centered in between two monitors in old multi-head setups**:
This happens if you have the RandR extension enabled, but configured
incorrectly. GTK is programmed to first try RandR and use Xinerama only as
a fallback if that fails. (See `gdkscreen-x11.c`.) So if your video drivers
for some reason detect your multiple monitors as one big screen you can not
simply use fakexinerama to fix things. This might also apply to nvidia drivers
older than version 304. I believe that I can not fix this without breaking
functionality for other users or maintaining a blacklist, so you should
deactivate RandR completely until your driver is able to provide correct
information, or use a fake xrand (like
[mine](https://github.com/phillipberndt/fakexrandr), for example)
* **Loading postscript files failes with `Error #12288; Unknown output format`**:
This issue happens if your poppler and spectre libraries are linked against
different versions of libcms. libcms and libcms2 will both be used, but
interfere with each other. Compile using `--backends-build=shared` to
circumvent this issue.
Changelog
---------
pqiv 2.6
* Added --enforce-window-aspect-ratio
* Do not enforce the aspect ratio of the window to match the image's by default
pqiv 2.5.1
* Prevent a crash in --lazy-load mode if many images fail to load
pqiv 2.5
* Added a configure option to build the backends as shared libraries
* Added a configure option to remove unneeded/unwanted features
* Added --watch-files to make the file-changed-on-disk action configurable
* Added support for cbz/cbr/cbt/cb7 comic books
* Key bindings are now configurable
* Deprecated --keyboard-alias and --reverse-cursor-keys in favor of
--bind-key.
* Added --actions-from-stdin to make pqiv scriptable
* Added --recreate-window to create a new window instead of resizing the
old one, as a workaround for buggy window managers
* Fixed crash on reloading of images created by pipe-command output
pqiv 2.4.1
* Fix --end-of-files-action=quit if only one file is present
* Fixed libav backend's pkg-config dependency list (by @onodera-punpun)
* Enable image format support in the libav backend
pqiv 2.4
* Added --sort-key=mtime to sort by modification time instead of file name
* Delay the "Image is still loading" message for half a second to avoid
flickering status messages
* Remove the "Image is still loading" message if --hide-info-box is set
* Added [libav](https://www.ffmpeg.org/) backend for video support
* Added --end-of-files-action=action to allow users to control what happens
once all images have been viewed
* Fix various minor memory allocation issues / possible race conditions
pqiv 2.3.5
* Fix parameters in pqivrc that are handled by a callback
* Fix reference counting if an image fails to load
* Properly reload multi-page files if they change on disk while being viewed
* Properly handle if a user closes pqiv while the image loader is still active
pqiv 2.3
* Refactored an abstraction layer around the image backend
* Added optional support for PDF-files through
[poppler](http://poppler.freedesktop.org/)
* Added optional support for PS-files through
[libspectre](http://www.freedesktop.org/wiki/Software/libspectre/)
* Added optional support for more image formats through
[ImageMagick's MagickWand](http://www.imagemagick.org/script/magick-wand.php)
* Support for gtk+ 3.14
* configure/Makefile updated to support (Free-)BSD
* Added ctrl + space/backspace hotkey for jumping to the next/previous directory
* Improved pqiv's reaction if a file is removed
* gtk 3.16 deprecates `gdk_cursor_new`, replaced by a different function
* Shuffle mode is now toggleable at run-time (using Ctrl-R)
pqiv 2.2
* Accept URLs as command line arguments
* Revived -r for reading additional files from stdin (by J.P. Reed)
* Display the help message if invoked without parameters (by J.P. Reed)
* Accept floating point slideshow intervals on the command line
* Update the info box with the current numbers if (new) images are (un)loaded
* Added --max-depth=n to limit how deep directories are searched
* Added --browse to load, in addition to images from the command line, also
all other images from the containing directories
* Bugfix: Fixed handling of non-image command line arguments
pqiv 2.1
* Support for watching directories for new files
* Downstream Makefile fix: Included LDFLAGS (from Gentoo package, by Tim
Harder), updated for clean builds on OpenBSD (by jca[at]wxcvbn[dot]org,
reported by github user @clod89)
* Also included CPPFLAGS, for completeness
* Renamed '.qiv-select' directory to '.pqiv-select'
* Added a certain level of autoconf compatibility to the configure script, for
automated building
* gtk 3.10 stock icon deprecation issue fixed
* Reimplemented fading between images
* Display the last image while the current image has not been loaded
* Gave users the option to abort the loading of huge images
* Respect --shuffle and --sort with --watch-directories, i.e. insert keeping
order, not always at the end
* New option --lazy-load to display the main window while still traversing
paths, searching for images
* New option --low-memory to disable memory hungry features
* Detect nested symlinks without preventing users from loading the same image
multiple times
* Improved cross-compilation support with mingw64
pqiv 2.0
* Complete rewrite from scratch
* Based on GTK 3 and Cairo
pqiv ≤ 1.0
* See the old GTK 2 release for information on that
(in the **gtk2** branch on github)
pqiv ≤ 0.3
* See the old python release for information on that
(in the **python** branch on github)
pqiv-2.6/backends/ 0000775 0000000 0000000 00000000000 12737703411 0014117 5 ustar 00root root 0000000 0000000 pqiv-2.6/backends/archive_cbx.c 0000664 0000000 0000000 00000020516 12737703411 0016544 0 ustar 00root root 0000000 0000000 /**
* pqiv
*
* Copyright (c) 2013-2014, Phillip Berndt
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
*
* libarchive backend
*
* TODO:
* This currently forceably uses gdkpixbuf to draw the contents. Since the
* major format that this supports is cb*, which should only contain PNG/JPG.
* this isn't much of an issue, but it would be nice to dynamically use other
* backends. This currently does not work, at least not for backends that can
* handle multi-page documents, because they expect to access image data by
* either access to a memory region (which this backend can of course offer,
* but keeping all archives as a whole in memory is not really a good solution)
* or as something that can be accessed by an URI/filename. I'd have to
* refactor this to support arbitrary callbacks, or finally bring myself to
* use GIOs abstractions..
*
*/
#include "../pqiv.h"
#include "../lib/filebuffer.h"
#include
#include
#include
typedef struct {
// The archive object and raw archive data
gchar *entry_name;
// The surface where the image is stored.
cairo_surface_t *image_surface;
} file_private_data_archive_t;
static struct archive *file_type_archive_cbx_gen_archive(GBytes *data) {/*{{{*/
struct archive *archive = archive_read_new();
archive_read_support_format_zip(archive);
archive_read_support_format_rar(archive);
archive_read_support_format_7zip(archive);
archive_read_support_format_tar(archive);
archive_read_support_filter_all(archive);
gsize data_size;
char *data_ptr = (char *)g_bytes_get_data(data, &data_size);
if(archive_read_open_memory(archive, data_ptr, data_size) != ARCHIVE_OK) {
g_printerr("Failed to load archive: %s\n", archive_error_string(archive));
archive_read_free(archive);
return NULL;
}
return archive;
}/*}}}*/
BOSNode *file_type_archive_cbx_alloc(load_images_state_t state, file_t *file) {/*{{{*/
GError *error_pointer = NULL;
GBytes *data = buffered_file_as_bytes(file, NULL, &error_pointer);
if(!data) {
g_printerr("Failed to load archive %s: %s\n", file->display_name, error_pointer ? error_pointer->message : "Unknown error");
g_clear_error(&error_pointer);
file_free(file);
return NULL;
}
struct archive *archive = file_type_archive_cbx_gen_archive(data);
if(!archive) {
file_free(file);
return NULL;
}
BOSNode *first_node = NULL;
struct archive_entry *entry;
while(archive_read_next_header(archive, &entry) == ARCHIVE_OK) {
const gchar *entry_name = archive_entry_pathname(entry);
file_t *new_file = image_loader_duplicate_file(file, g_strdup_printf("%s#%s", file->display_name, entry_name), g_strdup_printf("%s#%s", file->sort_name, entry_name));
new_file->private = g_slice_new0(file_private_data_archive_t);
((file_private_data_archive_t *)new_file->private)->entry_name = g_strdup(entry_name);
if(!first_node) {
first_node = load_images_handle_parameter_add_file(state, new_file);
}
else {
load_images_handle_parameter_add_file(state, new_file);
}
//printf("%s %d\n", archive_entry_pathname(entry), archive_entry_size(entry));
archive_read_data_skip(archive);
}
archive_read_free(archive);
buffered_file_unref(file);
file_free(file);
return NULL;
}/*}}}*/
void file_type_archive_cbx_free(file_t *file) {/*{{{*/
if(file->private) {
g_slice_free(file_private_data_archive_t, file->private);
}
}/*}}}*/
void file_type_archive_cbx_unload(file_t *file) {/*{{{*/
file_private_data_archive_t *private = (file_private_data_archive_t *)file->private;
if(private->entry_name) {
g_free(private->entry_name);
private->entry_name = NULL;
}
if(private->image_surface != NULL) {
cairo_surface_destroy(private->image_surface);
private->image_surface = NULL;
}
}/*}}}*/
gboolean file_type_archive_cbx_load_destroy_old_image_callback(gpointer old_surface) {/*{{{*/
cairo_surface_destroy((cairo_surface_t *)old_surface);
return FALSE;
}/*}}}*/
void file_type_archive_cbx_load(file_t *file, GInputStream *data_stream, GError **error_pointer) {/*{{{*/
file_private_data_archive_t *private = (file_private_data_archive_t *)file->private;
// Open the archive
GBytes *data = buffered_file_as_bytes(file, data_stream, error_pointer);
if(!data) {
return;
}
struct archive *archive = file_type_archive_cbx_gen_archive(data);
if(!archive) {
buffered_file_unref(file);
*error_pointer = g_error_new(g_quark_from_static_string("pqiv-archive-error"), 1, "Failed to open archive file");
return;
}
// Find the proper entry
size_t entry_size = 0;
gchar *entry_data = NULL;
struct archive_entry *entry;
while(archive_read_next_header(archive, &entry) == ARCHIVE_OK) {
if(private->entry_name && strcmp(private->entry_name, archive_entry_pathname(entry)) == 0) {
entry_size = archive_entry_size(entry);
entry_data = g_malloc(entry_size);
if(archive_read_data(archive, entry_data, entry_size) != (ssize_t)entry_size) {
archive_read_free(archive);
buffered_file_unref(file);
*error_pointer = g_error_new(g_quark_from_static_string("pqiv-archive-error"), 1, "The file had an unexpected size");
return;
}
break;
}
}
archive_read_free(archive);
buffered_file_unref(file);
if(!entry_size) {
*error_pointer = g_error_new(g_quark_from_static_string("pqiv-archive-error"), 1, "The file has gone within the archive");
return;
}
// Load it as a GdkPixbuf (This could be extended to support animations)
GInputStream *entry_data_stream = g_memory_input_stream_new_from_data(entry_data, entry_size, g_free);
GdkPixbuf *pixbuf = gdk_pixbuf_new_from_stream(entry_data_stream, NULL, error_pointer);
if(!pixbuf) {
g_object_unref(entry_data_stream);
return;
}
g_object_unref(entry_data_stream);
GdkPixbuf *new_pixbuf = gdk_pixbuf_apply_embedded_orientation(pixbuf);
g_object_unref(pixbuf);
pixbuf = new_pixbuf;
file->width = gdk_pixbuf_get_width(pixbuf);
file->height = gdk_pixbuf_get_height(pixbuf);
// Draw to a cairo surface, see gfkpixbuf.c for why this can not use gdk_cairo_surface_create_from_pixbuf.
cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, file->width, file->height);
if(cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
g_object_unref(pixbuf);
*error_pointer = g_error_new(g_quark_from_static_string("pqiv-archive-error"), 1, "Failed to create a cairo image surface for the loaded image (cairo status %d)\n", cairo_surface_status(surface));
return;
}
cairo_t *sf_cr = cairo_create(surface);
gdk_cairo_set_source_pixbuf(sf_cr, pixbuf, 0, 0);
cairo_paint(sf_cr);
cairo_destroy(sf_cr);
cairo_surface_t *old_surface = private->image_surface;
private->image_surface = surface;
if(old_surface != NULL) {
g_idle_add(file_type_archive_cbx_load_destroy_old_image_callback, old_surface);
}
g_object_unref(pixbuf);
file->is_loaded = TRUE;
}/*}}}*/
void file_type_archive_cbx_draw(file_t *file, cairo_t *cr) {/*{{{*/
file_private_data_archive_t *private = (file_private_data_archive_t *)file->private;
cairo_surface_t *current_image_surface = private->image_surface;
cairo_set_source_surface(cr, current_image_surface, 0, 0);
cairo_paint(cr);
}/*}}}*/
void file_type_archive_cbx_initializer(file_type_handler_t *info) {/*{{{*/
// Fill the file filter pattern
info->file_types_handled = gtk_file_filter_new();
char pattern[] = { '*', '.', 'c', 'b', '_', '\0' };
char formats[] = { 'z', 'r', '7', 't', 'a', '\0' };
for(char *format=formats; *format; format++) {
pattern[4] = *format;
gtk_file_filter_add_pattern(info->file_types_handled, pattern);
}
// Assign the handlers
info->alloc_fn = file_type_archive_cbx_alloc;
info->free_fn = file_type_archive_cbx_free;
info->load_fn = file_type_archive_cbx_load;
info->unload_fn = file_type_archive_cbx_unload;
info->draw_fn = file_type_archive_cbx_draw;
}/*}}}*/
pqiv-2.6/backends/gdkpixbuf.c 0000664 0000000 0000000 00000020556 12737703411 0016256 0 ustar 00root root 0000000 0000000 /**
* pqiv
*
* Copyright (c) 2013-2014, Phillip Berndt
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
*
* gdk-pixbuf backend
*/
#include "../pqiv.h"
/* Default (GdkPixbuf) file type implementation {{{ */
typedef struct {
// The surface where the image is stored. Only non-NULL for
// the current, previous and next image.
cairo_surface_t *image_surface;
// For file_type & FILE_FLAGS_ANIMATION, this stores the
// whole animation. As with the surface, this is only non-NULL
// for the current, previous and next image.
GdkPixbufAnimation *pixbuf_animation;
GdkPixbufAnimationIter *animation_iter;
} file_private_data_gdkpixbuf_t;
BOSNode *file_type_gdkpixbuf_alloc(load_images_state_t state, file_t *file) {/*{{{*/
file->private = (void *)g_slice_new0(file_private_data_gdkpixbuf_t);
return load_images_handle_parameter_add_file(state, file);
}/*}}}*/
void file_type_gdkpixbuf_free(file_t *file) {/*{{{*/
g_slice_free(file_private_data_gdkpixbuf_t, file->private);
}/*}}}*/
void file_type_gdkpixbuf_unload(file_t *file) {/*{{{*/
file_private_data_gdkpixbuf_t *private = file->private;
if(private->pixbuf_animation != NULL) {
g_object_unref(private->pixbuf_animation);
private->pixbuf_animation = NULL;
}
if(private->image_surface != NULL) {
cairo_surface_destroy(private->image_surface);
private->image_surface = NULL;
}
if(private->animation_iter != NULL) {
g_object_unref(private->animation_iter);
private->animation_iter = NULL;
}
}/*}}}*/
double file_type_gdkpixbuf_animation_initialize(file_t *file) {/*{{{*/
file_private_data_gdkpixbuf_t *private = file->private;
if(private->animation_iter == NULL) {
private->animation_iter = gdk_pixbuf_animation_get_iter(private->pixbuf_animation, NULL);
}
return gdk_pixbuf_animation_iter_get_delay_time(private->animation_iter);
}/*}}}*/
double file_type_gdkpixbuf_animation_next_frame(file_t *file) {/*{{{*/
file_private_data_gdkpixbuf_t *private = (file_private_data_gdkpixbuf_t *)file->private;
cairo_surface_t *surface = cairo_surface_reference(private->image_surface);
gdk_pixbuf_animation_iter_advance(private->animation_iter, NULL);
GdkPixbuf *pixbuf = gdk_pixbuf_animation_iter_get_pixbuf(private->animation_iter);
cairo_t *sf_cr = cairo_create(surface);
cairo_save(sf_cr);
cairo_set_source_rgba(sf_cr, 0., 0., 0., 0.);
cairo_set_operator(sf_cr, CAIRO_OPERATOR_SOURCE);
cairo_paint(sf_cr);
cairo_restore(sf_cr);
gdk_cairo_set_source_pixbuf(sf_cr, pixbuf, 0, 0);
cairo_paint(sf_cr);
cairo_destroy(sf_cr);
cairo_surface_destroy(surface);
return gdk_pixbuf_animation_iter_get_delay_time(private->animation_iter);
}/*}}}*/
gboolean file_type_gdkpixbuf_load_destroy_old_image_callback(gpointer old_surface) {/*{{{*/
cairo_surface_destroy((cairo_surface_t *)old_surface);
return FALSE;
}/*}}}*/
void file_type_gdkpixbuf_load(file_t *file, GInputStream *data, GError **error_pointer) {/*{{{*/
file_private_data_gdkpixbuf_t *private = (file_private_data_gdkpixbuf_t *)file->private;
GdkPixbufAnimation *pixbuf_animation = NULL;
#if (GDK_PIXBUF_MAJOR > 2 || (GDK_PIXBUF_MAJOR == 2 && GDK_PIXBUF_MINOR >= 28))
pixbuf_animation = gdk_pixbuf_animation_new_from_stream(data, image_loader_cancellable, error_pointer);
#else
#define IMAGE_LOADER_BUFFER_SIZE (1024 * 512)
GdkPixbufLoader *loader = gdk_pixbuf_loader_new();
guchar *buffer = g_malloc(IMAGE_LOADER_BUFFER_SIZE);
while(TRUE) {
gssize bytes_read = g_input_stream_read(data, buffer, IMAGE_LOADER_BUFFER_SIZE, image_loader_cancellable, error_pointer);
if(bytes_read == 0) {
// All OK, finish the image loader
gdk_pixbuf_loader_close(loader, error_pointer);
pixbuf_animation = gdk_pixbuf_loader_get_animation(loader);
if(pixbuf_animation != NULL) {
g_object_ref(pixbuf_animation); // see above
}
break;
}
if(bytes_read == -1) {
// Error. Handle this below.
gdk_pixbuf_loader_close(loader, NULL);
break;
}
// In all other cases, write to image loader
if(!gdk_pixbuf_loader_write(loader, buffer, bytes_read, error_pointer)) {
// In case of an error, abort.
break;
}
}
g_free(buffer);
g_object_unref(loader);
#endif
if(pixbuf_animation == NULL) {
return;
}
if(!gdk_pixbuf_animation_is_static_image(pixbuf_animation)) {
if(private->pixbuf_animation != NULL) {
g_object_unref(private->pixbuf_animation);
}
private->pixbuf_animation = g_object_ref(pixbuf_animation);
file->file_flags |= FILE_FLAGS_ANIMATION;
}
else {
file->file_flags &= ~FILE_FLAGS_ANIMATION;
}
// We apparently do not own this pixbuf!
GdkPixbuf *pixbuf = gdk_pixbuf_animation_get_static_image(pixbuf_animation);
if(pixbuf != NULL) {
GdkPixbuf *new_pixbuf = gdk_pixbuf_apply_embedded_orientation(pixbuf);
pixbuf = new_pixbuf;
// This should never happen and is only here as a security measure
// (glib will abort() if malloc() fails and nothing else can happen here)
if(pixbuf == NULL) {
g_object_unref(pixbuf_animation);
return;
}
file->width = gdk_pixbuf_get_width(pixbuf);
file->height = gdk_pixbuf_get_height(pixbuf);
#if 0 && (GDK_MAJOR_VERSION == 3 && GDK_MINOR_VERSION >= 10) || (GDK_MAJOR_VERSION > 3)
// This function has a bug, see
// https://bugzilla.gnome.org/show_bug.cgi?id=736624
// We therefore have to use the below version even if this function is available.
cairo_surface_t *surface = gdk_cairo_surface_create_from_pixbuf(pixbuf, 1., NULL);
#else
cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, file->width, file->height);
if(cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
g_object_unref(pixbuf);
g_object_unref(pixbuf_animation);
*error_pointer = g_error_new(g_quark_from_static_string("pqiv-pixbuf-error"), 1, "Failed to create a cairo image surface for the loaded image (cairo status %d)\n", cairo_surface_status(surface));
return;
}
cairo_t *sf_cr = cairo_create(surface);
gdk_cairo_set_source_pixbuf(sf_cr, pixbuf, 0, 0);
cairo_paint(sf_cr);
cairo_destroy(sf_cr);
#endif
cairo_surface_t *old_surface = private->image_surface;
private->image_surface = surface;
if(old_surface != NULL) {
g_idle_add(file_type_gdkpixbuf_load_destroy_old_image_callback, old_surface);
}
g_object_unref(pixbuf);
file->is_loaded = TRUE;
}
g_object_unref(pixbuf_animation);
}/*}}}*/
void file_type_gdkpixbuf_draw(file_t *file, cairo_t *cr) {/*{{{*/
file_private_data_gdkpixbuf_t *private = (file_private_data_gdkpixbuf_t *)file->private;
cairo_surface_t *current_image_surface = private->image_surface;
cairo_set_source_surface(cr, current_image_surface, 0, 0);
cairo_paint(cr);
}/*}}}*/
void file_type_gdkpixbuf_initializer(file_type_handler_t *info) {/*{{{*/
// Fill the file filter pattern
info->file_types_handled = gtk_file_filter_new();
gtk_file_filter_add_pixbuf_formats(info->file_types_handled);
GSList *file_formats_iterator = gdk_pixbuf_get_formats();
do {
gchar **file_format_extensions_iterator = gdk_pixbuf_format_get_extensions(file_formats_iterator->data);
while(*file_format_extensions_iterator != NULL) {
gchar *extn = g_strdup_printf("*.%s", *file_format_extensions_iterator);
gtk_file_filter_add_pattern(info->file_types_handled, extn);
g_free(extn);
++file_format_extensions_iterator;
}
} while((file_formats_iterator = g_slist_next(file_formats_iterator)) != NULL);
g_slist_free(file_formats_iterator);
// Assign the handlers
info->alloc_fn = file_type_gdkpixbuf_alloc;
info->free_fn = file_type_gdkpixbuf_free;
info->load_fn = file_type_gdkpixbuf_load;
info->unload_fn = file_type_gdkpixbuf_unload;
info->animation_initialize_fn = file_type_gdkpixbuf_animation_initialize;
info->animation_next_frame_fn = file_type_gdkpixbuf_animation_next_frame;
info->draw_fn = file_type_gdkpixbuf_draw;
}/*}}}*/
/* }}} */
pqiv-2.6/backends/libav.c 0000664 0000000 0000000 00000024437 12737703411 0015372 0 ustar 00root root 0000000 0000000 /**
* pqiv
*
* Copyright (c) 2013-2015, Phillip Berndt
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
*
* libav backend
*/
/* This backend is based on the excellent short API example from
http://hasanaga.info/tag/ffmpeg-libavcodec-avformat_open_input-example/ */
#include "../pqiv.h"
#include
#include
#include
#include
#include
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1)
#define av_frame_alloc avcodec_alloc_frame
#define av_frame_free avcodec_free_frame
#endif
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 0, 0)
#define AV_COMPAT_USE_PICTURE
#define av_packet_unref av_free_packet
#endif
// This is a list of extensions that are never handled by this backend
// It is not a complete list of audio formats supported by ffmpeg,
// only those I recognized right away.
static const char * const ignore_extensions[] = {
"aac", "ac3", "aiff", "dts", "flac", "gsm", "m4a", "mp3", "ogg", "f64be", "f64le",
"f32be", "f32le", "s32be", "s32le", "s24be", "s24le", "s16be", "s16le", "s8",
"u32be", "u32le", "u24be", "u24le", "u16be", "u16le", "u8", "sox", "spdif", "txt",
"w64", "wav", "xa", "xwma", NULL
};
typedef struct {
AVFormatContext *avcontext;
AVCodecContext *cocontext;
int video_stream_id;
gboolean pkt_valid;
AVPacket pkt;
AVFrame *frame;
AVFrame *rgb_frame;
uint8_t *buffer;
} file_private_data_libav_t;
BOSNode *file_type_libav_alloc(load_images_state_t state, file_t *file) {/*{{{*/
file->private = g_slice_new0(file_private_data_libav_t);
return load_images_handle_parameter_add_file(state, file);
}/*}}}*/
void file_type_libav_free(file_t *file) {/*{{{*/
g_slice_free(file_private_data_libav_t, file->private);
}/*}}}*/
void file_type_libav_unload(file_t *file) {/*{{{*/
file_private_data_libav_t *private = (file_private_data_libav_t *)file->private;
if(private->pkt_valid) {
av_packet_unref(&(private->pkt));
private->pkt_valid = FALSE;
}
if(private->frame) {
av_frame_free(&(private->frame));
}
if(private->rgb_frame) {
av_frame_free(&(private->rgb_frame));
}
if(private->avcontext) {
avcodec_close(private->cocontext);
avformat_close_input(&(private->avcontext));
}
if(private->buffer) {
g_free(private->buffer);
private->buffer = NULL;
}
}/*}}}*/
void file_type_libav_load(file_t *file, GInputStream *data, GError **error_pointer) {/*{{{*/
file_private_data_libav_t *private = (file_private_data_libav_t *)file->private;
if(private->avcontext) {
// Double check if the file was properly freed. It is an error if it was not, the check is merely
// here because libav crashes if it was not.
assert(!private->avcontext);
file_type_libav_unload(file);
}
if(avformat_open_input(&(private->avcontext), file->file_name, NULL, NULL) < 0) {
*error_pointer = g_error_new(g_quark_from_static_string("pqiv-libav-error"), 1, "Failed to load image using libav.");
return;
}
if(avformat_find_stream_info(private->avcontext, NULL) < 0) {
avformat_close_input(&(private->avcontext));
*error_pointer = g_error_new(g_quark_from_static_string("pqiv-libav-error"), 1, "Failed to load image using libav.");
return;
}
private->video_stream_id = -1;
for(size_t i=0; iavcontext->nb_streams; i++) {
if(private->avcontext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
private->video_stream_id = i;
break;
}
}
if(private->video_stream_id < 0 || private->avcontext->streams[private->video_stream_id]->codec->width == 0) {
*error_pointer = g_error_new(g_quark_from_static_string("pqiv-libav-error"), 1, "This is not a video file.");
avformat_close_input(&(private->avcontext));
return;
}
private->cocontext = private->avcontext->streams[private->video_stream_id]->codec;
AVCodec *codec = avcodec_find_decoder(private->cocontext->codec_id);
if(!codec || avcodec_open2(private->cocontext, codec, NULL) < 0) {
*error_pointer = g_error_new(g_quark_from_static_string("pqiv-libav-error"), 1, "Failed to open codec.");
avformat_close_input(&(private->avcontext));
return;
}
private->frame = av_frame_alloc();
private->rgb_frame = av_frame_alloc();
#ifdef AV_COMPAT_USE_PICTURE
size_t num_bytes = avpicture_get_size(AV_PIX_FMT_RGB32, private->cocontext->width, private->cocontext->height);
#else
size_t num_bytes = av_image_get_buffer_size(AV_PIX_FMT_RGB32, private->cocontext->width, private->cocontext->height, 1);
#endif
private->buffer = (uint8_t *)g_malloc(num_bytes * sizeof(uint8_t));
file->file_flags |= FILE_FLAGS_ANIMATION;
file->width = private->cocontext->width;
file->height = private->cocontext->height;
file->is_loaded = TRUE;
}/*}}}*/
double file_type_libav_animation_next_frame(file_t *file) {/*{{{*/
file_private_data_libav_t *private = (file_private_data_libav_t *)file->private;
if(!private->avcontext) {
return -1;
}
AVPacket old_pkt = private->pkt;
do {
// Loop until the next video frame is found
memset(&(private->pkt), 0, sizeof(AVPacket));
if(av_read_frame(private->avcontext, &(private->pkt)) < 0) {
av_packet_unref(&(private->pkt));
if(avformat_seek_file(private->avcontext, -1, 0, 0, 1, 0) < 0 || av_read_frame(private->avcontext, &(private->pkt)) < 0) {
// Reading failed; end stream here to be on the safe side
// Display last frame to the user
private->pkt = old_pkt;
return -1;
}
}
} while(private->pkt.stream_index != private->video_stream_id);
if(private->pkt_valid) {
av_packet_unref(&old_pkt);
}
else {
private->pkt_valid = TRUE;
}
if(private->avcontext->streams[private->video_stream_id]->avg_frame_rate.den != 0 && private->avcontext->streams[private->video_stream_id]->avg_frame_rate.num != 0) {
// Stream has reliable average framerate
return 1000. * private->avcontext->streams[private->video_stream_id]->avg_frame_rate.den / private->avcontext->streams[private->video_stream_id]->avg_frame_rate.num;
}
else if(private->avcontext->streams[private->video_stream_id]->time_base.den != 0 && private->avcontext->streams[private->video_stream_id]->time_base.num != 0) {
// Stream has usable time base
return private->pkt.duration * private->avcontext->streams[private->video_stream_id]->time_base.num * 1000. / private->avcontext->streams[private->video_stream_id]->time_base.den;
}
// TODO What could be done here as a last fallback?! -> Figure this out from ffmpeg!
return 10;
}/*}}}*/
double file_type_libav_animation_initialize(file_t *file) {/*{{{*/
return file_type_libav_animation_next_frame(file);
}/*}}}*/
void file_type_libav_draw(file_t *file, cairo_t *cr) {/*{{{*/
file_private_data_libav_t *private = (file_private_data_libav_t *)file->private;
if(private->pkt_valid) {
AVFrame *frame = private->frame;
AVFrame *rgb_frame = private->rgb_frame;
int got_picture_ptr = 0;
// Decode a frame
if(avcodec_decode_video2(private->cocontext, frame, &got_picture_ptr, &(private->pkt)) >= 0 && got_picture_ptr) {
// Prepare buffer for RGB32 version
uint8_t *buffer = private->buffer;
#ifdef AV_COMPAT_USE_PICTURE
avpicture_fill((AVPicture *)rgb_frame, buffer, AV_PIX_FMT_RGB32, private->cocontext->width, private->cocontext->height);
#else
av_image_fill_arrays(rgb_frame->data, rgb_frame->linesize, buffer, AV_PIX_FMT_RGB32, private->cocontext->width, private->cocontext->height, 1);
#endif
// Convert to RGB32
struct SwsContext *img_convert_ctx = sws_getCachedContext(NULL, private->cocontext->width, private->cocontext->height, private->cocontext->pix_fmt, private->cocontext->width,
private->cocontext->height, AV_PIX_FMT_RGB32, SWS_BICUBIC, NULL, NULL, NULL);
sws_scale(img_convert_ctx, (const uint8_t * const*)frame->data, frame->linesize, 0, private->cocontext->height, rgb_frame->data, rgb_frame->linesize);
sws_freeContext(img_convert_ctx);
// Draw to a temporary image surface and then to cr
cairo_surface_t *image_surface = cairo_image_surface_create_for_data(rgb_frame->data[0], CAIRO_FORMAT_ARGB32, file->width, file->height, rgb_frame->linesize[0]);
cairo_set_source_surface(cr, image_surface, 0, 0);
cairo_paint(cr);
cairo_surface_destroy(image_surface);
}
}
}/*}}}*/
static gboolean _is_ignored_extension(const char *extension) {/*{{{*/
for(const char * const * ext = ignore_extensions; *ext; ext++) {
if(strcmp(*ext, extension) == 0) {
return TRUE;
}
}
return FALSE;
}/*}}}*/
void file_type_libav_initializer(file_type_handler_t *info) {/*{{{*/
avcodec_register_all();
av_register_all();
avformat_network_init();
// Register all file formats supported by libavformat
info->file_types_handled = gtk_file_filter_new();
for(AVInputFormat *iter = av_iformat_next(NULL); iter; iter = av_iformat_next(iter)) {
if(iter->name) {
gchar **fmts = g_strsplit(iter->name, ",", -1);
for(gchar **fmt = fmts; *fmt; fmt++) {
if(_is_ignored_extension(*fmt)) {
continue;
}
gchar *format = g_strdup_printf("*.%s", *fmt);
gtk_file_filter_add_pattern(info->file_types_handled, format);
g_free(format);
}
g_strfreev(fmts);
}
if(iter->extensions) {
gchar **fmts = g_strsplit(iter->extensions, ",", -1);
for(gchar **fmt = fmts; *fmt; fmt++) {
if(_is_ignored_extension(*fmt)) {
continue;
}
gchar *format = g_strdup_printf("*.%s", *fmt);
gtk_file_filter_add_pattern(info->file_types_handled, format);
g_free(format);
}
g_strfreev(fmts);
}
}
// Assign the handlers
info->alloc_fn = file_type_libav_alloc;
info->free_fn = file_type_libav_free;
info->load_fn = file_type_libav_load;
info->unload_fn = file_type_libav_unload;
info->animation_initialize_fn = file_type_libav_animation_initialize;
info->animation_next_frame_fn = file_type_libav_animation_next_frame;
info->draw_fn = file_type_libav_draw;
}/*}}}*/
pqiv-2.6/backends/poppler.c 0000664 0000000 0000000 00000013263 12737703411 0015751 0 ustar 00root root 0000000 0000000 /**
* pqiv
*
* Copyright (c) 2013-2014, Phillip Berndt
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
*
* libpoppler backend (PDF support)
*/
#include "../pqiv.h"
#include "../lib/filebuffer.h"
#include
typedef struct {
// The page to be displayed
PopplerDocument *document;
PopplerPage *page;
// The page number, for loading
guint page_number;
} file_private_data_poppler_t;
BOSNode *file_type_poppler_alloc(load_images_state_t state, file_t *file) {/*{{{*/
// We have to load the file now to get the number of pages
GError *error_pointer = NULL;
#if POPPLER_CHECK_VERSION(0, 26, 5)
// The stream loading problem (bug #82630 upstream) was fixed upstream in
// http://cgit.freedesktop.org/poppler/poppler/commit/?h=poppler-0.26&id=f94ba85a736b4c90c05e7782939f32506472658e
// and the fix will appear in 0.26.5
//
GInputStream *data = image_loader_stream_file(file, NULL);
if(!data) {
g_printerr("Failed to load PDF %s: Error while reading file\n", file->display_name);
file_free(file);
return NULL;
}
PopplerDocument *poppler_document = poppler_document_new_from_stream(data, -1, NULL, NULL, &error_pointer);
#else
GBytes *data_bytes = buffered_file_as_bytes(file, NULL, &error_pointer);
if(!data_bytes) {
g_printerr("Failed to load PDF %s: %s\n", file->display_name, error_pointer->message);
g_clear_error(&error_pointer);
file_free(file);
return NULL;
}
gsize data_size;
char *data_ptr = (char *)g_bytes_get_data(data_bytes, &data_size);
PopplerDocument *poppler_document = poppler_document_new_from_data(data_ptr, (int)data_size, NULL, &error_pointer);
#endif
BOSNode *first_node = NULL;
if(poppler_document) {
int n_pages = poppler_document_get_n_pages(poppler_document);
g_object_unref(poppler_document);
for(int n=0; ndisplay_name, n + 1),
g_strdup_printf("%s[%d]", file->sort_name, n + 1));
new_file->private = g_slice_new0(file_private_data_poppler_t);
((file_private_data_poppler_t *)new_file->private)->page_number = n;
if(n == 0) {
first_node = load_images_handle_parameter_add_file(state, new_file);
}
else {
load_images_handle_parameter_add_file(state, new_file);
}
}
}
else if(error_pointer) {
g_printerr("Failed to load PDF %s: %s\n", file->display_name, error_pointer->message);
g_clear_error(&error_pointer);
}
#if POPPLER_CHECK_VERSION(0, 26, 5)
g_object_unref(data);
#else
buffered_file_unref(file);
#endif
file_free(file);
return first_node;
}/*}}}*/
void file_type_poppler_free(file_t *file) {/*{{{*/
g_slice_free(file_private_data_poppler_t, file->private);
}/*}}}*/
void file_type_poppler_load(file_t *file, GInputStream *data, GError **error_pointer) {/*{{{*/
file_private_data_poppler_t *private = file->private;
// We need to load the data into memory, because poppler has problems with serving from streams; see above
#if POPPLER_CHECK_VERSION(0, 26, 5)
PopplerDocument *document = poppler_document_new_from_stream(data, -1, NULL, image_loader_cancellable, error_pointer);
#else
GBytes *data_bytes = buffered_file_as_bytes(file, data, error_pointer);
if(!data_bytes) {
return;
}
gsize data_size;
char *data_ptr = (char *)g_bytes_get_data(data_bytes, &data_size);
PopplerDocument *document = poppler_document_new_from_data(data_ptr, (int)data_size, NULL, error_pointer);
#endif
if(document) {
PopplerPage *page = poppler_document_get_page(document, private->page_number);
if(page) {
double width, height;
poppler_page_get_size(page, &width, &height);
file->width = width;
file->height = height;
file->is_loaded = TRUE;
private->page = page;
private->document = document;
return;
}
g_object_unref(document);
}
#if !POPPLER_CHECK_VERSION(0, 26, 5)
buffered_file_unref(file);
#endif
}/*}}}*/
void file_type_poppler_unload(file_t *file) {/*{{{*/
file_private_data_poppler_t *private = file->private;
if(private->page) {
g_object_unref(private->page);
private->page = NULL;
}
if(private->document) {
g_object_unref(private->document);
private->document = NULL;
#if !POPPLER_CHECK_VERSION(0, 26, 5)
buffered_file_unref(file);
#endif
}
}/*}}}*/
void file_type_poppler_draw(file_t *file, cairo_t *cr) {/*{{{*/
file_private_data_poppler_t *private = (file_private_data_poppler_t *)file->private;
cairo_set_source_rgb(cr, 1., 1., 1.);
cairo_paint(cr);
poppler_page_render(private->page, cr);
}/*}}}*/
void file_type_poppler_initializer(file_type_handler_t *info) {/*{{{*/
// Fill the file filter pattern
info->file_types_handled = gtk_file_filter_new();
gtk_file_filter_add_pattern(info->file_types_handled, "*.pdf");
gtk_file_filter_add_mime_type(info->file_types_handled, "application/pdf");
// Assign the handlers
info->alloc_fn = file_type_poppler_alloc;
info->free_fn = file_type_poppler_free;
info->load_fn = file_type_poppler_load;
info->unload_fn = file_type_poppler_unload;
info->draw_fn = file_type_poppler_draw;
}/*}}}*/
pqiv-2.6/backends/shared-initializer.c 0000664 0000000 0000000 00000001640 12737703411 0020053 0 ustar 00root root 0000000 0000000 #include
#include "../pqiv.h"
static const char *available_backends[] = {
SHARED_BACKENDS
NULL
};
file_type_handler_t file_type_handlers[sizeof(available_backends) / sizeof(char *)];
void initialize_file_type_handlers() {
int i = 0;
for(char **backend=(char **)&available_backends[0]; *backend; backend++) {
gchar *backend_candidate = g_strdup_printf("pqiv-backend-%s.so", *backend);
GModule *backend_module = g_module_open(backend_candidate, G_MODULE_BIND_LOCAL);
if(backend_module) {
gchar *backend_initializer = g_strdup_printf("file_type_%s_initializer", *backend);
file_type_initializer_fn_t initializer;
if(g_module_symbol(backend_module, backend_initializer, (gpointer *)&initializer)) {
initializer(&file_type_handlers[i++]);
g_module_make_resident(backend_module);
}
g_free(backend_initializer);
g_module_close(backend_module);
}
g_free(backend_candidate);
}
}
pqiv-2.6/backends/spectre.c 0000664 0000000 0000000 00000016120 12737703411 0015730 0 ustar 00root root 0000000 0000000 /**
* pqiv
*
* Copyright (c) 2013-2014, Phillip Berndt
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
*
* libspectre backend (PS support)
*/
#include "../pqiv.h"
#include "../lib/filebuffer.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
typedef struct {
int page_number;
struct SpectreDocument *document;
struct SpectrePage *page;
} file_private_data_spectre_t;
void cmsPluginTHR(void *context, void *plugin) {
// This symbol is required to prevent gs from registering its own memory handler,
// causing a crash if poppler is also used
//
// See http://lists.freedesktop.org/archives/poppler/2014-January/010779.html
//
// Plugin is a structure with a member uint32_t type with offsetof(type) == 4*2,
// which has value 0x6D656D48 == "memH". To verify that no other plugins interfere,
// we check that.
//
if(*((uint32_t*)plugin + 2) != 0x6D656D48) {
g_printerr("Warning: cmsPluginTHR call was redirected because of a poppler/gs interaction bug, but was called in an unexpected manner.\n");
}
}
BOSNode *file_type_spectre_alloc(load_images_state_t state, file_t *file) {/*{{{*/
BOSNode *first_node = NULL;
GError *error_pointer = NULL;
// Load the document to get the number of pages
struct SpectreDocument *document = spectre_document_new();
char *file_name = buffered_file_as_local_file(file, NULL, &error_pointer);
if(!file_name) {
g_printerr("Failed to load PS file %s: %s\n", file->file_name, error_pointer->message);
g_clear_error(&error_pointer);
return NULL;
}
spectre_document_load(document, file_name);
if(spectre_document_status(document)) {
g_printerr("Failed to load image %s: %s\n", file->file_name, spectre_status_to_string(spectre_document_status(document)));
spectre_document_free(document);
buffered_file_unref(file);
file_free(file);
return NULL;
}
int n_pages = spectre_document_get_n_pages(document);
spectre_document_free(document);
buffered_file_unref(file);
for(int n=0; ndisplay_name, n + 1),
g_strdup_printf("%s[%d]", file->sort_name, n + 1));
new_file->private = g_slice_new0(file_private_data_spectre_t);
((file_private_data_spectre_t *)new_file->private)->page_number = n;
if(n == 0) {
first_node = load_images_handle_parameter_add_file(state, new_file);
}
else {
load_images_handle_parameter_add_file(state, new_file);
}
}
file_free(file);
return first_node;
}/*}}}*/
void file_type_spectre_free(file_t *file) {/*{{{*/
g_slice_free(file_private_data_spectre_t, file->private);
}/*}}}*/
void file_type_spectre_load(file_t *file, GInputStream *data, GError **error_pointer) {/*{{{*/
file_private_data_spectre_t *private = file->private;
gchar *file_name = buffered_file_as_local_file(file, data, error_pointer);
if(!file_name) {
return;
}
struct SpectreDocument *document = spectre_document_new();
spectre_document_load(document, file_name);
if(spectre_document_status(document)) {
*error_pointer = g_error_new(g_quark_from_static_string("pqiv-spectre-error"), 1, "Failed to load image %s: %s\n", file->file_name, spectre_status_to_string(spectre_document_status(private->document)));
buffered_file_unref(file);
return;
}
struct SpectrePage *page = spectre_document_get_page(document, private->page_number);
if(!page) {
*error_pointer = g_error_new(g_quark_from_static_string("pqiv-spectre-error"), 1, "Failed to load image %s: Failed to load page %d\n", file->file_name, private->page_number);
spectre_document_free(document);
buffered_file_unref(file);
return;
}
if(spectre_page_status(page)) {
*error_pointer = g_error_new(g_quark_from_static_string("pqiv-spectre-error"), 1, "Failed to load image %s / page %d: %s\n", file->file_name, private->page_number, spectre_status_to_string(spectre_page_status(private->page)));
spectre_page_free(page);
spectre_document_free(document);
buffered_file_unref(file);
return;
}
int width, height;
spectre_page_get_size(page, &width, &height);
file->width = width;
file->height = height;
private->page = page;
private->document = document;
file->is_loaded = TRUE;
}/*}}}*/
void file_type_spectre_unload(file_t *file) {/*{{{*/
file_private_data_spectre_t *private = file->private;
if(private->page) {
spectre_page_free(private->page);
private->page = NULL;
}
if(private->document) {
spectre_document_free(private->document);
private->document = NULL;
buffered_file_unref(file);
}
}/*}}}*/
void file_type_spectre_draw(file_t *file, cairo_t *cr) {/*{{{*/
file_private_data_spectre_t *private = (file_private_data_spectre_t *)file->private;
SpectreRenderContext *render_context = spectre_render_context_new();
spectre_render_context_set_scale(render_context, current_scale_level, current_scale_level);
unsigned char *page_data = NULL;
int row_length;
spectre_page_render(private->page, render_context, &page_data, &row_length);
spectre_render_context_free(render_context);
if(spectre_page_status(private->page)) {
g_printerr("Failed to draw image: %s\n", spectre_status_to_string(spectre_page_status(private->page)));
if(page_data) {
g_free(page_data);
}
return;
}
if(page_data == NULL) {
g_printerr("Failed to draw image: Unknown error\n");
return;
}
cairo_surface_t *image_surface = cairo_image_surface_create_for_data(page_data, CAIRO_FORMAT_RGB24, file->width * current_scale_level, file->height * current_scale_level, row_length);
cairo_scale(cr, 1 / current_scale_level, 1 / current_scale_level);
cairo_set_source_surface(cr, image_surface, 0, 0);
cairo_paint(cr);
cairo_surface_destroy(image_surface);
g_free(page_data);
}/*}}}*/
void file_type_spectre_initializer(file_type_handler_t *info) {/*{{{*/
// Fill the file filter pattern
info->file_types_handled = gtk_file_filter_new();
gtk_file_filter_add_pattern(info->file_types_handled, "*.ps");
gtk_file_filter_add_pattern(info->file_types_handled, "*.eps");
gtk_file_filter_add_mime_type(info->file_types_handled, "application/postscript");
gtk_file_filter_add_mime_type(info->file_types_handled, "image/x-eps");
// Assign the handlers
info->alloc_fn = file_type_spectre_alloc;
info->free_fn = file_type_spectre_free;
info->load_fn = file_type_spectre_load;
info->unload_fn = file_type_spectre_unload;
info->draw_fn = file_type_spectre_draw;
}/*}}}*/
pqiv-2.6/backends/wand.c 0000664 0000000 0000000 00000026662 12737703411 0015230 0 ustar 00root root 0000000 0000000 /**
* pqiv
*
* Copyright (c) 2013-2014, Phillip Berndt
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
*
* ImageMagick wand backend
*
*/
#include "../pqiv.h"
#include "../lib/filebuffer.h"
#include
#include
#include
#include
#include
// ImageMagick's multithreading is broken. To test this, open a multi-page
// postscript document using this backend without --low-memory and then quit
// pqiv. The backend will freeze in MagickWandTerminus() while waiting for
// a Mutex. We must do this call to allow ImageMagick to delete temporary
// files created using postscript processing (in /tmp usually).
//
// The only way around this, sadly, is to use a global mutex around all
// ImageMagick calls.
G_LOCK_DEFINE_STATIC(magick_wand_global_lock);
typedef struct {
MagickWand *wand;
cairo_surface_t *rendered_image_surface;
// Starting from 1 for numbered files, 0 for unpaginated files
unsigned int page_number;
} file_private_data_wand_t;
// Check if a (named) file has a certain extension. Used for psd fix and multi-page detection (ps, pdf, ..)
static gboolean file_type_wand_has_extension(file_t *file, const char *extension) {
char *actual_extension;
return (!(file->file_flags & FILE_FLAGS_MEMORY_IMAGE) && file->file_name && (actual_extension = strrchr(file->file_name, '.')) && strcasecmp(actual_extension, extension) == 0);
}
// Functions to render the Magick backend to a cairo surface via in-memory PNG export
cairo_status_t file_type_wand_read_data(void *closure, unsigned char *data, unsigned int length) {/*{{{*/
unsigned char **pos = closure;
memcpy(data, *pos, length);
*pos += length;
return CAIRO_STATUS_SUCCESS;
}/*}}}*/
void file_type_wand_update_image_surface(file_t *file) {/*{{{*/
file_private_data_wand_t *private = file->private;
if(private->rendered_image_surface) {
cairo_surface_destroy(private->rendered_image_surface);
private->rendered_image_surface = NULL;
}
MagickSetImageFormat(private->wand, "PNG32");
size_t image_size;
unsigned char *image_data = MagickGetImageBlob(private->wand, &image_size);
unsigned char *image_data_loc = image_data;
private->rendered_image_surface = cairo_image_surface_create_from_png_stream(file_type_wand_read_data, &image_data_loc);
MagickRelinquishMemory(image_data);
}/*}}}*/
BOSNode *file_type_wand_alloc(load_images_state_t state, file_t *file) {/*{{{*/
G_LOCK(magick_wand_global_lock);
if(file_type_wand_has_extension(file, ".pdf") || file_type_wand_has_extension(file, ".ps")) {
// Multi-page document. Load number of pages and create one file_t per page
GError *error_pointer = NULL;
MagickWand *wand = NewMagickWand();
GBytes *image_bytes = buffered_file_as_bytes(file, NULL, &error_pointer);
if(!image_bytes) {
g_printerr("Failed to read image %s: %s\n", file->file_name, error_pointer->message);
g_clear_error(&error_pointer);
G_UNLOCK(magick_wand_global_lock);
return NULL;
}
size_t image_size;
const gchar *image_data = g_bytes_get_data(image_bytes, &image_size);
MagickBooleanType success = MagickReadImageBlob(wand, image_data, image_size);
if(success == MagickFalse) {
ExceptionType severity;
char *message = MagickGetException(wand, &severity);
g_printerr("Failed to read image %s: %s\n", file->file_name, message);
MagickRelinquishMemory(message);
DestroyMagickWand(wand);
buffered_file_unref(file);
G_UNLOCK(magick_wand_global_lock);
return NULL;
}
int n_pages = MagickGetNumberImages(wand);
DestroyMagickWand(wand);
buffered_file_unref(file);
BOSNode *first_node = NULL;
for(int n=0; ndisplay_name, n + 1),
g_strdup_printf("%s[%d]", file->sort_name, n + 1));
new_file->private = g_slice_new0(file_private_data_wand_t);
((file_private_data_wand_t *)new_file->private)->page_number = n + 1;
if(n == 0) {
first_node = load_images_handle_parameter_add_file(state, new_file);
}
else {
load_images_handle_parameter_add_file(state, new_file);
}
}
file_free(file);
G_UNLOCK(magick_wand_global_lock);
return first_node;
}
else {
// Simple image
file->private = g_slice_new0(file_private_data_wand_t);
BOSNode *first_node = load_images_handle_parameter_add_file(state, file);
G_UNLOCK(magick_wand_global_lock);
return first_node;
}
}/*}}}*/
void file_type_wand_free(file_t *file) {/*{{{*/
g_slice_free(file_private_data_wand_t, file->private);
}/*}}}*/
void file_type_wand_load(file_t *file, GInputStream *data, GError **error_pointer) {/*{{{*/
G_LOCK(magick_wand_global_lock);
file_private_data_wand_t *private = file->private;
private->wand = NewMagickWand();
gsize image_size;
GBytes *image_bytes = buffered_file_as_bytes(file, data, error_pointer);
if(!image_bytes) {
G_UNLOCK(magick_wand_global_lock);
return;
}
const gchar *image_data = g_bytes_get_data(image_bytes, &image_size);
MagickBooleanType success = MagickReadImageBlob(private->wand, image_data, image_size);
if(success == MagickFalse) {
ExceptionType severity;
char *message = MagickGetException(private->wand, &severity);
*error_pointer = g_error_new(g_quark_from_static_string("pqiv-wand-error"), 1, "Failed to load image %s: %s", file->file_name, message);
MagickRelinquishMemory(message);
DestroyMagickWand(private->wand);
private->wand = NULL;
buffered_file_unref(file);
G_UNLOCK(magick_wand_global_lock);
return;
}
MagickResetIterator(private->wand);
if(private->page_number > 0) {
// PDF/PS files are displayed one page per file_t
MagickSetIteratorIndex(private->wand, private->page_number - 1);
}
else {
// Other files are either interpreted as animated (if they have a delay
// set) or merged down to one image (interpreted as layered, as in
// PSD/XCF files)
size_t delay = MagickGetImageDelay(private->wand);
if(delay) {
MagickWand *wand = MagickCoalesceImages(private->wand);
DestroyMagickWand(private->wand);
private->wand = wand;
MagickResetIterator(wand);
file->file_flags |= FILE_FLAGS_ANIMATION;
}
else if(MagickGetNumberImages(private->wand) > 1) {
// Merge multi-page files.
// This doesn't work as expected for .psd files. As a hack, disable
// it for them.
// TODO Check periodically if the problem still persists (heavily distorted images) and remove this once it has been solved
if(!file_type_wand_has_extension(file, ".psd")) {
MagickWand *wand = MagickMergeImageLayers(private->wand, FlattenLayer);
DestroyMagickWand(private->wand);
private->wand = wand;
MagickResetIterator(private->wand);
}
}
MagickNextImage(private->wand);
}
file_type_wand_update_image_surface(file);
file->width = MagickGetImageWidth(private->wand);
file->height = MagickGetImageHeight(private->wand);
file->is_loaded = TRUE;
G_UNLOCK(magick_wand_global_lock);
}/*}}}*/
double file_type_wand_animation_initialize(file_t *file) {/*{{{*/
file_private_data_wand_t *private = file->private;
// The unit of MagickGetImageDelay is "ticks-per-second"
return 1000. / MagickGetImageDelay(private->wand);
}/*}}}*/
double file_type_wand_animation_next_frame(file_t *file) {/*{{{*/
// ImageMagick tends to be really slow when it comes to loading frames.
// We therefore measure the required time and subtract it from the time
// pqiv waits before loading the next frame:
G_LOCK(magick_wand_global_lock);
gint64 begin_time = g_get_monotonic_time();
file_private_data_wand_t *private = file->private;
MagickBooleanType status = MagickNextImage(private->wand);
if(status == MagickFalse) {
MagickResetIterator(private->wand);
MagickNextImage(private->wand);
}
file_type_wand_update_image_surface(file);
gint64 required_time = (g_get_monotonic_time() - begin_time) / 1000;
gint pause = 1000. / MagickGetImageDelay(private->wand);
G_UNLOCK(magick_wand_global_lock);
return pause + 1 > required_time ? pause - required_time : 1;
}/*}}}*/
void file_type_wand_unload(file_t *file) {/*{{{*/
G_LOCK(magick_wand_global_lock);
file_private_data_wand_t *private = file->private;
if(private->rendered_image_surface) {
cairo_surface_destroy(private->rendered_image_surface);
private->rendered_image_surface = NULL;
}
if(private->wand) {
DestroyMagickWand(private->wand);
private->wand = NULL;
buffered_file_unref(file);
}
G_UNLOCK(magick_wand_global_lock);
}/*}}}*/
void file_type_wand_draw(file_t *file, cairo_t *cr) {/*{{{*/
file_private_data_wand_t *private = file->private;
if(private->rendered_image_surface) {
if(private->page_number > 0) {
// Is multi-page document. Draw white background.
cairo_set_source_rgb(cr, 1., 1., 1.);
cairo_paint(cr);
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
}
cairo_set_source_surface(cr, private->rendered_image_surface, 0, 0);
cairo_paint(cr);
}
}/*}}}*/
static void file_type_wand_exit_handler() {/*{{{*/
G_LOCK(magick_wand_global_lock);
MagickWandTerminus();
G_UNLOCK(magick_wand_global_lock);
}/*}}}*/
void file_type_wand_initializer(file_type_handler_t *info) {/*{{{*/
// Fill the file filter pattern
MagickWandGenesis();
info->file_types_handled = gtk_file_filter_new();
size_t count, i;
char **formats = MagickQueryFormats("*", &count);
for(i=0; ifile_types_handled, format);
g_free(format);
}
MagickRelinquishMemory(formats);
// We need to register MagickWandTerminus(), imageMagick's exit handler, to
// cleanup temporary files when pqiv exits.
atexit(file_type_wand_exit_handler);
// Magick Wand does not give us MIME types. Manually add the most interesting one:
gtk_file_filter_add_mime_type(info->file_types_handled, "image/vnd.adobe.photoshop");
// Assign the handlers
info->alloc_fn = file_type_wand_alloc;
info->free_fn = file_type_wand_free;
info->load_fn = file_type_wand_load;
info->unload_fn = file_type_wand_unload;
info->draw_fn = file_type_wand_draw;
info->animation_initialize_fn = file_type_wand_animation_initialize;
info->animation_next_frame_fn = file_type_wand_animation_next_frame;
}/*}}}*/
pqiv-2.6/configure 0000775 0000000 0000000 00000023240 12737703411 0014255 0 ustar 00root root 0000000 0000000 #!/usr/bin/env bash
#
# Configure script, for most users only needed for backwards compatibility with
# pqiv 1.0 (pre-gtk3). I still recommend to use it in automated builds to
# ensure continued compatibility.
#
tempdir() {
NAME=tmp_${RANDOM}
while [ -d $NAME ]; do
NAME=tmp_${RANDOM}
done
mkdir ${NAME}
echo ${NAME}
}
PREFIX=/usr
DESTDIR=
GTK_VERSION=0
CROSS=
BINARY_EXTENSION=
MANDIR=
PKG_CONFIG=pkg-config
BACKENDS=
ENFORCED_BACKENDS=
DISABLED_BACKENDS=
EXTRA_DEFS=
BACKENDS_BUILD=static
DEBUG_DEFS=
# For development, you can set default settings here
[ -x ./configure-dev ] && . ./configure-dev
# Help and options
help() {
cat >&2 < Alternative syntax for backend selection. Non-specified backends are
autodetermined.
--backends-build=.. Either \`shared' to compile the backends as shared libraries,
or \`static' to compile them into pqiv. \`shared' is only of use if you
plan to package pqiv and want to get rid of the run-time dependencies,
so this defaults to \`static'.
options to remove features from pqiv:
EOF
sed -n -re 's!#ifn?def (\S+)\s*/\* option (\S+): (.+) ?\*/!\1 \2 \3!p' pqiv.c | while read DEFNAME OPTFLAG DESCRIPTION; do
if [ ${#DESCRIPTION} -gt 50 ]; then
printf " %-22s\n %-22s%s\n" $OPTFLAG "" "$DESCRIPTION" >&2
else
printf " %-22s%s\n" $OPTFLAG "$DESCRIPTION" >&2
fi
done
echo >&2
}
while [ $# -gt 0 ]; do
PARAMETER=${1%=*}
VALUE=${1#*=}
case $PARAMETER in
--prefix)
PREFIX=$VALUE
;;
--destdir)
DESTDIR=$VALUE
;;
--gtk-version)
GTK_VERSION=$VALUE
;;
-h)
help
exit 0
;;
--help)
help
exit 0
;;
--cross)
CROSS=$VALUE
if [ "${CROSS: -1}" != "-" ]; then
CROSS="$CROSS-"
fi
;;
--backends-build)
BACKENDS_BUILD=$VALUE
if [ "$BACKENDS_BUILD" != "static" -a "$BACKENDS_BUILD" != "shared" ]; then
echo "Invalid argument to --backends-build: Value must be either \`static' or \`shared'." >&2
exit 1
fi
;;
# Undocumented options for autoconf (esp. dh_auto_configure) compatibility
--build)
CROSS=${VALUE}-
;;
--mandir)
MANDIR=${VALUE}
MANDIR="${MANDIR//\{/(}"
MANDIR="${MANDIR//\}/)}"
MANDIR="${MANDIR//prefix/PREFIX}"
echo "Use of autoconf option --mandir is discouraged, because support is incomplete. Rewrote \`$VALUE' to \`$MANDIR' and used that as the MANDIR Make variable." >&2
;;
--includedir | --infodir | --sysconfdir | --localstatedir | --libdir | --libexecdir | --disable-maintainer-mode | --disable-dependency-tracking)
echo "autoconf option ${PARAMETER} ignored" >&2
;;
--no-sorting | --no-compositing | --no-fading | --no-commands | --no-config-file | --no-inotify | --no-animations | --binary-name)
echo "obsolete 1.0 option ${PARAMETER} ignored" >&2
;;
--backends)
BACKENDS="${VALUE//,/ }"
for NAME in ${BACKENDS}; do
[ -e backends/${NAME}.c ] && continue
echo "Invalid argument to --backends: Backend ${NAME} was not found" >&2
exit 1
done
;;
*)
DEF=$(
sed -n -re 's!#ifn?def (\S+)\s*/\* option (\S+): (.+) ?\*/!\1 \2 \3!p' pqiv.c | while read DEFNAME OPTFLAG DESCRIPTION; do
if [ "${PARAMETER}" = "${OPTFLAG}" ]; then
echo "-D${DEFNAME}"
fi
done
)
if [ -n "${DEF}" ]; then
EXTRA_DEFS="${EXTRA_DEFS} ${DEF}"
else
if [ "${PARAMETER#--without-}" != "${PARAMETER}" ]; then
DISABLED_BACKENDS="${PARAMETER#--without-} ${DISABLED_BACKENDS}"
elif [ "${PARAMETER#--with-}" != "${PARAMETER}" ]; then
NAME="${PARAMETER#--with-}"
if ! [ -e backends/${NAME}.c ]; then
echo "Unknown option: $1" >&2
exit 1
fi
ENFORCED_BACKENDS="${NAME} ${ENFORCED_BACKENDS}"
else
echo "Unknown option: $1" >&2
help
exit 1
fi
fi
esac
shift
done
# The makefile is for GNU make
if [ -z $MAKE ]; then
MAKE=make
if ! (${MAKE} -v 2>&1 | grep -q "GNU Make"); then
MAKE=gmake
if ! which $MAKE 2>&1 >/dev/null; then
echo "GNU make is required for building pqiv" >&2
exit 1
fi
fi
fi
# If cross-compiling, check if cc is present (usually it is not)
if [ -n "$CROSS" -a -z "$CC" ]; then
echo -n "Checking for cross-compiler cc.. "
if ! which ${CROSS}cc >/dev/null 2>&1; then
if which ${CROSS}clang >/dev/null 2>&1; then
export CC=clang
elif which ${CROSS}gcc >/dev/null 2>&1; then
export CC=gcc
else
echo
echo
echo "No compiler found. Please set the appropriate CC environment variable." >&2
exit 1
fi
echo "using ${CROSS}${CC}"
else
echo "ok"
fi
fi
# Determine binary extension (for Windows)
echo -n "Determining executable extension.. "
DIR=`tempdir`
cd $DIR
echo 'int main(int argc, char *argv[]) { return 0; }' > test.c
${CROSS}${CC:-cc} ${CFLAGS} test.c ${LDFLAGS}
RV=$?
rm -f test.c
EXECUTABLE=`ls`
rm -f $EXECUTABLE
cd ..
rmdir $DIR
if [ "$RV" != 0 ]; then
echo
echo
echo "The compiler can't compile executables!?" >&2
exit 1
fi
EXECUTABLE_EXTENSION=${EXECUTABLE#a}
if [ "$EXECUTABLE_EXTENSION" = ".out" ]; then
EXECUTABLE_EXTENSION=
fi
echo ${EXECUTABLE_EXTENSION:-(none)}
# Do a rudimental prerequisites check to have user-friendlier error messages
if [ -n "${CROSS}" ]; then
echo -n "Checking for pkg-config.. "
PKG_CONFIG=${CROSS}pkg-config
if ! which ${PKG_CONFIG} >/dev/null 2>&1; then
echo
echo
echo "Did not find a specialized tool ${CROSS}pkg-config, defaulting to pkg-config" >&2
echo "If you really ARE cross-compiling, the build might therefore fail!" >&2
echo
PKG_CONFIG=pkg-config
else
echo "${PKG_CONFIG}"
fi
fi
# Auto-determine available backends
if [ -z "$BACKENDS" ]; then
echo -n "Checking for supported backends.. "
BACKENDS="`$MAKE get_available_backends PKG_CONFIG=$PKG_CONFIG | sed -nre 's#^BACKENDS: ?(.+)#\1#p'` "
echo "${BACKENDS:-(none)}"
fi
# Disable explicitly disabled and enable explicitly enabled backends
for BACKEND in ${DISABLED_BACKENDS}; do
BACKENDS="${BACKENDS//${BACKEND} /}"
done
for BACKEND in ${ENFORCED_BACKENDS}; do
BACKENDS="${BACKEND} ${BACKENDS//${BACKEND} /}"
done
echo "Building with backends: ${BACKENDS:-(none)}"
if [ -z "$BACKENDS" ]; then
echo "WARNING: Building without backends! You won't be able to see _any_ images." >&2
fi
echo -n "Checking if the prerequisites are installed.. "
LIBS_GTK3="`$MAKE get_libs GTK_VERSION=3 EXECUTABLE_EXTENSION=$EXECUTABLE_EXTENSION ${BACKENDS:+BACKENDS="$BACKENDS"} | sed -nre 's#^LIBS: ?(.+)#\1#p'`"
LIBS_GTK2="`$MAKE get_libs GTK_VERSION=2 EXECUTABLE_EXTENSION=$EXECUTABLE_EXTENSION ${BACKENDS:+BACKENDS="$BACKENDS"} | sed -nre 's#^LIBS: ?(.+)#\1#p'`"
if [ $? != 0 ]; then
echo "failed."
echo
echo
echo "Failed to run make. Is your make command compatible to GNU make?" >&2
exit 1
fi
if $PKG_CONFIG --exists "$LIBS_GTK3"; then
LIBS="${LIBS_GTK3}"
echo "ok"
else
if $PKG_CONFIG --exists "$LIBS_GTK2"; then
if [ "$GTK_VERSION" = 3 ]; then
echo "failed."
echo
echo
echo "GTK 2 was found, but you manually specified --gtk-version=3, which was not found." >&2
echo "If you want GTK3, install the development packages for" >&2
echo " ${LIBS_GTK3}" >&2
exit 1
fi
echo "ok, found GTK 2"
GTK_VERSION=2
LIBS="${LIBS_GTK2}"
else
echo "failed."
echo
echo
echo "Please install either the development packages for " >&2
echo " ${LIBS_GTK3}" >&2
echo "or for" >&2
echo " ${LIBS_GTK2}" >&2
exit 1
fi
fi
echo "Writing config.make."
cat > config.make <\n#include \nint main() { poppler_get_version(); spectre_status_to_string(SPECTRE_STATUS_SUCCESS); }" > test.c
${CROSS}${CC:-cc} ${CFLAGS} test.c ${LDFLAGS} $($PKG_CONFIG --libs --cflags ${LIBS}) -o test >/dev/null 2>&1
if [ -e test ]; then
if [ "$(ldd test | grep -E "liblcms[0-9]?.so" | wc -l)" -gt 1 ]; then
echo "yes"
echo 2>&1
echo "WARNING: You enabled both the spectre and poppler backends, and chose static linking." >&2
echo "Your system uses two different versions of liblcms that are known to interfere:" 2>&1
ldd test | grep -E "liblcms[0-9]?.so" >&2
echo "Recompile using --backends-build=shared if you experience problems." 2>&1
echo 2>&1
else
echo "no"
fi
else
echo "test failed"
fi
rm -f test.c test
cd ..
rmdir $DIR
fi
echo
echo "Done. Run \`$MAKE install' to install pqiv."
exit 0
pqiv-2.6/gpl.txt 0000664 0000000 0000000 00000104513 12737703411 0013674 0 ustar 00root root 0000000 0000000 GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc.
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of
protecting users' freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we
have designed this version of the GPL to prohibit the practice for those
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
Copyright (C)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
Copyright (C)
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
.
pqiv-2.6/lib/ 0000775 0000000 0000000 00000000000 12737703411 0013113 5 ustar 00root root 0000000 0000000 pqiv-2.6/lib/bostree.c 0000664 0000000 0000000 00000037552 12737703411 0014736 0 ustar 00root root 0000000 0000000 /*
Self-Balancing order statistic tree
Implements an AVL tree with two additional methods,
Select(i), which finds the i'th smallest element, and
Rank(x), which returns the rank of a given element,
which both run in O(log n).
Copyright (c) 2013, Phillip Berndt
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
#include "bostree.h"
#include
#include
#include
/* Macro to simplify the definition of left/right symmetric functions */
#define BOSTREE_LR_SYMMETRIC(x) \
size_t _bosnode_left_offset = x == 1 ? offsetof(BOSNode, left_child_node) : offsetof(BOSNode, right_child_node); \
size_t _bosnode_right_offset = x == 0 ? offsetof(BOSNode, left_child_node) : offsetof(BOSNode, right_child_node); \
size_t _bosnode_left_count_offset = x == 1 ? offsetof(BOSNode, left_child_count) : offsetof(BOSNode, right_child_count); \
size_t _bosnode_right_count_offset = x == 0 ? offsetof(BOSNode, left_child_count) : offsetof(BOSNode, right_child_count)
#define BOSTREE_LR_LEFT_CHILD(x) *(BOSNode**)(void*)((char*)x + _bosnode_left_offset)
#define BOSTREE_LR_RIGHT_CHILD(x) *(BOSNode**)(void*)((char*)x + _bosnode_right_offset)
#define BOSTREE_LR_RIGHT_CHILD_COUNT(x) *(unsigned int *)(void*)((char*)x + _bosnode_right_count_offset)
#define BOSTREE_LR_LEFT_CHILD_COUNT(x) *(unsigned int *)(void*)((char*)x + _bosnode_left_count_offset)
/* Tree structure */
struct _BOSTree {
BOSNode *root_node;
BOSTree_cmp_function cmp_function;
BOSTree_free_function free_function;
};
/* Private helper methods */
/*
Recalculate the depth of a node
*/
static void _bostree_depth_recalculate(BOSNode *node) {
node->depth = node->left_child_node == NULL ? 0 : node->left_child_node->depth + 1;
if(node->right_child_node == NULL) {
return;
}
unsigned int depth = node->right_child_node->depth + 1;
if(depth > node->depth) {
node->depth = depth;
}
}
/*
Generic rotation method. See below in the _left & _right implementations
for what it does.
*/
static int _bostree_rotate(BOSTree *tree, BOSNode *node, unsigned char side) {
BOSTREE_LR_SYMMETRIC(side);
if(node->parent_node == NULL) {
return -1;
}
BOSNode *old_parent = node->parent_node;
BOSNode *old_parent_parent = old_parent->parent_node;
/* Make left child of node the new right child of parent node */
if(BOSTREE_LR_LEFT_CHILD(node) != NULL) {
(BOSTREE_LR_LEFT_CHILD(node))->parent_node = old_parent;
assert((BOSTREE_LR_LEFT_CHILD(node))->parent_node != BOSTREE_LR_LEFT_CHILD(node));
}
BOSTREE_LR_RIGHT_CHILD(old_parent) = BOSTREE_LR_LEFT_CHILD(node);
BOSTREE_LR_RIGHT_CHILD_COUNT(old_parent) = BOSTREE_LR_LEFT_CHILD_COUNT(node);
_bostree_depth_recalculate(old_parent);
/* Make old parent new left child of node */
old_parent->parent_node = node;
assert(old_parent->parent_node != old_parent);
BOSTREE_LR_LEFT_CHILD(node) = old_parent;
BOSTREE_LR_LEFT_CHILD_COUNT(node) = old_parent->right_child_count + old_parent->left_child_count + 1;
_bostree_depth_recalculate(node);
/* Replace parent node with new parent node */
node->parent_node = old_parent_parent;
assert(node->parent_node != node);
if(old_parent_parent == NULL) {
tree->root_node = node;
}
else if(BOSTREE_LR_LEFT_CHILD(old_parent_parent) == old_parent) {
BOSTREE_LR_LEFT_CHILD(old_parent_parent) = node;
_bostree_depth_recalculate(old_parent_parent);
}
else {
BOSTREE_LR_RIGHT_CHILD(old_parent_parent) = node;
_bostree_depth_recalculate(old_parent_parent);
}
return 0;
}
/*
Left rotation. Transforms a tree as follows:
A C
C -> A
F F
The parameter node must point to element C in the left picture. Returns 0 on
success.
*/
static int _bostree_rotate_left(BOSTree *tree, BOSNode *node) {
return _bostree_rotate(tree, node, 1);
}
/*
Right rotation. Transforms the tree as follows:
A B
B -> A
E E
The parameter node must point to B in the left picture. Returns 0 on
success.
*/
static int _bostree_rotate_right(BOSTree *tree, BOSNode *node) {
return _bostree_rotate(tree, node, 0);
}
/*
Rebalances the tree at a given node. Returns the new node on the position
where node was before, or node if nothing was changed.
*/
static BOSNode *_bostree_rebalance(BOSTree *tree, BOSNode *node) {
int rdepth = node->right_child_node != NULL ? node->right_child_node->depth + 1 : 0;
int ldepth = node->left_child_node != NULL ? node->left_child_node->depth + 1 : 0;
int balance = ldepth - rdepth;
if(balance > 1) {
if(node->left_child_node->right_child_node != NULL && (node->left_child_node->left_child_node == NULL || node->left_child_node->right_child_node->depth > node->left_child_node->left_child_node->depth)) {
_bostree_rotate_left(tree, node->left_child_node->right_child_node);
}
_bostree_rotate_right(tree, node->left_child_node);
node = node->parent_node;
}
else if(balance < -1) {
if(node->right_child_node->left_child_node != NULL && (node->right_child_node->right_child_node == NULL || node->right_child_node->left_child_node->depth > node->right_child_node->right_child_node->depth)) {
_bostree_rotate_right(tree, node->right_child_node->left_child_node);
}
_bostree_rotate_left(tree, node->right_child_node);
node = node->parent_node;
}
return node;
}
/* Public methods */
/*
Create a new tree structure
*/
BOSTree *bostree_new(BOSTree_cmp_function cmp_function, BOSTree_free_function free_function) {
BOSTree *new_tree = (BOSTree *)calloc(1, sizeof(BOSTree));
new_tree->cmp_function = cmp_function;
new_tree->free_function = free_function;
return new_tree;
}
/*
Free the memory used by a tree structure and all of its nodes.
*/
void bostree_destroy(BOSTree *tree) {
while(tree->root_node != NULL) {
bostree_remove(tree, bostree_select(tree, 0));
}
free(tree);
}
/*
Return the number of nodes in a tree.
*/
unsigned int bostree_node_count(BOSTree *tree) {
if(tree->root_node == NULL) {
return 0;
}
return 1 + tree->root_node->left_child_count + tree->root_node->right_child_count;
}
/*
Insert data into the tree. The key is used for indexing and fed into the
compare-function. data is a pointer to arbitrary data.
Returns the newly created node.
*/
BOSNode *bostree_insert(BOSTree *tree, void *key, void *data) {
BOSNode *new_node = (BOSNode *)calloc(sizeof(BOSNode), 1);
new_node->weak_ref_node_valid = 1;
new_node->weak_ref_count = 1;
new_node->key = key;
new_node->data = data;
/*
Search for the correct insert position and increment the child count
along the path
*/
BOSNode **search = &tree->root_node;
while(*search != NULL) {
new_node->parent_node = *search;
int direction = tree->cmp_function(key, (*search)->key);
if(direction <= 0) {
(*search)->left_child_count++;
search = &((*search)->left_child_node);
}
else {
(*search)->right_child_count++;
search = &((*search)->right_child_node);
}
}
*search = new_node;
/*
Fix depth and rebalance upwards to the root
*/
BOSNode *bubble = new_node;
while(bubble != NULL) {
if(bubble->parent_node != NULL) {
if(bubble->parent_node->depth < bubble->depth + 1) {
bubble->parent_node->depth++;
}
else {
bubble = _bostree_rebalance(tree, bubble);
break;
}
}
bubble = _bostree_rebalance(tree, bubble);
bubble = bubble->parent_node;
}
return new_node;
}
/*
Remove a given node from the tree. The argument must be a node, not the
associated key! You can look up nodes using bostree_lookup().
This function decreases the weak reference count of the deleted node by
one. Once it reaches zero, the free() helper is called and the node
is freed.
*/
void bostree_remove(BOSTree *tree, BOSNode *node) {
if(node == NULL) {
return;
}
BOSNode **reparent_location;
if(node == tree->root_node) {
reparent_location = &tree->root_node;
}
else if(node->parent_node->left_child_node == node) {
reparent_location = &node->parent_node->left_child_node;
}
else {
reparent_location = &node->parent_node->right_child_node;
}
/* If this node had only one child, simply replace by that one */
if(node->left_child_node == NULL) {
*reparent_location = node->right_child_node;
}
else if(node->right_child_node == NULL) {
*reparent_location = node->left_child_node;
}
/* If not, we find the largest element of the left sub-tree and use that */
else {
BOSNode *replacer = bostree_previous_node(node);
assert(replacer->right_child_node == NULL);
BOSNode *iterator = replacer;
if(iterator->parent_node->left_child_node == iterator) {
iterator->parent_node->left_child_node = replacer->left_child_node;
iterator->parent_node->left_child_count = replacer->left_child_count;
}
else {
iterator->parent_node->right_child_node = replacer->left_child_node;
iterator->parent_node->right_child_count = replacer->left_child_count;
}
if(replacer->left_child_node) {
replacer->left_child_node->parent_node = iterator->parent_node;
}
_bostree_depth_recalculate(iterator->parent_node);
iterator = iterator->parent_node;
while(iterator != *reparent_location && iterator->parent_node != NULL) {
if(iterator->parent_node->left_child_node == iterator) {
iterator->parent_node->left_child_count--;
_bostree_depth_recalculate(iterator->parent_node);
}
else {
iterator->parent_node->right_child_count--;
_bostree_depth_recalculate(iterator->parent_node);
}
iterator = iterator->parent_node;
}
/* Reposition replacer */
*reparent_location = replacer;
replacer->right_child_node = node->right_child_node;
replacer->right_child_count = node->right_child_count;
if(replacer->right_child_node != NULL) {
replacer->right_child_node->parent_node = replacer;
assert(replacer->right_child_node->parent_node != replacer->right_child_node);
}
replacer->left_child_node = node->left_child_node;
replacer->left_child_count = node->left_child_count;
if(replacer->left_child_node != NULL) {
replacer->left_child_node->parent_node = replacer;
assert(replacer->left_child_node->parent_node != replacer->left_child_node);
}
_bostree_depth_recalculate(replacer);
}
if(*reparent_location != NULL) {
(*reparent_location)->parent_node = node->parent_node;
assert((*reparent_location)->parent_node != *reparent_location);
*reparent_location = _bostree_rebalance(tree, *reparent_location);
}
/* Fix depth and child count at the parent node */
if(node->parent_node != NULL) {
/*
Comparing the memory addresses instead of the contents here is
important if *reparent_location == NULL
*/
if(&node->parent_node->left_child_node == reparent_location) {
node->parent_node->left_child_count--;
}
else {
node->parent_node->right_child_count--;
}
_bostree_depth_recalculate(node->parent_node);
/* Fix depth and child counts down to the root */
BOSNode *iterator = node->parent_node;
while(iterator->parent_node != NULL) {
if(iterator->parent_node->left_child_node == iterator) {
iterator->parent_node->left_child_count--;
}
else {
iterator->parent_node->right_child_count--;
}
iterator = _bostree_rebalance(tree, iterator);
_bostree_depth_recalculate(iterator->parent_node);
iterator = iterator->parent_node;
}
}
if(tree->root_node != NULL) {
_bostree_rebalance(tree, tree->root_node);
}
node->weak_ref_node_valid = 0;
bostree_node_weak_unref(tree, node);
}
/*
Create a weak reference to a node
This simple implementation of weak references really only deletes the node
once the last reference to it has been dropped, but it invalidates the node
once it has been removed from the tree.
*/
BOSNode *bostree_node_weak_ref(BOSNode *node) {
assert(node->weak_ref_count > 0);
assert(node->weak_ref_count < 127);
node->weak_ref_count++;
return node;
}
/*
Remove the weak reference again, returning NULL if the node was invalid and
the node if it is still valid.
Once the weak reference count reaches zero, call the free() helper for
the user to delete node->data and node->key. The node structure itself is
freed by us.
*/
BOSNode *bostree_node_weak_unref(BOSTree *tree, BOSNode *node) {
BOSNode *retval = node->weak_ref_node_valid ? node : NULL;
assert(node->weak_ref_count > 0);
if(--node->weak_ref_count == 0) {
if(tree->free_function != NULL) {
tree->free_function(node);
}
free(node);
return NULL;
}
return retval;
}
/*
Lookup the node for a given key.
Returns NULL if the node is not found.
*/
BOSNode *bostree_lookup(BOSTree *tree, void *key) {
BOSNode *node = tree->root_node;
while(node != NULL) {
int direction = tree->cmp_function(key, node->key);
if(direction < 0) {
node = node->left_child_node;
}
else if(direction == 0) {
break;
}
else {
node = node->right_child_node;
}
}
return node;
}
/*
Returns the node for a given index.
Returns NULL if a node with the given index does not exist.
*/
BOSNode *bostree_select(BOSTree *tree, unsigned int index) {
BOSNode *node = tree->root_node;
while(node != NULL) {
if(node->left_child_count <= index) {
index -= node->left_child_count;
if(index == 0) {
return node;
}
index--;
node = node->right_child_node;
}
else {
node = node->left_child_node;
}
}
return NULL;
}
/*
Return the node following node in in-order-traversal.
Returns NULL when called with the last node.
*/
BOSNode *bostree_next_node(BOSNode *node) {
if(node->right_child_node != NULL) {
node = node->right_child_node;
while(node->left_child_node != NULL) {
node = node->left_child_node;
}
return node;
}
while(node->parent_node != NULL) {
if(node->parent_node->left_child_node == node) {
return node->parent_node;
}
node = node->parent_node;
}
return NULL;
}
/*
Return the node preceeding node in in-order-traversal.
Returns NULL when called with the first node.
*/
BOSNode *bostree_previous_node(BOSNode *node) {
if(node->left_child_node != NULL) {
node = node->left_child_node;
while(node->right_child_node != NULL) {
node = node->right_child_node;
}
return node;
}
while(node->parent_node != NULL) {
if(node->parent_node->right_child_node == node) {
return node->parent_node;
}
node = node->parent_node;
}
return NULL;
}
/*
Returns the rank associated with node.
*/
unsigned int bostree_rank(BOSNode *node) {
unsigned int rank = node->left_child_count;
while(node->parent_node != NULL) {
if(node->parent_node->right_child_node == node) {
rank += node->parent_node->left_child_count + 1;
}
node = node->parent_node;
}
return rank;
}
#if !defined(NDEBUG) && (_BSD_SOURCE || _XOPEN_SOURCE || _POSIX_C_SOURCE >= 200112L)
#include
#include
/* Debug helpers:
Print the tree to stdout. Makes use of console codes, so this will only
look neat on vt100 compatible terminals, i.e. unix/linux consoles.
*/
static void _bostree_print_helper(BOSNode *node, unsigned int indent, unsigned int level) {
printf("\033[%d;%dH", level + 1, indent);
fsync(0);
printf("%s(%d,%d,%d)\n", (char *)node->key, node->left_child_count, node->right_child_count, node->depth);
fsync(0);
if(node->left_child_node != NULL) {
_bostree_print_helper(node->left_child_node, indent - (2 << node->depth), level + 1);
}
if(node->right_child_node != NULL) {
_bostree_print_helper(node->right_child_node, indent + (2 << node->depth), level + 1);
}
}
void bostree_print(BOSTree *tree) {
if(tree->root_node == NULL) {
return;
}
puts("\033[2J");
fsync(0);
_bostree_print_helper(tree->root_node, (4 << tree->root_node->depth), 0);
printf("\033[%d;1H", tree->root_node->depth + 2);
fsync(0);
}
#endif
pqiv-2.6/lib/bostree.h 0000664 0000000 0000000 00000004437 12737703411 0014737 0 ustar 00root root 0000000 0000000 /*
Self-Balancing order statistic tree
Implements an AVL tree with two additional methods,
Select(i), which finds the i'th smallest element, and
Rank(x), which returns the rank of a given element,
which both run in O(log n).
Copyright (c) 2013, Phillip Berndt
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
#ifndef BOSTREE_H
#define BOSTREE_H
/* Opaque tree structure */
typedef struct _BOSTree BOSTree;
/* Node structure */
struct _BOSNode {
unsigned int left_child_count;
unsigned int right_child_count;
unsigned int depth;
struct _BOSNode *left_child_node;
struct _BOSNode *right_child_node;
struct _BOSNode *parent_node;
void *key;
void *data;
unsigned char weak_ref_count : 7;
unsigned char weak_ref_node_valid : 1;
};
typedef struct _BOSNode BOSNode;
/*
Public interface
See bostree.c for documentation.
*/
typedef int (*BOSTree_cmp_function)(const void *, const void *);
typedef void (*BOSTree_free_function)(BOSNode *node);
BOSTree *bostree_new(BOSTree_cmp_function cmp_function, BOSTree_free_function free_function);
void bostree_destroy(BOSTree *tree);
unsigned int bostree_node_count(BOSTree *tree);
BOSNode *bostree_insert(BOSTree *tree, void *key, void *data);
void bostree_remove(BOSTree *tree, BOSNode *node);
BOSNode *bostree_node_weak_ref(BOSNode *node);
BOSNode *bostree_node_weak_unref(BOSTree *tree, BOSNode *node);
BOSNode *bostree_lookup(BOSTree *tree, void *key);
BOSNode *bostree_select(BOSTree *tree, unsigned int index);
BOSNode *bostree_next_node(BOSNode *node);
BOSNode *bostree_previous_node(BOSNode *node);
unsigned int bostree_rank(BOSNode *node);
#if !defined(NDEBUG) && (_BSD_SOURCE || _XOPEN_SOURCE || _POSIX_C_SOURCE >= 200112L)
void bostree_print(BOSTree *tree);
#endif
#endif
pqiv-2.6/lib/config_parser.c 0000664 0000000 0000000 00000013623 12737703411 0016105 0 ustar 00root root 0000000 0000000 /* A simple INI file parser
* Copyright (c) 2016, Phillip Berndt
* Part of pqiv
*/
#define _POSIX_C_SOURCE 200809L
#include
#include
#include
#include
#include
#include
#include
#include
#ifdef _POSIX_VERSION
#define HAS_MMAP
#endif
#ifdef HAS_MMAP
#include
#endif
#include "config_parser.h"
void config_parser_strip_comments(char *p) {
char *k;
int state = 0;
for(; *p; p++) {
if(*p == '\n') {
state = 0;
}
else if((*p == '#' || *p == ';') && state == 0) {
k = strchr(p, '\n');
if(k) {
memmove(p, k, strlen(k) + 1);
}
else {
*p = 0;
break;
}
}
else if(*p != '\t' && *p != ' ') {
state = 1;
}
}
}
void config_parser_parse_file(const char *file_name, config_parser_callback_t callback) {
int fd = open(file_name, O_RDONLY);
if(fd < 0) {
return;
}
struct stat stat;
if(fstat(fd, &stat) < 0) {
close(fd);
return;
}
char *file_data = NULL;
#ifdef HAS_MMAP
file_data = mmap(NULL, stat.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
if(file_data) {
config_parser_parse_data(file_data, stat.st_size, callback);
munmap(file_data, stat.st_size);
close(fd);
return;
}
#endif
file_data = malloc(stat.st_size);
if(read(fd, file_data, stat.st_size) == stat.st_size) {
config_parser_parse_data(file_data, stat.st_size, callback);
}
free(file_data);
close(fd);
}
static void _config_parser_parse_data_invoke_callback(config_parser_callback_t callback, char *section_start, char *section_end, char *key_start, char *key_end, char *data_start, char *data_end) {
while(key_end > key_start && (*key_end == ' ' || *key_end == '\n' || *key_end == '\r' || *key_end == '\t')) {
key_end--;
}
while(data_end > data_start && (*data_end == ' ' || *data_end == '\n' || *data_end == '\r' || *data_end == '\t')) {
data_end--;
}
if(!data_start || data_end < data_start || *data_start == '\0') {
return;
}
char data_end_restore, section_end_restore, key_end_restore;
data_end_restore = data_end[1];
data_end[1] = 0;
if(section_end) {
section_end_restore = section_end[1];
section_end[1] = 0;
}
if(key_end) {
key_end_restore = key_end[1];
key_end[1] = 0;
}
config_parser_value_t value;
value.chrpval = data_start;
if(*value.chrpval == 'y' || *value.chrpval == 'Y' || *value.chrpval == 't' || *value.chrpval == 'T') {
value.intval = 1;
}
else {
value.intval = atoi(value.chrpval);
}
value.doubleval = atof(value.chrpval);
callback(section_start, key_start, &value);
data_end[1] = data_end_restore;
if(section_end) {
section_end[1] = section_end_restore;
}
if(key_end) {
key_end[1] = key_end_restore;
}
}
void config_parser_parse_data(char *file_data, size_t file_length, config_parser_callback_t callback) {
enum { DEFAULT, SECTION_IDENTIFIER, COMMENT, VALUE } state = DEFAULT;
int section_had_keys = 0;
char *section_start = NULL, *section_end = NULL, *key_start = NULL, *key_end = NULL, *data_start = NULL, *value_start = NULL;
data_start = file_data;
char *fp;
for(fp = file_data; *fp; fp++) {
if(*fp == ' ' || *fp == '\t') {
continue;
}
if(state == DEFAULT) {
if(*fp == '[' && key_start == NULL) {
if(!section_had_keys) {
_config_parser_parse_data_invoke_callback(callback, section_start, section_end, NULL, NULL, data_start, fp - 1);
}
section_had_keys = 0;
section_start = fp + 1;
data_start = NULL;
key_start = NULL;
state = SECTION_IDENTIFIER;
continue;
}
else if(*fp == ';' || *fp == '#') {
state = COMMENT;
}
else if(*fp == '=' && key_start != NULL) {
key_end = fp - 1;
value_start = NULL;
state = VALUE;
}
else if(*fp == '\r' || *fp == '\n') {
key_start = NULL;
}
else {
if(data_start == NULL) {
data_start = fp;
}
if(key_start == NULL) {
key_start = fp;
}
}
}
else if(state == SECTION_IDENTIFIER) {
if(*fp == ']') {
section_end = fp - 1;
state = DEFAULT;
continue;
}
}
else if(state == COMMENT) {
if(*fp == '\n') {
state = DEFAULT;
}
}
else if(state == VALUE) {
if(value_start == NULL) {
value_start = fp;
}
if(*fp != '\n') {
continue;
}
if(fp[1] == ' ' || fp[1] == '\t') {
continue;
}
state = DEFAULT;
section_had_keys |= 1;
_config_parser_parse_data_invoke_callback(callback, section_start, section_end, key_start, key_end, value_start, fp - 1);
key_start = NULL;
}
}
if(state == VALUE) {
_config_parser_parse_data_invoke_callback(callback, section_start, section_end, key_start, key_end, value_start, fp - 1);
}
else if(state != SECTION_IDENTIFIER) {
if(!section_had_keys) {
_config_parser_parse_data_invoke_callback(callback, section_start, section_end, NULL, NULL, data_start, fp - 1);
}
}
else {
fprintf(stderr, "Info: Failed to parse configuration, parsing finished in an unexpected state (%d).\n", state);
}
}
#ifdef TEST
void test_cb(char *section, char *key, config_parser_value_t *value) {
char dup[strlen(value->chrpval)];
strcpy(dup, value->chrpval);
config_parser_strip_comments(dup);
printf("%s.%s: i=%d, f=%f, b=%d, s=\"%s\"\n", section, key, value->intval, value->doubleval, !!value->intval, dup);
}
int main(int argc, char *argv[]) {
if(argc > 1) {
config_parser_parse_file(argv[1], &test_cb);
}
else {
char *data = malloc(10240);
size_t len = 0;
size_t data_size = 10240;
while(true) {
ssize_t red = read(0, &data[len], data_size - len);
if(red == 0) {
break;
}
len += red;
if(len == data_size) {
data_size = data_size + 10240;
data = realloc(data, data_size);
}
}
data[len] = 0;
char *data_copy = malloc(data_size);
memcpy(data_copy, data, data_size);
config_parser_parse_data(data, data_size, &test_cb);
if(memcmp(data, data_copy, data_size) != 0) {
fprintf(stderr, "Warning: Original data changed while processing!\n");
}
free(data);
free(data_copy);
}
}
#endif
pqiv-2.6/lib/config_parser.h 0000664 0000000 0000000 00000003012 12737703411 0016101 0 ustar 00root root 0000000 0000000 /* A simple INI file parser
* Copyright (c) 2016, Phillip Berndt
* Part of pqiv
*
*
* This is a simple stream based configuration file parser. Whitespace at the
* beginning of a line is ignored, except for the continuation of values.
* Configuration files are separated into sections, marked by [section name]. A
* section may contain key=value associations, or be any text alternatively.
* Values may be continued on the next line by indenting its content.
* "#" and ";" at the beginning of a line mark comments inside key/value sections.
* To remove comments from plain-text sections, config_parser_strip_comments()
* may be used.
*
* The API is simple, call config_parser_parse_data() or
* config_parser_parse_file() with a callback function. This function will be
* called for each section text or key/value association found.
*
* The parser makes sure that the file_data remains unchanged if the .._data
* API is used, but does not restore changes the user performs in the callback.
*/
#include
typedef struct {
int intval;
double doubleval;
char *chrpval;
} config_parser_value_t ;
typedef void (*config_parser_callback_t)(char *section, char *key, config_parser_value_t *value);
void config_parser_parse_file(const char *file_name, config_parser_callback_t callback);
void config_parser_parse_data(char *file_data, size_t file_length, config_parser_callback_t callback);
void config_parser_strip_comments(char *p);
#define config_parser_tolower(p) if(p) { for(char *n=p ; *n; ++n) *n = tolower(*n); }
pqiv-2.6/lib/filebuffer.c 0000664 0000000 0000000 00000014370 12737703411 0015375 0 ustar 00root root 0000000 0000000 /**
* pqiv
*
* Copyright (c) 2013-2014, Phillip Berndt
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
*/
#include "filebuffer.h"
#include
#include
#include
#include
#include
#include
#ifdef _POSIX_VERSION
#define HAS_MMAP
#endif
#ifdef HAS_MMAP
#include
#endif
struct buffered_file {
GBytes *data;
char *file_name;
int ref_count;
gboolean file_name_is_temporary;
};
GHashTable *file_buffer_table = NULL;
#ifdef HAS_MMAP
extern GFile *gfile_for_commandline_arg(const char *);
struct buffered_file_mmap_info {
void *ptr;
int fd;
size_t size;
};
static void buffered_file_mmap_free_helper(struct buffered_file_mmap_info *info) {
munmap(info->ptr, info->size);
close(info->fd);
g_slice_free(struct buffered_file_mmap_info, info);
}
#endif
GBytes *buffered_file_as_bytes(file_t *file, GInputStream *data, GError **error_pointer) {
if(!file_buffer_table) {
file_buffer_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
}
struct buffered_file *buffer = g_hash_table_lookup(file_buffer_table, file->file_name);
if(!buffer) {
GBytes *data_bytes = NULL;
if((file->file_flags & FILE_FLAGS_MEMORY_IMAGE)) {
data_bytes = g_bytes_ref(file->file_data);
}
else {
#ifdef HAS_MMAP
// If this is a local file, try to mmap() it first instead of loading it completely
GFile *input_file = gfile_for_commandline_arg(file->file_name);
char *input_file_abspath = g_file_get_path(input_file);
if(input_file_abspath) {
GFileInfo *file_info = g_file_query_info(input_file, G_FILE_ATTRIBUTE_STANDARD_SIZE, G_FILE_QUERY_INFO_NONE, NULL, error_pointer);
if(!file_info) {
g_object_unref(input_file);
return NULL;
}
goffset input_file_size = g_file_info_get_size(file_info);
g_object_unref(file_info);
int fd = open(input_file_abspath, O_RDONLY);
g_free(input_file_abspath);
void *input_file_data = mmap(NULL, input_file_size, PROT_READ, MAP_SHARED, fd, 0);
if(input_file_data) {
struct buffered_file_mmap_info *mmap_info = g_slice_new(struct buffered_file_mmap_info);
mmap_info->ptr = input_file_data;
mmap_info->fd = fd;
mmap_info->size = input_file_size;
data_bytes = g_bytes_new_with_free_func(input_file_data, input_file_size, (GDestroyNotify)buffered_file_mmap_free_helper, mmap_info);
}
}
g_object_unref(input_file);
#endif
if(data_bytes) {
// mmap() above worked
}
else if(!data) {
data = image_loader_stream_file(file, error_pointer);
if(!data) {
return NULL;
}
data_bytes = g_input_stream_read_completely(data, image_loader_cancellable, error_pointer);
g_object_unref(data);
}
else {
data_bytes = g_input_stream_read_completely(data, image_loader_cancellable, error_pointer);
}
if(!data_bytes) {
return NULL;
}
}
buffer = g_new0(struct buffered_file, 1);
g_hash_table_insert(file_buffer_table, g_strdup(file->file_name), buffer);
buffer->data = data_bytes;
}
buffer->ref_count++;
return buffer->data;
}
char *buffered_file_as_local_file(file_t *file, GInputStream *data, GError **error_pointer) {
if(!file_buffer_table) {
file_buffer_table = g_hash_table_new(g_str_hash, g_str_equal);
}
struct buffered_file *buffer = g_hash_table_lookup(file_buffer_table, file->file_name);
if(buffer) {
buffer->ref_count++;
return buffer->file_name;
}
buffer = g_new0(struct buffered_file, 1);
g_hash_table_insert(file_buffer_table, g_strdup(file->file_name), buffer);
gchar *path = NULL;
if(!(file->file_flags & FILE_FLAGS_MEMORY_IMAGE)) {
GFile *input_file = g_file_new_for_commandline_arg(file->file_name);
path = g_file_get_path(input_file);
g_object_unref(input_file);
}
if(path) {
buffer->file_name = path;
buffer->file_name_is_temporary = FALSE;
}
else {
gboolean local_data = FALSE;
if(!data) {
data = image_loader_stream_file(file, error_pointer);
if(!data) {
g_hash_table_remove(file_buffer_table, file->file_name);
return NULL;
}
local_data = TRUE;
}
GFile *temporary_file;
GFileIOStream *iostream = NULL;
gchar *extension = strrchr(file->file_name, '.');
if(extension) {
gchar *name_template = g_strdup_printf("pqiv-XXXXXX%s", extension);
temporary_file = g_file_new_tmp(name_template, &iostream, error_pointer);
g_free(name_template);
}
else {
temporary_file = g_file_new_tmp("pqiv-XXXXXX.ps", &iostream, error_pointer);
}
if(!temporary_file) {
g_printerr("Failed to buffer %s: Could not create a temporary file in %s\n", file->file_name, g_get_tmp_dir());
if(local_data) {
g_object_unref(data);
}
g_hash_table_remove(file_buffer_table, file->file_name);
return NULL;
}
if(g_output_stream_splice(g_io_stream_get_output_stream(G_IO_STREAM(iostream)), data, G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET, image_loader_cancellable, error_pointer) < 0) {
g_hash_table_remove(file_buffer_table, file->file_name);
if(local_data) {
g_object_unref(data);
}
return NULL;
}
buffer->file_name = g_file_get_path(temporary_file);
buffer->file_name_is_temporary = TRUE;
g_object_unref(iostream);
g_object_unref(temporary_file);
if(local_data) {
g_object_unref(data);
}
}
buffer->ref_count++;
return buffer->file_name;
}
void buffered_file_unref(file_t *file) {
struct buffered_file *buffer = g_hash_table_lookup(file_buffer_table, file->file_name);
if(!buffer) {
return;
}
if(--buffer->ref_count == 0) {
if(buffer->data) {
g_bytes_unref(buffer->data);
}
if(buffer->file_name) {
if(buffer->file_name_is_temporary) {
g_unlink(buffer->file_name);
}
g_free(buffer->file_name);
}
g_hash_table_remove(file_buffer_table, file->file_name);
}
}
pqiv-2.6/lib/filebuffer.h 0000664 0000000 0000000 00000002653 12737703411 0015403 0 ustar 00root root 0000000 0000000 /**
* pqiv
*
* Copyright (c) 2013-2014, Phillip Berndt
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
*/
// Utility functions for backends that do not support streams
//
// These functions assure that a file is available in-memory or as
// a local file, regardless of the GIO backend handling it. Through
// reference counting, multi-page documents can be handled with a
// single temporary copy, rather than having to keep one copy per
// page
//
#include "../pqiv.h"
// Return a bytes view on a file_t
GBytes *buffered_file_as_bytes(file_t *file, GInputStream *data, GError **error_pointer);
// Return a (possibly temporary) file for a file_t
char *buffered_file_as_local_file(file_t *file, GInputStream *data, GError **error_pointer);
// Unreference one of the above, free'ing memory if
// necessary
void buffered_file_unref(file_t *file);
pqiv-2.6/lib/strnatcmp.c 0000664 0000000 0000000 00000010454 12737703411 0015276 0 ustar 00root root 0000000 0000000 /* -*- mode: c; c-file-style: "k&r" -*-
strnatcmp.c -- Perform 'natural order' comparisons of strings in C.
Copyright (C) 2000, 2004 by Martin Pool
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
This version _is_ altered, namely two returns have been removed from the end
of two functions to make the code compile warning-free with -Wunreachable-code.
*/
/* partial change history:
*
* 2004-10-10 mbp: Lift out character type dependencies into macros.
*
* Eric Sosman pointed out that ctype functions take a parameter whose
* value must be that of an unsigned int, even on platforms that have
* negative chars in their default char type.
*/
#include /* size_t */
#include
#include "strnatcmp.h"
/* These are defined as macros to make it easier to adapt this code to
* different characters types or comparison functions. */
static inline int
nat_isdigit(nat_char a)
{
return isdigit((unsigned char) a);
}
static inline int
nat_isspace(nat_char a)
{
return isspace((unsigned char) a);
}
static inline nat_char
nat_toupper(nat_char a)
{
return toupper((unsigned char) a);
}
static int
compare_right(nat_char const *a, nat_char const *b)
{
int bias = 0;
/* The longest run of digits wins. That aside, the greatest
value wins, but we can't know that it will until we've scanned
both numbers to know that they have the same magnitude, so we
remember it in BIAS. */
for (;; a++, b++) {
if (!nat_isdigit(*a) && !nat_isdigit(*b))
return bias;
if (!nat_isdigit(*a))
return -1;
if (!nat_isdigit(*b))
return +1;
if (*a < *b) {
if (!bias)
bias = -1;
} else if (*a > *b) {
if (!bias)
bias = +1;
} else if (!*a && !*b)
return bias;
}
/* never reached: return 0; */
}
static int
compare_left(nat_char const *a, nat_char const *b)
{
/* Compare two left-aligned numbers: the first to have a
different value wins. */
for (;; a++, b++) {
if (!nat_isdigit(*a) && !nat_isdigit(*b))
return 0;
if (!nat_isdigit(*a))
return -1;
if (!nat_isdigit(*b))
return +1;
if (*a < *b)
return -1;
if (*a > *b)
return +1;
}
/* never reached: return 0; */
}
static int
strnatcmp0(nat_char const *a, nat_char const *b, int fold_case)
{
int ai, bi;
nat_char ca, cb;
int fractional, result;
ai = bi = 0;
while (1) {
ca = a[ai]; cb = b[bi];
/* skip over leading spaces or zeros */
while (nat_isspace(ca))
ca = a[++ai];
while (nat_isspace(cb))
cb = b[++bi];
/* process run of digits */
if (nat_isdigit(ca) && nat_isdigit(cb)) {
fractional = (ca == '0' || cb == '0');
if (fractional) {
if ((result = compare_left(a+ai, b+bi)) != 0)
return result;
} else {
if ((result = compare_right(a+ai, b+bi)) != 0)
return result;
}
}
if (!ca && !cb) {
/* The strings compare the same. Perhaps the caller
will want to call strcmp to break the tie. */
return 0;
}
if (fold_case) {
ca = nat_toupper(ca);
cb = nat_toupper(cb);
}
if (ca < cb)
return -1;
if (ca > cb)
return +1;
++ai; ++bi;
}
}
int
strnatcmp(nat_char const *a, nat_char const *b) {
return strnatcmp0(a, b, 0);
}
/* Compare, recognizing numeric string and ignoring case. */
int
strnatcasecmp(nat_char const *a, nat_char const *b) {
return strnatcmp0(a, b, 1);
}
pqiv-2.6/lib/strnatcmp.h 0000664 0000000 0000000 00000002407 12737703411 0015302 0 ustar 00root root 0000000 0000000 /* -*- mode: c; c-file-style: "k&r" -*-
strnatcmp.c -- Perform 'natural order' comparisons of strings in C.
Copyright (C) 2000, 2004 by Martin Pool
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
/* CUSTOMIZATION SECTION
*
* You can change this typedef, but must then also change the inline
* functions in strnatcmp.c */
typedef char nat_char;
int strnatcmp(nat_char const *a, nat_char const *b);
int strnatcasecmp(nat_char const *a, nat_char const *b);
pqiv-2.6/lib/update.sh 0000664 0000000 0000000 00000000655 12737703411 0014737 0 ustar 00root root 0000000 0000000 #!/bin/sh
for FILE in \
"https://raw.githubusercontent.com/sourcefrog/natsort/master/strnatcmp.c" \
"https://raw.githubusercontent.com/sourcefrog/natsort/master/strnatcmp.h" \
"https://raw.github.com/phillipberndt/bostree/master/bostree.c" \
"https://raw.github.com/phillipberndt/bostree/master/bostree.h"; do
BASENAME=`basename "$FILE"`
wget -O $BASENAME.new $FILE && mv $BASENAME.new $BASENAME
done
git diff . || true
pqiv-2.6/pqiv.1 0000664 0000000 0000000 00000043350 12737703411 0013413 0 ustar 00root root 0000000 0000000 .\" vim:filetype=groff
.TH pqiv 1 "March 2016" "1.5"
.SH NAME
pqiv \- quick image viewer
.\"
.SH SYNOPSIS
\fBpqiv\fR [options] [filename(s)...]
.\"
.SH DESCRIPTION
\fBpqiv\fR is a simple command line image viewer inspired by qiv. It is highly
customizable and supports a variety of formats.
.\"
.SH OPTIONS
.\"
.TP
.BR \-c ", " \-\-transparent\-background
Draw \fBpqiv\fR's window borderless and transparent. In window mode, a mouse
click activates and deactivates window decorations.
.\"
.TP
.BR \-d ", " \-\-slideshow\-interval=\fISECONDS\fR
In slideshow mode (Activated by \fB\-\-slideshow\fR or key \fBs\fR by default),
cycle through images at this rate. Floating point values are supported, e.g.
use 0.5 to move through the images at a rate of two images per second.
.\"
.TP
.BR \-f ", " \-\-fullscreen
Start in fullscreen mode. Fullscreen can be toggled by pressing \fBf\fR at
runtime by default.
.\"
.TP
.BR \-F ", " \-\-fade
Fade between images. See also \fB\-\-fade\-duration\fR.
.\"
.TP
.BR \-i ", " \-\-hide\-info\-box
Initially hide the info box. Whether the box is visible can be toggled by
pressing \fBi\fR at runtime by default.
.\"
.TP
.BR \-l ", " \-\-lazy\-load
\fBpqiv\fR normally processes all command line arguments before displaying its
main window. With this option, the window is shown as soon as the first image
has been found and loaded.
.\"
.TP
.BR \-n ", " \-\-sort
Instead of storing images in the order they are given on the command line and
found within directories, sort them. The default order is by name (natural
order). See \fB\-\-sort\-key\fR to change the order.
.\"
.TP
.BR \-P ", " \-\-window\-position=\fIPOSITION\fR
Set the initial window position. \fIPOSITION\fR may either be
.RS
.IP x,y 5
screen coordinates, or
.IP off
to not position the window at all.
.RE
.\"
.TP
.BR \-r ", " \-\-additional\-from\-stdin
Read additional filenames/folders from the standard input. This option
conflicts with \fB\-\-commands\-from\-stdin\fR.
.\"
.TP
.BR \-s ", " \-\-slideshow
Start in slideshow mode. Slideshow mode can be toggled at runtime by pressing
\fBs\fR by default.
.\"
.TP
.BR \-t ", " \-\-scale\-images\-up
Scale images up to fill the whole screen. This can be toggled at runtime by
pressing \fBt\fR by default. See also \fB\-\-disable\-scaling\fR.
.\"
.TP
.BR \-T ", " \-\-window\-title=\fITITLE\fR
Set the title of the window. \fBpqiv\fR substitutes several variables into \fITITLE\fR:
.RS
.IP $BASEFILENAME 15
The base file name of the current file (e.g. `\fIimage.png\fR'),
.IP $FILENAME
The file name of the current file (e.g. `\fI/home/user/image.png\fR'),
.IP $WIDTH
The width of the current image in pixels,
.IP $HEIGHT
The height of the current image in pixels,
.IP $ZOOM
The current zoom level,
.IP $IMAGE_NUMBER
The index of the current image,
.IP $IMAGE_COUNT
The total number of images.
.PP
The default is `pqiv: $FILENAME ($WIDTHx$HEIGHT) $ZOOM% [$IMAGE_NUMBER/$IMAGE_COUNT]'.
.RE
.\"
.TP
.BR \-z ", " \-\-zoom\-level=\fIFLOAT\fR
Set the initial zoom level as a floating point value, i.e., 1.0 is 100%. This
only applies to the first image, other images are scaled according to the scale
mode (see \fB\-\-scale\-images\-up\fR and \fB\-\-disable\-scaling\fR) and
window size.
.\"
.TP
.BR \-1 ", " \-\-command\-1=\fICOMMAND\fR
Bind the external \fICOMMAND\fR to key 1. You can use 2..9 to bind further
commands. The \fBACTIONS\fR feature (see below) allows to bind further keys to
other commands. \fICOMMAND\fR is executed using the default shell processor.
`$1' is substituted with the current file name.
.RS
.PP
If \fICOMMAND\fR begins with `>', its standard output is displayed in a popup window.
.PP
If \fICOMMAND\fR begins with `|', the current image is piped to its standard
input, and its standard output is loaded as an image. This can be used to e.g.
process images.
.RE
.\"
.TP
.BR \-\-action=\fIACTION\fR
Execute a specific \fIACTION\fR when starting \fBpqiv\fR. The syntax is
.RS
.RS
command(parameter); command(parameter);
.RE
See the \fBACTIONS\fR section below for available commands.
.RE
.\"
.TP
.BR \-\-actions\-from\-stdin
Like \fB\-\-action\fR, but read actions from the standard input. See the
\fBACTIONS\fR section below for syntax and available commands. This option
conflicts with \fB\-\-additional\-from\-stdin\fR.
.\"
.TP
.BR \-\-bind\-key=\fIKEY BINDING\fR
Rebind a key to an action. The syntax is
.RS
.RS
key sequence { command(parameter); command(parameter); }
.RE
A key sequence may be one or more characters, or special characters supplied as
`', where name is a GDK key specifier or a mouse button (`Mouse-1') or a
scrolling direction (`Mouse-Scroll-1'). If you e.g. use `ab', then a
user must hit `a' followed by control + `b' to trigger the command. It is possible
to bind `a' and `ab' as well. The action bound to `a' will then be slightly delayed
to allow a user to hit `b'. The semicolon separating commands is optional. See
\fBACTIONS\fR below for available commands.
.PP
If you need to know the name of a key specifier, you can run \fBxev\fR and
press the desired key. The name of the keysym will be printed in parentheses,
preceded by `keysym' and a hexadecimal representation. An alternative is to
run \fBxmodmap \-pk\fR. The command outputs the symbolic names in parentheses.
Or use the list at
\fIhttps://git.gnome.org/browse/gtk+/plain/gdk/gdkkeysyms.h\fR.
.RE
.\"
.TP
.BR \-\-browse
For each command line argument, additionally load all images from the image's
directory. \fBpqiv\fR will still start at the image that was given as the first
parameter.
.\"
.TP
.BR \-\-disable\-scaling
Completely disable scaling. This can be toggled at runtime by
pressing \fBt\fR by default. See also \fB\-\-scale\-images\-up\fR.
.\"
.TP
.BR \-\-end\-of\-files\-action=\fIACTION\fR
If all files have been viewed and the next image is to be viewed, either by the
user's request or because a slideshow is active, \fBpqiv\fR by default cycles
and restarts at the first image. This parameter can be used to modify this
behaviour. Valid choices for \fIACTION\fR are:
.RS
.IP quit 20
Quit \fBpqiv\fR,
.IP wait
Wait until a new image becomes available. This only makes sense if used with
e.g. \fB\-\-watch\-directories\fR,
.IP wrap\ (default)
Restart at the first image. In shuffle mode, choose a new random order,
.IP wrap-no-reshuffle
As wrap, but do not reshuffle in random mode.
.RE
.\"
.TP
.BR \-\-enforce\-window\-aspect\-ratio
Tell the window manager to enforce the aspect ratio of the window. If this flag
is set, then a compliant window manager will not allow users to resize
\fBpqiv\fR's window to a different aspect ratio.
This used to be the default behaviour, but window managers tend to have bugs in
the code handling forced aspect ratios. If the flag is not set and the aspect
ratios of the window and image do not match, then the image will be still be
drawn with the correct aspect ratio, with black borders added at the sides.
.\"
.TP
.BR \-\-fade\-duration=\fISECONDS\fR
With \fB\-\-fade\fR, make each fade this long. Floating point values are
accepted, e.g. 0.5 makes each fade take half a second.
.\"
.TP
.BR \-\-low\-memory
Try to keep memory usage to a minimum. \fBpqiv\fR by default e.g. preloads the
next and previous image to speed up navigation and caches scaled images to
speed up redraws. This flag disables such optimizations.
.\"
.TP
.BR \-\-max\-depth=\fILEVELS\fR
For parameters that are directories, \fBpqiv\fR searches recursively for
images. Use this parameter to limit the depth at which \fBpqiv\fR searches. A
level of 0 disables recursion completely, i.e. if you call pqiv with a
directory as a parameter, it will not search it at all.
.\"
.TP
.BR \-\-recreate\-window
Workaround for window managers that do not handle resize requests correctly:
Instead of resizing, recreate the window whenever the image is changed. This
does not redraw images upon changes in zoom alone.
.\"
.TP
.BR \-\-shuffle
Display files in random order. This option conflicts with \fB\-\-sort\fR. Files
are reshuffled after all images have been shown, but within one cycle, the
order is stable. The reshuffling can be disabled using
\fB\-\-end\-of\-files\-action\fR. At runtime, you can use \fBControl + R\fR by
default to toggle shuffle mode; this retains the shuffled order, i.e., you can
disable shuffle mode, view a few images, then enable it again and continue
after the last image you viewed earlier in shuffle mode.
.\"
.TP
.BR \-\-show\-bindings
Display the keyboard and mouse bindings and exit. This displays the key
bindings in the format accepted by \fB\-\-bind\-key\fR. See there, and the
\fBACTIONS\fR section for details on available actions.
.\"
.TP
.BR \-\-sort\-key=\fIPROPERTY\fR
Key to use for sorting. Supported values for \fIPROPERTY\fR are:
.RS
.IP NAME 8
To sort by filename in natural order, e.g. \fIabc32d\fR before \fIabc112d\fR,
but \fIb1\fR after both,
.IP MTIME
To sort by file modification date.
.RE
.\"
.TP
.BR \-\-watch\-directories
Watch all directories supplied as parameters to \fBpqiv\fR for new files and
add them as they appear. In \fB\-\-sort\fR mode, files are sorted into the
correct position, else, they are appended to the end of the list.
See also \fB\-\-watch\-files\fR, which handles how changes to the image that is
currently being viewed are handled.
.\"
.TP
.BR \-\-watch\-files=\fIVALUE\fR
Watch files for changes on disk. Valid choices for \fIVALUE\fR are:
.RS
.IP "on (default)" 15
Watch files for changes, reload upon a change, and skip to the next file if a file is removed,
.IP changes-only
Watch files for changes, reload upon a change, but do nothing if a file is removed,
.IP off
Do not watch files for changes at all.
.PP
Note that a file that has been removed will still be removed from \fBpqiv\fR's
image list when it has been unloaded, i.e. if a user moves more than one image
away from it. (See also \fB\-\-low\-memory\fR.)
.RE
.\"
.\"
.SH ACTIONS
Actions are the building blocks for controlling \fBpqiv\fR. The syntax for
entering an action is
.RS
\fICOMMAND\fR(\fIPARAMETER\fR)
.RE
where \fICOMMAND\fR is one of the commands described in the following and
\fIPARAMETER\fR is the command's parameter. Strings are not quoted. Instead,
the closing parenthesis must be escaped by a backslash if it is used in a
string. E.g., `command(echo \\))' will output a single `)'. The available
commands are:
.TP
.BR add_file(STRING)
Add a file or directory.
.TP
.BR bind_key(STRING)
Override a key binding. Remember to quote closing parenthesis inside the new
definition by prepending a backslash. Useful in conjunction with
\fBsend_keys(STRING)\fR to set up cyclic bindings.
.TP
.BR command(STRING)
Execute the given command.
.TP
.BR flip_horizontally()
Flip the current image horizontally.
.TP
.BR flip_vertically()
Flip the current image vertically.
.TP
.BR goto_directory_relative(INT)
Jump to the \fIn\fR'th next or previous directory.
.TP
.BR goto_file_byindex(INT)
Jump to a file given by its number.
.TP
.BR goto_file_byname(STRING)
Jump to a file given by its displayed name.
.TP
.BR goto_file_relative(INT)
Jump to the \fIn\fR'th next or previous file.
.TP
.BR hardlink_current_image()
Hardlink the current image to \fI./.pqiv-select/\fR, or copy it if hardlinking is not possible.
.TP
.BR jump_dialog()
Display the jump dialog.
.TP
.BR nop()
Do nothing. Can be used to clear an existing binding.
.TP
.BR numeric_command(INT)
Execute the \fin\fR'th command defined via \fB\-\-command\-1\fR etc.
.TP
.BR output_file_list()
Output a list of all loaded files to the standard output.
.TP
.BR quit()
Quit pqiv.
.TP
.BR reload()
Reload the current image from disk.
.TP
.BR remove_file_byindex(INT)
Remove a file given by its number.
.TP
.BR remove_file_byname(STRING)
Remove a file given by its displayed name.
.TP
.BR reset_scale_level()
Reset the scale level to the default value.
.TP
.BR rotate_left()
Rotate the current image left by 90°.
.TP
.BR rotate_right()
Rotate the current image right by 90°.
.TP
.BR send_keys(STRING)
Emulate pressing a sequence of keys. This action currently does not support
special keys that do not have an ASCII representation. Useful in conjunction
with \fBbind_key(STRING)\fR to set up cyclic key bindings.
.TP
.BR set_cursor_visibility(INT)
Set the visibility of the cursor; 0 disables, other values enable visibility.
.TP
.BR set_scale_level_absolute(DOUBLE)
Set the scale level to the parameter value. 1.0 is 100%. See also
\fB\-\-zoom\-level\fR.
.TP
.BR set_scale_level_relative(DOUBLE)
Adjust the scale level multiplicatively by the parameter value.
.TP
.BR set_scale_mode_fit_px(INT,\ INT)
Always adjust the scale level such that each image fits the given dimensions.
.TP
.BR set_shift_align_corner(STRING)
Align the image to the window/screen border. Possible parameter values are the
cardinal directions, e.g. \fINE\fR will align the image to the north east, i.e. \
top right, corner. You can prepend the parameter by an additional \fIC\fR to
perform the adjustment only if the image dimensions exceed the available space,
and to center the image elsewise.
.TP
.BR set_shift_x(INT)
Set the shift in horizontal direction to a fixed value.
.TP
.BR set_shift_y(INT)
Set the shift in vertical direction to a fixed value.
.TP
.BR set_slideshow_interval_absolute(DOUBLE)
Set the slideshow interval to the parameter value, in seconds.
.TP
.BR set_slideshow_interval_relative(DOUBLE)
Adjust the slideshow interval additively by the parameter value. See also
\fB\-\-slideshow\-interval\fR.
.TP
.BR set_status_output(INT)
Set this to non-zero to make pqiv print status information for scripts to
stdout, once upon activation and then whenever the user moves between images.
The format is compatible with shell variable definitions. Variables currently
implemented are \fICURRENT_FILE_NAME\fR and \fICURRENT_FILE_INDEX\fR. An
output sweep always ends with an empty line.
.TP
.BR shift_x(INT)
Shift the current image in x direction.
.TP
.BR shift_y(INT)
Shift the current image in y direction.
.TP
.BR toggle_fullscreen()
Toggle fullscreen mode.
.TP
.BR toggle_info_box()
Toggle the visibility of the info box.
.TP
.BR toggle_scale_mode(INT)
Change the scale mode: Use 1 to disable scaling, 2 for automated scaledown
(default), 3 to always scale images up, and 4 to maintain the user-set zoom
level. 0 cycles through modes 1\-3.
.TP
.BR toggle_shuffle_mode(INT)
Toggle shuffle mode. Use 0 to cycle through the possible values, 1 to enable shuffle, and any other value to disable it.
.TP
.BR toggle_slideshow()
Toggle slideshow mode.
.\"
.SH DEFAULT KEY BINDINGS
.IP Space 20
Next file.
.IP Backspace
Previous file.
.IP a
Link the current image to \fI./.pqiv-select/\fR, or copy it if hardlinking is not possible.
.IP f
Toggle fullscreen mode.
.IP h/v
Flip the image horizontally or vertically.
.IP k/l
Rotate the image right or left.
.IP i
Toggle visibility of the info box.
.IP j
Show a dialog with a list of all files for quick selection.
.IP q
Quit \fBpqiv\fR
.IP r
Reload the current image.
.IP s
Toggle slideshow mode.
.IP t
Toggle the scale mode; cycle between scaling all images up, scaling large images down and no scaling at all.
.IP Plus/minus
Zoom.
.IP "Mouse buttons (fullscreen)"
Goto the next and previous files.
.IP "Mouse drag (fullscreen)"
Move the image.
.IP "Mouse drag with right button (fullscreen)"
Zoom.
.IP "Arrow keys"
Move the image.
.PP
This list omitted some advanced default bindings. Run `\fBpqiv
\-\-show\-bindings\fR' to display a complete list.
.\"
.SH CONFIGURATION FILE
Upon startup, \fBpqiv\fR parses the file \fI~/.pqivrc\fR. It should be a
INI-style key/value file with an \fIoptions\fR section. All long form
parameters are valid keys. To set a boolean flag, set the value to 1. A set
flag inverts the meaning of the associated parameter. E.g., if you set
`\fIfullscreen=1\fR', then \fBpqiv\fR will start in fullscreen mode unless you supply
\fB\-f\fR upon startup.
.PP
As an example,
.RS
.nf
[options]
fullscreen=1
sort=1
command-1=|convert - -blur 20 -
.fi
.RE
will make \fBpqiv\fR start in fullscreen by default, sort the file list and
bind a blur filter to key \fB1\fR. The \fB\-f\fR flag on the command line will
make \fBpqiv\fR not start in fullscreen, and \fB\-n\fR will make it not sort
the list.
.PP
You can place key bindings in the format of the \fB\-\-bind\-key\fR
parameter in a special \fI[keybindings]\fR section. E.g.,
.RS
.nf
[keybindings]
q { goto_file_relative(-1); }
w { goto_file_relative(1); }
x { send_keys(#1); }
1 { set_scale_level_absolute(1.); bind_key(x { send_keys(#2\\); }); }
2 { set_scale_level_absolute(.5); bind_key(x { send_keys(#3\\); }); }
3 { set_scale_level_absolute(0.25); bind_key(x { send_keys(#1\\); }); }
.fi
.RE
will remap \fIq\fR and \fIw\fR to move between images, and set up \fIx\fR to
cycle through 100%, 50% and 25% zoom levels.
.PP
For backwards compatibility with old versions of \fBpqiv\fR, if the file does
not start with a section definition, the first line will be parsed as command
line parameters.
.PP
You may place comments into the file by beginning a line with `;' or `#'.
Comments at the end of a line are not supported.
.SH EXAMPLES
.\"
.TP
\fBpqiv \-\-bind\-key="a { goto_file_byindex(0) }" \-\-sort foo bar.pdf\fR
Rebinds \fBa\fR to go back to the first image, and loads all files from the
\fIfoo\fR folder and \fIbar.pdf\fR, sorted.
.TP
\fBpqiv \-\-slideshow \-\-watch\-directories \-\-end\-of\-files\-action=wait \-\-slideshow\-interval=0.001 test\fR
Load all files from the \fItest\fR folder in a slideshow progressing very fast,
and in the end wait until new files become available. This effectively displays
new images as they appear in a directory and is useful e.g. if you output images
from a script that you later intent to combine into a movie and wish to monitor
progress.
.TP
\fBecho "output_file_list(); quit()" | pqiv \-\-actions\-from\-stdin test\fR
Output a list of all files from the \fItest\fR folder that \fBpqiv\fR can
handle and quit.
.\"
.SH BUGS
Please report any bugs on github, on https://github.com/phillipberndt/pqiv
.\"
.SH AUTHOR
Phillip Berndt (phillip dot berndt at googlemail dot com)
pqiv-2.6/pqiv.c 0000664 0000000 0000000 00000555363 12737703411 0013511 0 ustar 00root root 0000000 0000000 /**
* pqiv
*
* Copyright (c) 2013-2014, Phillip Berndt
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
*/
#define _XOPEN_SOURCE 600
#include "pqiv.h"
#include "lib/config_parser.h"
#include "lib/strnatcmp.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#ifdef _WIN32
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x500
#else
#if _WIN32_WINNT < 0x800
#ifdef __MINGW32__
#pragma message "Microsoft Windows supported is limited to Windows 2000 and higher, but your mingw version indicates that it does not support those versions. Building might fail."
#else
#error Microsoft Windows supported is limited to Windows 2000 and higher.
#endif
#endif
#endif
#include
#include
#else
#include
#include
#include
#include
#if GTK_MAJOR_VERSION < 3
#include
#endif
#endif
#ifdef DEBUG
#ifndef _WIN32
#include
#endif
#define PQIV_VERSION_DEBUG "-debug"
#else
#define PQIV_VERSION_DEBUG ""
#endif
#if !GLIB_CHECK_VERSION(2, 32, 0)
#define g_thread_new(name, func, data) g_thread_create(func, data, FALSE, NULL)
#endif
// GTK 2 does not define keyboard aliases the way we do
#if GTK_MAJOR_VERSION < 3 // {{{
#define GDK_BUTTON_PRIMARY 1
#define GDK_BUTTON_MIDDLE 2
#define GDK_BUTTON_SECONDARY 3
#define GDK_KEY_VoidSymbol 0xffffff
#define GDK_KEY_Escape 0xff1b
#define GDK_KEY_Return 0xff0d
#define GDK_KEY_Left 0xff51
#define GDK_KEY_Up 0xff52
#define GDK_KEY_Right 0xff53
#define GDK_KEY_Down 0xff54
#define GDK_KEY_KP_Left 0xff96
#define GDK_KEY_KP_Up 0xff97
#define GDK_KEY_KP_Right 0xff98
#define GDK_KEY_KP_Down 0xff99
#define GDK_KEY_plus 0x02b
#define GDK_KEY_KP_Add 0xffab
#define GDK_KEY_KP_Subtract 0xffad
#define GDK_KEY_minus 0x02d
#define GDK_KEY_space 0x020
#define GDK_KEY_KP_Page_Up 0xff9a
#define GDK_KEY_Page_Up 0xff55
#define GDK_KEY_BackSpace 0xff08
#define GDK_KEY_Page_Down 0xff56
#define GDK_KEY_KP_Page_Down 0xff9b
#define GDK_KEY_0 0x030
#define GDK_KEY_1 0x031
#define GDK_KEY_2 0x032
#define GDK_KEY_3 0x033
#define GDK_KEY_4 0x034
#define GDK_KEY_5 0x035
#define GDK_KEY_6 0x036
#define GDK_KEY_7 0x037
#define GDK_KEY_8 0x038
#define GDK_KEY_9 0x039
#define GDK_KEY_A 0x041
#define GDK_KEY_B 0x042
#define GDK_KEY_C 0x043
#define GDK_KEY_D 0x044
#define GDK_KEY_E 0x045
#define GDK_KEY_F 0x046
#define GDK_KEY_G 0x047
#define GDK_KEY_H 0x048
#define GDK_KEY_I 0x049
#define GDK_KEY_J 0x04a
#define GDK_KEY_K 0x04b
#define GDK_KEY_L 0x04c
#define GDK_KEY_M 0x04d
#define GDK_KEY_N 0x04e
#define GDK_KEY_O 0x04f
#define GDK_KEY_P 0x050
#define GDK_KEY_Q 0x051
#define GDK_KEY_R 0x052
#define GDK_KEY_S 0x053
#define GDK_KEY_T 0x054
#define GDK_KEY_U 0x055
#define GDK_KEY_V 0x056
#define GDK_KEY_W 0x057
#define GDK_KEY_X 0x058
#define GDK_KEY_Y 0x059
#define GDK_KEY_Z 0x05a
#define GDK_KEY_a 0x061
#define GDK_KEY_b 0x062
#define GDK_KEY_c 0x063
#define GDK_KEY_d 0x064
#define GDK_KEY_e 0x065
#define GDK_KEY_f 0x066
#define GDK_KEY_g 0x067
#define GDK_KEY_h 0x068
#define GDK_KEY_i 0x069
#define GDK_KEY_j 0x06a
#define GDK_KEY_k 0x06b
#define GDK_KEY_l 0x06c
#define GDK_KEY_m 0x06d
#define GDK_KEY_n 0x06e
#define GDK_KEY_o 0x06f
#define GDK_KEY_p 0x070
#define GDK_KEY_q 0x071
#define GDK_KEY_r 0x072
#define GDK_KEY_s 0x073
#define GDK_KEY_t 0x074
#define GDK_KEY_u 0x075
#define GDK_KEY_v 0x076
#define GDK_KEY_w 0x077
#define GDK_KEY_x 0x078
#define GDK_KEY_y 0x079
#define GDK_KEY_z 0x07a
#endif // }}}
// Global variables and function signatures {{{
// The list of file type handlers and file type initializer function
void initialize_file_type_handlers();
// Storage of the file list
// These lists are accessed from multiple threads:
// * The main thread (count, next, prev, ..)
// * The option parser thread, if --lazy-load is used
// * The image loader thread
// Our thread safety strategy is as follows:
// * Access directory_tree only from the option parser
// * Wrap all file_tree operations with mutexes
// * Use weak references for any operation during which the image might
// invalidate.
// * If a weak reference is invalid, abort the pending operation
// * If an operation can't be aborted, lock the mutex from the start
// until it completes
// * If an operation takes too long for this to work, redesign the
// operation
G_LOCK_DEFINE_STATIC(file_tree);
// In case of trouble:
// #define D_LOCK(x) g_print("Waiting for lock " #x " at line %d\n", __LINE__); G_LOCK(x); g_print(" Locked " #x " at line %d\n", __LINE__)
// #define D_UNLOCK(x) g_print("Unlocked " #x " at line %d\n", __LINE__); G_UNLOCK(x);
#define D_LOCK(x) G_LOCK(x)
#define D_UNLOCK(x) G_UNLOCK(x)
BOSTree *file_tree;
BOSTree *directory_tree;
BOSNode *current_file_node = NULL;
BOSNode *image_loader_thread_currently_loading = NULL;
gboolean file_tree_valid = FALSE;
// We asynchroniously load images in a separate thread
GAsyncQueue *image_loader_queue = NULL;
GCancellable *image_loader_cancellable = NULL;
// Unloading of files is also handled by that thread, in a GC fashion
// For that, we keep a list of loaded files
GList *loaded_files_list = NULL;
// Filter for path traversing upon building the file list
GHashTable *load_images_file_filter_hash_table;
GtkFileFilterInfo *load_images_file_filter_info;
GTimer *load_images_timer;
// Easy access to the file_t within a node
// Remember to always lock file_tree!
#define FILE(x) ((file_t *)(x)->data)
#define CURRENT_FILE FILE(current_file_node)
#define next_file() relative_image_pointer(1)
#define previous_file() relative_image_pointer(-1)
// The node to be displayed first, used in conjunction with --browse
BOSNode *browse_startup_node = NULL;
// When loading additional images via the -r option, we need to know whether the
// image loader initialization succeeded, because we can't just cancel if it does
// not (it checks if any image is loadable and fails if not)
gboolean image_loader_initialization_succeeded = FALSE;
// We sometimes need to decide whether we have to draw the image or if it already
// is. We use this variable for that.
gboolean current_image_drawn = FALSE;
// Variables related to the window, display, etc.
GtkWindow *main_window;
gboolean main_window_visible = FALSE;
gint main_window_width = 10;
gint main_window_height = 10;
gboolean main_window_in_fullscreen = FALSE;
GdkRectangle screen_geometry = { 0, 0, 0, 0 };
gboolean wm_supports_fullscreen = TRUE;
// If a WM indicates no moveresize support that's a hint it's a tiling WM
gboolean wm_supports_moveresize = TRUE;
cairo_pattern_t *background_checkerboard_pattern = NULL;
gboolean gui_initialized = FALSE;
int global_argc;
char **global_argv;
// Those two surfaces are here to store scaled image versions (to reduce
// processor load) and to store the last visible image to have something to
// display while fading and while the (new) current image has not been loaded
// yet.
cairo_surface_t *last_visible_image_surface = NULL;
cairo_surface_t *current_scaled_image_surface = NULL;
#ifndef CONFIGURED_WITHOUT_INFO_TEXT
gchar *current_info_text = NULL;
cairo_rectangle_int_t current_info_text_bounding_box = { 0, 0, 0, 0 };
#endif
// Current state of the displayed image and user interaction
// This matrix stores rotations and reflections (makes ui with scaling/transforming easier)
cairo_matrix_t current_transformation;
gdouble current_scale_level = 1.0;
gint current_shift_x = 0;
gint current_shift_y = 0;
guint32 last_button_press_time = 0;
guint current_image_animation_timeout_id = 0;
// -1 means no slideshow, 0 means active slideshow but no current timeout
// source set, anything bigger than that actually is a slideshow id.
gint slideshow_timeout_id = -1;
// A list containing references to the images in shuffled order
typedef struct {
gboolean viewed;
BOSNode *node;
} shuffled_image_ref_t;
guint shuffled_images_visited_count = 0;
guint shuffled_images_list_length = 0;
GList *shuffled_images_list = NULL;
#define LIST_SHUFFLED_IMAGE(x) (((shuffled_image_ref_t *)x->data))
#ifndef CONFIGURED_WITHOUT_EXTERNAL_COMMANDS
// User options
gchar *external_image_filter_commands[] = {
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
};
#endif
gchar keyboard_aliases[127] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0 };
// Scaling mode, only 0-2 are available in the default UI, FIXED_SCALE can be
// set using a command line option, SCALE_TO_FIT only using an action
enum { NO_SCALING=0, AUTO_SCALEDOWN, AUTO_SCALEUP, FIXED_SCALE, SCALE_TO_FIT } option_scale = AUTO_SCALEDOWN;
struct {
guint width;
guint height;
} scale_to_fit_size;
gboolean scale_override = FALSE;
const gchar *option_window_title = "pqiv: $FILENAME ($WIDTHx$HEIGHT) $ZOOM% [$IMAGE_NUMBER/$IMAGE_COUNT]";
gdouble option_slideshow_interval = 5.;
#ifndef CONFIGURED_WITHOUT_INFO_TEXT
gboolean option_hide_info_box = FALSE;
#endif
gboolean option_start_fullscreen = FALSE;
gdouble option_initial_scale = 1.0;
gboolean option_start_with_slideshow_mode = FALSE;
gboolean option_sort = FALSE;
enum { NAME, MTIME } option_sort_key = NAME;
gboolean option_shuffle = FALSE;
gboolean option_reverse_cursor_keys = FALSE;
gboolean option_reverse_scroll = FALSE;
gboolean option_transparent_background = FALSE;
gboolean option_watch_directories = FALSE;
gboolean option_fading = FALSE;
gboolean option_lazy_load = FALSE;
gboolean option_lowmem = FALSE;
gboolean option_addl_from_stdin = FALSE;
gboolean option_recreate_window = FALSE;
gboolean option_enforce_window_aspect_ratio = FALSE;
#ifndef CONFIGURED_WITHOUT_ACTIONS
gboolean option_actions_from_stdin = FALSE;
gboolean option_status_output = FALSE;
#else
static const gboolean option_actions_from_stdin = FALSE;
#endif
double option_fading_duration = .5;
gint option_max_depth = -1;
gboolean option_browse = FALSE;
enum { QUIT, WAIT, WRAP, WRAP_NO_RESHUFFLE } option_end_of_files_action = WRAP;
enum { ON, OFF, CHANGES_ONLY } option_watch_files = ON;
double fading_current_alpha_stage = 0;
gint64 fading_initial_time;
gboolean options_keyboard_alias_set_callback(const gchar *option_name, const gchar *value, gpointer data, GError **error);
#ifndef CONFIGURED_WITHOUT_ACTIONS
gboolean options_bind_key_callback(const gchar *option_name, const gchar *value, gpointer data, GError **error);
gboolean help_show_key_bindings(const gchar *option_name, const gchar *value, gpointer data, GError **error);
#endif
gboolean option_window_position_callback(const gchar *option_name, const gchar *value, gpointer data, GError **error);
gboolean option_scale_level_callback(const gchar *option_name, const gchar *value, gpointer data, GError **error);
gboolean option_end_of_files_action_callback(const gchar *option_name, const gchar *value, gpointer data, GError **error);
gboolean option_watch_files_callback(const gchar *option_name, const gchar *value, gpointer data, GError **error);
gboolean option_sort_key_callback(const gchar *option_name, const gchar *value, gpointer data, GError **error);
gboolean option_action_callback(const gchar *option_name, const gchar *value, gpointer data, GError **error);
void load_images_handle_parameter(char *param, load_images_state_t state, gint depth);
struct {
gint x;
gint y;
} option_window_position = { -2, -2 };
// Hint: Only types G_OPTION_ARG_NONE, G_OPTION_ARG_STRING, G_OPTION_ARG_DOUBLE/INTEGER and G_OPTION_ARG_CALLBACK are
// implemented for option parsing.
GOptionEntry options[] = {
{ "keyboard-alias", 'a', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, (gpointer)&options_keyboard_alias_set_callback, "Define n as a keyboard alias for f", "nfnf.." },
{ "transparent-background", 'c', 0, G_OPTION_ARG_NONE, &option_transparent_background, "Borderless transparent window", NULL },
{ "slideshow-interval", 'd', 0, G_OPTION_ARG_DOUBLE, &option_slideshow_interval, "Set slideshow interval", "n" },
{ "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &option_start_fullscreen, "Start in fullscreen mode", NULL },
{ "fade", 'F', 0, G_OPTION_ARG_NONE, (gpointer)&option_fading, "Fade between images", NULL },
#ifndef CONFIGURED_WITHOUT_INFO_TEXT
{ "hide-info-box", 'i', 0, G_OPTION_ARG_NONE, &option_hide_info_box, "Initially hide the info box", NULL },
#endif
{ "lazy-load", 'l', 0, G_OPTION_ARG_NONE, &option_lazy_load, "Display the main window as soon as possible", NULL },
{ "sort", 'n', 0, G_OPTION_ARG_NONE, &option_sort, "Sort files in natural order", NULL },
{ "window-position", 'P', 0, G_OPTION_ARG_CALLBACK, (gpointer)&option_window_position_callback, "Set initial window position (`x,y' or `off' to not position the window at all)", "POSITION" },
{ "reverse-cursor-keys", 'R', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &option_reverse_cursor_keys, "Reverse the meaning of the cursor keys", NULL },
{ "additional-from-stdin", 'r', 0, G_OPTION_ARG_NONE, &option_addl_from_stdin, "Read additional filenames/folders from stdin", NULL },
{ "slideshow", 's', 0, G_OPTION_ARG_NONE, &option_start_with_slideshow_mode, "Activate slideshow mode", NULL },
{ "scale-images-up", 't', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, (gpointer)&option_scale_level_callback, "Scale images up to fill the whole screen", NULL },
{ "window-title", 'T', 0, G_OPTION_ARG_STRING, &option_window_title, "Set the title of the window. See manpage for available variables.", "TITLE" },
{ "zoom-level", 'z', 0, G_OPTION_ARG_DOUBLE, &option_initial_scale, "Set initial zoom level (1.0 is 100%)", "FLOAT" },
#ifndef CONFIGURED_WITHOUT_EXTERNAL_COMMANDS
{ "command-1", '1', 0, G_OPTION_ARG_STRING, &external_image_filter_commands[0], "Bind the external COMMAND to key 1. See manpage for extended usage (commands starting with `>' or `|'). Use 2..9 for further commands.", "COMMAND" },
{ "command-2", '2', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &external_image_filter_commands[1], NULL, NULL },
{ "command-3", '3', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &external_image_filter_commands[2], NULL, NULL },
{ "command-4", '4', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &external_image_filter_commands[3], NULL, NULL },
{ "command-5", '5', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &external_image_filter_commands[4], NULL, NULL },
{ "command-6", '6', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &external_image_filter_commands[5], NULL, NULL },
{ "command-7", '7', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &external_image_filter_commands[6], NULL, NULL },
{ "command-8", '8', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &external_image_filter_commands[7], NULL, NULL },
{ "command-9", '9', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &external_image_filter_commands[8], NULL, NULL },
#endif
#ifndef CONFIGURED_WITHOUT_ACTIONS
{ "action", 0, 0, G_OPTION_ARG_CALLBACK, &option_action_callback, "Read actions from stdin", "ACTION" },
{ "actions-from-stdin", 0, 0, G_OPTION_ARG_NONE, &option_actions_from_stdin, "Read actions from stdin", NULL },
{ "bind-key", 0, 0, G_OPTION_ARG_CALLBACK, &options_bind_key_callback, "Rebind a key to another action, see manpage and --show-keybindings output for details.", "KEY BINDING" },
#endif
{ "browse", 0, 0, G_OPTION_ARG_NONE, &option_browse, "For each command line argument, additionally load all images from the image's directory", NULL },
{ "disable-scaling", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, (gpointer)&option_scale_level_callback, "Disable scaling of images", NULL },
{ "end-of-files-action", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer)&option_end_of_files_action_callback, "Action to take after all images have been viewed. (`quit', `wait', `wrap', `wrap-no-reshuffle')", "ACTION" },
{ "enforce-window-aspect-ratio", 0, 0, G_OPTION_ARG_NONE, &option_enforce_window_aspect_ratio, "Fix the aspect ratio of the window to match the current image's", NULL },
{ "fade-duration", 0, 0, G_OPTION_ARG_DOUBLE, &option_fading_duration, "Adjust fades' duration", "SECONDS" },
{ "low-memory", 0, 0, G_OPTION_ARG_NONE, &option_lowmem, "Try to keep memory usage to a minimum", NULL },
{ "max-depth", 0, 0, G_OPTION_ARG_INT, &option_max_depth, "Descend at most LEVELS levels of directories below the command line arguments", "LEVELS" },
{ "reverse-scroll", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &option_reverse_scroll, "Reverse the meaning of scroll wheel", NULL },
{ "recreate-window", 0, 0, G_OPTION_ARG_NONE, &option_recreate_window, "Create a new window instead of resizing the old one", NULL },
{ "shuffle", 0, 0, G_OPTION_ARG_NONE, &option_shuffle, "Shuffle files", NULL },
#ifndef CONFIGURED_WITHOUT_ACTIONS
{ "show-bindings", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, &help_show_key_bindings, "Display the keyboard and mouse bindings and exit", NULL },
#endif
{ "sort-key", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer)&option_sort_key_callback, "Key to use for sorting", "PROPERTY" },
{ "watch-directories", 0, 0, G_OPTION_ARG_NONE, &option_watch_directories, "Watch directories for new files", NULL },
{ "watch-files", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer)&option_watch_files_callback, "Watch files for changes on disk (`on`, `off', `changes-only', i.e. do nothing on deletetion)", "VALUE" },
{ NULL, 0, 0, 0, NULL, NULL, NULL }
};
/* Key bindings & actions {{{ */
#define KEY_BINDING_VALUE(is_mouse, state, keycode) ((guint)(((is_mouse & 1) << 31) | ((state & 7) << 28) | (keycode & 0xfffffff)))
static const struct default_key_bindings_struct {
guint key_binding_value;
pqiv_action_t action;
pqiv_action_parameter_t parameter;
} default_key_bindings[] = {
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_Up ), ACTION_SHIFT_Y , { 10 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_KP_Up ), ACTION_SHIFT_Y , { 10 }},
{ KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_Up ), ACTION_SHIFT_Y , { 50 }},
{ KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_KP_Up ), ACTION_SHIFT_Y , { 50 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_Down ), ACTION_SHIFT_Y , { -10 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_KP_Down ), ACTION_SHIFT_Y , { -10 }},
{ KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_Down ), ACTION_SHIFT_Y , { -50 }},
{ KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_KP_Down ), ACTION_SHIFT_Y , { -50 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_Left ), ACTION_SHIFT_X , { 10 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_KP_Left ), ACTION_SHIFT_X , { 10 }},
{ KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_Left ), ACTION_SHIFT_X , { 50 }},
{ KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_KP_Left ), ACTION_SHIFT_X , { 50 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_Right ), ACTION_SHIFT_X , { -10 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_KP_Right ), ACTION_SHIFT_X , { -10 }},
{ KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_Right ), ACTION_SHIFT_X , { -50 }},
{ KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_KP_Right ), ACTION_SHIFT_X , { -50 }},
{ KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_plus ), ACTION_SET_SLIDESHOW_INTERVAL_RELATIVE , { .pdouble = 1. }},
{ KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_KP_Add ), ACTION_SET_SLIDESHOW_INTERVAL_RELATIVE , { .pdouble = 1. }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_plus ), ACTION_SET_SCALE_LEVEL_RELATIVE , { .pdouble = 1.1 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_KP_Add ), ACTION_SET_SCALE_LEVEL_RELATIVE , { .pdouble = 1.1 }},
{ KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_minus ), ACTION_SET_SLIDESHOW_INTERVAL_RELATIVE , { .pdouble = -1. }},
{ KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_KP_Subtract ), ACTION_SET_SLIDESHOW_INTERVAL_RELATIVE , { .pdouble = -1. }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_minus ), ACTION_SET_SCALE_LEVEL_RELATIVE , { .pdouble = 0.9 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_KP_Subtract ), ACTION_SET_SCALE_LEVEL_RELATIVE , { .pdouble = 0.9 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_t ), ACTION_TOGGLE_SCALE_MODE , { 0 }},
{ KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_r ), ACTION_TOGGLE_SHUFFLE_MODE , { 0 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_r ), ACTION_RELOAD , { 0 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_0 ), ACTION_RESET_SCALE_LEVEL , { 0 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_f ), ACTION_TOGGLE_FULLSCREEN , { 0 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_h ), ACTION_FLIP_HORIZONTALLY , { 0 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_v ), ACTION_FLIP_VERTICALLY , { 0 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_l ), ACTION_ROTATE_LEFT , { 0 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_k ), ACTION_ROTATE_RIGHT , { 0 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_i ), ACTION_TOGGLE_INFO_BOX , { 0 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_j ), ACTION_JUMP_DIALOG , { 0 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_s ), ACTION_TOGGLE_SLIDESHOW , { 0 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_a ), ACTION_HARDLINK_CURRENT_IMAGE , { 0 }},
{ KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_BackSpace ), ACTION_GOTO_DIRECTORY_RELATIVE , { -1 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_BackSpace ), ACTION_GOTO_FILE_RELATIVE , { -1 }},
{ KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_space ), ACTION_GOTO_DIRECTORY_RELATIVE , { 1 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_space ), ACTION_GOTO_FILE_RELATIVE , { 1 }},
{ KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_Page_Up ), ACTION_GOTO_FILE_RELATIVE , { 10 }},
{ KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_KP_Page_Up ), ACTION_GOTO_FILE_RELATIVE , { 10 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_Page_Down ), ACTION_GOTO_FILE_RELATIVE , { -10 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_Page_Up ), ACTION_GOTO_FILE_RELATIVE , { 10 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_KP_Page_Up ), ACTION_GOTO_FILE_RELATIVE , { 10 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_KP_Page_Down ), ACTION_GOTO_FILE_RELATIVE , { -10 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_q ), ACTION_QUIT , { 0 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_Escape ), ACTION_QUIT , { 0 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_1 ), ACTION_NUMERIC_COMMAND , { 1 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_2 ), ACTION_NUMERIC_COMMAND , { 2 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_3 ), ACTION_NUMERIC_COMMAND , { 3 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_4 ), ACTION_NUMERIC_COMMAND , { 4 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_5 ), ACTION_NUMERIC_COMMAND , { 5 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_6 ), ACTION_NUMERIC_COMMAND , { 6 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_7 ), ACTION_NUMERIC_COMMAND , { 7 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_8 ), ACTION_NUMERIC_COMMAND , { 8 }},
{ KEY_BINDING_VALUE(0 , 0 , GDK_KEY_9 ), ACTION_NUMERIC_COMMAND , { 9 }},
{ KEY_BINDING_VALUE(1 , 0 , GDK_BUTTON_PRIMARY ), ACTION_GOTO_FILE_RELATIVE , { -1 }},
{ KEY_BINDING_VALUE(1 , 0 , GDK_BUTTON_MIDDLE ), ACTION_QUIT , { 0 }},
{ KEY_BINDING_VALUE(1 , 0 , GDK_BUTTON_SECONDARY ), ACTION_GOTO_FILE_RELATIVE , { 1 }},
{ KEY_BINDING_VALUE(1 , 0 , (GDK_SCROLL_UP+1) << 2 ), ACTION_GOTO_FILE_RELATIVE , { 1 }},
{ KEY_BINDING_VALUE(1 , 0 , (GDK_SCROLL_DOWN+1) << 2 ), ACTION_GOTO_FILE_RELATIVE , { -1 }},
{ 0, 0, { 0 } }
};
#ifndef CONFIGURED_WITHOUT_ACTIONS
typedef struct key_binding key_binding_t;
struct key_binding {
pqiv_action_t action;
pqiv_action_parameter_t parameter;
struct key_binding *next_action; // For assinging multiple actions to one key
GHashTable *next_key_bindings; // For key sequences
};
GHashTable *key_bindings;
struct {
key_binding_t *key_binding;
gint timeout_id;
} active_key_binding = { NULL, -1 };
#endif
const struct pqiv_action_descriptor {
const char *name;
enum { PARAMETER_INT, PARAMETER_DOUBLE, PARAMETER_CHARPTR, PARAMETER_2SHORT, PARAMETER_NONE } parameter_type;
} pqiv_action_descriptors[] = {
{ "nop", PARAMETER_NONE },
{ "shift_y", PARAMETER_INT },
{ "shift_x", PARAMETER_INT },
{ "set_slideshow_interval_relative", PARAMETER_DOUBLE },
{ "set_slideshow_interval_absolute", PARAMETER_DOUBLE },
{ "set_scale_level_relative", PARAMETER_DOUBLE },
{ "set_scale_level_absolute", PARAMETER_DOUBLE },
{ "toggle_scale_mode", PARAMETER_INT },
{ "toggle_shuffle_mode", PARAMETER_INT },
{ "reload", PARAMETER_NONE },
{ "reset_scale_level", PARAMETER_NONE },
{ "toggle_fullscreen", PARAMETER_NONE },
{ "flip_horizontally", PARAMETER_NONE },
{ "flip_vertically", PARAMETER_NONE },
{ "rotate_left", PARAMETER_NONE },
{ "rotate_right", PARAMETER_NONE },
{ "toggle_info_box", PARAMETER_NONE },
{ "jump_dialog", PARAMETER_NONE },
{ "toggle_slideshow", PARAMETER_NONE },
{ "hardlink_current_image", PARAMETER_NONE },
{ "goto_directory_relative", PARAMETER_INT },
{ "goto_file_relative", PARAMETER_INT },
{ "quit", PARAMETER_NONE },
{ "numeric_command", PARAMETER_INT },
{ "command", PARAMETER_CHARPTR },
{ "add_file", PARAMETER_CHARPTR },
{ "goto_file_byindex", PARAMETER_INT },
{ "goto_file_byname", PARAMETER_CHARPTR },
{ "remove_file_byindex", PARAMETER_INT },
{ "remove_file_byname", PARAMETER_CHARPTR },
{ "output_file_list", PARAMETER_NONE },
{ "set_cursor_visibility", PARAMETER_INT },
{ "set_status_output", PARAMETER_INT },
{ "set_scale_mode_fit_px", PARAMETER_2SHORT },
{ "set_shift_x", PARAMETER_INT },
{ "set_shift_y", PARAMETER_INT },
{ "bind_key", PARAMETER_CHARPTR },
{ "send_keys", PARAMETER_CHARPTR },
{ "set_shift_align_corner", PARAMETER_CHARPTR },
{ NULL, 0 }
};
/* }}} */
typedef struct {
gint depth;
} directory_watch_options_t;
void set_scale_level_to_fit();
void set_scale_level_for_screen();
#ifndef CONFIGURED_WITHOUT_INFO_TEXT
void info_text_queue_redraw();
void update_info_text(const char *);
#define UPDATE_INFO_TEXT(fmt, ...) { \
gchar *_info_text = g_strdup_printf(fmt, __VA_ARGS__);\
update_info_text(_info_text); \
g_free(_info_text); \
}
#else
#define info_text_queue_redraw(...)
#define update_info_text(...)
#define UPDATE_INFO_TEXT(fmt, ...)
#endif
void queue_draw();
gboolean main_window_center();
void window_screen_changed_callback(GtkWidget *widget, GdkScreen *previous_screen, gpointer user_data);
gboolean image_loader_load_single(BOSNode *node, gboolean called_from_main);
gboolean fading_timeout_callback(gpointer user_data);
void queue_image_load(BOSNode *);
void unload_image(BOSNode *);
void remove_image(BOSNode *);
gboolean initialize_gui_callback(gpointer);
gboolean initialize_image_loader();
void window_hide_cursor();
void window_show_cursor();
void window_center_mouse();
void calculate_current_image_transformed_size(int *image_width, int *image_height);
cairo_surface_t *get_scaled_image_surface_for_current_image();
void window_state_into_fullscreen_actions();
void window_state_out_of_fullscreen_actions();
BOSNode *image_pointer_by_name(gchar *display_name);
BOSNode *relative_image_pointer(ptrdiff_t movement);
void file_tree_free_helper(BOSNode *node);
gint relative_image_pointer_shuffle_list_cmp(shuffled_image_ref_t *ref, BOSNode *node);
void relative_image_pointer_shuffle_list_unref_fn(shuffled_image_ref_t *ref);
gboolean slideshow_timeout_callback(gpointer user_data);
#ifndef CONFIGURED_WITHOUT_ACTIONS
void parse_key_bindings(const gchar *bindings);
gboolean read_commands_thread_helper(gpointer command);
#endif
void recreate_window();
static void status_output();
void handle_input_event(guint key_binding_value);
static void continue_active_input_event_action_chain();
static void block_active_input_event_action_chain();
static void unblock_active_input_event_action_chain();
// }}}
/* Command line handling, creation of the image list {{{ */
gboolean options_keyboard_alias_set_callback(const gchar *option_name, const gchar *value, gpointer data, GError **error) {/*{{{*/
// TODO Deprecated, remove with 2.6
g_printerr("Warning: --keyboard-alias is deprecated and will be removed in pqiv 2.6. Use --bind-key instead.\n");
if(strlen(value) % 2 != 0) {
g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, "The argument to the alias option must have a multiple of two characters: Every odd one is mapped to the even one following it.");
return FALSE;
}
for(size_t i=0; value[i] != 0; i+=2) {
keyboard_aliases[(size_t)value[i]] = value[i+1];
}
return TRUE;
}/*}}}*/
#ifndef CONFIGURED_WITHOUT_ACTIONS /* option --without-actions: Do not include support for configurable key/mouse bindings and actions */
gboolean options_bind_key_callback(const gchar *option_name, const gchar *value, gpointer data, GError **error) {/*{{{*/
// Format for value:
// {key sequence description, special keys as } { {action}({parameter});[...] } [...]
//
// Special names are: , , (GDK_MOD1_MASK), and any other must be fed to gdk_keyval_from_name
// String parameters must be given in quotes
// To set an error:
// g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, "The argument to the alias option must have a multiple of two characters: Every odd one is mapped to the even one following it.");
//
parse_key_bindings(value);
return TRUE;
}/*}}}*/
#endif
gboolean option_window_position_callback(const gchar *option_name, const gchar *value, gpointer data, GError **error) {/*{{{*/
if(strcmp(value, "off") == 0) {
option_window_position.x = option_window_position.y = -1;
return TRUE;
}
gchar *second;
option_window_position.x = g_ascii_strtoll(value, &second, 10);
if(second != value && *second == ',') {
option_window_position.y = g_ascii_strtoll(second + 1, &second, 10);
if(*second == 0) {
return TRUE;
}
}
g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, "Unexpected argument value for the -P option. Allowed formats are: `x,y' and `off'.");
return FALSE;
}/*}}}*/
gboolean option_scale_level_callback(const gchar *option_name, const gchar *value, gpointer data, GError **error) {/*{{{*/
if(g_strcmp0(option_name, "-t") == 0 || g_strcmp0(option_name, "--scale-images-up") == 0) {
option_scale = AUTO_SCALEUP;
}
else {
option_scale = NO_SCALING;
}
return TRUE;
}/*}}}*/
gboolean option_watch_files_callback(const gchar *option_name, const gchar *value, gpointer data, GError **error) {/*{{{*/
if(strcmp(value, "off") == 0) {
option_watch_files = OFF;
}
else if(strcmp(value, "on") == 0) {
option_watch_files = ON;
}
else if(strcmp(value, "changes-only") == 0) {
option_watch_files = CHANGES_ONLY;
}
else {
g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, "Unexpected argument value for the --watch-files option. Allowed values are: on, off and changes-only.");
return FALSE;
}
return TRUE;
}/*}}}*/
gboolean option_end_of_files_action_callback(const gchar *option_name, const gchar *value, gpointer data, GError **error) {/*{{{*/
if(strcmp(value, "quit") == 0) {
option_end_of_files_action = QUIT;
}
else if(strcmp(value, "wait") == 0) {
option_end_of_files_action = WAIT;
}
else if(strcmp(value, "wrap") == 0) {
option_end_of_files_action = WRAP;
}
else if(strcmp(value, "wrap-no-reshuffle") == 0) {
option_end_of_files_action = WRAP_NO_RESHUFFLE;
}
else {
g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, "Unexpected argument value for the --end-of-files-action option. Allowed values are: quit, wait, wrap (default) and wrap-no-reshuffle.");
return FALSE;
}
return TRUE;
}/*}}}*/
gboolean option_sort_key_callback(const gchar *option_name, const gchar *value, gpointer data, GError **error) {/*{{{*/
if(strcmp(value, "name") == 0) {
option_sort_key = NAME;
}
else if(strcmp(value, "mtime") == 0) {
option_sort_key = MTIME;
}
else {
g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, "Unexpected argument value for the --sort-key option. Allowed keys are: name, mtime.");
return FALSE;
}
return TRUE;
}/*}}}*/
#ifndef CONFIGURED_WITHOUT_ACTIONS
gboolean option_action_callback(const gchar *option_name, const gchar *value, gpointer data, GError **error) {/*{{{*/
gdk_threads_add_idle(read_commands_thread_helper, g_strdup(value));
return TRUE;
}/*}}}*/
#endif
void parse_configuration_file_callback(char *section, char *key, config_parser_value_t *value) {
int * const argc = &global_argc;
char *** const argv = &global_argv;
config_parser_tolower(section);
config_parser_tolower(key);
if(!section && !key) {
// Classic pqiv 1.x configuration file: Append to argv {{{
config_parser_strip_comments(value->chrpval);
char *options_contents = value->chrpval;
gint additional_arguments = 0;
gint additional_arguments_max = 10;
// Add configuration file contents to argument vector
char **new_argv = (char **)g_malloc(sizeof(char *) * (*argc + additional_arguments_max + 1));
new_argv[0] = (*argv)[0];
char *end_of_argument;
while(*options_contents != 0) {
end_of_argument = strpbrk(options_contents, " \n\t");
if(end_of_argument == options_contents) {
options_contents++;
continue;
}
else if(end_of_argument != NULL) {
*end_of_argument = 0;
}
else {
end_of_argument = options_contents + strlen(options_contents);
}
gchar *argv_val = options_contents;
g_strstrip(argv_val);
// Try to directly parse boolean options, to reverse their
// meaning on the command line
if(argv_val[0] == '-') {
gboolean direct_parsing_successfull = FALSE;
if(argv_val[1] == '-') {
// Long option
for(GOptionEntry *iter = options; iter->description != NULL; iter++) {
if(iter->long_name != NULL && iter->arg == G_OPTION_ARG_NONE && g_strcmp0(iter->long_name, argv_val + 2) == 0) {
*(gboolean *)(iter->arg_data) = TRUE;
iter->flags |= G_OPTION_FLAG_REVERSE;
direct_parsing_successfull = TRUE;
break;
}
}
}
else {
// Short option
direct_parsing_successfull = TRUE;
for(char *arg = argv_val + 1; *arg != 0; arg++) {
gboolean found = FALSE;
for(GOptionEntry *iter = options; iter->description != NULL && direct_parsing_successfull; iter++) {
if(iter->short_name == *arg) {
found = TRUE;
if(iter->arg == G_OPTION_ARG_NONE) {
*(gboolean *)(iter->arg_data) = TRUE;
iter->flags |= G_OPTION_FLAG_REVERSE;
}
else {
direct_parsing_successfull = FALSE;
// We only want the remainder of the option to be
// appended to the argument vector.
*(arg - 1) = '-';
argv_val = arg - 1;
}
break;
}
}
if(!found) {
g_printerr("Failed to parse the configuration file: Unknown option `%c'\n", *arg);
direct_parsing_successfull = FALSE;
}
}
}
if(direct_parsing_successfull) {
options_contents = end_of_argument + 1;
continue;
}
}
if(!argv_val[0]) {
continue;
}
// Add to argument vector
new_argv[1 + additional_arguments] = g_strdup(argv_val);
options_contents = end_of_argument;
if(++additional_arguments > additional_arguments_max) {
additional_arguments_max += 5;
new_argv = g_realloc(new_argv, sizeof(char *) * (*argc + additional_arguments_max + 1));
}
}
if(*options_contents != 0) {
new_argv[additional_arguments + 1] = g_strstrip(options_contents);
additional_arguments++;
}
// Add the real argument vector and make new_argv the new argv
new_argv = g_realloc(new_argv, sizeof(char *) * (*argc + additional_arguments + 1));
for(int i=1; i<*argc; i++) {
new_argv[i + additional_arguments] = (*argv)[i];
}
new_argv[*argc + additional_arguments] = NULL;
*argv = new_argv;
*argc = *argc + additional_arguments;
return;
// }}}
}
else if(strcmp(section, "options") == 0 && key) {
// pqiv 2.x configuration setting {{{
GError *error_pointer = NULL;
for(GOptionEntry *iter = options; iter->arg_data != NULL; iter++) {
if(iter->long_name != NULL && strcmp(iter->long_name, key) == 0) {
switch(iter->arg) {
case G_OPTION_ARG_NONE: {
*(gboolean *)(iter->arg_data) = !!value->intval;
if(value->intval) {
iter->flags |= G_OPTION_FLAG_REVERSE;
}
} break;
case G_OPTION_ARG_CALLBACK:
case G_OPTION_ARG_STRING:
if(value->chrpval != NULL) {
if(iter->arg == G_OPTION_ARG_CALLBACK) {
gchar long_name[64];
g_snprintf(long_name, 64, "--%s", iter->long_name);
((GOptionArgFunc)(iter->arg_data))(long_name, value->chrpval, NULL, &error_pointer);
}
else {
*(gchar **)(iter->arg_data) = value->chrpval;
}
}
break;
case G_OPTION_ARG_INT:
*(gint *)(iter->arg_data) = value->intval;
break;
case G_OPTION_ARG_DOUBLE:
*(gdouble *)(iter->arg_data) = value->doubleval;
break;
default:
// Unimplemented. See options array.
break;
}
}
}
if(error_pointer != NULL) {
if(error_pointer->code == G_KEY_FILE_ERROR_INVALID_VALUE) {
g_printerr("Failed to load setting for `%s' from configuration file: %s\n", key, error_pointer->message);
}
g_clear_error(&error_pointer);
}
// }}}
}
#ifndef CONFIGURED_WITHOUT_ACTIONS
else if(strcmp(section, "keybindings") == 0 && !key) {
config_parser_strip_comments(value->chrpval);
parse_key_bindings(value->chrpval);
}
#endif
}
void parse_configuration_file() {/*{{{*/
// Check for a configuration file
gchar *config_file_name = g_build_filename(g_getenv("HOME"), ".pqivrc", NULL);
if(!g_file_test(config_file_name, G_FILE_TEST_EXISTS)) {
g_free(config_file_name);
return;
}
config_parser_parse_file(config_file_name, parse_configuration_file_callback);
g_free(config_file_name);
}/*}}}*/
void parse_command_line() {/*{{{*/
GOptionContext *parser = g_option_context_new("FILES");
g_option_context_set_summary(parser, "A minimalist image viewer\npqiv version " PQIV_VERSION PQIV_VERSION_DEBUG " by Phillip Berndt");
g_option_context_set_help_enabled(parser, TRUE);
g_option_context_set_ignore_unknown_options(parser, FALSE);
g_option_context_add_main_entries(parser, options, NULL);
g_option_context_add_group(parser, gtk_get_option_group(TRUE));
GError *error_pointer = NULL;
if(g_option_context_parse(parser, &global_argc, &global_argv, &error_pointer) == FALSE) {
g_printerr("%s\n", error_pointer->message);
exit(1);
}
// User didn't specify any files to load; perhaps some help on how to use
// pqiv would be useful...
if (global_argc == 1 && !option_addl_from_stdin) {
g_printerr("%s", g_option_context_get_help(parser, TRUE, NULL));
exit(0);
}
g_option_context_free(parser);
}/*}}}*/
void load_images_directory_watch_callback(GFileMonitor *monitor, GFile *file, GFile *other_file, GFileMonitorEvent event_type, directory_watch_options_t *options) {/*{{{*/
// The current image holds its own file watch, so we do not have to react
// to changes.
if(event_type == G_FILE_MONITOR_EVENT_CREATED) {
gchar *name = g_file_get_path(file);
if(name != NULL) {
// In theory, handling regular files here should suffice. But files in subdirectories
// seem not always to be recognized correctly by file monitors, so we have to install
// one for each directory.
if(g_file_test(name, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_SYMLINK | G_FILE_TEST_IS_DIR)) {
// Use the standard loading mechanism. If directory watches are enabled,
// the temporary variables used therein are not freed.
load_images_handle_parameter(name, INOTIFY, options->depth);
}
g_free(name);
}
}
// We cannot reliably react on G_FILE_MONITOR_EVENT_DELETED here, because either the tree
// is unsorted, in which case it is indexed by numbers, or it is sorted by the display
// name (important for multi-page documents!), which can be a relative name that is not
// lookupable as well.
//
// Therefore we do not remove files here, but instead rely on nodes being deleted once the
// user tries to access then. For already loaded files (i.e. also the next/previous one),
// the file watch is used to remove the files.
}/*}}}*/
BOSNode *load_images_handle_parameter_add_file(load_images_state_t state, file_t *file) {/*{{{*/
// Add image to images list/tree
// We need to check if the previous/next images have changed, because they
// might have been preloaded and need unloading if so.
D_LOCK(file_tree);
// The file tree might have been invalidated if the user exited pqiv while a loader
// was processing a file. In that case, just cancel and free the file.
if(!file_tree_valid) {
file_free(file);
D_UNLOCK(file_tree);
return NULL;
}
BOSNode *new_node = NULL;
if(!option_sort) {
float *index = g_slice_new0(float);
if(state == FILTER_OUTPUT) {
// As index, use
// min(index(current) + .001, .5 index(current) + .5 index(next))
*index = 1.001f * *(float *)current_file_node->key;
BOSNode *next_node = bostree_next_node(current_file_node);
if(next_node) {
float alternative = .5f * (*(float *)current_file_node->key + *(float *)next_node->key);
*index = fminf(*index, alternative);
}
}
else {
*index = (float)bostree_node_count(file_tree);
}
new_node = bostree_insert(file_tree, (void *)index, file);
}
else {
new_node = bostree_insert(file_tree, file->sort_name, file);
}
if(state == BROWSE_ORIGINAL_PARAMETER && browse_startup_node == NULL) {
browse_startup_node = bostree_node_weak_ref(new_node);
}
D_UNLOCK(file_tree);
if(option_lazy_load && !gui_initialized) {
// When the first image has been processed, we can show the window
// Since it might not successfully load, we might need to call this
// multiple times. We cannot load the image in this thread because some
// backends have a global mutex and would call this function with
// the mutex locked.
if(!gui_initialized) {
gdk_threads_add_idle(initialize_gui_callback, NULL);
}
}
if(state == INOTIFY) {
// If this image was loaded via the INOTIFY handler, we need to update
// the info text. We do not update it here for images loaded via the
// --lazy-load function (i.e. check for main_window_visible /
// gui_initialized), because the high frequency of Xlib calls crashes
// the app (with an Xlib resource unavailable error) at least on my
// development machine.
update_info_text(NULL);
info_text_queue_redraw();
}
return new_node;
}/*}}}*/
GBytes *g_input_stream_read_completely(GInputStream *input_stream, GCancellable *cancellable, GError **error_pointer) {/*{{{*/
size_t data_length = 0;
char *data = g_malloc(1<<23); // + 8 Mib
while(TRUE) {
gsize bytes_read;
if(!g_input_stream_read_all(input_stream, &data[data_length], 1<<23, &bytes_read, cancellable, error_pointer)) {
g_free(data);
return 0;
}
data_length += bytes_read;
if(bytes_read < 1<<23) {
data = g_realloc(data, data_length);
break;
}
else {
data = g_realloc(data, data_length + (1<<23));
}
}
return g_bytes_new_take((guint8*)data, data_length);
}/*}}}*/
GFile *gfile_for_commandline_arg(const char *parameter) {/*{{{*/
// Support for URIs is an extra feature. To prevent breaking compatibility,
// always prefer existing files over URI interpretation.
// For example, all files containing a colon cannot be read using the
// g_file_new_for_commandline_arg command, because they are interpreted
// as an URI with an unsupported scheme.
if(g_file_test(parameter, G_FILE_TEST_EXISTS)) {
return g_file_new_for_path(parameter);
}
else {
return g_file_new_for_commandline_arg(parameter);
}
}/*}}}*/
gboolean load_images_handle_parameter_find_handler(const char *param, load_images_state_t state, file_t *file, GtkFileFilterInfo *file_filter_info) {/*{{{*/
// Check if one of the file type handlers can handle this file
file_type_handler_t *file_type_handler = &file_type_handlers[0];
while(file_type_handler->file_types_handled) {
if(gtk_file_filter_filter(file_type_handler->file_types_handled, file_filter_info) == TRUE) {
file->file_type = file_type_handler;
// Handle using this handler
if(file_type_handler->alloc_fn != NULL) {
file_type_handler->alloc_fn(state, file);
}
else {
load_images_handle_parameter_add_file(state, file);
}
return TRUE;
}
file_type_handler++;
}
return FALSE;
}/*}}}*/
gpointer load_images_handle_parameter_thread(char *param) {/*{{{*/
// Thread version of load_images_handle_parameter
// Free()s param after run
load_images_handle_parameter(param, PARAMETER, 0);
g_free(param);
gtk_widget_queue_draw(GTK_WIDGET(main_window));
return NULL;
}/*}}}*/
void load_images_handle_parameter(char *param, load_images_state_t state, gint depth) {/*{{{*/
file_t *file;
// If the file tree has been invalidated, cancel.
if(!file_tree_valid) {
return;
}
if(!load_images_file_filter_info) {
g_printerr("The image loader has been invalidated; your use case isn't covered. Please file a feature request!\n");
return;
}
// Check for memory image
if(state == PARAMETER && g_strcmp0(param, "-") == 0) {
file = g_slice_new0(file_t);
file->file_flags = FILE_FLAGS_MEMORY_IMAGE;
file->display_name = g_strdup("-");
if(option_sort) {
file->sort_name = g_strdup("-");
}
file->file_name = g_strdup("-");
GError *error_ptr = NULL;
#ifdef _WIN32
GInputStream *stdin_stream = g_win32_input_stream_new(GetStdHandle(STD_INPUT_HANDLE), FALSE);
#else
GInputStream *stdin_stream = g_unix_input_stream_new(0, FALSE);
#endif
file->file_data = g_input_stream_read_completely(stdin_stream, NULL, &error_ptr);
if(!file->file_data) {
g_printerr("Failed to load image from stdin: %s\n", error_ptr->message);
g_clear_error(&error_ptr);
g_slice_free(file_t, file);
g_object_unref(stdin_stream);
return;
}
g_object_unref(stdin_stream);
// Based on the file data, make a guess on the mime type
gsize file_content_size;
gconstpointer file_content = g_bytes_get_data(file->file_data, &file_content_size);
gchar *file_content_type = g_content_type_guess(NULL, file_content, file_content_size, NULL);
gchar *file_mime_type = g_content_type_get_mime_type(file_content_type);
g_free(file_content_type);
GtkFileFilterInfo mime_guesser;
mime_guesser.contains = GTK_FILE_FILTER_MIME_TYPE;
mime_guesser.mime_type = file_mime_type;
if(!load_images_handle_parameter_find_handler(param, state, file, &mime_guesser)) {
// As a last resort, use the default file type handler
g_printerr("Didn't recognize memory file: Its MIME-type `%s' is unknown. Fall-back to default file handler.\n", file_mime_type);
file->file_type = &file_type_handlers[0];
file->file_type->alloc_fn(state, file);
}
g_free(file_mime_type);
}
else {
// If the browse option is enabled, add the containing directory's images instead of the parameter itself
gchar *original_parameter = NULL;
if(state == PARAMETER && option_browse && g_file_test(param, G_FILE_TEST_IS_SYMLINK | G_FILE_TEST_IS_REGULAR) == TRUE) {
// Handle the actual parameter first, such that it is displayed
// first (unless sorting is enabled)
load_images_handle_parameter(param, BROWSE_ORIGINAL_PARAMETER, 0);
// Decrease depth such that the following recursive invocations
// will again have depth 0 (this is the base directory, after all)
depth -= 1;
// Replace param with the containing directory's name
original_parameter = param;
param = g_path_get_dirname(param);
}
// Recurse into directories
if(g_file_test(param, G_FILE_TEST_IS_DIR) == TRUE) {
if(option_max_depth >= 0 && option_max_depth <= depth) {
// Maximum depth exceeded, abort.
return;
}
// Check for recursion
char abs_path[PATH_MAX];
if(
#ifdef _WIN32
GetFullPathNameA(param, PATH_MAX, abs_path, NULL) != 0
#else
realpath(param, abs_path) != NULL
#endif
) {
if(bostree_lookup(directory_tree, abs_path) != NULL) {
if(original_parameter != NULL) {
g_free(param);
}
return;
}
bostree_insert(directory_tree, g_strdup(abs_path), NULL);
}
else {
// Consider this an error
if(original_parameter != NULL) {
g_free(param);
}
g_printerr("Probably too many level of symlinks. Will not traverse into: %s\n", param);
return;
}
// Display progress
if(load_images_timer && g_timer_elapsed(load_images_timer, NULL) > 5.) {
#ifdef _WIN32
g_print("Loading in %-50.50s ...\r", param);
#else
g_print("\033[s\033[?7lLoading in %s ...\033[J\033[u\033[?7h", param);
#endif
}
GDir *dir_ptr = g_dir_open(param, 0, NULL);
if(dir_ptr == NULL) {
if(state == PARAMETER) {
g_printerr("Failed to open directory: %s\n", param);
}
if(original_parameter != NULL) {
g_free(param);
}
return;
}
while(TRUE) {
const gchar *dir_entry = g_dir_read_name(dir_ptr);
if(dir_entry == NULL) {
break;
}
gchar *dir_entry_full = g_strdup_printf("%s%s%s", param, g_str_has_suffix(param, G_DIR_SEPARATOR_S) ? "" : G_DIR_SEPARATOR_S, dir_entry);
if(!(original_parameter != NULL && g_strcmp0(dir_entry_full, original_parameter) == 0)) {
// Skip if we are in --browse mode and this is the file which we have already added above.
load_images_handle_parameter(dir_entry_full, RECURSION, depth + 1);
}
g_free(dir_entry_full);
// If the file tree has been invalidated, cancel.
if(!file_tree_valid) {
return;
}
}
g_dir_close(dir_ptr);
// Add a watch for new files in this directory
if(option_watch_directories) {
// Note: It does not suffice to do this once for each parameter, but this must also be
// called for subdirectories. At least if it is not, new files in subdirectories are
// not always recognized.
GFile *file_ptr = g_file_new_for_path(param);
GFileMonitor *directory_monitor = g_file_monitor_directory(file_ptr, G_FILE_MONITOR_NONE, NULL, NULL);
if(directory_monitor != NULL) {
directory_watch_options_t *options = g_new0(directory_watch_options_t, 1);
options->depth = depth;
g_signal_connect(directory_monitor, "changed", G_CALLBACK(load_images_directory_watch_callback), options);
// We do not store the directory_monitor anywhere, because it is not used explicitly
// again. If this should ever be needed, this is the place where this should be done.
}
g_object_unref(file_ptr);
}
if(original_parameter != NULL) {
g_free(param);
}
return;
}
// Prepare file structure
file = g_slice_new0(file_t);
file->display_name = g_filename_display_name(param);
if(option_sort) {
if(option_sort_key == MTIME) {
// Prepend the modification time to the display name
GFile *param_file = gfile_for_commandline_arg(param);
if(param_file) {
GFileInfo *file_info = g_file_query_info(param_file, G_FILE_ATTRIBUTE_TIME_MODIFIED, G_FILE_QUERY_INFO_NONE, NULL, NULL);
if(file_info) {
GTimeVal result;
g_file_info_get_modification_time(file_info, &result);
g_object_unref(file_info);
file->sort_name = g_strdup_printf("%lu;%s", result.tv_sec, file->display_name);
}
g_object_unref(param_file);
}
}
if(file->sort_name == NULL) {
file->sort_name = g_strdup(file->display_name);
}
}
// In sorting/watch-directories mode, we store the full path to the file in file_name, to be able
// to identify the file if it is deleted
if(option_watch_directories && option_sort) {
char abs_path[PATH_MAX];
if(
#ifdef _WIN32
GetFullPathNameA(param, PATH_MAX, abs_path, NULL) != 0
#else
realpath(param, abs_path) != NULL
#endif
) {
file->file_name = g_strdup(abs_path);
}
else {
file->file_name = g_strdup(param);
}
}
else {
file->file_name = g_strdup(param);
}
// Filter based on formats supported by the different handlers
gchar *param_lowerc = g_utf8_strdown(param, -1);
load_images_file_filter_info->filename = load_images_file_filter_info->display_name = param_lowerc;
// Check if one of the file type handlers can handle this file
if(load_images_handle_parameter_find_handler(param, state, file, load_images_file_filter_info)) {
g_free(param_lowerc);
return;
}
g_free(param_lowerc);
if(state != PARAMETER && state != BROWSE_ORIGINAL_PARAMETER) {
// At this point, if the file was not mentioned explicitly by the user,
// abort.
return;
}
// Make a final attempt to guess the file type by mime type
GFile *param_file = gfile_for_commandline_arg(param);
if(!param_file) {
return;
}
GFileInfo *file_info = g_file_query_info(param_file, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, G_FILE_QUERY_INFO_NONE, NULL, NULL);
if(file_info) {
gchar *param_file_mime_type = g_content_type_get_mime_type(g_file_info_get_content_type(file_info));
if(param_file_mime_type) {
GtkFileFilterInfo mime_guesser;
mime_guesser.contains = GTK_FILE_FILTER_MIME_TYPE;
mime_guesser.mime_type = param_file_mime_type;
if(load_images_handle_parameter_find_handler(param, state, file, &mime_guesser)) {
g_free(param_file_mime_type);
g_object_unref(param_file);
g_object_unref(file_info);
return;
}
else {
g_printerr("Didn't recognize file `%s': Both its extension and MIME-type `%s' are unknown. Fall-back to default file handler.\n", param, param_file_mime_type);
g_free(param_file_mime_type);
g_object_unref(param_file);
}
}
g_object_unref(file_info);
}
// If nothing else worked, assume that this file is handled by the default handler
// Prepare file structure
file->file_type = &file_type_handlers[0];
file_type_handlers[0].alloc_fn(state, file);
}
}/*}}}*/
int image_tree_float_compare(const float *a, const float *b) {/*{{{*/
return *a > *b;
}/*}}}*/
void file_free(file_t *file) {/*{{{*/
if(file->file_type->free_fn != NULL && file->private) {
file->file_type->free_fn(file);
}
g_free(file->display_name);
g_free(file->file_name);
if(file->sort_name) {
g_free(file->sort_name);
}
if(file->file_data) {
g_bytes_unref(file->file_data);
file->file_data = NULL;
}
g_slice_free(file_t, file);
}/*}}}*/
void file_tree_free_helper(BOSNode *node) {
// This helper function is only called once a node is eventually freed,
// which happens only after the last weak reference to it is dropped,
// which happens only if an image is not in the list of loaded images
// anymore, which happens only if it never was loaded or was just
// unloaded. So the call to unload_image should have no side effects,
// and is only there for redundancy to be absolutely sure..
unload_image(node);
file_free(FILE(node));
if(!option_sort) {
g_slice_free(float, node->key);
}
}
void directory_tree_free_helper(BOSNode *node) {
free(node->key);
// value is NULL
}
void load_images() {/*{{{*/
int * const argc = &global_argc;
char ** const argv = global_argv;
// Allocate memory for the file list (Used for unsorted and random order file lists)
file_tree = bostree_new(
option_sort ? (BOSTree_cmp_function)strnatcasecmp : (BOSTree_cmp_function)image_tree_float_compare,
file_tree_free_helper
);
file_tree_valid = TRUE;
// The directory tree is used to prevent nested-symlink loops
directory_tree = bostree_new((BOSTree_cmp_function)g_strcmp0, directory_tree_free_helper);
// Allocate memory for the timer
if(!option_actions_from_stdin) {
load_images_timer = g_timer_new();
g_timer_start(load_images_timer);
}
// Prepare the file filter info structure used for handler detection
load_images_file_filter_info = g_new0(GtkFileFilterInfo, 1);
load_images_file_filter_info->contains = GTK_FILE_FILTER_FILENAME | GTK_FILE_FILTER_DISPLAY_NAME;
// Load the images from the remaining parameters
for(int i=1; i<*argc; i++) {
if(argv[i][0]) {
load_images_handle_parameter(argv[i], PARAMETER, 0);
}
}
if(option_addl_from_stdin) {
GIOChannel *stdin_reader =
#ifdef _WIN32
g_io_channel_win32_new_fd(_fileno(stdin));
#else
g_io_channel_unix_new(STDIN_FILENO);
#endif
gsize line_terminator_pos;
gchar *buffer = NULL;
const gchar *charset = NULL;
if(g_get_charset(&charset)) {
g_io_channel_set_encoding(stdin_reader, charset, NULL);
}
while(g_io_channel_read_line(stdin_reader, &buffer, NULL, &line_terminator_pos, NULL) == G_IO_STATUS_NORMAL) {
if (buffer == NULL) {
continue;
}
buffer[line_terminator_pos] = 0;
load_images_handle_parameter(buffer, PARAMETER, 0);
g_free(buffer);
}
g_io_channel_unref(stdin_reader);
}
// If we can be certain that no further images will be loaded, we can now
// drop the variables we used for loading to free some space
if(!option_watch_directories && !option_actions_from_stdin) {
// TODO
// g_object_ref_sink(load_images_file_filter);
g_free(load_images_file_filter_info);
load_images_file_filter_info = NULL;
bostree_destroy(directory_tree);
}
if(load_images_timer) {
g_timer_destroy(load_images_timer);
}
}/*}}}*/
// }}}
/* (A-)synchronous image loading and image operations {{{ */
void invalidate_current_scaled_image_surface() {/*{{{*/
if(current_scaled_image_surface != NULL) {
cairo_surface_destroy(current_scaled_image_surface);
current_scaled_image_surface = NULL;
}
}/*}}}*/
gboolean image_animation_timeout_callback(gpointer user_data) {/*{{{*/
D_LOCK(file_tree);
if((BOSNode *)user_data != current_file_node || FILE(current_file_node)->force_reload) {
D_UNLOCK(file_tree);
current_image_animation_timeout_id = 0;
return FALSE;
}
if(CURRENT_FILE->file_type->animation_next_frame_fn == NULL) {
D_UNLOCK(file_tree);
current_image_animation_timeout_id = 0;
return FALSE;
}
double delay = CURRENT_FILE->file_type->animation_next_frame_fn(CURRENT_FILE);
D_UNLOCK(file_tree);
if(delay >= 0) {
current_image_animation_timeout_id = gdk_threads_add_timeout(
delay,
image_animation_timeout_callback,
user_data);
}
else {
current_image_animation_timeout_id = 0;
}
invalidate_current_scaled_image_surface();
gtk_widget_queue_draw(GTK_WIDGET(main_window));
return FALSE;
}/*}}}*/
void image_file_updated_callback(GFileMonitor *monitor, GFile *file, GFile *other_file, GFileMonitorEvent event_type, gpointer user_data) {/*{{{*/
BOSNode *node = (BOSNode *)user_data;
if(option_watch_files == OFF) {
return;
}
D_LOCK(file_tree);
if(event_type == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT) {
FILE(node)->force_reload = TRUE;
queue_image_load(node);
}
if(event_type == G_FILE_MONITOR_EVENT_DELETED) {
// It is a difficult decision what to do here. We could either unload the deleted
// image and jump to the next one, or ignore this event. I see use-cases for both.
// Ultimatively, it feels more consistent to unload it: The same reasons why a user
// wouldn't want pqiv to unload an image if it was deleted would also apply to
// reloading upon changes. The one exception I see to this rule is when the current
// image is the last in the directory: Unloading it would cause pqiv to leave.
// A typical use case for pqiv is a picture frame on an Raspberry Pi, where users
// periodically exchange images using scp. There might be a race condition if a user
// is not aware that he should first move the new images to a folder and then remove
// the old ones. Therefore, if there is only one image remaining, pqiv does nothing.
// But as new images are added (if --watch-directories is set), the old one should
// be removed eventually. Hence, force_reload is still set on the deleted image.
//
// There's another race if a user deletes all files at once. --watch-files=ignore
// has been added for such situations, to disable this functionality
if(option_watch_files == ON) {
FILE(node)->force_reload = TRUE;
if(bostree_node_count(file_tree) > 1) {
queue_image_load(node);
}
}
}
D_UNLOCK(file_tree);
}/*}}}*/
gboolean window_move_helper_callback(gpointer user_data) {/*{{{*/
gtk_window_move(main_window, option_window_position.x, option_window_position.y);
option_window_position.x = -1;
return FALSE;
}/*}}}*/
gboolean main_window_resize_callback(gpointer user_data) {/*{{{*/
D_LOCK(file_tree);
// If there is no image loaded, abort
if(!CURRENT_FILE->is_loaded) {
D_UNLOCK(file_tree);
return FALSE;
}
// Get the image's size
int image_width, image_height;
calculate_current_image_transformed_size(&image_width, &image_height);
D_UNLOCK(file_tree);
// In in fullscreen, also abort
if(main_window_in_fullscreen) {
return FALSE;
}
// Recalculate the required window size
int new_window_width = current_scale_level * image_width;
int new_window_height = current_scale_level * image_height;
// Resize if this has not worked before, but accept a slight deviation (might be round-off error)
if(main_window_width >= 0 && abs(main_window_width - new_window_width) + abs(main_window_height - new_window_height) > 1) {
gtk_window_resize(main_window, new_window_width, new_window_height);
}
return FALSE;
}/*}}}*/
void main_window_adjust_for_image() {/*{{{*/
// We only need to adjust the window if it is not in fullscreen
if(main_window_in_fullscreen) {
queue_draw();
return;
}
int image_width, image_height;
calculate_current_image_transformed_size(&image_width, &image_height);
// Resize the window and update geometry hints (we enforce them below)
int new_window_width = current_scale_level * image_width;
int new_window_height = current_scale_level * image_height;
GdkGeometry hints;
if(option_enforce_window_aspect_ratio) {
#if GTK_MAJOR_VERSION >= 3
hints.min_aspect = hints.max_aspect = new_window_width * 1.0 / new_window_height;
#else
// Fix for issue #57: Some WMs calculate aspect ratios slightly different
// than GTK 2, resulting in an off-by-1px result for gtk_window_resize(),
// which creates a loop shrinking the window until it eventually vanishes.
// This is mitigated by temporarily removing the aspect constraint,
// resizing, and reenabling it afterwards.
hints.min_aspect = hints.max_aspect = 0;
#endif
}
if(main_window_width >= 0 && (main_window_width != new_window_width || main_window_height != new_window_height)) {
if(option_recreate_window && main_window_visible) {
recreate_window();
}
if(option_window_position.x >= 0) {
// This is upon startup. Do not attempt to move the window
// directly to the startup position, this won't work. WMs don't
// like being told what to do.. ;-) Wait till the window is visible,
// then move it away.
gdk_threads_add_idle(window_move_helper_callback, NULL);
}
else if(option_window_position.x != -1) {
// Tell the WM to center the window
gtk_window_set_position(main_window, GTK_WIN_POS_CENTER_ALWAYS);
}
if(!main_window_visible) {
gtk_window_set_default_size(main_window, new_window_width, new_window_height);
if(option_enforce_window_aspect_ratio) {
gtk_window_set_geometry_hints(main_window, NULL, &hints, GDK_HINT_ASPECT);
}
main_window_width = new_window_width;
main_window_height = new_window_height;
// Some window managers create a race here upon application startup:
// They resize, as requested above, and afterwards apply their idea of
// window size. To conquer that, we check for the window size again once
// all events are handled.
gdk_threads_add_idle(main_window_resize_callback, NULL);
}
else {
if(option_enforce_window_aspect_ratio) {
gtk_window_set_geometry_hints(main_window, NULL, &hints, GDK_HINT_ASPECT);
}
gtk_window_resize(main_window, new_window_width, new_window_height);
#if GTK_MAJOR_VERSION < 3
if(option_enforce_window_aspect_ratio) {
hints.min_aspect = hints.max_aspect = image_width * 1.0 / image_height;
gtk_window_set_geometry_hints(main_window, NULL, &hints, GDK_HINT_ASPECT);
}
#endif
}
// In theory, we do not need to draw manually here. The resizing will
// trigger a configure event, which will in particular redraw the
// window. But this does not work for tiling WMs. As _NET_WM_ACTION_RESIZE
// is not completely reliable either, we do queue for redraw at the
// cost of a double redraw.
queue_draw();
}
else {
// else: No configure event here, but that's fine: current_scale_level already
// has the correct value
queue_draw();
}
}/*}}}*/
gboolean image_loaded_handler(gconstpointer node) {/*{{{*/
D_LOCK(file_tree);
// Remove any old timeouts etc.
if(current_image_animation_timeout_id > 0) {
g_source_remove(current_image_animation_timeout_id);
current_image_animation_timeout_id = 0;
}
// Only react if the loaded node is still current
if(node && node != current_file_node) {
D_UNLOCK(file_tree);
return FALSE;
}
// If in shuffle mode, mark the current image as viewed, and possibly
// reset the list once all images have been
if(option_shuffle) {
GList *current_shuffled_image = g_list_find_custom(shuffled_images_list, current_file_node, (GCompareFunc)relative_image_pointer_shuffle_list_cmp);
if(current_shuffled_image) {
if(!LIST_SHUFFLED_IMAGE(current_shuffled_image)->viewed) {
LIST_SHUFFLED_IMAGE(current_shuffled_image)->viewed = 1;
if(++shuffled_images_visited_count == bostree_node_count(file_tree)) {
if(option_end_of_files_action == WRAP) {
g_list_free_full(shuffled_images_list, (GDestroyNotify)relative_image_pointer_shuffle_list_unref_fn);
shuffled_images_list = NULL;
shuffled_images_visited_count = 0;
shuffled_images_list_length = 0;
}
}
}
}
}
// Sometimes when a user is hitting the next image button really fast this
// function's execution can be delayed until CURRENT_FILE is again not loaded.
// Return without doing anything in that case.
if(!CURRENT_FILE->is_loaded) {
D_UNLOCK(file_tree);
return FALSE;
}
// Initialize animation timer if the image is animated
if((CURRENT_FILE->file_flags & FILE_FLAGS_ANIMATION) != 0 && CURRENT_FILE->file_type->animation_initialize_fn != NULL) {
current_image_animation_timeout_id = gdk_threads_add_timeout(
CURRENT_FILE->file_type->animation_initialize_fn(CURRENT_FILE),
image_animation_timeout_callback,
(gpointer)current_file_node);
}
// Update geometry hints, calculate initial window size and place window
D_UNLOCK(file_tree);
// Reset shift
current_shift_x = 0;
current_shift_y = 0;
// Reset rotation
cairo_matrix_init_identity(¤t_transformation);
// Adjust scale level, resize, set aspect ratio and place window
if(!current_image_drawn) {
scale_override = FALSE;
}
set_scale_level_for_screen();
main_window_adjust_for_image();
invalidate_current_scaled_image_surface();
current_image_drawn = FALSE;
queue_draw();
// Show window, if not visible yet
if(!main_window_visible) {
main_window_visible = TRUE;
gtk_widget_show_all(GTK_WIDGET(main_window));
}
// Reset the info text
update_info_text(NULL);
// Output status for scripts
status_output();
return FALSE;
}/*}}}*/
GInputStream *image_loader_stream_file(file_t *file, GError **error_pointer) {/*{{{*/
GInputStream *data;
if((file->file_flags & FILE_FLAGS_MEMORY_IMAGE) != 0) {
// Memory view on a memory image
#if GLIB_CHECK_VERSION(2, 34, 0)
data = g_memory_input_stream_new_from_bytes(file->file_data);
#else
gsize size = 0;
// TODO Is it possible to use this fallback and still refcount file_data?
data = g_memory_input_stream_new_from_data(g_bytes_get_data(file->file_data, &size), size, NULL);;
#endif
}
else {
// Classical file or URI
if(image_loader_cancellable) {
g_cancellable_reset(image_loader_cancellable);
}
GFile *input_file = gfile_for_commandline_arg(file->file_name);
if(!input_file) {
return NULL;
}
data = G_INPUT_STREAM(g_file_read(input_file, image_loader_cancellable, error_pointer));
g_object_unref(input_file);
}
return data;
}/*}}}*/
file_t *image_loader_duplicate_file(file_t *file, gchar *custom_display_name, gchar *custom_sort_name) {/*{{{*/
file_t *new_file = g_slice_new(file_t);
*new_file = *file;
if((file->file_flags & FILE_FLAGS_MEMORY_IMAGE)) {
g_bytes_ref(new_file->file_data);
}
new_file->file_name = g_strdup(file->file_name);
new_file->display_name = custom_display_name ? custom_display_name : g_strdup(file->display_name);
new_file->sort_name = custom_sort_name ? custom_sort_name : (file->sort_name ? g_strdup(file->sort_name) : NULL);
return new_file;
}/*}}}*/
gboolean image_loader_load_single(BOSNode *node, gboolean called_from_main) {/*{{{*/
// Sanity check
assert(bostree_node_weak_unref(file_tree, bostree_node_weak_ref(node)) != NULL);
// Already loaded?
file_t *file = (file_t *)node->data;
if(file->is_loaded) {
return TRUE;
}
GError *error_pointer = NULL;
if(file->file_type->load_fn != NULL) {
// Create an input stream for the image to be loaded
GInputStream *data = image_loader_stream_file(file, &error_pointer);
if(data) {
// Let the file type handler handle the details
file->file_type->load_fn(file, data, &error_pointer);
g_object_unref(data);
}
}
if(file->is_loaded) {
if(error_pointer) {
g_printerr("A recoverable error occoured: %s\n", error_pointer->message);
g_clear_error(&error_pointer);
}
if((file->file_flags & FILE_FLAGS_MEMORY_IMAGE) == 0) {
GFile *the_file = g_file_new_for_path(file->file_name);
if(the_file != NULL) {
file->file_monitor = g_file_monitor_file(the_file, G_FILE_MONITOR_NONE, NULL, NULL);
if(file->file_monitor != NULL) {
g_signal_connect(file->file_monitor, "changed", G_CALLBACK(image_file_updated_callback), (gpointer)node);
}
g_object_unref(the_file);
}
}
// Mark the image as loaded for the GC
D_LOCK(file_tree);
loaded_files_list = g_list_prepend(loaded_files_list, bostree_node_weak_ref(node));
D_UNLOCK(file_tree);
return TRUE;
}
else {
if(error_pointer) {
if(error_pointer->code == G_IO_ERROR_CANCELLED) {
g_clear_error(&error_pointer);
return FALSE;
}
g_printerr("Failed to load image %s: %s\n", file->display_name, error_pointer->message);
g_clear_error(&error_pointer);
}
else {
if(g_cancellable_is_cancelled(image_loader_cancellable)) {
return FALSE;
}
g_printerr("Failed to load image %s: Reason unknown\n", file->display_name);
}
// The node is invalid. Unload it.
D_LOCK(file_tree);
if(node == current_file_node) {
current_file_node = next_file();
if(current_file_node == node) {
if(bostree_node_count(file_tree) > 1) {
// This can be triggered in shuffle mode if images are deleted and the end of
// a shuffle cycle is reached, such that next_file() starts a new one. Fall
// back to display the first image. See bug #35 in github.
current_file_node = bostree_node_weak_ref(bostree_select(file_tree, 0));
queue_image_load(current_file_node);
}
else {
current_file_node = NULL;
}
}
else {
current_file_node = bostree_node_weak_ref(current_file_node);
queue_image_load(current_file_node);
}
bostree_remove(file_tree, node);
bostree_node_weak_unref(file_tree, node);
}
else {
bostree_remove(file_tree, node);
}
if(!called_from_main && bostree_node_count(file_tree) == 0) {
g_printerr("No images left to display.\n");
if(gtk_main_level() == 0) {
exit(1);
}
gtk_main_quit();
}
D_UNLOCK(file_tree);
}
return FALSE;
}/*}}}*/
gpointer image_loader_thread(gpointer user_data) {/*{{{*/
while(TRUE) {
// Handle new queued image load
BOSNode *node = g_async_queue_pop(image_loader_queue);
D_LOCK(file_tree);
if(bostree_node_weak_unref(file_tree, bostree_node_weak_ref(node)) == NULL) {
bostree_node_weak_unref(file_tree, node);
D_UNLOCK(file_tree);
continue;
}
D_UNLOCK(file_tree);
// It is a hard decision whether to first load the new image or whether
// to GC the old ones first: The former minimizes I/O for multi-page
// images, the latter is better if memory is low.
// As a compromise, load the new image first unless option_lowmem is
// set. Note that a node that has force_reload set will not be loaded
// here, because it still is_loaded.
if(!option_lowmem && !FILE(node)->is_loaded) {
// Load image
image_loader_thread_currently_loading = node;
image_loader_load_single(node, FALSE);
image_loader_thread_currently_loading = NULL;
}
// Before trying to load the image, unload the old ones to free
// up memory.
// We do that here to avoid a race condition with the image loaders
D_LOCK(file_tree);
for(GList *node_list = loaded_files_list; node_list; ) {
GList *next = g_list_next(node_list);
BOSNode *loaded_node = bostree_node_weak_unref(file_tree, bostree_node_weak_ref((BOSNode *)node_list->data));
if(!loaded_node) {
bostree_node_weak_unref(file_tree, (BOSNode *)node_list->data);
loaded_files_list = g_list_delete_link(loaded_files_list, node_list);
}
else {
// If the image to be loaded has force_reload set and this has the same file name, also set force_reload
if(FILE(node)->force_reload && strcmp(FILE(node)->file_name, FILE(loaded_node)->file_name) == 0) {
FILE(loaded_node)->force_reload = TRUE;
}
if(
// Unloading due to force_reload being set on either this image
// This is required because an image can be in a filebuffer, and would thus not be reloaded even if it changed on disk.
FILE(loaded_node)->force_reload ||
// Regular unloading: The image will not be seen by the user in the foreseeable feature
(loaded_node != node && loaded_node != current_file_node && (option_lowmem || (loaded_node != previous_file() && loaded_node != next_file())))
) {
// If this node had force_reload set, we must reload it to populate the cache
if(FILE(loaded_node)->force_reload && loaded_node == node) {
queue_image_load(node);
}
unload_image(loaded_node);
// It is important to unref after unloading, because the image data structure
// might be reduced to zero if it has been deleted before!
bostree_node_weak_unref(file_tree, (BOSNode *)node_list->data);
loaded_files_list = g_list_delete_link(loaded_files_list, node_list);
}
}
node_list = next;
}
D_UNLOCK(file_tree);
// Now take care of the queued image, unless it has been loaded above
if(option_lowmem && !FILE(node)->is_loaded) {
// Load image
image_loader_thread_currently_loading = node;
image_loader_load_single(node, FALSE);
image_loader_thread_currently_loading = NULL;
}
if(node == current_file_node && FILE(node)->is_loaded) {
current_image_drawn = FALSE;
gdk_threads_add_idle((GSourceFunc)image_loaded_handler, node);
}
D_LOCK(file_tree);
bostree_node_weak_unref(file_tree, node);
D_UNLOCK(file_tree);
}
}/*}}}*/
gboolean initialize_image_loader() {/*{{{*/
if(image_loader_initialization_succeeded) {
return TRUE;
}
if(image_loader_queue == NULL) {
image_loader_queue = g_async_queue_new();
image_loader_cancellable = g_cancellable_new();
}
D_LOCK(file_tree);
if(browse_startup_node != NULL) {
current_file_node = bostree_node_weak_unref(file_tree, browse_startup_node);
browse_startup_node = NULL;
if(!current_file_node) {
current_file_node = relative_image_pointer(0);
}
}
else {
current_file_node = relative_image_pointer(0);
}
if(!current_file_node) {
D_UNLOCK(file_tree);
return FALSE;
}
current_file_node = bostree_node_weak_ref(current_file_node);
D_UNLOCK(file_tree);
while(!image_loader_load_single(current_file_node, TRUE) && bostree_node_count(file_tree) > 0) usleep(10000);
if(bostree_node_count(file_tree) == 0) {
return FALSE;
}
g_thread_new("image-loader", image_loader_thread, NULL);
if(!option_lowmem) {
D_LOCK(file_tree);
BOSNode *next = next_file();
if(!FILE(next)->is_loaded) {
queue_image_load(next);
}
BOSNode *previous = previous_file();
if(!FILE(previous)->is_loaded) {
queue_image_load(previous);
}
D_UNLOCK(file_tree);
}
image_loader_initialization_succeeded = TRUE;
return TRUE;
}/*}}}*/
void abort_pending_image_loads(BOSNode *new_pos) {/*{{{*/
BOSNode *ref;
while((ref = g_async_queue_try_pop(image_loader_queue)) != NULL) bostree_node_weak_unref(file_tree, ref);
if(image_loader_thread_currently_loading != NULL && image_loader_thread_currently_loading != new_pos) {
g_cancellable_cancel(image_loader_cancellable);
}
}/*}}}*/
void queue_image_load(BOSNode *node) {/*{{{*/
g_async_queue_push(image_loader_queue, bostree_node_weak_ref(node));
}/*}}}*/
void unload_image(BOSNode *node) {/*{{{*/
if(!node) {
return;
}
file_t *file = FILE(node);
if(file->file_type->unload_fn != NULL) {
file->file_type->unload_fn(file);
}
file->is_loaded = FALSE;
file->force_reload = FALSE;
if(file->file_monitor != NULL) {
g_file_monitor_cancel(file->file_monitor);
if(G_IS_OBJECT(file->file_monitor)) {
g_object_unref(file->file_monitor);
}
file->file_monitor = NULL;
}
}/*}}}*/
void remove_image(BOSNode *node) {/*{{{*/
D_LOCK(file_tree);
node = bostree_node_weak_unref(file_tree, node);
if(!node) {
D_UNLOCK(file_tree);
return;
}
if(node == current_file_node) {
// Cheat the image loader into thinking that the file is no longer
// available, and force a reload. This is an easy way to use the
// mechanism from the loader thread to handle this situation.
CURRENT_FILE->force_reload = TRUE;
if(CURRENT_FILE->file_name) {
CURRENT_FILE->file_name[0] = 0;
}
if(CURRENT_FILE->file_data) {
g_bytes_unref(CURRENT_FILE->file_data);
CURRENT_FILE->file_data = NULL;
}
queue_image_load(current_file_node);
}
else {
unload_image(node);
bostree_remove(file_tree, node);
}
D_UNLOCK(file_tree);
}/*}}}*/
void preload_adjacent_images() {/*{{{*/
if(!option_lowmem) {
D_LOCK(file_tree);
BOSNode *new_prev = previous_file();
BOSNode *new_next = next_file();
if(!FILE(new_next)->is_loaded) {
queue_image_load(new_next);
}
if(!FILE(new_prev)->is_loaded) {
queue_image_load(new_prev);
}
D_UNLOCK(file_tree);
}
}/*}}}*/
gboolean absolute_image_movement_still_unloaded_timer_callback(gpointer user_data) {/*{{{*/
if(user_data == (void *)current_file_node && !CURRENT_FILE->is_loaded) {
update_info_text(NULL);
gtk_widget_queue_draw(GTK_WIDGET(main_window));
}
return FALSE;
}/*}}}*/
gboolean absolute_image_movement(BOSNode *ref) {/*{{{*/
D_LOCK(file_tree);
BOSNode *node = bostree_node_weak_unref(file_tree, ref);
if(!node) {
D_UNLOCK(file_tree);
return FALSE;
}
// No need to continue the other pending loads
abort_pending_image_loads(node);
// Set the new image as current
if(current_file_node != NULL) {
bostree_node_weak_unref(file_tree, current_file_node);
}
current_file_node = bostree_node_weak_ref(node);
D_UNLOCK(file_tree);
#ifndef CONFIGURED_WITHOUT_INFO_TEXT
// If the new image has not been loaded yet, prepare to display an information message
// after some grace period
if(!CURRENT_FILE->is_loaded && !option_hide_info_box) {
gdk_threads_add_timeout(500, absolute_image_movement_still_unloaded_timer_callback, current_file_node);
}
#endif
// Load it
queue_image_load(current_file_node);
// Preload the adjacent images
preload_adjacent_images();
// Activate fading
if(option_fading) {
// It is important to initialize this variable with a positive,
// non-null value, as 0. is used to indicate that no fading currently
// takes place.
fading_current_alpha_stage = DBL_EPSILON;
// We start the clock after the first draw, because it could take some
// time to calculate the resized version of the image
fading_initial_time = -1;
gdk_threads_add_idle(fading_timeout_callback, NULL);
}
// If there is an active slideshow, interrupt it until the image has been
// drawn
if(slideshow_timeout_id > 0) {
g_source_remove(slideshow_timeout_id);
slideshow_timeout_id = 0;
}
return FALSE;
}/*}}}*/
void relative_image_pointer_shuffle_list_unref_fn(shuffled_image_ref_t *ref) {
bostree_node_weak_unref(file_tree, ref->node);
g_slice_free(shuffled_image_ref_t, ref);
}
gint relative_image_pointer_shuffle_list_cmp(shuffled_image_ref_t *ref, BOSNode *node) {
if(node == ref->node) return 0;
return 1;
}
shuffled_image_ref_t *relative_image_pointer_shuffle_list_create(BOSNode *node) {
assert(node != NULL);
shuffled_image_ref_t *retval = g_slice_new(shuffled_image_ref_t);
retval->node = bostree_node_weak_ref(node);
retval->viewed = FALSE;
return retval;
}
BOSNode *image_pointer_by_name(gchar *display_name) {/*{{{*/
// Obtain a pointer to the image that has a given display_name
// Note that this is only fast (O(log n)) if the file tree is sorted,
// elsewise a linear search is used!
if(option_sort && option_sort_key == NAME) {
return bostree_lookup(file_tree, display_name);
}
else {
for(BOSNode *iter = bostree_select(file_tree, 0); iter; iter = bostree_next_node(iter)) {
if(strcasecmp(FILE(iter)->display_name, display_name) == 0) {
return iter;
}
}
return NULL;
}
}/*}}}*/
BOSNode *relative_image_pointer(ptrdiff_t movement) {/*{{{*/
// Obtain a pointer to the image that is +movement away from the current image
// This function behaves differently depending on whether shuffle mode is
// enabled. It implements the actual shuffling.
// It does not lock the file tree. This should be done before calling this
// function. Also, it does not return a weak reference to the found node.
//
size_t count = bostree_node_count(file_tree);
if(option_shuffle) {
#if 0
// Output some debug info
GList *aa = g_list_find_custom(shuffled_images_list, current_file_node, (GCompareFunc)relative_image_pointer_shuffle_list_cmp);
g_print("Current shuffle list: ");
for(GList *e = g_list_first(shuffled_images_list); e; e = e->next) {
BOSNode *n = bostree_node_weak_unref(file_tree, bostree_node_weak_ref(LIST_SHUFFLED_IMAGE(e)->node));
if(n) {
if(e == aa) {
g_print("*%02d* ", bostree_rank(n)+1);
}
else {
g_print(" %02d ", bostree_rank(n)+1);
}
}
else {
g_print(" ?? ");
}
}
g_print("\n");
#endif
// First, check if the relative movement is already possible within the existing list
GList *current_shuffled_image = g_list_find_custom(shuffled_images_list, current_file_node, (GCompareFunc)relative_image_pointer_shuffle_list_cmp);
if(!current_shuffled_image) {
current_shuffled_image = g_list_last(shuffled_images_list);
// This also happens if the user switched off random mode, moved a
// little, and reenabled it. The image that the user saw last is,
// expect if lowmem is used, the 2nd last in the list, because
// there already is a preloaded next one. Correct that.
if(!option_lowmem && current_shuffled_image && g_list_previous(current_shuffled_image)) {
current_shuffled_image = g_list_previous(current_shuffled_image);
}
}
if(movement > 0) {
while(movement && g_list_next(current_shuffled_image)) {
current_shuffled_image = g_list_next(current_shuffled_image);
movement--;
}
}
else if(movement < 0) {
while(movement && g_list_previous(current_shuffled_image)) {
current_shuffled_image = g_list_previous(current_shuffled_image);
movement++;
}
}
// The list isn't long enough to provide us with the desired image.
if(shuffled_images_list_length < bostree_node_count(file_tree)) {
// If not all images have been viewed, expand it
while(movement != 0) {
BOSNode *next_candidate, *chosen_candidate;
// We select one random list element and then choose the sequentially next
// until we find one that has not been chosen yet. Walking sequentially
// after chosing one random integer index still generates a
// equidistributed permutation.
// This is O(n^2), since we must in the worst case lookup n-1 elements
// in a list of already chosen ones, but I think that this still is a
// better choice than to store an additional boolean in each file_t,
// which would make this O(n).
next_candidate = chosen_candidate = bostree_select(file_tree, g_random_int_range(0, count));
if(!next_candidate) {
// All images have gone.
return current_file_node;
}
while(g_list_find_custom(shuffled_images_list, next_candidate, (GCompareFunc)relative_image_pointer_shuffle_list_cmp)) {
next_candidate = bostree_next_node(next_candidate);
if(!next_candidate) {
next_candidate = bostree_select(file_tree, 0);
}
if(next_candidate == chosen_candidate) {
// This ought not happen :/
g_warn_if_reached();
current_shuffled_image = NULL;
movement = 0;
}
}
// If this is the start of a cycle and the current image has
// been selected again by chance, jump one image ahead.
if((shuffled_images_list == NULL || shuffled_images_list->data == NULL) && next_candidate == current_file_node && bostree_node_count(file_tree) > 1) {
next_candidate = bostree_next_node(next_candidate);
if(!next_candidate) {
next_candidate = bostree_select(file_tree, 0);
}
}
if(movement > 0) {
shuffled_images_list = g_list_append(shuffled_images_list, relative_image_pointer_shuffle_list_create(next_candidate));
movement--;
shuffled_images_list_length++;
current_shuffled_image = g_list_last(shuffled_images_list);
}
else if(movement < 0) {
shuffled_images_list = g_list_prepend(shuffled_images_list, relative_image_pointer_shuffle_list_create(next_candidate));
movement++;
shuffled_images_list_length++;
current_shuffled_image = g_list_first(shuffled_images_list);
}
}
}
else {
// If all images have been used, wrap around the list's end
while(movement) {
current_shuffled_image = movement > 0 ? g_list_first(shuffled_images_list) : g_list_last(shuffled_images_list);
movement = movement > 0 ? movement - 1 : movement + 1;
if(movement > 0) {
while(movement && g_list_next(current_shuffled_image)) {
current_shuffled_image = g_list_next(current_shuffled_image);
movement--;
}
}
else if(movement < 0) {
while(movement && g_list_previous(current_shuffled_image)) {
current_shuffled_image = g_list_previous(current_shuffled_image);
movement++;
}
}
}
}
if(!current_shuffled_image) {
// Either the list was empty, or something went horribly wrong. Restart over.
BOSNode *chosen_candidate = bostree_select(file_tree, g_random_int_range(0, count));
if(!chosen_candidate) {
// All images have gone.
return current_file_node;
}
g_list_free_full(shuffled_images_list, (GDestroyNotify)relative_image_pointer_shuffle_list_unref_fn);
shuffled_images_list = g_list_append(NULL, relative_image_pointer_shuffle_list_create(chosen_candidate));
shuffled_images_visited_count = 0;
shuffled_images_list_length = 1;
return chosen_candidate;
}
// We found an image. Dereference the weak reference, and walk the list until a valid reference
// is found if it is invalid, removing all invalid references along the way.
BOSNode *image = bostree_node_weak_unref(file_tree, bostree_node_weak_ref(LIST_SHUFFLED_IMAGE(current_shuffled_image)->node));
while(!image && shuffled_images_list) {
GList *new_shuffled_image = g_list_next(current_shuffled_image);
shuffled_images_list_length--;
if(LIST_SHUFFLED_IMAGE(current_shuffled_image)->viewed) {
shuffled_images_visited_count--;
}
relative_image_pointer_shuffle_list_unref_fn(LIST_SHUFFLED_IMAGE(current_shuffled_image));
shuffled_images_list = g_list_delete_link(shuffled_images_list, current_shuffled_image);
current_shuffled_image = new_shuffled_image ? new_shuffled_image : g_list_last(shuffled_images_list);
if(current_shuffled_image) {
image = bostree_node_weak_unref(file_tree, bostree_node_weak_ref(LIST_SHUFFLED_IMAGE(current_shuffled_image)->node));
}
else {
// All images have gone. This _is_ a problem, and should not
// happen. pqiv will likely exit. But return the current image,
// just to be sure that nothing breaks.
g_warn_if_reached();
return current_file_node;
}
}
return image;
}
else {
// Sequential movement. This is the simple stuff:
if(movement == 0) {
// Only used for initialization, current_file_node might be 0
return current_file_node ? current_file_node : bostree_select(file_tree, 0);
}
else if(movement == 1) {
BOSNode *ret = bostree_next_node(current_file_node);
return ret ? ret : bostree_select(file_tree, 0);
}
else if(movement == -1) {
BOSNode *ret = bostree_previous_node(current_file_node);
return ret ? ret : bostree_select(file_tree, bostree_node_count(file_tree) - 1);
}
else {
ptrdiff_t pos = bostree_rank(current_file_node) + movement;
while(pos < 0) {
pos += count;
}
pos %= count;
return bostree_select(file_tree, pos);
}
}
}/*}}}*/
void relative_image_movement(ptrdiff_t movement) {/*{{{*/
// Calculate new position
D_LOCK(file_tree);
BOSNode *target = bostree_node_weak_ref(relative_image_pointer(movement));
D_UNLOCK(file_tree);
// Check if this movement is allowed
if((option_shuffle && shuffled_images_visited_count == bostree_node_count(file_tree)) ||
(!option_shuffle && movement > 0 && bostree_rank(target) <= bostree_rank(current_file_node))) {
if(option_end_of_files_action == QUIT) {
bostree_node_weak_unref(file_tree, target);
gtk_main_quit();
}
else if(option_end_of_files_action == WAIT) {
bostree_node_weak_unref(file_tree, target);
return;
}
}
// Only perform the movement if the file actually changed.
// Important for slideshows if only one file was available and said file has been deleted.
if(movement == 0 || target != current_file_node) {
absolute_image_movement(target);
}
else {
bostree_node_weak_unref(file_tree, target);
// If a slideshow called relative_image_movement, it has already stopped the slideshow
// callback at this point. It might be that target == current_file_node because the
// old slideshow cycle ended, and the new one started off with the same image.
// Reinitialize the slideshow in that case.
if(slideshow_timeout_id == 0) {
slideshow_timeout_id = gdk_threads_add_timeout(option_slideshow_interval * 1000, slideshow_timeout_callback, NULL);
}
}
}/*}}}*/
BOSNode *directory_image_movement_find_different_directory(BOSNode *current, int direction) {/*{{{*/
// Return a reference to the first image with a different directory than current
// when searching in direction direction (-1 or 1)
//
// This function does not perform any locking!
BOSNode *target = current;
if(FILE(current)->file_flags & FILE_FLAGS_MEMORY_IMAGE) {
target = direction > 0 ? bostree_next_node(target) : bostree_previous_node(target);
if(!target) {
target = direction > 0 ? bostree_select(file_tree, 0) : bostree_select(file_tree, bostree_node_count(file_tree) - 1);
}
}
else {
while(TRUE) {
// Select next image
target = direction > 0 ? bostree_next_node(target) : bostree_previous_node(target);
if(!target) {
target = direction > 0 ? bostree_select(file_tree, 0) : bostree_select(file_tree, bostree_node_count(file_tree) - 1);
}
// Check for special abort conditions: Again at first image (no different directory found),
// or memory image
if(target == current || (FILE(target)->file_flags & FILE_FLAGS_MEMORY_IMAGE)) {
break;
}
// Check if the directory changed. If it did, abort the search.
// Search for the first byte where the file names differ
unsigned int pos = 0;
while(FILE(target)->file_name[pos] && FILE(current)->file_name[pos] && FILE(target)->file_name[pos] == FILE(current)->file_name[pos]) {
pos++;
}
// The path changed if either
// * the target file name contains a slash at or after pos
// (e.g. current -> ./foo/bar.png, target -> ./foo2/baz.png)
// * the current file name contains a slash at or after pos
// (e.g. current -> ./foo/bar.png, target -> ./baz.png
gboolean directory_changed = FALSE;
for(unsigned int i=pos; FILE(target)->file_name[i]; i++) {
if(FILE(target)->file_name[i] == G_DIR_SEPARATOR) {
// Gotcha.
directory_changed = TRUE;
break;
}
}
if(!directory_changed) {
for(unsigned int i=pos; FILE(current)->file_name[i]; i++) {
if(FILE(current)->file_name[i] == G_DIR_SEPARATOR) {
directory_changed = TRUE;
break;
}
}
}
if(directory_changed) {
break;
}
}
}
return target;
}/*}}}*/
void directory_image_movement(int direction) {/*{{{*/
// Directory movement
//
// This should be consistent, i.e. movements in different directions should
// be inverse operations of each other. This makes this function slightly
// complex.
D_LOCK(file_tree);
BOSNode *target;
BOSNode *current = current_file_node;
if(direction == 1) {
// Forward searches are trivial
target = directory_image_movement_find_different_directory(current, 1);
}
else {
// Bardward searches are more involved, because we want to end up at the first image
// of the previous directory, not at the last one. The trick is to
// search backwards twice and then again go forward by one image.
target = directory_image_movement_find_different_directory(current, -1);
target = directory_image_movement_find_different_directory(target, -1);
if(target != current) {
target = bostree_next_node(target);
if(!target) {
target = bostree_select(file_tree, 0);
}
}
}
target = bostree_node_weak_ref(target);
D_UNLOCK(file_tree);
absolute_image_movement(target);
}/*}}}*/
void transform_current_image(cairo_matrix_t *transformation) {/*{{{*/
// Apply the transformation to the transformation matrix
cairo_matrix_t operand = current_transformation;
cairo_matrix_multiply(¤t_transformation, &operand, transformation);
// Resize and queue a redraw
main_window_adjust_for_image();
invalidate_current_scaled_image_surface();
gtk_widget_queue_draw(GTK_WIDGET(main_window));
}/*}}}*/
#ifndef CONFIGURED_WITHOUT_EXTERNAL_COMMANDS /* option --without-external-commands: Do not include support for calling external programs */
gchar *apply_external_image_filter_prepare_command(gchar *command) { /*{{{*/
D_LOCK(file_tree);
if((CURRENT_FILE->file_flags & FILE_FLAGS_MEMORY_IMAGE) != 0) {
D_UNLOCK(file_tree);
return g_strdup(command);
}
gchar *quoted = g_shell_quote(CURRENT_FILE->file_name);
D_UNLOCK(file_tree);
gchar *ins_pos;
gchar *retval;
if((ins_pos = g_strrstr(command, "$1")) != NULL) {
retval = (gchar*)g_malloc(strlen(command) + strlen(quoted) + 2);
memcpy(retval, command, ins_pos - command);
sprintf(retval + (ins_pos - command), "%s%s", quoted, ins_pos + 2);
}
else {
retval = (gchar*)g_malloc(strlen(command) + 2 + strlen(quoted));
sprintf(retval, "%s %s", command, quoted);
}
g_free(quoted);
return retval;
} /*}}}*/
gboolean window_key_press_close_handler_callback(GtkWidget *widget, GdkEventKey *event, gpointer user_data) {/*{{{*/
if(event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_Escape || event->keyval == GDK_KEY_q || event->keyval == GDK_KEY_Q) {
gtk_widget_destroy(widget);
}
return FALSE;
}/*}}}*/
gboolean apply_external_image_filter_show_output_window(gpointer text) {/*{{{*/
GtkWidget *output_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(output_window), "Command output");
gtk_window_set_position(GTK_WINDOW(output_window), GTK_WIN_POS_CENTER_ON_PARENT);
gtk_window_set_modal(GTK_WINDOW(output_window), TRUE);
gtk_window_set_destroy_with_parent(GTK_WINDOW(output_window), TRUE);
gtk_window_set_type_hint(GTK_WINDOW(output_window), GDK_WINDOW_TYPE_HINT_DIALOG);
gtk_widget_set_size_request(output_window, 400, 480);
g_signal_connect(output_window, "key-press-event",
G_CALLBACK(window_key_press_close_handler_callback), NULL);
GtkWidget *output_scroller = gtk_scrolled_window_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER(output_window), output_scroller);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(output_scroller),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
GtkWidget *output_window_text = gtk_text_view_new();
gtk_container_add(GTK_CONTAINER(output_scroller), output_window_text);
gtk_text_view_set_editable(GTK_TEXT_VIEW(output_window_text), FALSE);
gsize output_text_length;
gchar *output_text = g_locale_to_utf8((gchar*)text, strlen((gchar*)text), NULL, &output_text_length, NULL);
gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(output_window_text)),
output_text, output_text_length);
g_free(output_text);
gtk_widget_show_all(output_window);
g_free(text);
return FALSE;
}/*}}}*/
cairo_status_t apply_external_image_filter_thread_callback(void *closure, const unsigned char *data, unsigned int length) {/*{{{*/
if(write(*(gint *)closure, data, length) == -1) {
return CAIRO_STATUS_WRITE_ERROR;
}
else {
return CAIRO_STATUS_SUCCESS;
}
}/*}}}*/
gpointer apply_external_image_filter_image_writer_thread(gpointer data) {/*{{{*/
D_LOCK(file_tree);
cairo_surface_t *surface = get_scaled_image_surface_for_current_image();
if(!surface) {
D_UNLOCK(file_tree);
close(*(gint *)data);
return NULL;
}
D_UNLOCK(file_tree);
cairo_surface_write_to_png_stream(surface, apply_external_image_filter_thread_callback, data);
close(*(gint *)data);
cairo_surface_destroy(surface);
return NULL;
}/*}}}*/
void apply_external_image_filter(gchar *external_filter) {/*{{{*/
gchar *argv[4];
argv[0] = (gchar*)"/bin/sh"; // Ok: These are not changed below
argv[1] = (gchar*)"-c";
argv[3] = 0;
GError *error_pointer = NULL;
if(external_filter[0] == '>') {
// Pipe stdout into a new window
argv[2] = apply_external_image_filter_prepare_command(external_filter + 1);
gchar *child_stdout = NULL;
gchar *child_stderr = NULL;
if(g_spawn_sync(NULL, argv, NULL, 0, NULL, NULL, &child_stdout, &child_stderr, NULL, &error_pointer) == FALSE) {
g_printerr("Failed execute external command `%s': %s\n", argv[2], error_pointer->message);
g_clear_error(&error_pointer);
}
else {
g_print("%s", child_stderr);
g_free(child_stderr);
gdk_threads_add_idle(apply_external_image_filter_show_output_window, child_stdout);
}
// Reminder: Do not free the others, they are string constants
g_free(argv[2]);
}
else if(external_filter[0] == '|') {
// Pipe image into program, read image from its stdout
argv[2] = external_filter + 1;
GPid child_pid;
gint child_stdin;
gint child_stdout;
BOSNode *current_file_node_at_start = bostree_node_weak_ref(current_file_node);
if(!g_spawn_async_with_pipes(NULL, argv, NULL,
// In win32, the G_SPAWN_DO_NOT_REAP_CHILD is required to get the process handle
#ifdef _WIN32
G_SPAWN_DO_NOT_REAP_CHILD,
#else
0,
#endif
NULL, NULL, &child_pid, &child_stdin, &child_stdout, NULL, &error_pointer)
) {
g_printerr("Failed execute external command `%s': %s\n", argv[2], error_pointer->message);
g_clear_error(&error_pointer);
}
else {
g_thread_new("image-filter-writer", apply_external_image_filter_image_writer_thread, &child_stdin);
gchar *image_data;
gsize image_data_length;
GIOChannel *stdin_channel = g_io_channel_unix_new(child_stdout);
g_io_channel_set_encoding(stdin_channel, NULL, NULL);
if(g_io_channel_read_to_end(stdin_channel, &image_data, &image_data_length, &error_pointer) != G_IO_STATUS_NORMAL) {
g_printerr("Failed to load image from external command: %s\n", error_pointer->message);
g_clear_error(&error_pointer);
}
else {
gint status;
#ifdef _WIN32
WaitForSingleObject(child_pid, INFINITE);
DWORD exit_code = 0;
GetExitCodeProcess(child_pid, &exit_code);
status = (gint)exit_code;
#else
waitpid(child_pid, &status, 0);
#endif
g_spawn_close_pid(child_pid);
if(current_file_node_at_start != current_file_node) {
// The user navigated away from this image. Abort.
g_free(image_data);
}
else if(status != 0) {
g_printerr("External command failed with exit status %d\n", status);
g_free(image_data);
}
else {
// We now have a new image in memory in the char buffer image_data. Construct a new file
// for the result, and load it
//
file_t *new_image = g_slice_new0(file_t);
new_image->display_name = g_strdup_printf("%s [Output of `%s`]", CURRENT_FILE->display_name, argv[2]);
if(option_sort) {
new_image->sort_name = g_strdup_printf("%s;%s", CURRENT_FILE->sort_name, argv[2]);
}
new_image->file_name = g_strdup("-");
new_image->file_type = &file_type_handlers[0];
new_image->file_flags = FILE_FLAGS_MEMORY_IMAGE;
new_image->file_data = g_bytes_new_take(image_data, image_data_length);
BOSNode *loaded_file = new_image->file_type->alloc_fn(FILTER_OUTPUT, new_image);
absolute_image_movement(bostree_node_weak_ref(loaded_file));
}
}
g_io_channel_unref(stdin_channel);
}
}
else {
// Plain system() call
argv[2] = apply_external_image_filter_prepare_command(external_filter);
if(g_spawn_async(NULL, argv, NULL, 0, NULL, NULL, NULL, &error_pointer) == FALSE) {
g_printerr("Failed execute external command `%s': %s\n", argv[2], error_pointer->message);
g_clear_error(&error_pointer);
}
g_free(argv[2]);
}
}/*}}}*/
gpointer apply_external_image_filter_thread(gpointer external_filter_ptr) {/*{{{*/
apply_external_image_filter((gchar *)external_filter_ptr);
g_free(external_filter_ptr);
return NULL;
}/*}}}*/
#endif
void hardlink_current_image() {/*{{{*/
BOSNode *the_file = bostree_node_weak_ref(current_file_node);
if((FILE(the_file)->file_flags & FILE_FLAGS_MEMORY_IMAGE) != 0) {
g_mkdir("./.pqiv-select", 0755);
gchar *store_target = NULL;
do {
if(store_target != NULL) {
g_free(store_target);
}
#if(GLIB_CHECK_VERSION(2, 28, 0))
store_target = g_strdup_printf("./.pqiv-select/memory-%" G_GINT64_FORMAT "-%u.png", g_get_real_time(), g_random_int());
#else
store_target = g_strdup_printf("./.pqiv-select/memory-%u.png", g_random_int());
#endif
}
while(g_file_test(store_target, G_FILE_TEST_EXISTS));
cairo_surface_t *surface = get_scaled_image_surface_for_current_image();
if(surface) {
if(cairo_surface_write_to_png(surface, store_target) == CAIRO_STATUS_SUCCESS) {
UPDATE_INFO_TEXT("Stored what you see into %s", store_target);
}
else {
update_info_text("Failed to write to the .pqiv-select subdirectory");
}
cairo_surface_destroy(surface);
}
g_free(store_target);
bostree_node_weak_unref(file_tree, the_file);
return;
}
gchar *current_file_basename = g_path_get_basename(FILE(the_file)->file_name);
gchar *link_target = g_strdup_printf("./.pqiv-select/%s", current_file_basename);
if(g_file_test(link_target, G_FILE_TEST_EXISTS)) {
g_free(link_target);
g_free(current_file_basename);
update_info_text("File already exists in .pqiv-select");
gtk_widget_queue_draw(GTK_WIDGET(main_window));
bostree_node_weak_unref(file_tree, the_file);
return;
}
g_mkdir("./.pqiv-select", 0755);
if(
#ifdef _WIN32
CreateHardLink(link_target, FILE(the_file)->file_name, NULL) == 0
#else
link(FILE(the_file)->file_name, link_target) != 0
#endif
) {
gchar *dot = g_strrstr(link_target, ".");
if(dot != NULL && dot > link_target + 2) {
*dot = 0;
}
gchar *store_target = g_strdup_printf("%s.png", link_target);
cairo_surface_t *surface = get_scaled_image_surface_for_current_image();
if(surface) {
if(cairo_surface_write_to_png(surface, store_target) == CAIRO_STATUS_SUCCESS) {
UPDATE_INFO_TEXT("Failed to link file, but stored what you see into %s", store_target);
}
else {
update_info_text("Failed to write to the .pqiv-select subdirectory");
}
cairo_surface_destroy(surface);
}
g_free(store_target);
}
else {
update_info_text("Created hard-link into .pqiv-select");
}
g_free(link_target);
g_free(current_file_basename);
bostree_node_weak_unref(file_tree, the_file);
gtk_widget_queue_draw(GTK_WIDGET(main_window));
}/*}}}*/
gboolean slideshow_timeout_callback(gpointer user_data) {/*{{{*/
// Always abort this source: The clock will run again as soon as the image has been loaded.
// The draw callback addes a new timeout if we set the timeout id to zero:
slideshow_timeout_id = 0;
relative_image_movement(1);
return FALSE;
}/*}}}*/
gboolean fading_timeout_callback(gpointer user_data) {/*{{{*/
if(fading_initial_time < 0) {
// We just started. Leave the image invisible.
gtk_widget_queue_draw(GTK_WIDGET(main_window));
return TRUE;
}
double new_stage = (g_get_monotonic_time() - fading_initial_time) / (1e6 * option_fading_duration);
new_stage = (new_stage < 0.) ? 0. : ((new_stage > 1.) ? 1. : new_stage);
fading_current_alpha_stage = new_stage;
gtk_widget_queue_draw(GTK_WIDGET(main_window));
return (fading_current_alpha_stage < 1.); // FALSE aborts the source
}/*}}}*/
void calculate_current_image_transformed_size(int *image_width, int *image_height) {/*{{{*/
double transform_width = (double)CURRENT_FILE->width;
double transform_height = (double)CURRENT_FILE->height;
cairo_matrix_transform_distance(¤t_transformation, &transform_width, &transform_height);
*image_width = (int)fabs(transform_width);
*image_height = (int)fabs(transform_height);
}/*}}}*/
void draw_current_image_to_context(cairo_t *cr) {/*{{{*/
if(CURRENT_FILE->file_type->draw_fn != NULL) {
CURRENT_FILE->file_type->draw_fn(CURRENT_FILE, cr);
}
}/*}}}*/
void setup_checkerboard_pattern() {/*{{{*/
// Create pattern
cairo_surface_t *surface;
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 16, 16);
cairo_t *ccr = cairo_create(surface);
cairo_set_source_rgba(ccr, .5, .5, .5, 1.);
cairo_paint(ccr);
cairo_set_source_rgba(ccr, 1., 1., 1., 1.);
cairo_rectangle(ccr, 0, 0, 8, 8);
cairo_fill(ccr);
cairo_rectangle(ccr, 8, 8, 16, 16);
cairo_fill(ccr);
cairo_destroy(ccr);
background_checkerboard_pattern = cairo_pattern_create_for_surface(surface);
cairo_surface_destroy(surface);
cairo_pattern_set_extend(background_checkerboard_pattern, CAIRO_EXTEND_REPEAT);
cairo_pattern_set_filter(background_checkerboard_pattern, CAIRO_FILTER_NEAREST);
}/*}}}*/
cairo_surface_t *get_scaled_image_surface_for_current_image() {/*{{{*/
if(current_scaled_image_surface != NULL) {
return cairo_surface_reference(current_scaled_image_surface);
}
if(!CURRENT_FILE->is_loaded) {
return NULL;
}
cairo_surface_t *retval = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, current_scale_level * CURRENT_FILE->width + .5, current_scale_level * CURRENT_FILE->height + .5);
if(cairo_surface_status(retval) != CAIRO_STATUS_SUCCESS) {
cairo_surface_destroy(retval);
return NULL;
}
cairo_t *cr = cairo_create(retval);
cairo_scale(cr, current_scale_level, current_scale_level);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
draw_current_image_to_context(cr);
cairo_destroy(cr);
if(!option_lowmem) {
current_scaled_image_surface = cairo_surface_reference(retval);
}
return retval;
}/*}}}*/
static void status_output() {/*{{{*/
#ifndef CONFIGURED_WITHOUT_ACTIONS
if(!option_status_output) {
return;
}
D_LOCK(file_tree);
if(file_tree_valid && current_file_node) {
printf("CURRENT_FILE_NAME=\"%s\"\nCURRENT_FILE_INDEX=%d\n\n", CURRENT_FILE->file_name, bostree_rank(current_file_node));
}
D_UNLOCK(file_tree);
#endif
}/*}}}*/
// }}}
/* Jump dialog {{{ */
#ifndef CONFIGURED_WITHOUT_JUMP_DIALOG /* option --without-jump-dialog: Do not build with -j support */
gboolean jump_dialog_search_list_filter_callback(GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data) { /* {{{ */
/**
* List filter function for the jump dialog
*/
gchar *entry_text = (gchar*)gtk_entry_get_text(GTK_ENTRY(user_data));
if(entry_text[0] == 0) {
return TRUE;
}
gboolean retval;
if(entry_text[0] == '#') {
ssize_t desired_index = atoi(&entry_text[1]);
GValue col_data;
memset(&col_data, 0, sizeof(GValue));
gtk_tree_model_get_value(model, iter, 0, &col_data);
retval = g_value_get_long(&col_data) == desired_index;
g_value_unset(&col_data);
}
else {
entry_text = g_ascii_strdown(entry_text, -1);
GValue col_data;
memset(&col_data, 0, sizeof(GValue));
gtk_tree_model_get_value(model, iter, 1, &col_data);
gchar *compare_in = (char*)g_value_get_string(&col_data);
compare_in = g_ascii_strdown(compare_in, -1);
retval = (g_strstr_len(compare_in, -1, entry_text) != NULL);
g_free(compare_in);
g_value_unset(&col_data);
g_free(entry_text);
}
return retval;
} /* }}} */
gint jump_dialog_entry_changed_callback(GtkWidget *entry, gpointer user_data) { /*{{{*/
/**
* Refilter the list when the entry text is changed
*/
gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(gtk_tree_view_get_model(GTK_TREE_VIEW(user_data))));
GtkTreeIter iter;
memset(&iter, 0, sizeof(GtkTreeIter));
if(gtk_tree_model_get_iter_first(gtk_tree_view_get_model(GTK_TREE_VIEW(user_data)), &iter)) {
gtk_tree_selection_select_iter(gtk_tree_view_get_selection(GTK_TREE_VIEW(user_data)), &iter);
}
return FALSE;
} /* }}} */
gint jump_dialog_exit_on_enter_callback(GtkWidget *widget, GdkEventKey *event, gpointer user_data) { /*{{{*/
/**
* If return is pressed exit the dialog
*/
if(event->keyval == GDK_KEY_Return) {
gtk_dialog_response(GTK_DIALOG(user_data), GTK_RESPONSE_ACCEPT);
return TRUE;
}
return FALSE;
} /* }}} */
gint jump_dialog_exit_on_dbl_click_callback(GtkWidget *widget, GdkEventButton *event, gpointer user_data) { /*{{{*/
/**
* If the user doubleclicks into the list box, exit
* the dialog
*/
if(event->button == 1 && event->type == GDK_2BUTTON_PRESS) {
gtk_dialog_response(GTK_DIALOG(user_data), GTK_RESPONSE_ACCEPT);
return TRUE;
}
return FALSE;
} /* }}} */
void jump_dialog_open_image_callback(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer user_data) { /* {{{ */
/**
* "for each" function for the list of the jump dialog
* (there can't be more than one selected image)
* Loads the image
*/
GValue col_data;
memset(&col_data, 0, sizeof(GValue));
gtk_tree_model_get_value(model, iter, 2, &col_data);
BOSNode *jump_to = (BOSNode *)g_value_get_pointer(&col_data);
g_value_unset(&col_data);
g_idle_add((GSourceFunc)absolute_image_movement, bostree_node_weak_ref(jump_to));
} /* }}} */
void do_jump_dialog() { /* {{{ */
/**
* Show the jump dialog to jump directly
* to an image
*/
GtkTreeIter search_list_iter;
// For the duration of the dialog, inhibit the input action chain from
// continuing
block_active_input_event_action_chain();
// If in fullscreen, show the cursor again
if(main_window_in_fullscreen) {
window_center_mouse();
window_show_cursor();
}
// Create dialog box
GtkWidget *dlg_window = gtk_dialog_new_with_buttons("pqiv: Jump to image",
main_window,
GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL,
"_OK",
GTK_RESPONSE_ACCEPT,
NULL);
GtkWidget *search_entry = gtk_entry_new();
gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dlg_window))),
search_entry,
FALSE,
TRUE,
0);
// Build list for searching
GtkListStore *search_list = gtk_list_store_new(3, G_TYPE_LONG, G_TYPE_STRING, G_TYPE_POINTER);
size_t id = 1;
D_LOCK(file_tree);
for(BOSNode *node = bostree_select(file_tree, 0); node; node = bostree_next_node(node)) {
gtk_list_store_append(search_list, &search_list_iter);
gtk_list_store_set(search_list, &search_list_iter,
0, id++,
1, FILE(node)->display_name,
2, bostree_node_weak_ref(node),
-1);
}
D_UNLOCK(file_tree);
GtkTreeModel *search_list_filter = gtk_tree_model_filter_new(GTK_TREE_MODEL(search_list), NULL);
gtk_tree_model_filter_set_visible_func(GTK_TREE_MODEL_FILTER(search_list_filter),
jump_dialog_search_list_filter_callback,
search_entry,
NULL);
// Create tree view
GtkWidget *search_list_box = gtk_tree_view_new_with_model(GTK_TREE_MODEL(search_list_filter));
gtk_tree_view_set_search_column(GTK_TREE_VIEW(search_list_box), 0);
gtk_tree_view_set_enable_search(GTK_TREE_VIEW(search_list_box), TRUE);
GtkCellRenderer *search_list_renderer_0 = gtk_cell_renderer_text_new();
gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(search_list_box),
-1,
"#",
search_list_renderer_0,
"text", 0,
NULL);
GtkCellRenderer *search_list_renderer_1 = gtk_cell_renderer_text_new();
gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(search_list_box),
-1,
"File name",
search_list_renderer_1,
"text", 1,
NULL);
GtkWidget *scroll_bar = gtk_scrolled_window_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER(scroll_bar),
search_list_box);
gtk_box_pack_end(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dlg_window))),
scroll_bar,
TRUE,
TRUE,
0);
// Jump to active image
GtkTreePath *goto_active_path = gtk_tree_path_new_from_indices(bostree_rank(current_file_node), -1);
gtk_tree_selection_select_path(
gtk_tree_view_get_selection(GTK_TREE_VIEW(search_list_box)),
goto_active_path);
gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(search_list_box),
goto_active_path,
NULL,
FALSE, 0, 0);
gtk_tree_path_free(goto_active_path);
// Show dialog
g_signal_connect(search_entry, "changed", G_CALLBACK(jump_dialog_entry_changed_callback), search_list_box);
g_signal_connect(search_entry, "key-press-event", G_CALLBACK(jump_dialog_exit_on_enter_callback), dlg_window);
g_signal_connect(search_list_box, "key-press-event", G_CALLBACK(jump_dialog_exit_on_enter_callback), dlg_window);
g_signal_connect(search_list_box, "button-press-event", G_CALLBACK(jump_dialog_exit_on_dbl_click_callback), dlg_window);
gtk_widget_set_size_request(dlg_window, 640, 480);
gtk_widget_show_all(dlg_window);
if(gtk_dialog_run(GTK_DIALOG(dlg_window)) == GTK_RESPONSE_ACCEPT) {
gtk_tree_selection_selected_foreach(
gtk_tree_view_get_selection(GTK_TREE_VIEW(search_list_box)),
jump_dialog_open_image_callback,
NULL);
}
// Free the references again
GtkTreeIter iter;
memset(&iter, 0, sizeof(GtkTreeIter));
if(gtk_tree_model_get_iter_first(gtk_tree_view_get_model(GTK_TREE_VIEW(search_list_box)), &iter)) {
GValue col_data;
memset(&col_data, 0, sizeof(GValue));
gtk_tree_model_get_value(GTK_TREE_MODEL(search_list_filter), &iter, 2, &col_data);
bostree_node_weak_unref(file_tree, (BOSNode *)g_value_get_pointer(&col_data));
g_value_unset(&col_data);
}
if(main_window_in_fullscreen) {
window_hide_cursor();
}
gtk_widget_destroy(dlg_window);
g_object_unref(search_list);
g_object_unref(search_list_filter);
unblock_active_input_event_action_chain();
} /* }}} */
#endif
// }}}
/* Main window functions {{{ */
void window_fullscreen() {/*{{{*/
// Bugfix for Awesome WM: If hints are active, windows are fullscreen'ed honoring the aspect ratio
if(option_enforce_window_aspect_ratio) {
gtk_window_set_geometry_hints(main_window, NULL, NULL, 0);
}
#ifndef _WIN32
if(!wm_supports_fullscreen) {
// WM does not support _NET_WM_ACTION_FULLSCREEN or no WM present
main_window_in_fullscreen = TRUE;
gtk_window_move(main_window, screen_geometry.x, screen_geometry.y);
gtk_window_resize(main_window, screen_geometry.width, screen_geometry.height);
window_state_into_fullscreen_actions();
return;
}
#endif
gtk_window_fullscreen(main_window);
}/*}}}*/
void window_unfullscreen() {/*{{{*/
#ifndef _WIN32
if(!wm_supports_fullscreen) {
// WM does not support _NET_WM_ACTION_FULLSCREEN or no WM present
main_window_in_fullscreen = FALSE;
window_state_out_of_fullscreen_actions();
return;
}
#endif
gtk_window_unfullscreen(main_window);
}/*}}}*/
inline void queue_draw() {/*{{{*/
if(!current_image_drawn) {
gtk_widget_queue_draw(GTK_WIDGET(main_window));
}
}/*}}}*/
#ifndef CONFIGURED_WITHOUT_INFO_TEXT /* option --without-info-text: Build without support for the info text */
inline void info_text_queue_redraw() {/*{{{*/
if(!option_hide_info_box) {
gtk_widget_queue_draw_area(GTK_WIDGET(main_window),
current_info_text_bounding_box.x,
current_info_text_bounding_box.y,
current_info_text_bounding_box.width,
current_info_text_bounding_box.height
);
}
}/*}}}*/
void update_info_text(const gchar *action) {/*{{{*/
D_LOCK(file_tree);
gchar *file_name;
if((CURRENT_FILE->file_flags & FILE_FLAGS_MEMORY_IMAGE) != 0) {
file_name = g_strdup_printf("-");
}
else {
file_name = g_strdup(CURRENT_FILE->file_name);
}
const gchar *display_name = CURRENT_FILE->display_name;
// Free old info text
if(current_info_text != NULL) {
g_free(current_info_text);
current_info_text = NULL;
}
if(!CURRENT_FILE->is_loaded) {
// Image not loaded yet. Use loading information and abort.
current_info_text = g_strdup_printf("%s (Image is still loading...)", display_name);
g_free(file_name);
D_UNLOCK(file_tree);
return;
}
// Update info text
if(!option_hide_info_box) {
current_info_text = g_strdup_printf("%s (%dx%d) %03.2f%% [%d/%d]", display_name,
CURRENT_FILE->width,
CURRENT_FILE->height,
current_scale_level * 100.,
(unsigned int)(bostree_rank(current_file_node) + 1),
(unsigned int)(bostree_node_count(file_tree)));
if(action != NULL) {
gchar *old_info_text = current_info_text;
current_info_text = g_strdup_printf("%s (%s)", current_info_text, action);
g_free(old_info_text);
}
}
// Prepare main window title
GString *new_window_title = g_string_new(NULL);
const char *window_title_iter = option_window_title;
const char *temporary_iter;
while(*window_title_iter) {
temporary_iter = g_strstr_len(window_title_iter, -1, "$");
if(!temporary_iter) {
g_string_append(new_window_title, window_title_iter);
break;
}
g_string_append_len(new_window_title, window_title_iter, (gssize)(temporary_iter - window_title_iter));
window_title_iter = temporary_iter + 1;
if(g_strstr_len(window_title_iter, 12, "BASEFILENAME") != NULL) {
temporary_iter = g_filename_display_basename(file_name);
g_string_append(new_window_title, temporary_iter);
window_title_iter += 12;
}
else if(g_strstr_len(window_title_iter, 8, "FILENAME") != NULL) {
g_string_append(new_window_title, display_name);
window_title_iter += 8;
}
else if(g_strstr_len(window_title_iter, 5, "WIDTH") != NULL) {
g_string_append_printf(new_window_title, "%d", CURRENT_FILE->width);
window_title_iter += 5;
}
else if(g_strstr_len(window_title_iter, 6, "HEIGHT") != NULL) {
g_string_append_printf(new_window_title, "%d", CURRENT_FILE->height);
window_title_iter += 6;
}
else if(g_strstr_len(window_title_iter, 4, "ZOOM") != NULL) {
g_string_append_printf(new_window_title, "%02.2f", (current_scale_level * 100));
window_title_iter += 4;
}
else if(g_strstr_len(window_title_iter, 12, "IMAGE_NUMBER") != NULL) {
g_string_append_printf(new_window_title, "%d", (unsigned int)(bostree_rank(current_file_node) + 1));
window_title_iter += 12;
}
else if(g_strstr_len(window_title_iter, 11, "IMAGE_COUNT") != NULL) {
g_string_append_printf(new_window_title, "%d", (unsigned int)(bostree_node_count(file_tree)));
window_title_iter += 11;
}
else {
g_string_append_c(new_window_title, '$');
}
}
D_UNLOCK(file_tree);
g_free(file_name);
gtk_window_set_title(GTK_WINDOW(main_window), new_window_title->str);
g_string_free(new_window_title, TRUE);
}/*}}}*/
#endif
gboolean window_close_callback(GtkWidget *object, gpointer user_data) {/*{{{*/
gtk_main_quit();
return FALSE;
}/*}}}*/
void calculate_base_draw_pos_and_size(int *image_transform_width, int *image_transform_height, int *x, int *y) {/*{{{*/
calculate_current_image_transformed_size(image_transform_width, image_transform_height);
if(option_scale != NO_SCALING || main_window_in_fullscreen) {
*x = (main_window_width - current_scale_level * *image_transform_width) / 2;
*y = (main_window_height - current_scale_level * *image_transform_height) / 2;
}
else {
// When scaling is disabled always use the upper left corder to avoid
// problems with window managers ignoring the large window size request.
*x = *y = 0;
}
}/*}}}*/
gboolean window_draw_callback(GtkWidget *widget, cairo_t *cr_arg, gpointer user_data) {/*{{{*/
// Continue an action chain, if one exists The placement of this is a
// compromise: What we really want is to perform actions that follow an
// action that changes the current file only after the change has been
// performed. image_loaded_handler() is the natural place to do this. But
// the window hasn't been adjusted to its final size there, so any action
// that aligns the image would fail. The configure event would be the next
// obvious place, but tiling WMs never send one, because they do not honor
// the resize request. Hence this place: The new image will always be drawn
// eventually. Performing the action inline here rather than just adding
// it as an idle callback is to prevent the image from flickering in its
// initial position.
if(CURRENT_FILE->is_loaded) {
continue_active_input_event_action_chain();
}
// Draw image
int x = 0;
int y = 0;
D_LOCK(file_tree);
if(CURRENT_FILE->is_loaded) {
// Calculate where to draw the image and the transformation matrix to use
int image_transform_width, image_transform_height;
calculate_base_draw_pos_and_size(&image_transform_width, &image_transform_height, &x, &y);
cairo_matrix_t apply_transformation = current_transformation;
apply_transformation.x0 *= current_scale_level;
apply_transformation.y0 *= current_scale_level;
// Create a temporary image surface to render to first.
//
// We use this for fading and to display the last image if the current image is
// still unavailable
//
// The temporary image surface contains the image as it
// is displayed on the screen later, with all transformations applied.
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 12, 0)
cairo_surface_t *temporary_image_surface = cairo_surface_create_similar_image(cairo_get_target(cr_arg), CAIRO_FORMAT_ARGB32, main_window_width, main_window_height);
#else
cairo_surface_t *temporary_image_surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, main_window_width, main_window_height);
#endif
cairo_t *cr = NULL;
if(cairo_surface_status(temporary_image_surface) != CAIRO_STATUS_SUCCESS) {
// This image is too large to be rendered into a temorary image surface
// As a best effort solution, render directly to the window instead
cr = cr_arg;
}
else {
cr = cairo_create(temporary_image_surface);
}
// Draw black background
cairo_save(cr);
cairo_set_source_rgba(cr, 0., 0., 0., option_transparent_background ? 0. : 1.);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_paint(cr);
cairo_restore(cr);
// From here on, draw at the target position
cairo_translate(cr, current_shift_x + x, current_shift_y + y);
cairo_transform(cr, &apply_transformation);
// Draw background pattern
if(background_checkerboard_pattern != NULL && !option_transparent_background) {
cairo_save(cr);
cairo_scale(cr, current_scale_level, current_scale_level);
cairo_new_path(cr);
// Cairo or gdkpixbuf, I don't know which, feather the border of images, leading
// to the background pattern overlaying images, which doesn't look nice at all.
// TODO The current workaround is to draw the background pattern 1px into the image
// if in fullscreen mode, because that's where the pattern irretates most –
// but I'd prefer a cleaner solution.
if(main_window_in_fullscreen) {
cairo_rectangle(cr, 1, 1, CURRENT_FILE->width - 2, CURRENT_FILE->height - 2);
}
else {
cairo_rectangle(cr, 0, 0, CURRENT_FILE->width, CURRENT_FILE->height);
}
cairo_close_path(cr);
cairo_clip(cr);
cairo_set_source(cr, background_checkerboard_pattern);
cairo_paint(cr);
cairo_restore(cr);
}
// Draw the scaled image.
if(option_lowmem || cr == cr_arg) {
// In low memory mode, we scale here and draw on the fly
// The other situation where we do this is if creating the temporary
// image surface failed, because if this failed creating the temporary
// image surface will likely also fail.
cairo_save(cr);
cairo_scale(cr, current_scale_level, current_scale_level);
cairo_rectangle(cr, 0, 0, image_transform_width, image_transform_height);
cairo_clip(cr);
draw_current_image_to_context(cr);
cairo_restore(cr);
}
else {
// Elsewise, we cache a scaled copy in a separate image surface
// to speed up rotations on scaled images
cairo_surface_t *temporary_scaled_image_surface = get_scaled_image_surface_for_current_image();
cairo_set_source_surface(cr, temporary_scaled_image_surface, 0, 0);
cairo_paint(cr);
cairo_surface_destroy(temporary_scaled_image_surface);
}
// If we drew to an off-screen buffer before, render to the window now
if(cr != cr_arg) {
// The temporary image surface is now complete.
cairo_destroy(cr);
// If currently fading, draw the surface along with the old image
if(option_fading && fading_current_alpha_stage < 1. && fading_current_alpha_stage > 0. && last_visible_image_surface != NULL) {
cairo_set_source_surface(cr_arg, last_visible_image_surface, 0, 0);
cairo_set_operator(cr_arg, CAIRO_OPERATOR_SOURCE);
cairo_paint(cr_arg);
cairo_set_source_surface(cr_arg, temporary_image_surface, 0, 0);
cairo_paint_with_alpha(cr_arg, fading_current_alpha_stage);
cairo_surface_destroy(temporary_image_surface);
// If this was the first draw, start the fading clock
if(fading_initial_time < 0) {
fading_initial_time = g_get_monotonic_time();
}
}
else {
// Draw the temporary surface to the screen
cairo_set_source_surface(cr_arg, temporary_image_surface, 0, 0);
cairo_set_operator(cr_arg, CAIRO_OPERATOR_SOURCE);
cairo_paint(cr_arg);
// Store the surface, for fading and to have something to display if no
// image is loaded (see below)
if(last_visible_image_surface != NULL) {
cairo_surface_destroy(last_visible_image_surface);
}
if(!option_lowmem || option_fading) {
last_visible_image_surface = temporary_image_surface;
}
else {
cairo_surface_destroy(temporary_image_surface);
last_visible_image_surface = NULL;
}
}
}
else {
if(last_visible_image_surface) {
cairo_surface_destroy(last_visible_image_surface);
last_visible_image_surface = NULL;
}
}
// If we have an active slideshow, resume now.
if(slideshow_timeout_id == 0) {
slideshow_timeout_id = gdk_threads_add_timeout(option_slideshow_interval * 1000, slideshow_timeout_callback, NULL);
}
current_image_drawn = TRUE;
}
else {
// The image has not yet been loaded. If available, draw from the
// temporary image surface from the last call
if(last_visible_image_surface != NULL) {
cairo_set_source_surface(cr_arg, last_visible_image_surface, 0, 0);
cairo_set_operator(cr_arg, CAIRO_OPERATOR_SOURCE);
cairo_paint(cr_arg);
}
}
D_UNLOCK(file_tree);
// Draw info box (directly to the screen)
#ifndef CONFIGURED_WITHOUT_INFO_TEXT
if(current_info_text != NULL) {
double x1, x2, y1, y2;
cairo_save(cr_arg);
// Attempt this multiple times: If it does not fit the window,
// retry with a smaller font size
for(int font_size=12; font_size > 6; font_size--) {
cairo_set_font_size(cr_arg, font_size);
if(main_window_in_fullscreen == FALSE) {
// Tiling WMs, at least i3, react weird on our window size changing.
// Drawing the info box on the image helps to avoid users noticing that.
cairo_translate(cr_arg, x < 0 ? 0 : x, y < 0 ? 0 : y);
}
cairo_set_source_rgb(cr_arg, 1., 1., 0.);
cairo_translate(cr_arg, 10, 20);
cairo_text_path(cr_arg, current_info_text);
cairo_path_t *text_path = cairo_copy_path(cr_arg);
cairo_path_extents(cr_arg, &x1, &y1, &x2, &y2);
if(x2 > main_window_width && !main_window_in_fullscreen) {
cairo_new_path(cr_arg);
cairo_restore(cr_arg);
cairo_save(cr_arg);
continue;
}
cairo_new_path(cr_arg);
cairo_rectangle(cr_arg, -5, -(y2 - y1) - 2, x2 - x1 + 10, y2 - y1 + 8);
cairo_close_path(cr_arg);
cairo_fill(cr_arg);
cairo_set_source_rgb(cr_arg, 0., 0., 0.);
cairo_append_path(cr_arg, text_path);
cairo_fill(cr_arg);
cairo_path_destroy(text_path);
break;
}
cairo_restore(cr_arg);
// Store where the box was drawn to allow for partial updates of the screen
current_info_text_bounding_box.x = (main_window_in_fullscreen == TRUE ? 0 : (x < 0 ? 0 : x)) + 10 - 5;
current_info_text_bounding_box.y = (main_window_in_fullscreen == TRUE ? 0 : (y < 0 ? 0 : y)) + 20 -(y2 - y1) - 2;
// Redraw some extra pixels to make sure a wider new box would be covered:
current_info_text_bounding_box.width = x2 - x1 + 10 + 30;
current_info_text_bounding_box.height = y2 - y1 + 8;
}
#endif
return TRUE;
}/*}}}*/
#if GTK_MAJOR_VERSION < 3
gboolean window_expose_callback(GtkWidget *widget, GdkEvent *event, gpointer user_data) {/*{{{*/
cairo_t *cr = gdk_cairo_create(widget->window);
window_draw_callback(widget, cr, user_data);
cairo_destroy(cr);
return TRUE;
}/*}}}*/
#endif
void set_scale_level_for_screen() {/*{{{*/
if(!main_window_in_fullscreen) {
// Calculate diplay width/heights with rotation, but without scaling, applied
int image_width, image_height;
calculate_current_image_transformed_size(&image_width, &image_height);
const int screen_width = screen_geometry.width;
const int screen_height = screen_geometry.height;
if(option_scale == FIXED_SCALE || scale_override) {
return;
}
else {
current_scale_level = 1.0;
if(option_scale == AUTO_SCALEUP) {
// Scale up to 80% screen size
current_scale_level = screen_width * .8 / image_width;
}
else if(option_scale == SCALE_TO_FIT) {
current_scale_level = fmin(scale_to_fit_size.width * 1. / image_width, scale_to_fit_size.height * 1. / image_height);
}
else if(option_scale == AUTO_SCALEDOWN && image_width > screen_width * .8) {
// Scale down to 80% screen size
current_scale_level = screen_width * .8 / image_width;
}
// In both cases: If the height exceeds 80% screen size, scale
// down
if((option_scale == AUTO_SCALEUP || option_scale == AUTO_SCALEDOWN) && image_height * current_scale_level > screen_height * .8) {
current_scale_level = screen_height * .8 / image_height;
}
}
current_scale_level = round(current_scale_level * 100.) / 100.;
}
else {
// In fullscreen, the screen size and window size match, so the
// function to adjust to the window size works just fine (and does
// not come with the 80%, as users would expect in fullscreen)
set_scale_level_to_fit();
}
}/*}}}*/
void set_scale_level_to_fit() {/*{{{*/
if(scale_override || option_scale == FIXED_SCALE) {
return;
}
D_LOCK(file_tree);
if(CURRENT_FILE->is_loaded) {
if(!current_image_drawn) {
scale_override = FALSE;
}
// Calculate diplay width/heights with rotation, but without scaling, applied
int image_width, image_height;
calculate_current_image_transformed_size(&image_width, &image_height);
gdouble new_scale_level = 1.0;
// Only scale if scaling is not disabled. The alternative is to also
// scale for no-scaling mode if (!main_window_in_fullscreen). This
// effectively disables the no-scaling mode in non-fullscreen. I
// implemented that this way, but changed it per user request.
if(option_scale == AUTO_SCALEUP || option_scale == AUTO_SCALEDOWN) {
if(option_scale == AUTO_SCALEUP) {
// Scale up
if(image_width * new_scale_level < main_window_width) {
new_scale_level = main_window_width * 1.0 / image_width;
}
if(image_height * new_scale_level < main_window_height) {
new_scale_level = main_window_height * 1.0 / image_height;
}
}
// Scale down
if(main_window_height < new_scale_level * image_height) {
new_scale_level = main_window_height * 1.0 / image_height;
}
if(main_window_width < new_scale_level * image_width) {
new_scale_level = main_window_width * 1.0 / image_width;
}
}
else if(option_scale == SCALE_TO_FIT) {
new_scale_level = fmin(scale_to_fit_size.width * 1. / image_width, scale_to_fit_size.height * 1. / image_height);
}
if(fabs(new_scale_level - current_scale_level) > DBL_EPSILON) {
current_scale_level = new_scale_level;
invalidate_current_scaled_image_surface();
}
}
D_UNLOCK(file_tree);
}
gboolean set_scale_level_to_fit_callback(gpointer user_data) {
set_scale_level_to_fit();
return FALSE;
}
/*}}}*/
void action(pqiv_action_t action_id, pqiv_action_parameter_t parameter) {/*{{{*/
switch(action_id) {
case ACTION_NOP:
break;
case ACTION_SHIFT_Y:
if(!CURRENT_FILE->is_loaded) return;
current_shift_y += parameter.pint;
gtk_widget_queue_draw(GTK_WIDGET(main_window));
update_info_text(NULL);
break;
case ACTION_SHIFT_X:
if(!CURRENT_FILE->is_loaded) return;
current_shift_x += parameter.pint;
gtk_widget_queue_draw(GTK_WIDGET(main_window));
update_info_text(NULL);
break;
case ACTION_SET_SLIDESHOW_INTERVAL_RELATIVE:
case ACTION_SET_SLIDESHOW_INTERVAL_ABSOLUTE:
if(action_id == ACTION_SET_SLIDESHOW_INTERVAL_ABSOLUTE) {
option_slideshow_interval = fmax(parameter.pdouble, 1e-3);
}
else {
option_slideshow_interval = fmax(1., option_slideshow_interval + parameter.pdouble);
}
if(slideshow_timeout_id > 0) {
g_source_remove(slideshow_timeout_id);
slideshow_timeout_id = gdk_threads_add_timeout(option_slideshow_interval * 1000, slideshow_timeout_callback, NULL);
}
gchar *info_text = g_strdup_printf("Slideshow interval set to %d seconds", (int)option_slideshow_interval);
update_info_text(info_text);
gtk_widget_queue_draw(GTK_WIDGET(main_window));
g_free(info_text);
break;
case ACTION_SET_SCALE_LEVEL_RELATIVE:
case ACTION_SET_SCALE_LEVEL_ABSOLUTE:
if(!CURRENT_FILE->is_loaded) return;
if(action_id == ACTION_SET_SCALE_LEVEL_ABSOLUTE) {
current_scale_level = parameter.pdouble;
}
else {
current_scale_level *= parameter.pdouble;
}
current_scale_level = round(current_scale_level * 100.) / 100.;
if((option_scale == AUTO_SCALEDOWN && current_scale_level > 1) || option_scale == NO_SCALING) {
scale_override = TRUE;
}
invalidate_current_scaled_image_surface();
current_image_drawn = FALSE;
if(main_window_in_fullscreen) {
gtk_widget_queue_draw(GTK_WIDGET(main_window));
}
else {
int image_width, image_height;
calculate_current_image_transformed_size(&image_width, &image_height);
gtk_window_resize(main_window, current_scale_level * image_width, current_scale_level * image_height);
if(!wm_supports_moveresize) {
queue_draw();
}
}
update_info_text(NULL);
break;
case ACTION_TOGGLE_SCALE_MODE:
if(!CURRENT_FILE->is_loaded) return;
if(parameter.pint == 0) {
if(++option_scale > AUTO_SCALEUP) {
option_scale = NO_SCALING;
}
}
else {
option_scale = (parameter.pint - 1) % 4;
}
scale_override = FALSE;
current_image_drawn = FALSE;
current_shift_x = 0;
current_shift_y = 0;
set_scale_level_for_screen();
main_window_adjust_for_image();
invalidate_current_scaled_image_surface();
gtk_widget_queue_draw(GTK_WIDGET(main_window));
switch(option_scale) {
case NO_SCALING: update_info_text("Scaling disabled"); break;
case AUTO_SCALEDOWN: update_info_text("Automatic scaledown enabled"); break;
case AUTO_SCALEUP: update_info_text("Automatic scaling enabled"); break;
case FIXED_SCALE: update_info_text("Maintaining current scale level"); break;
default: break;
}
break;
case ACTION_TOGGLE_SHUFFLE_MODE:
if(parameter.pint == 0) {
option_shuffle = !option_shuffle;
}
else {
option_shuffle = parameter.pint == 1;
}
preload_adjacent_images();
update_info_text(option_shuffle ? "Shuffle mode enabled" : "Shuffle mode disabled");
gtk_widget_queue_draw(GTK_WIDGET(main_window));
break;
case ACTION_RELOAD:
if(!CURRENT_FILE->is_loaded) return;
CURRENT_FILE->force_reload = TRUE;
update_info_text("Reloading image..");
queue_image_load(relative_image_pointer(0));
return;
break;
case ACTION_RESET_SCALE_LEVEL:
if(!CURRENT_FILE->is_loaded) return;
current_image_drawn = FALSE;
scale_override = FALSE;
set_scale_level_for_screen();
main_window_adjust_for_image();
invalidate_current_scaled_image_surface();
gtk_widget_queue_draw(GTK_WIDGET(main_window));
update_info_text(NULL);
break;
case ACTION_TOGGLE_FULLSCREEN:
if(main_window_in_fullscreen == FALSE) {
window_fullscreen();
}
else {
window_unfullscreen();
}
return;
break;
case ACTION_FLIP_HORIZONTALLY:
if(!CURRENT_FILE->is_loaded) return;
{
int image_width, image_height;
calculate_current_image_transformed_size(&image_width, &image_height);
cairo_matrix_t transformation = { -1., 0., 0., 1., image_width, 0 };
transform_current_image(&transformation);
}
update_info_text("Image flipped horizontally");
break;
case ACTION_FLIP_VERTICALLY:
if(!CURRENT_FILE->is_loaded) return;
{
int image_width, image_height;
calculate_current_image_transformed_size(&image_width, &image_height);
cairo_matrix_t transformation = { 1., 0., 0., -1., 0, image_height };
transform_current_image(&transformation);
}
update_info_text("Image flipped vertically");
break;
case ACTION_ROTATE_LEFT:
if(!CURRENT_FILE->is_loaded) return;
{
int image_width, image_height;
calculate_current_image_transformed_size(&image_width, &image_height);
cairo_matrix_t transformation = { 0., -1., 1., 0., 0, image_width };
transform_current_image(&transformation);
}
update_info_text("Image rotated left");
break;
case ACTION_ROTATE_RIGHT:
if(!CURRENT_FILE->is_loaded) return;
{
int image_width, image_height;
calculate_current_image_transformed_size(&image_width, &image_height);
cairo_matrix_t transformation = { 0., 1., -1., 0., image_height, 0. };
transform_current_image(&transformation);
}
update_info_text("Image rotated right");
break;
#ifndef CONFIGURED_WITHOUT_INFO_TEXT
case ACTION_TOGGLE_INFO_BOX:
option_hide_info_box = !option_hide_info_box;
update_info_text(NULL);
gtk_widget_queue_draw(GTK_WIDGET(main_window));
break;
#endif
#ifndef CONFIGURED_WITHOUT_JUMP_DIALOG
case ACTION_JUMP_DIALOG:
do_jump_dialog();
return;
break;
#endif
case ACTION_TOGGLE_SLIDESHOW:
if(slideshow_timeout_id >= 0) {
if(slideshow_timeout_id > 0) {
g_source_remove(slideshow_timeout_id);
}
slideshow_timeout_id = -1;
update_info_text("Slideshow disabled");
}
else {
slideshow_timeout_id = gdk_threads_add_timeout(option_slideshow_interval * 1000, slideshow_timeout_callback, NULL);
update_info_text("Slideshow enabled");
}
gtk_widget_queue_draw(GTK_WIDGET(main_window));
break;
case ACTION_HARDLINK_CURRENT_IMAGE:
if(!CURRENT_FILE->is_loaded) return;
hardlink_current_image();
gtk_widget_queue_draw(GTK_WIDGET(main_window));
break;
case ACTION_GOTO_DIRECTORY_RELATIVE:
directory_image_movement(parameter.pint);
return;
break;
case ACTION_GOTO_FILE_RELATIVE:
relative_image_movement(parameter.pint);
return;
break;
case ACTION_QUIT:
gtk_widget_destroy(GTK_WIDGET(main_window));
return;
break;
#ifndef CONFIGURED_WITHOUT_EXTERNAL_COMMANDS
case ACTION_NUMERIC_COMMAND:
{
if(parameter.pint < 1 || parameter.pint > 8) {
g_printerr("Only commands 1..9 are supported.\n");
return;
}
gchar *command = external_image_filter_commands[parameter.pint - 1];
action(ACTION_COMMAND, (pqiv_action_parameter_t)( command));
return;
}
break;
case ACTION_COMMAND:
if(!CURRENT_FILE->is_loaded) return;
{
char *command = parameter.pcharptr;
if(command == NULL) {
break;
}
else if (
((CURRENT_FILE->file_flags & FILE_FLAGS_MEMORY_IMAGE) != 0 && command[0] != '|')
|| ((CURRENT_FILE->file_flags & FILE_FLAGS_ANIMATION) != 0 && command[0] == '|')) {
update_info_text("Command incompatible with current file type");
gtk_widget_queue_draw(GTK_WIDGET(main_window));
}
else {
gchar *info = g_strdup_printf("Executing command %s", command);
update_info_text(info);
g_free(info);
gtk_widget_queue_draw(GTK_WIDGET(main_window));
command = g_strdup(command);
g_thread_new("image-filter", apply_external_image_filter_thread, command);
return;
}
}
break;
#endif
#ifndef CONFIGURED_WITHOUT_ACTIONS
case ACTION_ADD_FILE:
g_thread_new("image-loader-from-action", (GThreadFunc)load_images_handle_parameter_thread, g_strdup(parameter.pcharptr));
break;
case ACTION_GOTO_FILE_BYINDEX:
case ACTION_REMOVE_FILE_BYINDEX:
{
D_LOCK(file_tree);
if(parameter.pint < 0) {
parameter.pint += bostree_node_count(file_tree);
}
BOSNode *node = bostree_select(file_tree, parameter.pint);
if(node) {
node = bostree_node_weak_ref(node);
}
D_UNLOCK(file_tree);
if(!node) {
g_printerr("Image #%d not found.\n", parameter.pint);
}
else {
if(action_id == ACTION_GOTO_FILE_BYINDEX) {
absolute_image_movement(node);
}
else {
remove_image(node);
gtk_widget_queue_draw(GTK_WIDGET(main_window));
}
}
}
return;
break;
case ACTION_GOTO_FILE_BYNAME:
case ACTION_REMOVE_FILE_BYNAME:
{
D_LOCK(file_tree);
BOSNode *node = image_pointer_by_name(parameter.pcharptr);
if(node) {
node = bostree_node_weak_ref(node);
}
D_UNLOCK(file_tree);
if(!node) {
g_printerr("Image `%s' not found.\n", parameter.pcharptr);
}
else {
if(action_id == ACTION_GOTO_FILE_BYNAME) {
absolute_image_movement(node);
}
else {
remove_image(node);
gtk_widget_queue_draw(GTK_WIDGET(main_window));
}
}
}
return;
break;
case ACTION_OUTPUT_FILE_LIST:
{
D_LOCK(file_tree);
for(BOSNode *iter = bostree_select(file_tree, 0); iter; iter = bostree_next_node(iter)) {
g_print("%s\n", FILE(iter)->display_name);
}
D_UNLOCK(file_tree);
}
break;
case ACTION_SET_CURSOR_VISIBILITY:
if(parameter.pint) {
window_show_cursor();
}
else {
window_hide_cursor();
}
break;
case ACTION_SET_STATUS_OUTPUT:
option_status_output = !!parameter.pint;
status_output();
break;
case ACTION_SET_SCALE_MODE_FIT_PX:
option_scale = SCALE_TO_FIT;
scale_to_fit_size.width = parameter.p2short.p1;
scale_to_fit_size.height = parameter.p2short.p2;
set_scale_level_for_screen();
main_window_adjust_for_image();
invalidate_current_scaled_image_surface();
gtk_widget_queue_draw(GTK_WIDGET(main_window));
{
gchar info_text[255];
snprintf(info_text, 255, "Scale level adjusted to fit %dx%d px", scale_to_fit_size.width, scale_to_fit_size.height);
update_info_text(info_text);
}
break;
case ACTION_SET_SHIFT_X:
if(!CURRENT_FILE->is_loaded) return;
current_shift_x = parameter.pint;
gtk_widget_queue_draw(GTK_WIDGET(main_window));
update_info_text(NULL);
break;
case ACTION_SET_SHIFT_Y:
if(!CURRENT_FILE->is_loaded) return;
current_shift_y = parameter.pint;
gtk_widget_queue_draw(GTK_WIDGET(main_window));
update_info_text(NULL);
break;
case ACTION_BIND_KEY:
parse_key_bindings(parameter.pcharptr);
break;
case ACTION_SEND_KEYS:
for(char *i=parameter.pcharptr; *i; i++) {
handle_input_event(KEY_BINDING_VALUE(0, 0, *i));
}
break;
case ACTION_SET_SHIFT_ALIGN_CORNER:
{
int flags = 0;
int x, y;
int image_width, image_height;
calculate_base_draw_pos_and_size(&image_width, &image_height, &x, &y);
image_width *= current_scale_level;
image_height *= current_scale_level;
for(char *direction = parameter.pcharptr; *direction; direction++) {
switch(*direction) {
case 'C':
flags = 1; // Prefer centering
current_shift_x = 0;
current_shift_y = 0;
break;
case 'N':
if(flags == 0 || image_height > main_window_height) {
current_shift_y = -y;
}
break;
case 'S':
if(flags == 0 || image_height > main_window_height) {
current_shift_y = -y - image_height + main_window_height;
}
break;
case 'E':
if(flags == 0 || image_width > main_window_width) {
current_shift_x = -x - image_width + main_window_width;
}
break;
case 'W':
if(flags == 0 || image_width > main_window_width) {
current_shift_x = -x;
}
break;
}
}
gtk_widget_queue_draw(GTK_WIDGET(main_window));
update_info_text(NULL);
}
break;
#endif
default:
break;
}
// By default execute the next action from a chain of actions bound to a key
// immediately; unless an action explicitly return'ed above.
continue_active_input_event_action_chain();
}/*}}}*/
gboolean window_configure_callback(GtkWidget *widget, GdkEventConfigure *event, gpointer user_data) {/*{{{*/
/*
* struct GdkEventConfigure {
GdkEventType type;
GdkWindow *window;
gint8 send_event;
gint x, y;
gint width;
gint height;
};
*/
static gint old_window_x, old_window_y;
if(old_window_x != event->x || old_window_y != event->y) {
// Window was moved. Reset automatic positioning to allow
// user-resizing without pain.
gtk_window_set_position(main_window, GTK_WIN_POS_NONE);
old_window_x = event->x;
old_window_y = event->y;
// Execute the "screen changed" callback, because the monitor at the window might have changed
window_screen_changed_callback(NULL, NULL, NULL);
// In fullscreen, the position should always match the upper left point
// of the screen. Some WMs get this wrong.
if(main_window_in_fullscreen && (event->x != screen_geometry.x || event->y != screen_geometry.y)) {
gtk_window_move(main_window, screen_geometry.x, screen_geometry.y);
}
}
if(main_window_width != event->width || main_window_height != event->height) {
// Update window size
if(main_window_in_fullscreen) {
main_window_width = screen_geometry.width;
main_window_height = screen_geometry.height;
}
else {
main_window_width = event->width;
main_window_height = event->height;
}
// Rescale the image
set_scale_level_to_fit();
queue_draw();
// We need to redraw in old GTK versions to avoid artifacts
#if GTK_MAJOR_VERSION < 3
gtk_widget_queue_draw(GTK_WIDGET(main_window));
#endif
}
return FALSE;
}/*}}}*/
void handle_input_event(guint key_binding_value);
#ifndef CONFIGURED_WITHOUT_ACTIONS
gboolean handle_input_event_timeout_callback(gpointer user_data) {/*{{{*/
handle_input_event(0);
active_key_binding.key_binding = NULL;
active_key_binding.timeout_id = -1;
return FALSE;
}/*}}}*/
#endif
static void continue_active_input_event_action_chain() {/*{{{*/
#ifndef CONFIGURED_WITHOUT_ACTIONS
if(active_key_binding.timeout_id == -1 && active_key_binding.key_binding) {
key_binding_t *binding = active_key_binding.key_binding;
active_key_binding.key_binding = binding->next_action;
action(binding->action, binding->parameter);
}
#endif
}/*}}}*/
static void block_active_input_event_action_chain() {/*{{{*/
#ifndef CONFIGURED_WITHOUT_ACTIONS
if(active_key_binding.timeout_id == -1 && active_key_binding.key_binding) {
active_key_binding.timeout_id = -2;
}
#endif
}/*}}}*/
static void unblock_active_input_event_action_chain() {/*{{{*/
#ifndef CONFIGURED_WITHOUT_ACTIONS
if(active_key_binding.timeout_id == -2 && active_key_binding.key_binding) {
active_key_binding.timeout_id = -1;
}
#endif
}/*}}}*/
void handle_input_event(guint key_binding_value) {/*{{{*/
gboolean is_mouse = (key_binding_value >> 31) & 1;
guint state = (key_binding_value >> 28) & 7;
guint keycode = key_binding_value & 0xfffffff;
// Filter unwanted state variables out
state &= gtk_accelerator_get_default_mod_mask();
state &= ~GDK_SHIFT_MASK;
key_binding_value = KEY_BINDING_VALUE(is_mouse, state, keycode);
#ifndef CONFIGURED_WITHOUT_ACTIONS
key_binding_t *binding = NULL;
if(active_key_binding.timeout_id >= 0 && active_key_binding.key_binding) {
g_source_remove(active_key_binding.timeout_id);
active_key_binding.timeout_id = -1;
if(active_key_binding.key_binding->next_key_bindings) {
binding = g_hash_table_lookup(active_key_binding.key_binding->next_key_bindings, GUINT_TO_POINTER(key_binding_value));
if(!binding && !is_mouse && gdk_keyval_is_upper(keycode) && !gdk_keyval_is_lower(keycode)) {
guint alternate_value = KEY_BINDING_VALUE(is_mouse, state & ~GDK_SHIFT_MASK, gdk_keyval_to_lower(keycode));
binding = g_hash_table_lookup(active_key_binding.key_binding->next_key_bindings, GUINT_TO_POINTER(alternate_value));
}
}
if(!binding) {
key_binding_t *binding = active_key_binding.key_binding;
active_key_binding.key_binding = binding->next_action;
action(binding->action, binding->parameter);
return;
}
active_key_binding.key_binding = NULL;
}
if(!key_binding_value) {
return;
}
if(!binding) {
binding = g_hash_table_lookup(key_bindings, GUINT_TO_POINTER(key_binding_value));
if(!binding && !is_mouse && gdk_keyval_is_upper(keycode) && !gdk_keyval_is_lower(keycode)) {
guint alternate_value = KEY_BINDING_VALUE(is_mouse, state & ~GDK_SHIFT_MASK, gdk_keyval_to_lower(keycode));
binding = g_hash_table_lookup(key_bindings, GUINT_TO_POINTER(alternate_value));
}
}
if(binding) {
if(binding->next_key_bindings) {
active_key_binding.key_binding = binding;
active_key_binding.timeout_id = gdk_threads_add_timeout(400, handle_input_event_timeout_callback, NULL);
}
else {
active_key_binding.key_binding = binding->next_action;
action(binding->action, binding->parameter);
}
}
#else
for(const struct default_key_bindings_struct *kb = default_key_bindings; kb->key_binding_value; kb++) {
if(kb->key_binding_value == key_binding_value) {
action(kb->action, kb->parameter);
break;
}
}
#endif
}/*}}}*/
gboolean window_key_press_callback(GtkWidget *widget, GdkEventKey *event, gpointer user_data) {/*{{{*/
if(event->keyval < 128 && keyboard_aliases[event->keyval] != 0) {
// TODO Deprecated, remove with 2.6
event->keyval = keyboard_aliases[event->keyval];
}
if(option_reverse_cursor_keys) {
// TODO Deprecated, remove with 2.6
switch(event->keyval) {
case GDK_KEY_Up:
case GDK_KEY_KP_Up:
event->keyval = GDK_KEY_Down;
break;
case GDK_KEY_Down:
case GDK_KEY_KP_Down:
event->keyval = GDK_KEY_Up;
break;
case GDK_KEY_Left:
case GDK_KEY_KP_Left:
event->keyval = GDK_KEY_Right;
break;
case GDK_KEY_Right:
case GDK_KEY_KP_Right:
event->keyval = GDK_KEY_Left;
break;
}
}
handle_input_event(KEY_BINDING_VALUE(0, event->state, event->keyval));
return FALSE;
}/*}}}*/
void window_center_mouse() {/*{{{*/
GdkDisplay *display = gtk_widget_get_display(GTK_WIDGET(main_window));
GdkScreen *screen = gtk_widget_get_screen(GTK_WIDGET(main_window));
#if GTK_MAJOR_VERSION < 3
gdk_display_warp_pointer(display, screen, screen_geometry.x + screen_geometry.width / 2., screen_geometry.y + screen_geometry.height / 2.);
#else
#if GTK_MAJOR_VERSION == 3 && GTK_MINOR_VERSION < 20
GdkDevice *device = gdk_device_manager_get_client_pointer(gdk_display_get_device_manager(display));
#else
GdkDevice *device = gdk_seat_get_pointer(gdk_display_get_default_seat(display));
#endif
gdk_device_warp(device, screen, screen_geometry.x + screen_geometry.width / 2., screen_geometry.y + screen_geometry.height / 2.);
#endif
}/*}}}*/
gboolean window_motion_notify_callback(GtkWidget *widget, GdkEventMotion *event, gpointer user_data) {/*{{{*/
/*
struct GdkEventMotion {
GdkEventType type;
GdkWindow *window;
gint8 send_event;
guint32 time;
gdouble x;
gdouble y;
gdouble *axes;
guint state;
gint16 is_hint;
GdkDevice *device;
gdouble x_root, y_root;
};
*/
if(!main_window_in_fullscreen) {
return FALSE;
}
if(event->state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | GDK_BUTTON3_MASK)) {
gdouble dev_x = screen_geometry.width / 2 + screen_geometry.x - event->x_root;
gdouble dev_y = screen_geometry.height / 2 + screen_geometry.y - event->y_root;
if(fabs(dev_x) < 5 && fabs(dev_y) < 4) {
return FALSE;
}
if(event->state & GDK_BUTTON1_MASK) {
current_shift_x += dev_x;
current_shift_y += dev_y;
}
else if(event->state & GDK_BUTTON3_MASK) {
current_scale_level += dev_y / 1000.;
if(current_scale_level < .01) {
current_scale_level = .01;
}
update_info_text(NULL);
invalidate_current_scaled_image_surface();
}
gtk_widget_queue_draw(GTK_WIDGET(main_window));
window_center_mouse();
}
return FALSE;
}/*}}}*/
gboolean window_button_press_callback(GtkWidget *widget, GdkEventButton *event, gpointer user_data) {/*{{{*/
/*
http://developer.gnome.org/gdk/stable/gdk-Event-Structures.html#GdkEventButton
struct GdkEventButton {
GdkEventType type;
GdkWindow *window;
gint8 send_event;
guint32 time;
gdouble x;
gdouble y;
gdouble *axes;
guint state;
guint button;
GdkDevice *device;
gdouble x_root, y_root;
};
*/
if(!main_window_in_fullscreen) {
if(option_transparent_background) {
gtk_window_set_decorated(main_window, !gtk_window_get_decorated(main_window));
}
return FALSE;
}
window_center_mouse();
last_button_press_time = event->time;
return FALSE;
}/*}}}*/
gboolean window_button_release_callback(GtkWidget *widget, GdkEventButton *event, gpointer user_data) {/*{{{*/
if(!main_window_in_fullscreen || event->time - last_button_press_time > 250) {
// Do nothing if the button was pressed for a long time or if not in fullscreen
return FALSE;
}
handle_input_event(KEY_BINDING_VALUE(1, event->state, event->button));
return FALSE;
}/*}}}*/
gboolean window_scroll_callback(GtkWidget *widget, GdkEventScroll *event, gpointer user_data) {/*{{{*/
/*
struct GdkEventScroll {
GdkEventType type;
GdkWindow *window;
gint8 send_event;
guint32 time;
gdouble x;
gdouble y;
guint state;
GdkScrollDirection direction;
GdkDevice *device;
gdouble x_root, y_root;
};
*/
if(option_reverse_scroll == TRUE) {
// TODO Deprecated, remove with 2.6
if(event->direction == GDK_SCROLL_UP) {
event->direction = GDK_SCROLL_DOWN;
}
else if(event->direction == GDK_SCROLL_DOWN) {
event->direction = GDK_SCROLL_UP;
}
}
handle_input_event(KEY_BINDING_VALUE(1, event->state, (event->direction + 1) << 2));
return FALSE;
}/*}}}*/
void window_hide_cursor() {/*{{{*/
GdkDisplay *display = gtk_widget_get_display(GTK_WIDGET(main_window));
GdkCursor *cursor = gdk_cursor_new_for_display(display, GDK_BLANK_CURSOR);
GdkWindow *window = gtk_widget_get_window(GTK_WIDGET(main_window));
gdk_window_set_cursor(window, cursor);
#if GTK_MAJOR_VERSION >= 3
g_object_unref(cursor);
#endif
}/*}}}*/
void window_show_cursor() {/*{{{*/
GdkWindow *window = gtk_widget_get_window(GTK_WIDGET(main_window));
gdk_window_set_cursor(window, NULL);
}/*}}}*/
void window_state_into_fullscreen_actions() {/*{{{*/
current_shift_x = 0;
current_shift_y = 0;
window_hide_cursor();
main_window_width = screen_geometry.width;
main_window_height = screen_geometry.height;
set_scale_level_to_fit();
invalidate_current_scaled_image_surface();
#if GTK_MAJOR_VERSION < 3
gtk_widget_queue_draw(GTK_WIDGET(main_window));
#endif
}/*}}}*/
void window_state_out_of_fullscreen_actions() {/*{{{*/
current_shift_x = 0;
current_shift_y = 0;
// If the fullscreen state is left, readjust image placement/size/..
scale_override = FALSE;
set_scale_level_for_screen();
main_window_adjust_for_image();
if(!main_window_visible) {
main_window_visible = TRUE;
gtk_widget_show_all(GTK_WIDGET(main_window));
}
invalidate_current_scaled_image_surface();
window_show_cursor();
}/*}}}*/
gboolean window_state_callback(GtkWidget *widget, GdkEventWindowState *event, gpointer user_data) {/*{{{*/
/*
struct GdkEventWindowState {
GdkEventType type;
GdkWindow *window;
gint8 send_event;
GdkWindowState changed_mask;
GdkWindowState new_window_state;
};
*/
if(event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN) {
gboolean new_in_fs_state = (event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN) != 0 ? TRUE : FALSE;
if(new_in_fs_state == main_window_in_fullscreen) {
return FALSE;
}
main_window_in_fullscreen = new_in_fs_state;
if(main_window_in_fullscreen) {
window_state_into_fullscreen_actions();
}
else {
window_state_out_of_fullscreen_actions();
}
update_info_text(NULL);
}
return FALSE;
}/*}}}*/
void window_screen_activate_rgba() {/*{{{*/
GdkScreen *screen = gtk_widget_get_screen(GTK_WIDGET(main_window));
#if GTK_MAJOR_VERSION >= 3
GdkVisual *visual = gdk_screen_get_rgba_visual(screen);
if (visual == NULL) {
visual = gdk_screen_get_system_visual(screen);
}
gtk_widget_set_visual(GTK_WIDGET(main_window), visual);
#else
if(gtk_widget_get_realized(GTK_WIDGET(main_window))) {
// In GTK2, this must not happen again after realization
return;
}
GdkColormap *colormap = gdk_screen_get_rgba_colormap(screen);
if (colormap != NULL) {
gtk_widget_set_colormap(GTK_WIDGET(main_window), colormap);
}
#endif
return;
}/*}}}*/
void window_screen_window_manager_changed_callback(gpointer user_data) {/*{{{*/
#ifndef _WIN32
GdkScreen *screen = GDK_SCREEN(user_data);
// TODO Would _NET_WM_ALLOWED_ACTIONS -> _NET_WM_ACTION_RESIZE and _NET_WM_ACTION_FULLSCREEN be a better choice here?
#if GTK_MAJOR_VERSION >= 3
if(GDK_IS_X11_SCREEN(screen)) {
wm_supports_fullscreen = gdk_x11_screen_supports_net_wm_hint(screen, gdk_x11_xatom_to_atom(gdk_x11_get_xatom_by_name("_NET_WM_STATE_FULLSCREEN")));
wm_supports_moveresize = gdk_x11_screen_supports_net_wm_hint(screen, gdk_x11_xatom_to_atom(gdk_x11_get_xatom_by_name("_NET_MOVERESIZE_WINDOW")));
}
#else
wm_supports_fullscreen = gdk_x11_screen_supports_net_wm_hint(screen, gdk_x11_xatom_to_atom(gdk_x11_get_xatom_by_name("_NET_WM_STATE_FULLSCREEN")));
wm_supports_moveresize = gdk_x11_screen_supports_net_wm_hint(screen, gdk_x11_xatom_to_atom(gdk_x11_get_xatom_by_name("_NET_MOVERESIZE_WINDOW")));
#endif
#endif
}/*}}}*/
void window_screen_changed_callback(GtkWidget *widget, GdkScreen *previous_screen, gpointer user_data) {/*{{{*/
GdkScreen *screen = gtk_widget_get_screen(GTK_WIDGET(main_window));
GdkWindow *window = gtk_widget_get_window(GTK_WIDGET(main_window));
guint monitor = gdk_screen_get_monitor_at_window(screen, window);
#ifndef _WIN32
g_signal_connect(screen, "window-manager-changed", G_CALLBACK(window_screen_window_manager_changed_callback), screen);
#endif
window_screen_window_manager_changed_callback(screen);
static guint old_monitor = 9999;
if(old_monitor != 9999 && option_transparent_background) {
window_screen_activate_rgba();
}
if(old_monitor != monitor) {
gdk_screen_get_monitor_geometry(screen, monitor, &screen_geometry);
}
}/*}}}*/
void window_realize_callback(GtkWidget *widget, gpointer user_data) {/*{{{*/
if(option_start_fullscreen) {
window_fullscreen();
}
// Execute the screen-changed callback, to assign the correct screen
// to the window (if it's not the primary one, which we assigned in
// create_window)
window_screen_changed_callback(NULL, NULL, NULL);
#if GTK_MAJOR_VERSION < 3
if(option_transparent_background) {
window_screen_activate_rgba();
}
#endif
#if GTK_MAJOR_VERSION < 3 && !defined(_WIN32)
gdk_property_change(gtk_widget_get_window(GTK_WIDGET(main_window)), gdk_atom_intern("_GTK_THEME_VARIANT", FALSE), (GdkAtom)XA_STRING, 8, GDK_PROP_MODE_REPLACE, (guchar *)"dark", 4);
#endif
}/*}}}*/
void create_window() { /*{{{*/
#if GTK_MAJOR_VERSION >= 3
GtkSettings *settings = gtk_settings_get_default();
if(settings != NULL) {
g_object_set(G_OBJECT(settings), "gtk-application-prefer-dark-theme", TRUE, NULL);
}
#endif
main_window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
g_signal_connect(main_window, "destroy", G_CALLBACK(window_close_callback), NULL);
#if GTK_MAJOR_VERSION < 3
g_signal_connect(main_window, "expose-event", G_CALLBACK(window_expose_callback), NULL);
#else
g_signal_connect(main_window, "draw", G_CALLBACK(window_draw_callback), NULL);
#endif
g_signal_connect(main_window, "configure-event", G_CALLBACK(window_configure_callback), NULL);
g_signal_connect(main_window, "key-press-event", G_CALLBACK(window_key_press_callback), NULL);
g_signal_connect(main_window, "scroll-event", G_CALLBACK(window_scroll_callback), NULL);
g_signal_connect(main_window, "screen-changed", G_CALLBACK(window_screen_changed_callback), NULL);
g_signal_connect(main_window, "motion-notify-event", G_CALLBACK(window_motion_notify_callback), NULL);
g_signal_connect(main_window, "button-press-event", G_CALLBACK(window_button_press_callback), NULL);
g_signal_connect(main_window, "button-release-event", G_CALLBACK(window_button_release_callback), NULL);
g_signal_connect(main_window, "window-state-event", G_CALLBACK(window_state_callback), NULL);
g_signal_connect(main_window, "realize", G_CALLBACK(window_realize_callback), NULL);
gtk_widget_set_events(GTK_WIDGET(main_window),
GDK_EXPOSURE_MASK | GDK_SCROLL_MASK | GDK_BUTTON_MOTION_MASK |
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_KEY_PRESS_MASK |
GDK_PROPERTY_CHANGE_MASK | GDK_KEY_RELEASE_MASK | GDK_STRUCTURE_MASK);
// Initialize the screen geometry variable to the primary screen
// Useful if no WM is present
GdkScreen *screen = gdk_screen_get_default();
guint monitor = gdk_screen_get_primary_monitor(screen);
gdk_screen_get_monitor_geometry(screen, monitor, &screen_geometry);
window_screen_window_manager_changed_callback(screen);
if(option_start_fullscreen) {
// If no WM is present, move the window to the screen origin and
// assume fullscreen right from the start
window_fullscreen();
}
else if(option_window_position.x >= 0) {
window_move_helper_callback(NULL);
}
else if(option_window_position.x != -1) {
gtk_window_set_position(main_window, GTK_WIN_POS_CENTER);
}
#if GTK_MAJOR_VERSION < 3
gtk_widget_set_double_buffered(GTK_WIDGET(main_window), TRUE);
#endif
gtk_widget_set_app_paintable(GTK_WIDGET(main_window), TRUE);
if(option_transparent_background) {
gtk_window_set_decorated(main_window, FALSE);
}
if(option_transparent_background) {
window_screen_activate_rgba();
}
}/*}}}*/
gboolean initialize_gui() {/*{{{*/
setup_checkerboard_pattern();
create_window();
if(initialize_image_loader()) {
image_loaded_handler(NULL);
if(option_start_with_slideshow_mode) {
slideshow_timeout_id = gdk_threads_add_timeout(option_slideshow_interval * 1000, slideshow_timeout_callback, NULL);
}
return TRUE;
}
return FALSE;
}/*}}}*/
gboolean initialize_gui_callback(gpointer user_data) {/*{{{*/
if(!gui_initialized && initialize_image_loader()) {
initialize_gui();
gui_initialized = TRUE;
}
return FALSE;
}/*}}}*/
gboolean initialize_gui_or_quit_callback(gpointer user_data) {/*{{{*/
if(!gui_initialized && initialize_image_loader()) {
initialize_gui();
gui_initialized = TRUE;
}
if(!file_tree_valid || bostree_node_count(file_tree) == 0) {
g_printerr("No images left to display.\n");
exit(1);
}
return FALSE;
}/*}}}*/
#ifndef CONFIGURED_WITHOUT_ACTIONS
void help_show_key_bindings_helper(gpointer key, gpointer value, gpointer user_data) {/*{{{*/
guint key_binding_value = GPOINTER_TO_UINT(key);
gboolean is_mouse = (key_binding_value >> 31) & 1;
guint state = (key_binding_value >> 28) & 7;
guint keycode = key_binding_value & 0xfffffff;
key_binding_t *key_binding = (key_binding_t *)value;
gchar *str_key;
gchar modifier[255];
snprintf(modifier, 255, "%s%s%s", state & GDK_SHIFT_MASK ? "" : "", state & GDK_CONTROL_MASK ? "" : "", state & GDK_MOD1_MASK ? "" : "");
if(is_mouse) {
if(keycode >> 2 == 0) {
str_key = g_strdup_printf("%s%s ", user_data ? (gchar*)user_data : "", modifier, keycode);
}
else {
str_key = g_strdup_printf("%s%s ", user_data ? (gchar*)user_data : "", modifier, keycode >> 2);
}
}
else {
char *keyval_name = gdk_keyval_name(keycode);
str_key = g_strdup_printf("%s%s%s%s%s ", user_data ? (gchar*)user_data : "", modifier, keyval_name[1] == 0 ? "" : "<", keyval_name, keyval_name[1] == 0 ? "" : ">");
}
if(key_binding->next_key_bindings) {
g_hash_table_foreach(key_binding->next_key_bindings, help_show_key_bindings_helper, str_key);
}
g_print("%30s { ", str_key);
for(key_binding_t *current_action = key_binding; current_action; current_action = current_action->next_action) {
g_print("%s(", pqiv_action_descriptors[current_action->action].name);
switch(pqiv_action_descriptors[current_action->action].parameter_type) {
case PARAMETER_NONE:
g_print(") ");
break;
case PARAMETER_INT:
g_print("%d) ", current_action->parameter.pint);
break;
case PARAMETER_DOUBLE:
g_print("%g) ", current_action->parameter.pdouble);
break;
case PARAMETER_CHARPTR:
for(const char *p = current_action->parameter.pcharptr; *p; p++) {
if(*p == ')' || *p == '\\') {
g_print("\\");
}
g_print("%c", *p);
}
g_print(") ");
break;
case PARAMETER_2SHORT:
g_print("%d, %d) ", current_action->parameter.p2short.p1, current_action->parameter.p2short.p2);
}
}
g_print("} \n");
g_free(str_key);
}/*}}}*/
gboolean help_show_key_bindings(const gchar *option_name, const gchar *value, gpointer data, GError **error) {/*{{{*/
gchar *old_locale = g_strdup(setlocale(LC_NUMERIC, NULL));
setlocale(LC_NUMERIC, "C");
g_hash_table_foreach(key_bindings, help_show_key_bindings_helper, (gpointer)"");
setlocale(LC_NUMERIC, old_locale);
g_free(old_locale);
exit(0);
return FALSE;
}/*}}}*/
void parse_key_bindings(const gchar *bindings) {/*{{{*/
/*
* This is a simple state machine based parser for the same format that the help function outputs:
* shortcut { command(parameter); command(parameter); }
*
* The states are
* 0 initial state, expecting keyboard shortcuts or EOF
* 1 keyboard shortcut entering started, expecting more or start of commands
* 2 expecting identifier inside <..>, e.g.
* 3 inside command list, e.g. after {. Expecting identifier of command.
* 4 inside command parameters, e.g. after (. Expecting parameter.
* 5 inside command list after state 4, same as 3 except that more commands
* add to the list instead of overwriting the old binding.
*/
GHashTable **active_key_bindings_table = &key_bindings;
int state = 0;
const gchar *token_start = NULL;
gchar *identifier;
ptrdiff_t identifier_length;
int keyboard_state = 0;
unsigned int keyboard_key_value;
key_binding_t *binding = NULL;
int parameter_type = 0;
const gchar *current_command_start = bindings;
gchar *error_message = NULL;
const gchar *scan;
gchar *old_locale = g_strdup(setlocale(LC_NUMERIC, NULL));
setlocale(LC_NUMERIC, "C");
for(scan = bindings; *scan; scan++) {
if(*scan == '\n' || *scan == '\r' || *scan == ' ' || *scan == '\t') {
if(token_start == scan) token_start++;
continue;
}
switch(state) {
case 0: // Expecting key description
current_command_start = scan;
case 1: // Expecting continuation of key description or start of command
switch(*scan) {
case '<':
token_start = scan + 1;
state = 2;
break;
case '{':
if(state == 0) {
error_message = g_strdup("Unallowed { before keyboard binding was given");
state = -1;
break;
}
token_start = scan + 1;
state = 3;
break;
default:
if(scan[0] == '\\' && scan[1]) {
scan++;
}
guint keyval = *scan;
if(keyboard_state & GDK_SHIFT_MASK) {
keyval = gdk_keyval_to_upper(keyval);
keyboard_state &= ~GDK_SHIFT_MASK;
}
keyboard_key_value = KEY_BINDING_VALUE(0, keyboard_state, keyval);
#define PARSE_KEY_BINDINGS_BIND(keyboard_key_value) \
keyboard_state = 0; \
if(!*active_key_bindings_table) { \
*active_key_bindings_table = g_hash_table_new((GHashFunc)g_direct_hash, (GEqualFunc)g_direct_equal); \
} \
binding = g_hash_table_lookup(*active_key_bindings_table, GUINT_TO_POINTER(keyboard_key_value)); \
if(!binding) { \
binding = g_slice_new0(key_binding_t); \
g_hash_table_insert(*active_key_bindings_table, GUINT_TO_POINTER(keyboard_key_value), binding); \
} \
active_key_bindings_table = &(binding->next_key_bindings);
PARSE_KEY_BINDINGS_BIND(keyboard_key_value);
state = 1;
}
break;
case 2: // Expecting identifier identifying a special key
// That's either Shift, Control, Alt, Mouse-%d, Mouse-Scroll-%d or gdk_keyval_name
// Closed by `>'
if(*scan == '>') {
identifier_length = scan - token_start;
if(identifier_length == 7 && g_ascii_strncasecmp(token_start, "mouse-", 6) == 0) {
// Is Mouse-
keyboard_key_value = KEY_BINDING_VALUE(1, keyboard_state, (token_start[6] - '0'));
PARSE_KEY_BINDINGS_BIND(keyboard_key_value);
}
else if(identifier_length == 14 && g_ascii_strncasecmp(token_start, "mouse-scroll-", 13) == 0) {
// Is Mouse-Scroll-
keyboard_key_value = KEY_BINDING_VALUE(1, keyboard_state, ((token_start[13] - '0') << 2));
PARSE_KEY_BINDINGS_BIND(keyboard_key_value);
}
else if(identifier_length == 5 && g_ascii_strncasecmp(token_start, "shift", 5) == 0) {
keyboard_state |= GDK_SHIFT_MASK;
}
else if(identifier_length == 7 && g_ascii_strncasecmp(token_start, "control", 7) == 0) {
keyboard_state |= GDK_CONTROL_MASK;
}
else if(identifier_length == 3 && g_ascii_strncasecmp(token_start, "alt", 3) == 0) {
keyboard_state |= GDK_MOD1_MASK;
}
else {
identifier = g_malloc(identifier_length + 1);
memcpy(identifier, token_start, identifier_length);
identifier[identifier_length] = 0;
guint keyval = gdk_keyval_from_name(identifier);
if(keyval == GDK_KEY_VoidSymbol) {
error_message = g_strdup_printf("Failed to parse key: `%s' is not a known GDK keyval name", identifier);
state = -1;
break;
}
if(keyboard_state & GDK_SHIFT_MASK) {
keyval = gdk_keyval_to_upper(keyval);
keyboard_state &= ~GDK_SHIFT_MASK;
}
keyboard_key_value = KEY_BINDING_VALUE(0, keyboard_state, keyval);
PARSE_KEY_BINDINGS_BIND(keyboard_key_value);
g_free(identifier);
}
token_start = NULL;
state = 1;
}
break;
case 3: // Expecting command identifier, ended by `(', or closing parenthesis
case 5: // Expecting further commands
if(token_start == scan && *scan == ';') {
token_start = scan + 1;
continue;
}
switch(*scan) {
case '(':
identifier_length = scan - token_start;
identifier = g_malloc(identifier_length + 1);
memcpy(identifier, token_start, identifier_length);
identifier[identifier_length] = 0;
if(binding->action && state == 5) {
binding->next_action = g_slice_new0(key_binding_t);
binding = binding->next_action;
}
state = -1;
unsigned int action_id = 0;
for(const struct pqiv_action_descriptor *descriptor = pqiv_action_descriptors; descriptor->name; descriptor++) {
if((ptrdiff_t)strlen(descriptor->name) == identifier_length && g_ascii_strncasecmp(descriptor->name, identifier, identifier_length) == 0) {
binding->action = action_id;
parameter_type = descriptor->parameter_type;
token_start = scan + 1;
state = 4;
break;
}
action_id++;
}
if(state != 4) {
error_message = g_strdup_printf("Unknown action: `%s'", identifier);
state = -1;
break;
}
g_free(identifier);
break;
case '}':
active_key_bindings_table = &key_bindings;
binding = NULL;
state = 0;
break;
}
break;
case 4: // Expecting action parameter, ended by `)'
if(parameter_type == PARAMETER_CHARPTR && *scan == '\\' && scan[1]) {
scan++;
continue;
}
if(*scan == ')') {
identifier_length = scan - token_start;
identifier = g_malloc(identifier_length + 1);
for(int i=0, j=0; j identifier_length) {
break;
}
}
identifier[i] = token_start[j];
}
identifier[identifier_length] = 0;
switch(parameter_type) {
case PARAMETER_NONE:
if(identifier_length > 0) {
error_message = g_strdup("This function does not expect a parameter");
state = -1;
break;
}
break;
case PARAMETER_INT:
binding->parameter.pint = atoi(identifier);
break;
case PARAMETER_DOUBLE:
binding->parameter.pdouble = atof(identifier);
break;
case PARAMETER_CHARPTR:
binding->parameter.pcharptr = g_strdup(identifier);
break;
case PARAMETER_2SHORT:
{
char *comma_pos = strchr(identifier, ',');
if(comma_pos) {
if(!strchr(comma_pos + 1, ',')) {
*comma_pos = 0;
for(comma_pos++; *comma_pos == '\t' || *comma_pos == '\n' || *comma_pos == ' '; comma_pos++);
binding->parameter.p2short.p1 = (short)atoi(identifier);
binding->parameter.p2short.p2 = (short)atoi(comma_pos);
break;
}
}
error_message = g_strdup("This function expects two parameters");
state = -1;
}
break;
}
g_free(identifier);
if(state == -1) {
break;
}
token_start = scan + 1;
state = 5;
}
break;
default:
error_message = g_strdup("Unexpected input");
state = -1;
break;
}
if(state == -1) {
break;
}
}
setlocale(LC_NUMERIC, old_locale);
g_free(old_locale);
if(state != 0) {
if(state != -1) {
error_message = g_strdup("Unexpected end of key binding definition");
}
g_printerr("Failed to parse key bindings. Error in definition:\n");
int error_pos = scan - current_command_start;
int print_after = strlen(scan);
if(print_after > 20) print_after = 20;
g_printerr("%*s\n", error_pos + print_after, current_command_start);
for(int i=0; iname; descriptor++) {
if((ptrdiff_t)strlen(descriptor->name) == identifier_length && g_ascii_strncasecmp(descriptor->name, action_name_start, identifier_length) == 0) {
command_found = TRUE;
gchar *parameter = g_malloc(parameter_length + 1);
for(int i=0, j=0; j parameter_length) {
break;
}
}
parameter[i] = parameter_start[j];
}
parameter[parameter_length] = 0;
pqiv_action_parameter_t parsed_parameter;
switch(descriptor->parameter_type) {
case PARAMETER_NONE:
if(parameter_length > 0) {
g_printerr("Invalid command: This command does not expect a parameter\n");
g_free(parameter);
return FALSE;
}
break;
case PARAMETER_INT:
parsed_parameter.pint = atoi(parameter);
break;
case PARAMETER_DOUBLE:
parsed_parameter.pdouble = atof(parameter);
break;
case PARAMETER_CHARPTR:
parsed_parameter.pcharptr = parameter;
break;
case PARAMETER_2SHORT:
{
char *comma_pos = strchr(parameter, ',');
if(comma_pos) {
if(!strchr(comma_pos + 1, ',')) {
*comma_pos = 0;
for(comma_pos++; *comma_pos == '\t' || *comma_pos == '\n' || *comma_pos == ' '; comma_pos++);
parsed_parameter.p2short.p1 = (short)atoi(parameter);
parsed_parameter.p2short.p2 = (short)atoi(comma_pos);
break;
}
}
g_printerr("Invalid command: This command expects two parameters\n");
g_free(parameter);
return FALSE;
}
break;
}
action(action_id, parsed_parameter);
g_free(parameter);
}
action_id++;
}
if(!command_found) {
g_printerr("Invalid command: Unknown command.\n");
return FALSE;
}
if(scan) {
return perform_string_action(scan);
}
return TRUE;
}/*}}}*/
gboolean read_commands_thread_helper(gpointer command) {/*{{{*/
perform_string_action((gchar *)command);
g_free(command);
return FALSE;
}/*}}}*/
gpointer read_commands_thread(gpointer user_data) {/*{{{*/
GIOChannel *stdin_reader =
#ifdef _WIN32
g_io_channel_win32_new_fd(_fileno(stdin));
#else
g_io_channel_unix_new(STDIN_FILENO);
#endif
gsize line_terminator_pos;
gchar *buffer = NULL;
const gchar *charset = NULL;
if(g_get_charset(&charset)) {
g_io_channel_set_encoding(stdin_reader, charset, NULL);
}
while(g_io_channel_read_line(stdin_reader, &buffer, NULL, &line_terminator_pos, NULL) == G_IO_STATUS_NORMAL) {
if (buffer == NULL) {
continue;
}
buffer[line_terminator_pos] = 0;
gdk_threads_add_idle(read_commands_thread_helper, buffer);
}
g_io_channel_unref(stdin_reader);
return NULL;
}/*}}}*/
void initialize_key_bindings() {/*{{{*/
key_bindings = g_hash_table_new((GHashFunc)g_direct_hash, (GEqualFunc)g_direct_equal);
for(const struct default_key_bindings_struct *kb = default_key_bindings; kb->key_binding_value; kb++) {
key_binding_t *nkb = g_slice_new(key_binding_t);
nkb->action = kb->action;
nkb->parameter = (pqiv_action_parameter_t)(kb->parameter);
nkb->next_action = NULL;
nkb->next_key_bindings = NULL;
g_hash_table_insert(key_bindings, GUINT_TO_POINTER(kb->key_binding_value), nkb);
}
}/*}}}*/
#endif
void recreate_window() {/*{{{*/
if(!main_window_visible) {
return;
}
g_signal_handlers_disconnect_by_func(main_window, G_CALLBACK(window_close_callback), NULL);
gtk_widget_destroy(GTK_WIDGET(main_window));
option_start_fullscreen = main_window_in_fullscreen;
main_window_visible = FALSE;
create_window();
}/*}}}*/
// }}}
#ifndef CONFIGURED_WITHOUT_INFO_TEXT
gboolean load_images_thread_update_info_text(gpointer user_data) {/*{{{*/
// If the window is already visible and new files have been found, update
// the info text every second
static gsize last_image_count = 0;
if(main_window_visible == TRUE) {
D_LOCK(file_tree);
gsize image_count = bostree_node_count(file_tree);
D_UNLOCK(file_tree);
if(image_count != last_image_count) {
last_image_count = image_count;
update_info_text(NULL);
info_text_queue_redraw();
}
}
return TRUE;
}/*}}}*/
#endif
gpointer load_images_thread(gpointer user_data) {/*{{{*/
#ifndef CONFIGURED_WITHOUT_INFO_TEXT
guint event_source;
if(user_data != NULL) {
// Use the info text updater only if this function was called in a separate
// thread (--lazy-load option)
event_source = gdk_threads_add_timeout(1000, load_images_thread_update_info_text, NULL);
}
#endif
load_images();
if(file_tree_valid) {
if(bostree_node_count(file_tree) == 0) {
g_printerr("No images left to display.\n");
exit(1);
}
}
if(option_lazy_load) {
gdk_threads_add_idle(initialize_gui_or_quit_callback, NULL);
}
#ifndef CONFIGURED_WITHOUT_INFO_TEXT
if(user_data != NULL) {
g_source_remove(event_source);
}
#endif
return NULL;
}/*}}}*/
int main(int argc, char *argv[]) {
#ifdef DEBUG
#ifndef _WIN32
struct rlimit core_limits;
core_limits.rlim_cur = core_limits.rlim_max = RLIM_INFINITY;
setrlimit(RLIMIT_CORE, &core_limits);
#endif
#endif
#ifndef _WIN32
XInitThreads();
#endif
#if (!GLIB_CHECK_VERSION(2, 32, 0))
g_thread_init(NULL);
gdk_threads_init();
#endif
gtk_init(&argc, &argv); // fyi, this generates a MemorySanitizer warning currently
initialize_file_type_handlers();
#ifndef CONFIGURED_WITHOUT_ACTIONS
initialize_key_bindings();
#endif
global_argc = argc;
global_argv = argv;
parse_configuration_file();
parse_command_line();
if(fabs(option_initial_scale - 1.0) > 2 * FLT_MIN) {
option_scale = FIXED_SCALE;
current_scale_level = option_initial_scale;
}
cairo_matrix_init_identity(¤t_transformation);
// TODO Deprecation warnings, remove with 2.6
if(option_reverse_cursor_keys) {
g_printerr("Warning: --reverse-cursor-keys is deprecated and will be removed in pqiv 2.6. Use --bind-key instead.\n");
}
if(option_reverse_scroll) {
g_printerr("Warning: --reverse-scroll is deprecated and will be removed in pqiv 2.6. Use --bind-key instead.\n");
}
#ifndef CONFIGURED_WITHOUT_ACTIONS
if(option_actions_from_stdin) {
if(option_addl_from_stdin) {
g_printerr("Error: --additional-from-stdin conflicts with --actions-from-stdin.\n");
exit(1);
}
g_thread_new("command-reader", read_commands_thread, NULL);
}
#endif
if(option_lazy_load) {
g_thread_new("image-loader", load_images_thread, GINT_TO_POINTER(1));
}
else {
load_images_thread(NULL);
if(!initialize_gui()) {
g_printerr("No images left to display.\n");
exit(1);
}
}
gtk_main();
// We are outside of the main thread again, so we can unload the remaining images
// We need to do this, because some file types create temporary files
//
// Note: If we locked the file_tree here, unload_image() could dead-lock
// (The wand backend has a global mutex and calls a function that locks file_tree)
// Instead, accept that in the worst case, some images might not be unloaded properly.
// At least, after file_tree_valid = FALSE, no new images will be inserted.
file_tree_valid = FALSE;
D_LOCK(file_tree);
D_UNLOCK(file_tree);
abort_pending_image_loads(NULL);
for(BOSNode *node = bostree_select(file_tree, 0); node; node = bostree_next_node(node)) {
// Iterate over the images ourselves, because there might be open weak references which
// prevent this to be called from bostree_destroy.
unload_image(node);
}
D_LOCK(file_tree);
bostree_destroy(file_tree);
D_UNLOCK(file_tree);
return 0;
}
// vim:noet ts=4 sw=4 tw=0 fdm=marker
pqiv-2.6/pqiv.h 0000664 0000000 0000000 00000017335 12737703411 0013506 0 ustar 00root root 0000000 0000000 /**
* pqiv
*
* Copyright (c) 2013-2014, Phillip Berndt
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
*/
// This file contains the definition of files, image types and
// the plugin infrastructure. It should be included in file type
// handlers.
#ifndef _PQIV_H_INCLUDED
#define _PQIV_H_INCLUDED
#include
#include
#include
#include "lib/bostree.h"
#ifndef PQIV_VERSION
#define PQIV_VERSION "2.6"
#endif
#define FILE_FLAGS_ANIMATION (guint)(1)
#define FILE_FLAGS_MEMORY_IMAGE (guint)(1<<1)
// The structure for images {{{
typedef struct file_type_handler_struct_t file_type_handler_t;
typedef struct {
// File type
const file_type_handler_t *file_type;
// Special flags
// FILE_FLAGS_ANIMATION -> Animation functions are invoked
// Set by file type handlers
// FILE_FLAGS_MEMORY_IMAGE -> File lives in memory
guint file_flags;
// The file name to display
// Must be different from file_name, because it is free()d seperately
gchar *display_name;
// The name to sort by
// Must be set if option_sort is set; in backends the simplest approach
// is to only touch this if it is not NULL
gchar *sort_name;
// The URI or file name of the file
gchar *file_name;
// If the file is a memory image, the actual image data
GBytes *file_data;
// The file monitor structure is used for inotify-watching of
// the files
GFileMonitor *file_monitor;
// This flag stores whether this image is currently loaded
// and valid. i.e. if it is set, you can assume that
// private_data contains a representation of the image;
// if not, you can NOT assume that it does not.
gboolean is_loaded : 1;
// This flag determines whether this file should be reloaded
// despite is_loaded being set
gboolean force_reload : 1;
// Cached image size
guint width;
guint height;
// File-type specific data, allocated and freed by the file type handlers
void *private;
} file_t;
// }}}
// Definition of the built-in file types {{{
// If you want to implement your own file type, you'll have to implement the
// following functions and a non-static initialization function named
// file_type_NAME_initializer that fills a file_type_handler_t with pointers to
// the functions. Store the file in backends/NAME.c and adjust the Makefile to
// add the required libraries if your backend is listed in the $(BACKENDS)
// variable.
typedef enum { PARAMETER, RECURSION, INOTIFY, BROWSE_ORIGINAL_PARAMETER, FILTER_OUTPUT } load_images_state_t;
// Allocation function: Allocate the ->private structure within a file and add the
// image(s) to the list of available images via load_images_handle_parameter_add_file()
// If an image is not to be loaded for any reason, the file structure should be
// deallocated using file_free()
// Returns a pointer to the first added image
// Optional, you can also set the pointer to this function to NULL.
// If new file_t structures are needed, use image_loader_duplicate_file
typedef BOSNode *(*file_type_alloc_fn_t)(load_images_state_t state, file_t *file);
// Deallocation, if a file is removed from the images list. Free the ->private structure.
// Only called if ->private is non-NULL.
typedef void (*file_type_free_fn_t)(file_t *file);
// Actually load a file into memory
typedef void (*file_type_load_fn_t)(file_t *file, GInputStream *data, GError **error_pointer);
// Unload a file
typedef void (*file_type_unload_fn_t)(file_t *file);
// Animation support: Initialize memory for animations, return ms until first frame
// Optional, you can also set the pointer to this function to NULL.
typedef double (*file_type_animation_initialize_fn_t)(file_t *file);
// Animation support: Advance to the next frame, return ms until next frame
// Optional, you can also set the pointer to this function to NULL.
typedef double (*file_type_animation_next_frame_fn_t)(file_t *file);
// Draw the current view to a cairo context
typedef void (*file_type_draw_fn_t)(file_t *file, cairo_t *cr);
struct file_type_handler_struct_t {
// All files will be filtered with this filter. If it lets it pass,
// a handler is assigned to a file. If none do, the file is
// discarded if it was found during directory traversal or
// loaded using the first image backend if it was an explicit
// parameter.
GtkFileFilter *file_types_handled;
// Pointers to the functions defined above
file_type_alloc_fn_t alloc_fn;
file_type_free_fn_t free_fn;
file_type_load_fn_t load_fn;
file_type_unload_fn_t unload_fn;
file_type_animation_initialize_fn_t animation_initialize_fn;
file_type_animation_next_frame_fn_t animation_next_frame_fn;
file_type_draw_fn_t draw_fn;
};
// Initialization function: Tell pqiv about a backend
typedef void (*file_type_initializer_fn_t)(file_type_handler_t *info);
// pqiv symbols available to plugins {{{
// Global cancellable that should be used for every i/o operation
extern GCancellable *image_loader_cancellable;
// Current scale level. For backends that don't support cairo natively.
extern gdouble current_scale_level;
// Load a file from disc/memory/network
GInputStream *image_loader_stream_file(file_t *file, GError **error_pointer);
// Duplicate a file_t; the private section does not get duplicated, only the pointer gets copied
file_t *image_loader_duplicate_file(file_t *file, gchar *custom_display_name, gchar *custom_sort_name);
// Add a file to the list of loaded files
// Should be called at least once in a file_type_alloc_fn_t, with the state being
// forwarded unaltered.
BOSNode *load_images_handle_parameter_add_file(load_images_state_t state, file_t *file);
// Load all data from an input stream into memory, conveinience function
GBytes *g_input_stream_read_completely(GInputStream *input_stream, GCancellable *cancellable, GError **error_pointer);
// Free a file
void file_free(file_t *file);
// }}}
// File type handlers, used in the initializer and file type guessing
extern file_type_handler_t file_type_handlers[];
/* }}} */
// The means to control pqiv remotely {{{
typedef enum {
ACTION_NOP,
ACTION_SHIFT_Y,
ACTION_SHIFT_X,
ACTION_SET_SLIDESHOW_INTERVAL_RELATIVE,
ACTION_SET_SLIDESHOW_INTERVAL_ABSOLUTE,
ACTION_SET_SCALE_LEVEL_RELATIVE,
ACTION_SET_SCALE_LEVEL_ABSOLUTE,
ACTION_TOGGLE_SCALE_MODE,
ACTION_TOGGLE_SHUFFLE_MODE,
ACTION_RELOAD,
ACTION_RESET_SCALE_LEVEL,
ACTION_TOGGLE_FULLSCREEN,
ACTION_FLIP_HORIZONTALLY,
ACTION_FLIP_VERTICALLY,
ACTION_ROTATE_LEFT,
ACTION_ROTATE_RIGHT,
ACTION_TOGGLE_INFO_BOX,
ACTION_JUMP_DIALOG,
ACTION_TOGGLE_SLIDESHOW,
ACTION_HARDLINK_CURRENT_IMAGE,
ACTION_GOTO_DIRECTORY_RELATIVE,
ACTION_GOTO_FILE_RELATIVE,
ACTION_QUIT,
ACTION_NUMERIC_COMMAND,
ACTION_COMMAND,
ACTION_ADD_FILE,
ACTION_GOTO_FILE_BYINDEX,
ACTION_GOTO_FILE_BYNAME,
ACTION_REMOVE_FILE_BYINDEX,
ACTION_REMOVE_FILE_BYNAME,
ACTION_OUTPUT_FILE_LIST,
ACTION_SET_CURSOR_VISIBILITY,
ACTION_SET_STATUS_OUTPUT,
ACTION_SET_SCALE_MODE_FIT_PX,
ACTION_SET_SHIFT_X,
ACTION_SET_SHIFT_Y,
ACTION_BIND_KEY,
ACTION_SEND_KEYS,
ACTION_SET_SHIFT_ALIGN_CORNER
} pqiv_action_t;
typedef union {
int pint;
double pdouble;
char *pcharptr;
struct {
short p1;
short p2;
} p2short;
} pqiv_action_parameter_t;
void action(pqiv_action_t action, pqiv_action_parameter_t parameter);
// }}}
#endif