1 /* Clutter - An OpenGL based 'interactive canvas' library.
2 * OSX backend - initial entry point
4 * Copyright (C) 2007-2008 Tommi Komulainen <tommi.komulainen@iki.fi>
5 * Copyright (C) 2007 OpenedHand Ltd.
6 * Copyright (C) 2011 Crystalnix <vgachkaylo@crystalnix.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
25 #include "clutter-osx.h"
27 #include "clutter-backend-osx.h"
28 #include "clutter-device-manager-osx.h"
29 #include "clutter-shader.h"
30 #include "clutter-stage-osx.h"
31 #include "clutter-event-loop-osx.h"
33 #include "clutter-debug.h"
34 #include "clutter-private.h"
35 #include "clutter-stage-private.h"
37 #include "cogl/cogl.h"
39 #import <AppKit/AppKit.h>
41 #define DEFAULT_FONT_NAME "Lucida Grande 13"
43 #define clutter_backend_osx_get_type _clutter_backend_osx_get_type
45 G_DEFINE_TYPE (ClutterBackendOSX, clutter_backend_osx, CLUTTER_TYPE_BACKEND)
47 /*************************************************************************/
49 clutter_backend_osx_post_parse (ClutterBackend *backend,
52 ClutterSettings *settings = clutter_settings_get_default ();
54 CLUTTER_OSX_POOL_ALLOC();
55 /* getting standart dpi for main screen */
56 NSDictionary* prop = [[NSScreen mainScreen] deviceDescription];
58 [[prop valueForKey:@"NSDeviceResolution"] getValue:&size];
59 CLUTTER_OSX_POOL_RELEASE();
61 /* setting dpi for backend, it needs by font rendering library */
64 int font_dpi = size.height * 1024;
66 g_object_set (settings, "font-dpi", font_dpi, NULL);
69 /* set the default font name */
70 g_object_set (settings, "font-name", DEFAULT_FONT_NAME, NULL);
72 /* finish launching the application */
73 [NSApp finishLaunching];
78 static ClutterFeatureFlags
79 clutter_backend_osx_get_features (ClutterBackend *backend)
81 return CLUTTER_FEATURE_STAGE_MULTIPLE
82 | CLUTTER_FEATURE_STAGE_USER_RESIZE;
86 _clutter_backend_osx_events_init (ClutterBackend *backend)
88 ClutterBackendOSX *backend_osx = CLUTTER_BACKEND_OSX (backend);
90 if (backend_osx->device_manager != NULL)
93 CLUTTER_NOTE (BACKEND, "init_events");
95 backend->device_manager = backend_osx->device_manager =
96 g_object_new (CLUTTER_TYPE_DEVICE_MANAGER_OSX,
97 "backend", CLUTTER_BACKEND(backend_osx),
100 _clutter_osx_event_loop_init ();
104 clutter_backend_osx_create_context (ClutterBackend *backend,
107 ClutterBackendOSX *backend_osx = CLUTTER_BACKEND_OSX (backend);
109 CLUTTER_OSX_POOL_ALLOC();
111 if (backend_osx->context == nil)
113 /* Allocate ourselves a GL context. Since we're supposed to have
114 * only one per backend we can just as well create it now.
116 NSOpenGLPixelFormatAttribute attrs[] = {
117 NSOpenGLPFADoubleBuffer,
118 NSOpenGLPFADepthSize, 32,
119 NSOpenGLPFAStencilSize, 8,
123 #ifdef MAC_OS_X_VERSION_10_5
129 backend_osx->pixel_format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
131 backend_osx->context = [[NSOpenGLContext alloc] initWithFormat: backend_osx->pixel_format
134 /* Enable vblank sync - http://developer.apple.com/qa/qa2007/qa1521.html */
135 [backend_osx->context setValues:&sw forParameter: NSOpenGLCPSwapInterval];
137 CLUTTER_NOTE (BACKEND, "Context was created");
140 [backend_osx->context makeCurrentContext];
142 CLUTTER_OSX_POOL_RELEASE();
148 clutter_backend_osx_ensure_context (ClutterBackend *backend,
149 ClutterStage *wrapper)
151 ClutterBackendOSX *backend_osx = CLUTTER_BACKEND_OSX (backend);
153 CLUTTER_OSX_POOL_ALLOC();
155 CLUTTER_NOTE (BACKEND, "ensure_context: wrapper=%p", wrapper);
159 ClutterStageWindow *impl = _clutter_stage_get_window (wrapper);
160 ClutterStageOSX *stage_osx;
162 g_assert (CLUTTER_IS_STAGE_OSX (impl));
163 stage_osx = CLUTTER_STAGE_OSX (impl);
165 [backend_osx->context clearDrawable];
166 [backend_osx->context setView:stage_osx->view];
167 [backend_osx->context makeCurrentContext];
171 [backend_osx->context clearDrawable];
172 [NSOpenGLContext clearCurrentContext];
175 CLUTTER_OSX_POOL_RELEASE();
178 /*************************************************************************/
181 clutter_backend_osx_init (ClutterBackendOSX *backend_osx)
183 const ProcessSerialNumber psn = { 0, kCurrentProcess };
185 backend_osx->context = nil;
186 backend_osx->pixel_format = nil;
188 /* Bring our app to foreground, background apps don't appear in dock or
189 * accept keyboard focus.
191 TransformProcessType (&psn, kProcessTransformToForegroundApplication);
193 /* Also raise our app to front, otherwise our window will remain under the
196 SetFrontProcess (&psn);
198 [NSApplication sharedApplication];
202 clutter_backend_osx_dispose (GObject *object)
204 ClutterBackendOSX *self = CLUTTER_BACKEND_OSX (object);
206 [self->context release];
207 self->context = NULL;
209 [self->pixel_format release];
210 self->pixel_format = NULL;
212 G_OBJECT_CLASS (clutter_backend_osx_parent_class)->dispose (object);
216 clutter_backend_osx_class_init (ClutterBackendOSXClass *klass)
218 GObjectClass *object_class = G_OBJECT_CLASS (klass);
219 ClutterBackendClass *backend_class = CLUTTER_BACKEND_CLASS (klass);
221 object_class->dispose = clutter_backend_osx_dispose;
223 backend_class->stage_window_type = CLUTTER_TYPE_STAGE_OSX;
225 backend_class->post_parse = clutter_backend_osx_post_parse;
226 backend_class->get_features = clutter_backend_osx_get_features;
227 backend_class->create_context = clutter_backend_osx_create_context;
228 backend_class->ensure_context = clutter_backend_osx_ensure_context;