Skip to content
Snippets Groups Projects
winwidget.c 33.9 KiB
Newer Older
/* winwidget.c

Copyright (C) 1999-2003 Tom Gilbert.
Birte Kristina Friesel's avatar
Birte Kristina Friesel committed
Copyright (C) 2010-2011 Daniel Friesel.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies of the Software and its documentation and acknowledgment shall be
given in the documentation and software packages that this Software was
used.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

#include "feh.h"
#include "filelist.h"
#include "winwidget.h"
#include "options.h"
Sven Willner's avatar
Sven Willner committed
#ifdef HAVE_INOTIFY
#include <sys/inotify.h>
#endif

static void winwidget_unregister(winwidget win);
static void winwidget_register(winwidget win);
static winwidget winwidget_allocate(void);


int window_num = 0;		/* For window list */
winwidget *windows = NULL;	/* List of windows to loop though */

static winwidget winwidget_allocate(void)
	winwidget ret = NULL;

	ret = emalloc(sizeof(_winwidget));
	memset(ret, 0, sizeof(_winwidget));

	ret->win = 0;
	ret->w = 0;
	ret->h = 0;
	ret->full_screen = 0;
	ret->im_w = 0;
	ret->im_h = 0;
	ret->im_angle = 0;
	ret->bg_pmap = 0;
	ret->bg_pmap_cache = 0;
	ret->im = NULL;
	ret->name = NULL;
	ret->file = NULL;
	ret->type = WIN_TYPE_UNSET;
	ret->visible = 0;
	ret->caption_entry = 0;
	ret->force_aliasing = opt.force_aliasing;

	/* Zoom stuff */
	ret->mode = MODE_NORMAL;

	ret->gc = None;

	/* New stuff */
	ret->im_x = 0;
	ret->im_y = 0;
	ret->zoom = 1.0;
	ret->old_zoom = 1.0;

	ret->click_offset_x = 0;
	ret->click_offset_y = 0;
	ret->has_rotated = 0;

Sven Willner's avatar
Sven Willner committed
#ifdef HAVE_INOTIFY
    ret->inotify_wd = -1;
#endif

winwidget winwidget_create_from_image(Imlib_Image im, char *name, char type)
	ret = winwidget_allocate();
	ret->type = type;
	ret->im = im;
	ret->w = ret->im_w = gib_imlib_image_get_width(ret->im);
	ret->h = ret->im_h = gib_imlib_image_get_height(ret->im);
	if (name)
		ret->name = estrdup(name);
	else
		ret->name = estrdup(PACKAGE);
	if (opt.full_screen && (type != WIN_TYPE_THUMBNAIL))
		ret->full_screen = True;
	winwidget_create_window(ret, ret->w, ret->h);
	winwidget_render_image(ret, 1, 0);
