summaryrefslogtreecommitdiffstats
path: root/src/glut/glx/layerutil.c
blob: 26ba0b6029df73df9f08f183abf41d2bb50b66c3 (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

/* Copyright (c) Mark J. Kilgard, 1993, 1994. */

/* This program is freely distributable without licensing fees
   and is provided without guarantee or warrantee expressed or
   implied. This program is -not- in the public domain. */

/* Based on XLayerUtil.c: Revision: 1.5 */

#include <stdio.h>
#include <stdlib.h>
#include "layerutil.h"

/* SGI optimization introduced in IRIX 6.3 to avoid X server
   round trips for interning common X atoms. */
#include <X11/Xatom.h>
#if defined(_SGI_EXTRA_PREDEFINES) && !defined(NO_FAST_ATOMS)
#include <X11/SGIFastAtom.h>
#else
#define XSGIFastInternAtom(dpy,string,fast_name,how) XInternAtom(dpy,string,how)
#endif

static Bool layersRead = False;
static OverlayInfo **overlayInfoPerScreen;
static unsigned long *numOverlaysPerScreen;

static void
findServerOverlayVisualsInfo(Display * dpy)
{
  static Atom overlayVisualsAtom;
  Atom actualType;
  Status status;
  unsigned long sizeData, bytesLeft;
  Window root;
  int actualFormat, numScreens, i;

  if (layersRead == False) {
    overlayVisualsAtom = XSGIFastInternAtom(dpy,
      "SERVER_OVERLAY_VISUALS", SGI_XA_SERVER_OVERLAY_VISUALS, True);
    if (overlayVisualsAtom != None) {
      numScreens = ScreenCount(dpy);
      overlayInfoPerScreen = (OverlayInfo **)
        malloc(numScreens * sizeof(OverlayInfo *));
      numOverlaysPerScreen = (unsigned long *)
        malloc(numScreens * sizeof(unsigned long));
      if (overlayInfoPerScreen != NULL &&
        numOverlaysPerScreen != NULL) {
        for (i = 0; i < numScreens; i++) {
          root = RootWindow(dpy, i);
          status = XGetWindowProperty(dpy, root,
            overlayVisualsAtom, 0L, (long) 10000, False,
            overlayVisualsAtom, &actualType, &actualFormat,
            &sizeData, &bytesLeft,
            (unsigned char **) &overlayInfoPerScreen[i]);
          if (status != Success ||
            actualType != overlayVisualsAtom ||
            actualFormat != 32 || sizeData < 4)
            numOverlaysPerScreen[i] = 0;
          else
            /* Four 32-bit quantities per
               SERVER_OVERLAY_VISUALS entry. */
            numOverlaysPerScreen[i] = sizeData / 4;
        }
        layersRead = True;
      } else {
        if (overlayInfoPerScreen != NULL)
          free(overlayInfoPerScreen);
        if (numOverlaysPerScreen != NULL)
          free(numOverlaysPerScreen);
      }
    }
  }
}

int
__glutGetTransparentPixel(Display * dpy, XVisualInfo * vinfo)
{
  int i, screen = vinfo->screen;
  OverlayInfo *overlayInfo;

  findServerOverlayVisualsInfo(dpy);
  if (layersRead) {
    for (i = 0; i < numOverlaysPerScreen[screen]; i++) {
      overlayInfo = &overlayInfoPerScreen[screen][i];
      if (vinfo->visualid == overlayInfo->overlay_visual) {
        if (overlayInfo->transparent_type == TransparentPixel) {
          return (int) overlayInfo->value;
        } else {
          return -1;
        }
      }
    }
  }
  return -1;
}

XLayerVisualInfo *
__glutXGetLayerVisualInfo(Display * dpy, long lvinfo_mask,
  XLayerVisualInfo * lvinfo_template, int *nitems_return)
{
  XVisualInfo *vinfo;
  XLayerVisualInfo *layerInfo;
  int numVisuals, count, i, j;

  vinfo = XGetVisualInfo(dpy, lvinfo_mask & VisualAllMask,
    &lvinfo_template->vinfo, nitems_return);
  if (vinfo == NULL)
    return NULL;
  numVisuals = *nitems_return;
  findServerOverlayVisualsInfo(dpy);
  layerInfo = (XLayerVisualInfo *)
    malloc(numVisuals * sizeof(XLayerVisualInfo));
  if (layerInfo == NULL) {
    XFree(vinfo);
    return NULL;
  }
  count = 0;
  for (i = 0; i < numVisuals; i++) {
    XVisualInfo *pVinfo = &vinfo[i];
    int screen = pVinfo->screen;
    OverlayInfo *overlayInfo = NULL;

    overlayInfo = NULL;
    if (layersRead) {
      for (j = 0; j < numOverlaysPerScreen[screen]; j++)
        if (pVinfo->visualid ==
          overlayInfoPerScreen[screen][j].overlay_visual) {
          overlayInfo = &overlayInfoPerScreen[screen][j];
          break;
        }
    }
    if (lvinfo_mask & VisualLayerMask) {
      if (overlayInfo == NULL) {
        if (lvinfo_template->layer != 0)
          continue;
      } else if (lvinfo_template->layer != overlayInfo->layer)
        continue;
    }
    if (lvinfo_mask & VisualTransparentType) {
      if (overlayInfo == NULL) {
        if (lvinfo_template->type != None)
          continue;
      } else if (lvinfo_template->type !=
        overlayInfo->transparent_type)
        continue;
    }
    if (lvinfo_mask & VisualTransparentValue) {
      if (overlayInfo == NULL)
        /* Non-overlay visuals have no sense of
           TransparentValue. */
        continue;
      else if (lvinfo_template->value != overlayInfo->value)
        continue;
    }
    layerInfo[count].vinfo = *pVinfo;
    if (overlayInfo == NULL) {
      layerInfo[count].layer = 0;
      layerInfo[count].type = None;
      layerInfo[count].value = 0;  /* meaningless */
    } else {
      layerInfo[count].layer = overlayInfo->layer;
      layerInfo[count].type = overlayInfo->transparent_type;
      layerInfo[count].value = overlayInfo->value;
    }
    count++;
  }
  XFree(vinfo);
  *nitems_return = count;
  if (count == 0) {
    XFree(layerInfo);
    return NULL;
  } else
    return layerInfo;
}

#if 0                   /* Unused by GLUT. */
Status
__glutXMatchLayerVisualInfo(Display * dpy, int screen,
  int depth, int visualClass, int layer,
  XLayerVisualInfo * lvinfo_return)
{
  XLayerVisualInfo *lvinfo;
  XLayerVisualInfo lvinfoTemplate;
  int nitems;

  lvinfoTemplate.vinfo.screen = screen;
  lvinfoTemplate.vinfo.depth = depth;
#if defined(__cplusplus) || defined(c_plusplus)
  lvinfoTemplate.vinfo.c_class = visualClass;
#else
  lvinfoTemplate.vinfo.class = visualClass;
#endif
  lvinfoTemplate.layer = layer;
  lvinfo = __glutXGetLayerVisualInfo(dpy,
    VisualScreenMask | VisualDepthMask |
    VisualClassMask | VisualLayerMask,
    &lvinfoTemplate, &nitems);
  if (lvinfo != NULL && nitems > 0) {
    *lvinfo_return = *lvinfo;
    return 1;
  } else
    return 0;
}
#endif