a30daaca3b52821db27e03732e32e4fa2c1443b5
[framework/uifw/xorg/lib/libxext.git] / src / XEVI.c
1 /************************************************************
2 Copyright (c) 1997 by Silicon Graphics Computer Systems, Inc.
3 Permission to use, copy, modify, and distribute this
4 software and its documentation for any purpose and without
5 fee is hereby granted, provided that the above copyright
6 notice appear in all copies and that both that copyright
7 notice and this permission notice appear in supporting
8 documentation, and that the name of Silicon Graphics not be
9 used in advertising or publicity pertaining to distribution
10 of the software without specific prior written permission.
11 Silicon Graphics makes no representation about the suitability
12 of this software for any purpose. It is provided "as is"
13 without any express or implied warranty.
14 SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
15 SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
16 AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
17 GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
18 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
20 OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
21 THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 ********************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 #include <X11/Xlibint.h>
28 #include <X11/extensions/XEVI.h>
29 #include <X11/extensions/EVIproto.h>
30 #include <X11/extensions/Xext.h>
31 #include <X11/extensions/extutil.h>
32 #include <X11/Xutil.h>
33 static XExtensionInfo *xevi_info;/* needs to move to globals.c */
34 static /* const */ char *xevi_extension_name = EVINAME;
35 #define XeviCheckExtension(dpy,i,val) \
36   XextCheckExtension (dpy, i, xevi_extension_name, val)
37 /*****************************************************************************
38  *                                                                           *
39  *                         private utility routines                          *
40  *                                                                           *
41  *****************************************************************************/
42 static /* const */ XExtensionHooks xevi_extension_hooks = {
43     NULL,                       /* create_gc */
44     NULL,                       /* copy_gc */
45     NULL,                       /* flush_gc */
46     NULL,                       /* free_gc */
47     NULL,                       /* create_font */
48     NULL,                       /* free_font */
49     NULL,                       /* close_display */
50     NULL,                       /* wire_to_event */
51     NULL,                       /* event_to_wire */
52     NULL,                       /* error */
53     NULL,                       /* error_string */
54 };
55 static XEXT_GENERATE_FIND_DISPLAY (find_display, xevi_info,
56                                    xevi_extension_name,
57                                    &xevi_extension_hooks, 0, NULL)
58 Bool XeviQueryExtension (Display *dpy)
59 {
60     XExtDisplayInfo *info = find_display (dpy);
61     if (XextHasExtension(info)) {
62         return True;
63     } else {
64         return False;
65     }
66 }
67 Bool XeviQueryVersion(Display *dpy, int *majorVersion, int *minorVersion)
68 {
69     XExtDisplayInfo *info = find_display (dpy);
70     xEVIQueryVersionReply rep;
71     register xEVIQueryVersionReq *req;
72     XeviCheckExtension (dpy, info, False);
73     LockDisplay(dpy);
74     GetReq(EVIQueryVersion, req);
75     req->reqType = info->codes->major_opcode;
76     req->xeviReqType = X_EVIQueryVersion;
77     if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
78         UnlockDisplay(dpy);
79         SyncHandle();
80         return False;
81     }
82     *majorVersion = rep.majorVersion;
83     *minorVersion = rep.minorVersion;
84     UnlockDisplay(dpy);
85     SyncHandle();
86     return True;
87 }
88 static Bool notInList(VisualID32 *visual, int sz_visual, VisualID newVisualid)
89 {
90     while  (sz_visual-- > 0)  {
91         if (*visual == newVisualid)
92             return False;
93         visual++;
94     }
95     return True;
96 }
97 Status XeviGetVisualInfo(
98     register Display *dpy,
99     VisualID *visual,
100     int n_visual,
101     ExtendedVisualInfo **evi_return,
102     int *n_info_return)
103 {
104     XExtDisplayInfo *info = find_display (dpy);
105     register xEVIGetVisualInfoReq *req;
106     xEVIGetVisualInfoReply rep;
107     int sz_info, sz_xInfo, sz_conflict, sz_xConflict;
108     VisualID32 *temp_conflict, *temp_visual, *xConflictPtr;
109     VisualID *conflict;
110     xExtendedVisualInfo *temp_xInfo;
111     XVisualInfo *vinfo;
112     register ExtendedVisualInfo *infoPtr;
113     register xExtendedVisualInfo *xInfoPtr;
114     register int n_data, visualIndex, vinfoIndex;
115     Bool isValid;
116     XeviCheckExtension (dpy, info, 0);
117     if (!n_info_return || !evi_return) {
118         return BadValue;
119     }
120     *n_info_return = 0;
121     *evi_return = NULL;
122     vinfo = XGetVisualInfo(dpy, 0, NULL, &sz_info);
123     if (!vinfo) {
124         return BadValue;
125     }
126     if (!n_visual || !visual) {         /* copy the all visual */
127         temp_visual = (VisualID32 *)Xmalloc(sz_VisualID32 * sz_info);
128         n_visual = 0;
129         for (vinfoIndex = 0; vinfoIndex < sz_info; vinfoIndex++)
130             if (notInList(temp_visual, n_visual, vinfo[vinfoIndex].visualid))
131                 temp_visual[n_visual++] = vinfo[vinfoIndex].visualid;
132     }
133     else {      /* check if the visual is valid */
134         for (visualIndex = 0; visualIndex < n_visual; visualIndex++) {
135             isValid = False;
136             for (vinfoIndex = 0; vinfoIndex < sz_info; vinfoIndex++) {
137                 if (visual[visualIndex] == vinfo[vinfoIndex].visualid) {
138                     isValid = True;
139                     break;
140                 }
141             }
142             if (!isValid) {
143                 XFree(vinfo);
144                 return BadValue;
145             }
146         }
147         temp_visual = (VisualID32 *)Xmalloc(sz_VisualID32 * n_visual);
148         for (visualIndex = 0; visualIndex < n_visual; visualIndex++)
149             temp_visual[visualIndex] = visual[visualIndex];
150     }
151     XFree(vinfo);
152     LockDisplay(dpy);
153     GetReq(EVIGetVisualInfo, req);
154     req->reqType = info->codes->major_opcode;
155     req->xeviReqType = X_EVIGetVisualInfo;
156     req->n_visual = n_visual;
157     SetReqLen(req, n_visual, 1);
158     Data(dpy, (char *)temp_visual, n_visual * sz_VisualID32);
159     if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
160         UnlockDisplay(dpy);
161         SyncHandle();
162         Xfree(temp_visual);
163         return BadAccess;
164     }
165     Xfree(temp_visual);
166     sz_info = rep.n_info * sizeof(ExtendedVisualInfo);
167     sz_xInfo = rep.n_info * sz_xExtendedVisualInfo;
168     sz_conflict = rep.n_conflicts * sizeof(VisualID);
169     sz_xConflict = rep.n_conflicts * sz_VisualID32;
170     infoPtr = *evi_return = (ExtendedVisualInfo *)Xmalloc(sz_info + sz_conflict);
171     xInfoPtr = temp_xInfo = (xExtendedVisualInfo *)Xmalloc(sz_xInfo);
172     xConflictPtr = temp_conflict = (VisualID32 *)Xmalloc(sz_xConflict);
173     if (!*evi_return || !temp_xInfo || !temp_conflict) {
174         _XEatData(dpy, (sz_xInfo + sz_xConflict + 3) & ~3);
175         UnlockDisplay(dpy);
176         SyncHandle();
177         if (evi_return)
178            Xfree(evi_return);
179         if (temp_xInfo)
180            Xfree(temp_xInfo);
181         if (temp_conflict)
182            Xfree(temp_conflict);
183         return BadAlloc;
184     }
185     _XRead(dpy, (char *)temp_xInfo, sz_xInfo);
186     _XRead(dpy, (char *)temp_conflict, sz_xConflict);
187     UnlockDisplay(dpy);
188     SyncHandle();
189     n_data = rep.n_info;
190     conflict = (VisualID *)(infoPtr + n_data);
191     while (n_data-- > 0) {
192         infoPtr->core_visual_id         = xInfoPtr->core_visual_id;
193         infoPtr->screen                 = xInfoPtr->screen;
194         infoPtr->level                  = xInfoPtr->level;
195         infoPtr->transparency_type      = xInfoPtr->transparency_type;
196         infoPtr->transparency_value     = xInfoPtr->transparency_value;
197         infoPtr->min_hw_colormaps       = xInfoPtr->min_hw_colormaps;
198         infoPtr->max_hw_colormaps       = xInfoPtr->max_hw_colormaps;
199         infoPtr->num_colormap_conflicts = xInfoPtr->num_colormap_conflicts;
200         infoPtr->colormap_conflicts     = conflict;
201         conflict += infoPtr->num_colormap_conflicts;
202         infoPtr++;
203         xInfoPtr++;
204     }
205     n_data = rep.n_conflicts;
206     conflict = (VisualID *)(infoPtr);
207     while (n_data-- > 0)
208        *conflict++ = *xConflictPtr++;
209     Xfree(temp_xInfo);
210     Xfree(temp_conflict);
211     *n_info_return = rep.n_info;
212     return Success;
213 }