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