XrrMode.c revision b042e37f
1/*
2 * Copyright © 2006 Keith Packard
3 *
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.
13 *
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
20 * OF THIS SOFTWARE.
21 */
22
23#ifdef HAVE_CONFIG_H
24#include <config.h>
25#endif
26
27#include <stdio.h>
28#include <X11/Xlib.h>
29/* we need to be able to manipulate the Display structure on events */
30#include <X11/Xlibint.h>
31#include <X11/extensions/render.h>
32#include <X11/extensions/Xrender.h>
33#include "Xrandrint.h"
34
35XRRModeInfo *
36XRRAllocModeInfo (char *name, int nameLength)
37{
38    XRRModeInfo	*mode_info;
39
40    mode_info = Xmalloc (sizeof (XRRModeInfo) + nameLength + 1);
41    if (!mode_info)
42	return NULL;
43    memset (mode_info, '\0', sizeof (XRRModeInfo));
44    mode_info->nameLength = nameLength;
45    mode_info->name = (char *) (mode_info + 1);
46    memcpy (mode_info->name, name, nameLength);
47    mode_info->name[nameLength] = '\0';
48    return mode_info;
49}
50
51RRMode
52XRRCreateMode (Display *dpy, Window window, XRRModeInfo *mode_info)
53{
54    XExtDisplayInfo	    *info = XRRFindDisplay(dpy);
55    xRRCreateModeReq	    *req;
56    xRRCreateModeReply	    rep;
57    long		    channelSize;
58
59    RRSimpleCheckExtension (dpy, info);
60
61    LockDisplay(dpy);
62    GetReq (RRCreateMode, req);
63    req->reqType = info->codes->major_opcode;
64    req->randrReqType = X_RRCreateMode;
65    req->length += (mode_info->nameLength + 3) >> 2;
66
67    req->window = window;
68
69    req->modeInfo.id = 0;
70    req->modeInfo.width = mode_info->width;
71    req->modeInfo.height = mode_info->height;
72    req->modeInfo.dotClock = mode_info->dotClock;
73    req->modeInfo.hSyncStart = mode_info->hSyncStart;
74    req->modeInfo.hSyncEnd = mode_info->hSyncEnd;
75    req->modeInfo.hTotal = mode_info->hTotal;
76    req->modeInfo.hSkew = mode_info->hSkew;
77    req->modeInfo.vSyncStart = mode_info->vSyncStart;
78    req->modeInfo.vSyncEnd = mode_info->vSyncEnd;
79    req->modeInfo.vTotal = mode_info->vTotal;
80    req->modeInfo.nameLength = mode_info->nameLength;
81    req->modeInfo.modeFlags = mode_info->modeFlags;
82
83    Data (dpy, mode_info->name, mode_info->nameLength);
84    if (!_XReply (dpy, (xReply *) &rep, 0, xFalse))
85    {
86	UnlockDisplay (dpy);
87	SyncHandle ();
88	return None;
89    }
90
91    UnlockDisplay (dpy);
92    SyncHandle ();
93    return rep.mode;
94}
95
96void
97XRRDestroyMode (Display *dpy, RRMode mode)
98{
99    XExtDisplayInfo	    *info = XRRFindDisplay(dpy);
100    xRRDestroyModeReq	    *req;
101    RRSimpleCheckExtension (dpy, info);
102
103    LockDisplay(dpy);
104    GetReq (RRDestroyMode, req);
105    req->reqType = info->codes->major_opcode;
106    req->randrReqType = X_RRDestroyMode;
107    req->mode = mode;
108    UnlockDisplay (dpy);
109    SyncHandle ();
110}
111
112void
113XRRAddOutputMode (Display *dpy, RROutput output, RRMode mode)
114{
115    XExtDisplayInfo	    *info = XRRFindDisplay(dpy);
116    xRRAddOutputModeReq	    *req;
117    RRSimpleCheckExtension (dpy, info);
118
119    LockDisplay(dpy);
120    GetReq (RRAddOutputMode, req);
121    req->reqType = info->codes->major_opcode;
122    req->randrReqType = X_RRAddOutputMode;
123    req->output = output;
124    req->mode = mode;
125    UnlockDisplay (dpy);
126    SyncHandle ();
127}
128
129void
130XRRDeleteOutputMode (Display *dpy, RROutput output, RRMode mode)
131{
132    XExtDisplayInfo	    *info = XRRFindDisplay(dpy);
133    xRRDeleteOutputModeReq  *req;
134    RRSimpleCheckExtension (dpy, info);
135
136    LockDisplay(dpy);
137    GetReq (RRDeleteOutputMode, req);
138    req->reqType = info->codes->major_opcode;
139    req->randrReqType = X_RRDeleteOutputMode;
140    req->output = output;
141    req->mode = mode;
142    UnlockDisplay (dpy);
143    SyncHandle ();
144}
145
146void
147XRRFreeModeInfo (XRRModeInfo *modeInfo)
148{
149    Xfree (modeInfo);
150}
151