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