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