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