summaryrefslogtreecommitdiff
path: root/src/screen.c
blob: 3ee95405f0d56902871b76972bbb8ff7670d53e6 (plain)
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
/*
 *                                  \/\/\
 *
 *  Copyright (C) 2012-2017  Vito Caputo - <vcaputo@pengaru.com>
 *
 *  This program is free software: you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License version 3 as published
 *  by the Free Software Foundation.
 *
 *  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 <http://www.gnu.org/licenses/>.
 */
/* return the appropriate screen, don't use the return value across event loops because randr events reallocate the array. */

#include <X11/Xlib.h>
#include <stdarg.h>
#include <values.h>

#include "list.h"
#include "screen.h"
#include "vwm.h"
#include "window.h"
#include "xwindow.h"

	/* Xinerama/multihead screen functions */

/* return what fraction (0.0-1.0) of vwin overlaps with scr */
static float vwm_screen_overlaps_xwin(vwm_t *vwm, const vwm_screen_t *scr, vwm_xwindow_t *xwin)
{
	float	pct = 0, xover = 0, yover = 0;

	if (scr->x_org + scr->width < xwin->attrs.x || scr->x_org > xwin->attrs.x + xwin->attrs.width ||
	    scr->y_org + scr->height < xwin->attrs.y || scr->y_org > xwin->attrs.y + xwin->attrs.height)
		goto _out;

	/* they overlap, by how much? */
	xover = MIN(scr->x_org + scr->width, xwin->attrs.x + xwin->attrs.width) - MAX(scr->x_org, xwin->attrs.x);
	yover = MIN(scr->y_org + scr->height, xwin->attrs.y + xwin->attrs.height) - MAX(scr->y_org, xwin->attrs.y);

	pct = (xover * yover) / (xwin->attrs.width * xwin->attrs.height);
_out:
	VWM_TRACE("xover=%f yover=%f width=%i height=%i pct=%.4f", xover, yover, xwin->attrs.width, xwin->attrs.height, pct);
	return pct;
}


const vwm_screen_t * vwm_screen_find(vwm_t *vwm, vwm_screen_rel_t rel, ...)
{
	static vwm_screen_t	faux;
	vwm_screen_t		*scr, *best = &faux; /* default to faux as best */
	int			i;

	faux.screen_number = 0;
	faux.x_org = 0;
	faux.y_org = 0;
	faux.width = WidthOfScreen(DefaultScreenOfDisplay(VWM_XDISPLAY(vwm)));
	faux.height = HeightOfScreen(DefaultScreenOfDisplay(VWM_XDISPLAY(vwm)));

	if (!vwm->xinerama_screens)
		goto _out;

#define for_each_screen(_tmp) \
	for (i = 0, _tmp = vwm->xinerama_screens; i < vwm->xinerama_screens_cnt; _tmp = &vwm->xinerama_screens[++i])

	switch (rel) {
		case VWM_SCREEN_REL_XWIN: {
			va_list		ap;
			vwm_xwindow_t	*xwin;
			float		best_pct = 0, this_pct;

			va_start(ap, rel);
			xwin = va_arg(ap, vwm_xwindow_t *);
			va_end(ap);

			for_each_screen(scr) {
				this_pct = vwm_screen_overlaps_xwin(vwm, scr, xwin);
				if (this_pct > best_pct) {
					best = scr;
					best_pct = this_pct;
				}
			}
			break;
		}

		case VWM_SCREEN_REL_POINTER: {
			int		root_x, root_y, win_x, win_y;
			unsigned int	mask;
			Window		root, child;

			/* get the pointer coordinates and find which screen it's in */
			XQueryPointer(VWM_XDISPLAY(vwm), VWM_XROOT(vwm), &root, &child, &root_x, &root_y, &win_x, &win_y, &mask);

			for_each_screen(scr) {
				if (root_x >= scr->x_org && root_x < scr->x_org + scr->width &&
				    root_y >= scr->y_org && root_y < scr->y_org + scr->height) {
					best = scr;
					break;
				}
			}
			break;
		}

		case VWM_SCREEN_REL_TOTAL: {
			short	x1 = MAXSHORT, y1 = MAXSHORT, x2 = MINSHORT, y2 = MINSHORT;
			/* find the smallest x_org and y_org, the highest x_org + width and y_org + height, those are the two corners of the total rect */
			for_each_screen(scr) {
				if (scr->x_org < x1)
					x1 = scr->x_org;

				if (scr->y_org < y1)
					y1 = scr->y_org;

				if (scr->x_org + scr->width > x2)
					x2 = scr->x_org + scr->width;

				if (scr->y_org + scr->height > y2)
					y2 = scr->y_org + scr->height;
			}
			faux.x_org = x1;
			faux.y_org = y1;
			faux.width = x2 - x1;
			faux.height = y2 - y1;
			best = &faux;
			break;
		}
	}
_out:
	VWM_TRACE("Found Screen #%i: %hix%hi @ %hi,%hi", best->screen_number, best->width, best->height, best->x_org, best->y_org);

	return best;
}


/* check if a screen contains any windows (assuming the current desktop) */
int vwm_screen_is_empty(vwm_t *vwm, const vwm_screen_t *scr, vwm_xwindow_t *ignore_xwin)
{
	vwm_xwindow_t	*xwin;
	int		is_empty = 1;

	list_for_each_entry(xwin, &vwm->xwindows, xwindows) {
		if (xwin == ignore_xwin || !xwin->client_mapped)
			continue;

		if (!xwin->managed || (xwin->managed->desktop == vwm->focused_desktop && !xwin->managed->shelved)) {
			/* XXX: it may make more sense to see what %age of the screen is overlapped by windows, and consider it empty if < some % */
			/*      This is just seeing if any window is predominantly within the specified screen, the rationale being if you had a focusable
			 *      window on the screen you would have used the keyboard to make windows go there; this function is only used in determining
			 *      wether a new window should go where the pointer is or not. */
			if (vwm_screen_overlaps_xwin(vwm, scr, xwin) >= 0.05) {
				is_empty = 0;
				break;
			}
		}
	}

	return is_empty;
}
© All Rights Reserved