summaryrefslogtreecommitdiff
path: root/driver/xf86-video-geode/src/gx_randr.c
blob: c0ca3b97884c8bbe91acf63f85d37d259658194d (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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/* 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

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

#include "geode.h"

static int GXRandRGeneration;

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

#if HAS_DEVPRIVATEKEYREC
static DevPrivateKeyRec GXRandRIndex;
#else
static int GXRandRIndex;
#endif

#define OLD_VIDEODRV_INTERFACE (GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) < 4)

#if OLD_VIDEODRV_INTERFACE
#define XF86RANDRINFO(p)   ((XF86RandRInfoPtr) (p)->devPrivates[GXRandRIndex].ptr)
#define XF86RANDRSET(p, v) (p)->devPrivates[GXRandRIndex].ptr = v
#else
#define XF86RANDRINFO(p) ((XF86RandRInfoPtr)						\
			  dixLookupPrivate(&(p)->devPrivates, &GXRandRIndex))
#define XF86RANDRSET(p, v) dixSetPrivate(&(p)->devPrivates, &GXRandRIndex, v)
#endif

static int
GXRandRModeRefresh(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
GXRandRGetInfo(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 = GXRandRModeRefresh(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
GXRandRSetMode(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;

#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) < 8
    WindowPtr pRoot = WindowTable[pScreen->myNum];
#else
    WindowPtr pRoot = pScreen->root;
#endif
    DisplayModePtr currentMode = NULL;
    Bool ret = TRUE;

#if XORG_VERSION_CURRENT < XORG_VERSION_NUMERIC(1,9,99,1,0)
    PixmapPtr pspix = NULL;
#endif

    if (pRoot)
        (*pScrni->
         EnableDisableFBAccess) (XF86_ENABLEDISABLEFB_ARG(pScrni, 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;
    }

#if XORG_VERSION_CURRENT < XORG_VERSION_NUMERIC(1,9,99,1,0)
    /*
     * 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;
#endif

    xf86ReconfigureLayout();

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

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

    return ret;
}

Bool
GXRandRSetConfig(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;
    }

/* FIXME: we don't have a new video ABI yet */
#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 3
    miPointerGetPosition(inputInfo.pointer, &px, &py);
#else
    miPointerPosition(&px, &py);
#endif

    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 || GXRandRModeRefresh(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 (!GXRandRSetMode(pScreen, mode, useVirtual, pSize->mmWidth,
                        pSize->mmHeight)) {
        pRandr->rotation = oldRotation;
        return FALSE;
    }

/* FIXME: we don't have a new video ABI yet */
#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 3
    if (pScreen == miPointerGetScreen(inputInfo.pointer))
#else
    if (pScreen == miPointerCurrentScreen())
#endif
    {
        px = (px >= pScreen->width ? (pScreen->width - 1) : px);
        py = (py >= pScreen->height ? (pScreen->height - 1) : py);

        xf86SetViewport(pScreen, px, py);

/* FIXME: we don't have a new video ABI yet */
        (*pScreen->SetCursorPosition) (
#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 3
                                          inputInfo.pointer,
#endif
                                          pScreen, px, py, FALSE);
    }

    return TRUE;
}

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

    return pRandr->rotation;
}

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

    if (GXRandRGeneration != serverGeneration) {
        GXRandRGeneration = serverGeneration;
    }
#if OLD_VIDEODRV_INTERFACE
    GXRandRIndex = AllocateScreenPrivateIndex();
#endif
#if HAS_DIXREGISTERPRIVATEKEY
    if (!dixRegisterPrivateKey(&GXRandRIndex, PRIVATE_SCREEN, 0))
        return FALSE;
#endif

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

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

    rp = rrGetScrPriv(pScreen);
    rp->rrGetInfo = GXRandRGetInfo;
    rp->rrSetConfig = GXRandRSetConfig;

    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;

    XF86RANDRSET(pScreen, pRandr);

    return TRUE;
}