rrinfo.c 8.8 KB
Newer Older
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
/*
 * Copyright © 2006 Keith Packard
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

#include "randrstr.h"

#ifdef RANDR_10_INTERFACE
static RRModePtr
27
RROldModeAdd(RROutputPtr output, RRScreenSizePtr size, int refresh)
28
{
29 30
    ScreenPtr pScreen = output->pScreen;

31
    rrScrPriv(pScreen);
32 33 34 35 36 37 38 39 40
    xRRModeInfo modeInfo;
    char name[100];
    RRModePtr mode;
    int i;
    RRModePtr *modes;

    memset(&modeInfo, '\0', sizeof(modeInfo));
    snprintf(name, sizeof(name), "%dx%d", size->width, size->height);

41 42 43 44 45
    modeInfo.width = size->width;
    modeInfo.height = size->height;
    modeInfo.hTotal = size->width;
    modeInfo.vTotal = size->height;
    modeInfo.dotClock = ((CARD32) size->width * (CARD32) size->height *
46 47 48
                         (CARD32) refresh);
    modeInfo.nameLength = strlen(name);
    mode = RRModeGet(&modeInfo, name);
49
    if (!mode)
50
        return NULL;
51
    for (i = 0; i < output->numModes; i++)
52 53 54 55 56
        if (output->modes[i] == mode) {
            RRModeDestroy(mode);
            return mode;
        }

57
    if (output->numModes)
58 59
        modes = reallocarray(output->modes,
                             output->numModes + 1, sizeof(RRModePtr));
60
    else
61 62 63 64 65
        modes = malloc(sizeof(RRModePtr));
    if (!modes) {
        RRModeDestroy(mode);
        FreeResource(mode->mode.id, 0);
        return NULL;
66 67 68 69 70
    }
    modes[output->numModes++] = mode;
    output->modes = modes;
    output->changed = TRUE;
    pScrPriv->changed = TRUE;
71
    pScrPriv->configChanged = TRUE;
72 73 74 75
    return mode;
}

static void
76
RRScanOldConfig(ScreenPtr pScreen, Rotation rotations)
77 78
{
    rrScrPriv(pScreen);
79 80 81 82 83 84
    RROutputPtr output;
    RRCrtcPtr crtc;
    RRModePtr mode, newMode = NULL;
    int i;
    CARD16 minWidth = MAXSHORT, minHeight = MAXSHORT;
    CARD16 maxWidth = 0, maxHeight = 0;
85
    CARD16 width, height;
86

87 88 89 90
    /*
     * First time through, create a crtc and output and hook
     * them together
     */
91 92 93 94 95 96 97 98 99 100
    if (pScrPriv->numOutputs == 0 && pScrPriv->numCrtcs == 0) {
        crtc = RRCrtcCreate(pScreen, NULL);
        if (!crtc)
            return;
        output = RROutputCreate(pScreen, "default", 7, NULL);
        if (!output)
            return;
        RROutputSetCrtcs(output, &crtc, 1);
        RROutputSetConnection(output, RR_Connected);
        RROutputSetSubpixelOrder(output, PictureGetSubpixelOrder(pScreen));
101 102
    }

103
    output = pScrPriv->outputs[0];
104
    if (!output)
105
        return;
106 107
    crtc = pScrPriv->crtcs[0];
    if (!crtc)
108
        return;
109 110

    /* check rotations */
111
    if (rotations != crtc->rotations) {
112
        crtc->rotations = rotations;
113 114
        crtc->changed = TRUE;
        pScrPriv->changed = TRUE;
115
    }
116

117
    /* regenerate mode list */
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    for (i = 0; i < pScrPriv->nSizes; i++) {
        RRScreenSizePtr size = &pScrPriv->pSizes[i];
        int r;

        if (size->nRates) {
            for (r = 0; r < size->nRates; r++) {
                mode = RROldModeAdd(output, size, size->pRates[r].rate);
                if (i == pScrPriv->size &&
                    size->pRates[r].rate == pScrPriv->rate) {
                    newMode = mode;
                }
            }
            free(size->pRates);
        }
        else {
            mode = RROldModeAdd(output, size, 0);
            if (i == pScrPriv->size)
                newMode = mode;
        }
137 138
    }
    if (pScrPriv->nSizes)
139
        free(pScrPriv->pSizes);
140 141
    pScrPriv->pSizes = NULL;
    pScrPriv->nSizes = 0;
142

143
    /* find size bounds */
144
    for (i = 0; i < output->numModes + output->numUserModes; i++) {
145
        mode = (i < output->numModes ?
146 147
                          output->modes[i] :
                          output->userModes[i - output->numModes]);
148 149
        width = mode->mode.width;
        height = mode->mode.height;
150 151 152 153 154 155 156 157 158

        if (width < minWidth)
            minWidth = width;
        if (width > maxWidth)
            maxWidth = width;
        if (height < minHeight)
            minHeight = height;
        if (height > maxHeight)
            maxHeight = height;
159 160
    }

161
    RRScreenSetSizeRange(pScreen, minWidth, minHeight, maxWidth, maxHeight);
162 163 164

    /* notice current mode */
    if (newMode)
165
        RRCrtcNotify(crtc, newMode, 0, 0, pScrPriv->rotation, NULL, 1, &output);
166 167 168 169 170 171
}
#endif

/*
 * Poll the driver for changed information
 */
