summaryrefslogtreecommitdiff
path: root/src/lx_randr.c
blob: 6b12fefb0e089196ad21ea4592d76e477f64a33a (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
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
/* Originally derived from the Intel example
 * Copyright (C) 2002 Keith Packard, member of The XFree86 Project, Inc.

 * Copyright (c) 2006 Advanced Micro Devices, Inc.
 *
 * 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.
 *
 * Neither the name of the Advanced Micro Devices, Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 */

#include "xf86.h"
#include "os.h"
#include "mibank.h"
#include "globals.h"
#include "xf86.h"
#include "xf86Priv.h"
#include "xf86DDC.h"
#include "mipointer.h"
#include "windowstr.h"
#include <X11/extensions/randr.h>
#include <randrstr.h>

#include "geode.h"

static int LXRandRGeneration;

typedef struct _LXRandRInfo
{
    int virtualX;
    int virtualY;
    int mmWidth;
    int mmHeight;
    int maxX;
    int maxY;
    Rotation rotation;		       /* current mode */
    Rotation supported_rotations;      /* driver supported */
} XF86RandRInfoRec, *XF86RandRInfoPtr;

#define AMD_OLDPRIV (GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) < 4)
#if AMD_OLDPRIV

static int LXRandRIndex;

#define XF86RANDRINFO(p) ((XF86RandRInfoPtr) (p)->devPrivates[LXRandRIndex].ptr)

#else

static DevPrivateKey LXRandRKey;

#define XF86RANDRINFO(p) ((XF86RandRInfoPtr) \
			  dixLookupPrivate(&(p)->devPrivates, LXRandRKey));

#endif

static int
LXRandRModeRefresh(DisplayModePtr mode)
{
    if (mode->VRefresh)
	return (int)(mode->VRefresh + 0.5);
    else
	return (int)(mode->Clock * 1000.0 / mode->HTotal / mode->VTotal +
	    0.5);
}

static Bool
LXRandRGetInfo(ScreenPtr pScreen, Rotation * rotations)
{
    RRScreenSizePtr pSize;
    ScrnInfoPtr pScrni = XF86SCRNINFO(pScreen);
    XF86RandRInfoPtr pRandr = XF86RANDRINFO(pScreen);
    DisplayModePtr mode;
    int refresh0 = 60;
    int maxX = 0, maxY = 0;

    *rotations = pRandr->supported_rotations;

    if (pRandr->virtualX == -1 || pRandr->virtualY == -1) {
	pRandr->virtualX = pScrni->virtualX;
	pRandr->virtualY = pScrni->virtualY;
    }

    for (mode = pScrni->modes;; mode = mode->next) {
	int refresh = LXRandRModeRefresh(mode);

	if (pRandr->maxX == 0 || pRandr->maxY == 0) {
	    if (maxX < mode->HDisplay)
		maxX = mode->HDisplay;
	    if (maxY < mode->VDisplay)
		maxY = mode->VDisplay;
	}

	if (mode == pScrni->modes)
	    refresh0 = refresh;

	pSize = RRRegisterSize(pScreen,
	    mode->HDisplay, mode->VDisplay,
	    pRandr->mmWidth, pRandr->mmHeight);
	if (!pSize)
	    return FALSE;

	RRRegisterRate(pScreen, pSize, refresh);

	if (mode == pScrni->currentMode &&
	    mode->HDisplay == pScrni->virtualX
	    && mode->VDisplay == pScrni->virtualY)
	    RRSetCurrentConfig(pScreen, pRandr->rotation, refresh, pSize);
	if (mode->next == pScrni->modes)
	    break;
    }

    if (pRandr->maxX == 0 || pRandr->maxY == 0) {
	pRandr->maxX = maxX;
	pRandr->maxY = maxY;
    }

    if (pScrni->currentMode->HDisplay != pScrni->virtualX ||
	pScrni->currentMode->VDisplay != pScrni->virtualY) {

	mode = pScrni->modes;
	pSize = RRRegisterSize(pScreen,
	    pRandr->virtualX, pRandr->virtualY,
	    pRandr->mmWidth, pRandr->mmHeight);
	if (!pSize)
	    return FALSE;

	RRRegisterRate(pScreen, pSize, refresh0);
	if (pScrni->virtualX == pRandr->virtualX &&
	    pScrni->virtualY == pRandr->virtualY) {
	    RRSetCurrentConfig(pScreen, pRandr->rotation, refresh0, pSize);
	}
    }

    return TRUE;
}

