fix bug #1546
[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, LR_COMMANDKEY) */
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_kKeyCommand:                 return COMMANDKEY;
242                 case GHOST_kKeyLeftAlt:                 return LEFTALTKEY;
243                 case GHOST_kKeyRightAlt:                return RIGHTALTKEY;
244
245                 case GHOST_kKeyCapsLock:                return CAPSLOCKKEY;
246                 case GHOST_kKeyNumLock:                 return 0;
247                 case GHOST_kKeyScrollLock:              return 0;
248
249                 case GHOST_kKeyLeftArrow:               return LEFTARROWKEY;
250                 case GHOST_kKeyRightArrow:              return RIGHTARROWKEY;
251                 case GHOST_kKeyUpArrow:                 return UPARROWKEY;
252                 case GHOST_kKeyDownArrow:               return DOWNARROWKEY;
253
254                 case GHOST_kKeyPrintScreen:             return 0;
255                 case GHOST_kKeyPause:                   return PAUSEKEY;
256
257                 case GHOST_kKeyInsert:                  return INSERTKEY;
258                 case GHOST_kKeyDelete:                  return DELKEY;
259                 case GHOST_kKeyHome:                    return HOMEKEY;
260                 case GHOST_kKeyEnd:                             return ENDKEY;
261                 case GHOST_kKeyUpPage:                  return PAGEUPKEY;
262                 case GHOST_kKeyDownPage:                return PAGEDOWNKEY;
263
264                 case GHOST_kKeyNumpadPeriod:    return PADPERIOD;
265                 case GHOST_kKeyNumpadEnter:             return PADENTER;
266                 case GHOST_kKeyNumpadPlus:              return PADPLUSKEY;
267                 case GHOST_kKeyNumpadMinus:             return PADMINUS;
268                 case GHOST_kKeyNumpadAsterisk:  return PADASTERKEY;
269                 case GHOST_kKeyNumpadSlash:             return PADSLASHKEY;
270
271                 case GHOST_kKeyGrLess:              return GRLESSKEY; 
272                         
273                 case GHOST_kKeyUnknown:                 return UNKNOWNKEY;
274
275                 default:
276                         return 0;
277                 }
278         }
279 }
280
281         /***/
282         
283
284 static Window *window_new(GHOST_WindowHandle ghostwin)
285 {
286         Window *win= MEM_callocN(sizeof(*win), "Window");
287         win->ghostwin= ghostwin;
288         
289         return win;
290 }
291
292 static void window_handle(Window *win, short event, short val)
293 {
294         if (win->handler) {
295                 win->handler(win, win->user_data, event, val, 0);
296         }
297 }
298
299 static void window_handle_ext(Window *win, short event, short val, short extra)
300 {
301         if (win->handler) {
302                 win->handler(win, win->user_data, event, val, extra);
303         }
304 }
305
306 static void window_free(Window *win) 
307 {
308         MEM_freeN(win);
309 }
310
311         /***/
312
313 static Window *active_gl_window= NULL;
314
315 Window *window_open(char *title, int posx, int posy, int sizex, int sizey, int start_maximized)
316 {
317         GHOST_WindowHandle ghostwin;
318         GHOST_TWindowState inital_state;
319         int scr_w, scr_h;
320
321         winlay_get_screensize(&scr_w, &scr_h);
322         posy= (scr_h-posy-sizey);
323         
324         /* create a fullscreen window on unix by default*/
325 #if !defined(WIN32) && !defined(__APPLE__)
326         inital_state= start_maximized?
327                 GHOST_kWindowStateFullScreen:GHOST_kWindowStateNormal;
328 #else
329 #ifdef _WIN32   // FULLSCREEN
330         if (start_maximized == G_WINDOWSTATE_FULLSCREEN)
331                 inital_state= GHOST_kWindowStateFullScreen;
332         else
333                 inital_state= start_maximized?GHOST_kWindowStateMaximized:GHOST_kWindowStateNormal;
334 #else                   // APPLE
335         inital_state= start_maximized?GHOST_kWindowStateMaximized:GHOST_kWindowStateNormal;
336         inital_state += macPrefState;
337 #endif
338 #endif
339
340         ghostwin= GHOST_CreateWindow(g_system, 
341                                                                 title, 
342                                                                 posx, posy, sizex, sizey, 
343                                                                 inital_state, 
344                                                                 GHOST_kDrawingContextTypeOpenGL,
345                                                                 0 /* no stereo */);
346         
347         if (ghostwin) {
348                 Window *win= window_new(ghostwin);
349                 
350                 if (win) {
351                         GHOST_SetWindowUserData(ghostwin, win);
352                         
353                         win->position[0]= posx;
354                         win->position[1]= posy;
355                         win->size[0]= sizex;
356                         win->size[1]= sizey;
357                         
358                         win->lmouse[0]= win->size[0]/2;
359                         win->lmouse[1]= win->size[1]/2;
360                 } else {
361                         GHOST_DisposeWindow(g_system, ghostwin);
362                 }
363                 
364                 return win;
365         } else {
366                 return NULL;
367         }
368 }
369
370 void window_set_handler(Window *win, WindowHandlerFP handler, void *user_data)
371 {
372         win->handler= handler;
373         win->user_data= user_data;
374 }
375
376 static void window_timer_proc(GHOST_TimerTaskHandle timer, GHOST_TUns64 time)
377 {
378         Window *win= GHOST_GetTimerTaskUserData(timer);
379
380         win->handler(win, win->user_data, win->timer_event, 0, 0);
381 }
382
383 void window_set_timer(Window *win, int delay_ms, int event)
384 {
385         if (win->timer) GHOST_RemoveTimer(g_system, win->timer);
386
387         win->timer_event= event;
388         win->timer= GHOST_InstallTimer(g_system, delay_ms, delay_ms, window_timer_proc, win);
389 }
390
391 void window_destroy(Window *win) {
392         if (active_gl_window==win) {
393                 active_gl_window= NULL;
394         }
395         
396         if (win->timer) {
397                 GHOST_RemoveTimer(g_system, win->timer);
398                 win->timer= NULL;
399         }
400
401         GHOST_DisposeWindow(g_system, win->ghostwin);
402         window_free(win);
403 }
404
405 void window_set_cursor(Window *win, int curs) {
406         if (curs==CURSOR_NONE) {
407                 GHOST_SetCursorVisibility(win->ghostwin, 0);
408         } else {
409                 GHOST_SetCursorVisibility(win->ghostwin, 1);
410                 GHOST_SetCursorShape(win->ghostwin, convert_cursor(curs));
411         }
412 }
413
414 void window_set_custom_cursor(Window *win, unsigned char mask[16][2], 
415                                         unsigned char bitmap[16][2], int hotx, int hoty) {
416         GHOST_SetCustomCursorShape(win->ghostwin, bitmap, mask, hotx, hoty);
417 }
418
419 void window_set_custom_cursor_ex(Window *win, BCursor *cursor, int useBig) {
420         if (useBig) {
421                 GHOST_SetCustomCursorShapeEx(win->ghostwin, 
422                         cursor->big_bm, cursor->big_mask, 
423                         cursor->big_sizex,cursor->big_sizey,
424                         cursor->big_hotx,cursor->big_hoty,
425                         cursor->fg_color, cursor->bg_color);
426         } else {
427                 GHOST_SetCustomCursorShapeEx(win->ghostwin, 
428                         cursor->small_bm, cursor->small_mask, 
429                         cursor->small_sizex,cursor->small_sizey,
430                         cursor->small_hotx,cursor->small_hoty,
431                         cursor->fg_color, cursor->bg_color);
432         }
433 }
434
435 void window_make_active(Window *win) {
436         if (win != active_gl_window) {
437                 active_gl_window= win;
438                 GHOST_ActivateWindowDrawingContext(win->ghostwin);
439         }
440 }
441
442 void window_swap_buffers(Window *win) {
443         GHOST_SwapWindowBuffers(win->ghostwin);
444 }
445
446 static int query_qual(char qual) {
447         GHOST_TModifierKeyMask left, right;
448         int val= 0;
449         
450         if (qual=='s') {
451                 left= GHOST_kModifierKeyLeftShift;
452                 right= GHOST_kModifierKeyRightShift;
453         } else if (qual=='c') {
454                 left= GHOST_kModifierKeyLeftControl;
455                 right= GHOST_kModifierKeyRightControl;
456         } else if (qual=='C') {
457                 left= right= GHOST_kModifierKeyCommand;
458         } else {
459                 left= GHOST_kModifierKeyLeftAlt;
460                 right= GHOST_kModifierKeyRightAlt;
461         }
462
463         GHOST_GetModifierKeyState(g_system, left, &val);
464         if (!val)
465                 GHOST_GetModifierKeyState(g_system, right, &val);
466         
467         return val;
468 }
469
470 static int change_bit(int val, int bit, int to_on) {
471         return to_on?(val|bit):(val&~bit);
472 }
473
474 static int event_proc(GHOST_EventHandle evt, GHOST_TUserDataPtr private) 
475 {
476         GHOST_TEventType type= GHOST_GetEventType(evt);
477
478         if (type == GHOST_kEventQuit) {
479                 exit_usiblender();
480         } else {
481                 GHOST_WindowHandle ghostwin= GHOST_GetEventWindow(evt);
482                 GHOST_TEventDataPtr data= GHOST_GetEventData(evt);
483                 Window *win;
484                 
485                 if (!ghostwin) {
486                         // XXX - should be checked, why are we getting an event here, and
487                         //      what is it?
488
489                         return 1;
490                 } else if (!GHOST_ValidWindow(g_system, ghostwin)) {
491                         // XXX - should be checked, why are we getting an event here, and
492                         //      what is it?
493
494                         return 1;
495                 } else {
496                         win= GHOST_GetWindowUserData(ghostwin);
497                 }
498                 
499                 switch (type) {
500                 case GHOST_kEventCursorMove: {
501                         if(win->active == 1) {
502                                 GHOST_TEventCursorData *cd= data;
503                                 int cx, cy;
504                                 
505                                 GHOST_ScreenToClient(win->ghostwin, cd->x, cd->y, &cx, &cy);
506                                 win->lmouse[0]= cx;
507                                 win->lmouse[1]= (win->size[1]-1) - cy;
508                                 
509                                 window_handle(win, MOUSEX, win->lmouse[0]);
510                                 window_handle(win, MOUSEY, win->lmouse[1]);
511                         }
512                         break;
513                 }
514                 case GHOST_kEventButtonDown:
515                 case GHOST_kEventButtonUp: {
516                         GHOST_TEventButtonData *bd= data;
517                         int val= (type==GHOST_kEventButtonDown);
518                         int bbut= convert_mbut(bd->button);
519                 
520                         if (bbut==LEFTMOUSE) {
521                                 if (val) {
522                                         if (win->commandqual) {
523                                                 bbut= win->faked_mbut= RIGHTMOUSE;
524                                         } else if ((win->lqual & LR_ALTKEY) && (U.flag & USER_TWOBUTTONMOUSE)) {
525                                                 /* finally, it actually USES the userpref! :) -intrr */
526                                                 bbut= win->faked_mbut= MIDDLEMOUSE;
527                                         }
528                                 } else {
529                                         if (win->faked_mbut) {
530                                                 bbut= win->faked_mbut;
531                                                 win->faked_mbut= 0;
532                                         }
533                                 }
534                         }
535
536                         if (bbut==LEFTMOUSE) {
537                                 win->lmbut= change_bit(win->lmbut, L_MOUSE, val);
538                         } else if (bbut==MIDDLEMOUSE) {
539                                 win->lmbut= change_bit(win->lmbut, M_MOUSE, val);
540                         } else {
541                                 win->lmbut= change_bit(win->lmbut, R_MOUSE, val);
542                         }
543                         window_handle(win, bbut, val);
544                         
545                         break;
546                 }
547         
548                 case GHOST_kEventKeyDown:
549                 case GHOST_kEventKeyUp: {
550                         GHOST_TEventKeyData *kd= data;
551                         int val= (type==GHOST_kEventKeyDown);
552                         int bkey= convert_key(kd->key);
553
554                         if (kd->key == GHOST_kKeyCommand) {
555                                 win->commandqual= val;
556                         }
557
558                         if (bkey) {
559                                 if (bkey==LEFTSHIFTKEY || bkey==RIGHTSHIFTKEY) {
560                                         win->lqual= change_bit(win->lqual, LR_SHIFTKEY, val);
561                                 } else if (bkey==LEFTCTRLKEY || bkey==RIGHTCTRLKEY) {
562                                         win->lqual= change_bit(win->lqual, LR_CTRLKEY, val);
563                                 } else if (bkey==LEFTALTKEY || bkey==RIGHTALTKEY) {
564                                         win->lqual= change_bit(win->lqual, LR_ALTKEY, val);
565                                 } else if (bkey==COMMANDKEY) {
566                                         win->lqual= change_bit(win->lqual, LR_COMMANDKEY, val);
567                                 }
568
569                                 window_handle_ext(win, bkey, val, kd->ascii);
570                         }
571                         
572                         break;
573                 }
574
575                 case GHOST_kEventWheel: {
576                         GHOST_TEventWheelData* wheelData = (GHOST_TEventWheelData*) data;
577                         if (wheelData->z > 0) {
578                                 window_handle(win, WHEELUPMOUSE, 1);
579                         } else {
580                                 window_handle(win, WHEELDOWNMOUSE, 1);
581                         }
582                         break;
583                 }
584
585                 case GHOST_kEventWindowDeactivate:
586                 case GHOST_kEventWindowActivate: {
587                         win->active= (type==GHOST_kEventWindowActivate);
588                         window_handle(win, INPUTCHANGE, win->active);
589                         
590                         if (win->active) {
591                                 if ((win->lqual & LR_SHIFTKEY) && !query_qual('s')) {
592                                         win->lqual= change_bit(win->lqual, LR_SHIFTKEY, 0);
593                                         window_handle(win, LEFTSHIFTKEY, 0);
594                                 }
595                                 if ((win->lqual & LR_CTRLKEY) && !query_qual('c')) {
596                                         win->lqual= change_bit(win->lqual, LR_CTRLKEY, 0);
597                                         window_handle(win, LEFTCTRLKEY, 0);
598                                 }
599                                 if ((win->lqual & LR_ALTKEY) && !query_qual('a')) {
600                                         win->lqual= change_bit(win->lqual, LR_ALTKEY, 0);
601                                         window_handle(win, LEFTALTKEY, 0);
602                                 }
603                                 if ((win->lqual & LR_COMMANDKEY) && !query_qual('C')) {
604                                         win->lqual= change_bit(win->lqual, LR_COMMANDKEY, 0);
605                                         window_handle(win, LR_COMMANDKEY, 0);
606                                 }
607                                 /* probably redundant now (ton) */
608                                 win->commandqual= query_qual('C');
609
610                                 /* 
611                                  * XXX quick hack so OSX version works better
612                                  * when the window is clicked on (focused).
613                                  *
614                                  * it used to pass on the old win->lmouse value,
615                                  * which causes a wrong click in Blender.
616                                  * Actually, a 'focus' click should not be passed
617                                  * on to blender... (ton)
618                                  */
619                                 if(1) { /* enables me to add locals */
620                                         int cx, cy, wx, wy;
621                                         GHOST_GetCursorPosition(g_system, &wx, &wy);
622
623                                         GHOST_ScreenToClient(win->ghostwin, wx, wy, &cx, &cy);
624                                         win->lmouse[0]= cx;
625                                         win->lmouse[1]= (win->size[1]-1) - cy;
626                                         window_handle(win, MOUSEX, win->lmouse[0]);
627                                         window_handle(win, MOUSEY, win->lmouse[1]);
628                                 }
629                         }
630                         
631                         break;
632                 }
633                 case GHOST_kEventWindowClose: {
634                         window_handle(win, WINCLOSE, 1);
635                         break;
636                 }
637                 case GHOST_kEventWindowUpdate: {
638                         window_handle(win, REDRAW, 1);
639                         break;
640                 }
641                 case GHOST_kEventWindowSize: {
642                         GHOST_RectangleHandle client_rect;
643                         int l, t, r, b, scr_w, scr_h;
644
645                         client_rect= GHOST_GetClientBounds(win->ghostwin);
646                         GHOST_GetRectangle(client_rect, &l, &t, &r, &b);
647                         
648                         GHOST_DisposeRectangle(client_rect);
649                         
650                         winlay_get_screensize(&scr_w, &scr_h);
651                         win->position[0]= l;
652                         win->position[1]= scr_h - b - 1;
653                         win->size[0]= r-l;
654                         win->size[1]= b-t;
655
656                         window_handle(win, RESHAPE, 1);
657                         break;
658                 }
659         }
660         }
661         
662         return 1;
663 }
664
665 char *window_get_title(Window *win) {
666         char *title= GHOST_GetTitle(win->ghostwin);
667         char *mem_title= BLI_strdup(title);
668         free(title);
669
670         return mem_title;
671 }
672
673 void window_set_title(Window *win, char *title) {
674         GHOST_SetTitle(win->ghostwin, title);
675 }
676
677 short window_get_qual(Window *win) 
678 {
679         int qual= 0;
680         
681         if( query_qual('s')) qual |= LR_SHIFTKEY;
682         if( query_qual('a')) qual |= LR_ALTKEY;
683         if( query_qual('c')) qual |= LR_CTRLKEY;
684         return qual;
685 //      return win->lqual;
686 }
687
688 short window_get_mbut(Window *win) {
689         return win->lmbut;
690 }
691
692 void window_get_mouse(Window *win, short *mval) {
693         mval[0]= win->lmouse[0];
694         mval[1]= win->lmouse[1];
695 }
696
697 void window_get_position(Window *win, int *posx_r, int *posy_r) {
698         *posx_r= win->position[0];
699         *posy_r= win->position[1];
700 }
701
702 void window_get_size(Window *win, int *width_r, int *height_r) {
703         *width_r= win->size[0];
704         *height_r= win->size[1];
705 }
706
707 void window_set_size(Window *win, int width, int height) {
708         GHOST_SetClientSize(win->ghostwin, width, height);
709 }
710
711 void window_lower(Window *win) {
712         GHOST_SetWindowOrder(win->ghostwin, GHOST_kWindowOrderBottom);
713 }
714
715 void window_raise(Window *win) {
716         GHOST_SetWindowOrder(win->ghostwin, GHOST_kWindowOrderTop);
717 }
718
719 #ifdef _WIN32   //FULLSCREEN
720 void window_toggle_fullscreen(Window *win, int fullscreen) {
721         /* these two lines make sure front and backbuffer are equal. for swapbuffers */
722         markdirty_all();
723         screen_swapbuffers();
724
725         if(fullscreen)
726                 GHOST_SetWindowState(win->ghostwin, GHOST_kWindowStateFullScreen);
727         else
728                 GHOST_SetWindowState(win->ghostwin, GHOST_kWindowStateMaximized);
729 }
730 #endif
731
732 void window_warp_pointer(Window *win, int x, int y) {
733         int oldx=x, oldy=y;
734         
735         y= win->size[1] - y - 1;
736         GHOST_ClientToScreen(win->ghostwin, x, y, &x, &y);
737         GHOST_SetCursorPosition(g_system, x, y);
738         
739         /* on OSX (for example) the setcursor doesnt create event */
740         win->lmouse[0]= oldx;
741         win->lmouse[1]= oldy;
742 }
743
744 void window_queue_redraw(Window *win) {
745         GHOST_InvalidateWindow(win->ghostwin); // ghost will send back a redraw to blender
746 }
747
748 /***/
749
750 void winlay_process_events(int wait_for_event) {
751         GHOST_ProcessEvents(g_system, wait_for_event);
752         GHOST_DispatchEvents(g_system);
753 }
754
755 void winlay_get_screensize(int *width_r, int *height_r) {
756         unsigned int uiwidth;
757         unsigned int uiheight;
758         
759         if (!g_system) {
760                 GHOST_EventConsumerHandle consumer= GHOST_CreateEventConsumer(event_proc, NULL);
761         
762                 g_system= GHOST_CreateSystem();
763                 GHOST_AddEventConsumer(g_system, consumer);
764         }
765         
766         GHOST_GetMainDisplayDimensions(g_system, &uiwidth, &uiheight);
767         *width_r= uiwidth;
768         *height_r= uiheight;
769 }
770
771 Window *winlay_get_active_window(void) {
772         return active_gl_window;
773 }