1/* -*- mode:ObjC -*-
2   Win32GLContext - backend implementation of NSOpenGLContext
3
4   Copyright (C) 1998, 2002, 2007 Free Software Foundation, Inc.
5
6   Written by:  Xavier Glattard
7   Date: Jan 2007
8
9   This file is part of the GNU Objective C User Interface Library.
10
11   This library is free software; you can redistribute it and/or
12   modify it under the terms of the GNU Lesser General Public
13   License as published by the Free Software Foundation; either
14   version 2 of the License, or (at your option) any later version.
15
16   This library is distributed in the hope that it will be useful,
17   but WITHOUT ANY WARRANTY; without even the implied warranty of
18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
19   Lesser General Public License for more details.
20
21   You should have received a copy of the GNU Lesser General Public
22   License along with this library; see the file COPYING.LIB.
23   If not, see <http://www.gnu.org/licenses/> or write to the
24   Free Software Foundation, 51 Franklin Street, Fifth Floor,
25   Boston, MA 02110-1301, USA.
26*/
27
28#include "config.h"
29#ifdef HAVE_WGL
30#include <Foundation/NSDebug.h>
31#include <Foundation/NSException.h>
32#include <Foundation/NSData.h>
33#include <GNUstepGUI/GSDisplayServer.h>
34#include "win32/WIN32Server.h"
35#include "win32/WIN32OpenGL.h"
36
37static void attributesNS2WGL( NSOpenGLPixelFormatAttribute *attribs, LPPIXELFORMATDESCRIPTOR ppfd )
38{
39  // TODO : switch to wglChoosePixelFormatEXT
40  NSOpenGLPixelFormatAttribute *ptr = attribs;
41
42  ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
43  ppfd->nVersion = 1;
44  ppfd->dwFlags = 0;
45
46  ppfd->dwFlags |= PFD_SUPPORT_OPENGL;
47  ppfd->iPixelType = PFD_TYPE_RGBA;
48
49  while (*ptr)
50    {
51      switch(*ptr)
52	{
53	// it means all the same on WGL - there is no difference here
54	case NSOpenGLPFAWindow:
55	  ppfd->dwFlags |= PFD_DRAW_TO_WINDOW;
56	  break;
57	case NSOpenGLPFAOffScreen:
58	  ppfd->dwFlags |= PFD_DRAW_TO_BITMAP;
59	  break;
60        case NSOpenGLPFAPixelBuffer: // TODO
61	  //ppfd->dwFlags |= PFD_DRAW_TO_WINDOW;
62	  break;
63	case NSOpenGLPFASingleRenderer:
64	case NSOpenGLPFAAllRenderers:
65	case NSOpenGLPFAAccelerated:
66	  ppfd->dwFlags |= PFD_GENERIC_ACCELERATED;
67	  break;
68	case  NSOpenGLPFADoubleBuffer:
69	  ppfd->dwFlags |= PFD_DOUBLEBUFFER;
70	  break;
71	case NSOpenGLPFAStereo:
72	  ppfd->dwFlags |= PFD_STEREO;
73	  break;
74	case NSOpenGLPFABackingStore:
75	  ppfd->dwFlags |= PFD_SWAP_COPY;
76	  break;
77	case NSOpenGLPFAAuxBuffers:
78	  ptr++;
79	  ppfd->cAuxBuffers = *ptr;
80	  break;
81	case NSOpenGLPFAColorSize:
82	  ptr++;
83	  ppfd->cColorBits = *ptr;
84	  break;
85	case NSOpenGLPFAAlphaSize:
86	  ptr++;
87	  ppfd->cAlphaBits = *ptr;
88	  break;
89	case NSOpenGLPFADepthSize:
90	  ptr++;
91	  ppfd->cDepthBits = *ptr;
92	  break;
93	case NSOpenGLPFAStencilSize:
94	  ptr++;
95	  ppfd->cStencilBits = *ptr;
96	  break;
97	case NSOpenGLPFAAccumSize:
98	  ptr++;
99	  ppfd->cAccumBits = *ptr;
100	  break;
101	switch ((int)*ptr)
102		{
103		case 8:
104		 	ppfd->cAccumRedBits = 3;
105		 	ppfd->cAccumGreenBits = 3;
106		 	ppfd->cAccumBlueBits = 2;
107		 	ppfd->cAccumAlphaBits = 0;
108		 	break;
109		case 15:
110		case 16:
111		 	ppfd->cAccumRedBits = 5;
112		 	ppfd->cAccumGreenBits = 5;
113		 	ppfd->cAccumBlueBits = 5;
114		 	ppfd->cAccumAlphaBits = 0;
115			break;
116		case 24:
117		 	ppfd->cAccumRedBits = 8;
118		 	ppfd->cAccumGreenBits = 8;
119		 	ppfd->cAccumBlueBits = 8;
120		 	ppfd->cAccumAlphaBits = 0;
121			break;
122		case 32:
123		 	ppfd->cAccumRedBits = 8;
124		 	ppfd->cAccumGreenBits = 8;
125		 	ppfd->cAccumBlueBits = 8;
126		 	ppfd->cAccumAlphaBits = 8;
127			break;
128		}
129		break;
130	//can not be handle by WGL
131	case NSOpenGLPFAMinimumPolicy:
132	  break;
133	// can not be handle by WGL
134	case NSOpenGLPFAMaximumPolicy:
135	  break;
136
137	//FIXME all of this stuff...
138	case NSOpenGLPFAFullScreen:
139	case NSOpenGLPFASampleBuffers:
140	case NSOpenGLPFASamples:
141	case NSOpenGLPFAAuxDepthStencil:
142	case NSOpenGLPFARendererID:
143	case NSOpenGLPFANoRecovery:
144	case NSOpenGLPFAClosestPolicy:
145	case NSOpenGLPFARobust:
146	case NSOpenGLPFAMPSafe:
147	case NSOpenGLPFAMultiScreen:
148	case NSOpenGLPFACompliant:
149	case NSOpenGLPFAScreenMask:
150	case NSOpenGLPFAVirtualScreenCount:
151
152        case NSOpenGLPFAAllowOfflineRenderers:
153        case NSOpenGLPFAColorFloat:
154        case NSOpenGLPFAMultisample:
155        case NSOpenGLPFASupersample:
156        case NSOpenGLPFASampleAlpha:
157	  break;
158	}
159      ptr ++;
160    }
161}
162
163static void attributesWGL2NS( LPPIXELFORMATDESCRIPTOR ppfd, NSOpenGLPixelFormatAttribute *attribs )
164{
165  // TODO
166}
167
168@implementation Win32GLPixelFormat
169
170- (void)getValues:(long *)vals
171     forAttribute:(NSOpenGLPixelFormatAttribute)attrib
172 forVirtualScreen:(int)screen
173{
174  PIXELFORMATDESCRIPTOR pfd;
175
176  //glXGetConfig(dpy, conf.visual, attrib, (int *)vals);
177  DescribePixelFormat( wgl_drawable, wgl_pixelformat, sizeof(pfd), &pfd);
178}
179
180- (id)initWithAttributes: (NSOpenGLPixelFormatAttribute *) attribs
181{
182  NSDebugMLLog(@"WGL", @"will init");
183  self = [super init];
184  if(self)
185  {
186    wgl_drawable = 0;
187    wgl_pixelformat = 0;
188
189    attributesNS2WGL(attribs, &pfd);
190  }
191  return self;
192}
193#if 0
194  //FIXME, what screen number ?
195  if (GSglxMinorVersion (dpy) >= 3)
196    conf.tab = glXChooseFBConfig(dpy, DefaultScreen(dpy), [data mutableBytes],
197				 &n_elem);
198  else
199    conf.visual = glXChooseVisual(dpy, DefaultScreen(dpy),
200				  [data mutableBytes]);
201
202  if (((GSglxMinorVersion (dpy) >= 3)
203	? (void *)conf.tab : (void *)conf.visual)
204       == NULL)
205    {
206      NSDebugMLLog(@"GLX", @"no pixel format found matching what is required");
207      RELEASE(self);
208      return nil;
209    }
210  else
211    {
212
213      NSDebugMLLog(@"GLX", @"We found %d pixel formats", n_elem);
214#if 0
215      if (GSglxMinorVersion (dpy) >= 3)
216	{
217	  int i;
218	  for (i = 0; i < n_elem; ++i)
219	    {
220	      int val;
221	      NSDebugMLLog(@"GLX", @"inspecting %dth", i+1);
222	      glXGetFBConfigAttrib(dpy, conf.tab[i], GLX_BUFFER_SIZE, &val);
223	      NSDebugMLLog(@"GLX", @"buffer size %d", val);
224
225
226	      glXGetFBConfigAttrib(dpy, conf.tab[i], GLX_DOUBLEBUFFER, &val);
227	      NSDebugMLLog(@"GLX", @"double buffer %d", val);
228
229	      glXGetFBConfigAttrib(dpy, conf.tab[i], GLX_DEPTH_SIZE, &val);
230	      NSDebugMLLog(@"GLX", @"depth size %d", val);
231
232	    }
233	}
234      else
235	{
236	  glXGetConfig(dpy, conf.visual, GLX_BUFFER_SIZE, &val);
237	  NSDebugMLLog(@"GLX", @"buffer size %d", val);
238
239
240	  glXGetConfig(dpy, conf.visual, GLX_DOUBLEBUFFER, &val);
241	  NSDebugMLLog(@"GLX", @"double buffer %d", val);
242
243	  glXGetConfig(dpy, conf.visual, GLX_DEPTH_SIZE, &val);
244	  NSDebugMLLog(@"GLX", @"depth size %d", val);
245	}
246#endif
247      return self;
248    }
249}
250#endif
251
252- (void) _setDrawable: (HDC) aDrawable
253{
254  // TODO : switch to wglChoosePixelFormatEXT
255
256  NSCAssert(
257      wgl_pixelformat = ChoosePixelFormat(aDrawable, &pfd),
258      @"ChoosePixelFormat failed.");
259  NSCAssert(
260      SetPixelFormat(aDrawable, wgl_pixelformat, &pfd),
261      @"SetPixelFormat failed.");
262  wgl_drawable = aDrawable;
263
264  NSDebugFLLog(@"WGL", @"found : %u", wgl_pixelformat);
265}
266
267- (void) dealloc
268{
269  NSDebugMLLog(@"WGL", @"deallocation");
270  [super dealloc];
271}
272
273- (int)numberOfVirtualScreens
274{
275  //  [self notImplemented: _cmd];
276  //FIXME
277  //This looks like a reasonable value to return...
278  return 1;
279}
280
281@end
282#endif
283