winwidget winwidget_create_from_file(gib_list * list, char *name, char type)
	winwidget ret = NULL;
	feh_file *file = FEH_FILE(list->data);

	if (!file || !file->filename)

	ret = winwidget_allocate();
	ret->file = list;
	ret->type = type;
	if (name)
		ret->name = estrdup(name);
	else
		ret->name = estrdup(file->filename);

	if (winwidget_loadimage(ret, file) == 0) {
		winwidget_destroy(ret);
	}

	if (!ret->win) {
		ret->w = ret->im_w = gib_imlib_image_get_width(ret->im);
		ret->h = ret->im_h = gib_imlib_image_get_height(ret->im);
		D(("image is %dx%d pixels, format %s\n", ret->w, ret->h, gib_imlib_image_format(ret->im)));
		if (opt.full_screen)
			ret->full_screen = True;
		winwidget_create_window(ret, ret->w, ret->h);
		winwidget_render_image(ret, 1, 0);
void winwidget_create_window(winwidget ret, int w, int h)
	XSetWindowAttributes attr;
	XEvent ev;
	XClassHint *xch;
	MWMHints mwmhints;
	Atom prop = None;
#ifdef HOST_NAME_MAX
	char hostname[HOST_NAME_MAX];
#else /* ! HOST_NAME_MAX */
	long host_name_max;
Niclas Zeising's avatar
Niclas Zeising committed
	char *hostname;
#endif /* HOST_NAME_MAX */
	D(("winwidget_create_window %dx%d\n", w, h));
	if (ret->full_screen) {
		w = scr->width;
		h = scr->height;

#ifdef HAVE_LIBXINERAMA
		if (opt.xinerama && xinerama_screens) {
			w = xinerama_screens[xinerama_screen].width;
			h = xinerama_screens[xinerama_screen].height;
			x = xinerama_screens[xinerama_screen].x_org;
			y = xinerama_screens[xinerama_screen].y_org;
		}
#endif				/* HAVE_LIBXINERAMA */
	} else if (opt.geom_flags) {
		if (opt.geom_flags & WidthValue) {
			w = opt.geom_w;
		}
		if (opt.geom_flags & HeightValue) {
			h = opt.geom_h;
		}
		if (opt.geom_flags & XValue) {
			if (opt.geom_flags & XNegative) {
				x = scr->width - opt.geom_x;
			} else {
				x = opt.geom_x;
			}
		}
		if (opt.geom_flags & YValue) {
			if (opt.geom_flags & YNegative) {
				y = scr->height - opt.geom_y;
			} else {
				y = opt.geom_y;
			}
		}
	} else if (opt.screen_clip) {
		if (w > scr->width)
			w = scr->width;
		if (h > scr->height)
			h = scr->height;

#ifdef HAVE_LIBXINERAMA
		if (opt.xinerama && xinerama_screens) {
			if (w > xinerama_screens[xinerama_screen].width)
				w = xinerama_screens[xinerama_screen].width;
			if (h > xinerama_screens[xinerama_screen].height)
				h = xinerama_screens[xinerama_screen].height;
		}
#endif				/* HAVE_LIBXINERAMA */
	}

	if (opt.paused) {
		printf("name %s\n", ret->name);
		tmpname = estrjoin(" ", ret->name, "[Paused]", NULL);
		free(ret->name);
		ret->name = tmpname;
	}

	ret->x = x;
	ret->y = y;
	ret->w = w;
	ret->h = h;
	ret->visible = False;

	attr.backing_store = NotUseful;
	attr.override_redirect = False;
	attr.colormap = cm;
	attr.border_pixel = 0;
	attr.background_pixel = 0;
	attr.save_under = False;
	attr.event_mask =
	    StructureNotifyMask | ButtonPressMask | ButtonReleaseMask |
	    PointerMotionMask | EnterWindowMask | LeaveWindowMask |
	    KeyPressMask | KeyReleaseMask | ButtonMotionMask | ExposureMask
	    | FocusChangeMask | PropertyChangeMask | VisibilityChangeMask;

Brian Mattern's avatar
Brian Mattern committed
	memset(&mwmhints, 0, sizeof(mwmhints));
	if (opt.borderless || ret->full_screen) {
		prop = XInternAtom(disp, "_MOTIF_WM_HINTS", True);
		if (prop == None) {
			weprintf
			    ("Window Manager does not support MWM hints. "
			     "To get a borderless window I have to bypass your wm.");
			attr.override_redirect = True;
			mwmhints.flags = 0;
		} else {
			mwmhints.flags = MWM_HINTS_DECORATIONS;
			mwmhints.decorations = 0;
		}

	ret->win =
	    XCreateWindow(disp, DefaultRootWindow(disp), x, y, w, h, 0,
			  depth, InputOutput, vis,
			  CWOverrideRedirect | CWSaveUnder | CWBackingStore
			  | CWColormap | CWBackPixel | CWBorderPixel | CWEventMask, &attr);

	if (mwmhints.flags) {
		XChangeProperty(disp, ret->win, prop, prop, 32,
				PropModeReplace, (unsigned char *) &mwmhints, PROP_MWM_HINTS_ELEMENTS);
	}
	if (ret->full_screen) {
		Atom prop_fs = XInternAtom(disp, "_NET_WM_STATE_FULLSCREEN", False);
		Atom prop_state = XInternAtom(disp, "_NET_WM_STATE", False);

		memset(&ev, 0, sizeof(ev));
		ev.xclient.type = ClientMessage;
		ev.xclient.message_type = prop_state;
		ev.xclient.display = disp;
		ev.xclient.window = ret->win;
		ev.xclient.format = 32;
		XChangeProperty(disp, ret->win, prop_state, XA_ATOM, 32,
				PropModeReplace, (unsigned char *) &prop_fs, 1);
	pid = getpid();
	prop = XInternAtom(disp, "_NET_WM_PID", False);
	XChangeProperty(disp, ret->win, prop, XA_CARDINAL, sizeof(pid_t) * 8,
			PropModeReplace, (const unsigned char *)&pid, 1);

#ifdef HOST_NAME_MAX
	if (gethostname(hostname, HOST_NAME_MAX) == 0) {
		hostname[HOST_NAME_MAX-1] = '\0';
		prop = XInternAtom(disp, "WM_CLIENT_MACHINE", False);
		XChangeProperty(disp, ret->win, prop, XA_STRING, sizeof(char) * 8,
				PropModeReplace, (unsigned char *)hostname, strlen(hostname));
	}
#else /* ! HOST_NAME_MAX */
Niclas Zeising's avatar
Niclas Zeising committed
	if ((host_name_max = sysconf(_SC_HOST_NAME_MAX)) != -1 ) {
		if ((hostname = calloc(1, host_name_max + 1)) != NULL ) {
			if (gethostname(hostname, host_name_max) == 0) {
				prop = XInternAtom(disp, "WM_CLIENT_MACHINE", False);
				XChangeProperty(disp, ret->win, prop, XA_STRING, sizeof(char) * 8,
						PropModeReplace, (unsigned char *)hostname, strlen(hostname));
			}
Niclas Zeising's avatar
Niclas Zeising committed
		}
#endif /* HOST_NAME_MAX */
	XSetWMProtocols(disp, ret->win, &wmDeleteWindow, 1);
	winwidget_update_title(ret);
	xch = XAllocClassHint();
	xch->res_name = "feh";
	xch->res_class = "feh";
	XSetClassHint(disp, ret->win, xch);
	XFree(xch);

	/* Size hints */
	if (ret->full_screen || opt.geom_flags) {
		XSizeHints xsz;

		xsz.flags = USPosition;
		xsz.x = x;
		xsz.y = y;
		XSetWMNormalHints(disp, ret->win, &xsz);
		XMoveWindow(disp, ret->win, x, y);
	}
		winwidget_set_pointer(ret, 0);

	/* set the icon name property */
	XSetIconName(disp, ret->win, "feh");
	/* set the command hint */
	XSetCommand(disp, ret->win, cmdargv, cmdargc);

	winwidget_register(ret);

	/* do not scale down a thumbnail list window, only those created from it */
	if (opt.scale_down && (ret->type != WIN_TYPE_THUMBNAIL)) {
		opt.geom_w = w;
		opt.geom_h = h;
		opt.geom_flags |= WidthValue | HeightValue;
	}
void winwidget_update_title(winwidget ret)
	Atom prop_name = XInternAtom(disp, "_NET_WM_NAME", False);
	Atom prop_icon = XInternAtom(disp, "_NET_WM_ICON_NAME", False);
	Atom prop_utf8 = XInternAtom(disp, "UTF8_STRING", False);
	D(("winwid->name = %s\n", ret->name));
	name = ret->name ? ret->name : "feh";
	XStoreName(disp, ret->win, name);
	XSetIconName(disp, ret->win, name);

	XChangeProperty(disp, ret->win, prop_name, prop_utf8, 8,
			PropModeReplace, (const unsigned char *)name, strlen(name));

	XChangeProperty(disp, ret->win, prop_icon, prop_utf8, 8,
			PropModeReplace, (const unsigned char *)name, strlen(name));

void winwidget_update_caption(winwidget winwid)
{
	if (opt.caption_path) {
		/* TODO: Does someone understand the caching here. Is this the right
		 * approach now that I have broken this out into a separate function. -lsmith */

		/* cache bg pixmap. during caption entry, multiple redraws are done
		 * because the caption overlay changes - the image doesn't though, so re-
		 * rendering that is a waste of time */
		if (winwid->caption_entry) {
			GC gc;
			if (winwid->bg_pmap_cache)
				XFreePixmap(disp, winwid->bg_pmap_cache);
			winwid->bg_pmap_cache = XCreatePixmap(disp, winwid->win, winwid->w, winwid->h, depth);
			gc = XCreateGC(disp, winwid->win, 0, NULL);
			XCopyArea(disp, winwid->bg_pmap, winwid->bg_pmap_cache, gc, 0, 0, winwid->w, winwid->h, 0, 0);
			XFreeGC(disp, gc);
		}
		feh_draw_caption(winwid);
	}
	return;
}

void winwidget_setup_pixmaps(winwidget winwid)
	if (winwid->full_screen) {
		if (!(winwid->bg_pmap)) {
			if (winwid->gc == None) {
				XGCValues gcval;

				if (opt.image_bg == IMAGE_BG_WHITE) {
					gcval.foreground = WhitePixel(disp, DefaultScreen(disp));
					winwid->gc = XCreateGC(disp, winwid->win, GCForeground, &gcval);
				}
				else if (opt.image_bg == IMAGE_BG_CHECKS) {
					gcval.tile = feh_create_checks();
					gcval.fill_style = FillTiled;
					winwid->gc = XCreateGC(disp, winwid->win, GCTile | GCFillStyle, &gcval);
				}
				else {
					gcval.foreground = BlackPixel(disp, DefaultScreen(disp));
					winwid->gc = XCreateGC(disp, winwid->win, GCForeground, &gcval);
				}
			}
			winwid->bg_pmap = XCreatePixmap(disp, winwid->win, scr->width, scr->height, depth);
		}
		XFillRectangle(disp, winwid->bg_pmap, winwid->gc, 0, 0, scr->width, scr->height);
	} else {
		if (!winwid->bg_pmap || winwid->had_resize) {
			D(("recreating background pixmap (%dx%d)\n", winwid->w, winwid->h));
			if (winwid->bg_pmap)
				XFreePixmap(disp, winwid->bg_pmap);

			if (winwid->w == 0)
				winwid->w = 1;
			if (winwid->h == 0)
				winwid->h = 1;
			winwid->bg_pmap = XCreatePixmap(disp, winwid->win, winwid->w, winwid->h, depth);
			winwid->had_resize = 0;
		}
	}
void winwidget_render_image(winwidget winwid, int resize, int force_alias)
	int sx, sy, sw, sh, dx, dy, dw, dh;
	int calc_w, calc_h;
	int need_center = winwid->had_resize;

	if (!winwid->full_screen && resize) {
Birte Kristina Friesel's avatar
Loading
Loading full blame...