== interface ==
[blender.git] / source / blender / src / ghostwinlay.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32 #include <stdlib.h>
33 #include <stdio.h>
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include "MEM_guardedalloc.h"
40
41 #include "DNA_listBase.h"       
42 #include "DNA_userdef_types.h"  /* U.flag & TWOBUTTONMOUSE */
43
44 #include "BLI_blenlib.h"
45
46 #include "GHOST_C-api.h"
47
48 #include "BKE_utildefines.h"
49 #include "BKE_global.h"
50
51 #include "BIF_gl.h"
52 #include "BIF_graphics.h"
53 #include "BIF_mywindow.h"
54 #include "BIF_screen.h"
55 #include "BIF_usiblender.h"
56 #include "BIF_cursors.h"
57
58 #include "mydevice.h"
59 #include "blendef.h"
60
61 #include "winlay.h"
62
63 #ifdef __APPLE__
64 #include <OpenGL/OpenGL.h>
65 #include <Carbon/Carbon.h>
66
67 /*declarations*/
68 int checkAppleVideoCard(void);
69 void getMacAvailableBounds(short *, short *, short *, short *);
70
71 #endif
72 ///
73
74 struct _Window {
75         GHOST_WindowHandle      ghostwin;
76         
77                 /* Handler and private data for handler */
78         WindowHandlerFP         handler;
79         void                            *user_data;
80         
81                 /* Window state */
82         int             size[2], position[2];
83         int             active, visible;
84         
85                 /* Last known mouse/button/qualifier state */
86         int             lmouse[2];
87         int             lqual;          /* (LR_SHFTKEY, LR_CTRLKEY, LR_ALTKEY, LR_COMMANDKEY) */
88         int             lmbut;          /* (L_MOUSE, M_MOUSE, R_MOUSE) */
89
90                 /* Tracks the faked mouse button, if non-zero it is
91                  * the event number of the last faked button.
92                  */
93         int             faked_mbut;
94
95         GHOST_TimerTaskHandle   timer;
96         int                                             timer_event;
97 };
98
99 ///
100
101 #ifdef __APPLE__
102
103 /* to avoid killing small end comps, we want to allow
104    blender to start maximised if all the followings are true :
105                 - Renderer is OpenGL capable
106                 - Hardware acceleration
107                 - VRAM > 16 Mo
108                 
109    we will bail out if VRAM is less than 8Mo
110                 */
111                 
112 static int macPrefState = 0;
113                 
114 int checkAppleVideoCard(void) {
115         long theErr;
116         unsigned long display_mask;
117         CGLRendererInfoObj rend;
118         long nrend;
119         int j;
120         long value;
121         long maxvram = 0;   /* we get always more than 1 renderer, check one, at least, has 8 Mo */
122         
123         display_mask = CGDisplayIDToOpenGLDisplayMask (CGMainDisplayID() );     
124         
125         theErr = CGLQueryRendererInfo( display_mask, &rend, &nrend);
126         if (theErr == 0) {
127                 theErr = CGLDescribeRenderer (rend, 0, kCGLRPRendererCount, &nrend);
128                 if (theErr == 0) {
129                         for (j = 0; j < nrend; j++) {
130                                 theErr = CGLDescribeRenderer (rend, j, kCGLRPVideoMemory, &value); 
131                                 if (value > maxvram)
132                                         maxvram = value;
133                                 if ((theErr == 0) && (value >= 20000000)) {
134                                         theErr = CGLDescribeRenderer (rend, j, kCGLRPAccelerated, &value); 
135                                         if ((theErr == 0) && (value != 0)) {
136                                                 theErr = CGLDescribeRenderer (rend, j, kCGLRPCompliant, &value); 
137                                                 if ((theErr == 0) && (value != 0)) {
138                                                         /*fprintf(stderr,"make it big\n");*/
139                                                         CGLDestroyRendererInfo (rend);
140                                                         macPrefState = 8;
141                                                         return 1;
142                                                 }
143                                         }
144                                 }
145                         }
146                 }
147         }
148         if (maxvram < 7500000 ) {       /* put a standard alert and quit*/ 
149                 SInt16 junkHit;
150                 char  inError[] = "* Not enough VRAM    ";
151                 char  inText[] = "* blender needs at least 8Mb    ";
152                 inError[0] = 16;
153                 inText[0] = 28;
154                                 
155                 fprintf(stderr, " vram is %li . not enough, aborting\n", maxvram);
156                 StandardAlert (   kAlertStopAlert, (ConstStr255Param) &inError, (ConstStr255Param)&inText,NULL,&junkHit);
157                 abort();
158         }
159         CGLDestroyRendererInfo (rend);
160         return 0;
161 }
162
163 void getMacAvailableBounds(short *top, short *left, short *bottom, short *right) {
164         Rect outAvailableRect;
165         
166         GetAvailableWindowPositioningBounds ( GetMainDevice(), &outAvailableRect);
167         
168         *top = outAvailableRect.top;  
169     *left = outAvailableRect.left;
170     *bottom = outAvailableRect.bottom; 
171     *right = outAvailableRect.right;
172 }
173
174 #endif
175
176
177 static GHOST_SystemHandle g_system= 0;
178
179         /* Some simple ghost <-> blender conversions */
180         
181 static GHOST_TStandardCursor convert_cursor(int curs) {
182         switch(curs) {
183         default:
184         case CURSOR_STD:                return GHOST_kStandardCursorDefault;
185         case CURSOR_VPAINT:             return GHOST_kStandardCursorRightArrow;
186         case CURSOR_FACESEL:            return GHOST_kStandardCursorRightArrow;
187         case CURSOR_WAIT:               return GHOST_kStandardCursorWait;
188         case CURSOR_EDIT:               return GHOST_kStandardCursorCrosshair;
189         case CURSOR_HELP:               return GHOST_kStandardCursorHelp;
190         case CURSOR_X_MOVE:             return GHOST_kStandardCursorLeftRight;
191         case CURSOR_Y_MOVE:             return GHOST_kStandardCursorUpDown;
192         case CURSOR_PENCIL:             return GHOST_kStandardCursorPencil;
193         }
194 }
195
196 static int convert_mbut(GHOST_TButtonMask but) {
197         if (but == GHOST_kButtonMaskLeft) {
198                 return LEFTMOUSE;
199         } else if (but == GHOST_kButtonMaskRight) {
200                 return RIGHTMOUSE;
201         } else {
202                 return MIDDLEMOUSE;
203         }
204 }
205
206 static int convert_key(GHOST_TKey key) {
207         if (key>=GHOST_kKeyA && key<=GHOST_kKeyZ) {
208                 return (AKEY + ((int) key - GHOST_kKeyA));
209         } else if (key>=GHOST_kKey0 && key<=GHOST_kKey9) {
210                 return (ZEROKEY + ((int) key - GHOST_kKey0));
211         } else if (key>=GHOST_kKeyNumpad0 && key<=GHOST_kKeyNumpad9) {
212                 return (PAD0 + ((int) key - GHOST_kKeyNumpad0));
213         } else if (key>=GHOST_kKeyF1 && key<=GHOST_kKeyF12) {
214                 return (F1KEY + ((int) key - GHOST_kKeyF1));
215         } else {
216                 switch (key) {
217                 case GHOST_kKeyBackSpace:               return BACKSPACEKEY;
218                 case GHOST_kKeyTab:                             return TABKEY;
219                 case GHOST_kKeyLinefeed:                return LINEFEEDKEY;
220                 case GHOST_kKeyClear:                   return 0;
221                 case GHOST_kKeyEnter:                   return RETKEY;
222         
223                 case GHOST_kKeyEsc:                             return ESCKEY;
224                 case GHOST_kKeySpace:                   return SPACEKEY;
225                 case GHOST_kKeyQuote:                   return QUOTEKEY;
226                 case GHOST_kKeyComma:                   return COMMAKEY;
227                 case GHOST_kKeyMinus:                   return MINUSKEY;
228                 case GHOST_kKeyPeriod:                  return PERIODKEY;
229                 case GHOST_kKeySlash:                   return SLASHKEY;
230
231                 case GHOST_kKeySemicolon:               return SEMICOLONKEY;
232                 case GHOST_kKeyEqual:                   return EQUALKEY;
233
234                 case GHOST_kKeyLeftBracket:             return LEFTBRACKETKEY;
235                 case GHOST_kKeyRightBracket:    return RIGHTBRACKETKEY;
236                 case GHOST_kKeyBackslash:               return BACKSLASHKEY;
237                 case GHOST_kKeyAccentGrave:             return ACCENTGRAVEKEY;
238
239                 case GHOST_kKeyLeftShift:               return LEFTSHIFTKEY;
240                 case GHOST_kKeyRightShift:              return RIGHTSHIFTKEY;
241                 case GHOST_kKeyLeftControl:             return LEFTCTRLKEY;
242                 case GHOST_kKeyRightControl:    return RIGHTCTRLKEY;
243                 case GHOST_kKeyCommand:                 return COMMANDKEY;
244                 case GHOST_kKeyLeftAlt:                 return LEFTALTKEY;
245                 case GHOST_kKeyRightAlt:                return RIGHTALTKEY;
246
247                 case GHOST_kKeyCapsLock:                return CAPSLOCKKEY;
248                 case GHOST_kKeyNumLock:                 return 0;
249                 case GHOST_kKeyScrollLock:              return 0;
250
251                 case GHOST_kKeyLeftArrow:               return LEFTARROWKEY;
252                 case GHOST_kKeyRightArrow:              return RIGHTARROWKEY;
253                 case GHOST_kKeyUpArrow:                 return UPARROWKEY;
254                 case GHOST_kKeyDownArrow:               return DOWNARROWKEY;
255
256                 case GHOST_kKeyPrintScreen:             return 0;
257                 case GHOST_kKeyPause:                   return PAUSEKEY;
258
259                 case GHOST_kKeyInsert:                  return INSERTKEY;
260                 case GHOST_kKeyDelete:                  return DELKEY;
261                 case GHOST_kKeyHome:                    return HOMEKEY;
262                 case GHOST_kKeyEnd:                             return ENDKEY;
263                 case GHOST_kKeyUpPage:                  return PAGEUPKEY;
264                 case GHOST_kKeyDownPage:                return PAGEDOWNKEY;
265
266                 case GHOST_kKeyNumpadPeriod:    return PADPERIOD;
267                 case GHOST_kKeyNumpadEnter:             return PADENTER;
268                 case GHOST_kKeyNumpadPlus:              return PADPLUSKEY;
269                 case GHOST_kKeyNumpadMinus:             return PADMINUS;
270                 case GHOST_kKeyNumpadAsterisk:  return PADASTERKEY;
271                 case GHOST_kKeyNumpadSlash:             return PADSLASHKEY;
272
273                 case GHOST_kKeyGrLess:              return GRLESSKEY; 
274                         
275                 case GHOST_kKeyUnknown:                 return UNKNOWNKEY;
276
277                 default:
278                         return 0;
279                 }
280         }
281 }
282
283         /***/
284         
285
286 static Window *window_new(GHOST_WindowHandle ghostwin)
287 {
288         Window *win= MEM_callocN(sizeof(*win), "Window");
289         win->ghostwin= ghostwin;
290         
291         return win;
292 }
293
294 static void window_handle(Window *win, short event, short val)
295 {
296         if (win->handler) {
297                 win->handler(win, win->user_data, event, val, 0);
298         }
299 }
300
301 static void window_handle_ext(Window *win, short event, short val, short extra)
302 {
303         if (win->handler) {
304                 win->handler(win, win->user_data, event, val, extra);
305         }
306 }
307
308 static void window_free(Window *win) 
309 {
310         MEM_freeN(win);
311 }
312
313         /***/
314
315 static Window *active_gl_window= NULL;
316
317 Window *window_open(char *title, int posx, int posy, int sizex, int sizey, int start_maximized)
318 {
319         GHOST_WindowHandle ghostwin;
320         GHOST_TWindowState inital_state;
321         int scr_w, scr_h;
322
323         winlay_get_screensize(&scr_w, &scr_h);
324         posy= (scr_h-posy-sizey);
325         
326         /* create a fullscreen window on unix by default*/
327 #if !defined(WIN32) && !defined(__APPLE__)
328         inital_state= start_maximized?
329                 GHOST_kWindowStateFullScreen:GHOST_kWindowStateNormal;
330 #else
331 #ifdef _WIN32   // FULLSCREEN
332 //      if (start_maximized == G_WINDOWSTATE_FULLSCREEN)
333 //              inital_state= GHOST_kWindowStateFullScreen;
334 //      else
335                 inital_state= start_maximized?GHOST_kWindowStateMaximized:GHOST_kWindowStateNormal;
336 #else                   // APPLE
337         inital_state= start_maximized?GHOST_kWindowStateMaximized:GHOST_kWindowStateNormal;
338         inital_state += macPrefState;
339 #endif
340 #endif
341
342         ghostwin= GHOST_CreateWindow(g_system, 
343                                                                 title, 
344                                                                 posx, posy, sizex, sizey, 
345                                                                 inital_state, 
346                                                                 GHOST_kDrawingContextTypeOpenGL,
347                                                                 0 /* no stereo */);
348         
349         if (ghostwin) {
350                 Window *win= window_new(ghostwin);
351                 
352                 if (win) {
353                         GHOST_SetWindowUserData(ghostwin, win);
354                         
355                         win->position[0]= posx;
356                         win->position[1]= posy;
357                         win->size[0]= sizex;
358                         win->size[1]= sizey;
359                         
360                         win->lmouse[0]= win->size[0]/2;
361                         win->lmouse[1]= win->size[1]/2;
362                         
363                         
364                 } else {
365                         GHOST_DisposeWindow(g_system, ghostwin);
366                 }
367                 
368                 return win;
369         } else {
370                 return NULL;
371         }
372 }
373
374 void window_set_handler(Window *win, WindowHandlerFP handler, void *user_data)
375 {
376         win->handler= handler;
377         win->user_data= user_data;
378 }
379
380 static void window_timer_proc(GHOST_TimerTaskHandle timer, GHOST_TUns64 time)
381 {
382         Window *win= GHOST_GetTimerTaskUserData(timer);
383
384         win->handler(win, win->user_data, win->timer_event, 0, 0);
385 }
386
387 void window_set_timer(Window *win, int delay_ms, int event)
388 {
389         if (win->timer) GHOST_RemoveTimer(g_system, win->timer);
390
391         win->timer_event= event;
392         win->timer= GHOST_InstallTimer(g_system, delay_ms, delay_ms, window_timer_proc, win);
393 }
394
395 void window_destroy(Window *win) {
396         if (active_gl_window==win) {
397                 active_gl_window= NULL;
398         }
399         
400         if (win->timer) {
401                 GHOST_RemoveTimer(g_system, win->timer);
402                 win->timer= NULL;
403         }
404
405         GHOST_DisposeWindow(g_system, win->ghostwin);
406         window_free(win);
407 }
408
409 void window_set_cursor(Window *win, int curs) {
410         if (curs==CURSOR_NONE) {
411                 GHOST_SetCursorVisibility(win->ghostwin, 0);
412         } else {
413                 GHOST_SetCursorVisibility(win->ghostwin, 1);
414                 
415                 /* detect if we use system cursor or Blender cursor */
416                 switch(curs) {
417                         case CURSOR_TEXTEDIT:
418                                 SetBlenderCursor(BC_TEXTEDITCURSOR);
419                                 break;
420                         case CURSOR_VPAINT:
421                                 SetBlenderCursor(BC_PAINTBRUSHCURSOR);
422                                 break;
423                         default:
424                                 GHOST_SetCursorShape(win->ghostwin, convert_cursor(curs));
425                 }
426         }
427 }
428
429 void window_set_custom_cursor(Window *win, unsigned char mask[16][2], 
430                                         unsigned char bitmap[16][2], int hotx, int hoty) {
431         GHOST_SetCustomCursorShape(win->ghostwin, bitmap, mask, hotx, hoty);
432 }
433
434 void window_set_custom_cursor_ex(Window *win, BCursor *cursor, int useBig) {
435         if (useBig) {
436                 GHOST_SetCustomCursorShapeEx(win->ghostwin, 
437                         cursor->big_bm, cursor->big_mask, 
438                         cursor->big_sizex,cursor->big_sizey,
439                         cursor->big_hotx,cursor->big_hoty,
440                         cursor->fg_color, cursor->bg_color);
441         } else {
442                 GHOST_SetCustomCursorShapeEx(win->ghostwin, 
443                         cursor->small_bm, cursor->small_mask, 
444                         cursor->small_sizex,cursor->small_sizey,
445                         cursor->small_hotx,cursor->small_hoty,
446                         cursor->fg_color, cursor->bg_color);
447         }
448 }
449
450 void window_make_active(Window *win) {
451         if (win != active_gl_window) {
452                 active_gl_window= win;
453                 GHOST_ActivateWindowDrawingContext(win->ghostwin);
454         }
455 }
456
457 void window_swap_buffers(Window *win) {
458         GHOST_SwapWindowBuffers(win->ghostwin);
459 }
460
461 static int query_qual(char qual) {
462         GHOST_TModifierKeyMask left, right;
463         int val= 0;
464         
465         if (qual=='s') {
466                 left= GHOST_kModifierKeyLeftShift;
467                 right= GHOST_kModifierKeyRightShift;
468         } else if (qual=='c') {
469                 left= GHOST_kModifierKeyLeftControl;
470                 right= GHOST_kModifierKeyRightControl;
471         } else if (qual=='C') {
472                 left= right= GHOST_kModifierKeyCommand;
473         } else {
474                 left= GHOST_kModifierKeyLeftAlt;
475                 right= GHOST_kModifierKeyRightAlt;
476         }
477
478         GHOST_GetModifierKeyState(g_system, left, &val);
479         if (!val)
480                 GHOST_GetModifierKeyState(g_system, right, &val);
481         
482         return val;
483 }
484
485 static int change_bit(int val, int bit, int to_on) {
486         return to_on?(val|bit):(val&~bit);
487 }
488
489 static int event_proc(GHOST_EventHandle evt, GHOST_TUserDataPtr private) 
490 {
491         GHOST_TEventType type= GHOST_GetEventType(evt);
492
493         if (type == GHOST_kEventQuit) {
494                 exit_usiblender();
495         } else {
496                 GHOST_WindowHandle ghostwin= GHOST_GetEventWindow(evt);
497                 GHOST_TEventDataPtr data= GHOST_GetEventData(evt);
498                 Window *win;
499                 
500                 if (!ghostwin) {
501                         // XXX - should be checked, why are we getting an event here, and
502                         //      what is it?
503
504                         return 1;
505                 } else if (!GHOST_ValidWindow(g_system, ghostwin)) {
506                         // XXX - should be checked, why are we getting an event here, and
507                         //      what is it?
508
509                         return 1;
510                 } else {
511                         win= GHOST_GetWindowUserData(ghostwin);
512                 }
513                 
514                 switch (type) {
515                 case GHOST_kEventCursorMove: {
516                         if(win->active == 1) {
517                                 GHOST_TEventCursorData *cd= data;
518                                 int cx, cy;
519                                 
520                                 GHOST_ScreenToClient(win->ghostwin, cd->x, cd->y, &cx, &cy);
521                                 win->lmouse[0]= cx;
522                                 win->lmouse[1]= (win->size[1]-1) - cy;
523                                 
524                                 window_handle(win, MOUSEX, win->lmouse[0]);
525                                 window_handle(win, MOUSEY, win->lmouse[1]);
526                         }
527                         break;
528                 }
529                 case GHOST_kEventButtonDown:
530                 case GHOST_kEventButtonUp: {
531                         GHOST_TEventButtonData *bd= data;
532                         int val= (type==GHOST_kEventButtonDown);
533                         int bbut= convert_mbut(bd->button);
534                 
535                         if (bbut==LEFTMOUSE) {
536                                 if (val) {
537                                         if (win->lqual & LR_COMMANDKEY) {
538                                                 bbut= win->faked_mbut= RIGHTMOUSE;
539                                         } else if ((win->lqual & LR_ALTKEY) && (U.flag & USER_TWOBUTTONMOUSE)) {
540                                                 /* finally, it actually USES the userpref! :) -intrr */
541                                                 bbut= win->faked_mbut= MIDDLEMOUSE;
542                                         }
543                                 } else {
544                                         if (win->faked_mbut) {
545                                                 bbut= win->faked_mbut;
546                                                 win->faked_mbut= 0;
547                                         }
548                                 }
549                         }
550
551                         if (bbut==LEFTMOUSE) {
552                                 win->lmbut= change_bit(win->lmbut, L_MOUSE, val);
553                         } else if (bbut==MIDDLEMOUSE) {
554                                 win->lmbut= change_bit(win->lmbut, M_MOUSE, val);
555                         } else {
556                                 win->lmbut= change_bit(win->lmbut, R_MOUSE, val);
557                         }
558                         window_handle(win, bbut, val);
559                         
560                         break;
561                 }
562         
563                 case GHOST_kEventKeyDown:
564                 case GHOST_kEventKeyUp: {
565                         GHOST_TEventKeyData *kd= data;
566                         int val= (type==GHOST_kEventKeyDown);
567                         int bkey= convert_key(kd->key);
568
569                         if (bkey) {
570                                 if (bkey==LEFTSHIFTKEY || bkey==RIGHTSHIFTKEY) {
571                                         win->lqual= change_bit(win->lqual, LR_SHIFTKEY, val);
572                                 } else if (bkey==LEFTCTRLKEY || bkey==RIGHTCTRLKEY) {
573                                         win->lqual= change_bit(win->lqual, LR_CTRLKEY, val);
574                                 } else if (bkey==LEFTALTKEY || bkey==RIGHTALTKEY) {
575                                         win->lqual= change_bit(win->lqual, LR_ALTKEY, val);
576                                 } else if (bkey==COMMANDKEY) {
577                                         win->lqual= change_bit(win->lqual, LR_COMMANDKEY, val);
578                                 }
579
580                                 window_handle_ext(win, bkey, val, kd->ascii);
581                         }
582                         
583                         break;
584                 }
585
586                 case GHOST_kEventWheel: {
587                         GHOST_TEventWheelData* wheelData = (GHOST_TEventWheelData*) data;
588                         if (wheelData->z > 0) {
589                                 window_handle(win, WHEELUPMOUSE, 1);
590                         } else {
591                                 window_handle(win, WHEELDOWNMOUSE, 1);
592                         }
593                         break;
594                 }
595
596                 case GHOST_kEventWindowDeactivate:
597                 case GHOST_kEventWindowActivate: {
598                         win->active= (type==GHOST_kEventWindowActivate);
599                         window_handle(win, INPUTCHANGE, win->active);
600                         
601                         if (win->active) {
602                                 if ((win->lqual & LR_SHIFTKEY) && !query_qual('s')) {
603                                         win->lqual= change_bit(win->lqual, LR_SHIFTKEY, 0);
604                                         window_handle(win, LEFTSHIFTKEY, 0);
605                                 }
606                                 if ((win->lqual & LR_CTRLKEY) && !query_qual('c')) {
607                                         win->lqual= change_bit(win->lqual, LR_CTRLKEY, 0);
608                                         window_handle(win, LEFTCTRLKEY, 0);
609                                 }
610                                 if ((win->lqual & LR_ALTKEY) && !query_qual('a')) {
611                                         win->lqual= change_bit(win->lqual, LR_ALTKEY, 0);
612                                         window_handle(win, LEFTALTKEY, 0);
613                                 }
614                                 if ((win->lqual & LR_COMMANDKEY) && !query_qual('C')) {
615                                         win->lqual= change_bit(win->lqual, LR_COMMANDKEY, 0);
616                                         window_handle(win, LR_COMMANDKEY, 0);
617                                 }
618
619                                 /* 
620                                  * XXX quick hack so OSX version works better
621                                  * when the window is clicked on (focused).
622                                  *
623                                  * it used to pass on the old win->lmouse value,
624                                  * which causes a wrong click in Blender.
625                                  * Actually, a 'focus' click should not be passed
626                                  * on to blender... (ton)
627                                  */
628                                 if(1) { /* enables me to add locals */
629                                         int cx, cy, wx, wy;
630                                         GHOST_GetCursorPosition(g_system, &wx, &wy);
631
632                                         GHOST_ScreenToClient(win->ghostwin, wx, wy, &cx, &cy);
633                                         win->lmouse[0]= cx;
634                                         win->lmouse[1]= (win->size[1]-1) - cy;
635                                         window_handle(win, MOUSEX, win->lmouse[0]);
636                                         window_handle(win, MOUSEY, win->lmouse[1]);
637                                 }
638                         }
639                         
640                         break;
641                 }
642                 case GHOST_kEventWindowClose: {
643                         window_handle(win, WINCLOSE, 1);
644                         break;
645                 }
646                 case GHOST_kEventWindowUpdate: {
647                         window_handle(win, REDRAW, 1);
648                         break;
649                 }
650                 case GHOST_kEventWindowSize: {
651                         GHOST_RectangleHandle client_rect;
652                         int l, t, r, b, scr_w, scr_h;
653
654                         client_rect= GHOST_GetClientBounds(win->ghostwin);
655                         GHOST_GetRectangle(client_rect, &l, &t, &r, &b);
656                         
657                         GHOST_DisposeRectangle(client_rect);
658                         
659                         winlay_get_screensize(&scr_w, &scr_h);
660                         win->position[0]= l;
661                         win->position[1]= scr_h - b - 1;
662                         win->size[0]= r-l;
663                         win->size[1]= b-t;
664
665                         window_handle(win, RESHAPE, 1);
666                         break;
667                 }
668                 case GHOST_kEventUnknown:
669                 case GHOST_kEventQuit:
670                 case GHOST_kNumEventTypes:
671                         break;
672         }
673         }
674         
675         return 1;
676 }
677
678 char *window_get_title(Window *win) {
679         char *title= GHOST_GetTitle(win->ghostwin);
680         char *mem_title= BLI_strdup(title);
681         free(title);
682
683         return mem_title;
684 }
685
686 void window_set_title(Window *win, char *title) {
687         GHOST_SetTitle(win->ghostwin, title);
688 }
689
690 short window_get_qual(Window *win) 
691 {
692         int qual= 0;
693         
694         if( query_qual('s')) qual |= LR_SHIFTKEY;
695         if( query_qual('a')) qual |= LR_ALTKEY;
696         if( query_qual('c')) qual |= LR_CTRLKEY;
697         return qual;
698 //      return win->lqual;
699 }
700
701 short window_get_mbut(Window *win) {
702         return win->lmbut;
703 }
704
705 void window_get_mouse(Window *win, short *mval) {
706         mval[0]= win->lmouse[0];
707         mval[1]= win->lmouse[1];
708 }
709
710 void window_get_position(Window *win, int *posx_r, int *posy_r) {
711         *posx_r= win->position[0];
712         *posy_r= win->position[1];
713 }
714
715 void window_get_size(Window *win, int *width_r, int *height_r) {
716         *width_r= win->size[0];
717         *height_r= win->size[1];
718 }
719
720 void window_set_size(Window *win, int width, int height) {
721         GHOST_SetClientSize(win->ghostwin, width, height);
722 }
723
724 void window_lower(Window *win) {
725         GHOST_SetWindowOrder(win->ghostwin, GHOST_kWindowOrderBottom);
726 }
727
728 void window_raise(Window *win) {
729         GHOST_SetWindowOrder(win->ghostwin, GHOST_kWindowOrderTop);
730 #ifdef _WIN32
731         markdirty_all(); /* to avoid redraw errors in fullscreen mode (aphex) */
732 #endif
733 }
734
735 #if 0
736 //#ifdef _WIN32 //FULLSCREEN
737 void window_toggle_fullscreen(Window *win, int fullscreen) {
738         /* these two lines make sure front and backbuffer are equal. for swapbuffers */
739         markdirty_all();
740         screen_swapbuffers();
741
742         if(fullscreen)
743                 GHOST_SetWindowState(win->ghostwin, GHOST_kWindowStateFullScreen);
744         else
745                 GHOST_SetWindowState(win->ghostwin, GHOST_kWindowStateMaximized);
746 }
747 #endif
748
749 void window_warp_pointer(Window *win, int x, int y) {
750         int oldx=x, oldy=y;
751         
752         y= win->size[1] - y - 1;
753         GHOST_ClientToScreen(win->ghostwin, x, y, &x, &y);
754         GHOST_SetCursorPosition(g_system, x, y);
755         
756         /* on OSX (for example) the setcursor doesnt create event */
757         win->lmouse[0]= oldx;
758         win->lmouse[1]= oldy;
759 }
760
761 void window_queue_redraw(Window *win) {
762         GHOST_InvalidateWindow(win->ghostwin); // ghost will send back a redraw to blender
763 }
764
765 /***/
766
767 void winlay_process_events(int wait_for_event) {
768         GHOST_ProcessEvents(g_system, wait_for_event);
769         GHOST_DispatchEvents(g_system);
770 }
771
772 void winlay_get_screensize(int *width_r, int *height_r) {
773         unsigned int uiwidth;
774         unsigned int uiheight;
775         
776         if (!g_system) {
777                 GHOST_EventConsumerHandle consumer= GHOST_CreateEventConsumer(event_proc, NULL);
778         
779                 g_system= GHOST_CreateSystem();
780                 GHOST_AddEventConsumer(g_system, consumer);
781         }
782         
783         GHOST_GetMainDisplayDimensions(g_system, &uiwidth, &uiheight);
784         *width_r= uiwidth;
785         *height_r= uiheight;
786 }
787
788 Window *winlay_get_active_window(void) {
789         return active_gl_window;
790 }