summaryrefslogtreecommitdiff
path: root/plugins/gtkui/gtkglext-gtk2/gdk/win32/gdkglconfig-win32.c
blob: 50c1fbfec8e826a12cc060adad66e5fa3aecd65b (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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
/* GdkGLExt - OpenGL Extension to GDK
 * Copyright (C) 2002-2004  Naofumi Yasufuku
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
 */

#include "gdkglwin32.h"
#include "gdkglprivate-win32.h"
#include "gdkglconfig-win32.h"

#ifdef GDKGLEXT_MULTIHEAD_SUPPORT
#include <gdk/gdkscreen.h>
#endif /* GDKGLEXT_MULTIHEAD_SUPPORT */

static void gdk_gl_config_impl_win32_class_init (GdkGLConfigImplWin32Class *klass);
static void gdk_gl_config_impl_win32_finalize   (GObject                   *object);

static gpointer parent_class = NULL;

GType
gdk_gl_config_impl_win32_get_type (void)
{
  static GType type = 0;

  if (!type)
    {
      static const GTypeInfo type_info = {
        sizeof (GdkGLConfigImplWin32Class),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gdk_gl_config_impl_win32_class_init,
        (GClassFinalizeFunc) NULL,
        NULL,                   /* class_data */
        sizeof (GdkGLConfigImplWin32),
        0,                      /* n_preallocs */
        (GInstanceInitFunc) NULL
      };

      type = g_type_register_static (GDK_TYPE_GL_CONFIG,
                                     "GdkGLConfigImplWin32",
                                     &type_info, 0);
    }

  return type;
}

static void
gdk_gl_config_impl_win32_class_init (GdkGLConfigImplWin32Class *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  GDK_GL_NOTE_FUNC_PRIVATE ();

  parent_class = g_type_class_peek_parent (klass);

  object_class->finalize = gdk_gl_config_impl_win32_finalize;
}

static void
gdk_gl_config_impl_win32_finalize (GObject *object)
{
  GdkGLConfigImplWin32 *impl = GDK_GL_CONFIG_IMPL_WIN32 (object);

  GDK_GL_NOTE_FUNC_PRIVATE ();

  g_object_unref (G_OBJECT (impl->colormap));

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

/*
 * This code is based on lib/glut/win32_glx.c of GLUT by Nate Robins.
 */
static void
gdk_gl_config_parse_attrib_list (const int             *attrib_list,
                                 PIXELFORMATDESCRIPTOR *pfd)
{
  int *p;
  gboolean buffer_size_is_specified = FALSE;
  BYTE buffer_size;
  int layer_plane;

  GDK_GL_NOTE_FUNC_PRIVATE ();

  memset (pfd, 0, sizeof (PIXELFORMATDESCRIPTOR));

  /* Specifies the size of this data structure. */
  pfd->nSize = sizeof (PIXELFORMATDESCRIPTOR);
  /* Specifies the version of this data structure. This value should be set to 1. */
  pfd->nVersion = 1;

  /* Defaults. */

  /* A set of bit flags that specify properties of the pixel buffer.
     PFD_SUPPORT_GDI and PFD_DOUBLEBUFFER are mutually exclusive in
     the current generic implementation. */
  /* PFD_DRAW_TO_WINDOW or PFD_DRAW_TO_BITMAP is not specified at this stage.
     The flag is set by gdk_gl_(window|pixmap)_new(). */
  pfd->dwFlags = PFD_SUPPORT_OPENGL |
                 PFD_SUPPORT_GDI;

  /* Specifies the type of pixel data. */
  pfd->iPixelType = PFD_TYPE_COLORINDEX;

  /* Specifies the number of color bitplanes in each color buffer.
     For RGBA pixel types, it is the size of the color buffer, excluding the alpha bitplanes.
     For color-index pixels, it is the size of the color-index buffer. */
  pfd->cColorBits = 32;		/* Max */

  /* Ignored. Earlier implementations of OpenGL used this member,
     but it is no longer used. */
  layer_plane = 0;
  pfd->iLayerType = PFD_MAIN_PLANE;

  p = (int *) attrib_list;
  while (*p != GDK_GL_ATTRIB_LIST_NONE)
    {
      switch (*p)
        {
        case GDK_GL_USE_GL:
	  /* The buffer supports OpenGL drawing. */
          pfd->dwFlags |= PFD_SUPPORT_OPENGL;
          break;
        case GDK_GL_BUFFER_SIZE:
	  /* Specifies the number of color bitplanes in each color buffer. */
          pfd->cColorBits = *(++p);
	  buffer_size_is_specified = TRUE;
          break;
        case GDK_GL_LEVEL:
          layer_plane = *(++p);
	  /* Ignored. Earlier implementations of OpenGL used this member,
	     but it is no longer used. */
	  if (layer_plane > 0)
	    pfd->iLayerType = PFD_OVERLAY_PLANE;
	  else if (layer_plane < 0)
	    pfd->iLayerType = PFD_UNDERLAY_PLANE;
          break;
        case GDK_GL_RGBA:
	  /* RGBA pixels. */
          pfd->iPixelType = PFD_TYPE_RGBA;
          break;
        case GDK_GL_DOUBLEBUFFER:
	  /* The buffer is double-buffered. */
	  pfd->dwFlags &= ~PFD_SUPPORT_GDI;
          pfd->dwFlags |= PFD_DOUBLEBUFFER;
          break;
        case GDK_GL_STEREO:
	  /* The buffer is stereoscopic.
	     This flag is not supported in the current generic implementation. */
          pfd->dwFlags |= PFD_STEREO;
          break;
        case GDK_GL_AUX_BUFFERS:
	  /* Specifies the number of auxiliary buffers.
	     Auxiliary buffers are not supported. */
          pfd->cAuxBuffers = *(++p);
          break;
        case GDK_GL_RED_SIZE:
	  /* Specifies the number of red bitplanes in each RGBA color buffer.
	     Not used by ChoosePixelFormat. */
          pfd->cRedBits = *(++p);
          break;
        case GDK_GL_GREEN_SIZE:
	  /* Specifies the number of green bitplanes in each RGBA color buffer.
	     Not used by ChoosePixelFormat. */
          pfd->cGreenBits = *(++p);
          break;
        case GDK_GL_BLUE_SIZE:
	  /* Specifies the number of blue bitplanes in each RGBA color buffer.
	     Not used by ChoosePixelFormat. */
          pfd->cBlueBits = *(++p);
          break;
        case GDK_GL_ALPHA_SIZE:
	  /* Specifies the number of alpha bitplanes in each RGBA color buffer.
	     Alpha bitplanes are not supported.  */
	  pfd->cAlphaBits = *(++p);
          break;
        case GDK_GL_DEPTH_SIZE:
	  /* Specifies the depth of the depth (z-axis) buffer. */
          pfd->cDepthBits = *(++p);
          break;
        case GDK_GL_STENCIL_SIZE:
	  /* Specifies the depth of the stencil buffer. */
          pfd->cStencilBits = *(++p);
          break;
        case GDK_GL_ACCUM_RED_SIZE:
	  /* Specifies the number of red bitplanes in the accumulation buffer.
	     Not used by ChoosePixelFormat. */
	  pfd->cAccumRedBits = *(++p);
	  break;
        case GDK_GL_ACCUM_GREEN_SIZE:
	  /* Specifies the number of green bitplanes in the accumulation buffer.
	     Not used by ChoosePixelFormat. */
	  pfd->cAccumGreenBits = *(++p);
	  break;
        case GDK_GL_ACCUM_BLUE_SIZE:
	  /* Specifies the number of blue bitplanes in the accumulation buffer.
	     Not used by ChoosePixelFormat. */
	  pfd->cAccumBlueBits = *(++p);
	  break;
        case GDK_GL_ACCUM_ALPHA_SIZE:
	  /* Specifies the number of alpha bitplanes in the accumulation buffer.
	     Not used by ChoosePixelFormat.*/
	  pfd->cAccumAlphaBits = *(++p);
          break;
        }
      ++p;
    }

  /* If GDK_GL_BUFFER_SIZE is not specified. */
  if (!buffer_size_is_specified)
    {
      buffer_size = pfd->cRedBits + pfd->cGreenBits + pfd->cBlueBits;
      if (buffer_size != 0)
	pfd->cColorBits = buffer_size;
    }

  /* Specifies the total number of bitplanes in the accumulation buffer. */
  /* Nate Robins says ...
     I believe that WGL only used the cAccumRedBits,
     cAccumBlueBits, cAccumGreenBits, and cAccumAlphaBits fields
     when returning info about the accumulation buffer precision.
     Only cAccumBits is used for requesting an accumulation buffer. */
  pfd->cAccumBits = pfd->cAccumRedBits +
                    pfd->cAccumGreenBits +
                    pfd->cAccumBlueBits +
                    pfd->cAccumAlphaBits;
}

/*
 * Find an appropriate pixel format.
 * Basic idea of this code is ripped from FLTK.
 */
/*< private >*/
int
_gdk_win32_gl_config_find_pixel_format (HDC                          hdc,
					CONST PIXELFORMATDESCRIPTOR *req_pfd,
					PIXELFORMATDESCRIPTOR       *found_pfd)
{
  PIXELFORMATDESCRIPTOR pfd, chosen_pfd;
  int pixel_format = 0;
  int i;

  GDK_GL_NOTE_FUNC_PRIVATE ();

  memset (&chosen_pfd, 0, sizeof (chosen_pfd));

  for (i = 1; ; i++)
    {
      if (DescribePixelFormat (hdc, i, sizeof (pfd), &pfd) == 0)
	break;

      if (~(pfd.dwFlags)   &  req_pfd->dwFlags)      continue;
      if (pfd.iPixelType   != req_pfd->iPixelType)   continue;
      if (pfd.cColorBits   <  req_pfd->cColorBits)   continue;
      if (pfd.cAlphaBits   <  req_pfd->cAlphaBits)   continue;
      if (pfd.cAccumBits   <  req_pfd->cAccumBits)   continue;
      if (pfd.cDepthBits   <  req_pfd->cDepthBits)   continue;
      if (pfd.cStencilBits <  req_pfd->cStencilBits) continue;
      if (pfd.cAuxBuffers  <  req_pfd->cAuxBuffers)  continue;
      /* if (pfd.iLayerType   != req_pfd->iLayerType)   continue; */

      /* Check whether pfd is better than chosen_pfd. */
      if (pixel_format != 0)
	{
	  /* Offering overlay is better. */
	  if ((pfd.bReserved & 0x0f) && !(chosen_pfd.bReserved & 0x0f)) {}
	  /* More color bitplanes is better. */
	  else if (pfd.cColorBits > chosen_pfd.cColorBits) {}
	  /* pfd is not better than chosen_pfd. */
	  else continue;
	}

      pixel_format = i;
      chosen_pfd = pfd;
    }

  *found_pfd = chosen_pfd;

  return pixel_format;
}

/* 
 * Setup PFD.
 */

static gboolean
gdk_gl_config_setup_pfd (CONST PIXELFORMATDESCRIPTOR *req_pfd,
			 PIXELFORMATDESCRIPTOR       *pfd)
{
  HDC hdc;
  PIXELFORMATDESCRIPTOR temp_pfd;
  PIXELFORMATDESCRIPTOR w_pfd, b_pfd;
  int w_pf, b_pf;

  GDK_GL_NOTE_FUNC_PRIVATE ();

  /* Get DC. */
  hdc = GetDC (NULL);
  if (hdc == NULL)
    {
      g_warning ("cannot get DC");
      return FALSE;
    }

  w_pfd = *req_pfd;
  w_pfd.dwFlags &= ~PFD_DRAW_TO_BITMAP;
  w_pfd.dwFlags |= PFD_DRAW_TO_WINDOW;
  w_pf = _gdk_win32_gl_config_find_pixel_format (hdc, &w_pfd, &w_pfd);

  GDK_GL_NOTE (MISC, g_message (" -- pixel format for windows = 0x%x", w_pf));
  GDK_GL_NOTE (MISC, _gdk_win32_gl_print_pfd (&w_pfd));

  b_pfd = *req_pfd;
  b_pfd.dwFlags &= ~PFD_DRAW_TO_WINDOW;
  b_pfd.dwFlags |= PFD_DRAW_TO_BITMAP;
  b_pf = _gdk_win32_gl_config_find_pixel_format (hdc, &b_pfd, &b_pfd);

  GDK_GL_NOTE (MISC, g_message (" -- pixel format for bitmaps = 0x%x", b_pf));
  GDK_GL_NOTE (MISC, _gdk_win32_gl_print_pfd (&b_pfd));

  /* Release DC. */
  ReleaseDC (NULL, hdc);

  if (w_pf == 0 && b_pf == 0)
    return FALSE;

  if (w_pf == 0)
    {
      temp_pfd = b_pfd;
      temp_pfd.dwFlags = req_pfd->dwFlags;
    }
  else if (b_pf == 0)
    {
      temp_pfd = w_pfd;
      temp_pfd.dwFlags = req_pfd->dwFlags;
    }
  else
    {
      temp_pfd = w_pfd;
      temp_pfd.dwFlags = req_pfd->dwFlags;
      if (temp_pfd.cColorBits > b_pfd.cColorBits)
        {
          temp_pfd.cColorBits  = b_pfd.cColorBits;
          temp_pfd.cRedBits    = b_pfd.cRedBits;
          temp_pfd.cRedShift   = b_pfd.cRedShift;
          temp_pfd.cGreenBits  = b_pfd.cGreenBits;
          temp_pfd.cGreenShift = b_pfd.cGreenShift;
          temp_pfd.cBlueBits   = b_pfd.cBlueBits;
          temp_pfd.cBlueShift  = b_pfd.cBlueShift;
        }
      if (temp_pfd.cAlphaBits > b_pfd.cAlphaBits)
        {
          temp_pfd.cAlphaBits  = b_pfd.cAlphaBits;
          temp_pfd.cAlphaShift = b_pfd.cAlphaShift;
        }
      if (temp_pfd.cAccumBits > b_pfd.cAccumBits)
        {
          temp_pfd.cAccumBits      = b_pfd.cAccumBits;
          temp_pfd.cAccumRedBits   = b_pfd.cAccumRedBits;
          temp_pfd.cAccumGreenBits = b_pfd.cAccumGreenBits;
          temp_pfd.cAccumBlueBits  = b_pfd.cAccumBlueBits;
          temp_pfd.cAccumAlphaBits = b_pfd.cAccumAlphaBits;
        }
      temp_pfd.cDepthBits   = MIN (temp_pfd.cDepthBits,   b_pfd.cDepthBits);
      temp_pfd.cStencilBits = MIN (temp_pfd.cStencilBits, b_pfd.cStencilBits);
      temp_pfd.cAuxBuffers  = MIN (temp_pfd.cAuxBuffers,  b_pfd.cAuxBuffers);
    }

  *pfd = temp_pfd;

  return TRUE;
}

/* 
 * Setup colormap.
 */

/* 
 * !!! RGB palette management should be implemented...
 */

#ifdef GDKGLEXT_MULTIHEAD_SUPPORT

static GdkColormap *
gdk_gl_config_setup_colormap (GdkScreen             *screen,
                              PIXELFORMATDESCRIPTOR *pfd,
                              gboolean               is_rgba)
{
  GDK_GL_NOTE_FUNC_PRIVATE ();

  if (is_rgba)
    {
      /*
       * For RGBA mode.
       */

      /* System default colormap. */

      GDK_GL_NOTE (MISC, g_message (" -- Colormap: system default"));

      return g_object_ref (G_OBJECT (gdk_screen_get_system_colormap (screen)));
    }
  else
    {
      /*
       * For color index mode.
       */

      /* New private colormap. */

      GDK_GL_NOTE (MISC, g_message (" -- Colormap: new allocated writable"));

      return gdk_colormap_new (gdk_screen_get_system_visual (screen), TRUE);
    }

  /* not reached */
  return NULL;
}

#else  /* GDKGLEXT_MULTIHEAD_SUPPORT */

static GdkColormap *
gdk_gl_config_setup_colormap (GdkScreen             *screen,
                              PIXELFORMATDESCRIPTOR *pfd,
                              gboolean               is_rgba)
{
  GDK_GL_NOTE_FUNC_PRIVATE ();

  if (is_rgba)
    {
      /*
       * For RGBA mode.
       */

      /* Default colormap. */

      GDK_GL_NOTE (MISC, g_message (" -- Colormap: system default"));

      return g_object_ref (G_OBJECT (gdk_colormap_get_system ()));
    }
  else
    {
      /*
       * For color index mode.
       */

      /* New private colormap. */

      GDK_GL_NOTE (MISC, g_message (" -- Colormap: new allocated writable"));

      return gdk_colormap_new (gdk_visual_get_system (), TRUE);
    }

  /* not reached */
  return NULL;
}

#endif /* GDKGLEXT_MULTIHEAD_SUPPORT */

static void
gdk_gl_config_init_attrib (GdkGLConfig *glconfig)
{
  PIXELFORMATDESCRIPTOR *pfd;

  pfd = GDK_GL_CONFIG_PFD (glconfig);

  /* RGBA mode? */
  glconfig->is_rgba = (pfd->iPixelType == PFD_TYPE_RGBA) ? TRUE : FALSE;

  /* Layer plane. */
  if (pfd->bReserved != 0)
    {
      glconfig->layer_plane = pfd->bReserved & 0x0f;
      if (glconfig->layer_plane == 0)
        glconfig->layer_plane = -1 * ((pfd->bReserved & 0xf0) >> 4);
    }
  else
    {
      glconfig->layer_plane = 0;
    }

  /* Double buffering is supported? */
  glconfig->is_double_buffered = (pfd->dwFlags & PFD_DOUBLEBUFFER) ? TRUE : FALSE;

  /* Stereo is supported? (not work on Windows) */
  glconfig->is_stereo = (pfd->dwFlags & PFD_STEREO) ? TRUE : FALSE;

  /* Number of aux buffers */
  glconfig->n_aux_buffers = pfd->cAuxBuffers;

  /* Has alpha bits? */
  glconfig->has_alpha = pfd->cAlphaBits ? TRUE : FALSE;

  /* Has depth buffer? */
  glconfig->has_depth_buffer = pfd->cDepthBits ? TRUE : FALSE;

  /* Has stencil buffer? */
  glconfig->has_stencil_buffer = pfd->cStencilBits ? TRUE : FALSE;

  /* Has accumulation buffer? */
  glconfig->has_accum_buffer = pfd->cAccumBits ? TRUE : FALSE;

  /* Number of multisample buffers (not supported yet) */
  glconfig->n_sample_buffers = 0;
}

static GdkGLConfig *
gdk_gl_config_new_common (GdkScreen *screen,
                          const int *attrib_list)
{
  GdkGLConfig *glconfig;
  GdkGLConfigImplWin32 *impl;
  PIXELFORMATDESCRIPTOR pfd;

  GDK_GL_NOTE_FUNC_PRIVATE ();

  /*
   * Parse GLX style attrib_list.
   */

  gdk_gl_config_parse_attrib_list (attrib_list, &pfd);

  GDK_GL_NOTE (MISC, _gdk_win32_gl_print_pfd (&pfd));

  /*
   * Setup PFD.
   */

  if (!gdk_gl_config_setup_pfd (&pfd, &pfd))
    return NULL;

  GDK_GL_NOTE (MISC, g_message (" -- created PFD"));
  GDK_GL_NOTE (MISC, _gdk_win32_gl_print_pfd (&pfd));

  /*
   * Instantiate the GdkGLConfigImplWin32 object.
   */

  glconfig = g_object_new (GDK_TYPE_GL_CONFIG_IMPL_WIN32, NULL);
  impl = GDK_GL_CONFIG_IMPL_WIN32 (glconfig);

  impl->pfd = pfd;

  impl->screen = screen;

  /*
   * Get an appropriate colormap.
   */

  impl->colormap = gdk_gl_config_setup_colormap (screen,
                                                 &pfd,
                                                 (pfd.iPixelType == PFD_TYPE_RGBA));

  /*
   * Set depth (number of bits per pixel).
   */

  impl->depth = pfd.cRedBits + pfd.cGreenBits + pfd.cBlueBits;

  /*
   * Init configuration attributes.
   */

  gdk_gl_config_init_attrib (glconfig);

  return glconfig;
}

GdkGLConfig *
gdk_gl_config_new (const int *attrib_list)
{
  GdkScreen *screen;

  GDK_GL_NOTE_FUNC ();

  g_return_val_if_fail (attrib_list != NULL, NULL);

#ifdef GDKGLEXT_MULTIHEAD_SUPPORT
  screen = gdk_screen_get_default ();
#else  /* GDKGLEXT_MULTIHEAD_SUPPORT */
  screen = NULL;
#endif /* GDKGLEXT_MULTIHEAD_SUPPORT */

  return gdk_gl_config_new_common (screen, attrib_list);
}

#ifdef GDKGLEXT_MULTIHEAD_SUPPORT

GdkGLConfig *
gdk_gl_config_new_for_screen (GdkScreen *screen,
                              const int *attrib_list)
{
  GDK_GL_NOTE_FUNC ();

  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
  g_return_val_if_fail (attrib_list != NULL, NULL);

  return gdk_gl_config_new_common (screen, attrib_list);
}

#endif /* GDKGLEXT_MULTIHEAD_SUPPORT */

GdkGLConfig *
gdk_win32_gl_config_new_from_pixel_format (int pixel_format)
{
  GdkGLConfig *glconfig;
  GdkGLConfigImplWin32 *impl;

  HDC hdc;
  PIXELFORMATDESCRIPTOR pfd;
  int result;

  GDK_GL_NOTE_FUNC ();

  /*
   * Get PFD.
   */

  /* Get DC. */
  hdc = GetDC (NULL);
  if (hdc == NULL)
    {
      g_warning ("cannot get DC");
      return NULL;
    }

  result = DescribePixelFormat (hdc, pixel_format, sizeof (pfd), &pfd);

  /* Release DC. */
  ReleaseDC (NULL, hdc);

  if (result == 0)
    return NULL;

  GDK_GL_NOTE (MISC, g_message (" -- pixel_format = 0x%x", pixel_format));

  GDK_GL_NOTE (MISC, _gdk_win32_gl_print_pfd (&pfd));

  /*
   * Instantiate the GdkGLConfigImplWin32 object.
   */

  glconfig = g_object_new (GDK_TYPE_GL_CONFIG_IMPL_WIN32, NULL);
  impl = GDK_GL_CONFIG_IMPL_WIN32 (glconfig);

  impl->pfd = pfd;

#ifdef GDKGLEXT_MULTIHEAD_SUPPORT
  impl->screen = gdk_screen_get_default ();
#else  /* GDKGLEXT_MULTIHEAD_SUPPORT */
  impl->screen = NULL;
#endif /* GDKGLEXT_MULTIHEAD_SUPPORT */

  /*
   * Get an appropriate colormap.
   */

  impl->colormap = gdk_gl_config_setup_colormap (impl->screen,
                                                 &pfd,
                                                 (pfd.iPixelType == PFD_TYPE_RGBA));

  /*
   * Set depth (number of bits per pixel).
   */

  impl->depth = pfd.cRedBits + pfd.cGreenBits + pfd.cBlueBits;

  /*
   * Init configuration attributes.
   */

  gdk_gl_config_init_attrib (glconfig);

  return glconfig;
}

GdkScreen *
gdk_gl_config_get_screen (GdkGLConfig *glconfig)
{
  g_return_val_if_fail (GDK_IS_GL_CONFIG_IMPL_WIN32 (glconfig), NULL);

  return GDK_GL_CONFIG_IMPL_WIN32 (glconfig)->screen;
}

/*
 * This code is based on lib/glut/win32_glx.c of GLUT by Nate Robins.
 */
gboolean
gdk_gl_config_get_attrib (GdkGLConfig *glconfig,
                          int          attribute,
                          int         *value)
{
  GdkGLConfigImplWin32 *impl;

  g_return_val_if_fail (GDK_IS_GL_CONFIG_IMPL_WIN32 (glconfig), FALSE);

  impl = GDK_GL_CONFIG_IMPL_WIN32 (glconfig);

  switch (attribute)
    {
    case GDK_GL_USE_GL:
      if (impl->pfd.dwFlags & PFD_SUPPORT_OPENGL)
        {
	  *value = 1;

          /* Mark J. Kilgard says ...
	     XXX Brad's Matrox Millenium II has problems creating
             color index windows in 24-bit mode (lead to GDI crash)
             and 32-bit mode (lead to black window).  The cColorBits
             filed of the PIXELFORMATDESCRIPTOR returned claims to
             have 24 and 32 bits respectively of color indices. 2^24
             and 2^32 are ridiculously huge writable colormaps.
             Assume that if we get back a color index
             PIXELFORMATDESCRIPTOR with 24 or more bits, the
             PIXELFORMATDESCRIPTOR doesn't really work and skip it.
             -mjk */
#if 0
          if (impl->pfd.iPixelType == PFD_TYPE_COLORINDEX &&
	      impl->pfd.cColorBits >= 24)
            *value = 0;
          else
            *value = 1;
#endif
        }
      else
        {
          *value = 0;
        }
      break;
    case GDK_GL_BUFFER_SIZE:
      /* Nate Robins says ...
	 KLUDGE: if we're RGBA, return the number of bits/pixel,
         otherwise, return 8 (we guessed at 256 colors in CI mode). */
      if (impl->pfd.iPixelType == PFD_TYPE_RGBA)
        *value = impl->pfd.cColorBits;
      else
        *value = 8;
      break;
    case GDK_GL_LEVEL:
      *value = glconfig->layer_plane;
      break;
    case GDK_GL_RGBA:
      *value = impl->pfd.iPixelType == PFD_TYPE_RGBA;
      break;
    case GDK_GL_DOUBLEBUFFER:
      *value = impl->pfd.dwFlags & PFD_DOUBLEBUFFER;
      break;
    case GDK_GL_STEREO:
      *value = impl->pfd.dwFlags & PFD_STEREO;
      break;
    case GDK_GL_AUX_BUFFERS:
      *value = impl->pfd.cAuxBuffers;
      break;
    case GDK_GL_RED_SIZE:
      *value = impl->pfd.cRedBits;
      break;
    case GDK_GL_GREEN_SIZE:
      *value = impl->pfd.cGreenBits;
      break;
    case GDK_GL_BLUE_SIZE:
      *value = impl->pfd.cBlueBits;
      break;
    case GDK_GL_ALPHA_SIZE:
      *value = impl->pfd.cAlphaBits;
      break;
    case GDK_GL_DEPTH_SIZE:
      *value = impl->pfd.cDepthBits;
      break;
    case GDK_GL_STENCIL_SIZE:
      *value = impl->pfd.cStencilBits;
      break;
    case GDK_GL_ACCUM_RED_SIZE:
      *value = impl->pfd.cAccumRedBits;
      break;
    case GDK_GL_ACCUM_GREEN_SIZE:
      *value = impl->pfd.cAccumGreenBits;
      break;
    case GDK_GL_ACCUM_BLUE_SIZE:
      *value = impl->pfd.cAccumBlueBits;
      break;
    case GDK_GL_ACCUM_ALPHA_SIZE:
      *value = impl->pfd.cAccumAlphaBits;
      break;
    default:
      return FALSE;
    }

  return TRUE;
}

GdkColormap *
gdk_gl_config_get_colormap (GdkGLConfig *glconfig)
{
  g_return_val_if_fail (GDK_IS_GL_CONFIG_IMPL_WIN32 (glconfig), NULL);

  return GDK_GL_CONFIG_IMPL_WIN32 (glconfig)->colormap;
}

GdkVisual *
gdk_gl_config_get_visual (GdkGLConfig *glconfig)
{
  g_return_val_if_fail (GDK_IS_GL_CONFIG_IMPL_WIN32 (glconfig), NULL);

  return gdk_colormap_get_visual (GDK_GL_CONFIG_IMPL_WIN32 (glconfig)->colormap);
}

gint
gdk_gl_config_get_depth (GdkGLConfig *glconfig)
{
  g_return_val_if_fail (GDK_IS_GL_CONFIG_IMPL_WIN32 (glconfig), 0);

  return GDK_GL_CONFIG_IMPL_WIN32 (glconfig)->depth;
}

PIXELFORMATDESCRIPTOR *
gdk_win32_gl_config_get_pfd (GdkGLConfig *glconfig)
{
  g_return_val_if_fail (GDK_IS_GL_CONFIG_IMPL_WIN32 (glconfig), NULL);

  return &(GDK_GL_CONFIG_IMPL_WIN32 (glconfig)->pfd);
}

/*< private >*/
void
_gdk_win32_gl_print_pfd (PIXELFORMATDESCRIPTOR *pfd)
{
  g_message (" -- pfd->dwFlags & PFD_DRAW_TO_WINDOW      = %s",
             (pfd->dwFlags & PFD_DRAW_TO_WINDOW)      ? "TRUE" : "FALSE");
  g_message (" -- pfd->dwFlags & PFD_DRAW_TO_BITMAP      = %s",
             (pfd->dwFlags & PFD_DRAW_TO_BITMAP)      ? "TRUE" : "FALSE");
  g_message (" -- pfd->dwFlags & PFD_SUPPORT_GDI         = %s",
             (pfd->dwFlags & PFD_SUPPORT_GDI)         ? "TRUE" : "FALSE");
  g_message (" -- pfd->dwFlags & PFD_SUPPORT_OPENGL      = %s",
             (pfd->dwFlags & PFD_SUPPORT_OPENGL)      ? "TRUE" : "FALSE");
  g_message (" -- pfd->dwFlags & PFD_GENERIC_ACCELERATED = %s",
             (pfd->dwFlags & PFD_GENERIC_ACCELERATED) ? "TRUE" : "FALSE");
  g_message (" -- pfd->dwFlags & PFD_GENERIC_FORMAT      = %s",
             (pfd->dwFlags & PFD_GENERIC_FORMAT)      ? "TRUE" : "FALSE");
  g_message (" -- pfd->dwFlags & PFD_NEED_PALETTE        = %s",
             (pfd->dwFlags & PFD_NEED_PALETTE)        ? "TRUE" : "FALSE");
  g_message (" -- pfd->dwFlags & PFD_NEED_SYSTEM_PALETTE = %s",
             (pfd->dwFlags & PFD_NEED_SYSTEM_PALETTE) ? "TRUE" : "FALSE");
  g_message (" -- pfd->dwFlags & PFD_DOUBLEBUFFER        = %s",
             (pfd->dwFlags & PFD_DOUBLEBUFFER)        ? "TRUE" : "FALSE");
  g_message (" -- pfd->dwFlags & PFD_STEREO              = %s",
             (pfd->dwFlags & PFD_STEREO)              ? "TRUE" : "FALSE");
  g_message (" -- pfd->dwFlags & PFD_SWAP_LAYER_BUFFERS  = %s",
             (pfd->dwFlags & PFD_SWAP_LAYER_BUFFERS)  ? "TRUE" : "FALSE");

  g_message (" -- pfd->dwFlags & PFD_DEPTH_DONTCARE        = %s",
             (pfd->dwFlags & PFD_DEPTH_DONTCARE)        ? "TRUE" : "FALSE");
  g_message (" -- pfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE = %s",
             (pfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE) ? "TRUE" : "FALSE");
  g_message (" -- pfd->dwFlags & PFD_STEREO_DONTCARE       = %s",
             (pfd->dwFlags & PFD_STEREO_DONTCARE)       ? "TRUE" : "FALSE");

  g_message (" -- pfd->dwFlags & PFD_SWAP_COPY     = %s",
             (pfd->dwFlags & PFD_SWAP_COPY)     ? "TRUE" : "FALSE");
  g_message (" -- pfd->dwFlags & PFD_SWAP_EXCHANGE = %s",
             (pfd->dwFlags & PFD_SWAP_EXCHANGE) ? "TRUE" : "FALSE");

  g_message (" -- pfd->iPixelType = %d (%s)",
             pfd->iPixelType,
             (pfd->iPixelType == PFD_TYPE_RGBA) ? "PFD_TYPE_RGBA" : "PFD_TYPE_COLORINDEX");

  g_message (" -- pfd->cColorBits      = %d", pfd->cColorBits);
  g_message (" -- pfd->cRedBits        = %d", pfd->cRedBits);
  g_message (" -- pfd->cRedShift       = %d", pfd->cRedShift);
  g_message (" -- pfd->cGreenBits      = %d", pfd->cGreenBits);
  g_message (" -- pfd->cGreenShift     = %d", pfd->cGreenShift);
  g_message (" -- pfd->cBlueBits       = %d", pfd->cBlueBits);
  g_message (" -- pfd->cBlueShift      = %d", pfd->cBlueShift);
  g_message (" -- pfd->cAlphaBits      = %d", pfd->cAlphaBits);
  g_message (" -- pfd->cAlphaShift     = %d", pfd->cAlphaShift);
  g_message (" -- pfd->cAccumBits      = %d", pfd->cAccumBits);
  g_message (" -- pfd->cAccumRedBits   = %d", pfd->cAccumRedBits);
  g_message (" -- pfd->cAccumGreenBits = %d", pfd->cAccumGreenBits);
  g_message (" -- pfd->cAccumBlueBits  = %d", pfd->cAccumBlueBits);
  g_message (" -- pfd->cAccumAlphaBits = %d", pfd->cAccumAlphaBits);
  g_message (" -- pfd->cDepthBits      = %d", pfd->cDepthBits);
  g_message (" -- pfd->cStencilBits    = %d", pfd->cStencilBits);
  g_message (" -- pfd->cAuxBuffers     = %d", pfd->cAuxBuffers);

  /* Ignored */
  g_message (" -- pfd->iLayerType = %d", pfd->iLayerType);

  g_message (" -- pfd->bReserved & 0x0f        = %d", pfd->bReserved & 0x0f);
  g_message (" -- (pfd->bReserved & 0xf0) >> 4 = %d", (pfd->bReserved & 0xf0) >> 4);

  /* Ignored */
  g_message (" -- pfd->dwLayerMask = 0x%lx", pfd->dwLayerMask);

  g_message (" -- pfd->dwVisibleMask = 0x%lx", pfd->dwVisibleMask);

  g_message (" -- pfd->dwDamageMask = 0x%lx", pfd->dwDamageMask);
}