44347d61b53473de3219ef32c729068970cdc3f6
[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 gint fghGetConfig( gint attribute )
80 {
81     gint 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     gint returnValue;
141
142     freeglut_assert_ready;
143
144     /*
145      * Check what is the caller querying for. In chronological code add order.
146      */
147     switch( eWhat )
148     {
149     case GLUT_ELAPSED_TIME:
150         /*
151          * This is easy and nicely portable, as we are using GLib...
152          */
153         return( (int) (g_timer_elapsed( fgState.Timer, NULL ) * 1000.0) );
154
155     /*
156      * Following values are stored in fgState and fgDisplay global structures
157      */
158     case GLUT_SCREEN_WIDTH:         return( fgDisplay.ScreenWidth    );
159     case GLUT_SCREEN_HEIGHT:        return( fgDisplay.ScreenHeight   );
160     case GLUT_SCREEN_WIDTH_MM:      return( fgDisplay.ScreenWidthMM  );
161     case GLUT_SCREEN_HEIGHT_MM:     return( fgDisplay.ScreenHeightMM );
162     case GLUT_INIT_WINDOW_X:        return( fgState.Position.X       );
163     case GLUT_INIT_WINDOW_Y:        return( fgState.Position.Y       );
164     case GLUT_INIT_WINDOW_WIDTH:    return( fgState.Size.X           );
165     case GLUT_INIT_WINDOW_HEIGHT:   return( fgState.Size.Y           );
166     case GLUT_INIT_DISPLAY_MODE:    return( fgState.DisplayMode      );
167
168     /*
169      * The window/context specific queries are handled mostly by fghGetConfig().
170      */
171     case GLUT_WINDOW_NUM_SAMPLES:
172         /*
173          * Multisampling. Return what I know about multisampling.
174          */
175         return( 0 );
176
177     /*
178      * The rest of GLX queries is general enough to use a macro to check them
179      */
180 #   define GLX_QUERY(a,b) case a: return( fghGetConfig( b ) );
181
182     GLX_QUERY( GLUT_WINDOW_RGBA,                GLX_RGBA                );
183     GLX_QUERY( GLUT_WINDOW_DOUBLEBUFFER,        GLX_DOUBLEBUFFER        );
184     GLX_QUERY( GLUT_WINDOW_BUFFER_SIZE,         GLX_BUFFER_SIZE         );
185     GLX_QUERY( GLUT_WINDOW_STENCIL_SIZE,        GLX_STENCIL_SIZE        );
186     GLX_QUERY( GLUT_WINDOW_DEPTH_SIZE,          GLX_DEPTH_SIZE          );
187     GLX_QUERY( GLUT_WINDOW_RED_SIZE,            GLX_RED_SIZE            );
188     GLX_QUERY( GLUT_WINDOW_GREEN_SIZE,          GLX_GREEN_SIZE          );
189     GLX_QUERY( GLUT_WINDOW_BLUE_SIZE,           GLX_BLUE_SIZE           );
190     GLX_QUERY( GLUT_WINDOW_ALPHA_SIZE,          GLX_ALPHA_SIZE          );
191     GLX_QUERY( GLUT_WINDOW_ACCUM_RED_SIZE,      GLX_ACCUM_RED_SIZE      );
192     GLX_QUERY( GLUT_WINDOW_ACCUM_GREEN_SIZE,    GLX_ACCUM_GREEN_SIZE    );
193     GLX_QUERY( GLUT_WINDOW_ACCUM_BLUE_SIZE,     GLX_ACCUM_BLUE_SIZE     );
194     GLX_QUERY( GLUT_WINDOW_ACCUM_ALPHA_SIZE,    GLX_ACCUM_ALPHA_SIZE    );
195     GLX_QUERY( GLUT_WINDOW_STEREO,              GLX_STEREO              );
196
197 #   undef GLX_QUERY
198
199 #if TARGET_HOST_UNIX_X11
200     /*
201      * Colormap size is handled in a bit different way than all the rest
202      */
203     case GLUT_WINDOW_COLORMAP_SIZE:
204         /*
205          * Check for the visual type
206          */
207         if( (fghGetConfig( GLX_RGBA )) || (fgStructure.Window == NULL) )
208         {
209             /*
210              * We've got a RGBA visual, so there is no colormap at all.
211              * The other possibility is that we have no current window set.
212              */
213             return( 0 );
214         }
215
216         /*
217          * Otherwise return the number of entries in the colormap
218          */
219         return( fgStructure.Window->Window.VisualInfo->visual->map_entries );
220
221     /*
222      * Those calls are somewhat similiar, as they use XGetWindowAttributes() function
223      */
224     case GLUT_WINDOW_X:
225     case GLUT_WINDOW_Y:
226     {
227         XWindowAttributes winAttributes;
228         Window another, window;
229         gint x, y;
230
231         /*
232          * Return zero if there is no current window set
233          */
234         if( fgStructure.Window == NULL )
235             return( 0 );
236
237         /*
238          * So, grab the current window's position
239          */
240         window = fgStructure.Window->Window.Handle;
241
242         /*
243          * Grab the current window's attributes now
244          */
245         XGetWindowAttributes(
246             fgDisplay.Display,
247             window,
248             &winAttributes
249         );
250
251         /*
252          * Correct the results for the parental relation and border size
253          */
254         XTranslateCoordinates(
255             fgDisplay.Display,
256             window,
257             winAttributes.root,
258             -winAttributes.border_width,
259             -winAttributes.border_width,
260             &x, &y,
261             &another
262         );
263
264         /*
265          * See if we have to return the X or Y coordinate
266          */
267         return( eWhat == GLUT_WINDOW_X ? x : y );
268     }
269
270     case GLUT_WINDOW_WIDTH:
271     case GLUT_WINDOW_HEIGHT:
272     {
273         XWindowAttributes winAttributes;
274
275         /*
276          * Return zero if there is no current window set
277          */
278         if( fgStructure.Window == NULL )
279             return( 0 );
280
281         /*
282          * Checking for window's size is much easier:
283          */
284         XGetWindowAttributes(
285             fgDisplay.Display,
286             fgStructure.Window->Window.Handle,
287             &winAttributes
288         );
289
290         /*
291          * See if to return the window's width or height
292          */
293         return( eWhat == GLUT_WINDOW_WIDTH ? winAttributes.width : winAttributes.height );
294     }
295
296     /*
297      * I do not know yet if there will be a fgChooseVisual() function for Win32
298      */
299     case GLUT_DISPLAY_MODE_POSSIBLE:
300         /*
301          * Check if the current display mode is possible
302          */
303         return( fgChooseVisual() == NULL ? 0 : 1 );
304
305     /*
306      * This is system-dependant
307      */
308     case GLUT_WINDOW_FORMAT_ID:
309         /*
310          * Return the visual ID, if there is a current window naturally:
311          */
312         if( fgStructure.Window == NULL )
313             return( 0 );
314
315         return( fgStructure.Window->Window.VisualInfo->visualid );
316
317 #elif TARGET_HOST_WIN32
318
319     case GLUT_WINDOW_X:
320     case GLUT_WINDOW_Y:
321     case GLUT_WINDOW_WIDTH:
322     case GLUT_WINDOW_HEIGHT:
323     {
324         RECT winRect;
325
326         /*
327          * Check if there is a window to be queried for dimensions:
328          */
329         freeglut_return_val_if_fail( fgStructure.Window != NULL, 0 );
330
331         /*
332          * We need to call GetWindowRect() first...
333          */
334         GetWindowRect( fgStructure.Window->Window.Handle, &winRect );
335
336         /*
337          * ...then we've got to correct the results we've just received...
338          */
339         winRect.left   += GetSystemMetrics( SM_CXSIZEFRAME ) - 1;
340         winRect.right  -= GetSystemMetrics( SM_CXSIZEFRAME ) - 1;
341         winRect.top    += GetSystemMetrics( SM_CYSIZEFRAME ) - 1 + GetSystemMetrics( SM_CYCAPTION );
342         winRect.bottom -= GetSystemMetrics( SM_CYSIZEFRAME ) + 1;
343
344         /*
345          * ...and finally return the caller the desired value:
346          */
347         switch( eWhat )
348         {
349         case GLUT_WINDOW_X:      return( winRect.left                 );
350         case GLUT_WINDOW_Y:      return( winRect.top                  );
351         case GLUT_WINDOW_WIDTH:  return( winRect.right - winRect.left );
352         case GLUT_WINDOW_HEIGHT: return( winRect.bottom - winRect.top );
353         }
354     }
355     break;
356
357     case GLUT_DISPLAY_MODE_POSSIBLE:
358         /*
359          * Check if the current display mode is possible
360          */
361         return( fgSetupPixelFormat( fgStructure.Window, TRUE ) );
362
363     case GLUT_WINDOW_FORMAT_ID:
364         /*
365          * Return the pixel format of the current window
366          */
367         if( fgStructure.Window != NULL )
368             return( GetPixelFormat( fgStructure.Window->Window.Device ) );
369
370         /*
371          * If the current window does not exist, fail:
372          */
373         return( 0 );
374
375 #endif
376
377     /*
378      * The window structure queries
379      */
380     case GLUT_WINDOW_PARENT:
381         /*
382          * Return the ID number of current window's parent, if any
383          */
384         if( fgStructure.Window         == NULL ) return( 0 );
385         if( fgStructure.Window->Parent == NULL ) return( 0 );
386
387         return( fgStructure.Window->Parent->ID );
388
389     case GLUT_WINDOW_NUM_CHILDREN:
390         /*
391          * Return the number of children attached to the current window
392          */
393         if( fgStructure.Window == NULL )
394             return( 0 );
395
396         return( g_list_length( fgStructure.Window->Children ) );
397
398     case GLUT_WINDOW_CURSOR:
399         /*
400          * Return the currently selected window cursor
401          */
402         if( fgStructure.Window == NULL )
403             return( 0 );
404
405         return( fgStructure.Window->State.Cursor );
406
407     case GLUT_MENU_NUM_ITEMS:
408         /*
409          * Return the number of menu entries in the current menu
410          */
411         if( fgStructure.Menu == NULL )
412             return( 0 );
413
414         return( g_list_length( fgStructure.Menu->Entries ) );
415
416     default:
417         /*
418          * Just have it reported, so that we can see what needs to be implemented
419          */
420         g_warning( "glutGet(): missing enum handle %i\n", eWhat );
421         break;
422     }
423
424     /*
425      * If nothing happens, then we are in deep trouble...
426      */
427     g_assert_not_reached();
428 }
429
430 /*
431  * Returns various device information.
432  */
433 int FGAPIENTRY glutDeviceGet( GLenum eWhat )
434 {
435     freeglut_assert_ready;
436
437     /*
438      * See why are we bothered...
439      *
440      * WARNING: we are mostly lying in this function.
441      */
442     switch( eWhat )
443     {
444     case GLUT_HAS_KEYBOARD:
445         /*
446          * We always have a keyboard present on PC machines...
447          */
448         return( TRUE );
449
450 #if TARGET_HOST_UNIX_X11
451
452     case GLUT_HAS_MOUSE:
453         /*
454          * Hey, my Atari 65XE hasn't had a mouse!
455          */
456         return( TRUE );
457
458     case GLUT_NUM_MOUSE_BUTTONS:
459         /*
460          * Return the number of mouse buttons available. This is a big guess.
461          */
462         return( 3 );
463
464 #elif TARGET_HOST_WIN32
465
466     case GLUT_HAS_MOUSE:
467         /*
468          * The Windows can be booted without a mouse. 
469          * It would be nice to have this reported.
470          */
471         return( GetSystemMetrics( SM_MOUSEPRESENT ) );
472
473     case GLUT_NUM_MOUSE_BUTTONS:
474         /*
475          * We are much more fortunate under Win32 about this...
476          */
477         return( GetSystemMetrics( SM_CMOUSEBUTTONS ) );
478
479 #endif
480
481     case GLUT_JOYSTICK_POLL_RATE:
482     case GLUT_HAS_JOYSTICK:
483     case GLUT_JOYSTICK_BUTTONS:
484     case GLUT_JOYSTICK_AXES:
485         /*
486          * WARNING: THIS IS A BIG LIE!
487          */
488         return( 0 );
489
490     case GLUT_HAS_SPACEBALL:
491     case GLUT_HAS_DIAL_AND_BUTTON_BOX:
492     case GLUT_HAS_TABLET:
493         /*
494          * Sounds cool. And unuseful.
495          */
496         return( FALSE );
497
498     case GLUT_NUM_SPACEBALL_BUTTONS:
499     case GLUT_NUM_BUTTON_BOX_BUTTONS:
500     case GLUT_NUM_DIALS:
501     case GLUT_NUM_TABLET_BUTTONS:
502         /*
503          * Zero is not the answer. Zero is the question. Continuum is the answer.
504          */
505         return( 0 );
506
507     case GLUT_DEVICE_IGNORE_KEY_REPEAT:
508         /*
509          * Return what we think about the key auto repeat settings
510          */
511         return( fgState.IgnoreKeyRepeat );
512
513     case GLUT_DEVICE_KEY_REPEAT:
514         /*
515          * WARNING: THIS IS A BIG LIE!
516          */
517         return( GLUT_KEY_REPEAT_DEFAULT );
518
519     default:
520         /*
521          * Complain.
522          */
523         g_warning( "glutDeviceGet(): missing enum handle %i\n", eWhat );
524         break;
525     }
526
527     /*
528      * And now -- the failure.
529      */
530     g_assert_not_reached();
531 }
532
533 /*
534  * This should return the current state of ALT, SHIFT and CTRL keys.
535  */
536 int FGAPIENTRY glutGetModifiers( void )
537 {
538     /*
539      * Fail if there is no current window or called outside an input callback
540      */
541     if( fgStructure.Window == NULL )
542         return( 0 );
543
544     if( fgStructure.Window->State.Modifiers == 0xffffffff )
545     {
546         g_warning( "glutGetModifiers() called outside an input callback" );
547         return( 0 );
548     }
549
550     /*
551      * Return the current modifiers state otherwise
552      */
553     return( fgStructure.Window->State.Modifiers );
554 }
555
556 /*
557  * Return the state of the GLUT API overlay subsystem. A misery ;-)
558  */
559 int FGAPIENTRY glutLayerGet( GLenum eWhat )
560 {
561     freeglut_assert_ready;
562
563     /*
564      * This is easy as layers are not implemented ;-)
565      */
566     switch( eWhat )
567     {
568     case GLUT_OVERLAY_POSSIBLE:
569         /*
570          * Nope, overlays are not possible.
571          */
572         return( FALSE );
573
574     case GLUT_LAYER_IN_USE:
575         /*
576          * The normal plane is always in use
577          */
578         return( GLUT_NORMAL );
579
580     case GLUT_HAS_OVERLAY:
581         /*
582          * No window is allowed to have an overlay
583          */
584         return( FALSE );
585
586     case GLUT_TRANSPARENT_INDEX:
587         /*
588          * Return just anything, which is always defined as zero
589          */
590         return( 0 );
591
592     case GLUT_NORMAL_DAMAGED:
593         /*
594          * Actually I do not know. Maybe.
595          */
596         return( FALSE );
597
598     case GLUT_OVERLAY_DAMAGED:
599         /*
600          * Return minus one to mark that no layer is in use
601          */
602         return( -1 );
603
604     default:
605         /*
606          * Complain to the user about the obvious bug
607          */
608         g_warning( "glutLayerGet(): missing enum handle %i\n", eWhat );
609         break;
610     }
611
612     /*
613      * And fail. That's good. Programs do love failing.
614      */
615     g_assert_not_reached();
616 }
617
618 /*** END OF FILE ***/