migrated NDOF code from soc-2010-merwin, SpaceNavigator now works on Mac blender
[blender.git] / source / blender / windowmanager / intern / wm.c
1 /*
2  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2007 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file blender/windowmanager/intern/wm.c
30  *  \ingroup wm
31  */
32
33
34 #include <string.h>
35 #include <stddef.h>
36
37 #include "BLO_sys_types.h"
38
39 #include "DNA_windowmanager_types.h"
40
41 #include "GHOST_C-api.h"
42
43 #include "BLI_blenlib.h"
44
45 #include "BKE_blender.h"
46 #include "BKE_context.h"
47 #include "BKE_idprop.h"
48 #include "BKE_library.h"
49 #include "BKE_main.h"
50 #include "BKE_screen.h"
51 #include "BKE_report.h"
52 #include "BKE_global.h"
53
54 #include "WM_api.h"
55 #include "WM_types.h"
56 #include "wm_window.h"
57 #include "wm_event_system.h"
58 #include "wm_event_types.h"
59 #include "wm_draw.h"
60 #include "wm.h"
61
62 #include "MEM_guardedalloc.h"
63
64 #include "ED_screen.h"
65
66 #ifdef WITH_PYTHON
67 #include "BPY_extern.h"
68 #endif
69
70 /* ****************************************************** */
71
72 #define MAX_OP_REGISTERED       32
73
74 void WM_operator_free(wmOperator *op)
75 {
76
77 #ifdef WITH_PYTHON
78         if(op->py_instance) {
79                 /* do this first incase there are any __del__ functions or
80                  * similar that use properties */
81                 BPY_DECREF(op->py_instance);
82         }
83 #endif
84
85         if(op->ptr) {
86                 op->properties= op->ptr->data;
87                 MEM_freeN(op->ptr);
88         }
89
90         if(op->properties) {
91                 IDP_FreeProperty(op->properties);
92                 MEM_freeN(op->properties);
93         }
94
95         if(op->reports && (op->reports->flag & RPT_FREE)) {
96                 BKE_reports_clear(op->reports);
97                 MEM_freeN(op->reports);
98         }
99
100         if(op->macro.first) {
101                 wmOperator *opm, *opmnext;
102                 for(opm= op->macro.first; opm; opm= opmnext) {
103                         opmnext = opm->next;
104                         WM_operator_free(opm);
105                 }
106         }
107         
108         MEM_freeN(op);
109 }
110
111 static void wm_reports_free(wmWindowManager *wm)
112 {
113         BKE_reports_clear(&wm->reports);
114         WM_event_remove_timer(wm, NULL, wm->reports.reporttimer);
115 }
116
117 /* all operations get registered in the windowmanager here */
118 /* called on event handling by event_system.c */
119 void wm_operator_register(bContext *C, wmOperator *op)
120 {
121         wmWindowManager *wm= CTX_wm_manager(C);
122         int tot;
123
124         BLI_addtail(&wm->operators, op);
125         tot= BLI_countlist(&wm->operators);
126         
127         while(tot>MAX_OP_REGISTERED) {
128                 wmOperator *opt= wm->operators.first;
129                 BLI_remlink(&wm->operators, opt);
130                 WM_operator_free(opt);
131                 tot--;
132         }
133         
134         /* so the console is redrawn */
135         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_INFO_REPORT, NULL);
136         WM_event_add_notifier(C, NC_WM|ND_HISTORY, NULL);
137 }
138
139
140 void WM_operator_stack_clear(wmWindowManager *wm)
141 {
142         wmOperator *op;
143         
144         while((op= wm->operators.first)) {
145                 BLI_remlink(&wm->operators, op);
146                 WM_operator_free(op);
147         }
148         
149         WM_main_add_notifier(NC_WM|ND_HISTORY, NULL);
150 }
151
152 /* ****************************************** */
153
154 static ListBase menutypes = {NULL, NULL}; /* global menutype list */
155
156 MenuType *WM_menutype_find(const char *idname, int quiet)
157 {
158         MenuType* mt;
159
160         if (idname[0]) {
161                 mt= BLI_findstring(&menutypes, idname, offsetof(MenuType, idname));
162                 if(mt)
163                         return mt;
164         }
165
166         if(!quiet)
167                 printf("search for unknown menutype %s\n", idname);
168
169         return NULL;
170 }
171
172 int WM_menutype_add(MenuType* mt)
173 {
174         BLI_addtail(&menutypes, mt);
175         return 1;
176 }
177
178 void WM_menutype_freelink(MenuType* mt)
179 {
180         BLI_freelinkN(&menutypes, mt);
181 }
182
183 void WM_menutype_free(void)
184 {
185         MenuType* mt= menutypes.first, *mt_next;
186
187         while(mt) {
188                 mt_next= mt->next;
189
190                 if(mt->ext.free)
191                         mt->ext.free(mt->ext.data);
192
193                 WM_menutype_freelink(mt);
194
195                 mt= mt_next;
196         }
197 }
198
199 /* ****************************************** */
200
201 void WM_keymap_init(bContext *C)
202 {
203         wmWindowManager *wm= CTX_wm_manager(C);
204
205         if(!wm->defaultconf)
206                 wm->defaultconf= WM_keyconfig_new(wm, "Blender");
207         
208         if(wm && CTX_py_init_get(C) && (wm->initialized & WM_INIT_KEYMAP) == 0) {
209                 /* create default key config */
210                 wm_window_keymap(wm->defaultconf);
211                 ED_spacetypes_keymap(wm->defaultconf);
212                 WM_keyconfig_userdef();
213
214                 wm->initialized |= WM_INIT_KEYMAP;
215         }
216 }
217
218 void WM_check(bContext *C)
219 {
220         wmWindowManager *wm= CTX_wm_manager(C);
221         
222         /* wm context */
223         if(wm==NULL) {
224                 wm= CTX_data_main(C)->wm.first;
225                 CTX_wm_manager_set(C, wm);
226         }
227         if(wm==NULL) return;
228         if(wm->windows.first==NULL) return;
229
230         if (!G.background) {
231                 /* case: fileread */
232                 if((wm->initialized & WM_INIT_WINDOW) == 0) {
233                         WM_keymap_init(C);
234                         WM_autosave_init(wm);
235                 }
236
237                 /* case: no open windows at all, for old file reads */
238                 wm_window_add_ghostwindows(C, wm);
239         }
240
241         /* case: fileread */
242         /* note: this runs in bg mode to set the screen context cb */
243         if((wm->initialized & WM_INIT_WINDOW) == 0) {
244                 ED_screens_initialize(wm);
245                 wm->initialized |= WM_INIT_WINDOW;
246         }
247 }
248
249 void wm_clear_default_size(bContext *C)
250 {
251         wmWindowManager *wm= CTX_wm_manager(C);
252         wmWindow *win;
253         
254         /* wm context */
255         if(wm==NULL) {
256                 wm= CTX_data_main(C)->wm.first;
257                 CTX_wm_manager_set(C, wm);
258         }
259         if(wm==NULL) return;
260         if(wm->windows.first==NULL) return;
261         
262         for(win= wm->windows.first; win; win= win->next) {
263                 win->sizex = 0;
264                 win->sizey = 0;
265                 win->posx = 0;
266                 win->posy = 0;
267         }
268
269 }
270
271 /* on startup, it adds all data, for matching */
272 void wm_add_default(bContext *C)
273 {
274         wmWindowManager *wm= alloc_libblock(&CTX_data_main(C)->wm, ID_WM, "WinMan");
275         wmWindow *win;
276         bScreen *screen= CTX_wm_screen(C); /* XXX from file read hrmf */
277         
278         CTX_wm_manager_set(C, wm);
279         win= wm_window_new(C);
280         win->screen= screen;
281         screen->winid= win->winid;
282         BLI_strncpy(win->screenname, screen->id.name+2, sizeof(win->screenname));
283         
284         wm->winactive= win;
285         wm->file_saved= 1;
286         wm_window_make_drawable(C, win); 
287 }
288
289
290 /* context is allowed to be NULL, do not free wm itself (library.c) */
291 void wm_close_and_free(bContext *C, wmWindowManager *wm)
292 {
293         wmWindow *win;
294         wmOperator *op;
295         wmKeyConfig *keyconf;
296
297         if(wm->autosavetimer)
298                 wm_autosave_timer_ended(wm);
299
300         while((win= wm->windows.first)) {
301                 BLI_remlink(&wm->windows, win);
302                 win->screen= NULL; /* prevent draw clear to use screen */
303                 wm_draw_window_clear(win);
304                 wm_window_free(C, wm, win);
305         }
306         
307         while((op= wm->operators.first)) {
308                 BLI_remlink(&wm->operators, op);
309                 WM_operator_free(op);
310         }
311
312         while((keyconf=wm->keyconfigs.first)) {
313                 BLI_remlink(&wm->keyconfigs, keyconf);
314                 WM_keyconfig_free(keyconf);
315         }
316
317         BLI_freelistN(&wm->queue);
318         
319         BLI_freelistN(&wm->paintcursors);
320         BLI_freelistN(&wm->drags);
321         
322         wm_reports_free(wm);
323         
324         if(C && CTX_wm_manager(C)==wm) CTX_wm_manager_set(C, NULL);
325 }
326
327 void wm_close_and_free_all(bContext *C, ListBase *wmlist)
328 {
329         wmWindowManager *wm;
330         
331         while((wm=wmlist->first)) {
332                 wm_close_and_free(C, wm);
333                 BLI_remlink(wmlist, wm);
334                 MEM_freeN(wm);
335         }
336 }
337
338 void WM_main(bContext *C)
339 {
340         while(1) {
341                 
342                 /* get events from ghost, handle window events, add to window queues */
343                 wm_window_process_events(C); 
344                 
345                 /* per window, all events to the window, screen, area and region handlers */
346                 wm_event_do_handlers(C);
347                 
348                 /* events have left notes about changes, we handle and cache it */
349                 wm_event_do_notifiers(C);
350                 
351                 /* execute cached changes draw */
352                 wm_draw_update(C);
353         }
354 }
355
356