pax_global_header00006660000000000000000000000064137607054650014527gustar00rootroot0000000000000052 comment=21966c22a3204522452dc6c0d6d54cc82bc8e116 pqiv-2.12/000077500000000000000000000000001376070546500124325ustar00rootroot00000000000000pqiv-2.12/.github/000077500000000000000000000000001376070546500137725ustar00rootroot00000000000000pqiv-2.12/.github/workflows/000077500000000000000000000000001376070546500160275ustar00rootroot00000000000000pqiv-2.12/.github/workflows/ci.yml000066400000000000000000000007231376070546500171470ustar00rootroot00000000000000name: CI build on: [push] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - name: prepare run: sudo apt-get update && sudo apt-get install build-essential libgtk-3-dev libmagickwand-dev libarchive-dev libpoppler-glib-dev libavformat-dev libavcodec-dev libswscale-dev libavutil-dev libwebp-dev - name: configure run: ./configure - name: make run: make - name: check run: ./pqiv --version pqiv-2.12/GNUmakefile000066400000000000000000000260371376070546500145140ustar00rootroot00000000000000# 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=1 -Wwrite-strings -Waggregate-return -Wunreachable-code -Wno-unused-parameter LDLIBS=-lm PREFIX=/usr EPREFIX=$(PREFIX) LIBDIR=$(PREFIX)/lib BINDIR=$(PREFIX)/bin 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 lib/thumbnailcache.o HEADERS=pqiv.h lib/bostree.h lib/filebuffer.h lib/strnatcmp.h BACKENDS=gdkpixbuf EXTRA_DEFS= BACKENDS_BUILD=static EXTRA_CFLAGS_SHARED_OBJECTS=-fPIC EXTRA_CFLAGS_BINARY= EXTRA_LDFLAGS_SHARED_OBJECTS= EXTRA_LDFLAGS_BINARY= # Always look for source code relative to the directory of this makefile SOURCEDIR:=$(dir $(abspath $(lastword $(MAKEFILE_LIST)))) ifeq ($(SOURCEDIR),$(CURDIR)) SOURCEDIR= else HEADERS:=$(patsubst %, $(SOURCEDIR)%, $(HEADERS)) endif # Load config.make (created by configure) CONFIG_MAKE_NAME=config.make ifeq ($(wildcard $(CONFIG_MAKE_NAME)),$(CONFIG_MAKE_NAME)) include $(CONFIG_MAKE_NAME) HEADERS+=$(CONFIG_MAKE_NAME) endif # First things first: Require at least one backend ifeq ($(BACKENDS),) $(error Building pqiv without any backends is unsupported.) 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 LIBS_archive=libarchive LIBS_webp=libwebp # 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)"), ) override GTK_VERSION=3 else LIBS=$(LIBS_GTK2) override GTK_VERSION=2 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, ifeq ($(EXECUTABLE_EXTENSION), .exe) LIBS+=gio-windows-2.0 else LIBS+=gio-unix-2.0 endif # We need X11 to workaround a bug, see http://stackoverflow.com/questions/18647475 ifeq ($(filter x11, $(shell $(PKG_CONFIG) --errors-to-stdout --variable=target gtk+-$(GTK_VERSION).0; $(PKG_CONFIG) --errors-to-stdout --variable=targets gtk+-$(GTK_VERSION).0)), x11) LIBS+=x11 endif # Add backend-specific libraries and objects SHARED_OBJECTS= SHARED_BACKENDS= HELPER_OBJECTS= 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 HELPER_OBJECTS+=backends/$(1).o 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 $(SOURCEDIR)backends/*.c), $(eval $(call handle-backend,$(basename $(notdir $(BACKEND_C)))))) PIXBUF_FILTER="gdkpixbuf", ifeq ($(BACKENDS_BUILD), shared) OBJECTS+=backends/shared-initializer.o BACKENDS_BUILD_CFLAGS_shared-initializer=-DSHARED_BACKENDS='$(filter $(PIXBUF_FILTER), $(SHARED_BACKENDS)) $(filter-out $(PIXBUF_FILTER), $(SHARED_BACKENDS))' -DSEARCH_PATHS='"backends", "../$(subst $(PREFIX),,$(LIBDIR))/pqiv", "$(LIBDIR)/pqiv",' LIBS+=gmodule-2.0 else OBJECTS+=$(BACKENDS_INITIALIZER).o endif # MagickWand changed their directory structure with version 7, pass the version # to the build ifneq ($(findstring wand, $(BACKENDS)),) backends/wand.o: CFLAGS_REAL+=-DWAND_VERSION=$(shell $(PKG_CONFIG) --modversion MagickWand | awk 'BEGIN { FS="." } { print $$1 }') endif # Add version information to builds from git PQIV_VERSION_STRING=$(shell [ -d $(SOURCEDIR).git ] && (which git 2>&1 >/dev/null) && git -C "$(SOURCEDIR)" describe --dirty --tags 2>/dev/null) 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) $(DEBUG_CFLAGS) $(EXTRA_DEFS) $(shell $(PKG_CONFIG) --cflags "$(LIBS)") LDLIBS_REAL=$(shell $(PKG_CONFIG) --libs "$(LIBS)") $(LDLIBS) LDFLAGS_REAL=$(LDFLAGS) all: pqiv$(EXECUTABLE_EXTENSION) pqiv.desktop $(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) $(EXTRA_CFLAGS_BINARY) -o $@ $+ $(LDLIBS_REAL) $(LDFLAGS_REAL) $(EXTRA_LDFLAGS_BINARY) ifeq ($(BACKENDS_BUILD), shared) backends/%.o: CFLAGS_REAL+=$(BACKENDS_BUILD_CFLAGS_$(notdir $*)) $(EXTRA_CFLAGS_SHARED_OBJECTS) $(SHARED_OBJECTS): backends/pqiv-backend-%.so: backends/%.o @[ -d backends ] || mkdir -p backends || true $(SILENT_CCLD) $(CROSS)$(CC) $(CPPFLAGS) $(EXTRA_CFLAGS_SHARED_OBJECTS) -o $@ $+ $(LDLIBS_REAL) $(LDFLAGS_REAL) $(BACKENDS_BUILD_LDLIBS_$*) $(EXTRA_LDFLAGS_SHARED_OBJECTS) -shared endif $(filter-out $(BACKENDS_INITIALIZER).o, $(OBJECTS)) $(HELPER_OBJECTS): %.o: $(SOURCEDIR)%.c $(HEADERS) @[ -d $(dir $@) ] || mkdir -p $(dir $@) || true $(SILENT_CC) $(CROSS)$(CC) $(CPPFLAGS) -c -o $@ $(CFLAGS_REAL) $< $(BACKENDS_INITIALIZER).o: $(BACKENDS_INITIALIZER).c $(HEADERS) @[ -d $(dir $@) ] || mkdir -p $(dir $@) || true $(SILENT_CC) $(CROSS)$(CC) $(CPPFLAGS) -I"$(SOURCEDIR)/lib" -c -o $@ $(CFLAGS_REAL) $< $(BACKENDS_INITIALIZER).c: @[ -d $(dir $(BACKENDS_INITIALIZER)) ] || mkdir -p $(dir $(BACKENDS_INITIALIZER)) || true @$(foreach BACKEND, $(sort $(BACKENDS)), [ -e $(SOURCEDIR)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(const gchar * const * disabled_backends) {"; \ echo " int i = 0;"; \ $(foreach BACKEND, $(filter gdkpixbuf, $(BACKENDS)), echo " if(!strv_contains(disabled_backends, \"$(BACKEND)\")) file_type_$(BACKEND)_initializer(&file_type_handlers[i++]);";) \ $(foreach BACKEND, $(sort $(filter-out gdkpixbuf, $(BACKENDS))), echo " if(!strv_contains(disabled_backends, \"$(BACKEND)\")) file_type_$(BACKEND)_initializer(&file_type_handlers[i++]);";) \ echo "}" \ ) > $@ pqiv.desktop: $(HEADERS) $(SILENT_GEN) ( \ echo "[Desktop Entry]"; \ echo "Version=1.0"; \ echo "Type=Application"; \ echo "Comment=Powerful quick image viewer"; \ echo "Name=pqiv"; \ echo "NoDisplay=true"; \ echo "Icon=emblem-photos"; \ echo "TryExec=$(PREFIX)/bin/pqiv"; \ echo "Exec=$(PREFIX)/bin/pqiv %F"; \ echo "MimeType=$(shell cat $(foreach BACKEND, $(sort $(BACKENDS)), $(SOURCEDIR)backends/$(BACKEND).mime) /dev/null | sort | uniq | awk 'ORS=";"')"; \ echo "Categories=Graphics;"; \ echo "Keywords=Viewer;" \ ) > $@ install: all mkdir -p $(DESTDIR)$(BINDIR) install pqiv$(EXECUTABLE_EXTENSION) $(DESTDIR)$(BINDIR)/pqiv$(EXECUTABLE_EXTENSION) -mkdir -p $(DESTDIR)$(MANDIR)/man1 -install -m 644 $(SOURCEDIR)pqiv.1 $(DESTDIR)$(MANDIR)/man1/pqiv.1 -mkdir -p $(DESTDIR)$(PREFIX)/share/applications -install -m 644 pqiv.desktop $(DESTDIR)$(PREFIX)/share/applications/pqiv.desktop ifeq ($(BACKENDS_BUILD), shared) mkdir -p $(DESTDIR)$(LIBDIR)/pqiv install $(SHARED_OBJECTS) $(DESTDIR)$(LIBDIR)/pqiv/ endif uninstall: rm -f $(DESTDIR)$(PREFIX)/bin/pqiv$(EXECUTABLE_EXTENSION) rm -f $(DESTDIR)$(MANDIR)/man1/pqiv.1 rm -f $(DESTDIR)$(PREFIX)/share/applications/pqiv.desktop ifeq ($(BACKENDS_BUILD), shared) rm -f $(foreach SO_FILE, $(SHARED_OBJECTS), $(DESTDIR)$(LIBDIR)/pqiv/$(notdir $(SO_FILE))) rmdir $(DESTDIR)$(LIBDIR)/pqiv endif # Rudimentary MacOS bundling # Only really useful for opening pqiv using "open pqiv.app --args ..." from the # command line right now, but that already has the benefit that the application # window will be visible right away pqiv.app: pqiv.app.tmp rm -f ../$@ cd pqiv.app.tmp && zip -9r ../$@ . pqiv.app.tmp: pqiv.app.tmp/Contents/MacOS/pqiv pqiv.app.tmp/Contents/Info.plist pqiv.app.tmp/Contents/PkgInfo pqiv.app.tmp/Contents/MacOS/pqiv: -mkdir -p pqiv.app.tmp/Contents/MacOS install pqiv$(EXECUTABLE_EXTENSION) $@ pqiv.app.tmp/Contents/PkgInfo: -mkdir -p pqiv.app.tmp/Contents $(SILENT_GEN) ( \ echo -n "APPL????"; \ ) > $@ pqiv.app.tmp/Contents/Info.plist: $(HEADERS) -mkdir -p pqiv.app.tmp/Contents $(SILENT_GEN) ( \ echo ''; \ echo 'CFBundleNamepqivCFBundleDisplayNamepqiv'; \ echo 'CFBundleIdentifiercom.pberndt.pqivCFBundleVersion$(PQIV_VERSION_STRING)'; \ echo 'CFBundlePackageTypeAPPLCFBundleExecutablepqivLSMinimumSystemVersion'; \ echo '10.4CFBundleDocumentTypesCFBundleTypeMIMETypes'; \ cat $(foreach BACKEND, $(sort $(BACKENDS)), $(SOURCEDIR)backends/$(BACKEND).mime) /dev/null | sort | uniq | awk '{print "" $$0 ""}'; \ echo ''; \ ) > $@ clean: rm -f pqiv$(EXECUTABLE_EXTENSION) *.o backends/*.o backends/*.so lib/*.o backends/initializer-*.c pqiv.desktop distclean: clean rm -f config.make get_libs: $(info LIBS: $(LIBS)) @true get_available_backends: @OUT=; $(foreach BACKEND_C, $(wildcard $(SOURCEDIR)backends/*.c), \ [ "$(DISABLE_AUTOMATED_BUILD_$(basename $(notdir $(BACKEND_C))))" != "yes" ] && \ [ -n "$(LIBS_$(basename $(notdir $(BACKEND_C))))" ] && \ $(PKG_CONFIG) --exists "$(LIBS_$(basename $(notdir $(BACKEND_C))))" \ && OUT="$$OUT $(basename $(notdir $(BACKEND_C))) ";) echo BACKENDS: $$OUT @true pqiv-2.12/LICENSE000066400000000000000000001045131376070546500134430ustar00rootroot00000000000000 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.12/README.markdown000066400000000000000000000377211376070546500151450ustar00rootroot00000000000000PQIV README =========== About pqiv ---------- pqiv is a powerful GTK 3 based command-line image viewer with a minimal UI. It is highly customizable, can be fully controlled from scripts, and has support for various file formats including PDF, Postscript, video files and archives. It is optimized to be quick and responsive. It comes with support for animations, slideshows, transparency, VIM-like key bindings, automated loading of new images as they appear, external image filters, marks, image preloading, and much more. pqiv started as a Python rewrite of qiv avoiding imlib, but evolved into a much more powerful tool. Today, pqiv stands for powerful quick image viewer. Features -------- * Recursive loading from directories * Can watch files and directories for changes * Sorts images in natural order * Has a status bar showing information on the current image * Comes with transparency support * Can move/zoom/rotate/flip images * Can pipe images through external filters * Loads the next image in the background for quick response times * Caches zoomed images for smoother movement * Supports fade image transition animations * Supports various image and video formats through a rich set of backends * Comes with an interactive montage mode (a.k.a. "image grid") * Customizable key-bindings with support for VIM-like key sequences, action cycling and binding multiple actions to a single key * Mark/unmark images and pipe the list of marked images to an external script Installation ------------ Usual stuff. `./configure && make && make install`. The configure script is optional if you only want gdk-pixbuf support and will auto-determine which backends to build if invoked without parameters. You can also use precompiled and packaged versions of pqiv. Note that the distribution packages are usually somewhat out of date: * [Nightly builds for Debian, Ubuntu, SUSE and Fedora](https://build.opensuse.org/package/show/home:phillipberndt/pqiv) thanks to the OpenSUSE build service * [Arch AUR package](https://aur.archlinux.org/packages/pqiv/) ([Git version](https://aur.archlinux.org/packages/pqiv-git/)) * [CRUX port](https://crux.nu/portdb/?a=search&q=pqiv) * [Debian package](https://packages.debian.org/en/sid/pqiv) * [FreeBSD port](https://www.freshports.org/graphics/pqiv/) * [Gentoo ebuild](https://packages.gentoo.org/packages/media-gfx/pqiv) * [macOS brew](http://braumeister.org/formula/pqiv) * [NixOS package](https://github.com/NixOS/nixpkgs/blob/HEAD/pkgs/applications/graphics/pqiv/default.nix) * [OpenBSD port](http://cvsweb.openbsd.org/cgi-bin/cvsweb/ports/graphics/pqiv/) If you'd like to compile pqiv manually, you'll need * gtk+ 3.0 *or* gtk+ 2.6 * gdk-pixbuf 2.2 (included in gtk+) * glib 2.32 (with gvfs for opening URLs) * cairo 1.6 * gio 2.0 * gdk 2.8 and optionally also * ffmpeg / libav (for video support) * libarchive (for images in archives and cbX comic book files) * libspectre (any version, for ps/eps support) * libwebp (for WebP support) * MagickWand (any version, for additional image formats like psd) * poppler (any version, for pdf support) 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 or to make the run-time dependencies optional, use the `--backends-build=shared` configure option. For macOS, have a look at the `pqiv.app` target of the Makefile, too. pqiv can be linked statically, though GTK only supports static linking in GTK 2.x; in early versions of GTK 3.x it was fairly simple to still link statically. Windows builds are supported and work in GTK 2.x, it is recommended to use [MXE](https://mxe.cc/) for cross-compiling. Thanks ------ This program uses Martin Pool's natsort algorithm . Contributors ------------ Contributors to pqiv 2.x are: * J. Paul Reed * Chen Jonh L * Anton Älgmyr * Christian Garbs * Kanon Kubose 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. Examples -------- Basic usage of pqiv is very straightforward, call pqiv and then use space, backspace, `f` (for fullscreen), `q` (to quit), and `m` for the montage overview to navigate through your images. To see all key bindings, see the `DEFAULT KEY BINDINGS` section of the man-page, or run `pqiv --show-bindings`. For some advanced uses of pqiv, take a look at these resouces: * [Play music while looking at specific images](https://github.com/phillipberndt/pqiv/issues/100#issuecomment-320651190) *
Bind keys to cycle through panels of a 2x2 comic Store this in your `.pqivrc`: ``` # Bind c to act as if "#c1" was typed c { send_keys(#c1); } # If "#c1" is typed, shift the current image to it's north west corner, and # rebind "c" to act as if "#c2" was typed c1 { set_shift_align_corner(NW); bind_key(c { send_keys(#c2\); }); } # ..etc.. c2 { set_shift_align_corner(NE); bind_key(c { send_keys(#c3\); }); } c3 { set_shift_align_corner(SW); bind_key(c { send_keys(#c4\); }); } # The last binding closes the cycle by rebinding "c" to act as if "#c1" was typed c4 { set_shift_align_corner(SE); bind_key(c { send_keys(#c1\); }); } ```
Changelog --------- pqiv 2.12 * Fix external image filters (Fixes #182) * Fix support for `best` interpolation quality (Fixes #139) * Fix wrap-around in shuffled image view (Fixes #176) * Fix max-depth behavior if the argument is a file (Fixes #170) * Allow keybinding of special keys with shift modifier * Add `--auto-montage-mode` (Fixes #181) * Replace GTimeVal with GDateTime for glib 2.62 support * Add an sxiv-like marks system
Click to expand changelog for old pqiv versions pqiv 2.11 * Added negate (color inversion) mode (bound to `n`, `--negate`) * Rebound `a` (hardlink image) to `c-a` by default (See #124) * Improved key bindings documentation (See #127) * Add `--actions-from-stdin` and let it block until actions are completed (See #118/#119) * Fix zooming on tiling WMs (See #129) * Support ffmpeg 4.0 API * Fix cross-compiling with X11 (Debian #913589) * Fix resizing in WMs without moveresize support (See #130) * Work around GTK bug resulting in crash due to invalid free() * Improve autotools compatibility of the configure script (See #135) pqiv 2.10.4 * Fix output of `montage_mode_shift_y_rows()` in key bindings * Update the info text when the background pattern is cycled * Prevent potential crashes in poppler backend for rapid image movements * Fix processing of dangling symlinks in the file buffer * Removed possible deadlock in ImageMagick wand backend * Fix --command-9 shortcut * Makefile: Move -shared compiler flag to the end of the command line pqiv 2.10 * Enable cursor auto-hide by default * Enable mouse navigation in montage mode * Added `toggle_background_pattern()` (bound to `b`) and `--background-pattern`. * Added support for alternate pqivrc paths, changed recommended location to ./.config/pqivrc. * Sped up `--low-memory` mode (using native- instead of image-surfaces) * Fixed graphical issues with fading mode and quick image transition * Fixed support for platforms with `sizeof(time_t) != sizeof(int)` * Fixed a race condition in the file buffer map pqiv 2.9 * Added a montage/image grid mode (bound to `m` by default) * Added a [WebP](https://developers.google.com/speed/webp/) backend (by @john0312) * Added the means to skip over "logical" directories, such as archive files (bound to `ctrl+space` and `ctrl+backspace` by default) * Improved responsivity by caching pre-scaled copies of images * Removed tearing/flickering in WMs without extended frame sync support * Fixed support for huge images (>32,767px) in the GdkPixbuf backend * Added option --info-box-colors to customize the colors used in the info box * It is now possible to view --help even if no display is available * Added --version * Added an auto scale mode that maintains window size * Bound `Control+t` to switch to "maintain scale level" by default * Bound `Alt+t` to switch to "maintain window size" by default * Added action `move_window()` to explicitly move pqiv's main window around pqiv 2.8.5 * Fixed an issue where the checkerboard pattern sometimes was visible at image borders * Fixed image rotation in low-memory mode * Fix a memory leak (leaking a few bytes each time an image is drawn) * Correctly handle string arguments from the configuration file * Fix building with old glib versions that do not expose their x11 dependency in pkgconfig * Fix support for duplicate files in sorted mode * Fix MagickWand exit handler code pqiv 2.8 * Added option --allow-empty-window: Show pqiv even if no images can be loaded * Explicitly allow to load all files from a directory multiple times * Allow to use --libdir option in configure to override .so-files location * Fix shared-backend-pqiv in environments that compile with --enable-new-dtags * Enable the libav backend by default * Add option --disable-backends to disable backends at runtime pqiv 2.7.4 * Fix GTK 2 compilation * Fix backends list in configure script * Fix race condition upon reloading animations * Fix Ctrl-R default binding (move `goto_earlier_file()` to Ctrl-P) pqiv 2.7 * Fixed window decoration toggling with --transparent-background * Work around bug #67, poppler bug #96884 * Added new action `set_interpolation_quality` to change interpolation/filter mode * pqiv now by default uses `nearest` interpolation for small images * Added actions and key bindings to control animation playback speed * Added a general archive backend for reading images from archives * Added a new action `goto_earlier_file()` to return to the image that was shown before the current one * Added a new action `set_cursor_auto_hide()` to automatically hide the pointer when it is not moved for some time * Support an `actions` section in the configuration file for default actions * Create and install a desktop file for pqiv during install * Disable GTK's transparent scaling on HiDpi monitors * New option --wait-for-images-to-appear to wait for images to appear if none are found 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.12/backends/000077500000000000000000000000001376070546500142045ustar00rootroot00000000000000pqiv-2.12/backends/archive.c000066400000000000000000000174211376070546500157760ustar00rootroot00000000000000/** * pqiv * * Copyright (c) 2013-2017, 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 * * This is the non-comicbook variant that handles arbitrary archives * (recursively, if necessary). * */ #include "../pqiv.h" #include "../lib/filebuffer.h" #include #include #include typedef struct { // The source archive file_t *source_archive; // The path to the target file within the archive gchar *entry_name; } file_loader_delegate_archive_t; static struct archive *file_type_archive_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; }/*}}}*/ void file_type_archive_data_free(file_loader_delegate_archive_t *data) {/*{{{*/ if(data->source_archive) { file_free(data->source_archive); data->source_archive = NULL; } g_free(data); }/*}}}*/ GBytes *file_type_archive_data_loader(file_t *file, GError **error_pointer) {/*{{{*/ const file_loader_delegate_archive_t *archive_data = g_bytes_get_data(file->file_data, NULL); GBytes *data = buffered_file_as_bytes(archive_data->source_archive, NULL, error_pointer); if(!data) { g_printerr("Failed to load archive %s: %s\n", file->display_name, error_pointer && *error_pointer ? (*error_pointer)->message : "Unknown error"); g_clear_error(error_pointer); return NULL; } struct archive *archive = file_type_archive_gen_archive(data); if(!archive) { buffered_file_unref(file); return NULL; } // Find the proper entry size_t entry_size = 0; void *entry_data = NULL; struct archive_entry *entry; while(archive_read_next_header(archive, &entry) == ARCHIVE_OK) { if(archive_data->entry_name && strcmp(archive_data->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 NULL; } break; } } archive_read_free(archive); buffered_file_unref(archive_data->source_archive); 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 NULL; } return g_bytes_new_take(entry_data, entry_size); }/*}}}*/ BOSNode *file_type_archive_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 FALSE_POINTER; } struct archive *archive = file_type_archive_gen_archive(data); if(!archive) { buffered_file_unref(file); file_free(file); return FALSE_POINTER; } GtkFileFilterInfo file_filter_info; file_filter_info.contains = GTK_FILE_FILTER_FILENAME | GTK_FILE_FILTER_DISPLAY_NAME; BOSNode *first_node = FALSE_POINTER; struct archive_entry *entry; while(archive_read_next_header(archive, &entry) == ARCHIVE_OK) { const gchar *entry_name = archive_entry_pathname(entry); #if ARCHIVE_VERSION_NUMBER < 3003002 // Affected by libarchive bug #869 if(archive_entry_size(entry) == 0) { const char *archive_format = archive_format_name(archive); if(strncmp("ZIP", archive_format, 3) == 0) { g_printerr("Failed to load archive %s: This ZIP file is affected by libarchive bug #869, which was fixed in v3.3.2. Skipping file.\n", file->display_name); archive_read_free(archive); buffered_file_unref(file); file_free(file); return FALSE_POINTER; } } #endif // Prepare a new file_t for this entry gchar *sub_name = g_strdup_printf("%s#%s", file->display_name, entry_name); file_t *new_file = image_loader_duplicate_file(file, g_strdup(sub_name), g_strdup(sub_name), sub_name); if(new_file->file_data) { g_bytes_unref(new_file->file_data); new_file->file_data = NULL; } size_t delegate_struct_alloc_size = sizeof(file_loader_delegate_archive_t) + strlen(entry_name) + 2; file_loader_delegate_archive_t *new_file_data = g_malloc(delegate_struct_alloc_size); new_file_data->source_archive = image_loader_duplicate_file(file, NULL, NULL, NULL); new_file_data->entry_name = (char *)(new_file_data) + sizeof(file_loader_delegate_archive_t) + 1; memcpy(new_file_data->entry_name, entry_name, strlen(entry_name) + 1); new_file->file_data = g_bytes_new_with_free_func(new_file_data, delegate_struct_alloc_size, (GDestroyNotify)file_type_archive_data_free, new_file_data); new_file->file_flags |= FILE_FLAGS_MEMORY_IMAGE; new_file->file_data_loader = file_type_archive_data_loader; // Find an appropriate handler for this file gchar *name_lowerc = g_utf8_strdown(entry_name, -1); file_filter_info.filename = file_filter_info.display_name = name_lowerc; // Check if one of the file type handlers can handle this file BOSNode *node = load_images_handle_parameter_find_handler(entry_name, state, new_file, &file_filter_info); if(node == NULL) { // No handler found. We could fall back to using a default. Free new_file instead. file_free(new_file); } else if(node == FALSE_POINTER) { // File type is known, but loading failed; new_file has already been free()d node = NULL; } else if(first_node == FALSE_POINTER) { first_node = node; } g_free(name_lowerc); archive_read_data_skip(archive); } archive_read_free(archive); buffered_file_unref(file); file_free(file); return first_node; }/*}}}*/ void file_type_archive_initializer(file_type_handler_t *info) {/*{{{*/ // Fill the file filter pattern info->file_types_handled = gtk_file_filter_new(); // Mime types for archives gtk_file_filter_add_mime_type(info->file_types_handled, "application/x-tar"); gtk_file_filter_add_mime_type(info->file_types_handled, "application/x-zip"); gtk_file_filter_add_mime_type(info->file_types_handled, "application/x-rar"); // Arbitrary archive files gtk_file_filter_add_pattern(info->file_types_handled, "*.zip"); gtk_file_filter_add_pattern(info->file_types_handled, "*.rar"); gtk_file_filter_add_pattern(info->file_types_handled, "*.7z"); gtk_file_filter_add_pattern(info->file_types_handled, "*.tar"); gtk_file_filter_add_pattern(info->file_types_handled, "*.tbz"); gtk_file_filter_add_pattern(info->file_types_handled, "*.tgz"); gtk_file_filter_add_pattern(info->file_types_handled, "*.tar.bz2"); gtk_file_filter_add_pattern(info->file_types_handled, "*.tar.gz"); // Assign the handlers info->alloc_fn = file_type_archive_alloc; }/*}}}*/ pqiv-2.12/backends/archive.mime000066400000000000000000000002621376070546500164760ustar00rootroot00000000000000application/x-bzip-compressed-tar application/x-gzip-compressed-tar application/zip application/x-zip application/x-zip-compressed application/x-rar application/x-rar-compressed pqiv-2.12/backends/archive_cbx.c000066400000000000000000000201051376070546500166230ustar00rootroot00000000000000/** * pqiv * * Copyright (c) 2013-2017, 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 for comic books * * This is a stripped down variant of the more advanced archive backend * which can only handle *.cb? files, archives for comic book storage. * Such files are guaranteed to contain _only_ jpg/png files, which allows * to handle them directly using a gdkpixbuf. * */ #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 FALSE_POINTER; } struct archive *archive = file_type_archive_cbx_gen_archive(data); if(!archive) { file_free(file); return FALSE_POINTER; } BOSNode *first_node = FALSE_POINTER; 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, NULL, 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 == FALSE_POINTER) { 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 first_node; }/*}}}*/ void file_type_archive_cbx_free(file_t *file) {/*{{{*/ if(file->private) { 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; } 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->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); apply_interpolation_quality(cr); 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.12/backends/archive_cbx.mime000066400000000000000000000001201376070546500173230ustar00rootroot00000000000000application/x-cbz application/x-ext-cbz application/x-cbr application/x-ext-cbr pqiv-2.12/backends/gdkpixbuf.c000066400000000000000000000265441376070546500163460ustar00rootroot00000000000000/** * pqiv * * Copyright (c) 2013-2017, 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" #include /* 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; #if GLIB_CHECK_VERSION(2, 62, 0) /* Glib 2.62 marks GTimeVal deprecated, but GdkPixbuf does not have an equivalent API * for the replacement structure yet. */ G_GNUC_BEGIN_IGNORE_DEPRECATIONS #endif GTimeVal animation_time; #if GLIB_CHECK_VERSION(2, 62, 0) G_GNUC_END_IGNORE_DEPRECATIONS #endif } 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, &private->animation_time); } 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); // We keep track of time manually to allow the user to adjust the playback speed: // It is assumed that this function is called exactly at the right time, each time. // TODO The downside from this is that animations won't play smoothly on slow X11 connections. // Maybe I should extend the API to allow to switch between auto and manual time? int millis_until_next = gdk_pixbuf_animation_iter_get_delay_time(private->animation_iter); if(millis_until_next > 0) { private->animation_time.tv_usec += millis_until_next * 1000; if(private->animation_time.tv_usec >= 1000000) { private->animation_time.tv_sec += private->animation_time.tv_usec / 1000000; private->animation_time.tv_usec %= 1000000; } } gdk_pixbuf_animation_iter_advance(private->animation_iter, &private->animation_time); 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; } GdkPixbuf *pixbuf = g_object_ref(gdk_pixbuf_animation_get_static_image(pixbuf_animation)); g_object_unref(pixbuf_animation); if(pixbuf != NULL) { GdkPixbuf *new_pixbuf = gdk_pixbuf_apply_embedded_orientation(pixbuf); g_object_unref(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) { return; } file->width = gdk_pixbuf_get_width(pixbuf); file->height = gdk_pixbuf_get_height(pixbuf); // Cairo cannot handle files larger than 32767x32767 // See https://lists.freedesktop.org/archives/cairo/2009-August/017881.html // But actually, we might have to use a lower limit in case we are out of memory. double cairo_image_dimensions_limit = 30000.; cairo_surface_t *surface = NULL; do { if(file->width > cairo_image_dimensions_limit || file->height > cairo_image_dimensions_limit) { double loading_scale_factor = 1.; loading_scale_factor = fmin(cairo_image_dimensions_limit / file->width, cairo_image_dimensions_limit / file->height); file->width *= loading_scale_factor; file->height *= loading_scale_factor; g_printerr("Warning: Resizing file %s down to %dx%d due to Cairo's image size limit / insufficient memory.\n", file->display_name, file->width, file->height); new_pixbuf = gdk_pixbuf_scale_simple(new_pixbuf, file->width, file->height, GDK_INTERP_BILINEAR); if(!new_pixbuf) { if(cairo_image_dimensions_limit > 10000) { cairo_image_dimensions_limit -= 10000; continue; } g_object_unref(pixbuf); *error_pointer = g_error_new(g_quark_from_static_string("pqiv-pixbuf-error"), 1, "Failed to allocate memory for the resized image.\n"); return; } else { g_object_unref(pixbuf); pixbuf = new_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. surface = gdk_cairo_surface_create_from_pixbuf(pixbuf, 1., NULL); // TODO Once this works, manually check if surface failed with "out of memory". #else 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-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); if(cairo_status(sf_cr) == CAIRO_STATUS_NO_MEMORY) { // Failed due to out of memory - retry with smaller copy of the image cairo_destroy(sf_cr); cairo_surface_destroy(surface); if(cairo_image_dimensions_limit > 10000) { cairo_image_dimensions_limit -= 10000; continue; } g_object_unref(pixbuf); *error_pointer = g_error_new(g_quark_from_static_string("pqiv-pixbuf-error"), 1, "Insufficient memory to load image"); return; } cairo_destroy(sf_cr); #endif break; } while(TRUE); // Do not ever repeat, only on explicit "continue", see break just above. 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; } }/*}}}*/ 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); apply_interpolation_quality(cr); 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_list = gdk_pixbuf_get_formats(); for(GSList *file_formats_iterator = file_formats_list; file_formats_iterator; file_formats_iterator = g_slist_next(file_formats_iterator)) { 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; } }; g_slist_free(file_formats_list); // 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.12/backends/gdkpixbuf.mime000066400000000000000000000007441376070546500170450ustar00rootroot00000000000000application/x-navi-animation image/bmp image/gif image/jpeg image/jpg image/png image/qtif image/svg image/svg-xml image/svg+xml image/svg+xml-compressed image/tiff image/vnd.adobe.svg+xml image/x-bmp image/x-icns image/x-ico image/x-icon image/x-ms-bmp image/x-MS-bmp image/x-png image/x-portable-anymap image/x-portable-bitmap image/x-portable-graymap image/x-portable-pixmap image/x-quicktime image/x-tga image/x-win-bitmap image/x-wmf image/x-xbitmap image/x-xpixmap text/xml-svg pqiv-2.12/backends/libav.c000066400000000000000000000374531376070546500154610ustar00rootroot00000000000000/** * pqiv * * Copyright (c) 2013-2017, 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 "../lib/filebuffer.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_packet_unref av_free_packet #endif #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54, 0, 0) #define avcodec_free_frame av_free #define AV_PIX_FMT_RGB32 PIX_FMT_RGB32 #endif #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 41, 0) #define AV_COMPAT_CODEC_DEPRECATED #endif #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(58, 9, 100) #define AV_API_NEXT_CHANGES #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", "bin", "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 { GBytes *file_data; gsize file_data_pos; AVFormatContext *avcontext; AVIOContext *aviocontext; AVCodecContext *cocontext; int video_stream_id; gboolean pkt_valid; AVPacket pkt; AVFrame *frame; AVFrame *rgb_frame; uint8_t *buffer; guint pixel_width; guint pixel_height; AVRational sample_aspect_ratio; } file_private_data_libav_t; static int file_type_libav_memory_access_reader(void *opaque, uint8_t *buf, int buf_size) {/*{{{*/ file_private_data_libav_t *private = opaque; gsize data_size = 0; gconstpointer data = g_bytes_get_data(private->file_data, &data_size); if(buf_size < 0) { return -1; } if((unsigned)buf_size > data_size - private->file_data_pos) { buf_size = data_size - private->file_data_pos; } if(private->file_data_pos < data_size) { memcpy(buf, (const char*)data + private->file_data_pos, buf_size); private->file_data_pos += buf_size; } return buf_size; }/*}}}*/ static int64_t file_type_libav_memory_access_seeker(void *opaque, int64_t offset, int whence) {/*{{{*/ file_private_data_libav_t *private = opaque; whence &= (SEEK_CUR | SEEK_SET | SEEK_END); gsize data_size = 0; g_bytes_get_data(private->file_data, &data_size); switch(whence) { case SEEK_CUR: if(0 <= (ssize_t)private->file_data_pos + offset && private->file_data_pos + offset < data_size) { private->file_data_pos += offset; } return 0; break; case SEEK_SET: if(offset >= 0 && offset < (ssize_t)data_size) { private->file_data_pos = offset; } return 0; break; case SEEK_END: if(offset <= 0) { private->file_data_pos = data_size + offset; } break; } return -1; }/*}}}*/ 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->file_data) { g_bytes_unref(private->file_data); buffered_file_unref(file); private->file_data = NULL; private->file_data_pos = 0; } 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); #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55,53,0) avcodec_free_context(&(private->cocontext)); #else av_freep(&(private->cocontext)); #endif avformat_close_input(&(private->avcontext)); } if(private->aviocontext) { av_freep(&private->aviocontext->buffer); av_freep(&private->aviocontext); private->aviocontext = NULL; } 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(file->file_flags & FILE_FLAGS_MEMORY_IMAGE) { if(!private->file_data) { private->file_data = buffered_file_as_bytes(file, data, error_pointer); } private->file_data_pos = 0; private->avcontext = avformat_alloc_context(); private->aviocontext = avio_alloc_context(av_malloc(4096), 4096, 0, private, &file_type_libav_memory_access_reader, NULL, &file_type_libav_memory_access_seeker); private->avcontext->pb = private->aviocontext; if(avformat_open_input(&(private->avcontext), NULL, NULL, NULL) < 0) { *error_pointer = g_error_new(g_quark_from_static_string("pqiv-libav-error"), 1, "Failed to load image using libav."); return; } } else { 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( #ifndef AV_COMPAT_CODEC_DEPRECATED private->avcontext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO #else private->avcontext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO #endif ) { private->video_stream_id = i; break; } } if(private->video_stream_id < 0 || ( #ifndef AV_COMPAT_CODEC_DEPRECATED private->avcontext->streams[private->video_stream_id]->codec->width == 0 #else private->avcontext->streams[private->video_stream_id]->codecpar->width == 0 #endif )) { *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; } #ifndef AV_COMPAT_CODEC_DEPRECATED AVCodec *codec = avcodec_find_decoder(private->avcontext->streams[private->video_stream_id]->codec->codec_id); #else AVCodec *codec = avcodec_find_decoder(private->avcontext->streams[private->video_stream_id]->codecpar->codec_id); #endif private->cocontext = avcodec_alloc_context3(codec); #ifndef AV_COMPAT_CODEC_DEPRECATED avcodec_copy_context(private->cocontext, private->avcontext->streams[private->video_stream_id]->codec); #else avcodec_parameters_to_context(private->cocontext, private->avcontext->streams[private->video_stream_id]->codecpar); #endif 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(); file->file_flags |= FILE_FLAGS_ANIMATION; #ifdef AV_COMPAT_CODEC_DEPRECATED private->pixel_width = private->avcontext->streams[private->video_stream_id]->codecpar->width; private->pixel_height = private->avcontext->streams[private->video_stream_id]->codecpar->height; private->sample_aspect_ratio = private->avcontext->streams[private->video_stream_id]->codecpar->sample_aspect_ratio; #else private->pixel_width = private->avcontext->streams[private->video_stream_id]->codec->width; private->pixel_height = private->avcontext->streams[private->video_stream_id]->codec->height; private->sample_aspect_ratio = private->avcontext->streams[private->video_stream_id]->codec->sample_aspect_ratio; #endif if(private->sample_aspect_ratio.num == 0 || private->sample_aspect_ratio.den == 0) { private->sample_aspect_ratio.num = private->sample_aspect_ratio.den = 1; file->width = private->pixel_width; file->height = private->pixel_height; } else if(private->sample_aspect_ratio.num > private->sample_aspect_ratio.den) { file->width = private->sample_aspect_ratio.num * private->pixel_width / private->sample_aspect_ratio.den; file->height = private->pixel_height; } else { file->width = private->pixel_width; file->height = private->sample_aspect_ratio.den * private->pixel_height / private->sample_aspect_ratio.num; } #if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(55, 0, 0) size_t num_bytes = avpicture_get_size(PIX_FMT_RGB32, file->width, file->height); #else size_t num_bytes = av_image_get_buffer_size(AV_PIX_FMT_RGB32, file->width, file->height, 16); #endif private->buffer = (uint8_t *)g_malloc(num_bytes * sizeof(uint8_t)); if(file->width == 0 || file->height == 0) { file_type_libav_unload(file); file->is_loaded = FALSE; return; } 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; } AVFrame *frame = private->frame; #ifndef AV_COMPAT_CODEC_DEPRECATED int got_picture_ptr = 0; avcodec_decode_video2(private->cocontext, frame, &got_picture_ptr, &(private->pkt)); #else if(avcodec_send_packet(private->cocontext, &(private->pkt)) >= 0) { avcodec_receive_frame(private->cocontext, frame); } #endif 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; #ifdef AV_COMPAT_CODEC_DEPRECATED const int pix_fmt = private->avcontext->streams[private->video_stream_id]->codecpar->format; #else const int pix_fmt = private->avcontext->streams[private->video_stream_id]->codec->pix_fmt; #endif // Prepare buffer for RGB32 version uint8_t *buffer = private->buffer; #if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(55, 0, 0) avpicture_fill((AVPicture *)rgb_frame, buffer, PIX_FMT_RGB32, file->width, file->height); #else av_image_fill_arrays(rgb_frame->data, rgb_frame->linesize, buffer, AV_PIX_FMT_RGB32, file->width, file->height, 16); #endif // If a valid frame is available.. if(frame->data[0]) { // ..convert to RGB32 struct SwsContext *img_convert_ctx = sws_getCachedContext(NULL, private->pixel_width, private->pixel_height, pix_fmt, file->width, file->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->pixel_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); apply_interpolation_quality(cr); 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) {/*{{{*/ #ifndef AV_API_NEXT_CHANGES avcodec_register_all(); av_register_all(); #else void *opaque_iter = NULL; #endif avformat_network_init(); // Register all file formats supported by libavformat info->file_types_handled = gtk_file_filter_new(); #ifndef AV_API_NEXT_CHANGES for(AVInputFormat *iter = av_iformat_next(NULL); iter; iter = av_iformat_next(iter)) #else for(const AVInputFormat *iter; (iter = av_demuxer_iterate(&opaque_iter)); /*nothing */) #endif { 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); } } // Register as general handler for video/* MIME types gtk_file_filter_add_mime_type(info->file_types_handled, "video/*"); // 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.12/backends/libav.mime000066400000000000000000000012351376070546500161530ustar00rootroot00000000000000application/x-flash-video application/x-troff-msvideo video/3gp video/3gpp video/avi video/divx video/dv video/fli video/flv video/mp2t video/mp4 video/mp4v-es video/mpeg video/msvideo video/ogg video/quicktime video/vivo video/vnd.divx video/vnd.mpegurl video/vnd.rn-realvideo video/vnd.vivo video/webm video/x-anim video/x-avi video/x-flc video/x-fli video/x-flic video/x-flv video/x-m4v video/x-matroska video/x-mpeg video/x-mpeg2 video/x-mpg video/x-ms-afs video/x-ms-asf video/x-ms-asx video/x-msvideo video/x-ms-wm video/x-ms-wmv video/x-ms-wmx video/x-ms-wvx video/x-ms-wvxvideo video/x-nsv video/x-ogm+ogg video/x-theora video/x-theora+ogg video/x-totem-stream pqiv-2.12/backends/poppler.c000066400000000000000000000137011376070546500160330ustar00rootroot00000000000000/** * pqiv * * Copyright (c) 2013-2017, 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 0 && POPPLER_CHECK_VERSION(0, 26, 5) // Poppler has a bug in its stream loader. The original problem #82630 was fixed in // http://cgit.freedesktop.org/poppler/poppler/commit/?h=poppler-0.26&id=f94ba85a736b4c90c05e7782939f32506472658e // and the fix appeared in 0.26.5. But there is another bug, see #96884. // 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 || error_pointer) { g_printerr("Failed to load PDF %s: %s\n", file->display_name, error_pointer->message); g_clear_error(&error_pointer); file_free(file); return FALSE_POINTER; } 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); first_node = FALSE_POINTER; } #if 0 && POPPLER_CHECK_VERSION(0, 26, 5) g_object_unref(data); #else buffered_file_unref(file); #endif if(first_node) { 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) {/*{{{*/ if(error_pointer) { *error_pointer = NULL; } 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 0 && 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 || (error_pointer && *error_pointer)) { 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); apply_interpolation_quality(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.12/backends/poppler.mime000066400000000000000000000000541376070546500165350ustar00rootroot00000000000000image/pdf application/pdf application/x-pdf pqiv-2.12/backends/shared-initializer.c000066400000000000000000000051721376070546500201440ustar00rootroot00000000000000#include #include #include #include #include "../pqiv.h" #ifndef SHARED_BACKENDS #warning The SHARED_BACKENDS constant is undefined. Defaulting to only gdkpixbuf support. #define SHARED_BACKENDS "gdkpixbuf", #endif #ifndef SEARCH_PATHS #warning The SEARCH_PATHS constant is undefined. Defaulting to only backends subdirectory. #define SEARCH_PATHS "backends", #endif static const char *available_backends[] = { SHARED_BACKENDS NULL }; static const char *search_paths[] = { SEARCH_PATHS NULL }; file_type_handler_t file_type_handlers[sizeof(available_backends) / sizeof(char *)]; extern char **global_argv; static char *self_path = NULL; static gchar *get_backend_path(const gchar *backend_name) { // We search for the libraries ourselves because with --enable-new-dtags // (default at least on Gentoo), ld writes the search path to RUNPATH // instead of RPATH, which dlopen() ignores. // #ifdef __linux__ if(self_path == NULL) { gchar self_name[PATH_MAX]; ssize_t name_length = readlink("/proc/self/exe", self_name, PATH_MAX); if(name_length >= 0) { self_name[name_length] = 0; self_path = g_strdup(dirname(self_name)); } } #endif if(self_path == NULL) { self_path = g_strdup(dirname(global_argv[0])); } for(char **search_path=(char **)&search_paths[0]; *search_path; search_path++) { gchar *backend_candidate = g_strdup_printf("%s/%s/pqiv-backend-%s.so", (*search_path)[0] == '/' ? "" : self_path, *search_path, backend_name); if(g_file_test(backend_candidate, G_FILE_TEST_IS_REGULAR)) { return backend_candidate; } g_free(backend_candidate); } // As a fallback, always use the system's library lookup mechanism return g_strdup_printf("pqiv-backend-%s.so", backend_name); } void initialize_file_type_handlers(const gchar * const * disabled_backends) { int i = 0; for(char **backend=(char **)&available_backends[0]; *backend; backend++) { if(strv_contains(disabled_backends, *backend)) { continue; } gchar *backend_candidate = get_backend_path(*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); } if(i == 0) { g_printerr("Failed to load any of the image backends.\n"); exit(1); } } pqiv-2.12/backends/spectre.c000066400000000000000000000170741376070546500160260ustar00rootroot00000000000000/** * pqiv * * Copyright (c) 2013-2017, 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; #if defined(__GNUC__) __attribute__((used)) #endif 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. // // Newer versions of ghostscript also try to set a mutex handler, which has type // mtzH (which is a typo, mtxH was intended) // const uint32_t type = *((uint32_t*)plugin + 2); if(type != 0x6D656D48 && type != 0x6D747A48) { #ifdef DEBUG g_printerr("Warning: cmsPluginTHR call was redirected because of a poppler/gs interaction bug, but was called in an unexpected manner.\n"); #endif } } BOSNode *file_type_spectre_alloc(load_images_state_t state, file_t *file) {/*{{{*/ BOSNode *first_node = FALSE_POINTER; 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 FALSE_POINTER; } 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 FALSE_POINTER; } 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); } } if(first_node) { 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); apply_interpolation_quality(cr); 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"); gtk_file_filter_add_mime_type(info->file_types_handled, "image/ps"); gtk_file_filter_add_mime_type(info->file_types_handled, "image/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.12/backends/spectre.mime000066400000000000000000000001011376070546500165120ustar00rootroot00000000000000application/postscript image/eps image/ps image/x-eps image/x-ps pqiv-2.12/backends/wand.c000066400000000000000000000304501376070546500153030ustar00rootroot00000000000000/** * pqiv * * Copyright (c) 2013-2017, 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 #if __clang__ // ImageMagick does throw some clang warnings #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wunused-variable" #pragma clang diagnostic ignored "-Wunknown-attributes" #pragma clang diagnostic ignored "-Wkeyword-macro" #endif #if defined(WAND_VERSION) && WAND_VERSION > 6 #include #else #include #endif #if __clang__ #pragma clang diagnostic pop #endif #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); file_free(file); return FALSE_POINTER; } 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); file_free(file); return FALSE_POINTER; } int n_pages = MagickGetNumberImages(wand); DestroyMagickWand(wand); buffered_file_unref(file); BOSNode *first_node = FALSE_POINTER; 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; // Temporarily give up lock to do this: Otherwise we might see a deadlock // if another thread holding the file tree's lock is waiting for the wand // lock for another operation. G_UNLOCK(magick_wand_global_lock); if(n == 0) { first_node = load_images_handle_parameter_add_file(state, new_file); } else { load_images_handle_parameter_add_file(state, new_file); } G_LOCK(magick_wand_global_lock); } if(first_node) { 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); apply_interpolation_quality(cr); 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.12/backends/wand.mime000066400000000000000000000017661376070546500160200ustar00rootroot00000000000000image/avi image/avs image/bie image/bmp image/cmyk image/dcx image/eps image/fax image/fits image/g3fax image/gif image/gray image/jp2 image/jpeg image/jpeg2000 image/jpg image/jpx image/miff image/mono image/mtv image/pcd image/pcx image/pdf image/pict image/pjpeg image/png image/ps image/rad image/rgba image/rla image/rle image/sgi image/sun-raster image/svg+xml image/svg+xml-compressed image/targa image/tiff image/uyvy image/vid image/viff image/vnd.rn-realpix image/vnd.wap.wbmp image/x-bmp image/x-bzeps image/x-compressed-xcf image/x-eps image/x-fits image/x-freehand image/x-gimp-gbr image/x-gimp-gih image/x-gimp-pat image/x-gray image/x-gzeps image/x-icb image/x-ico image/x-icon image/x-ms-bmp image/x-pcx image/x-pict image/x-png image/x-portable-anymap image/x-portable-bitmap image/x-portable-graymap image/x-portable-pixmap image/x-psd image/x-psp image/x-rgb image/x-sgi image/x-tga image/x-wmf image/x-xbitmap image/x-xcf image/x-xcursor image/x-xpixmap image/x-xwindowdump image/xpm image/yuv pqiv-2.12/backends/webp.c000066400000000000000000000134611376070546500153120ustar00rootroot00000000000000/** * pqiv * * Copyright (c) 2013-2017, Phillip Berndt * Copyright (c) 2017, Chen John L * * 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 . * * * webp backend * */ #include "../pqiv.h" #include "../lib/filebuffer.h" #include #include #include #include #include #include #include typedef struct { cairo_surface_t *rendered_image_surface; } file_private_data_webp_t; BOSNode *file_type_webp_alloc(load_images_state_t state, file_t *file) {/*{{{*/ file->private = g_slice_new0(file_private_data_webp_t); BOSNode *first_node = load_images_handle_parameter_add_file(state, file); return first_node; }/*}}}*/ void file_type_webp_free(file_t *file) {/*{{{*/ g_slice_free(file_private_data_webp_t, file->private); }/*}}}*/ void file_type_webp_load(file_t *file, GInputStream *data, GError **error_pointer) {/*{{{*/ file_private_data_webp_t *private = file->private; // Reset the rendered_image_surface back to NULL if(private->rendered_image_surface) { cairo_surface_destroy(private->rendered_image_surface); private->rendered_image_surface = NULL; } union { uint32_t u32; uint8_t u8arr[4]; } endian_tester; endian_tester.u32 = 0x12345678; int image_width, image_height; gsize image_size; GBytes *image_bytes = buffered_file_as_bytes(file, data, error_pointer); if(!image_bytes) { return; } const gchar *image_data = g_bytes_get_data(image_bytes, &image_size); WebPBitstreamFeatures image_features; VP8StatusCode webp_retstatus = WebPGetFeatures((const uint8_t*)image_data, image_size, &image_features); int image_decode_ok = 0; uint8_t* webp_retptr = NULL; uint8_t* surface_data = NULL; int surface_stride = 0; if(webp_retstatus == VP8_STATUS_OK) { image_width = image_features.width; image_height = image_features.height; // Create the surface private->rendered_image_surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, image_width, image_height); surface_data = cairo_image_surface_get_data(private->rendered_image_surface); surface_stride = cairo_image_surface_get_stride(private->rendered_image_surface); cairo_surface_flush(private->rendered_image_surface); if(endian_tester.u8arr[0] == 0x12) { // We are in big endian webp_retptr = WebPDecodeARGBInto((const uint8_t*)image_data, image_size, surface_data, surface_stride*image_height*4, surface_stride); } else { // We are in little endian webp_retptr = WebPDecodeBGRAInto((const uint8_t*)image_data, image_size, surface_data, surface_stride*image_height*4, surface_stride); } cairo_surface_mark_dirty(private->rendered_image_surface); if(webp_retptr != NULL) { image_decode_ok = 1; } } buffered_file_unref(file); image_data = NULL; image_size = 0; if(!image_decode_ok) { // Clear the rendered_image_surface if an error occurred if(private->rendered_image_surface) { cairo_surface_destroy(private->rendered_image_surface); private->rendered_image_surface = NULL; } *error_pointer = g_error_new(g_quark_from_static_string("pqiv-webp-error"), 1, "Failed to load image %s, malformed webp file", file->file_name); return; } /* Note that cairo's ARGB32 format requires precomputed alpha, but * the output from webp is not precomputed. Therefore, we do the * alpha precomputation below if the file has an alpha channel. */ int i, j; float fR, fG, fB, fA; int R, G, B; uint32_t pixel; if(image_features.has_alpha) { for(i = 0; i < image_height; i++) { for(j = 0; j < image_width; j++) { memcpy(&pixel, &surface_data[i*surface_stride+j*4], sizeof(uint32_t)); // Unpack into float fR = (pixel&0x0FF)/255.0; fG = ((pixel>>8)&0x0FF)/255.0; fB = ((pixel>>16)&0x0FF)/255.0; fA = ((pixel>>24)&0x0FF)/255.0; // Casting float to int truncates, so for rounding, we add 0.5 R = (fR*fA*255.0+0.5); G = (fG*fA*255.0+0.5); B = (fB*fA*255.0+0.5); pixel = R | (G<<8) | (B<<16) | (pixel&0xFF000000); memcpy(&surface_data[i*surface_stride+j*4], &pixel, sizeof(uint32_t)); } } } file->width = image_width; file->height = image_height; file->is_loaded = TRUE; }/*}}}*/ void file_type_webp_unload(file_t *file) {/*{{{*/ file_private_data_webp_t *private = file->private; if(private->rendered_image_surface) { cairo_surface_destroy(private->rendered_image_surface); private->rendered_image_surface = NULL; } }/*}}}*/ void file_type_webp_draw(file_t *file, cairo_t *cr) {/*{{{*/ file_private_data_webp_t *private = file->private; if(private->rendered_image_surface) { cairo_set_source_surface(cr, private->rendered_image_surface, 0, 0); apply_interpolation_quality(cr); cairo_paint(cr); } }/*}}}*/ void file_type_webp_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, "*.webp"); gtk_file_filter_add_mime_type(info->file_types_handled, "image/webp"); // Assign the handlers info->alloc_fn = file_type_webp_alloc; info->free_fn = file_type_webp_free; info->load_fn = file_type_webp_load; info->unload_fn = file_type_webp_unload; info->draw_fn = file_type_webp_draw; }/*}}}*/ pqiv-2.12/backends/webp.mime000066400000000000000000000000131376070546500160040ustar00rootroot00000000000000image/webp pqiv-2.12/configure000077500000000000000000000300551376070546500143440ustar00rootroot00000000000000#!/usr/bin/env bash # # Configure script for pqiv. Running this script is optional if you only want # gdk-pixbuf support, but still recommended. # tempdir() { NAME=tmp_${RANDOM} while ! mkdir "${NAME}" 2>/dev/null; do NAME=tmp_${RANDOM} done echo ${NAME} } PREFIX=/usr DESTDIR= GTK_VERSION=0 CROSS= BINARY_EXTENSION= BINDIR= LIBDIR= MANDIR= BACKENDS= ENFORCED_BACKENDS= DISABLED_BACKENDS= EXTRA_DEFS= BACKENDS_BUILD=static DEBUG_DEFS= # Check if configure was called from another directory; # support for out-of-source-tree builds STARTUP_DIR="$(pwd)" SOURCE_DIR="$( (cd "$(dirname "$0")"; pwd -P) )" cd "$SOURCE_DIR" # 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 awk 'BEGIN { FS="ifndef|ifdef|option|:|/\\*|\\*/" } /ifn?def .+ option / { print $2 " " $4 " " $5 }' 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 ;; --libdir) LIBDIR=${VALUE} LIBDIR="${LIBDIR//\{/(}" LIBDIR="${LIBDIR//\}/)}" LIBDIR="${LIBDIR//prefix/PREFIX}" ;; --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 ;; --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 ;; # Undocumented options for autoconf (esp. dh_auto_configure) # compatibility Note to maintainers: These options are here to make it # simpler to package pqiv, because they allow to run autotools wrappers # against this package. I will maintain them, but I'd recommend # against using them if you can avoid it. --host) CROSS=${VALUE}- ;; --bindir) BINDIR=${VALUE} BINDIR="${BINDIR//\{/(}" BINDIR="${BINDIR//\}/)}" BINDIR="${BINDIR//prefix/PREFIX}" echo "Use of autoconf option --bindir is discouraged, because support is incomplete. Rewrote \`$VALUE' to \`$BINDIR' and used that as the BINDIR Make variable." >&2 ;; --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 ;; --disable-silent-rules) VERBOSE=1 ;; --infodir | --sysconfdir | --includedir | --localstatedir | --libexecdir | --disable-maintainer-mode | --disable-dependency-tracking | --build | --sbindir | --includedir | --oldincludedir | --localedir | --docdir) 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 ;; *) # Check for disableable feature flag DEF=$( awk 'BEGIN { FS="ifndef|ifdef|option|:|/\\*|\\*/" } /ifn?def .+ option / { print $2 " " $4 " " $5 }' 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 # Check for dynamic backend en-/dis-abling flag if [ "${PARAMETER#--without-}" != "${PARAMETER}" ]; then NAME="${PARAMETER#--without-}" if ! [ -e backends/${NAME}.c ]; then echo "Unknown option: $1" >&2 exit 1 fi 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}" -a -z "${PKG_CONFIG}" ]; 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="$PKG_CONFIG"} | awk '/^BACKENDS:/ {print substr($0, 11);}')" 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 ${PKG_CONFIG:+PKG_CONFIG="$PKG_CONFIG"} ${BACKENDS:+BACKENDS="$BACKENDS"} | awk '/^LIBS:/ {print substr($0, 7);}'`" LIBS_GTK2="`$MAKE get_libs GTK_VERSION=2 EXECUTABLE_EXTENSION=$EXECUTABLE_EXTENSION ${PKG_CONFIG:+PKG_CONFIG="$PKG_CONFIG"} ${BACKENDS:+BACKENDS="$BACKENDS"} | awk '/^LIBS:/ {print substr($0, 7);}'`" 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:-pkg-config} --exists "$LIBS_GTK3"; then LIBS="${LIBS_GTK3}" echo "ok" else if ${PKG_CONFIG:-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 if [ "$SOURCE_DIR" != "$STARTUP_DIR" ]; then if ! [ -e $STARTUP_DIR/GNUmakefile ]; then echo "Writing GNUmakefile." echo "include $SOURCE_DIR/GNUmakefile" > $STARTUP_DIR/GNUmakefile else echo "Not touching existing GNUmakefile." fi fi echo "Writing config.make." cat > $STARTUP_DIR/config.make </dev/null)" ]; then echo -n "Checking if affected by liblcms bug.. " DIR=`tempdir` cd $DIR echo -e "#include \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:-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.12/lib/000077500000000000000000000000001376070546500132005ustar00rootroot00000000000000pqiv-2.12/lib/bostree.c000066400000000000000000000415311376070546500150130ustar00rootroot00000000000000/* 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). The tree is implemented with map semantics, that is, there are separete key and value pointers. Copyright (c) 2017, 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 #include /* Tree structure */ struct _BOSTree { BOSNode *root_node; BOSTree_cmp_function cmp_function; BOSTree_free_function free_function; }; /* Local helper functions */ static int _imax(const int i1, const int i2) { return i1 > i2 ? i1 : i2; } static int _bostree_balance(BOSNode *node) { const int left_depth = node->left_child_node ? node->left_child_node->depth + 1 : 0; const int right_depth = node->right_child_node ? node->right_child_node->depth + 1 : 0; return right_depth - left_depth; } static BOSNode *_bostree_rotate_right(BOSTree *tree, BOSNode *P) { // Rotate right: // // P L // L R --> c1 P //c1 c2 c2 R // BOSNode *L = P->left_child_node; if(P->parent_node) { if(P->parent_node->left_child_node == P) { P->parent_node->left_child_node = L; } else { P->parent_node->right_child_node = L; } } else { tree->root_node = L; } L->parent_node = P->parent_node; P->left_child_node = L->right_child_node; P->left_child_count = L->right_child_count; if(P->left_child_node) { P->left_child_node->parent_node = P; } P->depth = _imax(P->left_child_node ? 1 + P->left_child_node->depth : 0, P->right_child_node ? 1 + P->right_child_node->depth : 0); P->parent_node = L; L->right_child_node = P; P->parent_node = L; L->right_child_count = P->left_child_count + P->right_child_count + 1; L->depth = _imax(L->left_child_node ? 1 + L->left_child_node->depth : 0, L->right_child_node ? 1 + L->right_child_node->depth : 0); return L; } static BOSNode *_bostree_rotate_left(BOSTree *tree, BOSNode *P) { // Rotate left: // // P R // L R --> P c2 // c1 c2 L c1 // BOSNode *R = P->right_child_node; if(P->parent_node) { if(P->parent_node->left_child_node == P) { P->parent_node->left_child_node = R; } else { P->parent_node->right_child_node = R; } } else { tree->root_node = R; } R->parent_node = P->parent_node; P->right_child_node = R->left_child_node; P->right_child_count = R->left_child_count; if(P->right_child_node) { P->right_child_node->parent_node = P; } P->depth = _imax(P->left_child_node ? 1 + P->left_child_node->depth : 0, P->right_child_node ? 1 + P->right_child_node->depth : 0); P->parent_node = R; R->left_child_node = P; P->parent_node = R; R->left_child_count = P->left_child_count + P->right_child_count + 1; R->depth = _imax(R->left_child_node ? 1 + R->left_child_node->depth : 0, R->right_child_node ? 1 + R->right_child_node->depth : 0); return R; } /* API implementation */ BOSTree *bostree_new(BOSTree_cmp_function cmp_function, BOSTree_free_function free_function) { BOSTree *new_tree = malloc(sizeof(BOSTree)); new_tree->root_node = NULL; new_tree->cmp_function = cmp_function; new_tree->free_function = free_function; return new_tree; } void bostree_destroy(BOSTree *tree) { // Walk the tree and unref all nodes BOSNode *node = tree->root_node; while(node) { // The order should not really matter, but use post-order traversal here. while(node->left_child_node) { node = node->left_child_node; } if(node->right_child_node) { node = node->right_child_node; continue; } // At this point, we can be sure that this node has no child nodes. // So it is safe to remove it. BOSNode *next = node->parent_node; if(next) { if(next->left_child_node == node) { next->left_child_node = NULL; } else { next->right_child_node = NULL; } } bostree_node_weak_unref(tree, node); node = next; } free(tree); } unsigned int bostree_node_count(BOSTree *tree) { return tree->root_node ? tree->root_node->left_child_count + tree->root_node->right_child_count + 1 : 0; } BOSNode *bostree_insert(BOSTree *tree, void *key, void *data) { BOSNode **node = &tree->root_node; BOSNode *parent_node = NULL; // Find tree position to insert new node while(*node) { parent_node = *node; int cmp = tree->cmp_function(key, (*node)->key); if(cmp < 0) { (*node)->left_child_count++; node = &(*node)->left_child_node; } else { (*node)->right_child_count++; node = &(*node)->right_child_node; } } // Create new node BOSNode *new_node = malloc(sizeof(BOSNode)); memset(new_node, 0, sizeof(BOSNode)); new_node->key = key; new_node->data = data; new_node->weak_ref_count = 1; new_node->weak_ref_node_valid = 1; new_node->parent_node = parent_node; *node = new_node; if(!parent_node) { // Simple case, this is the first node. tree->root_node = new_node; return new_node; } // Check if the depth changed with the new node: // It does only change if this is the first child of the parent if(!!parent_node->left_child_node ^ !!parent_node->right_child_node) { // Bubble the information up the tree parent_node->depth++; while(parent_node->parent_node) { // Assign new depth parent_node = parent_node->parent_node; unsigned int new_left_depth = parent_node->left_child_node ? parent_node->left_child_node->depth + 1 : 0; unsigned int new_right_depth = parent_node->right_child_node ? parent_node->right_child_node->depth + 1 : 0; unsigned int max_depth = _imax(new_left_depth, new_right_depth); if(parent_node->depth != max_depth) { parent_node->depth = max_depth; } else { // We can break here, because if the depth did not change // at this level, it won't have further up either. break; } // Check if this node violates the AVL property, that is, that the // depths differ by no more than 1. if(new_left_depth - 2 == new_right_depth) { // Handle left-right case if(_bostree_balance(parent_node->left_child_node) > 0) { _bostree_rotate_left(tree, parent_node->left_child_node); } // Left is two levels deeper than right. Rotate right. parent_node = _bostree_rotate_right(tree, parent_node); } else if(new_left_depth + 2 == new_right_depth) { // Handle right-left case if(_bostree_balance(parent_node->right_child_node) < 0) { _bostree_rotate_right(tree, parent_node->right_child_node); } // Right is two levels deeper than left. Rotate left. parent_node = _bostree_rotate_left(tree, parent_node); } } } return new_node; } void bostree_remove(BOSTree *tree, BOSNode *node) { BOSNode *bubble_up = NULL; // If this node has children on both sides, bubble one of it upwards // and rotate within the subtrees. if(node->left_child_node && node->right_child_node) { BOSNode *candidate = NULL; BOSNode *lost_child = NULL; if(node->left_child_node->depth >= node->right_child_node->depth) { // Left branch is deeper than right branch, might be a good idea to // bubble from this side to maintain the AVL property with increased // likelihood. node->left_child_count--; candidate = node->left_child_node; while(candidate->right_child_node) { candidate->right_child_count--; candidate = candidate->right_child_node; } lost_child = candidate->left_child_node; } else { node->right_child_count--; candidate = node->right_child_node; while(candidate->left_child_node) { candidate->left_child_count--; candidate = candidate->left_child_node; } lost_child = candidate->right_child_node; } BOSNode *bubble_start = candidate->parent_node; if(bubble_start->left_child_node == candidate) { bubble_start->left_child_node = lost_child; } else { bubble_start->right_child_node = lost_child; } if(lost_child) { lost_child->parent_node = bubble_start; } // We will later rebalance upwards from bubble_start up to candidate. // But first, anchor candidate into the place where "node" used to be. if(node->parent_node) { if(node->parent_node->left_child_node == node) { node->parent_node->left_child_node = candidate; } else { node->parent_node->right_child_node = candidate; } } else { tree->root_node = candidate; } candidate->parent_node = node->parent_node; candidate->left_child_node = node->left_child_node; candidate->left_child_count = node->left_child_count; candidate->right_child_node = node->right_child_node; candidate->right_child_count = node->right_child_count; if(candidate->left_child_node) { candidate->left_child_node->parent_node = candidate; } if(candidate->right_child_node) { candidate->right_child_node->parent_node = candidate; } // From here on, node is out of the game. // Rebalance up to candidate. if(bubble_start != node) { while(bubble_start != candidate) { bubble_start->depth = _imax((bubble_start->left_child_node ? bubble_start->left_child_node->depth + 1 : 0), (bubble_start->right_child_node ? bubble_start->right_child_node->depth + 1 : 0)); int balance = _bostree_balance(bubble_start); if(balance > 1) { // Rotate left. Check for right-left case before. if(_bostree_balance(bubble_start->right_child_node) < 0) { _bostree_rotate_right(tree, bubble_start->right_child_node); } bubble_start = _bostree_rotate_left(tree, bubble_start); } else if(balance < -1) { // Rotate right. Check for left-right case before. if(_bostree_balance(bubble_start->left_child_node) > 0) { _bostree_rotate_left(tree, bubble_start->left_child_node); } bubble_start = _bostree_rotate_right(tree, bubble_start); } bubble_start = bubble_start->parent_node; } } // Fixup candidate's depth candidate->depth = _imax((candidate->left_child_node ? candidate->left_child_node->depth + 1 : 0), (candidate->right_child_node ? candidate->right_child_node->depth + 1 : 0)); // We'll have to fixup child counts and depths up to the root, do that // later. bubble_up = candidate->parent_node; // Fix immediate parent node child count here. if(bubble_up) { if(bubble_up->left_child_node == candidate) { bubble_up->left_child_count--; } else { bubble_up->right_child_count--; } } } else { // If this node has children on one side only, removing it is much simpler. if(!node->parent_node) { // Simple case: Node _was_ the old root. if(node->left_child_node) { tree->root_node = node->left_child_node; if(node->left_child_node) { node->left_child_node->parent_node = NULL; } } else { tree->root_node = node->right_child_node; if(node->right_child_node) { node->right_child_node->parent_node = NULL; } } // No rebalancing to do bubble_up = NULL; } else { BOSNode *candidate = node->left_child_node; int candidate_count = node->left_child_count; if(node->right_child_node) { candidate = node->right_child_node; candidate_count = node->right_child_count; } if(node->parent_node->left_child_node == node) { node->parent_node->left_child_node = candidate; node->parent_node->left_child_count = candidate_count; } else { node->parent_node->right_child_node = candidate; node->parent_node->right_child_count = candidate_count; } if(candidate) { candidate->parent_node = node->parent_node; } // Again, from here on, the original node is out of the game. // Rebalance up to the root. bubble_up = node->parent_node; } } // At this point, everything below and including bubble_start is // balanced, and we have to look further up. char bubbling_finished = 0; while(bubble_up) { if(!bubbling_finished) { // Calculate updated depth for bubble_up unsigned int left_depth = bubble_up->left_child_node ? bubble_up->left_child_node->depth + 1 : 0; unsigned int right_depth = bubble_up->right_child_node ? bubble_up->right_child_node->depth + 1 : 0; unsigned int new_depth = _imax(left_depth, right_depth); char depth_changed = (new_depth != bubble_up->depth); bubble_up->depth = new_depth; // Rebalance bubble_up // Not necessary for the first node, but calling _bostree_balance once // isn't that much overhead. int balance = _bostree_balance(bubble_up); if(balance < -1) { if(_bostree_balance(bubble_up->left_child_node) > 0) { _bostree_rotate_left(tree, bubble_up->left_child_node); } bubble_up = _bostree_rotate_right(tree, bubble_up); } else if(balance > 1) { if(_bostree_balance(bubble_up->right_child_node) < 0) { _bostree_rotate_right(tree, bubble_up->right_child_node); } bubble_up = _bostree_rotate_left(tree, bubble_up); } else { if(!depth_changed) { // If we neither had to rotate nor to change the depth, // then we are obviously finished. Only update child // counts from here on. bubbling_finished = 1; } } } if(bubble_up->parent_node) { if(bubble_up->parent_node->left_child_node == bubble_up) { bubble_up->parent_node->left_child_count--; } else { bubble_up->parent_node->right_child_count--; } } bubble_up = bubble_up->parent_node; } node->weak_ref_node_valid = 0; bostree_node_weak_unref(tree, node); } BOSNode *bostree_node_weak_ref(BOSNode *node) { assert(node->weak_ref_count < 127); assert(node->weak_ref_count > 0); node->weak_ref_count++; return node; } BOSNode *bostree_node_weak_unref(BOSTree *tree, BOSNode *node) { node->weak_ref_count--; if(node->weak_ref_count == 0) { if(tree->free_function) { tree->free_function(node); } free(node); } else if(node->weak_ref_node_valid) { return node; } return NULL; } BOSNode *bostree_lookup(BOSTree *tree, const void *key) { BOSNode *node = tree->root_node; while(node) { int cmp = tree->cmp_function(key, node->key); if(cmp == 0) { break; } else if(cmp < 0) { node = node->left_child_node; } else { node = node->right_child_node; } } return node; } BOSNode *bostree_select(BOSTree *tree, unsigned int index) { BOSNode *node = tree->root_node; while(node) { 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 node; } BOSNode *bostree_next_node(BOSNode *node) { if(node->right_child_node) { node = node->right_child_node; while(node->left_child_node) { node = node->left_child_node; } return node; } else if(node->parent_node) { while(node->parent_node && node->parent_node->right_child_node == node) { node = node->parent_node; } return node->parent_node; } return NULL; } BOSNode *bostree_previous_node(BOSNode *node) { if(node->left_child_node) { node = node->left_child_node; while(node->right_child_node) { node = node->right_child_node; } return node; } else if(node->parent_node) { while(node->parent_node && node->parent_node->left_child_node == node) { node = node->parent_node; } return node->parent_node; } return NULL; } unsigned int bostree_rank(BOSNode *node) { unsigned int counter = node->left_child_count; while(node) { if(node->parent_node && node->parent_node->right_child_node == node) counter += 1 + node->parent_node->left_child_count; node = node->parent_node; } return counter; } #if !defined(NDEBUG) #include #include /* Debug helpers: Print the tree to stdout in dot format. */ static void _bostree_print_helper(BOSNode *node) { printf(" %s [label=\"\\N (%d,%d,%d)\"];\n", (char *)node->key, node->left_child_count, node->right_child_count, node->depth); if(node->parent_node) { printf(" %s -> %s [color=green];\n", (char *)node->key, (char *)node->parent_node->key); } if(node->left_child_node != NULL) { printf(" %s -> %s\n", (char *)node->key, (char *)node->left_child_node->key); _bostree_print_helper(node->left_child_node); } if(node->right_child_node != NULL) { printf(" %s -> %s\n", (char *)node->key, (char *)node->right_child_node->key); _bostree_print_helper(node->right_child_node); } } void bostree_print(BOSTree *tree) { if(tree->root_node == NULL) { return; } printf("digraph {\n ordering = out;\n"); _bostree_print_helper(tree->root_node); printf("}\n"); fsync(0); } #endif pqiv-2.12/lib/bostree.h000066400000000000000000000103401376070546500150120ustar00rootroot00000000000000/* 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). The tree is implemented with map semantics, that is, there are separete key and value pointers. Copyright (c) 2017, 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; unsigned char weak_ref_node_valid; }; typedef struct _BOSNode BOSNode; /* Public interface */ /** * Key comparison function. * * Should return a positive value if the second argument is larger than the * first one, a negative value if the first is larger, and zero exactly if both * are equal. */ typedef int (*BOSTree_cmp_function)(const void *, const void *); /** * Free function for deleted nodes. * * This function should free the key and data members of a node. The node * structure itself is free()d internally by BOSZTree. */ typedef void (*BOSTree_free_function)(BOSNode *node); /** * Create a new tree. * * The cmp_function is mandatory, but for the free function, you may supply a * NULL pointer if you do not have any data that needs to be free()d in * ->key and ->data. */ BOSTree *bostree_new(BOSTree_cmp_function cmp_function, BOSTree_free_function free_function); /** * Destroy a tree and all its members. */ void bostree_destroy(BOSTree *tree); /** * Return the number of nodes in a tree */ unsigned int bostree_node_count(BOSTree *tree); /** * Insert a new member into the tree and return the associated node. */ BOSNode *bostree_insert(BOSTree *tree, void *key, void *data); /** * Remove a given node from a tree. */ void bostree_remove(BOSTree *tree, BOSNode *node); /** * Weak reference management for nodes. * * Nodes have an internal reference counter. They are only free()d after the * last weak reference has been removed. Calling bostree_node_weak_unref() on a * node which has been removed from the tree results in the weak reference * count being decreased, the node being possibly free()d if this has been the * last weak reference, and a NULL pointer being returned. */ BOSNode *bostree_node_weak_ref(BOSNode *node); /** * Weak reference management for nodes. * See bostree_node_weak_ref() */ BOSNode *bostree_node_weak_unref(BOSTree *tree, BOSNode *node); /** * Return a node given a key. NULL is returned if a key is not present in the * tree. */ BOSNode *bostree_lookup(BOSTree *tree, const void *key); /** * Return a node given an index in in-order traversal. Indexing starts at 0. */ BOSNode *bostree_select(BOSTree *tree, unsigned int index); /** * Return the next node in in-order traversal, or NULL is node was the last * node in the tree. */ BOSNode *bostree_next_node(BOSNode *node); /** * Return the previous node in in-order traversal, or NULL is node was the first * node in the tree. */ BOSNode *bostree_previous_node(BOSNode *node); /** * Return the rank of a node within it's owning tree. * * bostree_select(bostree_rank(node)) == node is always true. */ unsigned int bostree_rank(BOSNode *node); #if !defined(NDEBUG) && (_BSD_SOURCE || _XOPEN_SOURCE || _POSIX_C_SOURCE >= 200112L) void bostree_print(BOSTree *tree); #define bostree_debug(...) fprintf(stderr, __VA_ARGS__) #else #define bostree_debug(...) void #endif #endif pqiv-2.12/lib/config_parser.c000066400000000000000000000136231376070546500161720ustar00rootroot00000000000000/* 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.12/lib/config_parser.h000066400000000000000000000030121376070546500161660ustar00rootroot00000000000000/* 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.12/lib/filebuffer.c000066400000000000000000000167531376070546500154710ustar00rootroot00000000000000/** * 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 #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; GRecMutex file_buffer_table_mutex; #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) { g_rec_mutex_lock(&file_buffer_table_mutex); 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)) { if(file->file_data_loader) { data_bytes = file->file_data_loader(file, error_pointer); } else { data_bytes = g_bytes_ref(file->file_data); } if(!data_bytes) { g_rec_mutex_unlock(&file_buffer_table_mutex); return NULL; } } 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); g_rec_mutex_unlock(&file_buffer_table_mutex); 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); if(fd < 0) { g_object_unref(input_file); g_rec_mutex_unlock(&file_buffer_table_mutex); *error_pointer = g_error_new(g_quark_from_static_string("pqiv-filebuffer-error"), 1, "Opening the file failed with errno=%d: %s", errno, strerror(errno)); return NULL; } void *input_file_data = mmap(NULL, input_file_size, PROT_READ, MAP_SHARED, fd, 0); if(input_file_data != MAP_FAILED) { 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); } else { close(fd); } } 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) { g_rec_mutex_unlock(&file_buffer_table_mutex); 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) { g_rec_mutex_unlock(&file_buffer_table_mutex); 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++; g_rec_mutex_unlock(&file_buffer_table_mutex); return buffer->data; } char *buffered_file_as_local_file(file_t *file, GInputStream *data, GError **error_pointer) { g_rec_mutex_lock(&file_buffer_table_mutex); 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++; g_rec_mutex_unlock(&file_buffer_table_mutex); 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); g_rec_mutex_unlock(&file_buffer_table_mutex); 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); g_rec_mutex_unlock(&file_buffer_table_mutex); 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); } g_rec_mutex_unlock(&file_buffer_table_mutex); 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++; g_rec_mutex_unlock(&file_buffer_table_mutex); return buffer->file_name; } void buffered_file_unref(file_t *file) { g_rec_mutex_lock(&file_buffer_table_mutex); struct buffered_file *buffer = g_hash_table_lookup(file_buffer_table, file->file_name); if(!buffer) { g_rec_mutex_unlock(&file_buffer_table_mutex); 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); } g_rec_mutex_unlock(&file_buffer_table_mutex); } pqiv-2.12/lib/filebuffer.h000066400000000000000000000026531376070546500154700ustar00rootroot00000000000000/** * 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.12/lib/strnatcmp.c000066400000000000000000000104541376070546500153630ustar00rootroot00000000000000/* -*- 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.12/lib/strnatcmp.h000066400000000000000000000024071376070546500153670ustar00rootroot00000000000000/* -*- 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.12/lib/thumbnailcache.c000066400000000000000000000455261376070546500163270ustar00rootroot00000000000000/* * This file is part of pqiv * Copyright (c) 2017, 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 implements thumbnail caching as specified in * https://specifications.freedesktop.org/thumbnail-spec/thumbnail-spec-latest.html */ #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE #include "thumbnailcache.h" #ifdef _WIN32 #include #else #include #endif #include #include #include #include #include #include #include #include #include #if !GLIB_CHECK_VERSION(2, 36, 0) #define g_close(fd, errptr) close(fd) #endif #include #include static const char * thumbnail_levels[] = { "x-pqiv", "large", "normal" }; /* CRC calculation as per PNG TR, Annex D */ static unsigned long crc_table[256]; static gboolean crc_table_computed = 0; static void make_crc_table(void) { unsigned long c; int n, k; for(n = 0; n < 256; n++) { c = (unsigned long) n; for(k = 0; k < 8; k++) { if(c & 1) { c = 0xedb88320L ^ (c >> 1); } else { c >>= 1; } } crc_table[n] = c; } crc_table_computed = 1; } static unsigned long crc(unsigned long crc, unsigned char *buf, int len) { unsigned long c = crc ^ 0xffffffffL; int n; if (!crc_table_computed) { make_crc_table(); } for (n = 0; n < len; n++) { c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8); } return c ^ 0xffffffffL; } /* Auxiliary functions */ static gchar *get_local_filename(file_t *file) { // Memory files do not have a file name if(file->file_flags & FILE_FLAGS_MEMORY_IMAGE) { return NULL; } // Retrieve file name GFile *gfile = gfile_for_commandline_arg(file->file_name); gchar *file_path = g_file_get_path(gfile); g_object_unref(gfile); return file_path; } static const gchar *get_multi_page_suffix(file_t *file) { // Multi-page documents do not have an unambigous file name // Since the Thumbnail Managing Standard does not state how to format an // URI into e.g. an archive, we need to make something up. Do not use // the standard directories in this case. gchar *display_basename = g_strrstr(file->display_name, G_DIR_SEPARATOR_S); if(display_basename) { display_basename++; } else { display_basename = file->display_name; } gchar *filename_basename = g_strrstr(file->file_name, G_DIR_SEPARATOR_S); if(filename_basename) { filename_basename++; } else { filename_basename = file->file_name; } int filename_basename_length = strlen(filename_basename); int display_basename_length = strlen(display_basename); if(filename_basename_length == display_basename_length) { return NULL; } return display_basename + filename_basename_length; } static gchar *_local_thumbnail_cache_directory; static const gchar *get_thumbnail_cache_directory() { if(!_local_thumbnail_cache_directory) { const gchar *cache_dir = g_getenv("XDG_CACHE_HOME"); if(!cache_dir) { _local_thumbnail_cache_directory = g_build_filename(g_getenv("HOME"), ".cache", "thumbnails", NULL); } else { _local_thumbnail_cache_directory = g_build_filename(cache_dir, "thumbnails", NULL); } } return _local_thumbnail_cache_directory; } gboolean check_png_attributes(gchar *file_name, gchar *file_uri, time_t file_mtime) { // Parse PNG headers and check whether the Thumb::URI and Thumb::MTime // headers are up to date. // // See below in png_writer for a rough explaination, or read the PNG TR // https://www.w3.org/TR/PNG/ // gboolean file_uri_match = FALSE; gboolean file_mtime_match = FALSE; int fd = g_open(file_name, O_RDONLY, 0); if(fd < 0) { return FALSE; } union { char buf[8]; uint32_t uint32; } header; // File header if(read(fd, header.buf, 8) != 8) { g_close(fd, NULL); return FALSE; } const unsigned char expected_header[] = { 137, 80, 78, 71, 13, 10, 26, 10 }; if(memcmp(header.buf, expected_header, sizeof(expected_header)) != 0) { g_close(fd, NULL); return FALSE; } // Read all chunks until we have both matches while(1) { if(read(fd, header.buf, 8) != 8) { g_close(fd, NULL); return FALSE; } int header_length = (int)ntohl(header.uint32); if(header_length < 0) { // While technically, this is allowed, no header should ever be // this large. g_close(fd, NULL); return FALSE; } if(strncmp(&header.buf[4], "tEXt", 4) == 0) { // This is interesting. Read the whole contents first. char *data = g_malloc(header_length); if(read(fd, data, header_length) != header_length) { g_free(data); g_close(fd, NULL); return FALSE; } // Check against CRC if(read(fd, header.buf, 4) != 4) { g_free(data); g_close(fd, NULL); return FALSE; } unsigned file_crc = ntohl(header.uint32); unsigned actual_crc = crc(crc(0, (unsigned char*)"tEXt", 4), (unsigned char *)data, header_length); if(file_crc == actual_crc) { if(strcmp(data, "Thumb::URI") == 0) { file_uri_match = strncmp(&data[sizeof("Thumb::URI")], file_uri, strlen(file_uri)) == 0; } else if(strcmp(data, "Thumb::MTime") == 0) { gchar *file_mtime_str = g_strdup_printf("%" PRIuMAX, (intmax_t)file_mtime); file_mtime_match = strncmp(&data[sizeof("Thumb::MTime")], file_mtime_str, strlen(file_mtime_str)) == 0; g_free(file_mtime_str); } if(file_uri_match && file_mtime_match) { g_free(data); g_close(fd, NULL); return TRUE; } } g_free(data); } else { // Skip header and its CRC if(lseek(fd, header_length + 4, SEEK_CUR) < 0) { g_close(fd, NULL); return FALSE; } } } } static cairo_surface_t *load_thumbnail(gchar *file_name, gchar *file_uri, time_t file_mtime, unsigned width, unsigned height) { // Check if the file is up to date if(!check_png_attributes(file_name, file_uri, file_mtime)) { return NULL; } cairo_surface_t *thumbnail = cairo_image_surface_create_from_png(file_name); if(cairo_surface_status(thumbnail) != CAIRO_STATUS_SUCCESS) { cairo_surface_destroy(thumbnail); return NULL; } unsigned actual_width = cairo_image_surface_get_width(thumbnail); unsigned actual_height = cairo_image_surface_get_height(thumbnail); if(actual_width == width || actual_height == height) { return thumbnail; } if(actual_width < width && actual_height < height) { // Can't use this. Too small. cairo_surface_destroy(thumbnail); return NULL; } double scale_factor = fmin(1., fmin(width * 1. / actual_width, height * 1. / actual_height)); unsigned target_width = actual_width * scale_factor; unsigned target_height = actual_height * scale_factor; #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 12, 0) cairo_surface_t *target_thumbnail = cairo_surface_create_similar_image(thumbnail, CAIRO_FORMAT_ARGB32, target_width, target_height); #else cairo_surface_t *target_thumbnail = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, target_width, target_height); #endif cairo_t *cr = cairo_create(target_thumbnail); cairo_scale(cr, scale_factor, scale_factor); cairo_set_source_surface(cr, thumbnail, 0, 0); cairo_paint(cr); cairo_destroy(cr); cairo_surface_destroy(thumbnail); thumbnail = target_thumbnail; if(cairo_surface_status(thumbnail) != CAIRO_STATUS_SUCCESS) { cairo_surface_destroy(thumbnail); return NULL; } return thumbnail; } /* This library's public API */ gboolean load_thumbnail_from_cache(file_t *file, unsigned width, unsigned height, thumbnail_persist_mode_t persist_mode, char *special_thumbnail_directory) { if(persist_mode == THUMBNAILS_PERSIST_OFF) { return FALSE; } // Obtain a local path to the file gchar *local_filename = get_local_filename(file); if(!local_filename) { return FALSE; } const gchar *multi_page_suffix = get_multi_page_suffix(file); // Obtain modification timestamp struct stat file_stat; if(stat(local_filename, &file_stat) < 0) { g_free(local_filename); return FALSE; } time_t file_mtime = file_stat.st_mtime; // Obtain the name of the candidate for the local thumbnail file gchar *file_uri = multi_page_suffix ? g_strdup_printf("file://%s#%s", local_filename, multi_page_suffix) : g_strdup_printf("file://%s", local_filename); gchar *md5_filename = g_compute_checksum_for_string(G_CHECKSUM_MD5, file_uri, -1); // Search two directory structures: special_thumbnail_directory, then get_thumbnail_cache_directory() for(int k=0; k<2; k++) { if(k == 0 && special_thumbnail_directory == NULL) { continue; } // Search in the directories for the different sizes for(int j=(!multi_page_suffix && width <= 128 && height <= 128) ? 2 : (!multi_page_suffix && width <= 256 && height <= 256) ? 1 : 0; j>=0; j--) { gchar *thumbnail_candidate; if(j == 0) { thumbnail_candidate = g_strdup_printf("%s%s%s%s%dx%d%s%s.png", k == 0 ? special_thumbnail_directory : get_thumbnail_cache_directory(), G_DIR_SEPARATOR_S, thumbnail_levels[j], G_DIR_SEPARATOR_S, width, height, G_DIR_SEPARATOR_S, md5_filename); } else { thumbnail_candidate = g_strdup_printf("%s%s%s%s%s.png", k == 0 ? special_thumbnail_directory : get_thumbnail_cache_directory(), G_DIR_SEPARATOR_S, thumbnail_levels[j], G_DIR_SEPARATOR_S, md5_filename); } if(g_file_test(thumbnail_candidate, G_FILE_TEST_EXISTS)) { cairo_surface_t *thumbnail = load_thumbnail(thumbnail_candidate, file_uri, file_mtime, width, height); g_free(thumbnail_candidate); if(thumbnail != NULL) { file->thumbnail = thumbnail; g_free(local_filename); g_free(file_uri); g_free(md5_filename); return TRUE; } } else { g_free(thumbnail_candidate); } } } g_free(file_uri); g_free(md5_filename); // Check if a shared thumbnail directory exists and try to load from there gchar *file_dirname = g_path_get_dirname(local_filename); gchar *shared_thumbnail_directory = g_build_filename(file_dirname, ".sh_thumbnails", NULL); g_free(file_dirname); if(g_file_test(shared_thumbnail_directory, G_FILE_TEST_IS_DIR)) { gchar *file_basename = g_path_get_basename(local_filename); if(multi_page_suffix) { gchar *new_basename = g_strdup_printf("%s#%s", file_basename, multi_page_suffix); g_free(file_basename); file_basename = new_basename; } gchar *md5_basename = g_compute_checksum_for_string(G_CHECKSUM_MD5, file_basename, -1); for(int j=(!multi_page_suffix && width <= 128 && height <= 128) ? 2 : (!multi_page_suffix && width <= 256 && height <= 256) ? 1 : 0; j>=0; j--) { gchar *thumbnail_candidate; if(j == 0) { thumbnail_candidate = g_strdup_printf("%s%s%s%s%dx%d%s%s.png", shared_thumbnail_directory, G_DIR_SEPARATOR_S, thumbnail_levels[j], G_DIR_SEPARATOR_S, width, height, G_DIR_SEPARATOR_S, md5_basename); } else { thumbnail_candidate = g_strdup_printf("%s%s%s%s%s.png", shared_thumbnail_directory, G_DIR_SEPARATOR_S, thumbnail_levels[j], G_DIR_SEPARATOR_S, md5_basename); } if(g_file_test(thumbnail_candidate, G_FILE_TEST_EXISTS)) { cairo_surface_t *thumbnail = load_thumbnail(thumbnail_candidate, file_basename, file_mtime, width, height); g_free(thumbnail_candidate); if(thumbnail != NULL) { file->thumbnail = thumbnail; g_free(md5_basename); g_free(local_filename); g_free(shared_thumbnail_directory); return TRUE; } } else { g_free(thumbnail_candidate); } } g_free(md5_basename); g_free(file_basename); } g_free(shared_thumbnail_directory); g_free(local_filename); return FALSE; } struct png_writer_info { int output_file_fd; size_t bytes_written; gchar *Thumb_URI; gchar *Thumb_MTime; }; static cairo_status_t png_writer(struct png_writer_info *info, const unsigned char *data, unsigned int length) { // This is actually quite simple: A PNG file always begins with the bytes // (137, 80, 78, 71, 13, 10, 26, 10), followed by chunks, which are // 4 bytes payload length, 4 bytes (ASCII) type, payload, and 4 bytes CRC // as defined above, taken over type & payload. // We want to inject a chunk of type tEXt, whose payload is key\0value, // after the IHDR header, which does always come first, is required, and // has fixed length 13. // const unsigned inject_pos = 8 /* header */ + (4 + 4 + 4 + 13) /* IHDR */; if(info->bytes_written < inject_pos && info->bytes_written + length >= inject_pos) { ssize_t result = write(info->output_file_fd, data, inject_pos - info->bytes_written); if(result < 0 || (size_t)result != inject_pos - info->bytes_written) { return CAIRO_STATUS_WRITE_ERROR; } data += inject_pos - info->bytes_written; length -= inject_pos - info->bytes_written; info->bytes_written = inject_pos; int uri_length = strlen(info->Thumb_URI); int output_length = 4 + 4 + sizeof("Thumb::URI") + uri_length + 4; char *output = g_malloc(output_length); uint32_t write_uint32 = htonl(sizeof("Thumb::URI") + uri_length); memcpy(output, &write_uint32, sizeof(uint32_t)); strcpy(&output[4], "tEXtThumb::URI"); strcpy(&output[19], info->Thumb_URI); write_uint32 = htonl(crc(0, (unsigned char*)&output[4], 19 + uri_length - 4)); memcpy(&output[19+uri_length] , &write_uint32, sizeof(uint32_t)); if(write(info->output_file_fd, output, output_length) != output_length) { return CAIRO_STATUS_WRITE_ERROR; } g_free(output); int mtime_length = strlen(info->Thumb_MTime); output_length = 4 + 4 + sizeof("Thumb::MTime") + mtime_length + 4; output = g_malloc(output_length); write_uint32 = htonl(sizeof("Thumb::MTime") + mtime_length); memcpy(output, &write_uint32, sizeof(uint32_t)); strcpy(&output[4], "tEXtThumb::MTime"); strcpy(&output[21], info->Thumb_MTime); write_uint32 = htonl(crc(0, (unsigned char*)&output[4], 21 + mtime_length - 4)); memcpy(&output[21+mtime_length], &write_uint32, sizeof(uint32_t)); if(write(info->output_file_fd, output, output_length) != output_length) { return CAIRO_STATUS_WRITE_ERROR; } g_free(output); } if(length > 0) { ssize_t result = write(info->output_file_fd, data, length); if(result < 0 || (unsigned int)result != length) { return CAIRO_STATUS_WRITE_ERROR; } info->bytes_written += length; } return CAIRO_STATUS_SUCCESS; } gboolean store_thumbnail_to_cache(file_t *file, unsigned width, unsigned height, thumbnail_persist_mode_t persist_mode, char *special_thumbnail_directory) { if(persist_mode == THUMBNAILS_PERSIST_OFF || persist_mode == THUMBNAILS_PERSIST_RO) { return FALSE; } // We only store thumbnails if they have the correct size unsigned actual_width = cairo_image_surface_get_width(file->thumbnail); unsigned actual_height = cairo_image_surface_get_height(file->thumbnail); int thumbnail_level; // If the file didn't need thumbnailing, don't store a thumbnail either. // This is a simple way to make sure that we don't accidentally thumbnail // thumbnails again. if(actual_width == file->width || actual_height == file->height) { return FALSE; } if(width == 256 && height == 256) { thumbnail_level = 1; } else if(width == 128 && height == 128) { thumbnail_level = 2; } else { thumbnail_level = 0; if(persist_mode == THUMBNAILS_PERSIST_STANDARD) { return FALSE; } } // Obtain absolute path to file gchar *local_filename = get_local_filename(file); if(!local_filename) { return FALSE; } const gchar *multi_page_suffix = get_multi_page_suffix(file); if(multi_page_suffix) { // Unspecified by standard, use x-pqiv cache thumbnail_level = 0; if(persist_mode == THUMBNAILS_PERSIST_STANDARD) { g_free(local_filename); return FALSE; } } // Obtain modification timestamp struct stat file_stat; if(stat(local_filename, &file_stat) < 0) { g_free(local_filename); return FALSE; } time_t file_mtime = file_stat.st_mtime; // Obtain the name of the thumbnail file gchar *file_uri, *md5_filename, *thumbnail_directory, *thumbnail_file; if(persist_mode == THUMBNAILS_PERSIST_LOCAL) { // Create a .sh_thumbnails directory file_uri = g_path_get_basename(local_filename); if(multi_page_suffix) { gchar *new_uri = g_strdup_printf("%s#%s", file_uri, multi_page_suffix); g_free(file_uri); file_uri = new_uri; } md5_filename = g_compute_checksum_for_string(G_CHECKSUM_MD5, file_uri, -1); gchar *file_dirname = g_path_get_dirname(local_filename); if(thumbnail_level == 0) { thumbnail_directory = g_strdup_printf("%s%s.sh_thumbnails%s%s%s%dx%d", file_dirname, G_DIR_SEPARATOR_S, G_DIR_SEPARATOR_S, thumbnail_levels[0], G_DIR_SEPARATOR_S, width, height); } else { thumbnail_directory = g_strdup_printf("%s%s.sh_thumbnails%s%s", file_dirname, G_DIR_SEPARATOR_S, G_DIR_SEPARATOR_S, thumbnail_levels[thumbnail_level]); } g_free(file_dirname); thumbnail_file = g_strdup_printf("%s%s%s.png", thumbnail_directory, G_DIR_SEPARATOR_S, md5_filename); } else { // Use the standardized cache format, possibly with special directory if(multi_page_suffix) { file_uri = g_strdup_printf("file://%s#%s", local_filename, multi_page_suffix); } else { file_uri = g_strdup_printf("file://%s", local_filename); } md5_filename = g_compute_checksum_for_string(G_CHECKSUM_MD5, file_uri, -1); if(thumbnail_level == 0) { thumbnail_directory = g_strdup_printf("%s%s%s%s%dx%d", special_thumbnail_directory ? special_thumbnail_directory : get_thumbnail_cache_directory(), G_DIR_SEPARATOR_S, thumbnail_levels[thumbnail_level], G_DIR_SEPARATOR_S, width, height); } else { thumbnail_directory = g_strdup_printf("%s%s%s", special_thumbnail_directory ? special_thumbnail_directory : get_thumbnail_cache_directory(), G_DIR_SEPARATOR_S, thumbnail_levels[thumbnail_level]); } thumbnail_file = g_strdup_printf("%s%s%s.png", thumbnail_directory, G_DIR_SEPARATOR_S, md5_filename); } // Create the directory if necessary if(!g_file_test(thumbnail_directory, G_FILE_TEST_IS_DIR)) { g_mkdir_with_parents(thumbnail_directory, 0700); } g_free(thumbnail_directory); // Write out thumbnail // We use a wrapper to inject the tEXt chunks as required by the thumbnail standard gboolean retval = TRUE; int file_fd = g_open(thumbnail_file, O_CREAT | O_WRONLY, 0600); if(file_fd >= 0) { gchar *string_mtime = g_strdup_printf("%" PRIuMAX, (intmax_t)file_mtime); struct png_writer_info writer_info = { file_fd, 0, file_uri, string_mtime }; if(cairo_surface_write_to_png_stream(file->thumbnail, (cairo_write_func_t)png_writer, &writer_info) != CAIRO_STATUS_SUCCESS) { g_unlink(thumbnail_file); retval = FALSE; } g_free(string_mtime); } g_close(file_fd, NULL); g_free(file_uri); g_free(md5_filename); g_free(thumbnail_file); g_free(local_filename); return retval; } #else void __thumbnailcache__empty_translation_unit() {} #endif pqiv-2.12/lib/thumbnailcache.h000066400000000000000000000026001376070546500163160ustar00rootroot00000000000000/* * This file is part of pqiv * Copyright (c) 2017, 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 implements thumbnail caching as specified in * https://specifications.freedesktop.org/thumbnail-spec/thumbnail-spec-latest.html */ #include "../pqiv.h" #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE typedef enum { THUMBNAILS_PERSIST_OFF, THUMBNAILS_PERSIST_ON, THUMBNAILS_PERSIST_STANDARD, THUMBNAILS_PERSIST_RO, THUMBNAILS_PERSIST_LOCAL } thumbnail_persist_mode_t; gboolean load_thumbnail_from_cache(file_t *file, unsigned width, unsigned height, thumbnail_persist_mode_t persist_mode, char *special_thumbnail_directory); gboolean store_thumbnail_to_cache(file_t *file, unsigned width, unsigned height, thumbnail_persist_mode_t persist_mode, char *special_thumbnail_directory); #endif pqiv-2.12/lib/update.sh000066400000000000000000000006551376070546500150240ustar00rootroot00000000000000#!/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.12/pqiv.1000066400000000000000000000724271376070546500135070ustar00rootroot00000000000000.\" vim:filetype=groff .TH pqiv 1 "November 2020" "2.12" .SH NAME pqiv \- powerful 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\-\-actions\-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 one to bind further keys to other commands. \fICOMMAND\fR is executed using the default shell processor. `$1' is substituted with the current file name. Unless \fICOMMAND\fR begins with `|' or `-', if `$1' is not present, the file name is appended to the command line. .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. .PP If \fICOMMAND\fR begins with `-', a list of currently marked images is piped to its standard input. .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 \-\-allow\-empty\-window \fBpqiv\fR normally does not display the main window until one image has been found, and quits when it cannot load any of the images anymore. With this option, both situations result in an empty \fBpqiv\fR window being shown. .\" .TP .BR \-\-auto\-montage\-mode Automatically enter montage mode if \fBpqiv\fR is started with more than one image. .\" .TP .BR \-\-background\-pattern=\fIPATTERN\fR \fBpqiv\fR draws a checkerboard as transparent images' background. Use this option to alternatively use a white or black background. Valid values are \fIcheckerboard\fR, \fIwhite\fR and \fIblack\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. .PP \fBpqiv\fR groups key bindings into different contexts. Currently, \fImontage\fR mode is the only context other than the default one: In \fImontage\fR mode, different key bindings are used. To switch the context while binding key sequences, write .RS @MONTAGE { ... } .RE and insert the special key bindings within the curly braces. .RE .\" .TP .BR \-\-box\-colors=\fIFOREGROUND\ COLOR:BACKGROUND\ COLOR\fR Customize the colors used to draw the info box and montage mode borders. Colors can be specified either as a comma separated list of RBG-values in the range from 0 to 255 or as a hexvalue, e.g., #aabbcc. The default value is \fI#000000:#ffff00\fR. .\" .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\-backends=\fILIST\ OF\ BACKENDS\fR Use this option to selectively disable some of \fBpqiv\fR's backends. You can supply a comma separated list of backends here. Non-available backends are silently ignored. Disabling backends you don't want will speed up recursive loading significantly, especially if you disable the archive backend. Available backends are: .RS .IP archive 14 generic archive file support .IP archive_cbx *.cb? comic book archive support .IP libav video support, works with ffmpeg as well .IP gdkpixbuf images .IP poppler PDF .IP spectre PS/EPS .IP wand ImageMagick, various formats, e.g. PSD .IP webp WebP format .RE .\" .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 \-\-negate Display negatives of images. You can toggle this feature at runtime by pressing \fIn\fR. .\" .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 \-\-thumbnail\-size=\fIWIDTHxHEIGHT\fR Adjust the size of thumbnails in \fImontage\fR mode. The default is 128x128. .\" .TP .BR \-\-thumbnail\-preload=\fICOUNT\fR Preload \fICOUNT\fR thumbnails adjacent to the current image while displaying images or having them selected in montage mode. This can be used to speed up montage mode, but will lead to high CPU loads. .\" .TP .BR \-\-thumbnail\-persistence=\fIDIRECTORY/STATUS\fR Persist thumbnails to disk. The simplest way to use this option is to supply a value of \fIyes\fR. Thumbnails are then stored according to the Thumbnail Managing Standard, in \fI$XDG_CACHE_HOME/thumbnails/*\fR. The standard allows storage of thumbnails in sizes 128x128 and 256x256 exclusively, and does not specify how to store thumbnails for files in archives or multi-page documents. Thumbnails violating those constraints will be stored in a special \fIx-pqiv\fR subfolder. Supply \fIstandard\fR to store standard compliant thumbnails only. If this option is not used, then thumbnails will not be loaded from the cache either \- any thumbnails will be regenerated each time \fImontage\fR mode is used. A value of \fIread-only\fR can be used to load thumbnails, but never store them. \fIread-only\fR is the default. .PP .RS If you supply \fIlocal\fR as the argument value, \fBpqiv\fR will store thumbnails in a subfolder named \fI.sh_thumbnails\fR relative to the images as specified by the Thumbnail Managing Standard. Your third option is to provide the name of a directory. \fBpqiv\fR will then use that directory to store thumbnails to. The folder must be given as an absolute path, relative paths do not work. Note that any folder not named \fI.sh_thumbnails\fR will be considered in \fB\-\-watch\-directories\fR. Also, note that while \fBpqiv\fR will store thumbnails to another folder, it will still attempt to load them from the standard folders as well. .RE .\" .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 \-\-scale\-mode\-screen\-fraction=\fIFRACTION\fR Adjust how much screen space \fBpqiv\fR uses when auto-scaling images outside fullscreen mode. Defaults to 0.8 (80%). .\" .TP .BR \-\-wait\-for\-images\-to\-appear If no images are found in the directories specified on the command line, instead of exiting, wait for some to appear. This option only works in conjunction with \fB\-\-lazy\-load\fR and \fB\-\-watch\-directories\fR. .\" .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 animation_step(INT) Stop an animation, and advance by the given number of frames plus one. .TP .BR animation_continue() Continue a stopped animation. .TP .BR animation_set_speed_relative(DOUBLE) Scale the animation's display speed. .TP .BR animation_set_speed_absolute(DOUBLE) Set the animation's display speed scale level to an absolute value. 1.0 is the animation's natural speed. .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 clear_marks() Clear all marks. .TP .BR command(STRING) Execute the given shell command. The syntax of the argument is the same as for the \fB\-\-command\-1\fR option. .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_earlier_file() Return to the image that was opened before the current one. .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 goto_logical_directory_relative(INT) Jump to the \fIn\fR'th next or previous logical directory. Any multi-page documents, such as PDFs or archive files, are regarded as logical directories. Directories within archive files, recognizable by a slash in the archive member's file name, are regarded as directories too. Basically, the rule is that two images are in the same logical directory if no character following the common prefix of their file names in either name is a slash or a hash symbol. .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 montage_mode_enter() Enter montage mode, a view for interactive selection of images. .TP .BR montage_mode_follow(KEYS) Set up "follow" mode: Bind a sequence composed of the keys in KEYS to each visible thumbnail, such that typing that sequence moves the cursor to said position. At the same time, turn on binding overlays, increase the keyboard timeout, and revert everything after an image has been selected. .TP .BR montage_mode_return_proceed() Leave montage mode, and goto the currently selected image. .TP .BR montage_mode_return_cancel() Leave montage mode, and return to the last image viewed before entering montage mode. .TP .BR montage_mode_set_shift_x(INT) Set the horizontal cursor position in montage mode to an absolute value, indexed from 0. .TP .BR montage_mode_set_shift_y(INT) Set the vertical cursor position in montage mode to an absolute value, indexed from 0. .TP .BR montage_mode_set_wrap_mode(INT) Adjust how wrapping around edges works when shifting the cursor position in montage mode: The default, \fI1\fR, is to wrap around rows but not around the whole image list. Set this to \fI0\fR to disable wrapping entirely. A value of \fI2\fR enables full wrapping. .TP .BR montage_mode_shift_x(INT) Shift the cursor in montage mode in horizontal direction. Shifts wrap around edges to the adjacent vertical lines, but not around the end of the list back to its beginning. .TP .BR montage_mode_shift_y(INT) Shift the cursor in montage mode in vertical direction. .TP .BR montage_mode_show_binding_overlays(INT) Disable (by using a parameter value of 0) or enable (by using any other value) follow mode. In follow mode, \fBpqiv\fR will draw mnemonics on top of each thumbnail that is reachable by typing a key (combination). Use this to realize keyboard navigation similar to vimperator/pentadactyl/vimium/etc. .TP .BR montage_mode_shift_y_pg(INT) Shift the cursor in montage mode in vertical direction by \fIn\fR pages. .TP .BR move_window(INT,\ INT) Move \fBpqiv\fR's window to the specified coordinates. Negative values center the window on the current monitor. .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_cursor_auto_hide(INT) Automatically show the cursor when the pointer moves, and hide it after one second of inactivity. Set to 0 to disable this feature or any other value to enable it. Note that this enables pointer movement events, which might slow down pqiv if it is used over slow network links. .TP .BR set_fade_duration(DOUBLE) Set the duration of fades between images. In contrast to the command line option, this action also implicitly enables fading. Set the duration to zero to disable the feature. .TP .BR set_interpolation_quality(INT) Set the interpolation quality for resized images. Options are: 0 to cycle between the different modes, 1 for an automated choice based on the image size (small images use nearest interpolation, large ones Cairo's `good' mode), 2 for `fast', 3 for `good' and 4 for `best'. .TP .BR set_keyboard_timeout(DOUBLE) Set the timeout for key sequence input. For example, if you bind something to \fIa\fI and another action to \fIab\fR, \fBpqiv\fR will give you by default half a second to enter the \fIb\fR before assuming that you intended to type only \fIa\fR. Use this action to change this value. .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_scale_mode_screen_fraction(DOUBLE) Adjust how much of the available screen space is used for scaling the window outside fullscreen mode. Defaults to 0.8. This also affects the size of the \fImontage\fR mode window. .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 set_thumbnail_preload(INT) Change the amount of thumbnails to be preloaded. A value of zero disables the feature. .TP .BR set_thumbnail_size(INT, INT) Change the size of thumbnails. The order of the arguments is width, then height. Thumbnails are always scaled such that no side is larger than this limit. Note that the persistent thumbnail cache only supports 128x128 and 256x256 thumbnails. .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_background_pattern(INT) Toggle between the different background patterns: 0 to toggle, 1 for checkerboard pattern, 2 for black, 3 for white. .TP .BR toggle_fullscreen(INT) Toggle fullscreen mode: 0 to toggle, 1 to go to fullscreen, 2 to return to window mode. .TP .BR toggle_info_box() Toggle the visibility of the info box. .TP .BR toggle_mark() Toggle the current image's mark. .TP .BR toggle_negate_mode(INT) Toggle negate (color inversion) mode: 0 to toggle, 1 to enable, 2 to disable. .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, 4 to maintain the user-set zoom level, and 5 to maintain the window's size. 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 Backspace/Space 25 Previous/Next file. .IP ctrl-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 m Toggle \fImontage\fR mode, an interactive image selection mode. Use cursor keys or your mouse to select an image and Return to return to single image view. Use \fIg\fR to quickly navigate to a thumbnail. .IP o Toggle a mark on an image. Use \fIctrl-R\fR to reset all marks. Used in conjunction with commands starting with a \fI-\fR. .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 ctrl-t Maintain user-set scale level. .IP mod-t Maintain the window's size. .IP Plus/Minus Zoom. .IP "Period, ctrl-Period" Stop, single-step and continue animated images. .IP "mod-Plus, mod-Minus" Alter animation speed. .IP ctrl-r Go to the image viewed before the current one. .IP "ctrl-Space, ctrl-Backspace" Go to the next/previous logical directory. .IP "ctrl-Plus, ctrl-Minus" Alter slideshow interval. .IP b Toggle background pattern for transparent images. .IP n Toggle negate ("negative") mode. .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. The descriptions of the actions above is annotated with those bindings. You can also run `\fBpqiv \-\-show\-bindings\fR' to display a complete list. .\" .SH CONFIGURATION FILE Upon startup, \fBpqiv\fR parses the file \fI~/.config/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 Similarly, you can also specify (multiple) actions to be executed each time \fBpqiv\fR is started in a section called \fI[actions]\fR. .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. .PP Other supported paths for the configuration file are \fI~/.pqivrc\fR, \fI/etc/xdg/pqivrc\fR and \fI/etc/pqivrc\fR. \fBpqiv\fR will use whichever file it finds first. You can use the environment variable \fIPQIVRC_PATH\fR to override the configuration file. .SH EXAMPLES .\" .TP \fBpqiv \-\-bind\-key="a { goto_file_byindex(0) }" \-\-bind\-key='c { command(echo -n $1 | xclip) }' \-\-sort foo bar.pdf\fR Rebinds \fBa\fR to go back to the first image, \fBc\fR to store the path to the current image to the clipboard using \fIxclip\fR 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 \fBpqiv \-\-slideshow \-\-allow\-empty\-window \-\-watch\-directories \-\-wait\-for\-images\-to\-appear \-\-lazy\-load test\fR Set up a slideshow that displays all images from the \fItest\fR folder such that it is possible to remove all images from the directory and place new ones into it afterwards without \fBpqiv\fR exiting. .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.12/pqiv.c000066400000000000000000011222551376070546500135650ustar00rootroot00000000000000/** * pqiv * * Copyright (c) 2013-2017, 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 700 #define _GNU_SOURCE #include "pqiv.h" #include "lib/config_parser.h" #include "lib/thumbnailcache.h" #include "lib/strnatcmp.h" #include #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 #endif #ifdef GDK_WINDOWING_X11 #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 defined(__clang__) || defined(__GNUC__) #define UNUSED_FUNCTION __attribute__((unused)) #if defined(__clang__) #define PQIV_DISABLE_PEDANTIC _Pragma("clang diagnostic push") _Pragma("clang diagnostic ignored \"-Wpedantic\"") #define PQIV_ENABLE_PEDANTIC _Pragma("clang diagnostic pop") #elif defined(__GNUC__) || defined(__GNUG__) #define PQIV_DISABLE_PEDANTIC _Pragma("GCC diagnostic push") _Pragma("GCC diagnostic ignored \"-Wpedantic\"") #define PQIV_ENABLE_PEDANTIC _Pragma("GCC diagnostic pop") #endif #else #define UNUSED_FUNCTION #define PQIV_DISABLE_PEDANTIC #define PQIV_ENABLE_PEDANTIC #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 #include #endif // }}} // Global variables and function signatures {{{ // The list of file type handlers and file type initializer function void initialize_file_type_handlers(const gchar * const * disabled_backends); // 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: // * 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: #if 0 #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); #else #define D_LOCK(x) G_LOCK(x) #define D_UNLOCK(x) G_UNLOCK(x) #endif BOSTree *file_tree; BOSNode *current_file_node = NULL; BOSNode *earlier_file_node = NULL; BOSNode *image_loader_thread_currently_loading = NULL; gboolean file_tree_valid = FALSE; // We asynchroniously load images in a separate thread struct image_loader_queue_item { BOSNode *node_ref; int purpose; }; 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) #define is_current_file_loaded() (current_file_node && CURRENT_FILE->is_loaded) // 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; // Detection of tiled WMs: They should ignore our resize events gint requested_main_window_resize_pos_callback_id = -1; gint requested_main_window_width = -1; gint requested_main_window_height = -1; gboolean wm_ignores_size_requests = FALSE; gint main_window_width = 10; gint main_window_height = 10; gboolean main_window_in_fullscreen = FALSE; int fullscreen_transition_source_id = -1; GdkRectangle screen_geometry = { 0, 0, 0, 0 }; gint screen_scale_factor = 1; 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; // If a WM indicates no framedrawn support it's subject to tearing effects gboolean wm_supports_framedrawn = TRUE; cairo_pattern_t *background_checkerboard_pattern = NULL; gboolean gui_initialized = FALSE; int global_argc; char **global_argv; // Those 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. int last_visible_surface_width = -1; int last_visible_surface_height = -1; cairo_surface_t *last_visible_surface = NULL; cairo_surface_t *fading_surface = NULL; cairo_surface_t *current_scaled_image_surface = NULL; #if !defined(CONFIGURED_WITHOUT_INFO_TEXT) || !defined(CONFIGURED_WITHOUT_MONTAGE_MODE) struct { double fg_red; double fg_green; double fg_blue; double bg_red; double bg_green; double bg_blue; } option_box_colors = { 0., 0., 0., 1., 1., 0. }; #endif #ifndef CONFIGURED_WITHOUT_INFO_TEXT gint current_info_text_cached_font_size = -1; 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; guint32 last_button_release_time = 0; guint current_image_animation_timeout_id = 0; gdouble current_image_animation_speed_scale = 1.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 // Scaling mode, only 0-2 are available in the default UI, FIXED_SCALE can be // set using a command line option, SCALE_TO_FIT_PX and SCALE_TO_FIT_WINDOW only using an action enum { NO_SCALING=0, AUTO_SCALEDOWN, AUTO_SCALEUP, FIXED_SCALE, SCALE_TO_FIT_WINDOW, SCALE_TO_FIT_PX } option_scale = AUTO_SCALEDOWN; double option_scale_screen_fraction = .8; 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_transparent_background = FALSE; gboolean option_watch_directories = FALSE; gboolean option_wait_for_images_to_appear = FALSE; gboolean option_fading = FALSE; gboolean option_lazy_load = FALSE; gboolean option_allow_empty_window = FALSE; gboolean option_lowmem = FALSE; gboolean option_addl_from_stdin = FALSE; gboolean option_recreate_window = FALSE; gboolean option_enforce_window_aspect_ratio = FALSE; gboolean cursor_visible = TRUE; gboolean cursor_auto_hide_mode_enabled = FALSE; gboolean option_negate = FALSE; int cursor_auto_hide_timer_id = 0; #ifndef CONFIGURED_WITHOUT_ACTIONS gboolean option_actions_from_stdin = FALSE; gboolean option_status_output = FALSE; #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE gboolean option_auto_montage_mode = FALSE; #endif #else static const gboolean option_actions_from_stdin = FALSE; #endif double option_fading_duration = .5; double option_keyboard_timeout = .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; gchar *option_disable_backends; double fading_current_alpha_stage = 0; gint64 fading_initial_time; #ifdef CONFIGURED_WITHOUT_ACTIONS const #endif enum { AUTO, FAST, GOOD, BEST } option_interpolation_quality = AUTO; enum { CHECKERBOARD, BLACK, WHITE } option_background_pattern = CHECKERBOARD; gboolean options_background_pattern_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); char *key_binding_sequence_to_string(guint key_binding_value, gchar *prefix); gboolean help_show_key_bindings(const gchar *option_name, const gchar *value, gpointer data, GError **error); #endif gboolean help_show_version(const gchar *option_name, const gchar *value, gpointer data, GError **error); gboolean option_window_position_callback(const gchar *option_name, const gchar *value, gpointer data, GError **error); gboolean option_thumbnail_size_callback(const gchar *option_name, const gchar *value, gpointer data, GError **error); gboolean option_thumbnail_preload_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_thumbnail_persistence_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); #if !defined(CONFIGURED_WITHOUT_INFO_TEXT) || !defined(CONFIGURED_WITHOUT_MONTAGE_MODE) gboolean option_box_colors_callback(const gchar *option_name, const gchar *value, gpointer data, GError **error); #endif void load_images_handle_parameter(char *param, load_images_state_t state, gint depth, GSList *recursion_folder_stack); struct { gint x; gint y; } option_window_position = { -2, -2 }; #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE /* option --without-montage: Do not include support for a thumbnail overview */ struct { gboolean enabled; thumbnail_persist_mode_t persist; gchar *special_thumbnail_directory; gint width; gint height; gint auto_generate_for_adjacents; } option_thumbnails = { 0, THUMBNAILS_PERSIST_RO, NULL, 128, 128, -1 }; struct { int scroll_y; BOSNode *selected_node; gboolean show_binding_overlays; } montage_window_control; enum { MONTAGE_MODE_WRAP_OFF, MONTAGE_MODE_WRAP_ROWS, MONTAGE_MODE_WRAP_FULL, _MONTAGE_MODE_WRAP_SENTINEL } option_montage_mode_wrap_mode = MONTAGE_MODE_WRAP_ROWS; #endif struct Point { int x; int y; }; // The standard forbids casting object pointers to function pointers, but // GLib requires it in its GOptionEntry structure. PQIV_DISABLE_PEDANTIC // 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[] = { { "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 one image is loaded", NULL }, { "sort", 'n', 0, G_OPTION_ARG_NONE, &option_sort, "Sort files in natural order", NULL }, { "window-position", 'P', 0, G_OPTION_ARG_CALLBACK, &option_window_position_callback, "Set initial window position (`x,y' or `off' to not position the window at all)", "POSITION" }, { "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, &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, "Perform a given action", "ACTION" }, { "actions-from-stdin", 0, 0, G_OPTION_ARG_NONE, &option_actions_from_stdin, "Read actions from stdin", NULL }, { "allow-empty-window", 0, 0, G_OPTION_ARG_NONE, &option_allow_empty_window, "Show pqiv/do not quit even though no files are loaded", NULL }, #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE { "auto-montage-mode", 0, 0, G_OPTION_ARG_NONE, &option_auto_montage_mode, "Automatically enter montage mode if multiple images are opened", NULL }, #endif #endif { "background-pattern", 0, 0, G_OPTION_ARG_CALLBACK, &options_background_pattern_callback, "Set the background pattern to use for transparent images", "PATTERN" }, #ifndef CONFIGURED_WITHOUT_ACTIONS { "bind-key", 0, 0, G_OPTION_ARG_CALLBACK, &options_bind_key_callback, "Rebind a key to another action, see manpage and --show-bindings output for details.", "KEY BINDING" }, #endif #if !defined(CONFIGURED_WITHOUT_INFO_TEXT) || !defined(CONFIGURED_WITHOUT_MONTAGE_MODE) { "box-colors", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer)&option_box_colors_callback, "Set box colors", "TEXT:BACKGROUND" }, #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-backends", 0, 0, G_OPTION_ARG_STRING, &option_disable_backends, "Disable the given backends", "BACKENDS" }, { "disable-scaling", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, &option_scale_level_callback, "Disable scaling of images", NULL }, { "end-of-files-action", 0, 0, G_OPTION_ARG_CALLBACK, &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" }, { "negate", 0, 0, G_OPTION_ARG_NONE, &option_negate, "Negate images: show negatives", NULL }, { "recreate-window", 0, 0, G_OPTION_ARG_NONE, &option_recreate_window, "Create a new window instead of resizing the old one", NULL }, { "scale-mode-screen-fraction", 0, 0, G_OPTION_ARG_DOUBLE, &option_scale_screen_fraction, "Screen fraction to use for auto-scaling", "FLOAT" }, { "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, &option_sort_key_callback, "Key to use for sorting", "PROPERTY" }, #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE { "thumbnail-size", 0, 0, G_OPTION_ARG_CALLBACK, &option_thumbnail_size_callback, "Set the dimensions of thumbnails in montage mode", "WIDTHxHEIGHT" }, { "thumbnail-preload", 0, 0, G_OPTION_ARG_CALLBACK, &option_thumbnail_preload_callback, "Preload the adjacent COUNT thumbnails", "COUNT" }, { "thumbnail-persistence", 0, 0, G_OPTION_ARG_CALLBACK, &option_thumbnail_persistence_callback, "Persist thumbnails to disk, to DIRECTORY.", "DIRECTORY" }, #endif { "wait-for-images-to-appear", 0, 0, G_OPTION_ARG_NONE, &option_wait_for_images_to_appear, "If no images are found, wait until at least one appears", NULL }, { "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, &option_watch_files_callback, "Watch files for changes on disk (`on`, `off', `changes-only', i.e. do nothing on deletetion)", "VALUE" }, { "version", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, &help_show_version, "Show version information and quit", NULL }, { NULL, 0, 0, 0, NULL, NULL, NULL } }; PQIV_ENABLE_PEDANTIC /* Key bindings & actions {{{ */ #define KEY_BINDINGS_KEY_TOKEN_BEGIN_SYMBOL '<' #define KEY_BINDINGS_KEY_TOKEN_END_SYMBOL '>' #define KEY_BINDINGS_COMMANDS_BEGIN_SYMBOL '{' #define KEY_BINDINGS_COMMANDS_END_SYMBOL '}' #define KEY_BINDINGS_COMMAND_SEPARATOR_SYMBOL ';' #define KEY_BINDINGS_COMMAND_PARAMETER_BEGIN_SYMBOL '(' #define KEY_BINDINGS_COMMAND_PARAMETER_END_SYMBOL ')' #define KEY_BINDINGS_CONTEXT_SWITCH_SYMBOL '@' #define KEY_BINDING_STATE_BITS 4 #define KEY_BINDING_VALUE(is_mouse, state, keycode) ((guint)((((unsigned)is_mouse & 1u) << 31) | (((state & ((1u << KEY_BINDING_STATE_BITS) - 1)) << (31 - KEY_BINDING_STATE_BITS)) | (keycode & ((1u << (31 - KEY_BINDING_STATE_BITS)) - 1u))))) #define KEY_BINDING_CONTEXTS_COUNT 2 #ifndef CONFIGURED_WITHOUT_ACTIONS const char * const key_binding_context_names[] = { "DEFAULT", #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE "MONTAGE", #else "", #endif }; #endif enum context_t { DEFAULT, MONTAGE }; #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE static char montage_mode_default_keys[] = "asdfghjkl"; #endif static const struct default_key_bindings_struct { enum context_t context; guint key_binding_value; pqiv_action_t action; pqiv_action_parameter_t parameter; } default_key_bindings[] = { { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_Up ), ACTION_SHIFT_Y , { 10 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_KP_Up ), ACTION_SHIFT_Y , { 10 }}, { DEFAULT, KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_Up ), ACTION_SHIFT_Y , { 50 }}, { DEFAULT, KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_KP_Up ), ACTION_SHIFT_Y , { 50 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_Down ), ACTION_SHIFT_Y , { -10 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_KP_Down ), ACTION_SHIFT_Y , { -10 }}, { DEFAULT, KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_Down ), ACTION_SHIFT_Y , { -50 }}, { DEFAULT, KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_KP_Down ), ACTION_SHIFT_Y , { -50 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_Left ), ACTION_SHIFT_X , { 10 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_KP_Left ), ACTION_SHIFT_X , { 10 }}, { DEFAULT, KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_Left ), ACTION_SHIFT_X , { 50 }}, { DEFAULT, KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_KP_Left ), ACTION_SHIFT_X , { 50 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_Right ), ACTION_SHIFT_X , { -10 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_KP_Right ), ACTION_SHIFT_X , { -10 }}, { DEFAULT, KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_Right ), ACTION_SHIFT_X , { -50 }}, { DEFAULT, KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_KP_Right ), ACTION_SHIFT_X , { -50 }}, { DEFAULT, KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_plus ), ACTION_SET_SLIDESHOW_INTERVAL_RELATIVE , { .pdouble = 1. }}, { DEFAULT, KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_KP_Add ), ACTION_SET_SLIDESHOW_INTERVAL_RELATIVE , { .pdouble = 1. }}, { DEFAULT, KEY_BINDING_VALUE(0 , GDK_MOD1_MASK , GDK_KEY_KP_Add ), ACTION_ANIMATION_SET_SPEED_RELATIVE , { .pdouble = 1.1 }}, { DEFAULT, KEY_BINDING_VALUE(0 , GDK_MOD1_MASK , GDK_KEY_plus ), ACTION_ANIMATION_SET_SPEED_RELATIVE , { .pdouble = 1.1 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_plus ), ACTION_SET_SCALE_LEVEL_RELATIVE , { .pdouble = 1.1 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_KP_Add ), ACTION_SET_SCALE_LEVEL_RELATIVE , { .pdouble = 1.1 }}, { DEFAULT, KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_minus ), ACTION_SET_SLIDESHOW_INTERVAL_RELATIVE , { .pdouble = -1. }}, { DEFAULT, KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_KP_Subtract ), ACTION_SET_SLIDESHOW_INTERVAL_RELATIVE , { .pdouble = -1. }}, { DEFAULT, KEY_BINDING_VALUE(0 , GDK_MOD1_MASK , GDK_KEY_minus ), ACTION_ANIMATION_SET_SPEED_RELATIVE , { .pdouble = 0.9 }}, { DEFAULT, KEY_BINDING_VALUE(0 , GDK_MOD1_MASK , GDK_KEY_KP_Subtract ), ACTION_ANIMATION_SET_SPEED_RELATIVE , { .pdouble = 0.9 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_minus ), ACTION_SET_SCALE_LEVEL_RELATIVE , { .pdouble = 0.9 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_KP_Subtract ), ACTION_SET_SCALE_LEVEL_RELATIVE , { .pdouble = 0.9 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_t ), ACTION_TOGGLE_SCALE_MODE , { 0 }}, { DEFAULT, KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_t ), ACTION_TOGGLE_SCALE_MODE , { 4 }}, { DEFAULT, KEY_BINDING_VALUE(0 , GDK_MOD1_MASK , GDK_KEY_t ), ACTION_TOGGLE_SCALE_MODE , { 5 }}, { DEFAULT, KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_r ), ACTION_TOGGLE_SHUFFLE_MODE , { 0 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_r ), ACTION_RELOAD , { 0 }}, { DEFAULT, KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_p ), ACTION_GOTO_EARLIER_FILE , { 0 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_0 ), ACTION_RESET_SCALE_LEVEL , { 0 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_f ), ACTION_TOGGLE_FULLSCREEN , { 0 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_h ), ACTION_FLIP_HORIZONTALLY , { 0 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_v ), ACTION_FLIP_VERTICALLY , { 0 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_l ), ACTION_ROTATE_LEFT , { 0 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_k ), ACTION_ROTATE_RIGHT , { 0 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_i ), ACTION_TOGGLE_INFO_BOX , { 0 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_j ), ACTION_JUMP_DIALOG , { 0 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_m ), ACTION_MONTAGE_MODE_ENTER , { 0 }}, #ifndef CONFIGURED_WITHOUT_EXTERNAL_COMMANDS { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_o ), ACTION_TOGGLE_MARK , { 0 }}, { DEFAULT, KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_o ), ACTION_CLEAR_MARKS , { 0 }}, #endif { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_s ), ACTION_TOGGLE_SLIDESHOW , { 0 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_b ), ACTION_TOGGLE_BACKGROUND_PATTERN , { 0 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_n ), ACTION_TOGGLE_NEGATE_MODE , { 0 }}, { DEFAULT, KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_a ), ACTION_HARDLINK_CURRENT_IMAGE , { 0 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_period ), ACTION_ANIMATION_STEP , { 1 }}, { DEFAULT, KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_period ), ACTION_ANIMATION_CONTINUE , { 0 }}, { DEFAULT, KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_BackSpace ), ACTION_GOTO_LOGICAL_DIRECTORY_RELATIVE , { -1 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_BackSpace ), ACTION_GOTO_FILE_RELATIVE , { -1 }}, { DEFAULT, KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_space ), ACTION_GOTO_LOGICAL_DIRECTORY_RELATIVE , { 1 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_space ), ACTION_GOTO_FILE_RELATIVE , { 1 }}, { DEFAULT, KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_Page_Up ), ACTION_GOTO_FILE_RELATIVE , { 10 }}, { DEFAULT, KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_KP_Page_Up ), ACTION_GOTO_FILE_RELATIVE , { 10 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_Page_Down ), ACTION_GOTO_FILE_RELATIVE , { -10 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_Page_Up ), ACTION_GOTO_FILE_RELATIVE , { 10 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_KP_Page_Up ), ACTION_GOTO_FILE_RELATIVE , { 10 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_KP_Page_Down ), ACTION_GOTO_FILE_RELATIVE , { -10 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_q ), ACTION_QUIT , { 0 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_Escape ), ACTION_QUIT , { 0 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_1 ), ACTION_NUMERIC_COMMAND , { 1 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_2 ), ACTION_NUMERIC_COMMAND , { 2 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_3 ), ACTION_NUMERIC_COMMAND , { 3 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_4 ), ACTION_NUMERIC_COMMAND , { 4 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_5 ), ACTION_NUMERIC_COMMAND , { 5 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_6 ), ACTION_NUMERIC_COMMAND , { 6 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_7 ), ACTION_NUMERIC_COMMAND , { 7 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_8 ), ACTION_NUMERIC_COMMAND , { 8 }}, { DEFAULT, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_9 ), ACTION_NUMERIC_COMMAND , { 9 }}, { DEFAULT, KEY_BINDING_VALUE(1 , 0 , GDK_BUTTON_PRIMARY ), ACTION_GOTO_FILE_RELATIVE , { -1 }}, { DEFAULT, KEY_BINDING_VALUE(1 , 0 , GDK_BUTTON_MIDDLE ), ACTION_QUIT , { 0 }}, { DEFAULT, KEY_BINDING_VALUE(1 , 0 , GDK_BUTTON_SECONDARY ), ACTION_GOTO_FILE_RELATIVE , { 1 }}, { DEFAULT, KEY_BINDING_VALUE(1 , 0 , (GDK_SCROLL_UP+1) << 2 ), ACTION_GOTO_FILE_RELATIVE , { 1 }}, { DEFAULT, KEY_BINDING_VALUE(1 , 0 , (GDK_SCROLL_DOWN+1) << 2 ), ACTION_GOTO_FILE_RELATIVE , { -1 }}, #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE { MONTAGE, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_Down ), ACTION_MONTAGE_MODE_SHIFT_Y , { 1 }}, { MONTAGE, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_Up ), ACTION_MONTAGE_MODE_SHIFT_Y , { -1 }}, { MONTAGE, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_Left ), ACTION_MONTAGE_MODE_SHIFT_X , { -1 }}, { MONTAGE, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_Right ), ACTION_MONTAGE_MODE_SHIFT_X , { 1 }}, { MONTAGE, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_KP_Down ), ACTION_MONTAGE_MODE_SHIFT_Y , { 1 }}, { MONTAGE, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_KP_Up ), ACTION_MONTAGE_MODE_SHIFT_Y , { -1 }}, { MONTAGE, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_KP_Left ), ACTION_MONTAGE_MODE_SHIFT_X , { -1 }}, { MONTAGE, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_KP_Right ), ACTION_MONTAGE_MODE_SHIFT_X , { 1 }}, { MONTAGE, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_Page_Down ), ACTION_MONTAGE_MODE_SHIFT_Y_PG , { 1 }}, { MONTAGE, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_Page_Up ), ACTION_MONTAGE_MODE_SHIFT_Y_PG , { -1 }}, { MONTAGE, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_KP_Page_Up ), ACTION_MONTAGE_MODE_SHIFT_Y_PG , { -1 }}, { MONTAGE, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_KP_Page_Down ), ACTION_MONTAGE_MODE_SHIFT_Y_PG , { 1 }}, { MONTAGE, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_Home ), ACTION_GOTO_FILE_BYINDEX , { 0 }}, { MONTAGE, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_End ), ACTION_GOTO_FILE_BYINDEX , { -1 }}, { MONTAGE, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_Return ), ACTION_MONTAGE_MODE_RETURN_PROCEED , { 0 }}, { MONTAGE, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_Escape ), ACTION_MONTAGE_MODE_RETURN_CANCEL , { 0 }}, #ifndef CONFIGURED_WITHOUT_EXTERNAL_COMMANDS { MONTAGE, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_o ), ACTION_TOGGLE_MARK , { 0 }}, { MONTAGE, KEY_BINDING_VALUE(0 , GDK_CONTROL_MASK , GDK_KEY_o ), ACTION_CLEAR_MARKS , { 0 }}, #endif { MONTAGE, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_m ), ACTION_MONTAGE_MODE_RETURN_CANCEL , { 0 }}, { MONTAGE, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_f ), ACTION_TOGGLE_FULLSCREEN , { 0 }}, { MONTAGE, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_g ), ACTION_MONTAGE_MODE_FOLLOW , { .pcharptr = (char*)montage_mode_default_keys }}, { MONTAGE, KEY_BINDING_VALUE(0 , 0 , GDK_KEY_q ), ACTION_QUIT , { 0 }}, { MONTAGE, KEY_BINDING_VALUE(1 , 0 , (GDK_SCROLL_UP+1) << 2 ), ACTION_MONTAGE_MODE_SHIFT_Y_ROWS , { 1 }}, { MONTAGE, KEY_BINDING_VALUE(1 , 0 , (GDK_SCROLL_DOWN+1) << 2 ), ACTION_MONTAGE_MODE_SHIFT_Y_ROWS , { -1 }}, #endif { DEFAULT, 0, 0, { 0 } } }; enum context_t active_key_binding_context = DEFAULT; enum context_t application_mode = DEFAULT; #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[KEY_BINDING_CONTEXTS_COUNT]; struct { key_binding_t *key_binding; BOSNode *associated_image; gint timeout_id; } active_key_binding = { NULL, NULL, -1 }; GQueue action_queue = G_QUEUE_INIT; gint action_queue_idle_id = -1; void help_show_single_action(key_binding_t *current_action); #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE key_binding_t follow_mode_key_binding = { ACTION_MONTAGE_MODE_FOLLOW_PROCEED, { .p2short = { -1, -1 } }, NULL, NULL }; #endif #endif void UNUSED_FUNCTION action_done(); 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 }, { "set_scale_mode_screen_fraction", PARAMETER_DOUBLE }, { "toggle_shuffle_mode", PARAMETER_INT }, { "reload", PARAMETER_NONE }, { "reset_scale_level", PARAMETER_NONE }, { "toggle_fullscreen", PARAMETER_INT }, { "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_logical_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 }, { "set_interpolation_quality", PARAMETER_INT }, { "animation_step", PARAMETER_INT }, { "animation_continue", PARAMETER_NONE }, { "animation_set_speed_absolute", PARAMETER_DOUBLE }, { "animation_set_speed_relative", PARAMETER_DOUBLE }, { "goto_earlier_file", PARAMETER_NONE }, { "set_cursor_auto_hide", PARAMETER_INT }, { "set_fade_duration", PARAMETER_DOUBLE }, { "set_keyboard_timeout", PARAMETER_DOUBLE }, { "set_thumbnail_size", PARAMETER_2SHORT }, { "set_thumbnail_preload", PARAMETER_INT }, { "montage_mode_enter", PARAMETER_NONE }, { "montage_mode_shift_x", PARAMETER_INT }, { "montage_mode_shift_y", PARAMETER_INT }, { "montage_mode_set_shift_x", PARAMETER_INT }, { "montage_mode_set_shift_y", PARAMETER_INT }, { "montage_mode_set_wrap_mode", PARAMETER_INT }, { "montage_mode_shift_y_pg", PARAMETER_INT }, { "montage_mode_shift_y_rows", PARAMETER_INT }, { "montage_mode_show_binding_overlays", PARAMETER_INT }, { "montage_mode_follow", PARAMETER_CHARPTR }, { "montage_mode_follow_proceed", PARAMETER_2SHORT }, { "montage_mode_return_proceed", PARAMETER_NONE }, { "montage_mode_return_cancel", PARAMETER_NONE }, { "move_window", PARAMETER_2SHORT }, { "toggle_background_pattern", PARAMETER_INT }, { "toggle_negate_mode", PARAMETER_INT }, { "toggle_mark", PARAMETER_NONE }, { "clear_marks", PARAMETER_NONE }, { NULL, 0 } }; /* }}} */ typedef struct { gint depth; GTree *outstanding_files; GSList *recursion_folder_stack; gchar *base_param; } directory_watch_options_t; GHashTable *active_directory_watches; 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); typedef int image_loader_purpose_t; gboolean test_and_invalidate_thumbnail(file_t *file); gboolean image_loader_load_single(BOSNode *node, gboolean called_from_main); gboolean fading_timeout_callback(gpointer user_data); void queue_image_load(BOSNode *); #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE void queue_thumbnail_load(BOSNode *); #endif 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 preload_adjacent_images(); void window_center_mouse(); double calculate_scale_level_to_fit(int image_width, int image_height, int window_width, int window_height); gboolean main_window_calculate_ideal_size(int *new_window_width, int *new_window_height); void calculate_current_image_transformed_size(int *image_width, int *image_height); double calculate_auto_scale_level_for_screen(int image_width, int image_height); cairo_surface_t *get_scaled_image_surface_for_current_image(); gboolean window_state_into_fullscreen_actions(gpointer user_data); gboolean window_state_out_of_fullscreen_actions(gpointer user_data); gboolean window_draw_callback(GtkWidget *widget, cairo_t *cr_arg, gpointer user_data); void window_prerender_background_pixmap(int window_width, int window_height, double scale_level, gboolean fullscreen); void window_clear_background_pixmap(); gboolean window_show_background_pixmap_cb(gpointer user_data); 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); gboolean absolute_image_movement(BOSNode *ref); #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); void draw_current_image_to_context(cairo_t *cr); gboolean window_auto_hide_cursor_callback(gpointer user_data); #ifndef CONFIGURED_WITHOUT_ACTIONS gboolean handle_input_event_timeout_callback(gpointer user_data); void queue_action(pqiv_action_t action_id, pqiv_action_parameter_t parameter); #endif #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE gboolean montage_window_get_move_cursor_target(int, int, int, int*, int*, int*, BOSNode **); void montage_window_move_cursor(int, int, int); #endif #ifndef CONFIGURED_WITHOUT_EXTERNAL_COMMANDS void toggle_mark(); void clear_marks(); GString *get_all_marked(); #endif // }}} /* Helper functions {{{ */ gboolean strv_contains(const gchar * const *strv, const gchar *str) { #if GLIB_CHECK_VERSION(2, 44, 0) return g_strv_contains(strv, str); #else while(*strv) { if(g_strcmp0(*strv, str) == 0) { return TRUE; } strv++; } return FALSE; #endif } /* }}} */ /* Command line handling, creation of the image list {{{ */ gboolean options_background_pattern_callback(const gchar *option_name, const gchar *value, gpointer data, GError **error) {/*{{{*/ if(strcasecmp(value, "checkerboard") == 0) { option_background_pattern = CHECKERBOARD; } else if(strcasecmp(value, "black") == 0) { option_background_pattern = BLACK; } else if(strcasecmp(value, "white") == 0) { option_background_pattern = WHITE; } else { g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, "Unexpected argument value for the --background-pattern option. Allowed values are BLACK, WHITE and CHECKERBOARD."); return FALSE; } 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 #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE gboolean option_thumbnail_size_callback(const gchar *option_name, const gchar *value, gpointer data, GError **error) {/*{{{*/ gchar *second; option_thumbnails.width = g_ascii_strtoll(value, &second, 10); if(second != value && (*second == 'x' || *second == ',')) { option_thumbnails.height = 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 --thumbnail-size option. Format must be e.g. `320x240'."); return FALSE; }/*}}}*/ gboolean option_thumbnail_preload_callback(const gchar *option_name, const gchar *value, gpointer data, GError **error) {/*{{{*/ option_thumbnails.enabled = 1; option_thumbnails.auto_generate_for_adjacents = g_ascii_strtoll(value, NULL, 10); if(errno == EINVAL || errno == ERANGE) { g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, "Unexpected argument value for the --thumbnail-preload option."); return FALSE; } return TRUE; }/*}}}*/ gboolean option_thumbnail_persistence_callback(const gchar *option_name, const gchar *value, gpointer data, GError **error) {/*{{{*/ if(option_thumbnails.special_thumbnail_directory != NULL) { g_free(option_thumbnails.special_thumbnail_directory); option_thumbnails.special_thumbnail_directory = NULL; } if(value == NULL || !*value || strcasecmp(value, "yes") == 0 || strcasecmp(value, "true") == 0 || strcasecmp(value, "1") == 0 || strcasecmp(value, "on") == 0) { option_thumbnails.persist = THUMBNAILS_PERSIST_ON; } else if(strcasecmp(value, "read-only") == 0) { option_thumbnails.persist = THUMBNAILS_PERSIST_RO; } else if(strcasecmp(value, "standard") == 0) { option_thumbnails.persist = THUMBNAILS_PERSIST_STANDARD; } else if(strcasecmp(value, "no") == 0 || strcasecmp(value, "false") == 0 || strcasecmp(value, "1") == 0 || strcasecmp(value, "off") == 0) { option_thumbnails.persist = THUMBNAILS_PERSIST_OFF; } else if(strcasecmp(value, "local") == 0) { option_thumbnails.persist = THUMBNAILS_PERSIST_LOCAL; } else if(value[0] == '/') { option_thumbnails.persist = THUMBNAILS_PERSIST_ON; option_thumbnails.special_thumbnail_directory = g_strdup(value); } else { g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, "Unexpected argument value for the --thumbnail-persistence option."); return FALSE; } 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; }/*}}}*/ #if !defined(CONFIGURED_WITHOUT_INFO_TEXT) || !defined(CONFIGURED_WITHOUT_MONTAGE_MODE) gboolean option_box_colors_callback(const gchar *option_name, const gchar *value, gpointer data, GError **error) {/*{{{*/ // Parse manually rather than with sscanf to have the flexibility to use // hex notation without doing black magic unsigned char pos; for(pos=0; pos < 6 && value && *value; pos++) { while(*value == ' ') { value++; } if(pos % 3 == 0 && *value == '#') { value++; unsigned char ipos = pos + 3; for(; pos= 'a' && *value <= 'f') { mchar |= (*value - 'a') + 10; } else if(*value >= 'A' && *value <= 'F') { mchar |= (*value - 'A') + 10; } else if(*value >= '0' && *value <= '9') { mchar |= *value - '0'; } else { value = NULL; break; } value++; } *((double *)&option_box_colors + pos) = mchar / 255.; } pos--; } else { unsigned ivalue = 0; while(*value && *value >= '0' && *value <= '9') { ivalue = ivalue * 10 + (*value - '0'); value++; } *((double *)&option_box_colors + pos) = ivalue / 255.; if(pos != 2) { while(*value == ' ') { value++; } if(*value == ',') { value++; } else { value = NULL; } } } if(pos == 2) { while(*value == ' ') { value++; } if(*value == ':') { value++; } else { value = NULL; } } } if(pos != 6) { g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, "Unexpected argument value for the --box-colors option. Syntax is foreground-color:background-color, colors either given as a r,g,b pair or #aabbcc hex code."); return FALSE; } return TRUE; }/*}}}*/ #endif 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; iter->description = g_strdup_printf("[Set to do not/disable:] %s", iter->description); 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; iter->description = g_strdup_printf("[Set to do not/disable:] %s", iter->description); } 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(!section) { // Key/value entries in a non-section. g_printerr("Failed to handle non-section entry `%s' in configuration file.\n", key); } 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; iter->description = g_strdup_printf("[Set to do not/disable:] %s", iter->description); } } 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); PQIV_DISABLE_PEDANTIC ((GOptionArgFunc)(iter->arg_data))(long_name, value->chrpval, NULL, &error_pointer); PQIV_ENABLE_PEDANTIC } else { *(gchar **)(iter->arg_data) = g_strdup(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); } else if(strcmp(section, "actions") == 0 && !key) { config_parser_strip_comments(value->chrpval); gchar *actions = g_strdup(value->chrpval); gdk_threads_add_idle(read_commands_thread_helper, actions); } #endif } static void parse_configuration_file_nolocale(const gchar *config_file) { gchar *old_locale = g_strdup(setlocale(LC_NUMERIC, NULL)); setlocale(LC_NUMERIC, "C"); config_parser_parse_file(config_file, parse_configuration_file_callback); setlocale(LC_NUMERIC, old_locale); g_free(old_locale); return; } void parse_configuration_file() {/*{{{*/ // Check for a configuration file const gchar *env_config_file = g_getenv("PQIVRC_PATH"); if(env_config_file) { if(g_file_test(env_config_file, G_FILE_TEST_EXISTS)) { parse_configuration_file_nolocale(env_config_file); } return; } GQueue *test_dirs = g_queue_new(); const gchar *config_dir = g_getenv("XDG_CONFIG_HOME"); if(!config_dir) { g_queue_push_tail(test_dirs, g_build_filename(g_getenv("HOME"), ".config", "pqivrc", NULL)); } else { g_queue_push_tail(test_dirs, g_build_filename(config_dir, "pqivrc", NULL)); } g_queue_push_tail(test_dirs, g_build_filename(g_getenv("HOME"), ".pqivrc", NULL)); const gchar *system_config_dirs = g_getenv("XDG_CONFIG_DIRS"); if(system_config_dirs) { gchar **split_system_config_dirs = g_strsplit(system_config_dirs, ":", 0); for(gchar **system_dir = split_system_config_dirs; *system_dir; system_dir++) { g_queue_push_tail(test_dirs, g_build_filename(*system_dir, "pqivrc", NULL)); } g_strfreev(split_system_config_dirs); } g_queue_push_tail(test_dirs, g_build_filename(G_DIR_SEPARATOR_S "etc", "pqivrc", NULL)); gchar *config_file_name; while((config_file_name = g_queue_pop_head(test_dirs))) { if(g_file_test(config_file_name, G_FILE_TEST_EXISTS)) { parse_configuration_file_nolocale(config_file_name); g_free(config_file_name); break; } g_free(config_file_name); } while((config_file_name = g_queue_pop_head(test_dirs))) { g_free(config_file_name); } g_queue_free(test_dirs); }/*}}}*/ void parse_command_line() {/*{{{*/ GOptionContext *parser = g_option_context_new("FILES"); g_option_context_set_summary(parser, "Powerful quick 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 && !option_actions_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. Only handle creation. // Canonicalize the name of the object. GFileMonitor reports absolute file names, use relative paths // instead if the user did as well on the command line. gchar *name = NULL; if(event_type == G_FILE_MONITOR_EVENT_CREATED || event_type == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT) { name = g_file_get_path(file); if(options->recursion_folder_stack && options->base_param) { size_t original_path_length = strlen(options->recursion_folder_stack->data); if(original_path_length && original_path_length < strlen(name)) { gchar *new_name = g_strdup_printf("%s%s", options->base_param, name + original_path_length); g_free(name); name = new_name; } } } // Skip .sh_thumbnails directories if(name && strstr(name, G_DIR_SEPARATOR_S ".sh_thumbnails" G_DIR_SEPARATOR_S) != NULL) { g_free(name); return; } if(event_type == G_FILE_MONITOR_EVENT_CREATED && 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. // // One catch is that we should not handle files right away, because they might not // be completely written to disk at this point. // if(g_file_test(name, 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, options->recursion_folder_stack); } else if(g_file_test(name, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_SYMLINK)) { // Defer call to load_images_handle_parameter. // name now belongs to the hash table, do not free it here. // TODO Option for later: Insert time as value and regularly cleanup the hash. g_tree_replace(options->outstanding_files, name, NULL); name = NULL; } } else if(event_type == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT) { if(g_tree_remove(options->outstanding_files, name)) { // The file was in the "new files" hash. Add it now. load_images_handle_parameter(name, INOTIFY, options->depth, options->recursion_folder_stack); } } // 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. if(name != NULL) { g_free(name); } }/*}}}*/ 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; } if(bostree_node_count(file_tree) >= INT_MAX) { // This is a safegoard. Most image operations should actually have // ULONG_MAX as a limit, but sometimes, I cast to an integer type. g_printerr("Cannot add image %s: Maximum number of images reached.\n", file->display_name); 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(); #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE if(application_mode == MONTAGE) { D_LOCK(file_tree); montage_window_move_cursor(0, 0, 0); D_UNLOCK(file_tree); gtk_widget_queue_draw(GTK_WIDGET(main_window)); } #endif } 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); } }/*}}}*/ BOSNode *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) { return file_type_handler->alloc_fn(state, file); } else { return load_images_handle_parameter_add_file(state, file); } } file_type_handler++; } return NULL; }/*}}}*/ void gfree_with_dummy_arg(void *pointer, void *dummy) {/*{{{*/ g_free(pointer); }/*}}}*/ 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, NULL); g_free(param); if(main_window) { gtk_widget_queue_draw(GTK_WIDGET(main_window)); } return NULL; }/*}}}*/ int pqiv_utility_strcmp0_data(const void *data1, const void *data2, void *user_data) {/*{{{*/ return g_strcmp0(data1, data2); }/*}}}*/ void load_images_handle_parameter(char *param, load_images_state_t state, gint depth, GSList *recursion_folder_stack) {/*{{{*/ file_t *file; // If the file tree has been invalidated, cancel. if(!file_tree_valid) { 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("-"); g_mutex_init(&file->lock); 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; BOSNode *mime_guess_result = load_images_handle_parameter_find_handler(param, state, file, &mime_guesser); if(mime_guess_result == FALSE_POINTER) { return; } if(!mime_guess_result) { // 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, recursion_folder_stack); // 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. if(original_parameter != NULL) { g_free(param); } return; } // Check for loops // Note: PATH_MAX might be too small. However, GIO fails to work // with long file names as well. In fact, it even fails to work // with relative file names in directorys that happen to have a // long absolute name. See // https://bugzilla.gnome.org/show_bug.cgi?id=778798 // TODO As soon as this is resolved upstream, it'll make sense to // exchange the realpath() with something else as well. char abs_path[PATH_MAX]; if( #ifdef _WIN32 GetFullPathNameA(param, PATH_MAX, abs_path, NULL) != 0 #else realpath(param, abs_path) != NULL #endif ) { if(g_slist_find_custom(recursion_folder_stack, abs_path, (GCompareFunc)g_strcmp0)) { if(original_parameter != NULL) { g_free(param); } return; } recursion_folder_stack = g_slist_prepend(recursion_folder_stack, g_strdup(abs_path)); } 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; } if(strcmp(dir_entry, ".sh_thumbnails") == 0) { // Do not traverse into local thumbnail directories continue;; } 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, recursion_folder_stack); } g_free(dir_entry_full); // If the file tree has been invalidated, cancel. // Do not bother to free stuff, because pqiv will exit anyway. if(!file_tree_valid) { return; } } g_dir_close(dir_ptr); // Add a watch for new files in this directory if(option_watch_directories && !g_hash_table_lookup(active_directory_watches, abs_path)) { // 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->outstanding_files = g_tree_new_full(pqiv_utility_strcmp0_data, NULL, g_free, NULL); options->depth = depth; options->base_param = g_strdup(param); // Remove trailing '/', just for optics of filenames. for(char *iter = options->base_param; *iter; iter++) { if(!iter[1] && iter[0] == '/') { *iter = 0; break; } } options->recursion_folder_stack = recursion_folder_stack; g_signal_connect_data(directory_monitor, "changed", G_CALLBACK(load_images_directory_watch_callback), options, (GClosureNotify)gfree_with_dummy_arg, 0); g_hash_table_insert(active_directory_watches, g_strdup(abs_path), directory_monitor); } g_object_unref(file_ptr); } else { // If we do not use directory watches then there is no use in // maintaining the directory recursion stack. Remove the first // entry (current directory) again. g_free(recursion_folder_stack->data); recursion_folder_stack = g_slist_delete_link(recursion_folder_stack, recursion_folder_stack); // Since the net effect is that we didn't modify recursion_folder_stack, // it is fine that the result of recursion_folder_stack is lost (and not passed back // outside of this function) (void)recursion_folder_stack; } if(original_parameter != NULL) { g_free(param); } return; } // Prepare file structure file = g_slice_new0(file_t); file->file_name = g_strdup(param); file->display_name = g_filename_display_name(param); g_mutex_init(&file->lock); 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) { #if GLIB_CHECK_VERSION(2, 62, 0) GDateTime *result = g_file_info_get_modification_date_time(file_info); file->sort_name = g_strdup_printf("%zu;%s", g_date_time_to_unix(result), file->display_name); g_date_time_unref(result); #else GTimeVal result; g_file_info_get_modification_time(file_info, &result); file->sort_name = g_strdup_printf("%lu;%s", result.tv_sec, file->display_name); #endif g_object_unref(file_info); } g_object_unref(param_file); } } if(file->sort_name == NULL) { file->sort_name = g_strdup(file->display_name); } } // 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 BOSNode *new_node = load_images_handle_parameter_find_handler(param, state, file, load_images_file_filter_info); if(new_node && new_node != FALSE_POINTER) { #if !defined(CONFIGURED_WITHOUT_MONTAGE_MODE) && !defined(CONFIGURED_WITHOUT_ACTIONS) // Automatically enter montage mode if(option_auto_montage_mode && bostree_node_count(file_tree) == 2) { pqiv_action_parameter_t empty_param = { .pint = 0 }; queue_action(ACTION_MONTAGE_MODE_ENTER, empty_param); option_auto_montage_mode = FALSE; } #endif if(!current_file_node && main_window_visible) { current_file_node = bostree_node_weak_ref(new_node); g_idle_add((GSourceFunc)absolute_image_movement, bostree_node_weak_ref(new_node)); } g_free(param_lowerc); return; } g_free(param_lowerc); if(new_node == FALSE_POINTER) { return; } if(state != PARAMETER && state != BROWSE_ORIGINAL_PARAMETER) { // At this point, if the file was not mentioned explicitly by the user, // abort. file_free(file); return; } // Make a final attempt to guess the file type by mime type GFile *param_file = gfile_for_commandline_arg(param); if(!param_file) { file_free(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; new_node = load_images_handle_parameter_find_handler(param, state, file, &mime_guesser); if(new_node && new_node != FALSE_POINTER) { if(!current_file_node && main_window_visible) { current_file_node = bostree_node_weak_ref(new_node); g_idle_add((GSourceFunc)absolute_image_movement, bostree_node_weak_ref(new_node)); } 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); if(new_node == FALSE_POINTER) { return; } } } 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]; if(!file->file_type) { g_printerr("No default file handler available.\n"); file_free(file); return; } new_node = file_type_handlers[0].alloc_fn(state, file); if(!current_file_node && main_window_visible) { current_file_node = bostree_node_weak_ref(new_node); g_idle_add((GSourceFunc)absolute_image_movement, bostree_node_weak_ref(new_node)); } } }/*}}}*/ int image_tree_float_compare(const float *a, const float *b) {/*{{{*/ return *a > *b; }/*}}}*/ void file_free(file_t *file) {/*{{{*/ if(file->file_type && 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; } #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE if(file->thumbnail) { cairo_surface_destroy(file->thumbnail); file->thumbnail = NULL; } #endif g_mutex_clear(&file->lock); 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 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; // 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; // Initialize structure to hold directory watches if(option_watch_directories && active_directory_watches == NULL) { active_directory_watches = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_object_unref); } // 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, NULL); } } 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, NULL); g_free(buffer); } g_io_channel_unref(stdin_reader); } if(load_images_timer) { g_timer_destroy(load_images_timer); } }/*}}}*/ // }}} /* (A-)synchronous image loading and image operations {{{ */ #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE gboolean test_and_invalidate_thumbnail(file_t *file) {/*{{{*/ // Must be called with an active lock! if(file->thumbnail) { const int thumb_width = cairo_image_surface_get_width(file->thumbnail); const int thumb_height = cairo_image_surface_get_height(file->thumbnail); if(!((thumb_width == option_thumbnails.width && thumb_height <= option_thumbnails.height) || (thumb_width <= option_thumbnails.width && thumb_height == option_thumbnails.height) || (thumb_width == (int)file->width && thumb_height == (int)file->height))) { cairo_surface_destroy(file->thumbnail); file->thumbnail = NULL; } } return !!file->thumbnail; }/*}}}*/ #endif 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(!file_tree_valid || (BOSNode *)user_data != current_file_node || FILE(current_file_node)->force_reload || !FILE(current_file_node)->is_loaded) { 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; } g_mutex_lock(&CURRENT_FILE->lock); double delay = (1./current_image_animation_speed_scale) * CURRENT_FILE->file_type->animation_next_frame_fn(CURRENT_FILE); g_mutex_unlock(&CURRENT_FILE->lock); D_UNLOCK(file_tree); if(delay >= 0 && current_image_animation_speed_scale > 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(bostree_node_weak_ref(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 // // Single exception: With the --allow-empty-window option, it does make // sense to unload even the last image. if(option_watch_files == ON) { FILE(node)->force_reload = TRUE; if(bostree_node_count(file_tree) > 1 || option_allow_empty_window) { queue_image_load(bostree_node_weak_ref(node)); } } } D_UNLOCK(file_tree); }/*}}}*/ gboolean window_move_helper_callback(gpointer user_data) {/*{{{*/ gtk_window_move(main_window, option_window_position.x / screen_scale_factor, option_window_position.y / screen_scale_factor); option_window_position.x = -1; return FALSE; }/*}}}*/ gboolean main_window_resize_callback(gpointer user_data) {/*{{{*/ // Only used once at application startup, this is not a callback invoked // each time the window size changes! D_LOCK(file_tree); // If there is no image loaded, abort if(!is_current_file_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) { requested_main_window_width = new_window_width; requested_main_window_height = new_window_height; gtk_window_resize(main_window, new_window_width / screen_scale_factor, new_window_height / screen_scale_factor); } return FALSE; }/*}}}*/ gboolean main_window_calculate_ideal_size(int *new_window_width, int *new_window_height) {/*{{{*/ if(!current_file_node) { return FALSE; } // We only need to adjust the window if it is not in fullscreen if(main_window_in_fullscreen) { return FALSE; } if(application_mode == DEFAULT) { int image_width, image_height; calculate_current_image_transformed_size(&image_width, &image_height); *new_window_width = current_scale_level * image_width + 0.5; *new_window_height = current_scale_level * image_height + 0.5; } #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE else if(application_mode == MONTAGE) { const int screen_width = screen_geometry.width; const int screen_height = screen_geometry.height; *new_window_width = screen_width * option_scale_screen_fraction; *new_window_height = screen_height * option_scale_screen_fraction; } #endif else { *new_window_width = *new_window_height = 0; } if(*new_window_height <= 0) { *new_window_height = 1; } if(*new_window_width <= 0) { *new_window_width = 1; } return TRUE; }/*}}}*/ gboolean main_window_reset_pos_callback(gpointer user_data) {/*{{{*/ gtk_window_set_position(main_window, GTK_WIN_POS_NONE); requested_main_window_resize_pos_callback_id = -1; return FALSE; }/*}}}*/ void main_window_adjust_for_image() {/*{{{*/ if(!current_file_node) { return; } // We only need to adjust the window if it is not in fullscreen if(main_window_in_fullscreen) { queue_draw(); return; } // In SCALE_TO_FIT_WINDOW mode, do never resize if(option_scale == SCALE_TO_FIT_WINDOW) { queue_draw(); return; } int new_window_width, new_window_height; if(!main_window_calculate_ideal_size(&new_window_width, &new_window_height)) { return; } 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 || requested_main_window_width != -1)) { 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); // We need to reset the centering eventually to allow users to // resize pqiv without having the window jumping around. But we // cannot do that right after resizing, because resizing is // actually not done *right* know, only when GTK returns to idle. // Resetting on idle with very low priority does not work either, // because resizing might be further delayed due to other pending // configure events. This used to be done upon receiving the // configure event. But that does not work at least with i3, // unfortunately. See #92 in Github. Long story short, a resonable // timeout seems to be the best idea right now. if(requested_main_window_resize_pos_callback_id > -1) { g_source_remove(requested_main_window_resize_pos_callback_id); } requested_main_window_resize_pos_callback_id = g_timeout_add(500, main_window_reset_pos_callback, NULL); } if(!main_window_visible) { gtk_window_set_default_size(main_window, new_window_width / screen_scale_factor, new_window_height / screen_scale_factor); 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; requested_main_window_width = new_window_width; requested_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 { // Required to avoid tearing window_prerender_background_pixmap(new_window_width, new_window_height, current_scale_level, main_window_in_fullscreen); requested_main_window_width = new_window_width; requested_main_window_height = new_window_height; 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 / screen_scale_factor, new_window_height / screen_scale_factor); #if GTK_MAJOR_VERSION < 3 if(option_enforce_window_aspect_ratio) { int image_width, image_height; calculate_current_image_transformed_size(&image_width, &image_height); 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) {/*{{{*/ // Execute logic below only if the loaded image is the current one if(node != NULL && node != current_file_node) { #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE if(application_mode == MONTAGE) { gtk_widget_queue_draw(GTK_WIDGET(main_window)); } #endif return FALSE; } // Note: This might mean as well that the *thumbnail* has been loaded, // but not the image itself. So check ->is_loaded in any case! 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; } // Activate fading if(option_fading) { if(fading_surface) { cairo_surface_destroy(fading_surface); fading_surface = NULL; } if(last_visible_surface) { fading_surface = cairo_surface_reference(last_visible_surface); } if(fading_current_alpha_stage > 0 && fading_current_alpha_stage < 1.) { // If another fade was already active, don't start another one. fading_current_alpha_stage = DBL_EPSILON; fading_initial_time = -1; } else { // 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); } } // 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) { g_mutex_lock(&CURRENT_FILE->lock); 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); g_mutex_unlock(&CURRENT_FILE->lock); current_image_animation_speed_scale = 1.0; } // 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, // but only if not currently in the process of changing state if(fullscreen_transition_source_id < 0) { if(!current_image_drawn) { scale_override = FALSE; } invalidate_current_scaled_image_surface(); set_scale_level_for_screen(); main_window_adjust_for_image(); } 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 GBytes *bytes_source; if(file->file_data_loader) { bytes_source = file->file_data_loader(file, error_pointer); if(!bytes_source) { return NULL; } } else { bytes_source = g_bytes_ref(file->file_data); } #if GLIB_CHECK_VERSION(2, 34, 0) data = g_memory_input_stream_new_from_bytes(bytes_source); #else gsize size = 0; gpointer *mem_data = g_memdup(g_bytes_get_data(bytes_source, &size), size); data = g_memory_input_stream_new_from_data(mem_data, size, g_free); #endif g_bytes_unref(bytes_source); } 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_file_name, gchar *custom_display_name, gchar *custom_sort_name) {/*{{{*/ file_t *new_file = g_slice_new(file_t); *new_file = *file; if(file->file_data) { g_bytes_ref(new_file->file_data); } new_file->file_name = custom_file_name ? custom_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); new_file->private = NULL; new_file->file_monitor = NULL; new_file->is_loaded = FALSE; g_mutex_init(&new_file->lock); 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 g_mutex_lock(&file->lock); file->file_type->load_fn(file, data, &error_pointer); g_mutex_unlock(&file->lock); g_object_unref(data); } } if(file->is_loaded) { if(error_pointer) { g_printerr("A recoverable error occurred: %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) { g_mutex_lock(&file->lock); 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_mutex_unlock(&file->lock); 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(bostree_node_weak_ref(current_file_node)); } else { current_file_node = NULL; } } else { current_file_node = bostree_node_weak_ref(current_file_node); queue_image_load(bostree_node_weak_ref(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) { if(option_allow_empty_window) { D_UNLOCK(file_tree); current_file_node = NULL; earlier_file_node = NULL; invalidate_current_scaled_image_surface(); if(last_visible_surface) { cairo_surface_destroy(last_visible_surface); last_visible_surface = NULL; } current_image_drawn = FALSE; update_info_text(NULL); queue_draw(); return FALSE; } else { g_printerr("No images left to display.\n"); if(gtk_main_level() == 0) { exit(1); } gtk_main_quit(); } } D_UNLOCK(file_tree); } return FALSE; }/*}}}*/ #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE void image_loader_create_thumbnail(file_t *file) {/*{{{*/ const double scale_level_w = option_thumbnails.width * 1.0 / file->width; const double scale_level_h = option_thumbnails.height * 1.0 / file->height; double scale_level = scale_level_w > scale_level_h ? scale_level_h : scale_level_w; if(scale_level > 1.) { scale_level = 1.; } cairo_surface_t *surf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, scale_level * file->width + .5, scale_level * file->height + .5); if(cairo_surface_status(surf) != CAIRO_STATUS_SUCCESS) { cairo_surface_destroy(surf); return; } cairo_t *cr = cairo_create(surf); // 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 centered cairo_translate(cr, (cairo_image_surface_get_width(surf) - scale_level * file->width) / 2, (cairo_image_surface_get_height(surf) - scale_level * file->height) / 2); cairo_scale(cr, scale_level, scale_level); // Draw background pattern if(background_checkerboard_pattern != NULL && !option_transparent_background) { cairo_save(cr); cairo_new_path(cr); unsigned skip_px = (unsigned)(1./scale_level); if(skip_px == 0) { skip_px = 1; } cairo_rectangle(cr, skip_px, skip_px, file->width - 2*skip_px, file->height - 2*skip_px); cairo_close_path(cr); cairo_clip(cr); cairo_set_source(cr, background_checkerboard_pattern); cairo_paint(cr); cairo_restore(cr); } cairo_rectangle(cr, 0, 0, file->width, file->height); cairo_clip(cr); if(file->file_type->draw_fn != NULL) { g_mutex_lock(&file->lock); file->file_type->draw_fn(file, cr); g_mutex_unlock(&file->lock); } cairo_destroy(cr); file->thumbnail = surf; }/*}}}*/ #endif void image_generate_prerendered_view(file_t *file, gboolean force, double scale_level) {/*{{{*/ if(option_lowmem) { return; } if(file->file_flags && FILE_FLAGS_ANIMATION) { return; } if(force && file->prerendered_view) { cairo_surface_destroy(file->prerendered_view); file->prerendered_view = NULL; } if(scale_level < 0) { scale_level = calculate_auto_scale_level_for_screen(file->width, file->height); } int width = scale_level * file->width + .5; int height = scale_level * file->height + .5; if(file->prerendered_view) { int old_width = cairo_image_surface_get_width(file->prerendered_view); int old_height = cairo_image_surface_get_height(file->prerendered_view); if(old_width != width || old_height != height) { cairo_surface_destroy(file->prerendered_view); file->prerendered_view = NULL; } } if(!file->prerendered_view) { cairo_surface_t *prerendered_view = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); if(cairo_surface_status(prerendered_view) == CAIRO_STATUS_SUCCESS) { cairo_t *cr = cairo_create(prerendered_view); cairo_scale(cr, scale_level, scale_level); cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); if(file->file_type->draw_fn != NULL) { g_mutex_lock(&file->lock); file->file_type->draw_fn(file, cr); g_mutex_unlock(&file->lock); } cairo_destroy(cr); file->prerendered_view = cairo_surface_reference(prerendered_view); } cairo_surface_destroy(prerendered_view); } }/*}}}*/ gpointer image_loader_thread(gpointer user_data) {/*{{{*/ while(TRUE) { // Handle new queued image load struct image_loader_queue_item *it = g_async_queue_pop(image_loader_queue); BOSNode *node = it->node_ref; #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE image_loader_purpose_t purpose = it->purpose; #endif g_slice_free(struct image_loader_queue_item, it); // The image might still be in the loader queue though it has already // been invalidated. In this case, skip it. if(!bostree_node_weak_unref(file_tree, bostree_node_weak_ref(node))) { D_LOCK(file_tree); bostree_node_weak_unref(file_tree, node); D_UNLOCK(file_tree); continue; } // Short-circuit: If we want to load this image for its thumbnail, check the cache first. // We might not have to load it at all. #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE if(purpose == MONTAGE) { // Unload an old thumbnail if it does not have the correct size D_LOCK(file_tree); test_and_invalidate_thumbnail(FILE(node)); if(!FILE(node)->thumbnail && (option_thumbnails.enabled || application_mode == MONTAGE) && option_thumbnails.persist != THUMBNAILS_PERSIST_OFF) { if(load_thumbnail_from_cache(FILE(node), option_thumbnails.width, option_thumbnails.height, option_thumbnails.persist, option_thumbnails.special_thumbnail_directory) == TRUE) { // Loading the thumbnail succeeded. We may break here. bostree_node_weak_unref(file_tree, node); D_UNLOCK(file_tree); // Notify the main thread about this. gdk_threads_add_idle((GSourceFunc)image_loaded_handler, node); continue; } } D_UNLOCK(file_tree); } #endif // 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(bostree_node_weak_ref(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(FILE(node)->is_loaded) { #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE D_LOCK(file_tree); test_and_invalidate_thumbnail(FILE(node)); if(!FILE(node)->thumbnail && (option_thumbnails.enabled || application_mode == MONTAGE)) { if(option_thumbnails.persist == THUMBNAILS_PERSIST_OFF || load_thumbnail_from_cache(FILE(node), option_thumbnails.width, option_thumbnails.height, option_thumbnails.persist, option_thumbnails.special_thumbnail_directory) == FALSE) { D_UNLOCK(file_tree); image_loader_create_thumbnail(FILE(node)); D_LOCK(file_tree); if(FILE(node)->thumbnail && option_thumbnails.persist != THUMBNAILS_PERSIST_OFF && option_thumbnails.persist != THUMBNAILS_PERSIST_RO) { store_thumbnail_to_cache(FILE(node), option_thumbnails.width, option_thumbnails.height, option_thumbnails.persist, option_thumbnails.special_thumbnail_directory); } } } D_UNLOCK(file_tree); #endif if(node == current_file_node) { current_image_drawn = FALSE; } // Prerender the default scaled view of the image for faster image transitions image_generate_prerendered_view(FILE(node), FALSE, -1); gdk_threads_add_idle((GSourceFunc)image_loaded_handler, node); } D_LOCK(file_tree); bostree_node_weak_unref(file_tree, node); D_UNLOCK(file_tree); } }/*}}}*/ void image_loader_queue_destroy(gpointer data) {/*{{{*/ bostree_node_weak_unref(file_tree, ((struct image_loader_queue_item *)data)->node_ref); g_slice_free(struct image_loader_queue_item, data); }/*}}}*/ gboolean initialize_image_loader() {/*{{{*/ if(image_loader_initialization_succeeded) { return TRUE; } if(image_loader_queue == NULL) { image_loader_queue = g_async_queue_new_full(image_loader_queue_destroy); image_loader_cancellable = g_cancellable_new(); } D_LOCK(file_tree); if(current_file_node != NULL) { // If this has previously been ref'ed for any reason, unref here. bostree_node_weak_unref(file_tree, current_file_node); } 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); preload_adjacent_images(); image_loader_initialization_succeeded = TRUE; return TRUE; }/*}}}*/ void abort_pending_image_loads(BOSNode *new_pos) {/*{{{*/ struct image_loader_queue_item *ref; if(image_loader_queue == NULL) { return; } while((ref = g_async_queue_try_pop(image_loader_queue)) != NULL) { bostree_node_weak_unref(file_tree, ref->node_ref); g_slice_free(struct image_loader_queue_item, 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) {/*{{{*/ struct image_loader_queue_item *it = g_slice_new(struct image_loader_queue_item); it->node_ref = node; // Must be weak_ref'ed by caller. (Simplifies thread safety.) it->purpose = DEFAULT; g_async_queue_push(image_loader_queue, it); }/*}}}*/ #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE void queue_thumbnail_load(BOSNode *node) {/*{{{*/ struct image_loader_queue_item *it = g_slice_new(struct image_loader_queue_item); it->node_ref = node; // Must be weak_ref'ed by caller. it->purpose = MONTAGE; g_async_queue_push(image_loader_queue, it); }/*}}}*/ #endif void unload_image(BOSNode *node) {/*{{{*/ if(!node) { return; } file_t *file = FILE(node); g_mutex_lock(&file->lock); if(file->file_type->unload_fn != NULL) { file->file_type->unload_fn(file); } if(file->prerendered_view) { cairo_surface_destroy(file->prerendered_view); file->prerendered_view = NULL; } 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; } g_mutex_unlock(&file->lock); }/*}}}*/ 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(bostree_node_weak_ref(current_file_node)); } else { 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(bostree_node_weak_ref(new_next)); } if(!FILE(new_prev)->is_loaded) { queue_image_load(bostree_node_weak_ref(new_prev)); } D_UNLOCK(file_tree); } #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE if(option_thumbnails.enabled && option_thumbnails.auto_generate_for_adjacents > 0) { D_LOCK(file_tree); size_t thumbnail_rank = bostree_rank(current_file_node); size_t count; if(thumbnail_rank > (unsigned)option_thumbnails.auto_generate_for_adjacents) { count = 2 * option_thumbnails.auto_generate_for_adjacents + 2; thumbnail_rank -= option_thumbnails.auto_generate_for_adjacents; } else { count = thumbnail_rank + option_thumbnails.auto_generate_for_adjacents + 1; thumbnail_rank = 0; } BOSNode *thumbnail_node = bostree_select(file_tree, thumbnail_rank); for(; thumbnail_node && count > 0; thumbnail_node = bostree_next_node(thumbnail_node), count--) { if(!test_and_invalidate_thumbnail(FILE(thumbnail_node))) { queue_thumbnail_load(bostree_node_weak_ref(thumbnail_node)); } } D_UNLOCK(file_tree); } #endif }/*}}}*/ 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); #ifndef CONFIGURED_WITHOUT_ACTIONS // Set the new image as current if(earlier_file_node != NULL) { bostree_node_weak_unref(file_tree, earlier_file_node); } earlier_file_node = current_file_node; current_file_node = bostree_node_weak_ref(node); if(current_file_node != earlier_file_node) { invalidate_current_scaled_image_surface(); } #else if(current_file_node != NULL) { bostree_node_weak_unref(file_tree, current_file_node); } current_file_node = bostree_node_weak_ref(node); #endif #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(bostree_node_weak_ref(current_file_node)); D_UNLOCK(file_tree); // Preload the adjacent images preload_adjacent_images(); // 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 not all images have been viewed, expand it while(shuffled_images_list_length < bostree_node_count(file_tree) && 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; break; } } // 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); } } // 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); if(!file_tree_valid || !bostree_node_count(file_tree)) { D_UNLOCK(file_tree); return; } 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; } } #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE if(application_mode == MONTAGE) { D_LOCK(file_tree); if(montage_window_control.selected_node != NULL) { bostree_node_weak_unref(file_tree, montage_window_control.selected_node); } montage_window_control.selected_node = target; montage_window_move_cursor(0, 0, 0); D_UNLOCK(file_tree); gtk_widget_queue_draw(GTK_WIDGET(main_window)); return; } #endif // 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, gboolean logical_directories) {/*{{{*/ // 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(!logical_directories && 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 || (!logical_directories && FILE(target)->file_flags & FILE_FLAGS_MEMORY_IMAGE)) { break; } const char *target_name = logical_directories ? FILE(target)->display_name : FILE(target)->file_name; const char *current_name = logical_directories ? FILE(current)->display_name : FILE(current)->file_name; // 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(target_name[pos] && current_name[pos] && target_name[pos] == current_name[pos]) { pos++; } // The physical 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 // The logical path changed if the same holds, only that the // special character '#' (used to separate entries from the archive // name in archives) counts as a directory separator, too. gboolean directory_changed = FALSE; for(unsigned int i=pos; target_name[i]; i++) { if(target_name[i] == G_DIR_SEPARATOR || (logical_directories && target_name[i] == '#')) { // Gotcha. directory_changed = TRUE; break; } } if(!directory_changed) { for(unsigned int i=pos; current_name[i]; i++) { if(current_name[i] == G_DIR_SEPARATOR || (logical_directories && current_name[i] == '#')) { directory_changed = TRUE; break; } } } if(directory_changed) { break; } } } return target; }/*}}}*/ BOSNode *relative_image_pointer_directory(int direction, gboolean logical_directories) {/*{{{*/ // 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. // BOSNode *target; BOSNode *current = current_file_node; if(direction == 1) { // Forward searches are trivial target = directory_image_movement_find_different_directory(current, 1, logical_directories); } 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, logical_directories); target = directory_image_movement_find_different_directory(target, -1, logical_directories); if(target != current) { target = bostree_next_node(target); if(!target) { target = bostree_select(file_tree, 0); } } } return target; }/*}}}*/ void directory_image_movement(int direction, gboolean logical_directories) {/*{{{*/ // 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 = bostree_node_weak_ref(relative_image_pointer_directory(direction, logical_directories)); D_UNLOCK(file_tree); if(application_mode == DEFAULT) { absolute_image_movement(target); } #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE else if(application_mode == MONTAGE) { D_LOCK(file_tree); if(montage_window_control.selected_node != NULL) { bostree_node_weak_unref(file_tree, montage_window_control.selected_node); } montage_window_control.selected_node = target; montage_window_move_cursor(0, 0, 0); D_UNLOCK(file_tree); } #endif }/*}}}*/ 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 double old_scale_level = current_scale_level; set_scale_level_for_screen(); if(fabs(old_scale_level - current_scale_level) > DBL_EPSILON) { invalidate_current_scaled_image_surface(); image_generate_prerendered_view(CURRENT_FILE, FALSE, current_scale_level); } main_window_adjust_for_image(); 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] == '-') { GString *marklist = get_all_marked(); argv[2] = external_filter + 1; GPid child_pid; gint child_stdin; if(!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &child_pid, &child_stdin, NULL, NULL, &error_pointer) ) { g_printerr("Failed execute external command `%s': %s\n", argv[2], error_pointer->message); g_clear_error(&error_pointer); } else { if(write(child_stdin, marklist->str, marklist->len) == -1) { g_printerr("Failed writing to stdin of %s\n", argv[2]); } close(child_stdin); gint status = 0; // When left uninitialized, external command reports exiting due to signal 95 (exit statuses have been 233, 201, 217). Why? #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 (!WIFEXITED(status)) { if (WIFSIGNALED(status)) { g_printerr("External command exited due to signal %d (exit status: %d)\n", WTERMSIG(status), WEXITSTATUS(status)); } else { g_printerr("External command failed with exit status %d\n", WEXITSTATUS(status)); } } } g_string_free(marklist, TRUE); } 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, G_SPAWN_DO_NOT_REAP_CHILD, 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); g_mutex_init(&new_image->lock); 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) && !defined(_WIN32)) // Note: Win32 GLib uses I64 for G_GINT64_FORMAT, which isn't // supported by the standard. 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"); } info_text_queue_redraw(); 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"); info_text_queue_redraw(); bostree_node_weak_unref(file_tree, the_file); return; } // Intentionally ignoring mkdir return value -- the error case is handled below, and this // saves one extra access(2) call. 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); info_text_queue_redraw(); } g_free(store_target); } else { update_info_text("Created hard-link into .pqiv-select"); info_text_queue_redraw(); } g_free(link_target); g_free(current_file_basename); bostree_node_weak_unref(file_tree, the_file); }/*}}}*/ 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; } if(fading_current_alpha_stage < 1.) { 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)); if(fading_current_alpha_stage < 1.) { return TRUE; } else { if(fading_surface) { cairo_surface_destroy(fading_surface); fading_surface = NULL; } return FALSE; } }/*}}}*/ 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 apply_interpolation_quality(cairo_t *cr) {/*{{{*/ switch(option_interpolation_quality) { case AUTO: if(CURRENT_FILE->width < 100 || CURRENT_FILE->height < 100) { cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_NEAREST); } else { cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_GOOD); } break; case FAST: cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_FAST); break; case GOOD: cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_GOOD); break; case BEST: cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_BEST); break; } }/*}}}*/ void draw_current_image_to_context(cairo_t *cr) {/*{{{*/ if(CURRENT_FILE->file_type->draw_fn != NULL) { g_mutex_lock(&CURRENT_FILE->lock); CURRENT_FILE->file_type->draw_fn(CURRENT_FILE, cr); g_mutex_unlock(&CURRENT_FILE->lock); } }/*}}}*/ void setup_checkerboard_pattern() {/*{{{*/ // Create pattern if(background_checkerboard_pattern != NULL) { return; } 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; } if(CURRENT_FILE->prerendered_view && fabs(current_scale_level * CURRENT_FILE->width + .5 - cairo_image_surface_get_width(CURRENT_FILE->prerendered_view)) < 2 && fabs(current_scale_level * CURRENT_FILE->height + .5 - cairo_image_surface_get_height(CURRENT_FILE->prerendered_view)) < 2) { // If the file has a prerender at the correct size attached, we can reuse it here. cairo_surface_t *retval = cairo_surface_reference(CURRENT_FILE->prerendered_view); if(!option_lowmem) { current_scaled_image_surface = cairo_surface_reference(retval); } return retval; } /* else if(CURRENT_FILE->prerendered_view) { printf("Info: Cache miss! %dx%d (cached) vs %dx%d (requested)\n", cairo_image_surface_get_width(CURRENT_FILE->prerendered_view), cairo_image_surface_get_height(CURRENT_FILE->prerendered_view), (int)(current_scale_level * CURRENT_FILE->width + .5), (int)(current_scale_level * CURRENT_FILE->height + .5)); } else { printf("Info: Cache miss! Nothing present %dx%d (requested)\n", (int)(current_scale_level * CURRENT_FILE->width + .5), (int)(current_scale_level * CURRENT_FILE->height + .5)); } */ 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)); fflush(stdout); } 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; // 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); } /* }}} */ #endif // }}} /* Main window functions {{{ */ gboolean window_fullscreen_helper_reset_transition_id() {/*{{{*/ action_done(); fullscreen_transition_source_id = -1; return FALSE; }/*}}}*/ void window_fullscreen() {/*{{{*/ if(is_current_file_loaded()) { main_window_in_fullscreen = TRUE; image_generate_prerendered_view(CURRENT_FILE, FALSE, -1); main_window_in_fullscreen = FALSE; } // 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); } // Required to avoid tearing if(is_current_file_loaded() && main_window_visible) { // This calls only the 2nd part of window_show_background_pixmap, which // blanks the window. window_clear_background_pixmap(); window_show_background_pixmap_cb(NULL); } #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_scale_factor, screen_geometry.y / screen_scale_factor); gtk_window_resize(main_window, screen_geometry.width / screen_scale_factor, screen_geometry.height / screen_scale_factor); requested_main_window_width = screen_geometry.width; requested_main_window_height = screen_geometry.height; window_state_into_fullscreen_actions(NULL); return; } #endif if(fullscreen_transition_source_id >= 0) { g_source_remove(fullscreen_transition_source_id); } fullscreen_transition_source_id = g_timeout_add(500, window_fullscreen_helper_reset_transition_id, NULL); gtk_window_fullscreen(main_window); }/*}}}*/ void window_unfullscreen() {/*{{{*/ if(is_current_file_loaded()) { main_window_in_fullscreen = FALSE; image_generate_prerendered_view(CURRENT_FILE, FALSE, -1); main_window_in_fullscreen = TRUE; } // Required to avoid tearing if(is_current_file_loaded() && main_window_visible) { // This calls only the 2nd part of window_show_background_pixmap, which // blanks the window. window_clear_background_pixmap(); window_show_background_pixmap_cb(NULL); } // Ensure that the unfullscreened window will be centered again if(option_window_position.x != -1) { gtk_window_set_position(main_window, GTK_WIN_POS_CENTER_ALWAYS); if(requested_main_window_resize_pos_callback_id > -1) { g_source_remove(requested_main_window_resize_pos_callback_id); } requested_main_window_resize_pos_callback_id = g_timeout_add(500, main_window_reset_pos_callback, NULL); } #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(NULL); return; } #endif if(fullscreen_transition_source_id >= 0) { g_source_remove(fullscreen_transition_source_id); } fullscreen_transition_source_id = g_timeout_add(500, window_fullscreen_helper_reset_transition_id, NULL); 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 && main_window_visible) { gtk_widget_queue_draw_area(GTK_WIDGET(main_window), current_info_text_bounding_box.x, current_info_text_bounding_box.y, main_window_width - current_info_text_bounding_box.x, current_info_text_bounding_box.height ); } }/*}}}*/ void update_info_text(const gchar *action) {/*{{{*/ D_LOCK(file_tree); current_info_text_cached_font_size = -1; #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE if(application_mode == MONTAGE) { if(!option_hide_info_box) { if(current_info_text != NULL) { g_free(current_info_text); } current_info_text = g_strdup("Montage mode"); } gtk_window_set_title(GTK_WINDOW(main_window), "pqiv: Montage mode"); D_UNLOCK(file_tree); return; } #endif if(!current_file_node) { const char *none_loaded = "No image loaded"; if(!option_hide_info_box) { if(current_info_text != NULL) { g_free(current_info_text); } if(action) { current_info_text = g_strdup_printf("%s - %s", action, none_loaded); } else { current_info_text = g_strdup(none_loaded); } } gtk_window_set_title(GTK_WINDOW(main_window), "pqiv: No image loaded"); D_UNLOCK(file_tree); return; } 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. if(!option_hide_info_box) { current_info_text = g_strdup_printf("%s (Image is still loading...)", display_name); } gtk_window_set_title(GTK_WINDOW(main_window), "pqiv"); 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]%s", 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)), CURRENT_FILE->marked ? " [m]" : ""); 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; } }/*}}}*/ #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE void montage_window_set_cursor(int pos_x, int pos_y) {/*{{{*/ const unsigned n_thumbs_x = main_window_width / (option_thumbnails.width + 10); const unsigned n_thumbs_y = main_window_height / (option_thumbnails.height + 10); const size_t number_of_images = (ptrdiff_t)bostree_node_count(file_tree); if(!montage_window_control.selected_node) { return; } BOSNode *selected_node = bostree_node_weak_unref(file_tree, montage_window_control.selected_node); if(!selected_node) { selected_node = bostree_select(file_tree, montage_window_control.scroll_y * n_thumbs_x); if(!selected_node) { selected_node = bostree_select(file_tree, 0); } if(!selected_node) { montage_window_control.selected_node = NULL; return; } } size_t old_selection = bostree_rank(selected_node); if(pos_x < 0) { pos_x = old_selection % n_thumbs_x; } if(pos_y < 0) { pos_y = old_selection / n_thumbs_x; } if((unsigned)pos_y >= n_thumbs_y) { pos_y = n_thumbs_y - 1; } size_t new_selection = montage_window_control.scroll_y * n_thumbs_x + pos_x + pos_y * n_thumbs_x; if(new_selection > number_of_images) { new_selection = number_of_images - 1; } BOSNode *new_selected_node = bostree_select(file_tree, new_selection); if(!new_selected_node) { new_selected_node = selected_node; } montage_window_control.selected_node = bostree_node_weak_ref(new_selected_node); }/*}}}*/ gboolean montage_window_get_move_cursor_target(int pos_x, int pos_y, int move_y_pages, int *target_x, int *target_y, int *target_scroll_y, BOSNode **target_node) {/*{{{*/ /* The idea to call this function with a possibly invalid pair of on-screen coordinates (pos_x, pos_y) and an amount of pages to scroll move_y_pages. The function will calculate a valid set of coordinates based on the wrapping rules and store them in the output pointers. It returns whether the target is visible on the screen without scrolling */ const int n_thumbs_x = main_window_width / (option_thumbnails.width + 10); const int n_thumbs_y = main_window_height / (option_thumbnails.height + 10); const ptrdiff_t number_of_images = (ptrdiff_t)bostree_node_count(file_tree); const int n_rows_total = (number_of_images + n_thumbs_x - 1) / n_thumbs_x; const int last_row_n_thumbs = (number_of_images % n_thumbs_x == 0) ? n_thumbs_x : number_of_images % n_thumbs_x; int scroll_y = montage_window_control.scroll_y; int original_scroll_y = scroll_y; // Use absolute pos_y coordinates pos_y += scroll_y; // Adjust x position to fit, ignoring the end of the file list for now if(option_montage_mode_wrap_mode == MONTAGE_MODE_WRAP_OFF) { if(pos_x < 0) pos_x = 0; if(pos_x >= n_thumbs_x) pos_x = n_thumbs_x - 1; } else { if(pos_x <= -n_thumbs_x || pos_x >= n_thumbs_x) { pos_y += pos_x / n_thumbs_x; pos_x %= n_thumbs_x; } if(pos_x < 0) { pos_y--; pos_x += n_thumbs_x; } } // Scroll pages if(move_y_pages) { pos_y += move_y_pages * n_thumbs_y; scroll_y += move_y_pages * n_thumbs_y; } // Adjust y position to fit int wrap = 0; if(pos_y < 0) { if(option_montage_mode_wrap_mode != MONTAGE_MODE_WRAP_FULL) { pos_y = 0; pos_x = 0; } else { while(pos_y < 0) { pos_y += n_rows_total; } wrap = 1; } } if(pos_y >= n_rows_total) { if(option_montage_mode_wrap_mode != MONTAGE_MODE_WRAP_FULL) { pos_y = n_rows_total - 1; pos_x = last_row_n_thumbs - 1; } else { while(pos_y >= n_rows_total) { pos_y -= n_rows_total; } wrap = -1; } } if(pos_y == n_rows_total - 1) { if(pos_x >= last_row_n_thumbs) { if(option_montage_mode_wrap_mode != MONTAGE_MODE_WRAP_FULL) { pos_x = last_row_n_thumbs - 1; } else { if(wrap == 1) { pos_x -= (n_thumbs_x - last_row_n_thumbs); } else { pos_y = 0; pos_x -= last_row_n_thumbs; } } } } // Fixup scroll position if necessary if(scroll_y < 0) { scroll_y = 0; } int upper_bound = n_rows_total > n_thumbs_y ? n_rows_total - n_thumbs_y : n_rows_total; if(scroll_y > upper_bound) { scroll_y = upper_bound; } if(scroll_y > pos_y) { scroll_y = pos_y; } if(scroll_y + n_thumbs_y <= pos_y) { scroll_y = pos_y - n_thumbs_y + 1; } // Return to page coordinates pos_y -= scroll_y; if(target_x) { *target_x = pos_x; } if(target_y) { *target_y = pos_y; } if(target_scroll_y) { *target_scroll_y = scroll_y; } if(target_node) { *target_node = bostree_select(file_tree, (scroll_y + pos_y) * n_thumbs_x + pos_x); } return scroll_y == original_scroll_y; }/*}}}*/ void montage_window_move_cursor(int move_x, int move_y, int move_y_pages) {/*{{{*/ // Must be called with an active lock. const int n_thumbs_x = main_window_width / (option_thumbnails.width + 10); const int n_thumbs_y = main_window_height / (option_thumbnails.height + 10); if(n_thumbs_x == 0 || n_thumbs_y == 0) { return; } BOSNode *selected_node = bostree_node_weak_unref(file_tree, montage_window_control.selected_node); if(!selected_node) { selected_node = bostree_select(file_tree, montage_window_control.scroll_y * n_thumbs_x); if(!selected_node) { selected_node = bostree_select(file_tree, 0); } if(!selected_node) { montage_window_control.selected_node = NULL; return; } } size_t old_selection = bostree_rank(selected_node); int pos_x = old_selection % n_thumbs_x; int pos_y = old_selection / n_thumbs_x; if(montage_window_control.scroll_y + n_thumbs_y <= pos_y) { montage_window_control.scroll_y = pos_y - n_thumbs_y + 1; } else if(montage_window_control.scroll_y > pos_y) { montage_window_control.scroll_y = pos_y; } pos_y -= montage_window_control.scroll_y; if(move_x != 0 || move_y != 0 || move_y_pages != 0) { selected_node = NULL; montage_window_get_move_cursor_target(pos_x + move_x, pos_y + move_y, move_y_pages, &pos_x, &pos_y, &montage_window_control.scroll_y, &selected_node); } montage_window_control.selected_node = bostree_node_weak_ref(selected_node); // Queue loading of thumbnails abort_pending_image_loads(selected_node); int thumb_node_fwd_ctr = (n_thumbs_y - pos_y - 1) * n_thumbs_x + (n_thumbs_x - pos_x - 1) + (option_thumbnails.auto_generate_for_adjacents > 0 ? option_thumbnails.auto_generate_for_adjacents : 0) + 1; BOSNode *thumb_node_fwd = selected_node; int thumb_node_bwd_ctr = pos_y * n_thumbs_x + pos_x + (option_thumbnails.auto_generate_for_adjacents > 0 ? option_thumbnails.auto_generate_for_adjacents : 0); BOSNode *thumb_node_bwd = bostree_previous_node(selected_node); while(TRUE) { gboolean did_something = FALSE; if(thumb_node_fwd && thumb_node_fwd_ctr) { if(!test_and_invalidate_thumbnail(FILE(thumb_node_fwd))) { queue_thumbnail_load(bostree_node_weak_ref(thumb_node_fwd)); } thumb_node_fwd = bostree_next_node(thumb_node_fwd); thumb_node_fwd_ctr--; did_something = TRUE; } if(thumb_node_bwd && thumb_node_bwd_ctr) { if(!test_and_invalidate_thumbnail(FILE(thumb_node_bwd))) { queue_thumbnail_load(bostree_node_weak_ref(thumb_node_bwd)); } thumb_node_bwd = bostree_previous_node(thumb_node_bwd); thumb_node_bwd_ctr--; did_something = TRUE; } if(!did_something) { break; } } }/*}}}*/ #ifndef CONFIGURED_WITHOUT_ACTIONS struct window_draw_thumbnail_montage_show_binding_overlays_data { cairo_t *cr; int current_x; int current_y; char *active_prefix; }; void window_draw_thumbnail_montage_show_binding_overlays_looper(gpointer key, gpointer value, gpointer user_data) {/*{{{*/ const int n_thumbs_x = main_window_width / (option_thumbnails.width + 10); const int n_thumbs_y = main_window_height / (option_thumbnails.height + 10); const ptrdiff_t number_of_images = (ptrdiff_t)bostree_node_count(file_tree); const int n_rows_total = (number_of_images + n_thumbs_x - 1) / n_thumbs_x; const int last_row_n_thumbs = (number_of_images % n_thumbs_x == 0) ? n_thumbs_x : number_of_images % n_thumbs_x; struct window_draw_thumbnail_montage_show_binding_overlays_data data = *(struct window_draw_thumbnail_montage_show_binding_overlays_data *)user_data; guint key_binding_value = GPOINTER_TO_UINT(key); key_binding_t *binding = value; data.active_prefix = key_binding_sequence_to_string(key_binding_value, data.active_prefix); if(binding->next_key_bindings) { g_hash_table_foreach(binding->next_key_bindings, window_draw_thumbnail_montage_show_binding_overlays_looper, &data); } ptrdiff_t target_index; BOSNode *target_node; for(; binding; binding = binding->next_action) { switch(binding->action) { case ACTION_MONTAGE_MODE_SET_SHIFT_X: data.current_x = binding->parameter.pint; break; case ACTION_MONTAGE_MODE_SET_SHIFT_Y: data.current_y = binding->parameter.pint; break; case ACTION_MONTAGE_MODE_FOLLOW_PROCEED: if(binding->parameter.p2short.p1 >= 0) { data.current_x = binding->parameter.p2short.p1; } if(binding->parameter.p2short.p2 >= 0) { data.current_y = binding->parameter.p2short.p2; } break; case ACTION_MONTAGE_MODE_SHIFT_X: if(!montage_window_get_move_cursor_target(data.current_x + binding->parameter.pint, data.current_y, 0, &data.current_x, &data.current_y, NULL, NULL)) { data.current_y = -1; while(binding->next_action) binding = binding->next_action; break; } break; case ACTION_MONTAGE_MODE_SHIFT_Y: if(!montage_window_get_move_cursor_target(data.current_x, data.current_y + binding->parameter.pint, 0, &data.current_x, &data.current_y, NULL, NULL)) { data.current_y = -1; while(binding->next_action) binding = binding->next_action; break; } break; case ACTION_MONTAGE_MODE_SHIFT_Y_PG: if(!montage_window_get_move_cursor_target(data.current_x, data.current_y, binding->parameter.pint, &data.current_x, &data.current_y, NULL, NULL)) { data.current_y = -1; while(binding->next_action) binding = binding->next_action; break; } break; case ACTION_GOTO_FILE_RELATIVE: target_index = bostree_rank(relative_image_pointer(binding->parameter.pint)); data.current_y = target_index / n_thumbs_x - montage_window_control.scroll_y; data.current_x = target_index % n_thumbs_x; break; case ACTION_GOTO_FILE_BYINDEX: target_index = binding->parameter.pint; if(target_index < 0 || target_index > (int)bostree_node_count(file_tree) - 1) { target_index = bostree_node_count(file_tree) - 1; } data.current_y = target_index / n_thumbs_x - montage_window_control.scroll_y; data.current_x = target_index % n_thumbs_x; break; case ACTION_GOTO_FILE_BYNAME: target_node = image_pointer_by_name(binding->parameter.pcharptr); if(target_node) { target_index = bostree_rank(target_node); data.current_y = target_index / n_thumbs_x - montage_window_control.scroll_y; data.current_x = target_index % n_thumbs_x; } break; case ACTION_GOTO_DIRECTORY_RELATIVE: target_node = relative_image_pointer_directory(binding->parameter.pint, FALSE); if(target_node) { target_index = bostree_rank(target_node); data.current_y = target_index / n_thumbs_x - montage_window_control.scroll_y; data.current_x = target_index % n_thumbs_x; } break; case ACTION_GOTO_LOGICAL_DIRECTORY_RELATIVE: target_node = relative_image_pointer_directory(binding->parameter.pint, TRUE); if(target_node) { target_index = bostree_rank(target_node); data.current_y = target_index / n_thumbs_x - montage_window_control.scroll_y; data.current_x = target_index % n_thumbs_x; } break; default: break; } } if(data.current_x >= 0 && data.current_x < n_thumbs_x && data.current_y >= 0 && data.current_y < n_thumbs_y && (data.current_y + montage_window_control.scroll_y != n_rows_total - 1 || data.current_x < last_row_n_thumbs) && ((data.current_x != ((struct window_draw_thumbnail_montage_show_binding_overlays_data *)user_data)->current_x || data.current_y != ((struct window_draw_thumbnail_montage_show_binding_overlays_data *)user_data)->current_y))) { cairo_t *cr_arg = data.cr; cairo_save(cr_arg); cairo_translate(cr_arg, (main_window_width - n_thumbs_x * (option_thumbnails.width + 10)) / 2 + data.current_x * (option_thumbnails.width + 10), (main_window_height - n_thumbs_y * (option_thumbnails.height + 10)) / 2 + data.current_y * (option_thumbnails.height + 10) ); BOSNode *node = bostree_select(file_tree, (montage_window_control.scroll_y + data.current_y) * n_thumbs_x + data.current_x); if(node && FILE(node)->thumbnail) { cairo_translate(cr_arg, (option_thumbnails.width - cairo_image_surface_get_width(FILE(node)->thumbnail)) / 2 + 5, (option_thumbnails.height - cairo_image_surface_get_height(FILE(node)->thumbnail)) / 2 + 5 ); } double x1, y1, x2, y2; cairo_set_font_size(cr_arg, 12); cairo_text_path(cr_arg, data.active_prefix); cairo_path_extents(cr_arg, &x1, &y1, &x2, &y2); cairo_path_t *text_path = cairo_copy_path(cr_arg); 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_set_source_rgb(cr_arg, option_box_colors.bg_red, option_box_colors.bg_green, option_box_colors.bg_blue); cairo_fill(cr_arg); cairo_new_path(cr_arg); cairo_append_path(cr_arg, text_path); cairo_set_source_rgb(cr_arg, option_box_colors.fg_red, option_box_colors.fg_green, option_box_colors.fg_blue); cairo_fill(cr_arg); cairo_path_destroy(text_path); cairo_restore(cr_arg); } free(data.active_prefix); }/*}}}*/ #endif gboolean window_draw_thumbnail_montage(cairo_t *cr_arg) {/*{{{*/ D_LOCK(file_tree); // Draw black background cairo_save(cr_arg); cairo_set_source_rgba(cr_arg, 0., 0., 0., option_transparent_background ? 0. : 1.); cairo_set_operator(cr_arg, CAIRO_OPERATOR_SOURCE); cairo_paint(cr_arg); cairo_restore(cr_arg); // Calculate how many thumbnails to draw const unsigned n_thumbs_x = main_window_width / (option_thumbnails.width + 10); const unsigned n_thumbs_y = main_window_height / (option_thumbnails.height + 10); size_t top_left_id = montage_window_control.scroll_y * n_thumbs_x; BOSNode *selected_node = bostree_node_weak_unref(file_tree, bostree_node_weak_ref(montage_window_control.selected_node)); size_t selection_rank; if(!selected_node) { selected_node = NULL; selection_rank = (size_t)-1; } else { selection_rank = bostree_rank(selected_node); } // Do a check if the selected image is out of bounds. Fix if it is. if(top_left_id > selection_rank || top_left_id + n_thumbs_x * n_thumbs_y < selection_rank) { montage_window_move_cursor(0, 0, 0); top_left_id = montage_window_control.scroll_y * n_thumbs_x; } if(!file_tree_valid) { D_UNLOCK(file_tree); return FALSE; } BOSNode *thumb_node = bostree_select(file_tree, top_left_id); for(size_t draw_now = 0; draw_now < n_thumbs_x * n_thumbs_y && thumb_node; draw_now++, thumb_node = bostree_next_node(thumb_node)) { if(!file_tree_valid || !thumb_node) { break; } file_t *thumb_file = FILE(thumb_node); /*/ Debug: Draw a red box around the thumbnail box cairo_save(cr_arg); cairo_translate(cr_arg, (main_window_width - n_thumbs_x * (option_thumbnails.width + 10)) / 2 + (draw_now % n_thumbs_x) * (option_thumbnails.width + 10), (main_window_height - n_thumbs_y * (option_thumbnails.height + 10)) / 2 + (draw_now / n_thumbs_x) * (option_thumbnails.height + 10) ); cairo_set_source_rgb(cr_arg, 1., 0, 0); cairo_rectangle(cr_arg, 0, 0, option_thumbnails.width + 10, option_thumbnails.height + 10); cairo_stroke(cr_arg); cairo_restore(cr_arg);*/ if(thumb_file->thumbnail) { cairo_save(cr_arg); cairo_translate(cr_arg, (main_window_width - n_thumbs_x * (option_thumbnails.width + 10)) / 2 + (draw_now % n_thumbs_x) * (option_thumbnails.width + 10) + (option_thumbnails.width + 10 - cairo_image_surface_get_width(thumb_file->thumbnail))/2, (main_window_height - n_thumbs_y * (option_thumbnails.height + 10)) / 2 + (draw_now / n_thumbs_x) * (option_thumbnails.height + 10) + (option_thumbnails.height + 10 - cairo_image_surface_get_height(thumb_file->thumbnail))/2 ); cairo_set_source_surface(cr_arg, thumb_file->thumbnail, 0, 0); cairo_new_path(cr_arg); cairo_rectangle(cr_arg, 0, 0, cairo_image_surface_get_width(thumb_file->thumbnail), cairo_image_surface_get_height(thumb_file->thumbnail)); cairo_close_path(cr_arg); cairo_clip(cr_arg); cairo_paint(cr_arg); if(top_left_id + draw_now == selection_rank) { cairo_rectangle(cr_arg, 0, 0, cairo_image_surface_get_width(thumb_file->thumbnail), cairo_image_surface_get_height(thumb_file->thumbnail)); cairo_set_source_rgb(cr_arg, option_box_colors.bg_red, option_box_colors.bg_green, option_box_colors.bg_blue); cairo_set_line_width(cr_arg, 8.); cairo_stroke(cr_arg); } // Marks if(thumb_file->marked) { int markx = cairo_image_surface_get_width(thumb_file->thumbnail); int marky = cairo_image_surface_get_height(thumb_file->thumbnail); cairo_save(cr_arg); cairo_rectangle(cr_arg, markx - 5, marky - 5, markx + 1, marky + 1); cairo_set_source_rgb(cr_arg, 0, 0, 0); cairo_set_line_width(cr_arg, 1); cairo_stroke_preserve(cr_arg); cairo_set_source_rgb(cr_arg, 1, 0, 1); cairo_set_operator(cr_arg, CAIRO_OPERATOR_DIFFERENCE); cairo_fill(cr_arg); cairo_restore(cr_arg); } cairo_restore(cr_arg); } else if(top_left_id + draw_now == selection_rank) { cairo_save(cr_arg); cairo_translate(cr_arg, (main_window_width - n_thumbs_x * (option_thumbnails.width + 10)) / 2 + (draw_now % n_thumbs_x) * (option_thumbnails.width + 10) + (option_thumbnails.width - 5)/2, (main_window_height - n_thumbs_y * (option_thumbnails.height + 10)) / 2 + (draw_now / n_thumbs_x) * (option_thumbnails.height + 10) + (option_thumbnails.height - 5)/2 ); cairo_rectangle(cr_arg, 0, 0, 5, 5); cairo_set_source_rgb(cr_arg, option_box_colors.bg_red, option_box_colors.bg_green, option_box_colors.bg_blue); cairo_set_line_width(cr_arg, 8.); cairo_stroke(cr_arg); cairo_restore(cr_arg); } } #ifndef CONFIGURED_WITHOUT_ACTIONS // In follow mode, draw the key mappings on top of the images if(montage_window_control.show_binding_overlays) { const int selected_x = selection_rank % n_thumbs_x; const int selected_y = selection_rank / n_thumbs_x - montage_window_control.scroll_y; struct window_draw_thumbnail_montage_show_binding_overlays_data data = { cr_arg, selected_x, selected_y, (char*)"" }; g_hash_table_foreach( active_key_binding.key_binding && active_key_binding.key_binding->next_key_bindings ? active_key_binding.key_binding->next_key_bindings : key_bindings[active_key_binding_context], window_draw_thumbnail_montage_show_binding_overlays_looper, &data); } #endif D_UNLOCK(file_tree); return TRUE; }/*}}}*/ #endif void window_clear_background_pixmap() {/*{{{*/ if(wm_supports_moveresize) { // There's no need for that here. return; } #if defined(GDK_WINDOWING_X11) GdkScreen *screen = gdk_screen_get_default(); #if GTK_MAJOR_VERSION >= 3 if(!GDK_IS_X11_SCREEN(screen)) { return; } #endif Display *display = GDK_SCREEN_XDISPLAY(screen); GdkWindow *window = gtk_widget_get_window(GTK_WIDGET(main_window)); #if GTK_MAJOR_VERSION >= 3 unsigned long window_xid = gdk_x11_window_get_xid(window); #else unsigned long window_xid = GDK_WINDOW_XID(window); #endif XSetWindowBackground(display, window_xid, 0); #endif }/*}}}*/ void window_prerender_background_pixmap(int window_width, int window_height, double scale_level, gboolean fullscreen) {/*{{{*/ /* This function is for old X11 environments that do not support moveresize. One will typically see tearing effects there, because the time between resizing the window, pqiv receiving an expose event and actually drawing is to large to be unnoticable. This function resolves the issue by assigning a background pixmap to the window containing the new contents of the window. X11 will have something to display until the actual drawing pass is done, and things look better. The downside is that everything is drawn twice. This isn't a huge problem unless --low-memory is set, where, due to the disabled cache, the scaled image must be rendered twice. */ if(wm_supports_moveresize) { // There's no need for that here. return; } if(wm_ignores_size_requests) { // Tiling WM, do nothing return; } #if defined(GDK_WINDOWING_X11) GdkScreen *screen = gdk_screen_get_default(); #if GTK_MAJOR_VERSION >= 3 if(!GDK_IS_X11_SCREEN(screen)) { return; } #endif Display *display = GDK_SCREEN_XDISPLAY(screen); GdkWindow *window = gtk_widget_get_window(GTK_WIDGET(main_window)); #if GTK_MAJOR_VERSION >= 3 unsigned long window_xid = gdk_x11_window_get_xid(window); #else unsigned long window_xid = GDK_WINDOW_XID(window); #endif if(fullscreen_transition_source_id != -1) { // In progress of transitioning fullscreen state. Do nothing. XSetWindowBackground(display, window_xid, 0); return; } if(main_window_width == window_width && main_window_height == window_height) { // There will be no tearing, do nothing. XSetWindowBackground(display, window_xid, 0); return; } XWindowAttributes window_attributes; if(XGetWindowAttributes(display, window_xid, &window_attributes) == 0) { // Failure, abort. return; } const Screen *xscreen = window_attributes.screen; XVisualInfo visual_info_template = { .visualid = window_attributes.visual->visualid }; int visual_infos_found; XVisualInfo *visual_info = XGetVisualInfo(display, VisualIDMask, &visual_info_template, &visual_infos_found); Pixmap pixmap = XCreatePixmap(display, window_xid, window_width * screen_scale_factor, window_height * screen_scale_factor, visual_info ? visual_info->depth : xscreen->root_depth); cairo_surface_t *pixmap_surface = cairo_xlib_surface_create(display, pixmap, window_attributes.visual, window_width * screen_scale_factor, window_height * screen_scale_factor); int ow = main_window_width, oh = main_window_height; double osl = current_scale_level; gboolean ofs = main_window_in_fullscreen; main_window_width = window_width; main_window_height = window_height; current_scale_level = scale_level; main_window_in_fullscreen = fullscreen; #ifndef CONFIGURED_WITHOUT_INFO_TEXT current_info_text_cached_font_size = -1; #endif cairo_t *cr = cairo_create(pixmap_surface); cairo_save(cr); cairo_scale(cr, screen_scale_factor, screen_scale_factor); window_draw_callback(GTK_WIDGET(main_window), cr, GUINT_TO_POINTER(1)); cairo_restore(cr); /*cairo_set_source_rgba(cr, 1., 0, 0, .5); cairo_set_operator(cr, CAIRO_OPERATOR_OVERLAY); cairo_paint(cr);*/ cairo_surface_flush(cairo_get_target(cr)); cairo_destroy(cr); main_window_width = ow; main_window_height = oh; current_scale_level = osl; main_window_in_fullscreen = ofs; #ifndef CONFIGURED_WITHOUT_INFO_TEXT current_info_text_cached_font_size = -1; #endif XSetWindowBackgroundPixmap(display, window_xid, pixmap); XFreePixmap(display, pixmap); g_idle_add(window_show_background_pixmap_cb, NULL); #endif }/*}}}*/ gboolean window_show_background_pixmap_cb(gpointer user_data) {/*{{{*/ if(wm_supports_moveresize) { // There's no need for that here. return FALSE; } #if defined(GDK_WINDOWING_X11) GdkScreen *screen = gdk_screen_get_default(); #if GTK_MAJOR_VERSION >= 3 if(!GDK_IS_X11_SCREEN(screen)) { return FALSE; } #endif Display *display = GDK_SCREEN_XDISPLAY(screen); GdkWindow *window = gtk_widget_get_window(GTK_WIDGET(main_window)); #if GTK_MAJOR_VERSION >= 3 unsigned long window_xid = gdk_x11_window_get_xid(window); #else unsigned long window_xid = GDK_WINDOW_XID(window); #endif XClearWindow(display, window_xid); #endif return FALSE; }/*}}}*/ gboolean window_draw_callback(GtkWidget *widget, cairo_t *cr_arg, gpointer user_data) {/*{{{*/ // Drawing can generally mean that we succeeded in performing some action. // Resume the action queue action_done(); // We have different drawing modes. The default, below, is to draw a single // image. #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE if(application_mode == MONTAGE) { return window_draw_thumbnail_montage(cr_arg); } #endif // Draw image int x = 0; int y = 0; D_LOCK(file_tree); // We draw ignoring GDK_SCALE // Note that there also is cairo_surface_get_device_scale(). We // deliberately do not temper with that to keep the gtk <-> pqiv // coordinate transformations consistent in all places. cairo_scale(cr_arg, 1./screen_scale_factor, 1./screen_scale_factor); if(is_current_file_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 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 surface contains the image as it is displayed on the // screen later, with all transformations applied. cairo_surface_t *temporary_surface = cairo_surface_create_similar(cairo_get_target(cr_arg), CAIRO_CONTENT_COLOR_ALPHA, main_window_width, main_window_height); cairo_t *cr = NULL; if(cairo_surface_status(temporary_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 cairo_save(cr_arg); cr = cr_arg; cairo_surface_destroy(temporary_surface); temporary_surface = NULL; } else { cr = cairo_create(temporary_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. unsigned skip_px = (unsigned)(1./current_scale_level); if(skip_px == 0) { skip_px = 1; } if(CURRENT_FILE->width > 2*skip_px && CURRENT_FILE->height > 2*skip_px) { cairo_rectangle(cr, skip_px, skip_px, CURRENT_FILE->width - 2*skip_px, CURRENT_FILE->height - 2*skip_px); } else { cairo_rectangle(cr, 0, 0, CURRENT_FILE->width, CURRENT_FILE->height); } cairo_close_path(cr); cairo_clip(cr); if(option_background_pattern == CHECKERBOARD) { cairo_set_source(cr, background_checkerboard_pattern); } else if(option_background_pattern == WHITE) { cairo_set_source_rgba(cr, 1., 1., 1., 1.); } else { cairo_set_source_rgba(cr, 0., 0., 0., 1.); } cairo_paint(cr); cairo_restore(cr); } // Draw the scaled image. if(option_negate) { // Negated color mode: The drawing operation is more complex; to do // alpha channels correctly we _need_ to have a image surface copy // of the image, regardless of lowmem mode. So this drawing mode comes // before the option_lowmem special case. cairo_surface_t *temporary_scaled_image_surface = get_scaled_image_surface_for_current_image(); cairo_save(cr); // Draw white using the image's alpha channel as a mask. // Note that cairo_mask_surface already paints, despite the name. cairo_set_source_rgb(cr, 1., 1., 1.); cairo_mask_surface(cr, temporary_scaled_image_surface, 0, 0); cairo_restore(cr); // Now take the difference to the image: This will invert the colors. cairo_save(cr); cairo_set_operator(cr, CAIRO_OPERATOR_DIFFERENCE); cairo_set_source_surface(cr, temporary_scaled_image_surface, 0, 0); cairo_paint(cr); cairo_restore(cr); cairo_surface_destroy(temporary_scaled_image_surface); } else 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, CURRENT_FILE->width + 0.5, CURRENT_FILE->height + 0.5); 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 movement/redraws of 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. && fading_surface != NULL) { cairo_set_source_surface(cr_arg, fading_surface, 0, 0); cairo_set_operator(cr_arg, CAIRO_OPERATOR_SOURCE); cairo_paint(cr_arg); cairo_set_source_surface(cr_arg, temporary_surface, 0, 0); cairo_paint_with_alpha(cr_arg, fading_current_alpha_stage); // 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_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_surface != NULL) { cairo_surface_destroy(last_visible_surface); } if(!option_lowmem || option_fading) { last_visible_surface = temporary_surface; last_visible_surface_width = main_window_width; last_visible_surface_height = main_window_height; } else { cairo_surface_destroy(temporary_surface); last_visible_surface = NULL; } } else { cairo_restore(cr_arg); if(last_visible_surface) { cairo_surface_destroy(last_visible_surface); last_visible_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_surface != NULL) { // But only do it if the window size hasn't changed. It looks weird // to have an image drawn somewhere into the window. // TODO An overall neater solution would be to have // last_visible_surface store only the image part, and do the // centering here. if(last_visible_surface_width != main_window_width || last_visible_surface_height != main_window_height) { cairo_surface_destroy(last_visible_surface); last_visible_surface = NULL; } else { cairo_set_source_surface(cr_arg, last_visible_surface, 0, 0); cairo_set_operator(cr_arg, CAIRO_OPERATOR_SOURCE); cairo_paint(cr_arg); } } else { // Draw black background // This must be done explicitly in GTK2, otherwise the background will be white. cairo_save(cr_arg); cairo_set_source_rgba(cr_arg, 0., 0., 0., option_transparent_background ? 0. : 1.); cairo_set_operator(cr_arg, CAIRO_OPERATOR_SOURCE); cairo_paint(cr_arg); cairo_restore(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 = 0., x2 = 0., y1 = 0., y2 = 0.; cairo_save(cr_arg); // Attempt this multiple times: If it does not fit the window, // retry with a smaller font size int font_size; if(current_info_text_cached_font_size < 0) { font_size = 12*screen_scale_factor; current_info_text_cached_font_size = 0; } else { font_size = current_info_text_cached_font_size; } for(; 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, option_box_colors.bg_red, option_box_colors.bg_green, option_box_colors.bg_blue); cairo_translate(cr_arg, 10 * screen_scale_factor, 20 * screen_scale_factor); cairo_text_path(cr_arg, current_info_text); cairo_path_extents(cr_arg, &x1, &y1, &x2, &y2); if(x2 > main_window_width - 10 * screen_scale_factor && !main_window_in_fullscreen) { cairo_new_path(cr_arg); cairo_restore(cr_arg); cairo_save(cr_arg); continue; } current_info_text_cached_font_size = font_size; cairo_path_t *text_path = cairo_copy_path(cr_arg); 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, option_box_colors.fg_red, option_box_colors.fg_green, option_box_colors.fg_blue); 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 // TODO Maybe this will need to be changed someday; the GDK Wayland backend // currently does not draw window borders if the draw callback reports // success. Anyway, it also draws the borders at the wrong place (well // within the window rather than around it), so I'll leave things as they // are for the time being. 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 double calculate_scale_level_to_fit(int image_width, int image_height, int window_width, int window_height) {/*{{{*/ if(scale_override || option_scale == FIXED_SCALE) { return current_scale_level; } // Calculate display width/heights with rotation, but without scaling, applied gdouble 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 * scale_level < window_width) { scale_level = window_width * 1.0 / image_width; } if(image_height * scale_level < window_height) { scale_level = window_height * 1.0 / image_height; } } // Scale down if(window_height < scale_level * image_height) { scale_level = window_height * 1.0 / image_height; } if(window_width < scale_level * image_width) { scale_level = window_width * 1.0 / image_width; } } else if(option_scale == SCALE_TO_FIT_PX) { scale_level = fmin(scale_to_fit_size.width * 1. / image_width, scale_to_fit_size.height * 1. / image_height); } else if(option_scale == SCALE_TO_FIT_WINDOW) { scale_level = fmin(window_width * 1. / image_width, window_height * 1. / image_height); } return scale_level; }/*}}}*/ double calculate_auto_scale_level_for_screen(int image_width, int image_height) {/*{{{*/ double scale_level = current_scale_level; if(!main_window_in_fullscreen) { const int screen_width = screen_geometry.width; const int screen_height = screen_geometry.height; if(option_scale != FIXED_SCALE && !scale_override) { scale_level = 1.0; if(option_scale == AUTO_SCALEUP) { // Scale up to screen size scale_level = screen_width * option_scale_screen_fraction / image_width; } else if(option_scale == SCALE_TO_FIT_PX) { scale_level = fmin(scale_to_fit_size.width * 1. / image_width, scale_to_fit_size.height * 1. / image_height); } else if(option_scale == SCALE_TO_FIT_WINDOW) { scale_level = fmin(main_window_width * 1. / image_width, main_window_height * 1. / image_height); } else if(option_scale == AUTO_SCALEDOWN && image_width > screen_width * option_scale_screen_fraction) { // Scale down to screen size scale_level = screen_width * option_scale_screen_fraction / image_width; } if((option_scale == AUTO_SCALEUP || option_scale == AUTO_SCALEDOWN) && image_height * scale_level > screen_height * option_scale_screen_fraction) { // If the height exceeds screen size, scale down scale_level = screen_height * option_scale_screen_fraction / image_height; } } } else { scale_level = calculate_scale_level_to_fit(image_width, image_height, screen_geometry.width, screen_geometry.height); } return scale_level; }/*}}}*/ void set_scale_level_for_screen() {/*{{{*/ if(!current_file_node) { return; } 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); current_scale_level = calculate_auto_scale_level_for_screen(image_width, image_height); } 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 option_scale_screen_fraction limitation, // 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(is_current_file_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); double new_scale_level = calculate_scale_level_to_fit(image_width, image_height, main_window_width, main_window_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 set_cursor_auto_hide_mode(int auto_hide) {/*{{{*/ cursor_auto_hide_mode_enabled = auto_hide; if(!main_window) { return; } // We only enable the motion mask when it is absolutely necessary, because // communication with X11 becomes quite expensive when it is active: Every // movement of the mouse will trigger an event. The callback disables the // event once it is invoked - instead, a callback will query the mouse // position some time later to see if it changed. // // In theory, there is GDK_POINTER_MOTION_HINT_MASK to achieve this. // However, it is completely broken, according to my experiments and other // people's reports from both the GTK mailing list and Bugzilla. if(cursor_auto_hide_mode_enabled) { gtk_widget_add_events(GTK_WIDGET(main_window), GDK_POINTER_MOTION_MASK); } else { if(gtk_widget_get_realized(GTK_WIDGET(main_window))) { GdkWindow *window = gtk_widget_get_window(GTK_WIDGET(main_window)); gdk_window_set_events(window, gdk_window_get_events(window) & ~GDK_POINTER_MOTION_MASK); } else { gtk_widget_set_events(GTK_WIDGET(main_window), gtk_widget_get_events(GTK_WIDGET(main_window)) & ~GDK_POINTER_MOTION_MASK); } } if(cursor_auto_hide_timer_id) { g_source_remove(cursor_auto_hide_timer_id); } cursor_auto_hide_timer_id = g_idle_add(window_auto_hide_cursor_callback, NULL); }/*}}}*/ #ifndef CONFIGURED_WITHOUT_ACTIONS key_binding_t *key_binding_t_duplicate(key_binding_t *binding) {/*{{{*/ key_binding_t *retval = g_slice_new(key_binding_t); retval->action = binding->action; retval->parameter = binding->parameter; if(pqiv_action_descriptors[binding->action].parameter_type == PARAMETER_CHARPTR) { retval->parameter.pcharptr = g_strdup(retval->parameter.pcharptr); } retval->next_action = binding->next_action ? key_binding_t_duplicate(binding->next_action) : NULL; retval->next_key_bindings = binding->next_key_bindings ? g_hash_table_ref(binding->next_key_bindings) : NULL; return retval; }/*}}}*/ void key_binding_t_destroy_callback(gpointer data) {/*{{{*/ key_binding_t *binding = (key_binding_t *)data; if(binding->next_action) { key_binding_t_destroy_callback(binding->next_action); } if(pqiv_action_descriptors[binding->action].parameter_type == PARAMETER_CHARPTR) { g_free(binding->parameter.pcharptr); } if(binding->next_key_bindings) { g_hash_table_unref(binding->next_key_bindings); } g_slice_free(key_binding_t, binding); }/*}}}*/ gboolean queue_action_callback(gpointer user_data) {/*{{{*/ key_binding_t *binding = g_queue_pop_head(&action_queue); /* Reset action_queue_idle_id here because action() might want to add a new idle callback. */ action_queue_idle_id = -1; if(!binding) { return FALSE; } // Debug: printf("Queue length is %d. Now at: ", g_queue_get_length(&action_queue) + 1); help_show_single_action(binding); printf("\n"); action(binding->action, binding->parameter); key_binding_t_destroy_callback(binding); return FALSE; }/*}}}*/ void queue_action(pqiv_action_t action_id, pqiv_action_parameter_t parameter) {/*{{{*/ key_binding_t temporary_binding = { .action = action_id, .parameter = parameter, .next_action = NULL, .next_key_bindings = NULL }; g_queue_push_tail(&action_queue, key_binding_t_duplicate(&temporary_binding)); // Debug: printf("Queue length is %d after adding: ", g_queue_get_length(&action_queue)); help_show_single_action(&temporary_binding); printf("\n"); if(action_queue_idle_id == -1) { action_queue_idle_id = g_idle_add(queue_action_callback, NULL); } }/*}}}*/ void queue_action_from_binding(key_binding_t *binding) {/*{{{*/ while(binding) { queue_action(binding->action, binding->parameter); binding = binding->next_action; } }/*}}}*/ #endif void UNUSED_FUNCTION action_done() {/*{{{*/ #ifndef CONFIGURED_WITHOUT_ACTIONS if(!g_queue_is_empty(&action_queue) && action_queue_idle_id == -1) { action_queue_idle_id = g_idle_add(queue_action_callback, NULL); } #endif }/*}}}*/ void action(pqiv_action_t action_id, pqiv_action_parameter_t parameter) {/*{{{*/ switch(action_id) { case ACTION_NOP: break; case ACTION_SHIFT_Y: if(!main_window_visible) return; if(!is_current_file_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(!is_current_file_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); } UPDATE_INFO_TEXT("Slideshow interval set to %d seconds", (int)option_slideshow_interval); info_text_queue_redraw(); break; case ACTION_SET_SCALE_LEVEL_RELATIVE: case ACTION_SET_SCALE_LEVEL_ABSOLUTE: if(!is_current_file_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(); image_generate_prerendered_view(CURRENT_FILE, FALSE, current_scale_level); 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); // Required to avoid tearing requested_main_window_width = current_scale_level * image_width; requested_main_window_height = current_scale_level * image_height; window_prerender_background_pixmap(requested_main_window_width, requested_main_window_height, current_scale_level, main_window_in_fullscreen); gtk_window_resize(main_window, current_scale_level * image_width / screen_scale_factor, current_scale_level * image_height / screen_scale_factor); if(!wm_supports_moveresize) { queue_draw(); } } update_info_text(NULL); break; case ACTION_TOGGLE_SCALE_MODE: if(!is_current_file_loaded()) return; if(parameter.pint == 0) { if(++option_scale > AUTO_SCALEUP) { option_scale = NO_SCALING; } } else { option_scale = (parameter.pint - 1) % 5; } scale_override = FALSE; current_image_drawn = FALSE; current_shift_x = 0; current_shift_y = 0; invalidate_current_scaled_image_surface(); set_scale_level_for_screen(); main_window_adjust_for_image(); 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; case SCALE_TO_FIT_WINDOW: update_info_text("Maintaining window size"); break; default: break; } break; case ACTION_SET_SCALE_MODE_SCREEN_FRACTION: if(parameter.pdouble <= 0) { g_printerr("Invalid parameter for set_scale_mode_screen_fraction()\n"); break; } option_scale_screen_fraction = parameter.pdouble; scale_override = FALSE; current_image_drawn = FALSE; current_shift_x = 0; current_shift_y = 0; invalidate_current_scaled_image_surface(); image_generate_prerendered_view(CURRENT_FILE, FALSE, -1); set_scale_level_for_screen(); main_window_adjust_for_image(); gtk_widget_queue_draw(GTK_WIDGET(main_window)); 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"); info_text_queue_redraw(); break; case ACTION_RELOAD: if(!is_current_file_loaded()) return; CURRENT_FILE->force_reload = TRUE; update_info_text("Reloading image.."); info_text_queue_redraw(); D_LOCK(file_tree); queue_image_load(bostree_node_weak_ref(relative_image_pointer(0))); D_UNLOCK(file_tree); return; break; case ACTION_RESET_SCALE_LEVEL: if(!is_current_file_loaded()) return; current_image_drawn = FALSE; scale_override = FALSE; invalidate_current_scaled_image_surface(); image_generate_prerendered_view(CURRENT_FILE, FALSE, -1); set_scale_level_for_screen(); main_window_adjust_for_image(); gtk_widget_queue_draw(GTK_WIDGET(main_window)); update_info_text(NULL); break; case ACTION_TOGGLE_FULLSCREEN: if(parameter.pint == 1 || (parameter.pint == 0 && main_window_in_fullscreen == FALSE)) { window_fullscreen(); } else { window_unfullscreen(); } return; break; case ACTION_FLIP_HORIZONTALLY: if(!is_current_file_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(!is_current_file_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(!is_current_file_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(!is_current_file_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: if(!is_current_file_loaded()) return; 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"); } info_text_queue_redraw(); break; case ACTION_HARDLINK_CURRENT_IMAGE: if(!is_current_file_loaded()) return; hardlink_current_image(); gtk_widget_queue_draw(GTK_WIDGET(main_window)); break; case ACTION_GOTO_DIRECTORY_RELATIVE: directory_image_movement(parameter.pint, FALSE); return; break; case ACTION_GOTO_LOGICAL_DIRECTORY_RELATIVE: directory_image_movement(parameter.pint, TRUE); return; break; case ACTION_GOTO_FILE_RELATIVE: relative_image_movement(parameter.pint); return; break; case ACTION_QUIT: if(!main_window_visible) { gtk_main_quit(); } else { gtk_widget_destroy(GTK_WIDGET(main_window)); } return; break; #ifndef CONFIGURED_WITHOUT_EXTERNAL_COMMANDS case ACTION_NUMERIC_COMMAND: { if(parameter.pint < 1 || parameter.pint > 9) { g_printerr("Only commands 1..9 are supported.\n"); return; } gchar *command = external_image_filter_commands[parameter.pint - 1]; pqiv_action_parameter_t action_parameter = { .pcharptr = command }; action(ACTION_COMMAND, action_parameter); return; } break; case ACTION_COMMAND: if(!is_current_file_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"); info_text_queue_redraw(); } else { UPDATE_INFO_TEXT("Executing command %s", command); info_text_queue_redraw(); 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) { if(application_mode == DEFAULT) { absolute_image_movement(node); } #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE else if(application_mode == MONTAGE) { D_LOCK(file_tree); if(montage_window_control.selected_node != NULL) { bostree_node_weak_unref(file_tree, montage_window_control.selected_node); } montage_window_control.selected_node = node; montage_window_move_cursor(0, 0, 0); D_UNLOCK(file_tree); gtk_widget_queue_draw(GTK_WIDGET(main_window)); } #endif } 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) { if(application_mode == DEFAULT) { absolute_image_movement(node); } #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE else if(application_mode == MONTAGE) { D_LOCK(file_tree); if(montage_window_control.selected_node != NULL) { bostree_node_weak_unref(file_tree, montage_window_control.selected_node); } montage_window_control.selected_node = node; montage_window_move_cursor(0, 0, 0); D_UNLOCK(file_tree); gtk_widget_queue_draw(GTK_WIDGET(main_window)); } #endif } 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_PX; scale_to_fit_size.width = parameter.p2short.p1; scale_to_fit_size.height = parameter.p2short.p2; invalidate_current_scaled_image_surface(); set_scale_level_for_screen(); main_window_adjust_for_image(); UPDATE_INFO_TEXT("Scale level adjusted to fit %dx%d px", scale_to_fit_size.width, scale_to_fit_size.height); gtk_widget_queue_draw(GTK_WIDGET(main_window)); break; case ACTION_SET_SHIFT_X: if(!is_current_file_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(!is_current_file_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; case ACTION_SET_INTERPOLATION_QUALITY: if(parameter.pint > BEST + 1 || parameter.pint < 0) { g_printerr("Interpolation quality `%d' not supported.\n", parameter.pint); } else if(parameter.pint == 0) { if(++option_interpolation_quality > BEST) { option_interpolation_quality = AUTO; } } else { option_interpolation_quality = parameter.pint - 1; } switch(option_interpolation_quality) { case AUTO: update_info_text("Interpolation quality set to auto-determine."); break; case FAST: update_info_text("Interpolation quality set to fast."); break; case GOOD: update_info_text("Interpolation quality set to good."); break; case BEST: update_info_text("Interpolation quality set to best."); break; } invalidate_current_scaled_image_surface(); gtk_widget_queue_draw(GTK_WIDGET(main_window)); break; case ACTION_ANIMATION_STEP: if(!(CURRENT_FILE->file_flags & FILE_FLAGS_ANIMATION)) { break; } if(current_image_animation_timeout_id > 0) { g_source_remove(current_image_animation_timeout_id); current_image_animation_timeout_id = 0; } current_image_animation_speed_scale = 0; D_LOCK(file_tree); if(parameter.pint > 0 && CURRENT_FILE->file_type->animation_next_frame_fn != NULL) { // Skip all but one frame here, the last frame progression // happens in image_animation_timeout_callback for(int i = 0; i < parameter.pint - 1; i++) { CURRENT_FILE->file_type->animation_next_frame_fn(CURRENT_FILE); } } D_UNLOCK(file_tree); image_animation_timeout_callback(current_file_node); update_info_text(NULL); break; case ACTION_ANIMATION_CONTINUE: if(!(CURRENT_FILE->file_flags & FILE_FLAGS_ANIMATION)) { break; } current_image_animation_speed_scale = 1.0; if(current_image_animation_timeout_id == 0 && (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_info_text(NULL); break; case ACTION_ANIMATION_SET_SPEED_ABSOLUTE: if(!(CURRENT_FILE->file_flags & FILE_FLAGS_ANIMATION)) { break; } current_image_animation_speed_scale = parameter.pdouble; if(current_image_animation_speed_scale < 0) { current_image_animation_speed_scale = 0; } UPDATE_INFO_TEXT("Animation speed adjusted to %03.1f%%", current_image_animation_speed_scale * 100.); info_text_queue_redraw(); break; case ACTION_ANIMATION_SET_SPEED_RELATIVE: if(!(CURRENT_FILE->file_flags & FILE_FLAGS_ANIMATION)) { break; } current_image_animation_speed_scale *= parameter.pdouble; if(current_image_animation_speed_scale < 0) { current_image_animation_speed_scale = 0; } UPDATE_INFO_TEXT("Animation speed adjusted to %03.1f%%", current_image_animation_speed_scale * 100.); info_text_queue_redraw(); break; case ACTION_GOTO_EARLIER_FILE: if(earlier_file_node != NULL) { absolute_image_movement(bostree_node_weak_ref(earlier_file_node)); } break; case ACTION_SET_CURSOR_AUTO_HIDE: set_cursor_auto_hide_mode(!!parameter.pint); break; case ACTION_SET_FADE_DURATION: option_fading_duration = parameter.pdouble; option_fading = option_fading_duration > 0; UPDATE_INFO_TEXT("Fade duration adjusted to %2.2f seconds", option_fading_duration); info_text_queue_redraw(); break; case ACTION_SET_KEYBOARD_TIMEOUT: option_keyboard_timeout = parameter.pdouble; break; #endif #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE case ACTION_SET_THUMBNAIL_SIZE: option_thumbnails.width = parameter.p2short.p1; option_thumbnails.height = parameter.p2short.p2; if(application_mode == MONTAGE) { abort_pending_image_loads(NULL); } D_LOCK(file_tree); for(BOSNode *node = bostree_select(file_tree, 0); node; node = bostree_next_node(node)) { if(FILE(node)->thumbnail) { cairo_surface_destroy(FILE(node)->thumbnail); FILE(node)->thumbnail = NULL; } } D_UNLOCK(file_tree); if(application_mode == MONTAGE) { D_LOCK(file_tree); montage_window_move_cursor(0, 0, 0); D_UNLOCK(file_tree); gtk_widget_queue_draw(GTK_WIDGET(main_window)); } break; case ACTION_SET_THUMBNAIL_PRELOAD: option_thumbnails.enabled = parameter.pint > 0; option_thumbnails.auto_generate_for_adjacents = parameter.pint; if(parameter.pint > 0) { preload_adjacent_images(); UPDATE_INFO_TEXT("Thumbnail generation enabled for %d adjacent images", parameter.pint); } else { update_info_text("Thumbnail generation disabled"); } info_text_queue_redraw(); break; case ACTION_MONTAGE_MODE_ENTER: if(slideshow_timeout_id > 0) { g_source_remove(slideshow_timeout_id); slideshow_timeout_id = 0; } if(current_image_animation_timeout_id > 0) { g_source_remove(current_image_animation_timeout_id); current_image_animation_timeout_id = 0; } if(last_visible_surface) { cairo_surface_destroy(last_visible_surface); last_visible_surface = NULL; } invalidate_current_scaled_image_surface(); application_mode = MONTAGE; active_key_binding_context = MONTAGE; D_LOCK(file_tree); if(montage_window_control.selected_node) { bostree_node_weak_unref(file_tree, montage_window_control.selected_node); } montage_window_control.selected_node = bostree_node_weak_ref(current_file_node); montage_window_move_cursor(0, 0, 0); D_UNLOCK(file_tree); update_info_text(NULL); main_window_adjust_for_image(); set_cursor_auto_hide_mode(TRUE); gtk_widget_queue_draw(GTK_WIDGET(main_window)); return; break; case ACTION_MONTAGE_MODE_SHIFT_X: if(application_mode != MONTAGE) { break; } D_LOCK(file_tree); montage_window_move_cursor(parameter.pint, 0, 0); D_UNLOCK(file_tree); gtk_widget_queue_draw(GTK_WIDGET(main_window)); break; case ACTION_MONTAGE_MODE_SHIFT_Y: if(application_mode != MONTAGE) { break; } D_LOCK(file_tree); montage_window_move_cursor(0, parameter.pint, 0); D_UNLOCK(file_tree); gtk_widget_queue_draw(GTK_WIDGET(main_window)); break; case ACTION_MONTAGE_MODE_SET_WRAP_MODE: if(parameter.pint < 0 || parameter.pint >= _MONTAGE_MODE_WRAP_SENTINEL) { g_printerr("Invalid parameter for montage_mode_set_wrap_mode()\n"); break; } option_montage_mode_wrap_mode = parameter.pint; break; case ACTION_MONTAGE_MODE_SET_SHIFT_X: if(application_mode != MONTAGE) { break; } montage_window_set_cursor(parameter.pint, -1); gtk_widget_queue_draw(GTK_WIDGET(main_window)); break; case ACTION_MONTAGE_MODE_SET_SHIFT_Y: if(application_mode != MONTAGE) { break; } montage_window_set_cursor(-1, parameter.pint); gtk_widget_queue_draw(GTK_WIDGET(main_window)); break; case ACTION_MONTAGE_MODE_SHIFT_Y_PG: if(application_mode != MONTAGE) { break; } D_LOCK(file_tree); montage_window_move_cursor(0, 0, parameter.pint); D_UNLOCK(file_tree); gtk_widget_queue_draw(GTK_WIDGET(main_window)); break; case ACTION_MONTAGE_MODE_SHOW_BINDING_OVERLAYS: montage_window_control.show_binding_overlays = !!parameter.pint; gtk_widget_queue_draw(GTK_WIDGET(main_window)); break; #ifndef CONFIGURED_WITHOUT_ACTIONS case ACTION_MONTAGE_MODE_FOLLOW: if(application_mode != MONTAGE) { break; } if(parameter.pcharptr[0] == 0 || parameter.pcharptr[1] == 0) { g_printerr("Error: montage_mode_follow requires at least two characters to work with.\n"); break; } montage_window_control.show_binding_overlays = 1; option_keyboard_timeout = 5; if(follow_mode_key_binding.next_key_bindings) { g_hash_table_unref(follow_mode_key_binding.next_key_bindings); follow_mode_key_binding.next_key_bindings = NULL; } follow_mode_key_binding.next_key_bindings = g_hash_table_new_full((GHashFunc)g_direct_hash, (GEqualFunc)g_direct_equal, NULL, key_binding_t_destroy_callback); { const int n_thumbs_x = main_window_width / (option_thumbnails.width + 10); const int n_thumbs_y = main_window_height / (option_thumbnails.height + 10); const ptrdiff_t number_of_images = (ptrdiff_t)bostree_node_count(file_tree); const ptrdiff_t visible_thumbnails = ((montage_window_control.scroll_y + n_thumbs_y) * n_thumbs_x > number_of_images ? number_of_images - montage_window_control.scroll_y * n_thumbs_x : n_thumbs_x * n_thumbs_y); const int number_of_characters = strlen(parameter.pcharptr); /* On the algorithm used for generating follow mode key bindings: The problem of finding a prefix code in base m (I have m keys) for integers up to n (I have n integers) such that the total length of the encoding of the entire alphabet is minimized can be solved easily by looking at the base m representation of a number. Let l = log(n) / log(m). Observe how the problem is trivial if l is integer: Represent all images by their index in base m with leading "zeros" (that is, the first key). And you're done, there can't be a better representation. The nontrivial case is if we are between two powers of the basis. Let's look in detail at that case: Let k = m^(floor(l))-1; k is the largest number which can be represented using l-1 symbols, and the largest one that has a leading zero which could be omitted. The issue obviously is that this would destroy the prefix property. But note the following: If the leading digit of n in base m is d 1) { memset(key_sequence, 1, binding_length); if(high_image_digit > 0) { key_sequence[binding_length-1] = 0; } } else { key_sequence[0] = 1; } key_sequence[binding_length] = 0; // Walk through the grid for(short y=0; y= visible_thumbnails) { break; } // Now just bind the goto (x,y) command to the sequence in key_sequence key_binding_t *active_binding = &follow_mode_key_binding; int binding_pos; for(binding_pos=0; binding_posnext_key_bindings, GUINT_TO_POINTER(key_binding_value)); if(!binding) { binding = g_slice_new(key_binding_t); binding->action = ACTION_MONTAGE_MODE_FOLLOW_PROCEED; binding->parameter.p2short.p1 = -1; binding->parameter.p2short.p2 = -1; binding->next_action = NULL; binding->next_key_bindings = g_hash_table_new_full((GHashFunc)g_direct_hash, (GEqualFunc)g_direct_equal, NULL, key_binding_t_destroy_callback); g_hash_table_insert(active_binding->next_key_bindings, GUINT_TO_POINTER(key_binding_value), binding); } else if(!binding->next_key_bindings) { binding->next_key_bindings = g_hash_table_new_full((GHashFunc)g_direct_hash, (GEqualFunc)g_direct_equal, NULL, key_binding_t_destroy_callback); } active_binding = binding; } key_binding_t *binding = g_slice_new0(key_binding_t); binding->action = ACTION_MONTAGE_MODE_FOLLOW_PROCEED; binding->parameter.p2short.p1 = x; binding->parameter.p2short.p2 = y; // We bind the continuation of the current action chain // as the next action. if(active_key_binding.key_binding) { binding->next_action = key_binding_t_duplicate(active_key_binding.key_binding); } guint key_binding_value = KEY_BINDING_VALUE(0, 0, parameter.pcharptr[key_sequence[binding_pos] - 1]); g_hash_table_insert(active_binding->next_key_bindings, GUINT_TO_POINTER(key_binding_value), binding); // Increase the key_sequence "number" int pos = binding_length; while(key_sequence[pos] == 0) { pos--; } while((++key_sequence[pos]) > number_of_characters) { key_sequence[pos] = 1; pos--; } if(pos == 0 && key_sequence[0] - 1 == high_image_digit) { // We have transitioned into the regime starting // from which we need to add another digit. memset(key_sequence + 1, 1, binding_length - 1); } } } } active_key_binding.key_binding = &follow_mode_key_binding; active_key_binding.associated_image = current_file_node; active_key_binding.timeout_id = gdk_threads_add_timeout((size_t)(option_keyboard_timeout * 1000), handle_input_event_timeout_callback, NULL); gtk_widget_queue_draw(GTK_WIDGET(main_window)); return; break; case ACTION_MONTAGE_MODE_FOLLOW_PROCEED: if(application_mode != MONTAGE) { break; } option_keyboard_timeout = .5; montage_window_control.show_binding_overlays = 0; if(parameter.p2short.p1 >= 0 || parameter.p2short.p2 >= 0) { montage_window_set_cursor(parameter.p2short.p1, parameter.p2short.p2); } gtk_widget_queue_draw(GTK_WIDGET(main_window)); if(follow_mode_key_binding.next_key_bindings) { g_hash_table_unref(follow_mode_key_binding.next_key_bindings); follow_mode_key_binding.next_key_bindings = NULL; } active_key_binding.key_binding = NULL; return; break; #endif // without actions case ACTION_MONTAGE_MODE_RETURN_PROCEED: case ACTION_MONTAGE_MODE_RETURN_CANCEL: if(application_mode != MONTAGE) { break; } application_mode = DEFAULT; active_key_binding_context = DEFAULT; main_window_adjust_for_image(); gtk_widget_queue_draw(GTK_WIDGET(main_window)); if(main_window_in_fullscreen) { window_hide_cursor(); set_cursor_auto_hide_mode(FALSE); } D_LOCK(file_tree); BOSNode *target; if(action_id == ACTION_MONTAGE_MODE_RETURN_PROCEED) { target = montage_window_control.selected_node; montage_window_control.selected_node = NULL; } else if(current_file_node) { target = bostree_node_weak_ref(current_file_node); bostree_node_weak_unref(file_tree, montage_window_control.selected_node); montage_window_control.selected_node = NULL; } else { bostree_node_weak_unref(file_tree, montage_window_control.selected_node); montage_window_control.selected_node = NULL; if(!file_tree_valid) { break; } target = bostree_select(file_tree, 0); if(!target) { break; } target = bostree_node_weak_ref(target); } D_UNLOCK(file_tree); absolute_image_movement(target); if(option_lowmem) { D_LOCK(file_tree); // TODO // This currently is a linear search. Given that most users requiring lowmem mode will // probably not have many images loaded, this might suffice. But an asymptotically better // approach would be neat. for(BOSNode *node = bostree_select(file_tree, 0); node; node = bostree_next_node(node)) { if(FILE(node)->thumbnail) { cairo_surface_destroy(FILE(node)->thumbnail); FILE(node)->thumbnail = NULL; } } D_UNLOCK(file_tree); } update_info_text(NULL); return; break; #endif // without montage case ACTION_MOVE_WINDOW: if(!main_window_in_fullscreen) { if(parameter.p2short.p1 < 0) { parameter.p2short.p1 = screen_geometry.x + (screen_geometry.width - main_window_width) / 2; } if(parameter.p2short.p2 < 0) { parameter.p2short.p2 = screen_geometry.y + (screen_geometry.height - main_window_height) / 2; } gtk_window_move(main_window, parameter.p2short.p1, parameter.p2short.p2); } break; case ACTION_TOGGLE_BACKGROUND_PATTERN: if(parameter.pint == 0) { option_background_pattern++; } else { option_background_pattern = parameter.pint - 1; } if(option_background_pattern > WHITE || option_background_pattern < CHECKERBOARD) { option_background_pattern = CHECKERBOARD; } UPDATE_INFO_TEXT("Background pattern set to %s", option_background_pattern == BLACK ? "black" : option_background_pattern == WHITE ? "white" : "checkerboard"); gtk_widget_queue_draw(GTK_WIDGET(main_window)); break; case ACTION_TOGGLE_NEGATE_MODE: if(parameter.pint == 0) { option_negate = !option_negate; } else { option_negate = parameter.pint - 2; } UPDATE_INFO_TEXT("Negate mode %s", option_negate ? "enabled" : "disabled"); gtk_widget_queue_draw(GTK_WIDGET(main_window)); break; #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE case ACTION_MONTAGE_MODE_SHIFT_Y_ROWS: if(application_mode != MONTAGE) { break; } D_LOCK(file_tree); int old_scroll_y = montage_window_control.scroll_y; montage_window_move_cursor(0, parameter.pint, 0); montage_window_control.scroll_y = old_scroll_y + parameter.pint; if(montage_window_control.scroll_y < 0) { montage_window_control.scroll_y = 0; } montage_window_move_cursor(0, 0, 0); D_UNLOCK(file_tree); gtk_widget_queue_draw(GTK_WIDGET(main_window)); break; #endif // without montage #ifndef CONFIGURED_WITHOUT_EXTERNAL_COMMANDS case ACTION_TOGGLE_MARK: toggle_mark(); break; case ACTION_CLEAR_MARKS: clear_marks(); break; #endif // without external commands default: break; } // The current action is done, and the function wasn't explicitly returned // from. Issue the next action, if one's in the queue, to be run. action_done(); }/*}}}*/ 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; }; */ // Revert scale factor if(screen_scale_factor != 1) { event->x *= screen_scale_factor; event->y *= screen_scale_factor; event->width *= screen_scale_factor; event->height *= screen_scale_factor; } static gint old_window_x, old_window_y; if(old_window_x != event->x || old_window_y != event->y) { 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); } } // Check whether the WM completely ignored our size request to detect tiling WMs if(requested_main_window_width >= 0) { wm_ignores_size_requests = !(abs(event->width - requested_main_window_width) < 3 && abs(event->height - requested_main_window_height) < 3); requested_main_window_width = -1; } if(wm_ignores_size_requests || (main_window_width != event->width || main_window_height != event->height)) { // Reset cached font size for info text #ifndef CONFIGURED_WITHOUT_INFO_TEXT current_info_text_cached_font_size = -1; #endif // 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; } // If the fullscreen state just changed execute the post-change callbacks here if(fullscreen_transition_source_id >= 0) { g_source_remove(fullscreen_transition_source_id); if(main_window_in_fullscreen) { window_state_into_fullscreen_actions(main_window); } else { window_state_out_of_fullscreen_actions(main_window); } } // Rescale the image if(main_window_width != event->width || main_window_height != event->height) { 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 } #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE if(application_mode == MONTAGE) { // Make sure that the currently selected image stays in view & that all // visible thumbnails are loaded D_LOCK(file_tree); montage_window_move_cursor(0, 0, 0); D_UNLOCK(file_tree); } #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 void handle_input_event(guint key_binding_value) {/*{{{*/ /* Debug char *debug_keybinding = key_binding_sequence_to_string(key_binding_value, NULL); printf("You pressed %s\n", debug_keybinding); free(debug_keybinding); // */ gboolean is_mouse = (key_binding_value >> 31) & 1; guint state = (key_binding_value >> (31 - KEY_BINDING_STATE_BITS)) & ((1 << KEY_BINDING_STATE_BITS) - 1); guint keycode = key_binding_value & ((1 << (31 - KEY_BINDING_STATE_BITS)) - 1); // Filter unwanted state variables out state &= gtk_accelerator_get_default_mod_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; queue_action_from_binding(binding); return; } active_key_binding.key_binding = NULL; } if(!key_binding_value) { return; } if(!binding) { binding = g_hash_table_lookup(key_bindings[active_key_binding_context], 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[active_key_binding_context], GUINT_TO_POINTER(alternate_value)); } } if(binding) { if(binding->next_key_bindings) { active_key_binding.key_binding = binding; active_key_binding.associated_image = current_file_node; active_key_binding.timeout_id = gdk_threads_add_timeout((size_t)(option_keyboard_timeout * 1000), handle_input_event_timeout_callback, NULL); #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE if(application_mode == MONTAGE && montage_window_control.show_binding_overlays) { gtk_widget_queue_draw(GTK_WIDGET(main_window)); } #endif } else { active_key_binding.key_binding = binding->next_action; active_key_binding.associated_image = current_file_node; queue_action_from_binding(binding); } } #else for(const struct default_key_bindings_struct *kb = default_key_bindings; kb->key_binding_value; kb++) { if(kb->context == active_key_binding_context && 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) {/*{{{*/ GdkKeymap *keymap = gdk_keymap_get_for_display(gtk_widget_get_display(GTK_WIDGET(main_window))); guint keyval; GdkModifierType consumed; gdk_keymap_translate_keyboard_state(keymap, event->hardware_keycode, event->state, event->group, &keyval, NULL, NULL, &consumed); handle_input_event(KEY_BINDING_VALUE(0, event->state & ~consumed, 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_scale_factor, (screen_geometry.y + screen_geometry.height / 2.) / screen_scale_factor); #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_scale_factor, (screen_geometry.y + screen_geometry.height / 2.) / screen_scale_factor); #endif }/*}}}*/ gboolean window_auto_hide_cursor_callback(gpointer user_data) {/*{{{*/ struct Point *cursor_pos = (struct Point *)user_data; if(!main_window_visible) { return FALSE; } gboolean default_retval = TRUE; if(!cursor_pos) { // This function has been called in an improper way. But on purpose: // We are to allocate cursor_pos ourselves, and setup a timeout. // Note that it'll always appear as if the cursor position changed below. cursor_pos = g_malloc(sizeof(struct Point)); cursor_pos->x = -1; cursor_pos->y = -1; cursor_auto_hide_timer_id = g_timeout_add_full(G_PRIORITY_DEFAULT, 1000, window_auto_hide_cursor_callback, cursor_pos, g_free); default_retval = FALSE; } // Get current mouse position int x, y; #if GTK_MAJOR_VERSION >= 3 GdkDisplay *display = gtk_widget_get_display(GTK_WIDGET(main_window)); #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_window_get_device_position(gtk_widget_get_window(GTK_WIDGET(main_window)), device, &x, &y, NULL); #else gdk_window_get_pointer(gtk_widget_get_window(GTK_WIDGET(main_window)), &x, &y, NULL); #endif // Check if the mouse has been moved. If it has, try again in 2s. if(x != cursor_pos->x || y != cursor_pos->y) { cursor_pos->x = x; cursor_pos->y = y; return default_retval; } // This source is going to be deleted, since we'll return FALSE cursor_auto_hide_timer_id = 0; // Hide the cursor window_hide_cursor(); // Resubscripe to motion events gtk_widget_add_events(GTK_WIDGET(main_window), GDK_POINTER_MOTION_MASK); return FALSE; }/*}}}*/ gboolean window_motion_notify_callback(GtkWidget *widget, GdkEventMotion *event, gpointer user_data) {/*{{{*/ if(!(event->state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | GDK_BUTTON3_MASK))) { // Receiving pointer motion events is expensive. We are really only interested in being // informed when the user starts to move the mouse. So ask the display // server to stop sending motion events. // In theory, GDK_POINTER_MOTION_HINT_MASK could do this job, but, alas, it's broken. GdkWindow *window = gtk_widget_get_window(GTK_WIDGET(main_window)); gdk_window_set_events(window, gdk_window_get_events(window) & ~GDK_POINTER_MOTION_MASK); } if(cursor_auto_hide_mode_enabled) { // Show the cursor window_show_cursor(); // Set up a callback to check whether the cursor has been moved in 2s if(cursor_auto_hide_timer_id) { g_source_remove(cursor_auto_hide_timer_id); } struct Point *cursor_pos = g_malloc(sizeof(struct Point)); cursor_pos->x = event->x; cursor_pos->y = event->y; cursor_auto_hide_timer_id = g_timeout_add_full(G_PRIORITY_DEFAULT, 1000, window_auto_hide_cursor_callback, cursor_pos, g_free); } if(!main_window_in_fullscreen) { return FALSE; } if(application_mode == DEFAULT && event->state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | GDK_BUTTON3_MASK)) { gdouble dev_x = screen_geometry.width / 2 + screen_geometry.x - event->x_root * screen_scale_factor; gdouble dev_y = screen_geometry.height / 2 + screen_geometry.y - event->y_root * screen_scale_factor; if(fabs(dev_x) < 5 && fabs(dev_y) < 4) { return FALSE; } if(event->state & GDK_BUTTON1_MASK) { if(application_mode == DEFAULT) { 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) {/*{{{*/ if(event->time - last_button_press_time < 250 && (application_mode != MONTAGE || event->type != GDK_2BUTTON_PRESS)) { // GTK double-reported this. Ignore. return FALSE; } last_button_press_time = event->time; #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE if(application_mode == MONTAGE && cursor_visible && event->button == 1) { // In montage mode, the mouse may be used to select thumbnails // Thumbnails are drawn such that the whole mosaique is centered. Undo // that to find the correct index. // event->x -= (main_window_width % (option_thumbnails.width + 10)) / 2; event->y -= (main_window_height % (option_thumbnails.height + 10)) / 2; if(event->x < 0) event->x = 0; if(event->y < 0) event->y = 0; montage_window_set_cursor((int)(event->x / (option_thumbnails.width + 10)), (int)(event->y / (option_thumbnails.height + 10))); gtk_widget_queue_draw(GTK_WIDGET(main_window)); if(event->type == GDK_2BUTTON_PRESS) { pqiv_action_parameter_t empty_param = { .pint = 0 }; #ifndef CONFIGURED_WITHOUT_ACTIONS queue_action(ACTION_MONTAGE_MODE_RETURN_PROCEED, empty_param); #else action(ACTION_MONTAGE_MODE_RETURN_PROCEED, empty_param); #endif // Prevent the release handler from handling this event again last_button_press_time = 0; } return FALSE; } #endif // CONFIGURED_WITHOUT_MONTAGE_MODE if(main_window_in_fullscreen) { window_center_mouse(); } return FALSE; }/*}}}*/ gboolean window_button_release_callback(GtkWidget *widget, GdkEventButton *event, gpointer user_data) {/*{{{*/ if(event->time - last_button_press_time > 250 || (event->time == last_button_release_time && last_button_release_time > 0)) { // Do nothing if the button was pressed for a long time // Also, fix a bug where GTK reports the same release event twice -- by // assuming that no user would ever be able to press and release // buttons sufficiently fast for time to have the same (millis) value. return FALSE; } last_button_release_time = event->time; if(!main_window_in_fullscreen) { if(option_transparent_background) { gtk_window_set_decorated(main_window, !gtk_window_get_decorated(main_window)); } // All other bindings are only handled 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) {/*{{{*/ handle_input_event(KEY_BINDING_VALUE(1, event->state, (event->direction + 1) << 2)); return FALSE; }/*}}}*/ void window_hide_cursor() {/*{{{*/ if(!main_window_visible) { return; } GdkDisplay *display = gtk_widget_get_display(GTK_WIDGET(main_window)); #if GTK_CHECK_VERSION(3, 10, 0) // The nice version for obtaining a blank cursor is this: // GdkCursor *cursor = gdk_cursor_new_for_display(display, GDK_BLANK_CURSOR); // But there's a bug somewhere where this can result in "free(): invalid pointer". // So go the extra mile and create our own cursor. static GdkCursor *cursor = NULL; if(!cursor) { cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1); cursor = gdk_cursor_new_from_surface(display, surface, 0, 0); } #else GdkCursor *cursor = gdk_cursor_new_for_display(display, GDK_BLANK_CURSOR); #endif GdkWindow *window = gtk_widget_get_window(GTK_WIDGET(main_window)); if(window) { gdk_window_set_cursor(window, cursor); cursor_visible = FALSE; } #if GTK_MAJOR_VERSION >= 3 && !GTK_CHECK_VERSION(3, 10, 0) g_object_unref(cursor); #endif }/*}}}*/ void window_show_cursor() {/*{{{*/ if(!main_window_visible) { return; } GdkWindow *window = gtk_widget_get_window(GTK_WIDGET(main_window)); if(window) { gdk_window_set_cursor(window, NULL); cursor_visible = TRUE; } }/*}}}*/ gboolean window_state_into_fullscreen_actions(gpointer user_data) {/*{{{*/ if(user_data == NULL) { current_shift_x = 0; current_shift_y = 0; if(application_mode == DEFAULT) { window_hide_cursor(); set_cursor_auto_hide_mode(FALSE); } update_info_text(NULL); } 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 fullscreen_transition_source_id = -1; action_done(); return FALSE; }/*}}}*/ gboolean window_state_out_of_fullscreen_actions(gpointer user_data) {/*{{{*/ if(user_data == NULL) { current_shift_x = 0; current_shift_y = 0; window_show_cursor(); set_cursor_auto_hide_mode(TRUE); update_info_text(NULL); } // 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(); fullscreen_transition_source_id = -1; action_done(); return FALSE; }/*}}}*/ 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(fullscreen_transition_source_id >= 0) { g_source_remove(fullscreen_transition_source_id); } if(main_window_in_fullscreen) { window_state_into_fullscreen_actions(NULL); fullscreen_transition_source_id = g_timeout_add(500, window_state_into_fullscreen_actions, main_window); } else { window_state_out_of_fullscreen_actions(NULL); fullscreen_transition_source_id = g_timeout_add(500, window_state_out_of_fullscreen_actions, main_window); } } 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) {/*{{{*/ #if defined(GDK_WINDOWING_X11) 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"))); wm_supports_framedrawn = gdk_x11_screen_supports_net_wm_hint(screen, gdk_x11_xatom_to_atom(gdk_x11_get_xatom_by_name("_NET_WM_FRAME_DRAWN"))); } #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"))); wm_supports_framedrawn = gdk_x11_screen_supports_net_wm_hint(screen, gdk_x11_xatom_to_atom(gdk_x11_get_xatom_by_name("_NET_WM_FRAME_DRAWN"))); #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)); #if defined(GDK_WINDOWING_X11) #if GTK_CHECK_VERSION(3, 0, 0) if(GDK_IS_X11_DISPLAY(gdk_screen_get_display(screen))) { g_signal_connect(screen, "window-manager-changed", G_CALLBACK(window_screen_window_manager_changed_callback), screen); } #else g_signal_connect(screen, "window-manager-changed", G_CALLBACK(window_screen_window_manager_changed_callback), screen); #endif #endif window_screen_window_manager_changed_callback(screen); #if GTK_CHECK_VERSION(3, 22, 0) GdkDisplay *display = gdk_window_get_display(window); GdkMonitor *monitor = gdk_display_get_monitor_at_window(display, window); static GdkMonitor *old_monitor = NULL; if(old_monitor != NULL && option_transparent_background) { window_screen_activate_rgba(); } if(old_monitor != monitor) { gdk_monitor_get_geometry(monitor, &screen_geometry); old_monitor = monitor; screen_scale_factor = gdk_monitor_get_scale_factor(monitor); if(screen_scale_factor != 1) { screen_geometry.x *= screen_scale_factor; screen_geometry.y *= screen_scale_factor; screen_geometry.width *= screen_scale_factor; screen_geometry.height *= screen_scale_factor; } } #else guint monitor = gdk_screen_get_monitor_at_window(screen, window); 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); old_monitor = monitor; #if GTK_CHECK_VERSION(3, 10, 0) screen_scale_factor = gdk_screen_get_monitor_scale_factor(screen, monitor); if(screen_scale_factor != 1) { screen_geometry.x *= screen_scale_factor; screen_geometry.y *= screen_scale_factor; screen_geometry.width *= screen_scale_factor; screen_geometry.height *= screen_scale_factor; } #endif } #endif }/*}}}*/ 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 if(!option_transparent_background) { // Ensure that extra pixels (shown e.g. while resizing the window) are black window_clear_background_pixmap(); } if(!main_window_in_fullscreen) { // Start the timer to hide the cursor set_cursor_auto_hide_mode(TRUE); } }/*}}}*/ void create_window() { /*{{{*/ if(main_window != NULL) { return; } #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_POINTER_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(); #if GTK_CHECK_VERSION(3, 22, 0) GdkDisplay *display = gdk_screen_get_display(screen); GdkMonitor *monitor = gdk_display_get_primary_monitor(display); if(!monitor) { monitor = gdk_display_get_monitor(display, 0); } gdk_monitor_get_geometry(monitor, &screen_geometry); screen_scale_factor = gdk_monitor_get_scale_factor(monitor); #else guint monitor = gdk_screen_get_primary_monitor(screen); gdk_screen_get_monitor_geometry(screen, monitor, &screen_geometry); #if GTK_CHECK_VERSION(3, 10, 0) screen_scale_factor = gdk_screen_get_monitor_scale_factor(screen, monitor); #endif #endif if(screen_scale_factor != 1) { screen_geometry.x *= screen_scale_factor; screen_geometry.y *= screen_scale_factor; screen_geometry.width *= screen_scale_factor; screen_geometry.height *= screen_scale_factor; } 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"); gtk_main_quit(); } return FALSE; }/*}}}*/ gboolean help_show_version(const gchar *option_name, const gchar *value, gpointer data, GError **error) {/*{{{*/ g_print("pqiv " PQIV_VERSION "\n"); exit(0); return FALSE; } #ifndef CONFIGURED_WITHOUT_ACTIONS char *key_binding_sequence_to_string(guint key_binding_value, gchar *prefix) {/*{{{*/ gboolean is_mouse = (key_binding_value >> 31) & 1; guint state = (key_binding_value >> (31 - KEY_BINDING_STATE_BITS)) & ((1 << KEY_BINDING_STATE_BITS) - 1); guint keycode = key_binding_value & ((1 << (31 - KEY_BINDING_STATE_BITS)) - 1); 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 ", prefix ? prefix : "", modifier, keycode); } else { str_key = g_strdup_printf("%s%s ", prefix ? prefix : "", modifier, keycode >> 2); } } else { char *keyval_name = gdk_keyval_name(keycode); str_key = g_strdup_printf("%s%s%s%s%s ", prefix ? prefix : "", modifier, keyval_name && keyval_name[0] && !keyval_name[1] ? "" : "<", keyval_name, keyval_name && keyval_name[0] && !keyval_name[1] ? "" : ">"); } return str_key; }/*}}}*/ void help_show_single_action(key_binding_t *current_action) {/*{{{*/ g_print("%s%c", pqiv_action_descriptors[current_action->action].name, KEY_BINDINGS_COMMAND_PARAMETER_BEGIN_SYMBOL); switch(pqiv_action_descriptors[current_action->action].parameter_type) { case PARAMETER_NONE: g_print("%c ", KEY_BINDINGS_COMMAND_PARAMETER_END_SYMBOL); break; case PARAMETER_INT: g_print("%d%c ", current_action->parameter.pint, KEY_BINDINGS_COMMAND_PARAMETER_END_SYMBOL); break; case PARAMETER_DOUBLE: g_print("%g%c ", current_action->parameter.pdouble, KEY_BINDINGS_COMMAND_PARAMETER_END_SYMBOL); break; case PARAMETER_CHARPTR: for(const char *p = current_action->parameter.pcharptr; *p; p++) { if(*p == KEY_BINDINGS_COMMAND_PARAMETER_END_SYMBOL || *p == '\\') { g_print("\\"); } g_print("%c", *p); } g_print("%c ", KEY_BINDINGS_COMMAND_PARAMETER_END_SYMBOL); break; case PARAMETER_2SHORT: g_print("%d, %d%c ", current_action->parameter.p2short.p1, current_action->parameter.p2short.p2, KEY_BINDINGS_COMMAND_PARAMETER_END_SYMBOL); } }/*}}}*/ void help_show_key_bindings_helper(gpointer key, gpointer value, gpointer user_data) {/*{{{*/ guint key_binding_value = GPOINTER_TO_UINT(key); key_binding_t *key_binding = (key_binding_t *)value; char *str_key = key_binding_sequence_to_string(key_binding_value, user_data); 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 %c ", str_key, KEY_BINDINGS_COMMANDS_BEGIN_SYMBOL); for(key_binding_t *current_action = key_binding; current_action; current_action = current_action->next_action) { help_show_single_action(current_action); } g_print("%c \n", KEY_BINDINGS_COMMANDS_END_SYMBOL); 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[0], help_show_key_bindings_helper, (gpointer)""); for(int i=1; i, 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. * 6 context switch initialized, expecting identifier & open parenthesis */ GHashTable **active_key_bindings_table = &key_bindings[DEFAULT]; enum context_t current_context = DEFAULT; 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 if(current_context == DEFAULT && *scan == KEY_BINDINGS_CONTEXT_SWITCH_SYMBOL /* @ */) { // Expect name of a context token_start = scan+1; state = 6; break; } if(current_context != DEFAULT && *scan == KEY_BINDINGS_COMMANDS_END_SYMBOL /* } */) { current_context = DEFAULT; active_key_bindings_table = &key_bindings[current_context]; state = 0; break; } current_command_start = scan; /* fall through */ case 1: // Expecting continuation of key description or start of command switch(*scan) { case KEY_BINDINGS_KEY_TOKEN_BEGIN_SYMBOL: /* < */ token_start = scan + 1; state = 2; break; case KEY_BINDINGS_COMMANDS_BEGIN_SYMBOL: /* { */ if(state == 0) { error_message = g_strdup_printf("Unallowed %c before keyboard binding was given", KEY_BINDINGS_COMMANDS_END_SYMBOL); 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) { guint upper_keyval = gdk_keyval_to_upper(keyval); if (upper_keyval != keyval) { keyval = 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_full((GHashFunc)g_direct_hash, (GEqualFunc)g_direct_equal, NULL, key_binding_t_destroy_callback); \ } \ 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 == KEY_BINDINGS_KEY_TOKEN_END_SYMBOL) { /* > */ 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) { guint upper_keyval = gdk_keyval_to_upper(keyval); if (upper_keyval != keyval) { keyval = 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 == KEY_BINDINGS_COMMAND_SEPARATOR_SYMBOL) { token_start = scan + 1; continue; } switch(*scan) { case KEY_BINDINGS_COMMAND_PARAMETER_BEGIN_SYMBOL: /* ( */ 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; if (binding->next_action) { key_binding_t_destroy_callback(binding->next_action); binding->next_action = NULL; } 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 KEY_BINDINGS_COMMANDS_END_SYMBOL: /* } */ active_key_bindings_table = &key_bindings[current_context]; 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 == KEY_BINDINGS_COMMAND_PARAMETER_END_SYMBOL) { /* ) */ identifier_length = scan - token_start; identifier = g_malloc(identifier_length + 1); int identifier_end, identifier_pos; for(identifier_end=0, identifier_pos=0; identifier_pos identifier_length) { break; } } identifier[identifier_end] = token_start[identifier_pos]; } identifier[identifier_end] = 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_strndup(identifier, identifier_pos); 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; case 6: /* Context switch - expect name & opening parenthesis */ if(*scan == KEY_BINDINGS_COMMANDS_BEGIN_SYMBOL) { identifier_length = 0; const char *i; for(i=token_start; i 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; } parsed_parameter.pint = 0; // To calm clang 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; } queue_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) {/*{{{*/ if(!main_window_visible) { // TODO Properly defer this instead of short-circuiting the main loop return TRUE; } 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() {/*{{{*/ for(int i=0; ikey_binding_value; kb++) { key_binding_t *nkb = g_slice_new(key_binding_t); nkb->action = kb->action; nkb->parameter = kb->parameter; nkb->next_action = NULL; nkb->next_key_bindings = NULL; g_hash_table_insert(key_bindings[kb->context], GUINT_TO_POINTER(kb->key_binding_value), nkb); } }/*}}}*/ #endif void recreate_window() {/*{{{*/ if(!main_window_visible) { return; } PQIV_DISABLE_PEDANTIC g_signal_handlers_disconnect_by_func(main_window, G_CALLBACK(window_close_callback), NULL); PQIV_ENABLE_PEDANTIC gtk_widget_destroy(GTK_WIDGET(main_window)); main_window = NULL; 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(); gboolean tree_empty = TRUE; if(file_tree_valid) { tree_empty = bostree_node_count(file_tree) == 0; } if(!option_wait_for_images_to_appear) { if(tree_empty) { g_printerr("No images left to display.\n"); g_idle_add((GSourceFunc)gtk_main_quit, NULL); return NULL; } 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); load_images_thread_update_info_text(NULL); } #endif return NULL; }/*}}}*/ gboolean inner_main(void *user_data) {/*{{{*/ if(option_lazy_load) { if(option_allow_empty_window) { create_window(); gtk_widget_show_all(GTK_WIDGET(main_window)); main_window_visible = TRUE; } 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"); gtk_main_quit(); } } #ifndef CONFIGURED_WITHOUT_ACTIONS if(option_actions_from_stdin) { g_thread_new("command-reader", read_commands_thread, NULL); } #endif return FALSE; }/*}}}*/ /* Marks system functions {{{ */ #ifndef CONFIGURED_WITHOUT_EXTERNAL_COMMANDS void clear_marks() {/*{{{*/ D_LOCK(file_tree); for(BOSNode *iter = bostree_select(file_tree, 0); iter; iter = bostree_next_node(iter)) { FILE(iter)->marked = FALSE; } D_UNLOCK(file_tree); if(application_mode == DEFAULT) { update_info_text("Cleared all marks"); info_text_queue_redraw(); } }/*}}}*/ void toggle_mark() {/*{{{*/ if(application_mode == DEFAULT) { FILE(current_file_node)->marked = !FILE(current_file_node)->marked; update_info_text(NULL); info_text_queue_redraw(); } #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE else if(application_mode == MONTAGE) { FILE(montage_window_control.selected_node)->marked = !FILE(montage_window_control.selected_node)->marked; } #endif }/*}}}*/ GString *get_all_marked() {/*{{{*/ GString *result = g_string_new(NULL); D_LOCK(file_tree); for(BOSNode *iter = bostree_select(file_tree, 0); iter; iter = bostree_next_node(iter)) { file_t *file = FILE(iter); if(file->marked) { g_string_append_printf(result, "%s\n", file->file_name); } } D_UNLOCK(file_tree); return result; }/*}}}*/ #endif // }}} 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 #if defined(GDK_WINDOWING_X11) XInitThreads(); #endif #if (!GLIB_CHECK_VERSION(2, 32, 0)) g_thread_init(NULL); gdk_threads_init(); #endif gboolean windowing_available = gtk_init_check(&argc, &argv); // fyi, this generates a MemorySanitizer warning currently #ifndef CONFIGURED_WITHOUT_ACTIONS initialize_key_bindings(); #endif global_argc = argc; global_argv = argv; parse_configuration_file(); parse_command_line(); if(!windowing_available) { g_warn_if_reached(); // this should never be called because parse_command_line() calls gtk_init() again. return 1; } if(option_disable_backends) { gchar **disabled_backends = g_strsplit(option_disable_backends, ",", 0); initialize_file_type_handlers((const gchar * const *)disabled_backends); g_strfreev(disabled_backends); } else { const gchar * disabled_backends[] = { NULL }; initialize_file_type_handlers(disabled_backends); } 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); if(option_fading_duration > option_slideshow_interval) { g_printerr("Warning: Fade durations larger than the slideslow interval won't work as expected.\n"); } #ifndef CONFIGURED_WITHOUT_ACTIONS if(option_actions_from_stdin && global_argc == 1 && !option_wait_for_images_to_appear) { g_printerr("Warning: --actions-from-stdin with no files given implies --wait-for-images-to-appear.\n"); option_wait_for_images_to_appear = TRUE; } if(option_actions_from_stdin && option_addl_from_stdin) { g_printerr("Error: --additional-from-stdin conflicts with --actions-from-stdin.\n"); exit(1); } #endif if(option_wait_for_images_to_appear) { if(!option_watch_directories) { g_printerr("Warning: --wait-for-images-to-appear implies --watch-directories.\n"); option_watch_directories = TRUE; } if(!option_lazy_load) { g_printerr("Warning: --wait-for-images-to-appear implies --lazy-load.\n"); option_lazy_load = TRUE; } } // Start image loader & show window inside main loop, in order to have // gtk_main_quit() available. gdk_threads_add_idle(inner_main, NULL); gtk_main(); // We are outside of the main loop 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); abort_pending_image_loads(NULL); D_UNLOCK(file_tree); 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); // Note: This still won't free all the data, because we hold weak // references. But that doesn't matter, the unloading is the important // thing as it removes any temporary files. bostree_destroy(file_tree); D_UNLOCK(file_tree); return 0; } // vim:noet ts=4 sw=4 tw=0 fdm=marker pqiv-2.12/pqiv.h000066400000000000000000000242671376070546500135750ustar00rootroot00000000000000/** * pqiv * * Copyright (c) 2013-2017, 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.12" #endif #define FILE_FLAGS_ANIMATION (guint)(1) #define FILE_FLAGS_MEMORY_IMAGE (guint)(1<<1) #define FALSE_POINTER ((void*)-1) // The structure for images {{{ typedef struct _file file_t; typedef GBytes *(*file_data_loader_fn_t)(file_t *file, GError **error_pointer); typedef struct file_type_handler_struct_t file_type_handler_t; struct _file { // 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 _or_ data for the // file_data_loader callback to use to construct the _actual_ bytes object // to use. GBytes *file_data; // If the image is a memory image that can be generated at load time, // store a pointer to the generator. file_data_loader_fn_t file_data_loader; // 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; // This flag determines whether this file should be reloaded // despite is_loaded being set gboolean force_reload; // Cached image size guint width; guint height; #ifndef CONFIGURED_WITHOUT_MONTAGE_MODE // Cached thumbnail cairo_surface_t *thumbnail; #endif // Lock to prevent multiple threads from accessing the backend at the same // time GMutex lock; // Default render, automatically unloaded with the image, not guaranteed to // be present, not guaranteed to have the correct scale level. cairo_surface_t *prerendered_view; // File-type specific data, allocated and freed by the file type handlers void *private; // TRUE if file is marked #ifndef CONFIGURED_WITHOUT_EXTERNAL_COMMANDS gboolean marked; #endif }; // }}} // 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); // Create a GFile for a file's name (We have a wrapper to support names with colons) GFile *gfile_for_commandline_arg(const char *parameter); // 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_file_name, 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); // Find a handler for a given file; useful for handler redirection, see archive // file type BOSNode *load_images_handle_parameter_find_handler(const char *param, load_images_state_t state, file_t *file, GtkFileFilterInfo *file_filter_info); // 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); // Set the interpolation filter in a cairo context for the current file based on the user settings void apply_interpolation_quality(cairo_t *cr); // Wrapper for string vector contains function gboolean strv_contains(const gchar * const *strv, const gchar *str); // }}} // 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_SET_SCALE_MODE_SCREEN_FRACTION, 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_LOGICAL_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, ACTION_SET_INTERPOLATION_QUALITY, ACTION_ANIMATION_STEP, ACTION_ANIMATION_CONTINUE, ACTION_ANIMATION_SET_SPEED_ABSOLUTE, ACTION_ANIMATION_SET_SPEED_RELATIVE, ACTION_GOTO_EARLIER_FILE, ACTION_SET_CURSOR_AUTO_HIDE, ACTION_SET_FADE_DURATION, ACTION_SET_KEYBOARD_TIMEOUT, ACTION_SET_THUMBNAIL_SIZE, ACTION_SET_THUMBNAIL_PRELOAD, ACTION_MONTAGE_MODE_ENTER, ACTION_MONTAGE_MODE_SHIFT_X, ACTION_MONTAGE_MODE_SHIFT_Y, ACTION_MONTAGE_MODE_SET_SHIFT_X, ACTION_MONTAGE_MODE_SET_SHIFT_Y, ACTION_MONTAGE_MODE_SET_WRAP_MODE, ACTION_MONTAGE_MODE_SHIFT_Y_PG, ACTION_MONTAGE_MODE_SHIFT_Y_ROWS, ACTION_MONTAGE_MODE_SHOW_BINDING_OVERLAYS, ACTION_MONTAGE_MODE_FOLLOW, ACTION_MONTAGE_MODE_FOLLOW_PROCEED, ACTION_MONTAGE_MODE_RETURN_PROCEED, ACTION_MONTAGE_MODE_RETURN_CANCEL, ACTION_MOVE_WINDOW, ACTION_TOGGLE_BACKGROUND_PATTERN, ACTION_TOGGLE_NEGATE_MODE, ACTION_TOGGLE_MARK, ACTION_CLEAR_MARKS, } 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