~samuel-thibault/compiz/clone

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
/*
 * Compiz Core: OutputDevices class
 *
 * Copyright (c) 2012 Canonical Ltd.
 * 
 * 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 or substantial portions of the Software.
 * 
 * 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 OR COPYRIGHT HOLDERS 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 <core/option.h>
#include "outputdevices.h"
#include "core_options.h"

namespace compiz
{
namespace core
{

OutputDevices::OutputDevices () :
    outputDevs (),
    overlappingOutputs (false),
    currentOutputDev (0)
{
}

void
OutputDevices::setGeometryOnDevice (unsigned int const nOutput,
				    int                x,
				    int                y,
				    const int          width,
				    const int          height)
{
    if (outputDevs.size() < nOutput + 1)
	outputDevs.resize (nOutput + 1);

    outputDevs[nOutput].setGeometry (x, y, width, height);
}

void
OutputDevices::adoptDevices (unsigned int nOutput,
			     CompSize     *screen)
{
    /* make sure we have at least one output */
    if (!nOutput)
    {
	setGeometryOnDevice (nOutput, 0, 0, screen->width (), screen->height ());
	++nOutput;
    }

    if (outputDevs.size () > nOutput)
	outputDevs.resize (nOutput);

    char str[10];
    /* set name, width, height and update rect pointers in all regions */
    for (unsigned int i = 0; i < nOutput; ++i)
    {
	snprintf(str, 10, "Output %u", i);
	outputDevs[i].setId (str, i);
    }

    overlappingOutputs = false;
    setCurrentOutput (currentOutputDev);

    for (unsigned int i = 0; i < nOutput - 1; ++i)
	for (unsigned int j = i + 1; j < nOutput; ++j)
	    if (outputDevs[i].intersects (outputDevs[j]))
		overlappingOutputs = true;
}

void
OutputDevices::setCurrentOutput (unsigned int outputNum)
{
    if (outputNum >= outputDevs.size ())
	outputNum = 0;

    currentOutputDev = outputNum;
}

int
OutputDevices::outputDeviceForGeometry (const CompWindow::Geometry &gm,
					int                        strategy,
					CompSize                   *screen) const
{
    if (outputDevs.size () == 1)
	return 0;

    int          overlapAreas[outputDevs.size ()];
    int          highest, seen, highestScore;
    unsigned int i;
    CompRect     geomRect, overlap;

    if (strategy == CoreOptions::OverlappingOutputsSmartMode)
    {
	/* We're only going to use geomRect for overlapping area calculations,
	   so the window rectangle is enough. We don't need to consider
	   anything more like the border because it will never be significant
	   to the result */
	geomRect = gm;
    }
    else
    {
	/* for biggest/smallest modes, only use the window center to determine
	   the correct output device */
	int x = (gm.x () + (gm.width () / 2) + gm.border ()) % screen->width ();

	if (x < 0)
	    x += screen->width ();

	int y = (gm.y () + (gm.height () / 2) + gm.border ()) % screen->height ();

	if (y < 0)
	    y += screen->height ();

	geomRect.setGeometry (x, y, 1, 1);
    }

    /* get amount of overlap on all output devices */
    for (i = 0; i < outputDevs.size (); ++i)
    {
	overlap         = outputDevs[i] & geomRect;
	overlapAreas[i] = overlap.area ();
    }

    /* find output with largest overlap */
    for (i = 0, highest = 0, highestScore = 0; i < outputDevs.size (); ++i)
    {
	if (overlapAreas[i] > highestScore)
	{
	    highest      = i;
	    highestScore = overlapAreas[i];
	}
    }

    /* look if the highest score is unique */
    for (i = 0, seen = 0; i < outputDevs.size (); ++i)
	if (overlapAreas[i] == highestScore)
	    ++seen;

    if (seen > 1)
    {
	/* it's not unique, select one output of the matching ones and use the
	   user preferred strategy for that */
	unsigned int currentSize, bestOutputSize;
	bool         bestFit;

	bool searchLargest =
	    (strategy != CoreOptions::OverlappingOutputsPreferSmallerOutput);

	if (searchLargest)
	    bestOutputSize = 0;
	else
	    bestOutputSize = UINT_MAX;

	for (i = 0, highest = 0; i < outputDevs.size (); ++i)
	{
	    if (overlapAreas[i] == highestScore)
	    {
		currentSize = outputDevs[i].area ();

		if (searchLargest)
		    bestFit = (currentSize > bestOutputSize);
		else
		    bestFit = (currentSize < bestOutputSize);

		if (bestFit)
		{
		    highest        = i;
		    bestOutputSize = currentSize;
		}
	    }
	}
    }

    return highest;
}

} // namespace core
} // namespace compiz