73cb6cb9f403ddb62fa99dd216cf61ab129042fd
[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
207 static void window_handle_ext(Window *win, short event, short val, short extra)
208 {
209         if (win->handler) {
210                 win->handler(win, win->user_data, event, val, extra);
211         }
212 }
213
214 static void window_free(Window *win) 
215 {
216         MEM_freeN(win);
217 }
218
219         /***/
220
221 static Window *active_gl_window= NULL;
222
223 Window *window_open(char *title, int posx, int posy, int sizex, int sizey, int start_maximized)
224 {
225         GHOST_WindowHandle ghostwin;
226         GHOST_TWindowState inital_state;
227         int scr_w, scr_h;
228
229         winlay_get_screensize(&scr_w, &scr_h);
230         posy= (scr_h-posy-sizey);
231         
232         /* create a fullscreen window on unix by default*/
233 #if !defined(WIN32) && !defined(__APPLE__)
234         inital_state= start_maximized?
235                 GHOST_kWindowStateFullScreen:GHOST_kWindowStateNormal;
236 #else
237         if (start_maximized == 2)
238                 inital_state= GHOST_kWindowStateFullScreen;
239         else
240                 inital_state= start_maximized?GHOST_kWindowStateMaximized:GHOST_kWindowStateNormal;
241 #endif
242
243         ghostwin= GHOST_CreateWindow(g_system, 
244                                                                 title, 
245                                                                 posx, posy, sizex, sizey, 
246                                                                 inital_state, 
247                                                                 GHOST_kDrawingContextTypeOpenGL,
248                                                                 0 /* no stereo */);
249         
250         if (ghostwin) {
251                 Window *win= window_new(ghostwin);
252                 
253                 if (win) {
254                         GHOST_SetWindowUserData(ghostwin, win);
255                         
256                         win->position[0]= posx;
257                         win->position[1]= posy;
258                         win->size[0]= sizex;
259                         win->size[1]= sizey;
260                         
261                         win->lmouse[0]= win->size[0]/2;
262                         win->lmouse[1]= win->size[1]/2;
263                 } else {
264                         GHOST_DisposeWindow(g_system, ghostwin);
265                 }
266                 
267                 return win;
268         } else {
269                 return NULL;
270         }
271 }
272
273 void window_set_handler(Window *win, WindowHandlerFP handler, void *user_data)
274 {
275         win->handler= handler;
276         win->user_data= user_data;
277 }
278
279 static void window_timer_proc(GHOST_TimerTaskHandle timer, GHOST_TUns64 time)
280 {
281         Window *win= GHOST_GetTimerTaskUserData(timer);
282
283         win->handler(win, win->user_data, win->timer_event, 0, 0);
284 }
285
286 void window_set_timer(Window *win, int delay_ms, int event)
287 {
288         if (win->timer) GHOST_RemoveTimer(g_system, win->timer);
289
290         win->timer_event= event;
291         win->timer= GHOST_InstallTimer(g_system, delay_ms, delay_ms, window_timer_proc, win);
292 }
293
294 void window_destroy(Window *win) {
295         if (active_gl_window==win) {
296                 active_gl_window= NULL;
297         }
298         
299         if (win->timer) {
300                 GHOST_RemoveTimer(g_system, win->timer);
301                 win->timer= NULL;
302         }
303
304         GHOST_DisposeWindow(g_system, win->ghostwin);
305         window_free(win);
306 }
307
308 void window_set_cursor(Window *win, int curs) {
309         if (curs==CURSOR_NONE) {
310                 GHOST_SetCursorVisibility(win->ghostwin, 0);
311         } else {
312                 GHOST_SetCursorVisibility(win->ghostwin, 1);
313                 GHOST_SetCursorShape(win->ghostwin, convert_cursor(curs));
314         }
315 }
316
317 void window_set_custom_cursor(Window *win, unsigned char mask[16][2], unsigned char bitmap[16][2]) {
318         GHOST_SetCustomCursorShape(win->ghostwin, bitmap, mask, 7, 7);
319 }
320
321 void window_make_active(Window *win) {
322         if (win != active_gl_window) {
323                 active_gl_window= win;
324                 GHOST_ActivateWindowDrawingContext(win->ghostwin);
325         }
326 }
327
328 void window_swap_buffers(Window *win) {
329         GHOST_SwapWindowBuffers(win->ghostwin);
330 }
331
332 static int query_qual(char qual) {
333         GHOST_TModifierKeyMask left, right;
334         int val= 0;
335         
336         if (qual=='s') {
337                 left= GHOST_kModifierKeyLeftShift;
338                 right= GHOST_kModifierKeyRightShift;
339         } else if (qual=='c') {
340                 left= GHOST_kModifierKeyLeftControl;
341                 right= GHOST_kModifierKeyRightControl;
342         } else if (qual=='C') {
343                 left= right= GHOST_kModifierKeyCommand;
344         } else {
345                 left= GHOST_kModifierKeyLeftAlt;
346                 right= GHOST_kModifierKeyRightAlt;
347         }
348
349         GHOST_GetModifierKeyState(g_system, left, &val);
350         if (!val)
351                 GHOST_GetModifierKeyState(g_system, right, &val);
352         
353         return val;
354 }
355
356 static int change_bit(int val, int bit, int to_on) {
357         return to_on?(val|bit):(val&~bit);
358 }
359
360 static int event_proc(GHOST_EventHandle evt, GHOST_TUserDataPtr private) {
361         GHOST_TEventType type= GHOST_GetEventType(evt);
362
363         if (type == GHOST_kEventQuit) {
364                 exit_usiblender();
365         } else {
366                 GHOST_WindowHandle ghostwin= GHOST_GetEventWindow(evt);
367                 GHOST_TEventDataPtr data= GHOST_GetEventData(evt);
368                 Window *win;
369                 
370                 if (!ghostwin) {
371                         printf("GHOST event error - no window - type: %d\n", type);
372                         return 1;
373                 } else if (!GHOST_ValidWindow(g_system, ghostwin)) {
374                         printf("GHOST event error - invalid window - win: %p\n", ghostwin);
375                         return 1;
376                 } else {
377                         win= GHOST_GetWindowUserData(ghostwin);
378                 }
379                 
380                 switch (type) {
381                 case GHOST_kEventCursorMove: {
382                         GHOST_TEventCursorData *cd= data;
383                         int cx, cy;
384                         
385                         GHOST_ScreenToClient(win->ghostwin, cd->x, cd->y, &cx, &cy);
386                         win->lmouse[0]= cx;
387                         win->lmouse[1]= (win->size[1]-1) - cy;
388
389                         window_handle(win, MOUSEX, win->lmouse[0]);
390                         window_handle(win, MOUSEY, win->lmouse[1]);
391                         
392                         break;
393                 }
394                 case GHOST_kEventButtonDown:
395                 case GHOST_kEventButtonUp: {
396                         GHOST_TEventButtonData *bd= data;
397                         int val= (type==GHOST_kEventButtonDown);
398                         int bbut= convert_mbut(bd->button);
399                 
400                         if (bbut==LEFTMOUSE) {
401                                 if (val) {
402                                         if (win->commandqual) {
403                                                 bbut= win->faked_mbut= RIGHTMOUSE;
404                                         } else if (win->lqual & LR_ALTKEY) {
405                                                 bbut= win->faked_mbut= MIDDLEMOUSE;
406                                         }
407                                 } else {
408                                         if (win->faked_mbut) {
409                                                 bbut= win->faked_mbut;
410                                                 win->faked_mbut= 0;
411                                         }
412                                 }
413                         }
414
415                         if (bbut==LEFTMOUSE) {
416                                 win->lmbut= change_bit(win->lmbut, L_MOUSE, val);
417                         } else if (bbut==MIDDLEMOUSE) {
418                                 win->lmbut= change_bit(win->lmbut, M_MOUSE, val);
419                         } else {
420                                 win->lmbut= change_bit(win->lmbut, R_MOUSE, val);
421                         }
422                         window_handle(win, bbut, val);
423                         
424                         break;
425                 }
426         
427                 case GHOST_kEventKeyDown:
428                 case GHOST_kEventKeyUp: {
429                         GHOST_TEventKeyData *kd= data;
430                         int val= (type==GHOST_kEventKeyDown);
431                         int bkey= convert_key(kd->key);
432
433                         if (kd->key == GHOST_kKeyCommand) {
434                                 win->commandqual= val;
435                         }
436
437                         if (bkey) {
438                                 if (bkey==LEFTSHIFTKEY || bkey==RIGHTSHIFTKEY) {
439                                         win->lqual= change_bit(win->lqual, LR_SHIFTKEY, val);
440                                 } else if (bkey==LEFTCTRLKEY || bkey==RIGHTCTRLKEY) {
441                                         win->lqual= change_bit(win->lqual, LR_CTRLKEY, val);
442                                 } else if (bkey==LEFTALTKEY || bkey==RIGHTALTKEY) {
443                                         win->lqual= change_bit(win->lqual, LR_ALTKEY, val);
444                                 }
445
446                                 window_handle_ext(win, bkey, val, kd->ascii);
447                         }
448                         
449                         break;
450                 }
451
452                 case GHOST_kEventWheel: {
453                         GHOST_TEventWheelData* wheelData = (GHOST_TEventWheelData*) data;
454                         if (wheelData->z > 0) {
455                                 window_handle(win, WHEELUPMOUSE, 1);
456                         } else {
457                                 window_handle(win, WHEELDOWNMOUSE, 1);
458                         }
459                         break;
460                 }
461
462                 case GHOST_kEventWindowDeactivate:
463                 case GHOST_kEventWindowActivate: {
464                         win->active= (type==GHOST_kEventWindowActivate);
465                         window_handle(win, INPUTCHANGE, win->active);
466                         
467                         if (win->active) {
468                                 if ((win->lqual & LR_SHIFTKEY) && !query_qual('s')) {
469                                         win->lqual= change_bit(win->lqual, LR_SHIFTKEY, 0);
470                                         window_handle(win, LEFTSHIFTKEY, 0);
471                                 }
472                                 if ((win->lqual & LR_CTRLKEY) && !query_qual('c')) {
473                                         win->lqual= change_bit(win->lqual, LR_CTRLKEY, 0);
474                                         window_handle(win, LEFTCTRLKEY, 0);
475                                 }
476                                 if ((win->lqual & LR_ALTKEY) && !query_qual('a')) {
477                                         win->lqual= change_bit(win->lqual, LR_ALTKEY, 0);
478                                         window_handle(win, LEFTALTKEY, 0);
479                                 }
480                                 win->commandqual= query_qual('C');
481
482                                 /* 
483                                  * XXX quick hack so OSX version works better
484                                  * when the window is clicked on (focused). 
485                                  */
486                                 window_handle(win, MOUSEX, win->lmouse[0]);
487                                 window_handle(win, MOUSEY, win->lmouse[1]);
488                         }
489                         
490                         break;
491                 }
492                 case GHOST_kEventWindowClose: {
493                         window_handle(win, WINCLOSE, 1);
494                         break;
495                 }
496                 case GHOST_kEventWindowUpdate: {
497                         window_handle(win, REDRAW, 1);
498                         break;
499                 }
500                 case GHOST_kEventWindowSize: {
501                         GHOST_RectangleHandle client_rect;
502                         int l, t, r, b, scr_w, scr_h;
503
504                         client_rect= GHOST_GetClientBounds(win->ghostwin);
505                         GHOST_GetRectangle(client_rect, &l, &t, &r, &b);
506                         
507                         GHOST_DisposeRectangle(client_rect);
508                         
509                         winlay_get_screensize(&scr_w, &scr_h);
510                         win->position[0]= l;
511                         win->position[1]= scr_h - b - 1;
512                         win->size[0]= r-l;
513                         win->size[1]= b-t;
514
515                         window_handle(win, RESHAPE, 1);
516                         break;
517                 }
518                 }
519         }
520
521         return 1;
522 }
523
524 char *window_get_title(Window *win) {
525         char *title= GHOST_GetTitle(win->ghostwin);
526         char *mem_title= BLI_strdup(title);
527         free(title);
528
529         return mem_title;
530 }
531
532 void window_set_title(Window *win, char *title) {
533         GHOST_SetTitle(win->ghostwin, title);
534 }
535
536 short window_get_qual(Window *win) {
537         return win->lqual;
538 }
539
540 short window_get_mbut(Window *win) {
541         return win->lmbut;
542 }
543
544 void window_get_mouse(Window *win, short *mval) {
545         mval[0]= win->lmouse[0];
546         mval[1]= win->lmouse[1];
547 }
548
549 void window_get_position(Window *win, int *posx_r, int *posy_r) {
550         *posx_r= win->position[0];
551         *posy_r= win->position[1];
552 }
553
554 void window_get_size(Window *win, int *width_r, int *height_r) {
555         *width_r= win->size[0];
556         *height_r= win->size[1];
557 }
558
559 void window_set_size(Window *win, int width, int height) {
560         GHOST_SetClientSize(win->ghostwin, width, height);
561 }
562
563 void window_lower(Window *win) {
564         GHOST_SetWindowOrder(win->ghostwin, GHOST_kWindowOrderBottom);
565 }
566
567 void window_raise(Window *win) {
568         GHOST_SetWindowOrder(win->ghostwin, GHOST_kWindowOrderTop);
569 }
570
571 void window_warp_pointer(Window *win, int x, int y) {
572         y= win->size[1] - y - 1;
573         GHOST_ClientToScreen(win->ghostwin, x, y, &x, &y);
574         GHOST_SetCursorPosition(g_system, x, y);
575 }
576
577 void window_queue_redraw(Window *win) {
578         GHOST_InvalidateWindow(win->ghostwin);
579 }
580
581 /***/
582
583 void winlay_process_events(int wait_for_event) {
584         GHOST_ProcessEvents(g_system, wait_for_event);
585         GHOST_DispatchEvents(g_system);
586 }
587
588 void winlay_get_screensize(int *width_r, int *height_r) {
589         unsigned int uiwidth;
590         unsigned int uiheight;
591         
592         if (!g_system) {
593                 GHOST_EventConsumerHandle consumer= GHOST_CreateEventConsumer(event_proc, NULL);
594         
595                 g_system= GHOST_CreateSystem();
596                 GHOST_AddEventConsumer(g_system, consumer);
597         }
598         
599         GHOST_GetMainDisplayDimensions(g_system, &uiwidth, &uiheight);
600         *width_r= uiwidth;
601         *height_r= uiheight;
602 }
603
604 Window *winlay_get_active_window(void) {
605         return active_gl_window;
606 }