8f7e6f29a14e80c3e40aedc430cfd9e11c7f0a18
[profile/ivi/clutter.git] / clutter / osx / clutter-backend-osx.c
1 /* Clutter -  An OpenGL based 'interactive canvas' library.
2  * OSX backend - initial entry point
3  *
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>
7  *
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.
12  *
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.
17  *
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/>.
20  *
21  *
22  */
23 #include "config.h"
24
25 #include "clutter-osx.h"
26
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"
32
33 #include "clutter-debug.h"
34 #include "clutter-private.h"
35 #include "clutter-stage-private.h"
36
37 #include "cogl/cogl.h"
38
39 #import <AppKit/AppKit.h>
40
41 #define DEFAULT_FONT_NAME       "Lucida Grande 13"
42
43 #define clutter_backend_osx_get_type    _clutter_backend_osx_get_type
44
45 G_DEFINE_TYPE (ClutterBackendOSX, clutter_backend_osx, CLUTTER_TYPE_BACKEND)
46
47 /*************************************************************************/
48 static gboolean
49 clutter_backend_osx_post_parse (ClutterBackend  *backend,
50                                 GError         **error)
51 {
52   ClutterSettings *settings = clutter_settings_get_default ();
53
54   CLUTTER_OSX_POOL_ALLOC();
55   /* getting standart dpi for main screen */
56   NSDictionary* prop = [[NSScreen mainScreen] deviceDescription];
57   NSSize size;
58   [[prop valueForKey:@"NSDeviceResolution"] getValue:&size];
59   CLUTTER_OSX_POOL_RELEASE();
60
61   /* setting dpi for backend, it needs by font rendering library */
62   if (size.height > 0)
63     {
64       int font_dpi = size.height * 1024;
65
66       g_object_set (settings, "font-dpi", font_dpi, NULL);
67     }
68
69   /* set the default font name */
70   g_object_set (settings, "font-name", DEFAULT_FONT_NAME, NULL);
71
72   /* finish launching the application */
73   [NSApp finishLaunching];
74
75   return TRUE;
76 }
77
78 static ClutterFeatureFlags
79 clutter_backend_osx_get_features (ClutterBackend *backend)
80 {
81   return CLUTTER_FEATURE_STAGE_MULTIPLE
82        | CLUTTER_FEATURE_STAGE_USER_RESIZE;
83 }
84
85 void
86 _clutter_backend_osx_events_init (ClutterBackend *backend)
87 {
88   ClutterBackendOSX *backend_osx = CLUTTER_BACKEND_OSX (backend);
89
90   if (backend_osx->device_manager != NULL)
91     return;
92
93   CLUTTER_NOTE (BACKEND, "init_events");
94
95   backend->device_manager = backend_osx->device_manager =
96     g_object_new (CLUTTER_TYPE_DEVICE_MANAGER_OSX,
97                   "backend", CLUTTER_BACKEND(backend_osx),
98                   NULL);
99
100   _clutter_osx_event_loop_init ();
101 }
102
103 static gboolean
104 clutter_backend_osx_create_context (ClutterBackend  *backend,
105                                     GError         **error)
106 {
107   ClutterBackendOSX *backend_osx = CLUTTER_BACKEND_OSX (backend);
108
109   CLUTTER_OSX_POOL_ALLOC();
110
111   if (backend_osx->context == nil)
112     {
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.
115        */
116       NSOpenGLPixelFormatAttribute attrs[] = {
117         NSOpenGLPFADoubleBuffer,
118         NSOpenGLPFADepthSize, 32,
119         NSOpenGLPFAStencilSize, 8,
120         0
121       };
122
123 #ifdef MAC_OS_X_VERSION_10_5
124       const int sw = 1;
125 #else
126       const long sw = 1;
127 #endif
128
129       backend_osx->pixel_format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
130
131       backend_osx->context = [[NSOpenGLContext alloc] initWithFormat: backend_osx->pixel_format
132                                                         shareContext: nil];
133
134       /* Enable vblank sync - http://developer.apple.com/qa/qa2007/qa1521.html */
135       [backend_osx->context setValues:&sw forParameter: NSOpenGLCPSwapInterval];
136
137       CLUTTER_NOTE (BACKEND, "Context was created");
138     }
139
140   [backend_osx->context makeCurrentContext];
141
142   CLUTTER_OSX_POOL_RELEASE();
143
144   return TRUE;
145 }
146
147 static void
148 clutter_backend_osx_ensure_context (ClutterBackend *backend,
149                                     ClutterStage   *wrapper)
150 {
151   ClutterBackendOSX *backend_osx = CLUTTER_BACKEND_OSX (backend);
152
153   CLUTTER_OSX_POOL_ALLOC();
154
155   CLUTTER_NOTE (BACKEND, "ensure_context: wrapper=%p", wrapper);
156
157   if (wrapper)
158     {
159       ClutterStageWindow *impl = _clutter_stage_get_window (wrapper);
160       ClutterStageOSX *stage_osx;
161
162       g_assert (CLUTTER_IS_STAGE_OSX (impl));
163       stage_osx = CLUTTER_STAGE_OSX (impl);
164
165       [backend_osx->context clearDrawable];
166       [backend_osx->context setView:stage_osx->view];
167       [backend_osx->context makeCurrentContext];
168     }
169   else
170     {
171       [backend_osx->context clearDrawable];
172       [NSOpenGLContext clearCurrentContext];
173     }
174
175   CLUTTER_OSX_POOL_RELEASE();
176 }
177
178 /*************************************************************************/
179
180 static void
181 clutter_backend_osx_init (ClutterBackendOSX *backend_osx)
182 {
183   const ProcessSerialNumber psn = { 0, kCurrentProcess };
184
185   backend_osx->context = nil;
186   backend_osx->pixel_format = nil;
187
188   /* Bring our app to foreground, background apps don't appear in dock or
189    * accept keyboard focus.
190    */
191   TransformProcessType (&psn, kProcessTransformToForegroundApplication);
192
193   /* Also raise our app to front, otherwise our window will remain under the
194    * terminal.
195    */
196   SetFrontProcess (&psn);
197
198   [NSApplication sharedApplication];
199 }
200
201 static void
202 clutter_backend_osx_dispose (GObject *object)
203 {
204   ClutterBackendOSX *self = CLUTTER_BACKEND_OSX (object);
205
206   [self->context release];
207   self->context = NULL;
208
209   [self->pixel_format release];
210   self->pixel_format = NULL;
211
212   G_OBJECT_CLASS (clutter_backend_osx_parent_class)->dispose (object);
213 }
214
215 static void
216 clutter_backend_osx_class_init (ClutterBackendOSXClass *klass)
217 {
218   GObjectClass *object_class = G_OBJECT_CLASS (klass);
219   ClutterBackendClass *backend_class = CLUTTER_BACKEND_CLASS (klass);
220
221   object_class->dispose = clutter_backend_osx_dispose;
222
223   backend_class->stage_window_type = CLUTTER_TYPE_STAGE_OSX;
224
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;
229 }