172
Bool
173
RRGetInfo(ScreenPtr pScreen, Bool force_query)
174
{
175 176 177
    rrScrPriv(pScreen);
    Rotation rotations;
    int i;
178

179 180 181 182
    /* Return immediately if we don't need to re-query and we already have the
     * information.
     */
    if (!force_query) {
183 184
        if (pScrPriv->numCrtcs != 0 || pScrPriv->numOutputs != 0)
            return TRUE;
185 186
    }

187
    for (i = 0; i < pScrPriv->numOutputs; i++)
188
        pScrPriv->outputs[i]->changed = FALSE;
189
    for (i = 0; i < pScrPriv->numCrtcs; i++)
190 191
        pScrPriv->crtcs[i]->changed = FALSE;

192 193
    rotations = 0;
    pScrPriv->changed = FALSE;
194
    pScrPriv->configChanged = FALSE;
195

196
    if (!(*pScrPriv->rrGetInfo) (pScreen, &rotations))
197
        return FALSE;
198 199 200

#if RANDR_10_INTERFACE
    if (pScrPriv->nSizes)
201
        RRScanOldConfig(pScreen, rotations);
202
#endif
203
    RRTellChanged(pScreen);
204 205 206 207 208 209
    return TRUE;
}

/*
 * Register the range of sizes for the screen
 */
210
void
211 212 213
RRScreenSetSizeRange(ScreenPtr pScreen,
                     CARD16 minWidth,
                     CARD16 minHeight, CARD16 maxWidth, CARD16 maxHeight)
214
{
215
    rrScrPriv(pScreen);
216 217

    if (!pScrPriv)
218
        return;
219
    if (pScrPriv->minWidth == minWidth && pScrPriv->minHeight == minHeight &&
220 221
        pScrPriv->maxWidth == maxWidth && pScrPriv->maxHeight == maxHeight) {
        return;
222
    }
223 224

    pScrPriv->minWidth = minWidth;
225
    pScrPriv->minHeight = minHeight;
226
    pScrPriv->maxWidth = maxWidth;
227
    pScrPriv->maxHeight = maxHeight;
228
    RRSetChanged(pScreen);
229
    pScrPriv->configChanged = TRUE;
230 231 232 233
}

#ifdef RANDR_10_INTERFACE
static Bool
234
RRScreenSizeMatches(RRScreenSizePtr a, RRScreenSizePtr b)
235 236
{
    if (a->width != b->width)
237
        return FALSE;
238
    if (a->height != b->height)
239
        return FALSE;
240
    if (a->mmWidth != b->mmWidth)
241
        return FALSE;
242
    if (a->mmHeight != b->mmHeight)
243
        return FALSE;
244 245 246
    return TRUE;
}

247
RRScreenSizePtr
248 249
RRRegisterSize(ScreenPtr pScreen,
               short width, short height, short mmWidth, short mmHeight)
250
{
251 252 253
    rrScrPriv(pScreen);
    int i;
    RRScreenSize tmp;
254 255 256
    RRScreenSizePtr pNew;

    if (!pScrPriv)
257 258
        return 0;

259 260
    tmp.id = 0;
    tmp.width = width;
261
    tmp.height = height;
262 263 264 265 266
    tmp.mmWidth = mmWidth;
    tmp.mmHeight = mmHeight;
    tmp.pRates = 0;
    tmp.nRates = 0;
    for (i = 0; i < pScrPriv->nSizes; i++)
267 268
        if (RRScreenSizeMatches(&tmp, &pScrPriv->pSizes[i]))
            return &pScrPriv->pSizes[i];
269 270
    pNew = reallocarray(pScrPriv->pSizes,
                        pScrPriv->nSizes + 1, sizeof(RRScreenSize));
271
    if (!pNew)
272
        return 0;
273 274
    pNew[pScrPriv->nSizes++] = tmp;
    pScrPriv->pSizes = pNew;
275
    return &pNew[pScrPriv->nSizes - 1];
276 277
}

278 279
Bool
RRRegisterRate(ScreenPtr pScreen, RRScreenSizePtr pSize, int rate)
280 281
{
    rrScrPriv(pScreen);
282
    int i;
283 284 285
    RRScreenRatePtr pNew, pRate;

    if (!pScrPriv)
286 287
        return FALSE;

288
    for (i = 0; i < pSize->nRates; i++)
289 290
        if (pSize->pRates[i].rate == rate)
            return TRUE;
291

292
    pNew = reallocarray(pSize->pRates, pSize->nRates + 1, sizeof(RRScreenRate));
293
    if (!pNew)
294
        return FALSE;
295 296 297 298 299 300
    pRate = &pNew[pSize->nRates++];
    pRate->rate = rate;
    pSize->pRates = pNew;
    return TRUE;
}

301
Rotation
302 303
RRGetRotation(ScreenPtr pScreen)
{
304
    RROutputPtr output = RRFirstOutput(pScreen);
305 306

    if (!output)
307
        return RR_Rotate_0;
308 309 310 311

    return output->crtc->rotation;
}

312
void
313 314
RRSetCurrentConfig(ScreenPtr pScreen,
                   Rotation rotation, int rate, RRScreenSizePtr pSize)
315
{
316
    rrScrPriv(pScreen);
317 318

    if (!pScrPriv)
319
        return;
320 321 322 323 324
    pScrPriv->size = pSize - pScrPriv->pSizes;
    pScrPriv->rotation = rotation;
    pScrPriv->rate = rate;
}
#endif