2 * Copyright © 2006 Keith Packard
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 RRModeEqual (xRRModeInfo *a, xRRModeInfo *b)
30 if (a->width != b->width) return FALSE;
31 if (a->height != b->height) return FALSE;
32 if (a->dotClock != b->dotClock) return FALSE;
33 if (a->hSyncStart != b->hSyncStart) return FALSE;
34 if (a->hSyncEnd != b->hSyncEnd) return FALSE;
35 if (a->hTotal != b->hTotal) return FALSE;
36 if (a->hSkew != b->hSkew) return FALSE;
37 if (a->vSyncStart != b->vSyncStart) return FALSE;
38 if (a->vSyncEnd != b->vSyncEnd) return FALSE;
39 if (a->vTotal != b->vTotal) return FALSE;
40 if (a->nameLength != b->nameLength) return FALSE;
41 if (a->modeFlags != b->modeFlags) return FALSE;
46 * Keep a list so it's easy to find modes in the resource database.
49 static RRModePtr *modes;
52 RRModeCreate (xRRModeInfo *modeInfo,
56 RRModePtr mode, *newModes;
61 mode = malloc(sizeof (RRModeRec) + modeInfo->nameLength + 1);
65 mode->mode = *modeInfo;
66 mode->name = (char *) (mode + 1);
67 memcpy (mode->name, name, modeInfo->nameLength);
68 mode->name[modeInfo->nameLength] = '\0';
69 mode->userScreen = userScreen;
72 newModes = realloc(modes, (num_modes + 1) * sizeof (RRModePtr));
74 newModes = malloc(sizeof (RRModePtr));
82 mode->mode.id = FakeClientID(0);
83 if (!AddResource (mode->mode.id, RRModeType, (pointer) mode))
86 modes[num_modes++] = mode;
89 * give the caller a reference to this mode
96 RRModeFindByName (const char *name,
102 for (i = 0; i < num_modes; i++)
105 if (mode->mode.nameLength == nameLength &&
106 !memcmp (name, mode->name, nameLength))
115 RRModeGet (xRRModeInfo *modeInfo,
120 for (i = 0; i < num_modes; i++)
122 RRModePtr mode = modes[i];
123 if (RRModeEqual (&mode->mode, modeInfo) &&
124 !memcmp (name, mode->name, modeInfo->nameLength))
131 return RRModeCreate (modeInfo, name, NULL);
135 RRModeCreateUser (ScreenPtr pScreen,
136 xRRModeInfo *modeInfo,
142 mode = RRModeFindByName (name, modeInfo->nameLength);
149 mode = RRModeCreate (modeInfo, name, pScreen);
160 RRModesForScreen (ScreenPtr pScreen, int *num_ret)
164 RRModePtr *screen_modes;
165 int num_screen_modes = 0;
167 screen_modes = malloc((num_modes ? num_modes : 1) * sizeof (RRModePtr));
172 * Add modes from all outputs
174 for (o = 0; o < pScrPriv->numOutputs; o++)
176 RROutputPtr output = pScrPriv->outputs[o];
179 for (m = 0; m < output->numModes + output->numUserModes; m++)
181 RRModePtr mode = (m < output->numModes ?
183 output->userModes[m-output->numModes]);
184 for (n = 0; n < num_screen_modes; n++)
185 if (screen_modes[n] == mode)
187 if (n == num_screen_modes)
188 screen_modes[num_screen_modes++] = mode;
192 * Add modes from all crtcs. The goal is to
193 * make sure all available and active modes
194 * are visible to the client
196 for (c = 0; c < pScrPriv->numCrtcs; c++)
198 RRCrtcPtr crtc = pScrPriv->crtcs[c];
199 RRModePtr mode = crtc->mode;
203 for (n = 0; n < num_screen_modes; n++)
204 if (screen_modes[n] == mode)
206 if (n == num_screen_modes)
207 screen_modes[num_screen_modes++] = mode;
210 * Add all user modes for this screen
212 for (m = 0; m < num_modes; m++)
214 RRModePtr mode = modes[m];
217 if (mode->userScreen != pScreen)
219 for (n = 0; n < num_screen_modes; n++)
220 if (screen_modes[n] == mode)
222 if (n == num_screen_modes)
223 screen_modes[num_screen_modes++] = mode;
226 *num_ret = num_screen_modes;
231 RRModeDestroy (RRModePtr mode)
235 if (--mode->refcnt > 0)
237 for (m = 0; m < num_modes; m++)
239 if (modes[m] == mode)
241 memmove (modes + m, modes + m + 1,
242 (num_modes - m - 1) * sizeof (RRModePtr));
257 RRModeDestroyResource (pointer value, XID pid)
259 RRModeDestroy ((RRModePtr) value);
264 * Initialize mode type
269 assert (num_modes == 0);
270 assert (modes == NULL);
271 RRModeType = CreateNewResourceType (RRModeDestroyResource, "MODE");
279 * Initialize mode type error value
282 RRModeInitErrorValue(void)
284 SetResourceTypeErrorValue(RRModeType, RRErrorBase + BadRRMode);
288 ProcRRCreateMode (ClientPtr client)
290 REQUEST(xRRCreateModeReq);
291 xRRCreateModeReply rep;
294 rrScrPrivPtr pScrPriv;
295 xRRModeInfo *modeInfo;
301 REQUEST_AT_LEAST_SIZE (xRRCreateModeReq);
302 rc = dixLookupWindow(&pWin, stuff->window, client, DixGetAttrAccess);
306 pScreen = pWin->drawable.pScreen;
307 pScrPriv = rrGetScrPriv(pScreen);
309 modeInfo = &stuff->modeInfo;
310 name = (char *) (stuff + 1);
311 units_after = (stuff->length - bytes_to_int32(sizeof (xRRCreateModeReq)));
313 /* check to make sure requested name fits within the data provided */
314 if (bytes_to_int32(modeInfo->nameLength) > units_after)
317 mode = RRModeCreateUser (pScreen, modeInfo, name, &error);
323 rep.sequenceNumber = client->sequence;
325 rep.mode = mode->mode.id;
329 swaps(&rep.sequenceNumber, n);
330 swapl(&rep.length, n);
333 WriteToClient(client, sizeof(xRRCreateModeReply), (char *)&rep);
334 /* Drop out reference to this mode */
335 RRModeDestroy (mode);
340 ProcRRDestroyMode (ClientPtr client)
342 REQUEST(xRRDestroyModeReq);
345 REQUEST_SIZE_MATCH(xRRDestroyModeReq);
346 VERIFY_RR_MODE(stuff->mode, mode, DixDestroyAccess);
348 if (!mode->userScreen)
350 if (mode->refcnt > 1)
352 FreeResource (stuff->mode, 0);
357 ProcRRAddOutputMode (ClientPtr client)
359 REQUEST(xRRAddOutputModeReq);
363 REQUEST_SIZE_MATCH(xRRAddOutputModeReq);
364 VERIFY_RR_OUTPUT(stuff->output, output, DixReadAccess);
365 VERIFY_RR_MODE(stuff->mode, mode, DixUseAccess);
367 return RROutputAddUserMode (output, mode);
371 ProcRRDeleteOutputMode (ClientPtr client)
373 REQUEST(xRRDeleteOutputModeReq);
377 REQUEST_SIZE_MATCH(xRRDeleteOutputModeReq);
378 VERIFY_RR_OUTPUT(stuff->output, output, DixReadAccess);
379 VERIFY_RR_MODE(stuff->mode, mode, DixUseAccess);
381 return RROutputDeleteUserMode (output, mode);