1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
/*
Module : main.c
Purpose : GDK Quick Image Viewer (qiv)
More : see qiv README
Homepage : http://qiv.spiegl.de/
Original : http://www.klografx.net/qiv/
Policy : GNU GPL
*/
#define _DEFAULT_SOURCE
#include <gdk/gdkx.h>
#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
#include <ctype.h>
#include <string.h>
#ifdef HAVE_MAGIC
#include <magic.h>
#endif
#include "qiv.h"
#include "main.h"
qiv_image main_img;
qiv_mgl magnify_img; /* [lc] */
static int check_extension(const char *);
static void qiv_signal_usr(int sig);
static gboolean qiv_handle_timer(gpointer);
static void qiv_timer_restart(gpointer);
#ifdef HAVE_MAGIC
static int check_magic(magic_t cookie, const char *name);
#endif
int main(int argc, char **argv)
{
struct timeval tv;
int i;
// [as] workaround for problem with X composite extension
// is this still needed with imlib2 ??
putenv("XLIB_SKIP_ARGB_VISUALS=1");
// set GDK backend to x11. Otherwise qiv won't work on wayland.
putenv("GDK_BACKEND=x11");
/* Randomize seed for 'true' random */
gettimeofday(&tv,NULL);
srand(tv.tv_usec*1000000+tv.tv_sec);
/* Initialize GDK */
int has_display = gdk_init_check(&argc,&argv);
/* Set up our options, image list, etc */
strncpy(select_dir, SELECT_DIR, sizeof select_dir);
reset_mod(&main_img);
options_read(argc, argv, &main_img);
if (!images) { /* No images to display */
g_print("qiv: cannot load any images.\n");
usage(argv[0],1);
}
if (!has_display) { /* No DISPLAY */
g_print("qiv: DISPLAY not set.\n");
usage(argv[0],1);
}
#ifdef SUPPORT_LCMS
/* read profiles if provided */
if (cms_transform)
{
if (source_profile) {
h_source_profile = cmsOpenProfileFromFile(source_profile, "r");
} else {
h_source_profile = cmsCreate_sRGBProfile();
}
if (h_source_profile == NULL)
{
g_print("qiv: cannot create source color profile.\n");
usage(argv[0],1);
}
if (display_profile) {
h_display_profile = cmsOpenProfileFromFile(display_profile, "r");
} else {
h_display_profile = cmsCreate_sRGBProfile();
}
if (h_display_profile == NULL)
{
g_print("qiv: cannot create display color profile.\n");
usage(argv[0],1);
}
}
/* TYPE_BGRA_8 or TYPE_ARGB_8 depending on endianess */
h_cms_transform = cmsCreateTransform(h_source_profile,
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
TYPE_BGRA_8,
h_display_profile,
TYPE_BGRA_8,
#else
TYPE_ARGB_8,
h_display_profile,
TYPE_ARGB_8,
#endif
INTENT_PERCEPTUAL, 0);
#endif
/* Load things from GDK */
qiv_main_loop = g_main_loop_new(NULL, TRUE);
screen_x = gdk_screen_width();
screen_y = gdk_screen_height();
screen = gdk_screen_get_default();
num_monitors = gdk_screen_get_n_monitors(screen);
monitor = malloc( num_monitors * sizeof(GdkRectangle));
for(i=0; i< num_monitors ; i++)
{
gdk_screen_get_monitor_geometry(screen, i, &monitor[i]);
}
if(user_screen < num_monitors)
{
main_img.mon_id = user_screen;
}
/* statusbar with pango */
layout = pango_layout_new(gdk_pango_context_get());
fontdesc = pango_font_description_from_string (STATUSBAR_FONT);
/* set default fontsize if no fontsize is given */
if(!pango_font_description_get_size(fontdesc))
{
pango_font_description_set_size(fontdesc, PANGO_SCALE * STATUSBAR_FS);
}
pango_font_description_set_size(fontdesc, pango_font_description_get_size(fontdesc) * highDPIfactor);
metrics = pango_context_get_metrics (gdk_pango_context_get(), fontdesc, NULL);
pango_layout_set_font_description (layout, fontdesc);
/* jpeg comment with pango */
layoutComment = pango_layout_new(gdk_pango_context_get());
fontdescComment = pango_font_description_from_string (COMMENT_FONT);
/* set default fontsize if no fontsize is given */
if(!pango_font_description_get_size(fontdescComment))
{
pango_font_description_set_size(fontdescComment, PANGO_SCALE * COMMENT_FS);
}
pango_font_description_set_size(fontdescComment, pango_font_description_get_size(fontdescComment) * highDPIfactor);
metricsComment = pango_context_get_metrics (gdk_pango_context_get(), fontdescComment, NULL);
pango_layout_set_font_description (layoutComment, fontdescComment);
/* show artist name with pango */
layoutArtist = pango_layout_new(gdk_pango_context_get());
fontdescArtist = pango_font_description_from_string (ARTIST_FONT);
/* set default fontsize if no fontsize is given */
if(!pango_font_description_get_size(fontdescArtist))
{
pango_font_description_set_size(fontdescArtist, PANGO_SCALE * ARTIST_FS);
}
pango_font_description_set_size(fontdescArtist, pango_font_description_get_size(fontdescArtist) * highDPIfactor);
metricsArtist = pango_context_get_metrics (gdk_pango_context_get(), fontdescArtist, NULL);
pango_layout_set_font_description (layoutArtist, fontdescArtist);
max_rand_num = images;
/* get colors */
#if GDK_MAJOR_VERSION < 3
cmap = gdk_colormap_get_system();
color_alloc(image_bg_spec, &image_bg);
#else
gdk_rgba_parse(&image_bg, image_bg_spec);
#endif
/* Display first image first, except in random mode */
if (random_order)
next_image(0);
//disabled because 'params' is never used, see above
//if (to_root || to_root_t || to_root_s) {
// params.flags |= PARAMS_VISUALID;
// (GdkVisual*)params.visualid = gdk_window_get_visual(GDK_ROOT_PARENT());
//}
/* Setup callbacks */
gdk_event_handler_set(qiv_handle_event, &main_img, NULL);
qiv_timer_restart(NULL);
/* And signal catchers */
signal(SIGTERM, finish);
signal(SIGINT, finish);
signal(SIGUSR1, qiv_signal_usr);
signal(SIGUSR2, qiv_signal_usr);
/* check for DPMS capability and disable it if slideshow
was started from command options */
dpms_check();
if(slide){
dpms_disable();
}
/* Load & display the first image */
qiv_load_image(&main_img);
if(watch_file){
g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, 100, qiv_watch_file, &main_img, NULL);
}
g_main_loop_run(qiv_main_loop); /* will never return */
return 0;
}
void qiv_exit(int code)
{
destroy_image(&main_img);
dpms_enable();
pango_font_description_free (fontdesc);
g_object_unref (layout);
pango_font_metrics_unref(metrics);
g_main_loop_unref(qiv_main_loop);
finish(SIGTERM); /* deprecated, subject to change */
}
/*
* function for handling signals (only USR1 and USR2 handled)
*/
static void qiv_signal_usr(int sig)
{
if (sig == SIGUSR1) {
next_image(1);
} else {
next_image(-1);
}
qiv_load_image(&main_img);
}
/*
* Slideshow timer function
*
* If this function returns false, the timer is destroyed
* and qiv_timer_restart() is automatically called, which
* then starts the timer again. Thus images which takes some
* time to load will still be displayed for "delay" seconds.
*/
static gboolean qiv_handle_timer(gpointer data)
{
if (*(int *)data || slide) {
next_image(0);
/* disable screensaver during slideshow */
XResetScreenSaver(gdk_x11_display_get_xdisplay(gdk_display_get_default()));
qiv_load_image(&main_img);
}
return FALSE;
}
/*
* Slideshow timer (re)start function
*/
static void qiv_timer_restart(gpointer dummy)
{
g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, delay,
qiv_handle_timer, &slide,
qiv_timer_restart);
}
/* Filter images by extension */
void filter_images(int *images, char **image_names)
{
int i = 0;
#ifdef HAVE_MAGIC
magic_t cookie;
cookie = magic_open(MAGIC_SYMLINK);
magic_load(cookie,NULL);
#endif
while(i < *images) {
if (check_extension(image_names[i])
#ifdef HAVE_MAGIC
|| check_magic(cookie, image_names[i])
#endif
) {
i++;
} else {
int j = i;
if (j < *images-1)
image_idx--;
while(j < *images-1) {
image_names[j] = image_names[j+1];
++j;
}
--(*images);
}
}
#ifdef HAVE_MAGIC
magic_close(cookie);
#endif
if (image_idx < 0)
image_idx = 0;
}
static int check_extension(const char *name)
{
char *extn = strrchr(name, '.');
int i;
if (extn)
for (i=0; image_extensions[i]; i++)
if (strcasecmp(extn, image_extensions[i]) == 0)
return 1;
return 0;
}
#ifdef HAVE_MAGIC
static int check_magic(magic_t cookie, const char *name)
{
const char *description=NULL;
int i;
int ret=0;
description = magic_file(cookie, name);
if(description)
{
for(i=0; image_magic[i]; i++ )
if (strncasecmp(description, image_magic[i], strlen(image_magic[i])) == 0)
{
ret = 1;
break;
}
}
return ret;
}
#endif
|