static Bool
LXRandRSetMode(ScreenPtr pScreen,
    DisplayModePtr mode, Bool useVirtual, int mmWidth, int mmHeight)
{
    ScrnInfoPtr pScrni = XF86SCRNINFO(pScreen);
    XF86RandRInfoPtr pRandr = XF86RANDRINFO(pScreen);

    int oldWidth = pScreen->width;
    int oldHeight = pScreen->height;
    int oldmmWidth = pScreen->mmWidth;
    int oldmmHeight = pScreen->mmHeight;
    WindowPtr pRoot = WindowTable[pScreen->myNum];
    DisplayModePtr currentMode = NULL;
    Bool ret = TRUE;
    PixmapPtr pspix = NULL;

    if (pRoot)
	(*pScrni->EnableDisableFBAccess) (pScreen->myNum, FALSE);

    if (useVirtual) {
	pScrni->virtualX = pRandr->virtualX;
	pScrni->virtualY = pRandr->virtualY;
    } else {
	pScrni->virtualX = mode->HDisplay;
	pScrni->virtualY = mode->VDisplay;
    }

    if (pRandr->rotation & (RR_Rotate_90 | RR_Rotate_270)) {
	pScreen->width = pScrni->virtualY;
	pScreen->height = pScrni->virtualX;
	pScreen->mmWidth = mmHeight;
	pScreen->mmHeight = mmWidth;
    } else {
	pScreen->width = pScrni->virtualX;
	pScreen->height = pScrni->virtualY;
	pScreen->mmWidth = mmWidth;
	pScreen->mmHeight = mmHeight;
    }

    if (pScrni->currentMode == mode) {
	currentMode = pScrni->currentMode;
	pScrni->currentMode = NULL;
    }

    if (!xf86SwitchMode(pScreen, mode)) {
	ret = FALSE;
	pScrni->virtualX = pScreen->width = oldWidth;
	pScrni->virtualY = pScreen->height = oldHeight;
	pScreen->mmWidth = oldmmWidth;
	pScreen->mmHeight = oldmmHeight;
	pScrni->currentMode = currentMode;
    }

    /*
     * Get the new Screen pixmap ptr as SwitchMode might have called
     * ModifyPixmapHeader and xf86EnableDisableFBAccess will put it back...
     * Unfortunately.

     */

    pspix = (*pScreen->GetScreenPixmap) (pScreen);
    if (pspix->devPrivate.ptr)
	pScrni->pixmapPrivate = pspix->devPrivate;

    xf86ReconfigureLayout();

    xf86SetViewport(pScreen, pScreen->width, pScreen->height);
    xf86SetViewport(pScreen, 0, 0);

    if (pRoot)
	(*pScrni->EnableDisableFBAccess) (pScreen->myNum, TRUE);

    return ret;
}

Bool
LXRandRSetConfig(ScreenPtr pScreen, Rotation rotation,
    int rate, RRScreenSizePtr pSize)
{
    ScrnInfoPtr pScrni = XF86SCRNINFO(pScreen);
    XF86RandRInfoPtr pRandr = XF86RANDRINFO(pScreen);

    DisplayModePtr mode;
    int px, py;
    Bool useVirtual = FALSE;
    int maxX = 0, maxY = 0;
    Rotation oldRotation = pRandr->rotation;

    pRandr->rotation = rotation;

    if (pRandr->virtualX == -1 || pRandr->virtualY == -1) {
	pRandr->virtualX = pScrni->virtualX;
	pRandr->virtualY = pScrni->virtualY;
    }

    miPointerGetPosition(inputInfo.pointer, &px, &py);

    for (mode = pScrni->modes;; mode = mode->next) {
	if (pRandr->maxX == 0 || pRandr->maxY == 0) {
	    if (maxX < mode->HDisplay)
		maxX = mode->HDisplay;
	    if (maxY < mode->VDisplay)
		maxY = mode->VDisplay;
	}
	if (mode->HDisplay == pSize->width &&
	    mode->VDisplay == pSize->height &&
	    (rate == 0 || LXRandRModeRefresh(mode) == rate))
	    break;
	if (mode->next == pScrni->modes) {
	    if (pSize->width == pRandr->virtualX &&
		pSize->height == pRandr->virtualY) {
		mode = pScrni->modes;
		useVirtual = TRUE;
		break;
	    }
	    if (pRandr->maxX == 0 || pRandr->maxY == 0) {
		pRandr->maxX = maxX;
		pRandr->maxY = maxY;
	    }
	    return FALSE;
	}
    }

    if (pRandr->maxX == 0 || pRandr->maxY == 0) {
	pRandr->maxX = maxX;
	pRandr->maxY = maxY;
    }

    if (!LXRandRSetMode(pScreen, mode, useVirtual, pSize->mmWidth,
	    pSize->mmHeight)) {
	pRandr->rotation = oldRotation;
	return FALSE;
    }

    if (pScreen == miPointerGetScreen(inputInfo.pointer)) {
	px = (px >= pScreen->width ? (pScreen->width - 1) : px);
	py = (py >= pScreen->height ? (pScreen->height - 1) : py);

	xf86SetViewport(pScreen, px, py);

	(*pScreen->SetCursorPosition) (inputInfo.pointer, pScreen, px, py, FALSE);
    }

    return TRUE;
}

Rotation
LXGetRotation(ScreenPtr pScreen)
{
    XF86RandRInfoPtr pRandr = XF86RANDRINFO(pScreen);

    return pRandr ? pRandr->rotation : RR_Rotate_0;
}

Bool
LXRandRInit(ScreenPtr pScreen, int rotation)
{
    XF86RandRInfoPtr pRandr;
    rrScrPrivPtr rp;

    if (LXRandRGeneration != serverGeneration) {
	LXRandRGeneration = serverGeneration;
    }
#if AMD_OLDPRIV
    LXRandRIndex = AllocateScreenPrivateIndex();
#else
    LXRandRKey = &LXRandRKey;
#endif

    pRandr = xcalloc(sizeof(XF86RandRInfoRec), 1);
    if (pRandr == NULL)
	return FALSE;

    if (!RRScreenInit(pScreen)) {
	xfree(pRandr);
	return FALSE;
    }

    rp = rrGetScrPriv(pScreen);
    rp->rrGetInfo = LXRandRGetInfo;
    rp->rrSetConfig = LXRandRSetConfig;

    pRandr->virtualX = -1;
    pRandr->virtualY = -1;

    pRandr->mmWidth = pScreen->mmWidth;
    pRandr->mmHeight = pScreen->mmHeight;

    pRandr->rotation = RR_Rotate_0;
    pRandr->supported_rotations = rotation;
    pRandr->maxX = pRandr->maxY = 0;

#if AMD_OLDPRIV
    pScreen->devPrivates[LXRandRIndex].ptr = pRandr;
#else
    dixSetPrivate(&pScreen->devPrivates, LXRandRKey, pRandr);
#endif
    return TRUE;
}