Merge branch 'master' into blender2.8
[blender.git] / source / blender / windowmanager / intern / wm.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2007 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/windowmanager/intern/wm.c
28  *  \ingroup wm
29  *
30  * Internal functions for managing UI registrable types (operator, UI and menu types)
31  *
32  * Also Blenders main event loop (WM_main)
33  */
34
35 #include <string.h>
36 #include <stddef.h>
37
38 #include "BLI_sys_types.h"
39
40 #include "DNA_windowmanager_types.h"
41
42 #include "MEM_guardedalloc.h"
43
44 #include "BLI_utildefines.h"
45 #include "BLI_blenlib.h"
46 #include "BLI_ghash.h"
47
48 #include "BKE_context.h"
49 #include "BKE_idprop.h"
50 #include "BKE_library.h"
51 #include "BKE_main.h"
52 #include "BKE_screen.h"
53 #include "BKE_report.h"
54 #include "BKE_global.h"
55
56 #include "WM_api.h"
57 #include "WM_types.h"
58 #include "wm_window.h"
59 #include "wm_event_system.h"
60 #include "wm_draw.h"
61 #include "wm.h"
62
63 #include "ED_screen.h"
64
65 #ifdef WITH_PYTHON
66 #include "BPY_extern.h"
67 #endif
68
69 /* ****************************************************** */
70
71 #define MAX_OP_REGISTERED   32
72
73 void WM_operator_free(wmOperator *op)
74 {
75
76 #ifdef WITH_PYTHON
77         if (op->py_instance) {
78                 /* do this first in case there are any __del__ functions or
79                  * similar that use properties */
80                 BPY_DECREF_RNA_INVALIDATE(op->py_instance);
81         }
82 #endif
83
84         if (op->ptr) {
85                 op->properties = op->ptr->data;
86                 MEM_freeN(op->ptr);
87         }
88
89         if (op->properties) {
90                 IDP_FreeProperty(op->properties);
91                 MEM_freeN(op->properties);
92         }
93
94         if (op->reports && (op->reports->flag & RPT_FREE)) {
95                 BKE_reports_clear(op->reports);
96                 MEM_freeN(op->reports);
97         }
98
99         if (op->macro.first) {
100                 wmOperator *opm, *opmnext;
101                 for (opm = op->macro.first; opm; opm = opmnext) {
102                         opmnext = opm->next;
103                         WM_operator_free(opm);
104                 }
105         }
106         
107         MEM_freeN(op);
108 }
109
110 void WM_operator_free_all_after(wmWindowManager *wm, struct wmOperator *op)
111 {
112         op = op->next;
113         while (op != NULL) {
114                 wmOperator *op_next = op->next;
115                 BLI_remlink(&wm->operators, op);
116                 WM_operator_free(op);
117                 op = op_next;
118         }
119 }
120
121 /**
122  * Use with extreme care!,
123  * properties, customdata etc - must be compatible.
124  *
125  * \param op  Operator to assign the type to.
126  * \param ot  OperatorType to assign.
127  */
128 void WM_operator_type_set(wmOperator *op, wmOperatorType *ot)
129 {
130         /* not supported for Python */
131         BLI_assert(op->py_instance == NULL);
132
133         op->type = ot;
134         op->ptr->type = ot->srna;
135
136         /* ensure compatible properties */
137         if (op->properties) {
138                 PointerRNA ptr;
139
140                 WM_operator_properties_create_ptr(&ptr, ot);
141
142                 WM_operator_properties_default(&ptr, false);
143
144                 if (ptr.data) {
145                         IDP_SyncGroupTypes(op->properties, ptr.data, true);
146                 }
147
148                 WM_operator_properties_free(&ptr);
149         }
150 }
151
152 static void wm_reports_free(wmWindowManager *wm)
153 {
154         BKE_reports_clear(&wm->reports);
155         WM_event_remove_timer(wm, NULL, wm->reports.reporttimer);
156 }
157
158 /* all operations get registered in the windowmanager here */
159 /* called on event handling by event_system.c */
160 void wm_operator_register(bContext *C, wmOperator *op)
161 {
162         wmWindowManager *wm = CTX_wm_manager(C);
163         int tot = 0;
164
165         BLI_addtail(&wm->operators, op);
166
167         /* only count registered operators */
168         while (op) {
169                 wmOperator *op_prev = op->prev;
170                 if (op->type->flag & OPTYPE_REGISTER) {
171                         tot += 1;
172                 }
173                 if (tot > MAX_OP_REGISTERED) {
174                         BLI_remlink(&wm->operators, op);
175                         WM_operator_free(op);
176                 }
177                 op = op_prev;
178         }
179
180         /* so the console is redrawn */
181         WM_event_add_notifier(C, NC_SPACE | ND_SPACE_INFO_REPORT, NULL);
182         WM_event_add_notifier(C, NC_WM | ND_HISTORY, NULL);
183 }
184
185
186 void WM_operator_stack_clear(wmWindowManager *wm)
187 {
188         wmOperator *op;
189         
190         while ((op = BLI_pophead(&wm->operators))) {
191                 WM_operator_free(op);
192         }
193         
194         WM_main_add_notifier(NC_WM | ND_HISTORY, NULL);
195 }
196
197 /**
198  * This function is needed in the case when an addon id disabled
199  * while a modal operator it defined is running.
200  */
201 void WM_operator_handlers_clear(wmWindowManager *wm, wmOperatorType *ot)
202 {
203         wmWindow *win;
204         for (win = wm->windows.first; win; win = win->next) {
205                 ListBase *lb[2] = {&win->handlers, &win->modalhandlers};
206                 wmEventHandler *handler;
207                 int i;
208
209                 for (i = 0; i < 2; i++) {
210                         for (handler = lb[i]->first; handler; handler = handler->next) {
211                                 if (handler->op && handler->op->type == ot) {
212                                         /* don't run op->cancel because it needs the context,
213                                          * assume whoever unregisters the operator will cleanup */
214                                         handler->flag |= WM_HANDLER_DO_FREE;
215                                         WM_operator_free(handler->op);
216                                         handler->op = NULL;
217                                 }
218                         }
219                 }
220         }
221 }
222
223 /* ************ uiListType handling ************** */
224
225 static GHash *uilisttypes_hash = NULL;
226
227 uiListType *WM_uilisttype_find(const char *idname, bool quiet)
228 {
229         uiListType *ult;
230
231         if (idname[0]) {
232                 ult = BLI_ghash_lookup(uilisttypes_hash, idname);
233                 if (ult) {
234                         return ult;
235                 }
236         }
237
238         if (!quiet) {
239                 printf("search for unknown uilisttype %s\n", idname);
240         }
241
242         return NULL;
243 }
244
245 bool WM_uilisttype_add(uiListType *ult)
246 {
247         BLI_ghash_insert(uilisttypes_hash, ult->idname, ult);
248         return 1;
249 }
250
251 void WM_uilisttype_freelink(uiListType *ult)
252 {
253         bool ok;
254
255         ok = BLI_ghash_remove(uilisttypes_hash, ult->idname, NULL, MEM_freeN);
256
257         BLI_assert(ok);
258         (void)ok;
259 }
260
261 /* called on initialize WM_init() */
262 void WM_uilisttype_init(void)
263 {
264         uilisttypes_hash = BLI_ghash_str_new_ex("uilisttypes_hash gh", 16);
265 }
266
267 void WM_uilisttype_free(void)
268 {
269         GHashIterator gh_iter;
270
271         GHASH_ITER (gh_iter, uilisttypes_hash) {
272                 uiListType *ult = BLI_ghashIterator_getValue(&gh_iter);
273                 if (ult->ext.free) {
274                         ult->ext.free(ult->ext.data);
275                 }
276         }
277
278         BLI_ghash_free(uilisttypes_hash, NULL, MEM_freeN);
279         uilisttypes_hash = NULL;
280 }
281
282 /* ************ MenuType handling ************** */
283
284 static GHash *menutypes_hash = NULL;
285
286 MenuType *WM_menutype_find(const char *idname, bool quiet)
287 {
288         MenuType *mt;
289
290         if (idname[0]) {
291                 mt = BLI_ghash_lookup(menutypes_hash, idname);
292                 if (mt)
293                         return mt;
294         }
295
296         if (!quiet)
297                 printf("search for unknown menutype %s\n", idname);
298
299         return NULL;
300 }
301
302 bool WM_menutype_add(MenuType *mt)
303 {
304         BLI_ghash_insert(menutypes_hash, mt->idname, mt);
305         return true;
306 }
307
308 void WM_menutype_freelink(MenuType *mt)
309 {
310         bool ok;
311
312         ok = BLI_ghash_remove(menutypes_hash, mt->idname, NULL, MEM_freeN);
313
314         BLI_assert(ok);
315         (void)ok;
316 }
317
318 /* called on initialize WM_init() */
319 void WM_menutype_init(void)
320 {
321         /* reserve size is set based on blender default setup */
322         menutypes_hash = BLI_ghash_str_new_ex("menutypes_hash gh", 512);
323 }
324
325 void WM_menutype_free(void)
326 {
327         GHashIterator gh_iter;
328
329         GHASH_ITER (gh_iter, menutypes_hash) {
330                 MenuType *mt = BLI_ghashIterator_getValue(&gh_iter);
331                 if (mt->ext.free) {
332                         mt->ext.free(mt->ext.data);
333                 }
334         }
335
336         BLI_ghash_free(menutypes_hash, NULL, MEM_freeN);
337         menutypes_hash = NULL;
338 }
339
340 /* ****************************************** */
341
342 void WM_keymap_init(bContext *C)
343 {
344         wmWindowManager *wm = CTX_wm_manager(C);
345
346         /* create standard key configs */
347         if (!wm->defaultconf)
348                 wm->defaultconf = WM_keyconfig_new(wm, "Blender");
349         if (!wm->addonconf)
350                 wm->addonconf = WM_keyconfig_new(wm, "Blender Addon");
351         if (!wm->userconf)
352                 wm->userconf = WM_keyconfig_new(wm, "Blender User");
353         
354         /* initialize only after python init is done, for keymaps that
355          * use python operators */
356         if (CTX_py_init_get(C) && (wm->initialized & WM_INIT_KEYMAP) == 0) {
357                 /* create default key config, only initialize once,
358                  * it's persistent across sessions */
359                 if (!(wm->defaultconf->flag & KEYCONF_INIT_DEFAULT)) {
360                         wm_window_keymap(wm->defaultconf);
361                         ED_spacetypes_keymap(wm->defaultconf);
362
363                         wm->defaultconf->flag |= KEYCONF_INIT_DEFAULT;
364                 }
365
366                 WM_keyconfig_update_tag(NULL, NULL);
367                 WM_keyconfig_update(wm);
368
369                 wm->initialized |= WM_INIT_KEYMAP;
370         }
371 }
372
373 void WM_check(bContext *C)
374 {
375         wmWindowManager *wm = CTX_wm_manager(C);
376         
377         /* wm context */
378         if (wm == NULL) {
379                 wm = CTX_data_main(C)->wm.first;
380                 CTX_wm_manager_set(C, wm);
381         }
382
383         if (wm == NULL || BLI_listbase_is_empty(&wm->windows)) {
384                 return;
385         }
386
387         if (!G.background) {
388                 /* case: fileread */
389                 if ((wm->initialized & WM_INIT_WINDOW) == 0) {
390                         WM_keymap_init(C);
391                         WM_autosave_init(wm);
392                 }
393
394                 /* case: no open windows at all, for old file reads */
395                 wm_window_ghostwindows_ensure(wm);
396         }
397
398         /* case: fileread */
399         /* note: this runs in bg mode to set the screen context cb */
400         if ((wm->initialized & WM_INIT_WINDOW) == 0) {
401                 ED_screens_initialize(wm);
402                 wm->initialized |= WM_INIT_WINDOW;
403         }
404 }
405
406 void wm_clear_default_size(bContext *C)
407 {
408         wmWindowManager *wm = CTX_wm_manager(C);
409         wmWindow *win;
410         
411         /* wm context */
412         if (wm == NULL) {
413                 wm = CTX_data_main(C)->wm.first;
414                 CTX_wm_manager_set(C, wm);
415         }
416
417         if (wm == NULL || BLI_listbase_is_empty(&wm->windows)) {
418                 return;
419         }
420         
421         for (win = wm->windows.first; win; win = win->next) {
422                 win->sizex = 0;
423                 win->sizey = 0;
424                 win->posx = 0;
425                 win->posy = 0;
426         }
427
428 }
429
430 /* on startup, it adds all data, for matching */
431 void wm_add_default(bContext *C)
432 {
433         wmWindowManager *wm = BKE_libblock_alloc(CTX_data_main(C), ID_WM, "WinMan");
434         wmWindow *win;
435         bScreen *screen = CTX_wm_screen(C); /* XXX from file read hrmf */
436         struct WorkSpace *workspace = G.main->workspaces.last;
437
438         CTX_wm_manager_set(C, wm);
439         win = wm_window_new(C);
440         WM_window_set_active_workspace(win, workspace);
441         WM_window_set_active_screen(win, workspace, screen);
442         screen->winid = win->winid;
443         BLI_strncpy(win->screenname, screen->id.name + 2, sizeof(win->screenname));
444         
445         wm->winactive = win;
446         wm->file_saved = 1;
447         wm_window_make_drawable(wm, win); 
448 }
449
450
451 /* context is allowed to be NULL, do not free wm itself (library.c) */
452 void wm_close_and_free(bContext *C, wmWindowManager *wm)
453 {
454         wmWindow *win;
455         wmOperator *op;
456         wmKeyConfig *keyconf;
457
458         if (wm->autosavetimer)
459                 wm_autosave_timer_ended(wm);
460
461         while ((win = BLI_pophead(&wm->windows))) {
462                 WM_window_set_active_workspace(win, NULL); /* prevent draw clear to use screen */
463                 wm_draw_window_clear(win);
464                 wm_window_free(C, wm, win);
465         }
466         
467         while ((op = BLI_pophead(&wm->operators))) {
468                 WM_operator_free(op);
469         }
470
471         while ((keyconf = BLI_pophead(&wm->keyconfigs))) {
472                 WM_keyconfig_free(keyconf);
473         }
474
475         BLI_freelistN(&wm->queue);
476         
477         BLI_freelistN(&wm->paintcursors);
478
479         WM_drag_free_list(&wm->drags);
480         
481         wm_reports_free(wm);
482         
483         if (C && CTX_wm_manager(C) == wm) CTX_wm_manager_set(C, NULL);
484 }
485
486 void wm_close_and_free_all(bContext *C, ListBase *wmlist)
487 {
488         wmWindowManager *wm;
489
490         while ((wm = wmlist->first)) {
491                 wm_close_and_free(C, wm);
492                 BLI_remlink(wmlist, wm);
493                 BKE_libblock_free_data(&wm->id, true);
494                 MEM_freeN(wm);
495         }
496 }
497
498 void WM_main(bContext *C)
499 {
500         while (1) {
501                 
502                 /* get events from ghost, handle window events, add to window queues */
503                 wm_window_process_events(C); 
504                 
505                 /* per window, all events to the window, screen, area and region handlers */
506                 wm_event_do_handlers(C);
507                 
508                 /* events have left notes about changes, we handle and cache it */
509                 wm_event_do_notifiers(C);
510                 
511                 /* execute cached changes draw */
512                 wm_draw_update(C);
513         }
514 }
515
516