e88653ca0d213fa148b51ee3c4269c65fe2b571f
[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
36 /*
37  * Opens a window. Requires a SFG_Window object created and attached
38  * to the freeglut structure. OpenGL context is created here.
39  */
40 void fgPlatformOpenWindow( SFG_Window* window, const char* title,
41                            GLboolean positionUse, int x, int y,
42                            GLboolean sizeUse, int w, int h,
43                            GLboolean gameMode, GLboolean isSubWindow )
44 {
45     /* TODO: only one full-screen window possible? */
46     if (fgDisplay.pDisplay.single_native_window != NULL) {
47         fgWarning("You can't have more than one window on BlackBerry");
48         return;
49     }
50
51     /* Create window */
52     screen_window_t sWindow;
53     if (screen_create_window(&sWindow, fgDisplay.pDisplay.screenContext)) {
54         fgError("Could not create window");
55         return;
56     }
57     fgDisplay.pDisplay.single_native_window = sWindow;
58
59     /* Set window properties */
60     int orientation = atoi(getenv("ORIENTATION"));
61     int screenFormat = SCREEN_FORMAT_RGBA8888; //Only SCREEN_FORMAT_RGBA8888 and SCREEN_FORMAT_RGB565 are supported. See fg_window_egl for more info
62 #ifdef GL_ES_VERSION_2_0
63     int screenUsage = SCREEN_USAGE_OPENGL_ES2 | SCREEN_USAGE_ROTATION;
64 #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
65     int screenUsage = SCREEN_USAGE_OPENGL_ES1 | SCREEN_USAGE_ROTATION;
66 #endif
67 #if !defined(__X86__) && !defined(__PLAYBOOK__)
68     screenUsage |= SCREEN_USAGE_DISPLAY; // Physical device copy directly into physical display
69 #endif
70     if (screen_set_window_property_iv(sWindow, SCREEN_PROPERTY_FORMAT, &screenFormat)) {
71         screen_destroy_window(sWindow);
72         fgError("Could not set window format");
73         return;
74     }
75     if (screen_set_window_property_iv(sWindow, SCREEN_PROPERTY_USAGE, &screenUsage)) {
76         screen_destroy_window(sWindow);
77         fgError("Could not set window usage");
78         return;
79     }
80
81     int value[2];
82     /* Uncomment when multiple windows are supported
83     if(positionUse) {
84         value[0] = x;
85         value[1] = y;
86         if (screen_set_window_property_iv(sWindow, SCREEN_PROPERTY_POSITION, value)) {
87             screen_destroy_window(sWindow);
88             fgError("Could not set window position");
89             return;
90         }
91     }*/
92
93     if(sizeUse) {
94         /* Uncomment when multiple windows are supported
95         value[0] = w;
96         value[1] = h;
97         */
98         //TEMP until ^^ is uncommented
99         if (screen_get_window_property_iv(sWindow, SCREEN_PROPERTY_BUFFER_SIZE, value)) {
100             screen_destroy_window(sWindow);
101             fgError("Could not get window mode");
102             return;
103         }
104     } else {
105         /* From PlatformBlackBerry in GamePlay3d */
106         screen_display_t display;
107         if (screen_get_window_property_pv(sWindow, SCREEN_PROPERTY_DISPLAY, (void**)&display)) {
108             screen_destroy_window(sWindow);
109             fgError("Could not get window display");
110             return;
111         }
112
113         screen_display_mode_t displayMode;
114         if (screen_get_display_property_pv(display, SCREEN_PROPERTY_MODE, (void**)&displayMode)) {
115             screen_destroy_window(sWindow);
116             fgError("Could not get display mode");
117             return;
118         }
119
120         if (screen_get_window_property_iv(sWindow, SCREEN_PROPERTY_BUFFER_SIZE, value)) {
121             screen_destroy_window(sWindow);
122             fgError("Could not get window mode");
123             return;
124         }
125
126         /* Adjust buffer sizes based on rotation */
127         if ((orientation == 0) || (orientation == 180))
128         {
129             if (((displayMode.width > displayMode.height) && (value[0] < value[1])) ||
130                 ((displayMode.width < displayMode.height) && (value[0] > value[1])))
131             {
132                 int tmp = value[1];
133                 value[1] = value[0];
134                 value[0] = tmp;
135             }
136         }
137         else if ((orientation == 90) || (orientation == 270))
138         {
139             if (((displayMode.width > displayMode.height) && (value[0] > value[1])) ||
140                 ((displayMode.width < displayMode.height) && (value[0] < value[1])))
141             {
142                 int tmp = value[1];
143                 value[1] = value[0];
144                 value[0] = tmp;
145             }
146         }
147         else
148         {
149             screen_destroy_window(sWindow);
150             fgError("Unexpected rotation angle");
151             return;
152         }
153     }
154
155     /* Set rotation if usage allows it */
156     if (screen_set_window_property_iv(sWindow, SCREEN_PROPERTY_ROTATION, &orientation)) {
157         screen_destroy_window(sWindow);
158         fgError("Could not set window rotation");
159         return;
160     }
161     window->State.pWState.originalRotation = orientation;
162
163     /* Set buffer sizes */
164     if (screen_set_window_property_iv(sWindow, SCREEN_PROPERTY_BUFFER_SIZE, value)) {
165         screen_destroy_window(sWindow);
166         fgError("Could not set window buffer size");
167         return;
168     }
169
170     /* Create window buffers */
171     if (screen_create_window_buffers(sWindow, (fgState.DisplayMode & GLUT_DOUBLE) ? 2 : 1)) {
172         screen_destroy_window(sWindow);
173         fgError("Could not create window buffers");
174         return;
175     }
176
177     /* Save window and set state */
178     window->Window.Handle = sWindow;
179     window->State.WorkMask |= GLUT_INIT_WORK;
180     window->State.IsFullscreen = GL_TRUE; //XXX Always fullscreen for now
181
182     /* Create context */
183     fghChooseConfig(&window->Window.pContext.egl.Config);
184     window->Window.Context = EGL_NO_CONTEXT;
185     if( fgState.UseCurrentContext == GL_TRUE )
186         window->Window.Context = eglGetCurrentContext();
187     if( window->Window.Context == EGL_NO_CONTEXT )
188         window->Window.Context = fghCreateNewContextEGL(window);
189
190     /* Create EGL window */
191     fghPlatformOpenWindowEGL(window);
192
193     window->State.Visible = GL_TRUE;
194 }
195
196 void fgPlatformFlushCommands()
197 {
198     if(screen_flush_context(fgDisplay.pDisplay.screenContext, 0)) {
199         fgWarning("Could not flush screen context");
200     }
201 }
202
203 void fgPlatformRotateWindow(SFG_Window* window, int rotation)
204 {
205     if(screen_set_window_property_iv(window->Window.Handle, SCREEN_PROPERTY_ROTATION, &rotation)) {
206         fgWarning("Could not set window rotation");
207     }
208 }
209
210 /*
211  * Request a window resize
212  */
213 void fgPlatformReshapeWindow ( SFG_Window *window, int width, int height )
214 {
215     fprintf(stderr, "fgPlatformReshapeWindow: STUB\n");
216 }
217
218 /*
219  * Closes a window, destroying the frame and OpenGL context
220  */
221 void fgPlatformCloseWindow( SFG_Window* window )
222 {
223     fghPlatformCloseWindowEGL(window);
224
225     screen_destroy_window((screen_window_t)window->Window.Handle);
226 }
227
228 /*
229  * This function makes the specified window visible
230  */
231 void fgPlatformShowWindow( void )
232 {
233     fprintf(stderr, "fgPlatformShowWindow: STUB\n");
234 }
235
236 /*
237  * This function hides the specified window
238  */
239 void fgPlatformHideWindow( SFG_Window *window )
240 {
241     fprintf(stderr, "fgPlatformHideWindow: STUB\n");
242 }
243
244 /*
245  * Iconify the specified window (top-level windows only)
246  */
247 void fgPlatformIconifyWindow( SFG_Window *window )
248 {
249     //XXX This is possible via Cascades, but can't seem to find a C-level API
250     //XXX bb::Application::instance()->minimize();
251     fprintf(stderr, "fgPlatformGlutIconifyWindow: STUB\n");
252 }
253
254 /*
255  * Set the current window's title
256  */
257 void fgPlatformGlutSetWindowTitle( const char* title )
258 {
259     fprintf(stderr, "fgPlatformGlutSetWindowTitle: STUB\n");
260 }
261
262 /*
263  * Set the current window's iconified title
264  */
265 void fgPlatformGlutSetIconTitle( const char* title )
266 {
267     fprintf(stderr, "fgPlatformGlutSetIconTitle: STUB\n");
268 }
269
270 /*
271  * Change the specified window's position
272  */
273 void fgPlatformPositionWindow( SFG_Window *window, int x, int y )
274 {
275     fprintf(stderr, "fgPlatformPositionWindow: STUB\n");
276 }
277
278 /*
279  * Lowers the specified window (by Z order change)
280  */
281 void fgPlatformPushWindow( SFG_Window *window )
282 {
283     fprintf(stderr, "fgPlatformPushWindow: STUB\n");
284 }
285
286 /*
287  * Raises the specified window (by Z order change)
288  */
289 void fgPlatformPopWindow( SFG_Window *window )
290 {
291     fprintf(stderr, "fgPlatformPopWindow: STUB\n");
292 }
293
294 /*
295  * Toggle the window's full screen state.
296  */
297 void fgPlatformFullScreenToggle( SFG_Window *win )
298 {
299     fprintf(stderr, "fgPlatformFullScreenToggle: STUB\n");
300 }