1 /*
2  * fg_state_blackberry.c
3  *
4  * BlackBerry-specific freeglut state query methods.
5  *
6  * Copyright (c) 2012 Stephen J. Baker. All Rights Reserved.
7  * Written by John F. Fay, <fayjf@sourceforge.net>
8  * Copyright (C) 2012  Sylvain Beucler
9  * Copyright (C) 2013  Vincent Simonetti
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included
19  * in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24  * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
25  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  */
28 
29 #include <GL/freeglut.h>
30 #include <stdio.h>
31 #include <screen/screen.h>
32 #include "fg_internal.h"
33 #include "egl/fg_state_egl.h"
34 
35 //From fg_state_android.c
fgPlatformGlutDeviceGet(GLenum eWhat)36 int fgPlatformGlutDeviceGet ( GLenum eWhat )
37 {
38 #ifndef __PLAYBOOK__
39     int deviceCount, i, value;
40     screen_device_t* devices;
41 #endif
42 
43     switch( eWhat )
44     {
45     case GLUT_HAS_KEYBOARD:
46         /* BlackBerry has a keyboard, though it may be virtual. */
47         return 1;
48 
49     case GLUT_HAS_MOUSE:
50         /* BlackBerry has a touchscreen. Consider it as a mouse since we have no guarantee
51            that a mouse will be used (in which case it's a simulator). */
52         return 1 ;
53 
54     case GLUT_NUM_MOUSE_BUTTONS:
55         /* BlackBerry has a touchscreen, which we can consider a 1-button mouse at min.
56            Otherwise check for an actual mouse, else get max touch points. PlayBook does not support this. */
57 #ifndef __PLAYBOOK__
58         if(!screen_get_context_property_iv(fgDisplay.pDisplay.screenContext, SCREEN_PROPERTY_DEVICE_COUNT, &deviceCount)) {
59             devices = (screen_device_t*)calloc(deviceCount, sizeof(screen_device_t));
60             if(!screen_get_context_property_pv(fgDisplay.pDisplay.screenContext, SCREEN_PROPERTY_DEVICES, (void**)devices)) {
61                 /* Check for a pointer */
62                 for(i = 0; i < deviceCount; i++) {
63                     if(!screen_get_device_property_iv(devices[i], SCREEN_PROPERTY_TYPE, &value) &&
64                             value == SCREEN_EVENT_POINTER &&
65                             !screen_get_device_property_iv(devices[i], SCREEN_PROPERTY_BUTTON_COUNT, &value)) {
66                         free(devices);
67                         return value;
68                     }
69                 }
70                 /* Check for mtouch */
71                 for(i = 0; i < deviceCount; i++) {
72                     if(!screen_get_device_property_iv(devices[i], SCREEN_PROPERTY_TYPE, &value) &&
73                             value == SCREEN_EVENT_MTOUCH_TOUCH &&
74                             !screen_get_device_property_iv(devices[i], SCREEN_PROPERTY_MAXIMUM_TOUCH_ID, &value)) {
75                         free(devices);
76                         return value;
77                     }
78                 }
79             }
80             free(devices);
81         }
82 #endif
83         /* Backup, pretend it's a 1-button mouse */
84         return 1;
85 
86     default:
87         fgWarning( "glutDeviceGet(): missing enum handle %d", eWhat );
88         break;
89     }
90 
91     /* And now -- the failure. */
92     return -1;
93 }
94 
fgPlatformGlutGet(GLenum eWhat)95 int fgPlatformGlutGet ( GLenum eWhat )
96 {
97     switch (eWhat) {
98     /* One full-screen window only */
99     case GLUT_WINDOW_X:
100     case GLUT_WINDOW_Y:
101     case GLUT_WINDOW_BORDER_WIDTH:
102     case GLUT_WINDOW_HEADER_HEIGHT:
103         return 0;
104 
105     case GLUT_WINDOW_WIDTH:
106     case GLUT_WINDOW_HEIGHT:
107     {
108         if ( fgStructure.CurrentWindow == NULL )
109             return 0;
110 
111         int size[2];
112         int orientation;
113         if ( screen_get_window_property_iv(fgStructure.CurrentWindow->Window.Handle, SCREEN_PROPERTY_BUFFER_SIZE, size) != 0 )
114             return 0;
115         if ( screen_get_window_property_iv(fgStructure.CurrentWindow->Window.Handle, SCREEN_PROPERTY_ROTATION, &orientation) != 0 )
116             return 0;
117 
118         int orientationDif = abs(orientation - fgStructure.CurrentWindow->State.pWState.originalRotation);
119         if (orientationDif == 90 || orientationDif == 270) {
120             /* Swap dim. if screen is rotated */
121             int tmp = size[0];
122             size[0] = size[1];
123             size[1] = tmp;
124         }
125         switch ( eWhat )
126         {
127         case GLUT_WINDOW_WIDTH:
128             return size[0];
129         case GLUT_WINDOW_HEIGHT:
130             return size[1];
131         }
132         break;
133     }
134 
135     case GLUT_WINDOW_COLORMAP_SIZE:
136         /* 0 for RGBA/non-indexed mode */
137         /* Under BlackBerry and GLES more generally, no indexed-mode */
138         return 0;
139 
140     default:
141         return fghPlatformGlutGetEGL(eWhat);
142     }
143     return -1;
144 }
145