Windowmanager, ghost side:
[blender-staging.git] / source / blender / windowmanager / intern / wm_window.c
1 /**
2  * $Id: wm_window.c
3  *
4  * ***** BEGIN GPL 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. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2007 Blender Foundation but based 
21  * on ghostwinlay.c (C) 2001-2002 by NaN Holding BV
22  * All rights reserved.
23  *
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <stdlib.h>
30 #include <stdio.h>
31
32 #include "DNA_listBase.h"       
33 #include "DNA_screen_types.h"
34 #include "DNA_windowmanager_types.h"
35
36 #include "MEM_guardedalloc.h"
37
38 #include "GHOST_C-api.h"
39
40 #include "BLI_blenlib.h"
41
42 #include "BKE_blender.h"
43 #include "BKE_global.h"
44 #include "BKE_utildefines.h"
45
46 #include "BIF_gl.h"
47
48 #include "WM_api.h"
49 #include "wm.h"
50 #include "wm_window.h"
51 #include "wm_event_system.h"
52
53 /* the global to talk to ghost */
54 GHOST_SystemHandle g_system= NULL;
55
56 /* set by commandline */
57 static int prefsizx= 0, prefsizy= 0, prefstax= 0, prefstay= 0;
58
59
60 /* ******** win open & close ************ */
61
62
63 static void wm_get_screensize(int *width_r, int *height_r) 
64 {
65         unsigned int uiwidth;
66         unsigned int uiheight;
67         
68         GHOST_GetMainDisplayDimensions(g_system, &uiwidth, &uiheight);
69         *width_r= uiwidth;
70         *height_r= uiheight;
71 }
72
73 static void wm_ghostwindow_destroy(wmWindow *win) 
74 {
75         
76         if (win->timer) {
77                 GHOST_RemoveTimer(g_system, (GHOST_TimerTaskHandle)win->timer);
78                 win->timer= NULL;
79         }
80         
81         if(win->ghostwin) {
82                 GHOST_DisposeWindow(g_system, win->ghostwin);
83                 win->ghostwin= NULL;
84         }
85 }
86
87 /* including window itself */
88 void wm_window_free(bContext *C, wmWindow *win)
89 {
90         
91         /* update context */
92         if(C) {
93                 if(C->wm->windrawable==win)
94                         C->wm->windrawable= NULL;
95                 if(C->wm->winactive==win)
96                         C->wm->winactive= NULL;
97                 if(C->window==win)
98                         C->window= NULL;
99                 if(C->screen==win->screen)
100                         C->screen= NULL;
101         }       
102         /* XXX free screens */
103         
104         if(win->eventstate) MEM_freeN(win->eventstate);
105         
106         BLI_freelistN(&win->queue);
107         wm_event_free_handlers(&win->handlers);
108         
109         wm_event_free_all(win);
110         wm_ghostwindow_destroy(win);
111         
112         MEM_freeN(win);
113 }
114
115 static int find_free_winid(wmWindowManager *wm)
116 {
117         wmWindow *win;
118         int id= 0;
119         
120         for(win= wm->windows.first; win; win= win->next)
121                 if(id <= win->winid)
122                         id= win->winid+1;
123         
124         return id;
125 }
126
127 /* dont change context itself */
128 wmWindow *wm_window_new(bContext *C, bScreen *screen)
129 {
130         wmWindow *win= MEM_callocN(sizeof(wmWindow), "window");
131         
132         BLI_addtail(&C->wm->windows, win);
133         win->winid= find_free_winid(C->wm);
134
135         win->screen= screen;
136         return win;
137 }
138
139 /* part of wm_window.c api */
140 wmWindow *wm_window_copy(bContext *C, wmWindow *winorig)
141 {
142         wmWindow *win= wm_window_new(C, winorig->screen); /* XXX need copy */
143         
144         win->posx= winorig->posx+10;
145         win->posy= winorig->posy;
146         win->sizex= winorig->sizex;
147         win->sizey= winorig->sizey;
148         
149         return win;
150 }
151
152 /* operator callback */
153 int wm_window_duplicate_op(bContext *C, wmOperator *op)
154 {
155         
156         wm_window_copy(C, C->window);
157         wm_check(C);
158         
159         return 1;
160 }
161
162 /* this is event from ghost */
163 static void wm_window_close(bContext *C, wmWindow *win)
164 {
165         BLI_remlink(&C->wm->windows, win);
166         wm_window_free(C, win);
167         
168         if(C->wm->windows.first==NULL)
169                 WM_exit(C);
170 }
171
172 static void wm_window_open(wmWindowManager *wm, char *title, wmWindow *win)
173 {
174         GHOST_WindowHandle ghostwin;
175         GHOST_TWindowState inital_state;
176         int scr_w, scr_h, posy;
177         
178         wm_get_screensize(&scr_w, &scr_h);
179         posy= (scr_h - win->posy - win->sizey);
180         
181         if (win->windowstate == G_WINDOWSTATE_FULLSCREEN)
182                 inital_state = GHOST_kWindowStateFullScreen;
183         else if (win->windowstate == G_WINDOWSTATE_BORDER)
184                 inital_state = GHOST_kWindowStateMaximized;
185         else
186                 inital_state = GHOST_kWindowStateNormal;
187         
188 #ifdef __APPLE__
189         {
190                 extern int macPrefState; /* creator.c */
191                 inital_state += macPrefState;
192         }
193 #endif
194         
195         ghostwin= GHOST_CreateWindow(g_system, title, 
196                                                                  win->posx, posy, win->sizex, win->sizey, 
197                                                                  inital_state, 
198                                                                  GHOST_kDrawingContextTypeOpenGL,
199                                                                  0 /* no stereo */);
200         
201         if (ghostwin) {
202
203                 win->ghostwin= ghostwin;
204                 GHOST_SetWindowUserData(ghostwin, win); /* pointer back */
205                 
206                 if(win->eventstate==NULL)
207                         win->eventstate= MEM_callocN(sizeof(wmEvent), "window event state");
208                 
209                 /* add keymap handler (1 for all keys in map!) */
210                 WM_event_add_keymap_handler(&wm->windowkeymap, &win->handlers);
211                 
212                 /* until screens get drawn, make it nice grey */
213                 glClearColor(.55, .55, .55, 0.0);
214                 glClear(GL_COLOR_BUFFER_BIT);
215                 wm_window_swap_buffers(win);
216         }
217         
218         
219 }
220
221 /* for wmWindows without ghostwin, open these and clear */
222 void wm_window_add_ghostwindows(wmWindowManager *wm)
223 {
224         wmWindow *win;
225         
226         /* no commandline prefsize? then we set this */
227         if (!prefsizx) {
228                 wm_get_screensize(&prefsizx, &prefsizy);
229                 
230 #ifdef __APPLE__
231                 extern void wm_set_apple_prefsize(int, int);    /* wm_apple.c */
232                 
233                 wm_set_apple_prefsize(prefsizx, prefsizy);
234 #else
235                 prefstax= 0;
236                 prefstay= 0;
237                 
238 #endif
239         }
240         
241         for(win= wm->windows.first; win; win= win->next) {
242                 if(win->ghostwin==NULL) {
243                         if(win->sizex==0) {
244                                 win->posx= prefstax;
245                                 win->posy= prefstay;
246                                 win->sizex= prefsizx;
247                                 win->sizey= prefsizy;
248                                 win->windowstate= G.windowstate;
249                         }
250                         wm_window_open(wm, "Blender", win);
251                 }
252         }
253 }
254
255 /* ************ events *************** */
256
257 static int query_qual(char qual) 
258 {
259         GHOST_TModifierKeyMask left, right;
260         int val= 0;
261         
262         if (qual=='s') {
263                 left= GHOST_kModifierKeyLeftShift;
264                 right= GHOST_kModifierKeyRightShift;
265         } else if (qual=='c') {
266                 left= GHOST_kModifierKeyLeftControl;
267                 right= GHOST_kModifierKeyRightControl;
268         } else if (qual=='C') {
269                 left= right= GHOST_kModifierKeyCommand;
270         } else {
271                 left= GHOST_kModifierKeyLeftAlt;
272                 right= GHOST_kModifierKeyRightAlt;
273         }
274         
275         GHOST_GetModifierKeyState(g_system, left, &val);
276         if (!val)
277                 GHOST_GetModifierKeyState(g_system, right, &val);
278         
279         return val;
280 }
281
282 void wm_window_make_drawable(bContext *C, wmWindow *win) 
283 {
284         if (win != C->window && win->ghostwin) {
285 //              win->lmbut= 0;  /* keeps hanging when mousepressed while other window opened */
286                 
287                 C->wm->windrawable= win;
288                 C->window= win;
289                 C->screen= win->screen;
290                 
291                 GHOST_ActivateWindowDrawingContext(win->ghostwin);
292         }
293 }
294
295 /* called by ghost, here we handle events for windows themselves or send to event system */
296 int ghost_event_proc(GHOST_EventHandle evt, GHOST_TUserDataPtr private) 
297 {
298         bContext *C= private;
299         GHOST_TEventType type= GHOST_GetEventType(evt);
300         
301         if (type == GHOST_kEventQuit) {
302                 WM_exit(C);
303         } else {
304                 GHOST_WindowHandle ghostwin= GHOST_GetEventWindow(evt);
305                 GHOST_TEventDataPtr data= GHOST_GetEventData(evt);
306                 wmWindow *win;
307                 
308                 if (!ghostwin) {
309                         // XXX - should be checked, why are we getting an event here, and
310                         //      what is it?
311                         
312                         return 1;
313                 } else if (!GHOST_ValidWindow(g_system, ghostwin)) {
314                         // XXX - should be checked, why are we getting an event here, and
315                         //      what is it?
316                         
317                         return 1;
318                 } else {
319                         win= GHOST_GetWindowUserData(ghostwin);
320                 }
321                 
322                 switch(type) {
323                         case GHOST_kEventWindowDeactivate:
324                                 win->active= 0; /* XXX */
325                                 break;
326                         case GHOST_kEventWindowActivate: 
327                         {
328                                 GHOST_TEventKeyData kdata;
329                                 int cx, cy, wx, wy;
330                                 
331                                 C->wm->winactive= win; /* no context change! c->window is drawable, or for area queues */
332                                 
333                                 win->active= 1;
334 //                              window_handle(win, INPUTCHANGE, win->active);
335                                 
336                                 /* bad ghost support for modifier keys... */
337                                 kdata.ascii= 0;
338                                 if (win->eventstate->shift && !query_qual('s')) {
339                                         kdata.key= GHOST_kKeyLeftShift;
340                                         wm_event_add_ghostevent(win, GHOST_kEventKeyUp, &kdata);
341                                 }
342                                 if (win->eventstate->ctrl && !query_qual('c')) {
343                                         kdata.key= GHOST_kKeyLeftControl;
344                                         wm_event_add_ghostevent(win, GHOST_kEventKeyUp, &kdata);
345                                 }
346                                 if (win->eventstate->alt && !query_qual('a')) {
347                                         kdata.key= GHOST_kKeyLeftAlt;
348                                         wm_event_add_ghostevent(win, GHOST_kEventKeyUp, &kdata);
349                                 }
350                                 if (win->eventstate->oskey && !query_qual('C')) {
351                                         kdata.key= GHOST_kKeyCommand;
352                                         wm_event_add_ghostevent(win, GHOST_kEventKeyUp, &kdata);
353                                 }
354                                 
355                                 /* entering window, update mouse pos. but no event */
356                                 GHOST_GetCursorPosition(g_system, &wx, &wy);
357                                 
358                                 GHOST_ScreenToClient(win->ghostwin, wx, wy, &cx, &cy);
359                                 win->eventstate->x= cx;
360                                 win->eventstate->y= (win->sizey-1) - cy;
361                                 
362                                 break;
363                         }
364                         case GHOST_kEventWindowClose: {
365                                 wm_window_close(C, win);
366                                 break;
367                         }
368                         case GHOST_kEventWindowUpdate: {
369 //                              window_handle(win, REDRAW, 1);
370                                 break;
371                         }
372                         case GHOST_kEventWindowSize:
373                         case GHOST_kEventWindowMove: {
374                                 GHOST_RectangleHandle client_rect;
375                                 int l, t, r, b, scr_w, scr_h;
376                                 
377                                 client_rect= GHOST_GetClientBounds(win->ghostwin);
378                                 GHOST_GetRectangle(client_rect, &l, &t, &r, &b);
379                                 
380                                 GHOST_DisposeRectangle(client_rect);
381                                 
382                                 wm_get_screensize(&scr_w, &scr_h);
383                                 win->sizex= r-l;
384                                 win->sizey= b-t;
385                                 win->posx= l;
386                                 win->posy= scr_h - t - win->sizey;
387
388                                 if(type!=GHOST_kEventWindowSize)
389                                         printf("win move event pos %d %d size %d %d\n", win->posx, win->posy, win->sizex, win->sizey);
390                                 
391 //                              window_handle(win, RESHAPE, 1);
392                                 break;
393                         }
394                         default:
395                                 wm_event_add_ghostevent(win, type, data);
396                                 break;
397                 }
398
399         }
400         return 1;
401 }
402
403 void wm_window_process_events(int wait_for_event) 
404 {
405         GHOST_ProcessEvents(g_system, wait_for_event);
406         GHOST_DispatchEvents(g_system);
407 }
408
409 /* **************** init ********************** */
410
411 void wm_ghost_init(bContext *C)
412 {
413         if (!g_system) {
414                 GHOST_EventConsumerHandle consumer= GHOST_CreateEventConsumer(ghost_event_proc, C);
415                 
416                 g_system= GHOST_CreateSystem();
417                 GHOST_AddEventConsumer(g_system, consumer);
418         }       
419 }
420
421 /* **************** timer ********************** */
422
423 static void window_timer_proc(GHOST_TimerTaskHandle timer, GHOST_TUns64 time)
424 {
425         wmWindow *win= GHOST_GetTimerTaskUserData(timer);
426         
427         wm_event_add_ghostevent(win, win->timer_event, NULL);
428 }
429
430 void wm_window_set_timer(wmWindow *win, int delay_ms, int event)
431 {
432         if (win->timer) GHOST_RemoveTimer(g_system, win->timer);
433         
434         win->timer_event= event;
435         win->timer= GHOST_InstallTimer(g_system, delay_ms, delay_ms, window_timer_proc, win);
436 }
437
438 /* ************************************ */
439
440 void wm_window_set_title(wmWindow *win, char *title) 
441 {
442         GHOST_SetTitle(win->ghostwin, title);
443 }
444
445 void wm_window_get_position(wmWindow *win, int *posx_r, int *posy_r) 
446 {
447         *posx_r= win->posx;
448         *posy_r= win->posy;
449 }
450
451 void wm_window_get_size(wmWindow *win, int *width_r, int *height_r) 
452 {
453         *width_r= win->sizex;
454         *height_r= win->sizey;
455 }
456
457 void wm_window_set_size(wmWindow *win, int width, int height) 
458 {
459         GHOST_SetClientSize(win->ghostwin, width, height);
460 }
461
462 void wm_window_lower(wmWindow *win) 
463 {
464         GHOST_SetWindowOrder(win->ghostwin, GHOST_kWindowOrderBottom);
465 }
466
467 void wm_window_raise(wmWindow *win) 
468 {
469         GHOST_SetWindowOrder(win->ghostwin, GHOST_kWindowOrderTop);
470 #ifdef _WIN32
471 //      markdirty_all(); /* to avoid redraw errors in fullscreen mode (aphex) */
472 #endif
473 }
474
475 void wm_window_swap_buffers(wmWindow *win)
476 {
477         GHOST_SwapWindowBuffers(win->ghostwin);
478 }
479
480 /* ******************* exported api ***************** */
481
482
483 /* called whem no ghost system was initialized */
484 void WM_setprefsize(int stax, int stay, int sizx, int sizy)
485 {
486         prefstax= stax;
487         prefstay= stay;
488         prefsizx= sizx;
489         prefsizy= sizy;
490 }
491