440425757c6e6d00ed2a3a38be13c3257c59e4d9
[blender.git] / intern / ghost / intern / GHOST_SystemCocoa.mm
1 /**
2  * $Id$
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s):      Maarten Gribnau 05/2001
25  *                                      Damien Plisson 09/2009
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #import <Cocoa/Cocoa.h>
31
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #include <sys/sysctl.h>
35
36 #include "GHOST_SystemCocoa.h"
37
38 #include "GHOST_DisplayManagerCocoa.h"
39 #include "GHOST_EventKey.h"
40 #include "GHOST_EventButton.h"
41 #include "GHOST_EventCursor.h"
42 #include "GHOST_EventWheel.h"
43 #include "GHOST_EventNDOF.h"
44 #include "GHOST_EventTrackpad.h"
45 #include "GHOST_EventDragnDrop.h"
46 #include "GHOST_EventString.h"
47
48 #include "GHOST_TimerManager.h"
49 #include "GHOST_TimerTask.h"
50 #include "GHOST_WindowManager.h"
51 #include "GHOST_WindowCocoa.h"
52 #include "GHOST_NDOFManager.h"
53 #include "AssertMacros.h"
54
55 #pragma mark KeyMap, mouse converters
56
57
58 /* Keycodes from Carbon include file */
59 /*  
60  *  Summary:
61  *    Virtual keycodes
62  *  
63  *  Discussion:
64  *    These constants are the virtual keycodes defined originally in
65  *    Inside Mac Volume V, pg. V-191. They identify physical keys on a
66  *    keyboard. Those constants with "ANSI" in the name are labeled
67  *    according to the key position on an ANSI-standard US keyboard.
68  *    For example, kVK_ANSI_A indicates the virtual keycode for the key
69  *    with the letter 'A' in the US keyboard layout. Other keyboard
70  *    layouts may have the 'A' key label on a different physical key;
71  *    in this case, pressing 'A' will generate a different virtual
72  *    keycode.
73  */
74 enum {
75         kVK_ANSI_A                    = 0x00,
76         kVK_ANSI_S                    = 0x01,
77         kVK_ANSI_D                    = 0x02,
78         kVK_ANSI_F                    = 0x03,
79         kVK_ANSI_H                    = 0x04,
80         kVK_ANSI_G                    = 0x05,
81         kVK_ANSI_Z                    = 0x06,
82         kVK_ANSI_X                    = 0x07,
83         kVK_ANSI_C                    = 0x08,
84         kVK_ANSI_V                    = 0x09,
85         kVK_ANSI_B                    = 0x0B,
86         kVK_ANSI_Q                    = 0x0C,
87         kVK_ANSI_W                    = 0x0D,
88         kVK_ANSI_E                    = 0x0E,
89         kVK_ANSI_R                    = 0x0F,
90         kVK_ANSI_Y                    = 0x10,
91         kVK_ANSI_T                    = 0x11,
92         kVK_ANSI_1                    = 0x12,
93         kVK_ANSI_2                    = 0x13,
94         kVK_ANSI_3                    = 0x14,
95         kVK_ANSI_4                    = 0x15,
96         kVK_ANSI_6                    = 0x16,
97         kVK_ANSI_5                    = 0x17,
98         kVK_ANSI_Equal                = 0x18,
99         kVK_ANSI_9                    = 0x19,
100         kVK_ANSI_7                    = 0x1A,
101         kVK_ANSI_Minus                = 0x1B,
102         kVK_ANSI_8                    = 0x1C,
103         kVK_ANSI_0                    = 0x1D,
104         kVK_ANSI_RightBracket         = 0x1E,
105         kVK_ANSI_O                    = 0x1F,
106         kVK_ANSI_U                    = 0x20,
107         kVK_ANSI_LeftBracket          = 0x21,
108         kVK_ANSI_I                    = 0x22,
109         kVK_ANSI_P                    = 0x23,
110         kVK_ANSI_L                    = 0x25,
111         kVK_ANSI_J                    = 0x26,
112         kVK_ANSI_Quote                = 0x27,
113         kVK_ANSI_K                    = 0x28,
114         kVK_ANSI_Semicolon            = 0x29,
115         kVK_ANSI_Backslash            = 0x2A,
116         kVK_ANSI_Comma                = 0x2B,
117         kVK_ANSI_Slash                = 0x2C,
118         kVK_ANSI_N                    = 0x2D,
119         kVK_ANSI_M                    = 0x2E,
120         kVK_ANSI_Period               = 0x2F,
121         kVK_ANSI_Grave                = 0x32,
122         kVK_ANSI_KeypadDecimal        = 0x41,
123         kVK_ANSI_KeypadMultiply       = 0x43,
124         kVK_ANSI_KeypadPlus           = 0x45,
125         kVK_ANSI_KeypadClear          = 0x47,
126         kVK_ANSI_KeypadDivide         = 0x4B,
127         kVK_ANSI_KeypadEnter          = 0x4C,
128         kVK_ANSI_KeypadMinus          = 0x4E,
129         kVK_ANSI_KeypadEquals         = 0x51,
130         kVK_ANSI_Keypad0              = 0x52,
131         kVK_ANSI_Keypad1              = 0x53,
132         kVK_ANSI_Keypad2              = 0x54,
133         kVK_ANSI_Keypad3              = 0x55,
134         kVK_ANSI_Keypad4              = 0x56,
135         kVK_ANSI_Keypad5              = 0x57,
136         kVK_ANSI_Keypad6              = 0x58,
137         kVK_ANSI_Keypad7              = 0x59,
138         kVK_ANSI_Keypad8              = 0x5B,
139         kVK_ANSI_Keypad9              = 0x5C
140 };
141
142 /* keycodes for keys that are independent of keyboard layout*/
143 enum {
144         kVK_Return                    = 0x24,
145         kVK_Tab                       = 0x30,
146         kVK_Space                     = 0x31,
147         kVK_Delete                    = 0x33,
148         kVK_Escape                    = 0x35,
149         kVK_Command                   = 0x37,
150         kVK_Shift                     = 0x38,
151         kVK_CapsLock                  = 0x39,
152         kVK_Option                    = 0x3A,
153         kVK_Control                   = 0x3B,
154         kVK_RightShift                = 0x3C,
155         kVK_RightOption               = 0x3D,
156         kVK_RightControl              = 0x3E,
157         kVK_Function                  = 0x3F,
158         kVK_F17                       = 0x40,
159         kVK_VolumeUp                  = 0x48,
160         kVK_VolumeDown                = 0x49,
161         kVK_Mute                      = 0x4A,
162         kVK_F18                       = 0x4F,
163         kVK_F19                       = 0x50,
164         kVK_F20                       = 0x5A,
165         kVK_F5                        = 0x60,
166         kVK_F6                        = 0x61,
167         kVK_F7                        = 0x62,
168         kVK_F3                        = 0x63,
169         kVK_F8                        = 0x64,
170         kVK_F9                        = 0x65,
171         kVK_F11                       = 0x67,
172         kVK_F13                       = 0x69,
173         kVK_F16                       = 0x6A,
174         kVK_F14                       = 0x6B,
175         kVK_F10                       = 0x6D,
176         kVK_F12                       = 0x6F,
177         kVK_F15                       = 0x71,
178         kVK_Help                      = 0x72,
179         kVK_Home                      = 0x73,
180         kVK_PageUp                    = 0x74,
181         kVK_ForwardDelete             = 0x75,
182         kVK_F4                        = 0x76,
183         kVK_End                       = 0x77,
184         kVK_F2                        = 0x78,
185         kVK_PageDown                  = 0x79,
186         kVK_F1                        = 0x7A,
187         kVK_LeftArrow                 = 0x7B,
188         kVK_RightArrow                = 0x7C,
189         kVK_DownArrow                 = 0x7D,
190         kVK_UpArrow                   = 0x7E
191 };
192
193 /* ISO keyboards only*/
194 enum {
195         kVK_ISO_Section               = 0x0A
196 };
197
198 /* JIS keyboards only*/
199 enum {
200         kVK_JIS_Yen                   = 0x5D,
201         kVK_JIS_Underscore            = 0x5E,
202         kVK_JIS_KeypadComma           = 0x5F,
203         kVK_JIS_Eisu                  = 0x66,
204         kVK_JIS_Kana                  = 0x68
205 };
206
207
208 static GHOST_TButtonMask convertButton(int button)
209 {
210         switch (button) {
211                 case 0:
212                         return GHOST_kButtonMaskLeft;
213                 case 1:
214                         return GHOST_kButtonMaskRight;
215                 case 2:
216                         return GHOST_kButtonMaskMiddle;
217                 case 3:
218                         return GHOST_kButtonMaskButton4;
219                 case 4:
220                         return GHOST_kButtonMaskButton5;
221                 default:
222                         return GHOST_kButtonMaskLeft;
223         }
224 }
225
226 /**
227  * Converts Mac rawkey codes (same for Cocoa & Carbon)
228  * into GHOST key codes
229  * @param rawCode The raw physical key code
230  * @param recvChar the character ignoring modifiers (except for shift)
231  * @return Ghost key code
232  */
233 static GHOST_TKey convertKey(int rawCode, unichar recvChar) 
234 {       
235         
236         //printf("\nrecvchar %c 0x%x",recvChar,recvChar);
237         switch (rawCode) {
238                 /*Physical keycodes not used due to map changes in int'l keyboards
239                 case kVK_ANSI_A:        return GHOST_kKeyA;
240                 case kVK_ANSI_B:        return GHOST_kKeyB;
241                 case kVK_ANSI_C:        return GHOST_kKeyC;
242                 case kVK_ANSI_D:        return GHOST_kKeyD;
243                 case kVK_ANSI_E:        return GHOST_kKeyE;
244                 case kVK_ANSI_F:        return GHOST_kKeyF;
245                 case kVK_ANSI_G:        return GHOST_kKeyG;
246                 case kVK_ANSI_H:        return GHOST_kKeyH;
247                 case kVK_ANSI_I:        return GHOST_kKeyI;
248                 case kVK_ANSI_J:        return GHOST_kKeyJ;
249                 case kVK_ANSI_K:        return GHOST_kKeyK;
250                 case kVK_ANSI_L:        return GHOST_kKeyL;
251                 case kVK_ANSI_M:        return GHOST_kKeyM;
252                 case kVK_ANSI_N:        return GHOST_kKeyN;
253                 case kVK_ANSI_O:        return GHOST_kKeyO;
254                 case kVK_ANSI_P:        return GHOST_kKeyP;
255                 case kVK_ANSI_Q:        return GHOST_kKeyQ;
256                 case kVK_ANSI_R:        return GHOST_kKeyR;
257                 case kVK_ANSI_S:        return GHOST_kKeyS;
258                 case kVK_ANSI_T:        return GHOST_kKeyT;
259                 case kVK_ANSI_U:        return GHOST_kKeyU;
260                 case kVK_ANSI_V:        return GHOST_kKeyV;
261                 case kVK_ANSI_W:        return GHOST_kKeyW;
262                 case kVK_ANSI_X:        return GHOST_kKeyX;
263                 case kVK_ANSI_Y:        return GHOST_kKeyY;
264                 case kVK_ANSI_Z:        return GHOST_kKeyZ;*/
265                 
266                 /* Numbers keys mapped to handle some int'l keyboard (e.g. French)*/
267                 case kVK_ISO_Section: return    GHOST_kKeyUnknown;
268                 case kVK_ANSI_1:        return GHOST_kKey1;
269                 case kVK_ANSI_2:        return GHOST_kKey2;
270                 case kVK_ANSI_3:        return GHOST_kKey3;
271                 case kVK_ANSI_4:        return GHOST_kKey4;
272                 case kVK_ANSI_5:        return GHOST_kKey5;
273                 case kVK_ANSI_6:        return GHOST_kKey6;
274                 case kVK_ANSI_7:        return GHOST_kKey7;
275                 case kVK_ANSI_8:        return GHOST_kKey8;
276                 case kVK_ANSI_9:        return GHOST_kKey9;
277                 case kVK_ANSI_0:        return GHOST_kKey0;
278         
279                 case kVK_ANSI_Keypad0:                  return GHOST_kKeyNumpad0;
280                 case kVK_ANSI_Keypad1:                  return GHOST_kKeyNumpad1;
281                 case kVK_ANSI_Keypad2:                  return GHOST_kKeyNumpad2;
282                 case kVK_ANSI_Keypad3:                  return GHOST_kKeyNumpad3;
283                 case kVK_ANSI_Keypad4:                  return GHOST_kKeyNumpad4;
284                 case kVK_ANSI_Keypad5:                  return GHOST_kKeyNumpad5;
285                 case kVK_ANSI_Keypad6:                  return GHOST_kKeyNumpad6;
286                 case kVK_ANSI_Keypad7:                  return GHOST_kKeyNumpad7;
287                 case kVK_ANSI_Keypad8:                  return GHOST_kKeyNumpad8;
288                 case kVK_ANSI_Keypad9:                  return GHOST_kKeyNumpad9;
289                 case kVK_ANSI_KeypadDecimal:    return GHOST_kKeyNumpadPeriod;
290                 case kVK_ANSI_KeypadEnter:              return GHOST_kKeyNumpadEnter;
291                 case kVK_ANSI_KeypadPlus:               return GHOST_kKeyNumpadPlus;
292                 case kVK_ANSI_KeypadMinus:              return GHOST_kKeyNumpadMinus;
293                 case kVK_ANSI_KeypadMultiply:   return GHOST_kKeyNumpadAsterisk;
294                 case kVK_ANSI_KeypadDivide:     return GHOST_kKeyNumpadSlash;
295                 case kVK_ANSI_KeypadClear:              return GHOST_kKeyUnknown;
296
297                 case kVK_F1:                            return GHOST_kKeyF1;
298                 case kVK_F2:                            return GHOST_kKeyF2;
299                 case kVK_F3:                            return GHOST_kKeyF3;
300                 case kVK_F4:                            return GHOST_kKeyF4;
301                 case kVK_F5:                            return GHOST_kKeyF5;
302                 case kVK_F6:                            return GHOST_kKeyF6;
303                 case kVK_F7:                            return GHOST_kKeyF7;
304                 case kVK_F8:                            return GHOST_kKeyF8;
305                 case kVK_F9:                            return GHOST_kKeyF9;
306                 case kVK_F10:                           return GHOST_kKeyF10;
307                 case kVK_F11:                           return GHOST_kKeyF11;
308                 case kVK_F12:                           return GHOST_kKeyF12;
309                 case kVK_F13:                           return GHOST_kKeyF13;
310                 case kVK_F14:                           return GHOST_kKeyF14;
311                 case kVK_F15:                           return GHOST_kKeyF15;
312                 case kVK_F16:                           return GHOST_kKeyF16;
313                 case kVK_F17:                           return GHOST_kKeyF17;
314                 case kVK_F18:                           return GHOST_kKeyF18;
315                 case kVK_F19:                           return GHOST_kKeyF19;
316                 case kVK_F20:                           return GHOST_kKeyF20;
317                         
318                 case kVK_UpArrow:                       return GHOST_kKeyUpArrow;
319                 case kVK_DownArrow:                     return GHOST_kKeyDownArrow;
320                 case kVK_LeftArrow:                     return GHOST_kKeyLeftArrow;
321                 case kVK_RightArrow:            return GHOST_kKeyRightArrow;
322                         
323                 case kVK_Return:                        return GHOST_kKeyEnter;
324                 case kVK_Delete:                        return GHOST_kKeyBackSpace;
325                 case kVK_ForwardDelete:         return GHOST_kKeyDelete;
326                 case kVK_Escape:                        return GHOST_kKeyEsc;
327                 case kVK_Tab:                           return GHOST_kKeyTab;
328                 case kVK_Space:                         return GHOST_kKeySpace;
329                         
330                 case kVK_Home:                          return GHOST_kKeyHome;
331                 case kVK_End:                           return GHOST_kKeyEnd;
332                 case kVK_PageUp:                        return GHOST_kKeyUpPage;
333                 case kVK_PageDown:                      return GHOST_kKeyDownPage;
334                         
335                 /*case kVK_ANSI_Minus:          return GHOST_kKeyMinus;
336                 case kVK_ANSI_Equal:            return GHOST_kKeyEqual;
337                 case kVK_ANSI_Comma:            return GHOST_kKeyComma;
338                 case kVK_ANSI_Period:           return GHOST_kKeyPeriod;
339                 case kVK_ANSI_Slash:            return GHOST_kKeySlash;
340                 case kVK_ANSI_Semicolon:        return GHOST_kKeySemicolon;
341                 case kVK_ANSI_Quote:            return GHOST_kKeyQuote;
342                 case kVK_ANSI_Backslash:        return GHOST_kKeyBackslash;
343                 case kVK_ANSI_LeftBracket:      return GHOST_kKeyLeftBracket;
344                 case kVK_ANSI_RightBracket:     return GHOST_kKeyRightBracket;
345                 case kVK_ANSI_Grave:            return GHOST_kKeyAccentGrave;*/
346                         
347                 case kVK_VolumeUp:
348                 case kVK_VolumeDown:
349                 case kVK_Mute:
350                         return GHOST_kKeyUnknown;
351                         
352                 default:
353                         /*Then detect on character value for "remappable" keys in int'l keyboards*/
354                         if ((recvChar >= 'A') && (recvChar <= 'Z')) {
355                                 return (GHOST_TKey) (recvChar - 'A' + GHOST_kKeyA);
356                         } else if ((recvChar >= 'a') && (recvChar <= 'z')) {
357                                 return (GHOST_TKey) (recvChar - 'a' + GHOST_kKeyA);
358                         } else
359                         switch (recvChar) {
360                                 case '-':       return GHOST_kKeyMinus;
361                                 case '=':       return GHOST_kKeyEqual;
362                                 case ',':       return GHOST_kKeyComma;
363                                 case '.':       return GHOST_kKeyPeriod;
364                                 case '/':       return GHOST_kKeySlash;
365                                 case ';':       return GHOST_kKeySemicolon;
366                                 case '\'':      return GHOST_kKeyQuote;
367                                 case '\\':      return GHOST_kKeyBackslash;
368                                 case '[':       return GHOST_kKeyLeftBracket;
369                                 case ']':       return GHOST_kKeyRightBracket;
370                                 case '`':       return GHOST_kKeyAccentGrave;
371                                 default:
372                                         return GHOST_kKeyUnknown;
373                         }
374         }
375         return GHOST_kKeyUnknown;
376 }
377
378
379 #pragma mark defines for 10.6 api not documented in 10.5
380 #ifndef MAC_OS_X_VERSION_10_6
381 enum {
382         /* The following event types are available on some hardware on 10.5.2 and later */
383         NSEventTypeGesture          = 29,
384         NSEventTypeMagnify          = 30,
385         NSEventTypeSwipe            = 31,
386         NSEventTypeRotate           = 18,
387         NSEventTypeBeginGesture     = 19,
388         NSEventTypeEndGesture       = 20
389 };
390
391 @interface NSEvent(GestureEvents)
392 /* This message is valid for events of type NSEventTypeMagnify, on 10.5.2 or later */
393 #if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4
394 - (float)magnification;       // change in magnification.
395 #else
396 - (CGFloat)magnification;       // change in magnification.
397 #endif
398 @end 
399
400 @interface NSEvent(SnowLeopardEvents)
401 /* modifier keys currently down.  This returns the state of devices combined
402  with synthesized events at the moment, independent of which events
403  have been delivered via the event stream. */
404 #if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4
405 + (unsigned int)modifierFlags; //NSUInteger is defined only from 10.5
406 #else
407 + (NSUInteger)modifierFlags;
408 #endif
409 @end
410
411 #endif
412
413
414 #pragma mark Utility functions
415
416 #define FIRSTFILEBUFLG 512
417 static bool g_hasFirstFile = false;
418 static char g_firstFileBuf[512];
419
420 //TODO:Need to investigate this. Function called too early in creator.c to have g_hasFirstFile == true
421 extern "C" int GHOST_HACK_getFirstFile(char buf[FIRSTFILEBUFLG]) { 
422         if (g_hasFirstFile) {
423                 strncpy(buf, g_firstFileBuf, FIRSTFILEBUFLG - 1);
424                 buf[FIRSTFILEBUFLG - 1] = '\0';
425                 return 1;
426         } else {
427                 return 0; 
428         }
429 }
430
431 #if defined(WITH_QUICKTIME) && !defined(USE_QTKIT)
432 //Need to place this quicktime function in an ObjC file
433 //It is used to avoid memory leak when raising the quicktime "compression settings" standard dialog
434 extern "C" {
435         struct bContext;
436         struct wmOperator;
437         extern int fromcocoa_request_qtcodec_settings(bContext *C, wmOperator *op);
438
439
440 int cocoa_request_qtcodec_settings(bContext *C, wmOperator *op)
441 {
442         int result;
443         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
444         
445         result = fromcocoa_request_qtcodec_settings(C, op);
446         
447         [pool drain];
448         return result;
449 }
450 };
451 #endif
452
453
454 #pragma mark Cocoa objects
455
456 /**
457  * CocoaAppDelegate
458  * ObjC object to capture applicationShouldTerminate, and send quit event
459  **/
460 @interface CocoaAppDelegate : NSObject {
461         GHOST_SystemCocoa *systemCocoa;
462 }
463 - (void)setSystemCocoa:(GHOST_SystemCocoa *)sysCocoa;
464 - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename;
465 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
466 - (void)applicationWillTerminate:(NSNotification *)aNotification;
467 - (void)applicationWillBecomeActive:(NSNotification *)aNotification;
468 @end
469
470 @implementation CocoaAppDelegate : NSObject
471 -(void)setSystemCocoa:(GHOST_SystemCocoa *)sysCocoa
472 {
473         systemCocoa = sysCocoa;
474 }
475
476 - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
477 {
478         return systemCocoa->handleOpenDocumentRequest(filename);
479 }
480
481 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
482 {
483         //TODO: implement graceful termination through Cocoa mechanism to avoid session log off to be cancelled
484         //Note that Cmd+Q is already handled by keyhandler
485     if (systemCocoa->handleQuitRequest() == GHOST_kExitNow)
486                 return NSTerminateCancel;//NSTerminateNow;
487         else
488                 return NSTerminateCancel;
489 }
490
491 // To avoid cancelling a log off process, we must use Cocoa termination process
492 // And this function is the only chance to perform clean up
493 // So WM_exit needs to be called directly, as the event loop will never run before termination
494 - (void)applicationWillTerminate:(NSNotification *)aNotification
495 {
496         /*G.afbreek = 0; //Let Cocoa perform the termination at the end
497         WM_exit(C);*/
498 }
499
500 - (void)applicationWillBecomeActive:(NSNotification *)aNotification
501 {
502         systemCocoa->handleApplicationBecomeActiveEvent();
503 }
504 @end
505
506
507
508 #pragma mark initialization/finalization
509
510
511 GHOST_SystemCocoa::GHOST_SystemCocoa()
512 {
513         int mib[2];
514         struct timeval boottime;
515         size_t len;
516         char *rstring = NULL;
517         
518         m_modifierMask =0;
519         m_pressedMouseButtons =0;
520         m_isGestureInProgress = false;
521         m_cursorDelta_x=0;
522         m_cursorDelta_y=0;
523         m_outsideLoopEventProcessed = false;
524         m_needDelayedApplicationBecomeActiveEventProcessing = false;
525         m_displayManager = new GHOST_DisplayManagerCocoa ();
526         GHOST_ASSERT(m_displayManager, "GHOST_SystemCocoa::GHOST_SystemCocoa(): m_displayManager==0\n");
527         m_displayManager->initialize();
528
529         //NSEvent timeStamp is given in system uptime, state start date is boot time
530         mib[0] = CTL_KERN;
531         mib[1] = KERN_BOOTTIME;
532         len = sizeof(struct timeval);
533         
534         sysctl(mib, 2, &boottime, &len, NULL, 0);
535         m_start_time = ((boottime.tv_sec*1000)+(boottime.tv_usec/1000));
536         
537         //Detect multitouch trackpad
538         mib[0] = CTL_HW;
539         mib[1] = HW_MODEL;
540         sysctl( mib, 2, NULL, &len, NULL, 0 );
541         rstring = (char*)malloc( len );
542         sysctl( mib, 2, rstring, &len, NULL, 0 );
543         
544         //Hack on MacBook revision, as multitouch avail. function missing
545         if (strstr(rstring,"MacBookAir") ||
546                 (strstr(rstring,"MacBook") && (rstring[strlen(rstring)-3]>='5') && (rstring[strlen(rstring)-3]<='9')))
547                 m_hasMultiTouchTrackpad = true;
548         else m_hasMultiTouchTrackpad = false;
549         
550         free( rstring );
551         rstring = NULL;
552         
553         m_ignoreWindowSizedMessages = false;
554 }
555
556 GHOST_SystemCocoa::~GHOST_SystemCocoa()
557 {
558 }
559
560
561 GHOST_TSuccess GHOST_SystemCocoa::init()
562 {
563         
564     GHOST_TSuccess success = GHOST_System::init();
565     if (success) {
566                 //ProcessSerialNumber psn;
567                 
568                 //Carbon stuff to move window & menu to foreground
569                 /*if (!GetCurrentProcess(&psn)) {
570                         TransformProcessType(&psn, kProcessTransformToForegroundApplication);
571                         SetFrontProcess(&psn);
572                 }*/
573                 
574                 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
575                 if (NSApp == nil) {
576                         [NSApplication sharedApplication];
577                         
578                         if ([NSApp mainMenu] == nil) {
579                                 NSMenu *mainMenubar = [[NSMenu alloc] init];
580                                 NSMenuItem *menuItem;
581                                 NSMenu *windowMenu;
582                                 NSMenu *appMenu;
583                                 
584                                 //Create the application menu
585                                 appMenu = [[NSMenu alloc] initWithTitle:@"Blender"];
586                                 
587                                 [appMenu addItemWithTitle:@"About Blender" action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
588                                 [appMenu addItem:[NSMenuItem separatorItem]];
589                                 
590                                 menuItem = [appMenu addItemWithTitle:@"Hide Blender" action:@selector(hide:) keyEquivalent:@"h"];
591                                 [menuItem setKeyEquivalentModifierMask:NSCommandKeyMask];
592                                  
593                                 menuItem = [appMenu addItemWithTitle:@"Hide others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
594                                 [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask | NSCommandKeyMask)];
595                                 
596                                 [appMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
597                                 
598                                 menuItem = [appMenu addItemWithTitle:@"Quit Blender" action:@selector(terminate:) keyEquivalent:@"q"];
599                                 [menuItem setKeyEquivalentModifierMask:NSCommandKeyMask];
600                                 
601                                 menuItem = [[NSMenuItem alloc] init];
602                                 [menuItem setSubmenu:appMenu];
603                                 
604                                 [mainMenubar addItem:menuItem];
605                                 [menuItem release];
606                                 [NSApp performSelector:@selector(setAppleMenu:) withObject:appMenu]; //Needed for 10.5
607                                 [appMenu release];
608                                 
609                                 //Create the window menu
610                                 windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
611                                 
612                                 menuItem = [windowMenu addItemWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
613                                 [menuItem setKeyEquivalentModifierMask:NSCommandKeyMask];
614                                 
615                                 [windowMenu addItemWithTitle:@"Zoom" action:@selector(performZoom:) keyEquivalent:@""];
616                                 
617                                 menuItem = [windowMenu addItemWithTitle:@"Close" action:@selector(performClose:) keyEquivalent:@"w"];
618                                 [menuItem setKeyEquivalentModifierMask:NSCommandKeyMask];
619                                 
620                                 menuItem = [[NSMenuItem alloc] init];
621                                 [menuItem setSubmenu:windowMenu];
622                                 
623                                 [mainMenubar addItem:menuItem];
624                                 [menuItem release];
625                                 
626                                 [NSApp setMainMenu:mainMenubar];
627                                 [NSApp setWindowsMenu:windowMenu];
628                                 [windowMenu release];
629                         }
630                 }
631                 if ([NSApp delegate] == nil) {
632                         CocoaAppDelegate *appDelegate = [[CocoaAppDelegate alloc] init];
633                         [appDelegate setSystemCocoa:this];
634                         [NSApp setDelegate:appDelegate];
635                 }
636                 
637                 [NSApp finishLaunching];
638                 
639                 [pool drain];
640     }
641     return success;
642 }
643
644
645 #pragma mark window management
646
647 GHOST_TUns64 GHOST_SystemCocoa::getMilliSeconds() const
648 {
649         //Cocoa equivalent exists in 10.6 ([[NSProcessInfo processInfo] systemUptime])
650         struct timeval currentTime;
651         
652         gettimeofday(&currentTime, NULL);
653         
654         //Return timestamp of system uptime
655         
656         return ((currentTime.tv_sec*1000)+(currentTime.tv_usec/1000)-m_start_time);
657 }
658
659
660 GHOST_TUns8 GHOST_SystemCocoa::getNumDisplays() const
661 {
662         //Note that OS X supports monitor hot plug
663         // We do not support multiple monitors at the moment
664         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
665
666         GHOST_TUns8 count = [[NSScreen screens] count];
667
668         [pool drain];
669         return count;
670 }
671
672
673 void GHOST_SystemCocoa::getMainDisplayDimensions(GHOST_TUns32& width, GHOST_TUns32& height) const
674 {
675         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
676         //Get visible frame, that is frame excluding dock and top menu bar
677         NSRect frame = [[NSScreen mainScreen] visibleFrame];
678         
679         //Returns max window contents (excluding title bar...)
680         NSRect contentRect = [NSWindow contentRectForFrameRect:frame
681                                                                                                  styleMask:(NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask)];
682         
683         width = contentRect.size.width;
684         height = contentRect.size.height;
685
686         [pool drain];
687 }
688
689
690 GHOST_IWindow* GHOST_SystemCocoa::createWindow(
691         const STR_String& title, 
692         GHOST_TInt32 left,
693         GHOST_TInt32 top,
694         GHOST_TUns32 width,
695         GHOST_TUns32 height,
696         GHOST_TWindowState state,
697         GHOST_TDrawingContextType type,
698         bool stereoVisual,
699         const GHOST_TUns16 numOfAASamples,
700         const GHOST_TEmbedderWindowID parentWindow
701 )
702 {
703     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
704         GHOST_IWindow* window = 0;
705         
706         //Get the available rect for including window contents
707         NSRect frame = [[NSScreen mainScreen] visibleFrame];
708         NSRect contentRect = [NSWindow contentRectForFrameRect:frame
709                                                                                                  styleMask:(NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask)];
710         
711         //Ensures window top left is inside this available rect
712         left = left > contentRect.origin.x ? left : contentRect.origin.x;
713         top = top > contentRect.origin.y ? top : contentRect.origin.y;
714         
715         window = new GHOST_WindowCocoa (this, title, left, top, width, height, state, type, stereoVisual, numOfAASamples);
716
717     if (window) {
718         if (window->getValid()) {
719             // Store the pointer to the window 
720             GHOST_ASSERT(m_windowManager, "m_windowManager not initialized");
721             m_windowManager->addWindow(window);
722             m_windowManager->setActiveWindow(window);
723                         //Need to tell window manager the new window is the active one (Cocoa does not send the event activate upon window creation)
724             pushEvent(new GHOST_Event(getMilliSeconds(), GHOST_kEventWindowActivate, window));
725                         pushEvent(new GHOST_Event(getMilliSeconds(), GHOST_kEventWindowSize, window));
726
727         }
728         else {
729                         GHOST_PRINT("GHOST_SystemCocoa::createWindow(): window invalid\n");
730             delete window;
731             window = 0;
732         }
733     }
734         else {
735                 GHOST_PRINT("GHOST_SystemCocoa::createWindow(): could not create window\n");
736         }
737         [pool drain];
738     return window;
739 }
740
741 GHOST_TSuccess GHOST_SystemCocoa::beginFullScreen(const GHOST_DisplaySetting& setting, GHOST_IWindow** window, const bool stereoVisual)
742 {       
743         GHOST_IWindow* currentWindow = m_windowManager->getActiveWindow();
744
745         *window = currentWindow;
746         
747         return currentWindow->setState(GHOST_kWindowStateFullScreen);
748 }
749
750 GHOST_TSuccess GHOST_SystemCocoa::endFullScreen(void)
751 {       
752         GHOST_IWindow* currentWindow = m_windowManager->getActiveWindow();
753         
754         return currentWindow->setState(GHOST_kWindowStateNormal);
755 }
756
757
758         
759 /**
760  * @note : returns coordinates in Cocoa screen coordinates
761  */
762 GHOST_TSuccess GHOST_SystemCocoa::getCursorPosition(GHOST_TInt32& x, GHOST_TInt32& y) const
763 {
764     NSPoint mouseLoc = [NSEvent mouseLocation];
765         
766     // Returns the mouse location in screen coordinates
767     x = (GHOST_TInt32)mouseLoc.x;
768     y = (GHOST_TInt32)mouseLoc.y;
769     return GHOST_kSuccess;
770 }
771
772 /**
773  * @note : expect Cocoa screen coordinates
774  */
775 GHOST_TSuccess GHOST_SystemCocoa::setCursorPosition(GHOST_TInt32 x, GHOST_TInt32 y) const
776 {
777         float xf=(float)x, yf=(float)y;
778         GHOST_WindowCocoa* window = (GHOST_WindowCocoa*)m_windowManager->getActiveWindow();
779         NSScreen *windowScreen = window->getScreen();
780         NSRect screenRect = [windowScreen frame];
781         
782         //Set position relative to current screen
783         xf -= screenRect.origin.x;
784         yf -= screenRect.origin.y;
785         
786         //Quartz Display Services uses the old coordinates (top left origin)
787         yf = screenRect.size.height -yf;
788
789         CGDisplayMoveCursorToPoint((CGDirectDisplayID)[[[windowScreen deviceDescription] objectForKey:@"NSScreenNumber"] unsignedIntValue], CGPointMake(xf, yf));
790
791     return GHOST_kSuccess;
792 }
793
794
795 GHOST_TSuccess GHOST_SystemCocoa::getModifierKeys(GHOST_ModifierKeys& keys) const
796 {
797         keys.set(GHOST_kModifierKeyCommand, (m_modifierMask & NSCommandKeyMask) ? true : false);
798         keys.set(GHOST_kModifierKeyLeftAlt, (m_modifierMask & NSAlternateKeyMask) ? true : false);
799         keys.set(GHOST_kModifierKeyLeftShift, (m_modifierMask & NSShiftKeyMask) ? true : false);
800         keys.set(GHOST_kModifierKeyLeftControl, (m_modifierMask & NSControlKeyMask) ? true : false);
801         
802     return GHOST_kSuccess;
803 }
804
805 GHOST_TSuccess GHOST_SystemCocoa::getButtons(GHOST_Buttons& buttons) const
806 {
807         buttons.clear();
808     buttons.set(GHOST_kButtonMaskLeft, m_pressedMouseButtons & GHOST_kButtonMaskLeft);
809         buttons.set(GHOST_kButtonMaskRight, m_pressedMouseButtons & GHOST_kButtonMaskRight);
810         buttons.set(GHOST_kButtonMaskMiddle, m_pressedMouseButtons & GHOST_kButtonMaskMiddle);
811         buttons.set(GHOST_kButtonMaskButton4, m_pressedMouseButtons & GHOST_kButtonMaskButton4);
812         buttons.set(GHOST_kButtonMaskButton5, m_pressedMouseButtons & GHOST_kButtonMaskButton5);
813     return GHOST_kSuccess;
814 }
815
816
817
818 #pragma mark Event handlers
819
820 /**
821  * The event queue polling function
822  */
823 bool GHOST_SystemCocoa::processEvents(bool waitForEvent)
824 {
825         bool anyProcessed = false;
826         NSEvent *event;
827         
828         //      SetMouseCoalescingEnabled(false, NULL);
829         //TODO : implement timer ??
830         
831         /*do {
832                 GHOST_TimerManager* timerMgr = getTimerManager();
833                 
834                  if (waitForEvent) {
835                  GHOST_TUns64 next = timerMgr->nextFireTime();
836                  double timeOut;
837                  
838                  if (next == GHOST_kFireTimeNever) {
839                  timeOut = kEventDurationForever;
840                  } else {
841                  timeOut = (double)(next - getMilliSeconds())/1000.0;
842                  if (timeOut < 0.0)
843                  timeOut = 0.0;
844                  }
845                  
846                  ::ReceiveNextEvent(0, NULL, timeOut, false, &event);
847                  }
848                  
849                  if (timerMgr->fireTimers(getMilliSeconds())) {
850                  anyProcessed = true;
851                  }*/
852                 
853                 do {
854                         NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
855                         event = [NSApp nextEventMatchingMask:NSAnyEventMask
856                                                                            untilDate:[NSDate distantPast]
857                                                                                   inMode:NSDefaultRunLoopMode
858                                                                                  dequeue:YES];
859                         if (event==nil) {
860                                 [pool drain];
861                                 break;
862                         }
863                         
864                         anyProcessed = true;
865                         
866                         switch ([event type]) {
867                                 case NSKeyDown:
868                                 case NSKeyUp:
869                                 case NSFlagsChanged:
870                                         handleKeyEvent(event);
871                                         
872                                         /* Support system-wide keyboard shortcuts, like Expos√©, ...) =>included in always NSApp sendEvent */
873                                         /*              if (([event modifierFlags] & NSCommandKeyMask) || [event type] == NSFlagsChanged) {
874                                          [NSApp sendEvent:event];
875                                          }*/
876                                         break;
877                                         
878                                 case NSLeftMouseDown:
879                                 case NSLeftMouseUp:
880                                 case NSRightMouseDown:
881                                 case NSRightMouseUp:
882                                 case NSMouseMoved:
883                                 case NSLeftMouseDragged:
884                                 case NSRightMouseDragged:
885                                 case NSScrollWheel:
886                                 case NSOtherMouseDown:
887                                 case NSOtherMouseUp:
888                                 case NSOtherMouseDragged:
889                                 case NSEventTypeMagnify:
890                                 case NSEventTypeRotate:
891                                 case NSEventTypeBeginGesture:
892                                 case NSEventTypeEndGesture:
893                                         handleMouseEvent(event);
894                                         break;
895                                         
896                                 case NSTabletPoint:
897                                 case NSTabletProximity:
898                                         handleTabletEvent(event,[event type]);
899                                         break;
900                                         
901                                         /* Trackpad features, fired only from OS X 10.5.2
902                                          case NSEventTypeGesture:
903                                          case NSEventTypeSwipe:
904                                          break; */
905                                         
906                                         /*Unused events
907                                          NSMouseEntered       = 8,
908                                          NSMouseExited        = 9,
909                                          NSAppKitDefined      = 13,
910                                          NSSystemDefined      = 14,
911                                          NSApplicationDefined = 15,
912                                          NSPeriodic           = 16,
913                                          NSCursorUpdate       = 17,*/
914                                         
915                                 default:
916                                         break;
917                         }
918                         //Resend event to NSApp to ensure Mac wide events are handled
919                         [NSApp sendEvent:event];
920                         [pool drain];
921                 } while (event!= nil);          
922         //} while (waitForEvent && !anyProcessed); Needed only for timer implementation
923         
924         if (m_needDelayedApplicationBecomeActiveEventProcessing) handleApplicationBecomeActiveEvent();
925         
926         if (m_outsideLoopEventProcessed) {
927                 m_outsideLoopEventProcessed = false;
928                 return true;
929         }
930         
931     return anyProcessed;
932 }
933
934 //Note: called from NSApplication delegate
935 GHOST_TSuccess GHOST_SystemCocoa::handleApplicationBecomeActiveEvent()
936 {
937         //Update the modifiers key mask, as its status may have changed when the application was not active
938         //(that is when update events are sent to another application)
939         unsigned int modifiers;
940         GHOST_IWindow* window = m_windowManager->getActiveWindow();
941         
942         if (!window) {
943                 m_needDelayedApplicationBecomeActiveEventProcessing = true;
944                 return GHOST_kFailure;
945         }
946         else m_needDelayedApplicationBecomeActiveEventProcessing = false;
947
948 #ifdef MAC_OS_X_VERSION_10_6
949         modifiers = [NSEvent modifierFlags];
950 #else
951         //If build against an older SDK, check if running on 10.6 to use the correct function
952         if ([NSEvent respondsToSelector:@selector(modifierFlags)]) {
953                 modifiers = [NSEvent modifierFlags];
954         }
955         else {
956                 //TODO: need to find a better workaround for the missing cocoa "getModifierFlag" function in 10.4/10.5
957                 modifiers = 0;
958         }
959 #endif
960         
961         /* Discard erroneous 10.6 modifiers values reported when switching back from spaces */
962         if ((modifiers & NSDeviceIndependentModifierFlagsMask) == 0xb00000) modifiers = 0;
963         
964         if ((modifiers & NSShiftKeyMask) != (m_modifierMask & NSShiftKeyMask)) {
965                 pushEvent( new GHOST_EventKey(getMilliSeconds(), (modifiers & NSShiftKeyMask)?GHOST_kEventKeyDown:GHOST_kEventKeyUp, window, GHOST_kKeyLeftShift) );
966         }
967         if ((modifiers & NSControlKeyMask) != (m_modifierMask & NSControlKeyMask)) {
968                 pushEvent( new GHOST_EventKey(getMilliSeconds(), (modifiers & NSControlKeyMask)?GHOST_kEventKeyDown:GHOST_kEventKeyUp, window, GHOST_kKeyLeftControl) );
969         }
970         if ((modifiers & NSAlternateKeyMask) != (m_modifierMask & NSAlternateKeyMask)) {
971                 pushEvent( new GHOST_EventKey(getMilliSeconds(), (modifiers & NSAlternateKeyMask)?GHOST_kEventKeyDown:GHOST_kEventKeyUp, window, GHOST_kKeyLeftAlt) );
972         }
973         if ((modifiers & NSCommandKeyMask) != (m_modifierMask & NSCommandKeyMask)) {
974                 pushEvent( new GHOST_EventKey(getMilliSeconds(), (modifiers & NSCommandKeyMask)?GHOST_kEventKeyDown:GHOST_kEventKeyUp, window, GHOST_kKeyCommand) );
975         }
976         
977         m_modifierMask = modifiers;
978         
979         m_outsideLoopEventProcessed = true;
980         return GHOST_kSuccess;
981 }
982
983 //Note: called from NSWindow delegate
984 GHOST_TSuccess GHOST_SystemCocoa::handleWindowEvent(GHOST_TEventType eventType, GHOST_WindowCocoa* window)
985 {
986         if (!validWindow(window)) {
987                 return GHOST_kFailure;
988         }
989                 switch(eventType) 
990                 {
991                         case GHOST_kEventWindowClose:
992                                 pushEvent( new GHOST_Event(getMilliSeconds(), GHOST_kEventWindowClose, window) );
993                                 break;
994                         case GHOST_kEventWindowActivate:
995                                 m_windowManager->setActiveWindow(window);
996                                 window->loadCursor(window->getCursorVisibility(), window->getCursorShape());
997                                 pushEvent( new GHOST_Event(getMilliSeconds(), GHOST_kEventWindowActivate, window) );
998                                 break;
999                         case GHOST_kEventWindowDeactivate:
1000                                 m_windowManager->setWindowInactive(window);
1001                                 pushEvent( new GHOST_Event(getMilliSeconds(), GHOST_kEventWindowDeactivate, window) );
1002                                 break;
1003                         case GHOST_kEventWindowUpdate:
1004                                 pushEvent( new GHOST_Event(getMilliSeconds(), GHOST_kEventWindowUpdate, window) );
1005                                 break;
1006                         case GHOST_kEventWindowMove:
1007                                 pushEvent( new GHOST_Event(getMilliSeconds(), GHOST_kEventWindowMove, window) );
1008                                 break;
1009                         case GHOST_kEventWindowSize:
1010                                 if (!m_ignoreWindowSizedMessages)
1011                                 {
1012                                         window->updateDrawingContext();
1013                                         pushEvent( new GHOST_Event(getMilliSeconds(), GHOST_kEventWindowSize, window) );
1014                                 }
1015                                 break;
1016                         default:
1017                                 return GHOST_kFailure;
1018                                 break;
1019                 }
1020         
1021         m_outsideLoopEventProcessed = true;
1022         return GHOST_kSuccess;
1023 }
1024
1025 //Note: called from NSWindow subclass
1026 GHOST_TSuccess GHOST_SystemCocoa::handleDraggingEvent(GHOST_TEventType eventType, GHOST_TDragnDropTypes draggedObjectType,
1027                                                                    GHOST_WindowCocoa* window, int mouseX, int mouseY, void* data)
1028 {
1029         if (!validWindow(window)) {
1030                 return GHOST_kFailure;
1031         }
1032         switch(eventType) 
1033         {
1034                 case GHOST_kEventDraggingEntered:
1035                 case GHOST_kEventDraggingUpdated:
1036                 case GHOST_kEventDraggingExited:
1037                         pushEvent(new GHOST_EventDragnDrop(getMilliSeconds(),eventType,draggedObjectType,window,mouseX,mouseY,NULL));
1038                         break;
1039                         
1040                 case GHOST_kEventDraggingDropDone:
1041                 {
1042                         GHOST_TUns8 * temp_buff;
1043                         GHOST_TStringArray *strArray;
1044                         NSArray *droppedArray;
1045                         size_t pastedTextSize;  
1046                         NSString *droppedStr;
1047                         GHOST_TEventDataPtr eventData;
1048                         int i;
1049
1050                         if (!data) return GHOST_kFailure;
1051                         
1052                         switch (draggedObjectType) {
1053                                 case GHOST_kDragnDropTypeBitmap:
1054                                         //TODO: implement bitmap conversion to a blender friendly format
1055                                         return GHOST_kFailure;
1056                                         break;
1057                                 case GHOST_kDragnDropTypeFilenames:
1058                                         droppedArray = (NSArray*)data;
1059                                         
1060                                         strArray = (GHOST_TStringArray*)malloc(sizeof(GHOST_TStringArray));
1061                                         if (!strArray) return GHOST_kFailure;
1062                                         
1063                                         strArray->count = [droppedArray count];
1064                                         if (strArray->count == 0) return GHOST_kFailure;
1065                                         
1066                                         strArray->strings = (GHOST_TUns8**) malloc(strArray->count*sizeof(GHOST_TUns8*));
1067                                         
1068                                         for (i=0;i<strArray->count;i++)
1069                                         {
1070                                                 droppedStr = [droppedArray objectAtIndex:i];
1071                                                 
1072                                                 pastedTextSize = [droppedStr lengthOfBytesUsingEncoding:NSISOLatin1StringEncoding];
1073                                                 temp_buff = (GHOST_TUns8*) malloc(pastedTextSize+1); 
1074                                         
1075                                                 if (!temp_buff) {
1076                                                         strArray->count = i;
1077                                                         break;
1078                                                 }
1079                                         
1080                                                 strncpy((char*)temp_buff, [droppedStr cStringUsingEncoding:NSISOLatin1StringEncoding], pastedTextSize);
1081                                                 temp_buff[pastedTextSize] = '\0';
1082                                                 
1083                                                 strArray->strings[i] = temp_buff;
1084                                         }
1085
1086                                         eventData = (GHOST_TEventDataPtr) strArray;     
1087                                         break;
1088                                         
1089                                 case GHOST_kDragnDropTypeString:
1090                                         droppedStr = (NSString*)data;
1091                                         pastedTextSize = [droppedStr lengthOfBytesUsingEncoding:NSISOLatin1StringEncoding];
1092                                         
1093                                         temp_buff = (GHOST_TUns8*) malloc(pastedTextSize+1); 
1094                                         
1095                                         if (temp_buff == NULL) {
1096                                                 return GHOST_kFailure;
1097                                         }
1098                                         
1099                                         strncpy((char*)temp_buff, [droppedStr cStringUsingEncoding:NSISOLatin1StringEncoding], pastedTextSize);
1100                                         
1101                                         temp_buff[pastedTextSize] = '\0';
1102                                         
1103                                         eventData = (GHOST_TEventDataPtr) temp_buff;
1104                                         break;
1105                                         
1106                                 default:
1107                                         return GHOST_kFailure;
1108                                         break;
1109                         }
1110                         pushEvent(new GHOST_EventDragnDrop(getMilliSeconds(),eventType,draggedObjectType,window,mouseX,mouseY,eventData));
1111                 }
1112                         break;
1113                 default:
1114                         return GHOST_kFailure;
1115         }
1116         m_outsideLoopEventProcessed = true;
1117         return GHOST_kSuccess;
1118 }
1119
1120
1121 GHOST_TUns8 GHOST_SystemCocoa::handleQuitRequest()
1122 {
1123         GHOST_Window* window = (GHOST_Window*)m_windowManager->getActiveWindow();
1124         
1125         //Discard quit event if we are in cursor grab sequence
1126         if (window && (window->getCursorGrabMode() != GHOST_kGrabDisable) && (window->getCursorGrabMode() != GHOST_kGrabNormal))
1127                 return GHOST_kExitCancel;
1128         
1129         //Check open windows if some changes are not saved
1130         if (m_windowManager->getAnyModifiedState())
1131         {
1132                 int shouldQuit = NSRunAlertPanel(@"Exit Blender", @"Some changes have not been saved.\nDo you really want to quit ?",
1133                                                                                  @"Cancel", @"Quit Anyway", nil);
1134                 if (shouldQuit == NSAlertAlternateReturn)
1135                 {
1136                         pushEvent( new GHOST_Event(getMilliSeconds(), GHOST_kEventQuit, NULL) );
1137                         return GHOST_kExitNow;
1138                 } else {
1139                         //Give back focus to the blender window if user selected cancel quit
1140                         NSArray *windowsList = [NSApp orderedWindows];
1141                         if ([windowsList count]) {
1142                                 [[windowsList objectAtIndex:0] makeKeyAndOrderFront:nil];
1143                         }
1144                 }
1145
1146         }
1147         else {
1148                 pushEvent( new GHOST_Event(getMilliSeconds(), GHOST_kEventQuit, NULL) );
1149                 m_outsideLoopEventProcessed = true;
1150                 return GHOST_kExitNow;
1151         }
1152         
1153         return GHOST_kExitCancel;
1154 }
1155
1156 bool GHOST_SystemCocoa::handleOpenDocumentRequest(void *filepathStr)
1157 {
1158         NSString *filepath = (NSString*)filepathStr;
1159         int confirmOpen = NSAlertAlternateReturn;
1160         NSArray *windowsList;
1161         char * temp_buff;
1162         size_t filenameTextSize;        
1163         GHOST_Window* window= (GHOST_Window*)m_windowManager->getActiveWindow();
1164         
1165         if (!window) {
1166                 return NO;
1167         }       
1168         
1169         //Discard event if we are in cursor grab sequence, it'll lead to "stuck cursor" situation if the alert panel is raised
1170         if (window && (window->getCursorGrabMode() != GHOST_kGrabDisable) && (window->getCursorGrabMode() != GHOST_kGrabNormal))
1171                 return GHOST_kExitCancel;
1172
1173         //Check open windows if some changes are not saved
1174         if (m_windowManager->getAnyModifiedState())
1175         {
1176                 confirmOpen = NSRunAlertPanel([NSString stringWithFormat:@"Opening %@",[filepath lastPathComponent]],
1177                                                                                  @"Current document has not been saved.\nDo you really want to proceed?",
1178                                                                                  @"Cancel", @"Open", nil);
1179         }
1180
1181         //Give back focus to the blender window
1182         windowsList = [NSApp orderedWindows];
1183         if ([windowsList count]) {
1184                 [[windowsList objectAtIndex:0] makeKeyAndOrderFront:nil];
1185         }
1186
1187         if (confirmOpen == NSAlertAlternateReturn)
1188         {
1189                 filenameTextSize = [filepath lengthOfBytesUsingEncoding:NSISOLatin1StringEncoding];
1190                 
1191                 temp_buff = (char*) malloc(filenameTextSize+1); 
1192                 
1193                 if (temp_buff == NULL) {
1194                         return GHOST_kFailure;
1195                 }
1196                 
1197                 strncpy(temp_buff, [filepath cStringUsingEncoding:NSISOLatin1StringEncoding], filenameTextSize);
1198                 
1199                 temp_buff[filenameTextSize] = '\0';
1200
1201                 pushEvent(new GHOST_EventString(getMilliSeconds(),GHOST_kEventOpenMainFile,window,(GHOST_TEventDataPtr) temp_buff));
1202
1203                 return YES;
1204         }
1205         else return NO;
1206 }
1207
1208 GHOST_TSuccess GHOST_SystemCocoa::handleTabletEvent(void *eventPtr, short eventType)
1209 {
1210         NSEvent *event = (NSEvent *)eventPtr;
1211         GHOST_IWindow* window = m_windowManager->getActiveWindow();
1212         
1213         if (!window) {
1214                 /* If no active window found, still tries to find the window associated with the event
1215                  This may happen when Cocoa continues to send some events after the window deactivate one */
1216                 window = m_windowManager->getWindowAssociatedWithOSWindow((void*)[event window]);
1217                 if (!window) {
1218                         //printf("\nW failure for event 0x%x",[event type]);
1219                         return GHOST_kFailure;
1220                 }
1221         }
1222         
1223         GHOST_TabletData& ct=((GHOST_WindowCocoa*)window)->GetCocoaTabletData();
1224         
1225         switch (eventType) {
1226                 case NSTabletPoint:
1227                         ct.Pressure = [event pressure];
1228                         ct.Xtilt = [event tilt].x;
1229                         ct.Ytilt = [event tilt].y;
1230                         break;
1231                 
1232                 case NSTabletProximity:
1233                         ct.Pressure = 0;
1234                         ct.Xtilt = 0;
1235                         ct.Ytilt = 0;
1236                         if ([event isEnteringProximity])
1237                         {
1238                                 //pointer is entering tablet area proximity
1239                                 switch ([event pointingDeviceType]) {
1240                                         case NSPenPointingDevice:
1241                                                 ct.Active = GHOST_kTabletModeStylus;
1242                                                 break;
1243                                         case NSEraserPointingDevice:
1244                                                 ct.Active = GHOST_kTabletModeEraser;
1245                                                 break;
1246                                         case NSCursorPointingDevice:
1247                                         case NSUnknownPointingDevice:
1248                                         default:
1249                                                 ct.Active = GHOST_kTabletModeNone;
1250                                                 break;
1251                                 }
1252                         } else {
1253                                 // pointer is leaving - return to mouse
1254                                 ct.Active = GHOST_kTabletModeNone;
1255                         }
1256                         break;
1257                 
1258                 default:
1259                         GHOST_ASSERT(FALSE,"GHOST_SystemCocoa::handleTabletEvent : unknown event received");
1260                         return GHOST_kFailure;
1261                         break;
1262         }
1263         return GHOST_kSuccess;
1264 }
1265
1266
1267 GHOST_TSuccess GHOST_SystemCocoa::handleMouseEvent(void *eventPtr)
1268 {
1269         NSEvent *event = (NSEvent *)eventPtr;
1270     GHOST_Window* window = (GHOST_Window*)m_windowManager->getActiveWindow();
1271         
1272         if (!window) {
1273                 /* If no active window found, still tries to find the window associated with the event
1274                  This may happen when Cocoa continues to send some events after the window deactivate one */
1275                 window = (GHOST_Window*)m_windowManager->getWindowAssociatedWithOSWindow((void*)[event window]);
1276                 if (!window) {
1277                         //printf("\nW failure for event 0x%x",[event type]);
1278                         return GHOST_kFailure;
1279                 }
1280         }
1281         
1282         switch ([event type])
1283     {
1284                 case NSLeftMouseDown:
1285                 case NSRightMouseDown:
1286                 case NSOtherMouseDown:
1287                         pushEvent(new GHOST_EventButton([event timestamp]*1000, GHOST_kEventButtonDown, window, convertButton([event buttonNumber])));
1288                         //Handle tablet events combined with mouse events
1289                         switch ([event subtype]) {
1290                                 case NX_SUBTYPE_TABLET_POINT:
1291                                         handleTabletEvent(eventPtr, NSTabletPoint);
1292                                         break;
1293                                 case NX_SUBTYPE_TABLET_PROXIMITY:
1294                                         handleTabletEvent(eventPtr, NSTabletProximity);
1295                                         break;
1296                                 default:
1297                                         //No tablet event included : do nothing
1298                                         break;
1299                         }
1300                         break;
1301                                                 
1302                 case NSLeftMouseUp:
1303                 case NSRightMouseUp:
1304                 case NSOtherMouseUp:
1305                         pushEvent(new GHOST_EventButton([event timestamp]*1000, GHOST_kEventButtonUp, window, convertButton([event buttonNumber])));
1306                         //Handle tablet events combined with mouse events
1307                         switch ([event subtype]) {
1308                                 case NX_SUBTYPE_TABLET_POINT:
1309                                         handleTabletEvent(eventPtr, NSTabletPoint);
1310                                         break;
1311                                 case NX_SUBTYPE_TABLET_PROXIMITY:
1312                                         handleTabletEvent(eventPtr, NSTabletProximity);
1313                                         break;
1314                                 default:
1315                                         //No tablet event included : do nothing
1316                                         break;
1317                         }
1318                         break;
1319                         
1320                 case NSLeftMouseDragged:
1321                 case NSRightMouseDragged:
1322                 case NSOtherMouseDragged:                               
1323                         //Handle tablet events combined with mouse events
1324                         switch ([event subtype]) {
1325                                 case NX_SUBTYPE_TABLET_POINT:
1326                                         handleTabletEvent(eventPtr, NSTabletPoint);
1327                                         break;
1328                                 case NX_SUBTYPE_TABLET_PROXIMITY:
1329                                         handleTabletEvent(eventPtr, NSTabletProximity);
1330                                         break;
1331                                 default:
1332                                         //No tablet event included : do nothing
1333                                         break;
1334                         }
1335                         
1336                 case NSMouseMoved:
1337                                 switch (window->getCursorGrabMode()) {
1338                                         case GHOST_kGrabHide: //Cursor hidden grab operation : no cursor move
1339                                         {
1340                                                 GHOST_TInt32 x_warp, y_warp, x_accum, y_accum;
1341                                                 
1342                                                 window->getCursorGrabInitPos(x_warp, y_warp);
1343                                                 
1344                                                 window->getCursorGrabAccum(x_accum, y_accum);
1345                                                 x_accum += [event deltaX];
1346                                                 y_accum += -[event deltaY]; //Strange Apple implementation (inverted coordinates for the deltaY) ...
1347                                                 window->setCursorGrabAccum(x_accum, y_accum);
1348                                                 
1349                                                 pushEvent(new GHOST_EventCursor([event timestamp]*1000, GHOST_kEventCursorMove, window, x_warp+x_accum, y_warp+y_accum));
1350                                         }
1351                                                 break;
1352                                         case GHOST_kGrabWrap: //Wrap cursor at area/window boundaries
1353                                         {
1354                                                 NSPoint mousePos = [event locationInWindow];
1355                                                 GHOST_TInt32 x_mouse= mousePos.x;
1356                                                 GHOST_TInt32 y_mouse= mousePos.y;
1357                                                 GHOST_TInt32 x_accum, y_accum, x_cur, y_cur;
1358                                                 GHOST_Rect bounds, windowBounds, correctedBounds;
1359                                                 
1360                                                 /* fallback to window bounds */
1361                                                 if(window->getCursorGrabBounds(bounds)==GHOST_kFailure)
1362                                                         window->getClientBounds(bounds);
1363                                                 
1364                                                 //Switch back to Cocoa coordinates orientation (y=0 at botton,the same as blender internal btw!), and to client coordinates
1365                                                 window->getClientBounds(windowBounds);
1366                                                 window->screenToClient(bounds.m_l,bounds.m_b, correctedBounds.m_l, correctedBounds.m_t);
1367                                                 window->screenToClient(bounds.m_r, bounds.m_t, correctedBounds.m_r, correctedBounds.m_b);
1368                                                 correctedBounds.m_b = (windowBounds.m_b - windowBounds.m_t) - correctedBounds.m_b;
1369                                                 correctedBounds.m_t = (windowBounds.m_b - windowBounds.m_t) - correctedBounds.m_t;
1370                                                 
1371                                                 //Update accumulation counts
1372                                                 window->getCursorGrabAccum(x_accum, y_accum);
1373                                                 x_accum += [event deltaX]-m_cursorDelta_x;
1374                                                 y_accum += -[event deltaY]-m_cursorDelta_y; //Strange Apple implementation (inverted coordinates for the deltaY) ...
1375                                                 window->setCursorGrabAccum(x_accum, y_accum);
1376                                                 
1377                                                 
1378                                                 //Warp mouse cursor if needed
1379                                                 x_mouse += [event deltaX]-m_cursorDelta_x;
1380                                                 y_mouse += -[event deltaY]-m_cursorDelta_y;
1381                                                 correctedBounds.wrapPoint(x_mouse, y_mouse, 2);
1382                                                 
1383                                                 //Compensate for mouse moved event taking cursor position set into account
1384                                                 m_cursorDelta_x = x_mouse-mousePos.x;
1385                                                 m_cursorDelta_y = y_mouse-mousePos.y;
1386                                                 
1387                                                 //Set new cursor position
1388                                                 window->clientToScreen(x_mouse, y_mouse, x_cur, y_cur);
1389                                                 setCursorPosition(x_cur, y_cur); /* wrap */
1390                                                 
1391                                                 //Post event
1392                                                 window->getCursorGrabInitPos(x_cur, y_cur);
1393                                                 pushEvent(new GHOST_EventCursor([event timestamp]*1000, GHOST_kEventCursorMove, window, x_cur + x_accum, y_cur + y_accum));
1394                                         }
1395                                                 break;
1396                                         default:
1397                                         {
1398                                                 //Normal cursor operation: send mouse position in window
1399                                                 NSPoint mousePos = [event locationInWindow];
1400                                                 pushEvent(new GHOST_EventCursor([event timestamp]*1000, GHOST_kEventCursorMove, window, mousePos.x, mousePos.y));
1401                                                 m_cursorDelta_x=0;
1402                                                 m_cursorDelta_y=0; //Mouse motion occured between two cursor warps, so we can reset the delta counter
1403                                         }
1404                                                 break;
1405                                 }
1406                                 break;
1407                         
1408                 case NSScrollWheel:
1409                         {
1410                                 /* Send trackpad event if inside a trackpad gesture, send wheel event otherwise */
1411                                 if (!m_hasMultiTouchTrackpad || !m_isGestureInProgress) {
1412                                         GHOST_TInt32 delta;
1413                                         
1414                                         double deltaF = [event deltaY];
1415                                         if (deltaF == 0.0) break; //discard trackpad delta=0 events
1416                                         
1417                                         delta = deltaF > 0.0 ? 1 : -1;
1418                                         pushEvent(new GHOST_EventWheel([event timestamp]*1000, window, delta));
1419                                 }
1420                                 else {
1421                                         NSPoint mousePos = [event locationInWindow];
1422                                         double dx = [event deltaX];
1423                                         double dy = -[event deltaY];
1424                                         
1425                                         const double deltaMax = 50.0;
1426                                         
1427                                         if ((dx == 0) && (dy == 0)) break;
1428                                         
1429                                         /* Quadratic acceleration */
1430                                         dx = dx*(fabs(dx)+0.5);
1431                                         if (dx<0.0) dx-=0.5; else dx+=0.5;
1432                                         if (dx< -deltaMax) dx= -deltaMax; else if (dx>deltaMax) dx=deltaMax;
1433                                         
1434                                         dy = dy*(fabs(dy)+0.5);
1435                                         if (dy<0.0) dy-=0.5; else dy+=0.5;
1436                                         if (dy< -deltaMax) dy= -deltaMax; else if (dy>deltaMax) dy=deltaMax;
1437
1438                                         pushEvent(new GHOST_EventTrackpad([event timestamp]*1000, window, GHOST_kTrackpadEventScroll, mousePos.x, mousePos.y, dx, dy));
1439                                 }
1440                         }
1441                         break;
1442                         
1443                 case NSEventTypeMagnify:
1444                         {
1445                                 NSPoint mousePos = [event locationInWindow];
1446                                 pushEvent(new GHOST_EventTrackpad([event timestamp]*1000, window, GHOST_kTrackpadEventMagnify, mousePos.x, mousePos.y,
1447                                                                                                   [event magnification]*250.0 + 0.1, 0));
1448                         }
1449                         break;
1450
1451                 case NSEventTypeRotate:
1452                         {
1453                                 NSPoint mousePos = [event locationInWindow];
1454                                 pushEvent(new GHOST_EventTrackpad([event timestamp]*1000, window, GHOST_kTrackpadEventRotate, mousePos.x, mousePos.y,
1455                                                                                                   -[event rotation] * 5.0, 0));
1456                         }
1457                 case NSEventTypeBeginGesture:
1458                         m_isGestureInProgress = true;
1459                         break;
1460                 case NSEventTypeEndGesture:
1461                         m_isGestureInProgress = false;
1462                         break;
1463                 default:
1464                         return GHOST_kFailure;
1465                         break;
1466                 }
1467         
1468         return GHOST_kSuccess;
1469 }
1470
1471
1472 GHOST_TSuccess GHOST_SystemCocoa::handleKeyEvent(void *eventPtr)
1473 {
1474         NSEvent *event = (NSEvent *)eventPtr;
1475         GHOST_IWindow* window = m_windowManager->getActiveWindow();
1476         unsigned int modifiers;
1477         NSString *characters;
1478         NSData *convertedCharacters;
1479         GHOST_TKey keyCode;
1480         unsigned char ascii;
1481         NSString* charsIgnoringModifiers;
1482
1483         if (!window) {
1484                 /* If no active window found, still tries to find the window associated with the event
1485                  This may happen when Cocoa continues to send some events after the window deactivate one */
1486                 window = m_windowManager->getWindowAssociatedWithOSWindow((void*)[event window]);
1487                 if (!window) {
1488                         //printf("\nW failure for event 0x%x",[event type]);
1489                         return GHOST_kFailure;
1490                 }
1491         }
1492         
1493         switch ([event type]) {
1494                 case NSKeyDown:
1495                 case NSKeyUp:
1496                         charsIgnoringModifiers = [event charactersIgnoringModifiers];
1497                         if ([charsIgnoringModifiers length]>0)
1498                                 keyCode = convertKey([event keyCode],
1499                                                                          [charsIgnoringModifiers characterAtIndex:0]);
1500                         else
1501                                 keyCode = convertKey([event keyCode],0);
1502
1503                                 
1504                         characters = [event characters];
1505                         if ([characters length]>0) { //Check for dead keys
1506                                 //Convert characters to iso latin 1 encoding
1507                                 convertedCharacters = [characters dataUsingEncoding:NSISOLatin1StringEncoding];
1508                                 if ([convertedCharacters length]>0)
1509                                         ascii =((char*)[convertedCharacters bytes])[0];
1510                                 else
1511                                         ascii = 0; //Character not available in iso latin 1 encoding
1512                         }
1513                         else
1514                                 ascii= 0;
1515                         
1516                         if ((keyCode == GHOST_kKeyQ) && (m_modifierMask & NSCommandKeyMask))
1517                                 break; //Cmd-Q is directly handled by Cocoa
1518
1519                         if ([event type] == NSKeyDown) {
1520                                 pushEvent( new GHOST_EventKey([event timestamp]*1000, GHOST_kEventKeyDown, window, keyCode, ascii) );
1521                                 //printf("\nKey pressed keyCode=%u ascii=%i %c",keyCode,ascii,ascii);
1522                         } else {
1523                                 pushEvent( new GHOST_EventKey([event timestamp]*1000, GHOST_kEventKeyUp, window, keyCode, ascii) );
1524                         }
1525                         break;
1526         
1527                 case NSFlagsChanged: 
1528                         modifiers = [event modifierFlags];
1529                         
1530                         if ((modifiers & NSShiftKeyMask) != (m_modifierMask & NSShiftKeyMask)) {
1531                                 pushEvent( new GHOST_EventKey([event timestamp]*1000, (modifiers & NSShiftKeyMask)?GHOST_kEventKeyDown:GHOST_kEventKeyUp, window, GHOST_kKeyLeftShift) );
1532                         }
1533                         if ((modifiers & NSControlKeyMask) != (m_modifierMask & NSControlKeyMask)) {
1534                                 pushEvent( new GHOST_EventKey([event timestamp]*1000, (modifiers & NSControlKeyMask)?GHOST_kEventKeyDown:GHOST_kEventKeyUp, window, GHOST_kKeyLeftControl) );
1535                         }
1536                         if ((modifiers & NSAlternateKeyMask) != (m_modifierMask & NSAlternateKeyMask)) {
1537                                 pushEvent( new GHOST_EventKey([event timestamp]*1000, (modifiers & NSAlternateKeyMask)?GHOST_kEventKeyDown:GHOST_kEventKeyUp, window, GHOST_kKeyLeftAlt) );
1538                         }
1539                         if ((modifiers & NSCommandKeyMask) != (m_modifierMask & NSCommandKeyMask)) {
1540                                 pushEvent( new GHOST_EventKey([event timestamp]*1000, (modifiers & NSCommandKeyMask)?GHOST_kEventKeyDown:GHOST_kEventKeyUp, window, GHOST_kKeyCommand) );
1541                         }
1542                         
1543                         m_modifierMask = modifiers;
1544                         break;
1545                         
1546                 default:
1547                         return GHOST_kFailure;
1548                         break;
1549         }
1550         
1551         return GHOST_kSuccess;
1552 }
1553
1554
1555
1556 #pragma mark Clipboard get/set
1557
1558 GHOST_TUns8* GHOST_SystemCocoa::getClipboard(bool selection) const
1559 {
1560         GHOST_TUns8 * temp_buff;
1561         size_t pastedTextSize;  
1562         
1563         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1564         
1565         NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
1566         
1567         if (pasteBoard == nil) {
1568                 [pool drain];
1569                 return NULL;
1570         }
1571         
1572         NSArray *supportedTypes =
1573                 [NSArray arrayWithObjects: NSStringPboardType, nil];
1574         
1575         NSString *bestType = [[NSPasteboard generalPasteboard]
1576                                                   availableTypeFromArray:supportedTypes];
1577         
1578         if (bestType == nil) {
1579                 [pool drain];
1580                 return NULL;
1581         }
1582         
1583         NSString * textPasted = [pasteBoard stringForType:NSStringPboardType];
1584
1585         if (textPasted == nil) {
1586                 [pool drain];
1587                 return NULL;
1588         }
1589         
1590         pastedTextSize = [textPasted lengthOfBytesUsingEncoding:NSISOLatin1StringEncoding];
1591         
1592         temp_buff = (GHOST_TUns8*) malloc(pastedTextSize+1); 
1593
1594         if (temp_buff == NULL) {
1595                 [pool drain];
1596                 return NULL;
1597         }
1598         
1599         strncpy((char*)temp_buff, [textPasted cStringUsingEncoding:NSISOLatin1StringEncoding], pastedTextSize);
1600         
1601         temp_buff[pastedTextSize] = '\0';
1602         
1603         [pool drain];
1604
1605         if(temp_buff) {
1606                 return temp_buff;
1607         } else {
1608                 return NULL;
1609         }
1610 }
1611
1612 void GHOST_SystemCocoa::putClipboard(GHOST_TInt8 *buffer, bool selection) const
1613 {
1614         NSString *textToCopy;
1615         
1616         if(selection) {return;} // for copying the selection, used on X11
1617
1618         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1619                 
1620         NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
1621         
1622         if (pasteBoard == nil) {
1623                 [pool drain];
1624                 return;
1625         }
1626         
1627         NSArray *supportedTypes = [NSArray arrayWithObject:NSStringPboardType];
1628         
1629         [pasteBoard declareTypes:supportedTypes owner:nil];
1630         
1631         textToCopy = [NSString stringWithCString:buffer encoding:NSISOLatin1StringEncoding];
1632         
1633         [pasteBoard setString:textToCopy forType:NSStringPboardType];
1634         
1635         [pool drain];
1636 }