Made glutGet, for window width and height, return width and height determined by...
[freeglut] / src / blackberry / fg_window_blackberry.c
1 /*
2  * fg_window_blackberry.c
3  *
4  * Window management methods for BlackBerry
5  *
6  * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7  * Written by Pawel W. Olszta, <olszta@sourceforge.net>
8  * Copied for Platform code by Evan Felix <karcaw at gmail.com>
9  * Copyright (C) 2012  Sylvain Beucler
10  * Copyright (C) 2013  Vincent Simonetti
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included
20  * in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
25  * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
26  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28  */
29
30 #define FREEGLUT_BUILDING_LIB
31 #include <GL/freeglut.h>
32 #include "fg_internal.h"
33 #include "egl/fg_window_egl.h"
34 #include <screen/screen.h>
35 #include <bps/screen.h>
36
37 /*
38  * Opens a window. Requires a SFG_Window object created and attached
39  * to the freeglut structure. OpenGL context is created here.
40  */
41 void fgPlatformOpenWindow( SFG_Window* window, const char* title,
42                            GLboolean positionUse, int x, int y,
43                            GLboolean sizeUse, int w, int h,
44                            GLboolean gameMode, GLboolean isSubWindow )
45 {
46     /* TODO: only one full-screen window possible? */
47     if (fgDisplay.pDisplay.single_native_window != NULL) {
48         fgWarning("You can't have more than one window on BlackBerry");
49         return;
50     }
51
52     /* Create window */
53     screen_window_t sWindow;
54     if (screen_create_window(&sWindow, fgDisplay.pDisplay.screenContext)) {
55         fgError("Could not create window");
56         return;
57     }
58     fgDisplay.pDisplay.single_native_window = sWindow;
59
60     /* Set window properties */
61     int orientation = atoi(getenv("ORIENTATION"));
62     int screenFormat = SCREEN_FORMAT_RGBA8888; //XXX Should this be determined by config?
63 #ifdef GL_ES_VERSION_2_0
64     int screenUsage = SCREEN_USAGE_OPENGL_ES2 | SCREEN_USAGE_ROTATION;
65 #elif GL_VERSION_ES_CM_1_0 || GL_VERSION_ES_CL_1_0 || GL_VERSION_ES_CM_1_1 || GL_VERSION_ES_CL_1_1
66     int screenUsage = SCREEN_USAGE_OPENGL_ES1 | SCREEN_USAGE_ROTATION;
67 #endif
68 #ifndef __X86__
69     screenUsage |= SCREEN_USAGE_DISPLAY; // Physical device copy directly into physical display
70 #endif
71     if (screen_set_window_property_iv(sWindow, SCREEN_PROPERTY_FORMAT, &screenFormat)) {
72         screen_destroy_window(sWindow);
73         fgError("Could not set window format");
74         return;
75     }
76     if (screen_set_window_property_iv(sWindow, SCREEN_PROPERTY_USAGE, &screenUsage)) {
77         screen_destroy_window(sWindow);
78         fgError("Could not set window usage");
79         return;
80     }
81
82     int value[2];
83     /* Uncomment when multiple windows are supported
84     if(positionUse) {
85         value[0] = x;
86         value[1] = y;
87         if (screen_set_window_property_iv(sWindow, SCREEN_PROPERTY_POSITION, value)) {
88             screen_destroy_window(sWindow);
89             fgError("Could not set window position");
90             return;
91         }
92     }*/
93
94     if(sizeUse) {
95         /* Uncomment when multiple windows are supported
96         value[0] = w;
97         value[1] = h;
98         */
99         //TEMP until ^^ is uncommented
100         if (screen_get_window_property_iv(sWindow, SCREEN_PROPERTY_BUFFER_SIZE, value)) {
101             screen_destroy_window(sWindow);
102             fgError("Could not get window mode");
103             return;
104         }
105     } else {
106         /* From PlatformBlackBerry in GamePlay3d */
107         screen_display_t display;
108         if (screen_get_window_property_pv(sWindow, SCREEN_PROPERTY_DISPLAY, (void**)&display)) {
109             screen_destroy_window(sWindow);
110             fgError("Could not get window display");
111             return;
112         }
113
114         screen_display_mode_t displayMode;
115         if (screen_get_display_property_pv(display, SCREEN_PROPERTY_MODE, (void**)&displayMode)) {
116             screen_destroy_window(sWindow);
117             fgError("Could not get display mode");
118             return;
119         }
120
121         if (screen_get_window_property_iv(sWindow, SCREEN_PROPERTY_BUFFER_SIZE, value)) {
122             screen_destroy_window(sWindow);
123             fgError("Could not get window mode");
124             return;
125         }
126
127         /* Adjust buffer sizes based on rotation */
128         if ((orientation == 0) || (orientation == 180))
129         {
130             if (((displayMode.width > displayMode.height) && (value[0] < value[1])) ||
131                 ((displayMode.width < displayMode.height) && (value[0] > value[1])))
132             {
133                 int tmp = value[1];
134                 value[1] = value[0];
135                 value[0] = tmp;
136             }
137         }
138         else if ((orientation == 90) || (orientation == 270))
139         {
140             if (((displayMode.width > displayMode.height) && (value[0] > value[1])) ||
141                 ((displayMode.width < displayMode.height) && (value[0] < value[1])))
142             {
143                 int tmp = value[1];
144                 value[1] = value[0];
145                 value[0] = tmp;
146             }
147         }
148         else
149         {
150             screen_destroy_window(sWindow);
151             fgError("Unexpected rotation angle");
152             return;
153         }
154     }
155
156     /* Set rotation if usage allows it */
157     if (screen_set_window_property_iv(sWindow, SCREEN_PROPERTY_ROTATION, &orientation)) {
158         screen_destroy_window(sWindow);
159         fgError("Could not set window rotation");
160         return;
161     }
162     window->State.pWState.originalRotation = orientation;
163
164     /* Set buffer sizes */
165     if (screen_set_window_property_iv(sWindow, SCREEN_PROPERTY_BUFFER_SIZE, value)) {
166         screen_destroy_window(sWindow);
167         fgError("Could not set window buffer size");
168         return;
169     }
170
171     /* Create window buffers */
172     if (screen_create_window_buffers(sWindow, (fgState.DisplayMode & GLUT_DOUBLE) ? 2 : 1)) {
173         screen_destroy_window(sWindow);
174         fgError("Could not create window buffers");
175         return;
176     }
177
178     /* Save window and set state */
179     window->Window.Handle = sWindow;
180     window->State.WorkMask |= GLUT_INIT_WORK;
181     window->State.IsFullscreen = GL_TRUE; //XXX Always fullscreen for now
182
183     /* Create context */
184     fghChooseConfig(&window->Window.pContext.egl.Config);
185     window->Window.Context = fghCreateNewContextEGL(window);
186
187     /* Create EGL window */
188     fghPlatformOpenWindowEGL(window);
189
190     window->State.Visible = GL_TRUE;
191 }
192
193 void fgPlatformFlushCommands()
194 {
195     if(screen_flush_context(fgDisplay.pDisplay.screenContext, 0)) {
196         fgWarning("Could not flush screen context");
197     }
198 }
199
200 void fgPlatformRotateWindow(SFG_Window* window, int rotation)
201 {
202     if(screen_set_window_property_iv(window->Window.Handle, SCREEN_PROPERTY_ROTATION, &rotation)) {
203         fgWarning("Could not set window rotation");
204     }
205 }
206
207 /*
208  * Request a window resize
209  */
210 void fgPlatformReshapeWindow ( SFG_Window *window, int width, int height )
211 {
212     fprintf(stderr, "fgPlatformReshapeWindow: STUB\n");
213 }
214
215 /*
216  * Closes a window, destroying the frame and OpenGL context
217  */
218 void fgPlatformCloseWindow( SFG_Window* window )
219 {
220     fghPlatformCloseWindowEGL(window);
221
222     screen_destroy_window((screen_window_t)window->Window.Handle);
223 }
224
225 /*
226  * This function makes the specified window visible
227  */
228 void fgPlatformShowWindow( void )
229 {
230     fprintf(stderr, "fgPlatformShowWindow: STUB\n");
231 }
232
233 /*
234  * This function hides the specified window
235  */
236 void fgPlatformHideWindow( SFG_Window *window )
237 {
238     fprintf(stderr, "fgPlatformHideWindow: STUB\n");
239 }
240
241 /*
242  * Iconify the specified window (top-level windows only)
243  */
244 void fgPlatformIconifyWindow( SFG_Window *window )
245 {
246     //XXX This is possible via Cascades, but can't seem to find a C-level API
247     //XXX bb::Application::instance()->minimize();
248     fprintf(stderr, "fgPlatformGlutIconifyWindow: STUB\n");
249 }
250
251 /*
252  * Set the current window's title
253  */
254 void fgPlatformGlutSetWindowTitle( const char* title )
255 {
256     fprintf(stderr, "fgPlatformGlutSetWindowTitle: STUB\n");
257 }
258
259 /*
260  * Set the current window's iconified title
261  */
262 void fgPlatformGlutSetIconTitle( const char* title )
263 {
264     fprintf(stderr, "fgPlatformGlutSetIconTitle: STUB\n");
265 }
266
267 /*
268  * Change the specified window's position
269  */
270 void fgPlatformPositionWindow( SFG_Window *window, int x, int y )
271 {
272     fprintf(stderr, "fgPlatformPositionWindow: STUB\n");
273 }
274
275 /*
276  * Lowers the specified window (by Z order change)
277  */
278 void fgPlatformPushWindow( SFG_Window *window )
279 {
280     fprintf(stderr, "fgPlatformPushWindow: STUB\n");
281 }
282
283 /*
284  * Raises the specified window (by Z order change)
285  */
286 void fgPlatformPopWindow( SFG_Window *window )
287 {
288     fprintf(stderr, "fgPlatformPopWindow: STUB\n");
289 }
290
291 /*
292  * Toggle the window's full screen state.
293  */
294 void fgPlatformFullScreenToggle( SFG_Window *win )
295 {
296     fprintf(stderr, "fgPlatformFullScreenToggle: STUB\n");
297 }