summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/util/render/x11/initDisplay.cpp
blob: d0029eb671ccc981048b5747f6d4edd79161bcda (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
  here are the different initialisation routines for different displays
  Copyright (C) 1999  Martin Vogt

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Library General Public License as published by
  the Free Software Foundation.

  For more information look at the file COPYRIGHT in this package

 */



#include "initDisplay.h"

#include <iostream>

using namespace std;



static unsigned long wpixel[256];




/*
 *--------------------------------------------------------------
 *
 * InitColorDisplay --
 *
 *	Initialized display for full color output.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      None.
 *
 *--------------------------------------------------------------
 */

void initColorDisplay(XWindow* xwindow) {
  XWindowAttributes winattr;


  XGetWindowAttributes(xwindow->display, xwindow->window, &winattr);

  xwindow->redMask = winattr.visual->red_mask;
  xwindow->greenMask = winattr.visual->green_mask;
  xwindow->blueMask = winattr.visual->blue_mask;
}







/*
 *--------------------------------------------------------------
 *
 * FindFullColorVisual
 *
 *  Returns a pointer to a full color bit visual on the display
 *
 * Results:
 *      See above.
 *  
 * Side effects:
 *      Unknown.
 *
 *--------------------------------------------------------------
 */
Visual* FindFullColorVisual (Display* dpy,int*  depth) {
  XVisualInfo vinfo;
  XVisualInfo *vinfo_ret;
  int numitems, maxdepth;

#if defined(__cplusplus) || defined(c_plusplus)
  vinfo.c_class = TrueColor;
#else
  vinfo.class = TrueColor;
#endif
  
  vinfo_ret = XGetVisualInfo(dpy, VisualClassMask, &vinfo, &numitems);
  
  if (numitems == 0) return NULL;

  maxdepth = 0;
  while(numitems > 0) {
    if (vinfo_ret[numitems-1].depth > maxdepth) {
      maxdepth = vinfo_ret[numitems-1 ].depth;
    }
    numitems--;
  }
  XFree((void *) vinfo_ret);

  if (maxdepth < 16) return NULL;

  if (XMatchVisualInfo(dpy, DefaultScreen(dpy), maxdepth, 
		       TrueColor, &vinfo)) {
    *depth = maxdepth;
    return vinfo.visual;
  }
  
  return NULL;
}


/*
 *--------------------------------------------------------------
 *
 * CreateFullColorWindow
 *
 *  Creates a window capable of handling 32 bit color.
 *
 * Results:
 *      See above.
 *  
 * Side effects:
 *      Unknown.
 *
 *--------------------------------------------------------------
 */
void CreateFullColorWindow (XWindow* xwindow) {
  int depth; 
  Visual *visual;
  XSetWindowAttributes xswa;
  unsigned long mask;
  unsigned int c_class;
  int screen;
  Display *dpy=xwindow->display;
  /*
  int x = xinfo->hints.x,
      y = xinfo->hints.y;
  unsigned int w = xinfo->hints.width,
               h = xinfo->hints.height;
  */
  screen = XDefaultScreen(dpy);
  c_class = InputOutput;	/* Could be InputOnly */
  if (xwindow->visual == NULL) {
    xwindow->visual = visual = FindFullColorVisual (dpy, &depth);
    xwindow->depth = depth;
  } else {
     visual=xwindow->visual;
     depth=xwindow->depth;
  }

  if (visual == NULL) {
    cout << "visual is null"<<endl;
    return;
  }
  mask = CWBackPixel | CWColormap | CWBorderPixel;
  if (xwindow->colormap==0) {
    xswa.colormap = XCreateColormap(dpy,
				    XRootWindow(dpy, screen),
				    visual, AllocNone);
  } else xswa.colormap = xwindow->colormap;
  xswa.background_pixel = BlackPixel(dpy, DefaultScreen(dpy));
  xswa.border_pixel = WhitePixel(dpy, DefaultScreen(dpy));
  XSetWindowColormap(xwindow->display,xwindow->window,xwindow->colormap);


  /*
  xwindow->window = XCreateWindow(dpy, RootWindow(dpy, screen), x, y, w, h,
				  (unsigned int) 1, depth, c_class, 
				  visual, mask, &xswa);
  */
}







/*
 *--------------------------------------------------------------
 *
 * InitSimpleDisplay --
 *
 *      Initialized display, sets up colormap, etc. Use for 8 Bit color
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      None.
 *
 *--------------------------------------------------------------
 */

void initSimpleDisplay(XWindow* xwindow) {
  int ncolors = LUM_RANGE*CB_RANGE*CR_RANGE;
  XColor xcolor;
  int i, lum_num, cr_num, cb_num;
  unsigned char r, g, b;
  Colormap dcmap;
  Display *display;
  ColorTable8Bit colorTable8Bit;

  display = xwindow->display;

    
  xwindow->colormap = XDefaultColormap(display, DefaultScreen(display));
  dcmap = xwindow->colormap;
    
  xcolor.flags = DoRed | DoGreen | DoBlue;
    
    
  //if (xinfo->owncmFlag) goto create_map;

retry_alloc_colors:
  for (i=0; i<ncolors; i++) {

    lum_num = (i / (CR_RANGE*CB_RANGE))%LUM_RANGE;
    cr_num = (i / CB_RANGE)%CR_RANGE;
    cb_num = i % CB_RANGE;

    colorTable8Bit.ConvertColor(lum_num, cr_num, cb_num, &r, &g, &b);

    xcolor.red = r * 256;
    xcolor.green = g * 256;
    xcolor.blue = b * 256;

   if ((XAllocColor(display,xwindow->colormap , &xcolor) == 0 
        && xwindow->colormap == dcmap)) {
      int j;
      unsigned long tmp_pixel;
      XWindowAttributes xwa;

      // Free colors. 
      for (j = 0; j < i; j ++) {
        tmp_pixel = wpixel[j];
        XFreeColors(display,xwindow->colormap , &tmp_pixel, 1, 0);
      }


      //create_map:
      XGetWindowAttributes(display, xwindow->window, &xwa);
      xwindow->colormap = XCreateColormap(display, xwindow->window,
                                          xwa.visual, AllocNone);
      XSetWindowColormap(display, xwindow->window,xwindow->colormap );

      goto retry_alloc_colors;
    }
    xwindow->pixel[i]=xcolor.pixel;
    wpixel[i] = xcolor.pixel;
  }

}