Removed glib dependancy
[freeglut] / freeglut-1.3 / freeglut_state.c
1 /*
2  * freeglut_state.c
3  *
4  * Freeglut state query methods.
5  *
6  * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7  * Written by Pawel W. Olszta, <olszta@sourceforge.net>
8  * Creation date: Thu Dec 16 1999
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23  * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #define  G_LOG_DOMAIN  "freeglut-state"
33
34 #include "../include/GL/freeglut.h"
35 #include "../include/GL/freeglut_internal.h"
36
37 /*
38  * TODO BEFORE THE STABLE RELEASE:
39  *
40  *  glutGet()               -- X11 tests passed, but check if all enums handled (what about Win32?)
41  *  glutDeviceGet()         -- X11 tests passed, but check if all enums handled (what about Win32?)
42  *  glutGetModifiers()      -- OK, but could also remove the limitation
43  *  glutLayerGet()          -- what about GLUT_NORMAL_DAMAGED?
44  *
45  * The fail-on-call policy will help adding the most needed things imho.
46  */
47
48 /* -- LOCAL DEFINITIONS ---------------------------------------------------- */
49
50 /*
51  * Those are definitions introduced to make the glutGet() more easy,
52  * however they might introduce errors if someone ports GLX to Win32 :)
53  *
54  * Btw. this is not that a bad idea (wrapping WGL around GLX)...
55  */
56 #if TARGET_HOST_WIN32
57 #       define GLX_RGBA                 0x01
58 #       define GLX_DOUBLEBUFFER         0x02
59 #       define GLX_BUFFER_SIZE          0x03
60 #       define GLX_STENCIL_SIZE         0x04
61 #       define GLX_DEPTH_SIZE           0x05
62 #       define GLX_RED_SIZE             0x06
63 #       define GLX_GREEN_SIZE           0x07
64 #       define GLX_BLUE_SIZE            0x08
65 #       define GLX_ALPHA_SIZE           0x09
66 #       define GLX_ACCUM_RED_SIZE       0x0A
67 #       define GLX_ACCUM_GREEN_SIZE     0x0B
68 #       define GLX_ACCUM_BLUE_SIZE      0x0C
69 #       define GLX_ACCUM_ALPHA_SIZE     0x0D
70 #       define GLX_STEREO               0x0E
71 #endif
72
73
74 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
75
76 /*
77  * Queries the GL context about some attributes
78  */
79 static int fghGetConfig( int attribute )
80 {
81     int returnValue;
82
83     /*
84      * Return nothing if there is no current window set
85      */
86     if( fgStructure.Window == NULL )
87         return( 0 );
88
89 #if TARGET_HOST_UNIX_X11
90     /*
91      * glXGetConfig should work fine
92      */
93     glXGetConfig( fgDisplay.Display, fgStructure.Window->Window.VisualInfo, attribute, &returnValue );
94
95 #elif TARGET_HOST_WIN32
96     /*
97      * This is going to be a bit harder than the X11 version...
98      */
99 #   pragma message( "freeglut_state.c::fghGetConfig() says hello world my name is not implemented!" )
100     switch( attribute )
101     {
102     case GLX_RGBA:
103     case GLX_DOUBLEBUFFER:
104     case GLX_BUFFER_SIZE:
105     case GLX_RED_SIZE:
106     case GLX_GREEN_SIZE:
107     case GLX_BLUE_SIZE:
108     case GLX_DEPTH_SIZE:
109         return( 1 );
110
111     case GLX_STENCIL_SIZE:
112     case GLX_ALPHA_SIZE:
113     case GLX_ACCUM_RED_SIZE:
114     case GLX_ACCUM_GREEN_SIZE:
115     case GLX_ACCUM_BLUE_SIZE:
116     case GLX_ACCUM_ALPHA_SIZE:
117     case GLX_STEREO:
118     default:
119         /*
120          * Well, this looks like not implemented to me :)
121          */
122         return( 0 );
123     }
124
125 #endif
126
127     /*
128      * Have the query results returned
129      */
130     return( returnValue );
131 }
132
133 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
134
135 /*
136  * General settings query method
137  */
138 int FGAPIENTRY glutGet( GLenum eWhat )
139 {
140     freeglut_assert_ready;
141
142     /*
143      * Check what is the caller querying for. In chronological code add order.
144      */
145     switch( eWhat )
146     {
147     case GLUT_ELAPSED_TIME:
148         /*
149          * This is easy and nicely portable, as we are using GLib...
150          */
151         return( fgElapsedTime() );
152
153     /*
154      * Following values are stored in fgState and fgDisplay global structures
155      */
156     case GLUT_SCREEN_WIDTH:         return( fgDisplay.ScreenWidth    );
157     case GLUT_SCREEN_HEIGHT:        return( fgDisplay.ScreenHeight   );
158     case GLUT_SCREEN_WIDTH_MM:      return( fgDisplay.ScreenWidthMM  );
159     case GLUT_SCREEN_HEIGHT_MM:     return( fgDisplay.ScreenHeightMM );
160     case GLUT_INIT_WINDOW_X:        return( fgState.Position.X       );
161     case GLUT_INIT_WINDOW_Y:        return( fgState.Position.Y       );
162     case GLUT_INIT_WINDOW_WIDTH:    return( fgState.Size.X           );
163     case GLUT_INIT_WINDOW_HEIGHT:   return( fgState.Size.Y           );
164     case GLUT_INIT_DISPLAY_MODE:    return( fgState.DisplayMode      );
165
166     /*
167      * The window/context specific queries are handled mostly by fghGetConfig().
168      */
169     case GLUT_WINDOW_NUM_SAMPLES:
170         /*
171          * Multisampling. Return what I know about multisampling.
172          */
173         return( 0 );
174
175     /*
176      * The rest of GLX queries is general enough to use a macro to check them
177      */
178 #   define GLX_QUERY(a,b) case a: return( fghGetConfig( b ) );
179
180     GLX_QUERY( GLUT_WINDOW_RGBA,                GLX_RGBA                );
181     GLX_QUERY( GLUT_WINDOW_DOUBLEBUFFER,        GLX_DOUBLEBUFFER        );
182     GLX_QUERY( GLUT_WINDOW_BUFFER_SIZE,         GLX_BUFFER_SIZE         );
183     GLX_QUERY( GLUT_WINDOW_STENCIL_SIZE,        GLX_STENCIL_SIZE        );
184     GLX_QUERY( GLUT_WINDOW_DEPTH_SIZE,          GLX_DEPTH_SIZE          );
185     GLX_QUERY( GLUT_WINDOW_RED_SIZE,            GLX_RED_SIZE            );
186     GLX_QUERY( GLUT_WINDOW_GREEN_SIZE,          GLX_GREEN_SIZE          );
187     GLX_QUERY( GLUT_WINDOW_BLUE_SIZE,           GLX_BLUE_SIZE           );
188     GLX_QUERY( GLUT_WINDOW_ALPHA_SIZE,          GLX_ALPHA_SIZE          );
189     GLX_QUERY( GLUT_WINDOW_ACCUM_RED_SIZE,      GLX_ACCUM_RED_SIZE      );
190     GLX_QUERY( GLUT_WINDOW_ACCUM_GREEN_SIZE,    GLX_ACCUM_GREEN_SIZE    );
191     GLX_QUERY( GLUT_WINDOW_ACCUM_BLUE_SIZE,     GLX_ACCUM_BLUE_SIZE     );
192     GLX_QUERY( GLUT_WINDOW_ACCUM_ALPHA_SIZE,    GLX_ACCUM_ALPHA_SIZE    );
193     GLX_QUERY( GLUT_WINDOW_STEREO,              GLX_STEREO              );
194
195 #   undef GLX_QUERY
196
197 #if TARGET_HOST_UNIX_X11
198     /*
199      * Colormap size is handled in a bit different way than all the rest
200      */
201     case GLUT_WINDOW_COLORMAP_SIZE:
202         /*
203          * Check for the visual type
204          */
205         if( (fghGetConfig( GLX_RGBA )) || (fgStructure.Window == NULL) )
206         {
207             /*
208              * We've got a RGBA visual, so there is no colormap at all.
209              * The other possibility is that we have no current window set.
210              */
211             return( 0 );
212         }
213
214         /*
215          * Otherwise return the number of entries in the colormap
216          */
217         return( fgStructure.Window->Window.VisualInfo->visual->map_entries );
218
219     /*
220      * Those calls are somewhat similiar, as they use XGetWindowAttributes() function
221      */
222     case GLUT_WINDOW_X:
223     case GLUT_WINDOW_Y:
224     {
225         XWindowAttributes winAttributes;
226         Window another, window;
227         int x, y;
228
229         /*
230          * Return zero if there is no current window set
231          */
232         if( fgStructure.Window == NULL )
233             return( 0 );
234
235         /*
236          * So, grab the current window's position
237          */
238         window = fgStructure.Window->Window.Handle;
239
240         /*
241          * Grab the current window's attributes now
242          */
243         XGetWindowAttributes(
244             fgDisplay.Display,
245             window,
246             &winAttributes
247         );
248
249         /*
250          * Correct the results for the parental relation and border size
251          */
252         XTranslateCoordinates(
253             fgDisplay.Display,
254             window,
255             winAttributes.root,
256             -winAttributes.border_width,
257             -winAttributes.border_width,
258             &x, &y,
259             &another
260         );
261
262         /*
263          * See if we have to return the X or Y coordinate
264          */
265         return( eWhat == GLUT_WINDOW_X ? x : y );
266     }
267
268     case GLUT_WINDOW_WIDTH:
269     case GLUT_WINDOW_HEIGHT:
270     {
271         XWindowAttributes winAttributes;
272
273         /*
274          * Return zero if there is no current window set
275          */
276         if( fgStructure.Window == NULL )
277             return( 0 );
278
279         /*
280          * Checking for window's size is much easier:
281          */
282         XGetWindowAttributes(
283             fgDisplay.Display,
284             fgStructure.Window->Window.Handle,
285             &winAttributes
286         );
287
288         /*
289          * See if to return the window's width or height
290          */
291         return( eWhat == GLUT_WINDOW_WIDTH ? winAttributes.width : winAttributes.height );
292     }
293
294     /*
295      * I do not know yet if there will be a fgChooseVisual() function for Win32
296      */
297     case GLUT_DISPLAY_MODE_POSSIBLE:
298         /*
299          * Check if the current display mode is possible
300          */
301         return( fgChooseVisual() == NULL ? 0 : 1 );
302
303     /*
304      * This is system-dependant
305      */
306     case GLUT_WINDOW_FORMAT_ID:
307         /*
308          * Return the visual ID, if there is a current window naturally:
309          */
310         if( fgStructure.Window == NULL )
311             return( 0 );
312
313         return( fgStructure.Window->Window.VisualInfo->visualid );
314
315 #elif TARGET_HOST_WIN32
316
317     case GLUT_WINDOW_X:
318     case GLUT_WINDOW_Y:
319     case GLUT_WINDOW_WIDTH:
320     case GLUT_WINDOW_HEIGHT:
321     {
322         RECT winRect;
323
324         /*
325          * Check if there is a window to be queried for dimensions:
326          */
327         freeglut_return_val_if_fail( fgStructure.Window != NULL, 0 );
328
329         /*
330          * We need to call GetWindowRect() first...
331          */
332         GetWindowRect( fgStructure.Window->Window.Handle, &winRect );
333
334         /*
335          * ...then we've got to correct the results we've just received...
336          */
337         winRect.left   += GetSystemMetrics( SM_CXSIZEFRAME ) - 1;
338         winRect.right  -= GetSystemMetrics( SM_CXSIZEFRAME ) - 1;
339         winRect.top    += GetSystemMetrics( SM_CYSIZEFRAME ) - 1 + GetSystemMetrics( SM_CYCAPTION );
340         winRect.bottom -= GetSystemMetrics( SM_CYSIZEFRAME ) + 1;
341
342         /*
343          * ...and finally return the caller the desired value:
344          */
345         switch( eWhat )
346         {
347         case GLUT_WINDOW_X:      return( winRect.left                 );
348         case GLUT_WINDOW_Y:      return( winRect.top                  );
349         case GLUT_WINDOW_WIDTH:  return( winRect.right - winRect.left );
350         case GLUT_WINDOW_HEIGHT: return( winRect.bottom - winRect.top );
351         }
352     }
353     break;
354
355     case GLUT_DISPLAY_MODE_POSSIBLE:
356         /*
357          * Check if the current display mode is possible
358          */
359         return( fgSetupPixelFormat( fgStructure.Window, TRUE ) );
360
361     case GLUT_WINDOW_FORMAT_ID:
362         /*
363          * Return the pixel format of the current window
364          */
365         if( fgStructure.Window != NULL )
366             return( GetPixelFormat( fgStructure.Window->Window.Device ) );
367
368         /*
369          * If the current window does not exist, fail:
370          */
371         return( 0 );
372
373 #endif
374
375     /*
376      * The window structure queries
377      */
378     case GLUT_WINDOW_PARENT:
379         /*
380          * Return the ID number of current window's parent, if any
381          */
382         if( fgStructure.Window         == NULL ) return( 0 );
383         if( fgStructure.Window->Parent == NULL ) return( 0 );
384
385         return( fgStructure.Window->Parent->ID );
386
387     case GLUT_WINDOW_NUM_CHILDREN:
388         /*
389          * Return the number of children attached to the current window
390          */
391         if( fgStructure.Window == NULL )
392             return( 0 );
393
394         return( fgListLength( &fgStructure.Window->Children ) );
395
396     case GLUT_WINDOW_CURSOR:
397         /*
398          * Return the currently selected window cursor
399          */
400         if( fgStructure.Window == NULL )
401             return( 0 );
402
403         return( fgStructure.Window->State.Cursor );
404
405     case GLUT_MENU_NUM_ITEMS:
406         /*
407          * Return the number of menu entries in the current menu
408          */
409         if( fgStructure.Menu == NULL )
410             return( 0 );
411
412         return( fgListLength( &fgStructure.Menu->Entries ) );
413
414     default:
415         /*
416          * Just have it reported, so that we can see what needs to be implemented
417          */
418         fgWarning( "glutGet(): missing enum handle %i\n", eWhat );
419         break;
420     }
421
422     /*
423      * If nothing happens, then we are in deep trouble...
424      */
425     return( -1 );
426 }
427
428 /*
429  * Returns various device information.
430  */
431 int FGAPIENTRY glutDeviceGet( GLenum eWhat )
432 {
433     freeglut_assert_ready;
434
435     /*
436      * See why are we bothered...
437      *
438      * WARNING: we are mostly lying in this function.
439      */
440     switch( eWhat )
441     {
442     case GLUT_HAS_KEYBOARD:
443         /*
444          * We always have a keyboard present on PC machines...
445          */
446         return( TRUE );
447
448 #if TARGET_HOST_UNIX_X11
449
450     case GLUT_HAS_MOUSE:
451         /*
452          * Hey, my Atari 65XE hasn't had a mouse!
453          */
454         return( TRUE );
455
456     case GLUT_NUM_MOUSE_BUTTONS:
457         /*
458          * Return the number of mouse buttons available. This is a big guess.
459          */
460         return( 3 );
461
462 #elif TARGET_HOST_WIN32
463
464     case GLUT_HAS_MOUSE:
465         /*
466          * The Windows can be booted without a mouse. 
467          * It would be nice to have this reported.
468          */
469         return( GetSystemMetrics( SM_MOUSEPRESENT ) );
470
471     case GLUT_NUM_MOUSE_BUTTONS:
472         /*
473          * We are much more fortunate under Win32 about this...
474          */
475         return( GetSystemMetrics( SM_CMOUSEBUTTONS ) );
476
477 #endif
478
479     case GLUT_JOYSTICK_POLL_RATE:
480     case GLUT_HAS_JOYSTICK:
481     case GLUT_JOYSTICK_BUTTONS:
482     case GLUT_JOYSTICK_AXES:
483         /*
484          * WARNING: THIS IS A BIG LIE!
485          */
486         return( 0 );
487
488     case GLUT_HAS_SPACEBALL:
489     case GLUT_HAS_DIAL_AND_BUTTON_BOX:
490     case GLUT_HAS_TABLET:
491         /*
492          * Sounds cool. And unuseful.
493          */
494         return( FALSE );
495
496     case GLUT_NUM_SPACEBALL_BUTTONS:
497     case GLUT_NUM_BUTTON_BOX_BUTTONS:
498     case GLUT_NUM_DIALS:
499     case GLUT_NUM_TABLET_BUTTONS:
500         /*
501          * Zero is not the answer. Zero is the question. Continuum is the answer.
502          */
503         return( 0 );
504
505     case GLUT_DEVICE_IGNORE_KEY_REPEAT:
506         /*
507          * Return what we think about the key auto repeat settings
508          */
509         return( fgState.IgnoreKeyRepeat );
510
511     case GLUT_DEVICE_KEY_REPEAT:
512         /*
513          * WARNING: THIS IS A BIG LIE!
514          */
515         return( GLUT_KEY_REPEAT_DEFAULT );
516
517     default:
518         /*
519          * Complain.
520          */
521         fgWarning( "glutDeviceGet(): missing enum handle %i\n", eWhat );
522         break;
523     }
524
525     /*
526      * And now -- the failure.
527      */
528     return( -1 );
529 }
530
531 /*
532  * This should return the current state of ALT, SHIFT and CTRL keys.
533  */
534 int FGAPIENTRY glutGetModifiers( void )
535 {
536     /*
537      * Fail if there is no current window or called outside an input callback
538      */
539     if( fgStructure.Window == NULL )
540         return( 0 );
541
542     if( fgStructure.Window->State.Modifiers == 0xffffffff )
543     {
544         fgWarning( "glutGetModifiers() called outside an input callback" );
545         return( 0 );
546     }
547
548     /*
549      * Return the current modifiers state otherwise
550      */
551     return( fgStructure.Window->State.Modifiers );
552 }
553
554 /*
555  * Return the state of the GLUT API overlay subsystem. A misery ;-)
556  */
557 int FGAPIENTRY glutLayerGet( GLenum eWhat )
558 {
559     freeglut_assert_ready;
560
561     /*
562      * This is easy as layers are not implemented ;-)
563      */
564     switch( eWhat )
565     {
566     case GLUT_OVERLAY_POSSIBLE:
567         /*
568          * Nope, overlays are not possible.
569          */
570         return( FALSE );
571
572     case GLUT_LAYER_IN_USE:
573         /*
574          * The normal plane is always in use
575          */
576         return( GLUT_NORMAL );
577
578     case GLUT_HAS_OVERLAY:
579         /*
580          * No window is allowed to have an overlay
581          */
582         return( FALSE );
583
584     case GLUT_TRANSPARENT_INDEX:
585         /*
586          * Return just anything, which is always defined as zero
587          */
588         return( 0 );
589
590     case GLUT_NORMAL_DAMAGED:
591         /*
592          * Actually I do not know. Maybe.
593          */
594         return( FALSE );
595
596     case GLUT_OVERLAY_DAMAGED:
597         /*
598          * Return minus one to mark that no layer is in use
599          */
600         return( -1 );
601
602     default:
603         /*
604          * Complain to the user about the obvious bug
605          */
606         fgWarning( "glutLayerGet(): missing enum handle %i\n", eWhat );
607         break;
608     }
609
610     /*
611      * And fail. That's good. Programs do love failing.
612      */
613     return( -1 );
614 }
615
616 /*** END OF FILE ***/