6d38707811a6e81a008874521a14f7ff650d921d
[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 "BLI_blenlib.h"
46
47 #include "GHOST_C-api.h"
48
49 #include "BKE_utildefines.h"
50 #include "BKE_global.h"
51
52 #include "BIF_gl.h"
53 #include "BIF_graphics.h"
54 #include "BIF_mywindow.h"
55 #include "BIF_screen.h"
56 #include "BIF_usiblender.h"
57
58 #include "mydevice.h"
59 #include "blendef.h"
60
61 #include "winlay.h"
62
63 static GHOST_SystemHandle g_system= 0;
64
65         /* Some simple ghost <-> blender conversions */
66         
67 static GHOST_TStandardCursor convert_cursor(int curs) {
68         switch(curs) {
69         default:
70         case CURSOR_STD:                return GHOST_kStandardCursorDefault;
71         case CURSOR_VPAINT:             return GHOST_kStandardCursorLeftArrow;
72         case CURSOR_FACESEL:    return GHOST_kStandardCursorRightArrow;
73         case CURSOR_WAIT:               return GHOST_kStandardCursorWait;
74         case CURSOR_EDIT:               return GHOST_kStandardCursorCrosshair;
75         case CURSOR_HELP:               return GHOST_kStandardCursorHelp;
76         case CURSOR_X_MOVE:             return GHOST_kStandardCursorLeftRight;
77         case CURSOR_Y_MOVE:             return GHOST_kStandardCursorUpDown;
78         }
79 }
80
81 static int convert_mbut(GHOST_TButtonMask but) {
82         if (but == GHOST_kButtonMaskLeft) {
83                 return LEFTMOUSE;
84         } else if (but == GHOST_kButtonMaskRight) {
85                 return RIGHTMOUSE;
86         } else {
87                 return MIDDLEMOUSE;
88         }
89 }
90
91 static int convert_key(GHOST_TKey key) {
92         if (key>=GHOST_kKeyA && key<=GHOST_kKeyZ) {
93                 return (AKEY + ((int) key - GHOST_kKeyA));
94         } else if (key>=GHOST_kKey0 && key<=GHOST_kKey9) {
95                 return (ZEROKEY + ((int) key - GHOST_kKey0));
96         } else if (key>=GHOST_kKeyNumpad0 && key<=GHOST_kKeyNumpad9) {
97                 return (PAD0 + ((int) key - GHOST_kKeyNumpad0));
98         } else if (key>=GHOST_kKeyF1 && key<=GHOST_kKeyF12) {
99                 return (F1KEY + ((int) key - GHOST_kKeyF1));
100         } else {
101                 switch (key) {
102                 case GHOST_kKeyBackSpace:               return BACKSPACEKEY;
103                 case GHOST_kKeyTab:                             return TABKEY;
104                 case GHOST_kKeyLinefeed:                return LINEFEEDKEY;
105                 case GHOST_kKeyClear:                   return 0;
106                 case GHOST_kKeyEnter:                   return RETKEY;
107         
108                 case GHOST_kKeyEsc:                             return ESCKEY;
109                 case GHOST_kKeySpace:                   return SPACEKEY;
110                 case GHOST_kKeyQuote:                   return QUOTEKEY;
111                 case GHOST_kKeyComma:                   return COMMAKEY;
112                 case GHOST_kKeyMinus:                   return MINUSKEY;
113                 case GHOST_kKeyPeriod:                  return PERIODKEY;
114                 case GHOST_kKeySlash:                   return SLASHKEY;
115
116                 case GHOST_kKeySemicolon:               return SEMICOLONKEY;
117                 case GHOST_kKeyEqual:                   return EQUALKEY;
118
119                 case GHOST_kKeyLeftBracket:             return LEFTBRACKETKEY;
120                 case GHOST_kKeyRightBracket:    return RIGHTBRACKETKEY;
121                 case GHOST_kKeyBackslash:               return BACKSLASHKEY;
122                 case GHOST_kKeyAccentGrave:             return ACCENTGRAVEKEY;
123
124                 case GHOST_kKeyLeftShift:               return LEFTSHIFTKEY;
125                 case GHOST_kKeyRightShift:              return RIGHTSHIFTKEY;
126                 case GHOST_kKeyLeftControl:             return LEFTCTRLKEY;
127                 case GHOST_kKeyRightControl:    return RIGHTCTRLKEY;
128                 case GHOST_kKeyLeftAlt:                 return LEFTALTKEY;
129                 case GHOST_kKeyRightAlt:                return RIGHTALTKEY;
130
131                 case GHOST_kKeyCapsLock:                return CAPSLOCKKEY;
132                 case GHOST_kKeyNumLock:                 return 0;
133                 case GHOST_kKeyScrollLock:              return 0;
134
135                 case GHOST_kKeyLeftArrow:               return LEFTARROWKEY;
136                 case GHOST_kKeyRightArrow:              return RIGHTARROWKEY;
137                 case GHOST_kKeyUpArrow:                 return UPARROWKEY;
138                 case GHOST_kKeyDownArrow:               return DOWNARROWKEY;
139
140                 case GHOST_kKeyPrintScreen:             return 0;
141                 case GHOST_kKeyPause:                   return PAUSEKEY;
142
143                 case GHOST_kKeyInsert:                  return INSERTKEY;
144                 case GHOST_kKeyDelete:                  return DELKEY;
145                 case GHOST_kKeyHome:                    return HOMEKEY;
146                 case GHOST_kKeyEnd:                             return ENDKEY;
147                 case GHOST_kKeyUpPage:                  return PAGEUPKEY;
148                 case GHOST_kKeyDownPage:                return PAGEDOWNKEY;
149
150                 case GHOST_kKeyNumpadPeriod:    return PADPERIOD;
151                 case GHOST_kKeyNumpadEnter:             return PADENTER;
152                 case GHOST_kKeyNumpadPlus:              return PADPLUSKEY;
153                 case GHOST_kKeyNumpadMinus:             return PADMINUS;
154                 case GHOST_kKeyNumpadAsterisk:  return PADASTERKEY;
155                 case GHOST_kKeyNumpadSlash:             return PADSLASHKEY;
156                 case GHOST_kKeyUnknown:                 return UNKNOWNKEY;
157
158                 default:
159                         return 0;
160                 }
161         }
162 }
163
164         /***/
165         
166 struct _Window {
167         GHOST_WindowHandle      ghostwin;
168         
169                 /* Handler and private data for handler */
170         WindowHandlerFP         handler;
171         void                            *user_data;
172         
173                 /* Window state */
174         int             size[2], position[2];
175         int             active, visible;
176         
177                 /* Last known mouse/button/qualifier state */
178         int             lmouse[2];
179         int             lqual;          /* (LR_SHFTKEY, LR_CTRLKEY, LR_ALTKEY) */
180         int             lmbut;          /* (L_MOUSE, M_MOUSE, R_MOUSE) */
181         int             commandqual;
182
183                 /* Tracks the faked mouse button, if non-zero it is
184                  * the event number of the last faked button.
185                  */
186         int             faked_mbut;
187
188         GHOST_TimerTaskHandle   timer;
189         int                                             timer_event;
190 };
191
192 static Window *window_new(GHOST_WindowHandle ghostwin)
193 {
194         Window *win= MEM_callocN(sizeof(*win), "Window");
195         win->ghostwin= ghostwin;
196         
197         return win;
198 }
199
200 static void window_handle(Window *win, short event, short val)
201 {
202         if (win->handler) {
203                 win->handler(win, win->user_data, event, val, 0);
204         }
205 }
206 static void window_handle_ext(Window *win, short event, short val, short extra)
207 {
208         if (win->handler) {
209                 win->handler(win, win->user_data, event, val, extra);
210         }
211 }
212
213 static void window_free(Window *win) 
214 {
215         MEM_freeN(win);
216 }
217
218         /***/
219
220 static Window *active_gl_window= NULL;
221
222 Window *window_open(char *title, int posx, int posy, int sizex, int sizey, int start_maximized)
223 {
224         GHOST_WindowHandle ghostwin;
225         GHOST_TWindowState inital_state;
226         int scr_w, scr_h;
227
228         winlay_get_screensize(&scr_w, &scr_h);
229         posy= (scr_h-posy-sizey);
230         
231         /* create a fullscreen window on unix by default*/
232 #if !defined(WIN32) && !defined(__APPLE__)
233         inital_state= start_maximized?
234                 GHOST_kWindowStateFullScreen:GHOST_kWindowStateNormal;
235 #else
236         inital_state= start_maximized?
237                 GHOST_kWindowStateMaximized:GHOST_kWindowStateNormal;
238 #endif
239
240         ghostwin= GHOST_CreateWindow(g_system, 
241                                                                 title, 
242                                                                 posx, posy, sizex, sizey, 
243                                                                 inital_state, 
244                                                                 GHOST_kDrawingContextTypeOpenGL,
245                                                                 0 /* no stereo */);
246         
247         if (ghostwin) {
248                 Window *win= window_new(ghostwin);
249                 
250                 if (win) {
251                         GHOST_SetWindowUserData(ghostwin, win);
252                         
253                         win->position[0]= posx;
254                         win->position[1]= posy;
255                         win->size[0]= sizex;
256                         win->size[1]= sizey;
257                         
258                         win->lmouse[0]= win->size[0]/2;
259                         win->lmouse[1]= win->size[1]/2;
260                 } else {
261                         GHOST_DisposeWindow(g_system, ghostwin);
262                 }
263                 
264                 return win;
265         } else {
266                 return NULL;
267         }
268 }
269
270 void window_set_handler(Window *win, WindowHandlerFP handler, void *user_data)
271 {
272         win->handler= handler;
273         win->user_data= user_data;
274 }
275
276 static void window_timer_proc(GHOST_TimerTaskHandle timer, GHOST_TUns64 time)
277 {
278         Window *win= GHOST_GetTimerTaskUserData(timer);
279
280         win->handler(win, win->user_data, win->timer_event, 0, 0);
281 }
282
283 void window_set_timer(Window *win, int delay_ms, int event)
284 {
285         if (win->timer) GHOST_RemoveTimer(g_system, win->timer);
286
287         win->timer_event= event;
288         win->timer= GHOST_InstallTimer(g_system, delay_ms, delay_ms, window_timer_proc, win);
289 }
290
291 void window_destroy(Window *win) {
292         if (active_gl_window==win) {
293                 active_gl_window= NULL;
294         }
295         
296         if (win->timer) {
297                 GHOST_RemoveTimer(g_system, win->timer);
298                 win->timer= NULL;
299         }
300
301         GHOST_DisposeWindow(g_system, win->ghostwin);
302         window_free(win);
303 }
304
305 void window_set_cursor(Window *win, int curs) {
306         if (curs==CURSOR_NONE) {
307                 GHOST_SetCursorVisibility(win->ghostwin, 0);
308         } else {
309                 GHOST_SetCursorVisibility(win->ghostwin, 1);
310                 GHOST_SetCursorShape(win->ghostwin, convert_cursor(curs));
311         }
312 }
313
314 void window_set_custom_cursor(Window *win, unsigned char mask[16][2], unsigned char bitmap[16][2]) {
315         GHOST_SetCustomCursorShape(win->ghostwin, bitmap, mask, 7, 7);
316 }
317
318 void window_make_active(Window *win) {
319         if (win != active_gl_window) {
320                 active_gl_window= win;
321                 GHOST_ActivateWindowDrawingContext(win->ghostwin);
322         }
323 }
324 void window_swap_buffers(Window *win) {
325         GHOST_SwapWindowBuffers(win->ghostwin);
326 }
327
328 static int query_qual(char qual) {
329         GHOST_TModifierKeyMask left, right;
330         int val= 0;
331         
332         if (qual=='s') {
333                 left= GHOST_kModifierKeyLeftShift;
334                 right= GHOST_kModifierKeyRightShift;
335         } else if (qual=='c') {
336                 left= GHOST_kModifierKeyLeftControl;
337                 right= GHOST_kModifierKeyRightControl;
338         } else if (qual=='C') {
339                 left= right= GHOST_kModifierKeyCommand;
340         } else {
341                 left= GHOST_kModifierKeyLeftAlt;
342                 right= GHOST_kModifierKeyRightAlt;
343         }
344
345         GHOST_GetModifierKeyState(g_system, left, &val);
346         if (!val)
347                 GHOST_GetModifierKeyState(g_system, right, &val);
348         
349         return val;
350 }
351 static int change_bit(int val, int bit, int to_on) {
352         return to_on?(val|bit):(val&~bit);
353 }
354 static int event_proc(GHOST_EventHandle evt, GHOST_TUserDataPtr private) {
355         GHOST_TEventType type= GHOST_GetEventType(evt);
356
357         if (type == GHOST_kEventQuit) {
358                 exit_usiblender();
359         } else {
360                 GHOST_WindowHandle ghostwin= GHOST_GetEventWindow(evt);
361                 GHOST_TEventDataPtr data= GHOST_GetEventData(evt);
362                 Window *win;
363                 
364                 if (!ghostwin) {
365                         printf("GHOST event error - no window - type: %d\n", type);
366                         return 1;
367                 } else if (!GHOST_ValidWindow(g_system, ghostwin)) {
368                         printf("GHOST event error - invalid window - win: %p\n", ghostwin);
369                         return 1;
370                 } else {
371                         win= GHOST_GetWindowUserData(ghostwin);
372                 }
373                 
374                 switch (type) {
375                 case GHOST_kEventCursorMove: {
376                         GHOST_TEventCursorData *cd= data;
377                         int cx, cy;
378                         
379                         GHOST_ScreenToClient(win->ghostwin, cd->x, cd->y, &cx, &cy);
380                         win->lmouse[0]= cx;
381                         win->lmouse[1]= (win->size[1]-1) - cy;
382
383                         window_handle(win, MOUSEX, win->lmouse[0]);
384                         window_handle(win, MOUSEY, win->lmouse[1]);
385                         
386                         break;
387                 }
388                 case GHOST_kEventButtonDown:
389                 case GHOST_kEventButtonUp: {
390                         GHOST_TEventButtonData *bd= data;
391                         int val= (type==GHOST_kEventButtonDown);
392                         int bbut= convert_mbut(bd->button);
393                 
394                         if (bbut==LEFTMOUSE) {
395                                 if (val) {
396                                         if (win->commandqual) {
397                                                 bbut= win->faked_mbut= RIGHTMOUSE;
398                                         } else if (win->lqual & LR_ALTKEY) {
399                                                 bbut= win->faked_mbut= MIDDLEMOUSE;
400                                         }
401                                 } else {
402                                         if (win->faked_mbut) {
403                                                 bbut= win->faked_mbut;
404                                                 win->faked_mbut= 0;
405                                         }
406                                 }
407                         }
408
409                         if (bbut==LEFTMOUSE) {
410                                 win->lmbut= change_bit(win->lmbut, L_MOUSE, val);
411                         } else if (bbut==MIDDLEMOUSE) {
412                                 win->lmbut= change_bit(win->lmbut, M_MOUSE, val);
413                         } else {
414                                 win->lmbut= change_bit(win->lmbut, R_MOUSE, val);
415                         }
416                         window_handle(win, bbut, val);
417                         
418                         break;
419                 }
420         
421                 case GHOST_kEventKeyDown:
422                 case GHOST_kEventKeyUp: {
423                         GHOST_TEventKeyData *kd= data;
424                         int val= (type==GHOST_kEventKeyDown);
425                         int bkey= convert_key(kd->key);
426
427                         if (kd->key == GHOST_kKeyCommand) {
428                                 win->commandqual= val;
429                         }
430
431                         if (bkey) {
432                                 if (bkey==LEFTSHIFTKEY || bkey==RIGHTSHIFTKEY) {
433                                         win->lqual= change_bit(win->lqual, LR_SHIFTKEY, val);
434                                 } else if (bkey==LEFTCTRLKEY || bkey==RIGHTCTRLKEY) {
435                                         win->lqual= change_bit(win->lqual, LR_CTRLKEY, val);
436                                 } else if (bkey==LEFTALTKEY || bkey==RIGHTALTKEY) {
437                                         win->lqual= change_bit(win->lqual, LR_ALTKEY, val);
438                                 }
439
440                                 window_handle_ext(win, bkey, val, kd->ascii);
441                         }
442                         
443                         break;
444                 }
445         
446                 case GHOST_kEventWheel:
447                 {
448                         GHOST_TEventWheelData* wheelData = (GHOST_TEventWheelData*) data;
449                         if (wheelData->z > 0)
450                         {
451                                 window_handle(win, WHEELUPMOUSE, 1);
452                         }
453                         else
454                         {
455                                 window_handle(win, WHEELDOWNMOUSE, 1);
456                         }
457                         }
458                         break;
459
460                 case GHOST_kEventWindowDeactivate:
461                 case GHOST_kEventWindowActivate: {
462                         win->active= (type==GHOST_kEventWindowActivate);
463                         window_handle(win, INPUTCHANGE, win->active);
464                         
465                         if (win->active) {
466                                 if ((win->lqual & LR_SHIFTKEY) && !query_qual('s')) {
467                                         win->lqual= change_bit(win->lqual, LR_SHIFTKEY, 0);
468                                         window_handle(win, LEFTSHIFTKEY, 0);
469                                 }
470                                 if ((win->lqual & LR_CTRLKEY) && !query_qual('c')) {
471                                         win->lqual= change_bit(win->lqual, LR_CTRLKEY, 0);
472                                         window_handle(win, LEFTCTRLKEY, 0);
473                                 }
474                                 if ((win->lqual & LR_ALTKEY) && !query_qual('a')) {
475                                         win->lqual= change_bit(win->lqual, LR_ALTKEY, 0);
476                                         window_handle(win, LEFTALTKEY, 0);
477                                 }
478                                 win->commandqual= query_qual('C');
479
480                                 /* 
481                                  * XXX quick hack so OSX version works better
482                                  * when the window is clicked on (focused). 
483                                  */
484                                 window_handle(win, MOUSEX, win->lmouse[0]);
485                                 window_handle(win, MOUSEY, win->lmouse[1]);
486                         }
487                         
488                         break;
489                 }
490                 case GHOST_kEventWindowClose: {
491                         window_handle(win, WINCLOSE, 1);
492                         break;
493                 }
494                 case GHOST_kEventWindowUpdate: {
495                         window_handle(win, REDRAW, 1);
496                         break;
497                 }
498                 case GHOST_kEventWindowSize: {
499                         GHOST_RectangleHandle client_rect;
500                         int l, t, r, b, scr_w, scr_h;
501
502                         client_rect= GHOST_GetClientBounds(win->ghostwin);
503                         GHOST_GetRectangle(client_rect, &l, &t, &r, &b);
504                         
505                         GHOST_DisposeRectangle(client_rect);
506                         
507                         winlay_get_screensize(&scr_w, &scr_h);
508                         win->position[0]= l;
509                         win->position[1]= scr_h - b - 1;
510                         win->size[0]= r-l;
511                         win->size[1]= b-t;
512
513                         window_handle(win, RESHAPE, 1);
514                         break;
515                 }
516                 }
517         }
518
519         return 1;
520 }
521
522 char *window_get_title(Window *win) {
523         char *title= GHOST_GetTitle(win->ghostwin);
524         char *mem_title= BLI_strdup(title);
525         free(title);
526
527         return mem_title;
528 }
529 void window_set_title(Window *win, char *title) {
530         GHOST_SetTitle(win->ghostwin, title);
531 }
532
533 short window_get_qual(Window *win) {
534         return win->lqual;
535 }
536 short window_get_mbut(Window *win) {
537         return win->lmbut;
538 }
539 void window_get_mouse(Window *win, short *mval) {
540         mval[0]= win->lmouse[0];
541         mval[1]= win->lmouse[1];
542 }
543 void window_get_position(Window *win, int *posx_r, int *posy_r) {
544         *posx_r= win->position[0];
545         *posy_r= win->position[1];
546 }
547 void window_get_size(Window *win, int *width_r, int *height_r) {
548         *width_r= win->size[0];
549         *height_r= win->size[1];
550 }
551
552 void window_set_size(Window *win, int width, int height) {
553         GHOST_SetClientSize(win->ghostwin, width, height);
554 }
555
556 void window_lower(Window *win) {
557         GHOST_SetWindowOrder(win->ghostwin, GHOST_kWindowOrderBottom);
558 }
559 void window_raise(Window *win) {
560         GHOST_SetWindowOrder(win->ghostwin, GHOST_kWindowOrderTop);
561 }
562
563 void window_warp_pointer(Window *win, int x, int y) {
564         y= win->size[1] - y - 1;
565         GHOST_ClientToScreen(win->ghostwin, x, y, &x, &y);
566         GHOST_SetCursorPosition(g_system, x, y);
567 }
568
569 void window_queue_redraw(Window *win) {
570         GHOST_InvalidateWindow(win->ghostwin);
571 }
572
573 /***/
574
575 void winlay_process_events(int wait_for_event) {
576         GHOST_ProcessEvents(g_system, wait_for_event);
577         GHOST_DispatchEvents(g_system);
578 }
579
580 void winlay_get_screensize(int *width_r, int *height_r) {
581         unsigned int uiwidth;
582         unsigned int uiheight;
583         
584         if (!g_system) {
585                 GHOST_EventConsumerHandle consumer= GHOST_CreateEventConsumer(event_proc, NULL);
586         
587                 g_system= GHOST_CreateSystem();
588                 GHOST_AddEventConsumer(g_system, consumer);
589         }
590         
591         GHOST_GetMainDisplayDimensions(g_system, &uiwidth, &uiheight);
592         *width_r= uiwidth;
593         *height_r= uiheight;
594 }
595
596 Window *winlay_get_active_window(void) {
597         return active_gl_window;
598 }