XrrMode.c revision b042e37f
1b042e37fSmrg/*
2b042e37fSmrg * Copyright © 2006 Keith Packard
3b042e37fSmrg *
4b042e37fSmrg * Permission to use, copy, modify, distribute, and sell this software and its
5b042e37fSmrg * documentation for any purpose is hereby granted without fee, provided that
6b042e37fSmrg * the above copyright notice appear in all copies and that both that copyright
7b042e37fSmrg * notice and this permission notice appear in supporting documentation, and
8b042e37fSmrg * that the name of the copyright holders not be used in advertising or
9b042e37fSmrg * publicity pertaining to distribution of the software without specific,
10b042e37fSmrg * written prior permission.  The copyright holders make no representations
11b042e37fSmrg * about the suitability of this software for any purpose.  It is provided "as
12b042e37fSmrg * is" without express or implied warranty.
13b042e37fSmrg *
14b042e37fSmrg * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15b042e37fSmrg * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16b042e37fSmrg * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17b042e37fSmrg * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18b042e37fSmrg * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19b042e37fSmrg * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20b042e37fSmrg * OF THIS SOFTWARE.
21b042e37fSmrg */
22b042e37fSmrg
23b042e37fSmrg#ifdef HAVE_CONFIG_H
24b042e37fSmrg#include <config.h>
25b042e37fSmrg#endif
26b042e37fSmrg
27b042e37fSmrg#include <stdio.h>
28b042e37fSmrg#include <X11/Xlib.h>
29b042e37fSmrg/* we need to be able to manipulate the Display structure on events */
30b042e37fSmrg#include <X11/Xlibint.h>
31b042e37fSmrg#include <X11/extensions/render.h>
32b042e37fSmrg#include <X11/extensions/Xrender.h>
33b042e37fSmrg#include "Xrandrint.h"
34b042e37fSmrg
35b042e37fSmrgXRRModeInfo *
36b042e37fSmrgXRRAllocModeInfo (char *name, int nameLength)
37b042e37fSmrg{
38b042e37fSmrg    XRRModeInfo	*mode_info;
39b042e37fSmrg
40b042e37fSmrg    mode_info = Xmalloc (sizeof (XRRModeInfo) + nameLength + 1);
41b042e37fSmrg    if (!mode_info)
42b042e37fSmrg	return NULL;
43b042e37fSmrg    memset (mode_info, '\0', sizeof (XRRModeInfo));
44b042e37fSmrg    mode_info->nameLength = nameLength;
45b042e37fSmrg    mode_info->name = (char *) (mode_info + 1);
46b042e37fSmrg    memcpy (mode_info->name, name, nameLength);
47b042e37fSmrg    mode_info->name[nameLength] = '\0';
48b042e37fSmrg    return mode_info;
49b042e37fSmrg}
50b042e37fSmrg
51b042e37fSmrgRRMode
52b042e37fSmrgXRRCreateMode (Display *dpy, Window window, XRRModeInfo *mode_info)
53b042e37fSmrg{
54b042e37fSmrg    XExtDisplayInfo	    *info = XRRFindDisplay(dpy);
55b042e37fSmrg    xRRCreateModeReq	    *req;
56b042e37fSmrg    xRRCreateModeReply	    rep;
57b042e37fSmrg    long		    channelSize;
58b042e37fSmrg
59b042e37fSmrg    RRSimpleCheckExtension (dpy, info);
60b042e37fSmrg
61b042e37fSmrg    LockDisplay(dpy);
62b042e37fSmrg    GetReq (RRCreateMode, req);
63b042e37fSmrg    req->reqType = info->codes->major_opcode;
64b042e37fSmrg    req->randrReqType = X_RRCreateMode;
65b042e37fSmrg    req->length += (mode_info->nameLength + 3) >> 2;
66b042e37fSmrg
67b042e37fSmrg    req->window = window;
68b042e37fSmrg
69b042e37fSmrg    req->modeInfo.id = 0;
70b042e37fSmrg    req->modeInfo.width = mode_info->width;
71b042e37fSmrg    req->modeInfo.height = mode_info->height;
72b042e37fSmrg    req->modeInfo.dotClock = mode_info->dotClock;
73b042e37fSmrg    req->modeInfo.hSyncStart = mode_info->hSyncStart;
74b042e37fSmrg    req->modeInfo.hSyncEnd = mode_info->hSyncEnd;
75b042e37fSmrg    req->modeInfo.hTotal = mode_info->hTotal;
76b042e37fSmrg    req->modeInfo.hSkew = mode_info->hSkew;
77b042e37fSmrg    req->modeInfo.vSyncStart = mode_info->vSyncStart;
78b042e37fSmrg    req->modeInfo.vSyncEnd = mode_info->vSyncEnd;
79b042e37fSmrg    req->modeInfo.vTotal = mode_info->vTotal;
80b042e37fSmrg    req->modeInfo.nameLength = mode_info->nameLength;
81b042e37fSmrg    req->modeInfo.modeFlags = mode_info->modeFlags;
82b042e37fSmrg
83b042e37fSmrg    Data (dpy, mode_info->name, mode_info->nameLength);
84b042e37fSmrg    if (!_XReply (dpy, (xReply *) &rep, 0, xFalse))
85b042e37fSmrg    {
86b042e37fSmrg	UnlockDisplay (dpy);
87b042e37fSmrg	SyncHandle ();
88b042e37fSmrg	return None;
89b042e37fSmrg    }
90b042e37fSmrg
91b042e37fSmrg    UnlockDisplay (dpy);
92b042e37fSmrg    SyncHandle ();
93b042e37fSmrg    return rep.mode;
94b042e37fSmrg}
95b042e37fSmrg
96b042e37fSmrgvoid
97b042e37fSmrgXRRDestroyMode (Display *dpy, RRMode mode)
98b042e37fSmrg{
99b042e37fSmrg    XExtDisplayInfo	    *info = XRRFindDisplay(dpy);
100b042e37fSmrg    xRRDestroyModeReq	    *req;
101b042e37fSmrg    RRSimpleCheckExtension (dpy, info);
102b042e37fSmrg
103b042e37fSmrg    LockDisplay(dpy);
104b042e37fSmrg    GetReq (RRDestroyMode, req);
105b042e37fSmrg    req->reqType = info->codes->major_opcode;
106b042e37fSmrg    req->randrReqType = X_RRDestroyMode;
107b042e37fSmrg    req->mode = mode;
108b042e37fSmrg    UnlockDisplay (dpy);
109b042e37fSmrg    SyncHandle ();
110b042e37fSmrg}
111b042e37fSmrg
112b042e37fSmrgvoid
113b042e37fSmrgXRRAddOutputMode (Display *dpy, RROutput output, RRMode mode)
114b042e37fSmrg{
115b042e37fSmrg    XExtDisplayInfo	    *info = XRRFindDisplay(dpy);
116b042e37fSmrg    xRRAddOutputModeReq	    *req;
117b042e37fSmrg    RRSimpleCheckExtension (dpy, info);
118b042e37fSmrg
119b042e37fSmrg    LockDisplay(dpy);
120b042e37fSmrg    GetReq (RRAddOutputMode, req);
121b042e37fSmrg    req->reqType = info->codes->major_opcode;
122b042e37fSmrg    req->randrReqType = X_RRAddOutputMode;
123b042e37fSmrg    req->output = output;
124b042e37fSmrg    req->mode = mode;
125b042e37fSmrg    UnlockDisplay (dpy);
126b042e37fSmrg    SyncHandle ();
127b042e37fSmrg}
128b042e37fSmrg
129b042e37fSmrgvoid
130b042e37fSmrgXRRDeleteOutputMode (Display *dpy, RROutput output, RRMode mode)
131b042e37fSmrg{
132b042e37fSmrg    XExtDisplayInfo	    *info = XRRFindDisplay(dpy);
133b042e37fSmrg    xRRDeleteOutputModeReq  *req;
134b042e37fSmrg    RRSimpleCheckExtension (dpy, info);
135b042e37fSmrg
136b042e37fSmrg    LockDisplay(dpy);
137b042e37fSmrg    GetReq (RRDeleteOutputMode, req);
138b042e37fSmrg    req->reqType = info->codes->major_opcode;
139b042e37fSmrg    req->randrReqType = X_RRDeleteOutputMode;
140b042e37fSmrg    req->output = output;
141b042e37fSmrg    req->mode = mode;
142b042e37fSmrg    UnlockDisplay (dpy);
143b042e37fSmrg    SyncHandle ();
144b042e37fSmrg}
145b042e37fSmrg
146b042e37fSmrgvoid
147b042e37fSmrgXRRFreeModeInfo (XRRModeInfo *modeInfo)
148b042e37fSmrg{
149b042e37fSmrg    Xfree (modeInfo);
150b042e37fSmrg}
151