2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2007 Blender Foundation.
19 * All rights reserved.
22 * Contributor(s): Blender Foundation
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/windowmanager/intern/wm.c
30 * Internal functions for managing UI registrable types (operator, UI and menu types)
32 * Also Blenders main event loop (WM_main)
38 #include "BLI_sys_types.h"
40 #include "DNA_windowmanager_types.h"
42 #include "MEM_guardedalloc.h"
44 #include "BLI_utildefines.h"
45 #include "BLI_blenlib.h"
46 #include "BLI_ghash.h"
48 #include "BKE_context.h"
49 #include "BKE_idprop.h"
50 #include "BKE_library.h"
52 #include "BKE_screen.h"
53 #include "BKE_report.h"
54 #include "BKE_global.h"
58 #include "wm_window.h"
59 #include "wm_event_system.h"
63 #include "ED_screen.h"
66 #include "BPY_extern.h"
69 /* ****************************************************** */
71 #define MAX_OP_REGISTERED 32
73 void WM_operator_free(wmOperator *op)
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);
85 op->properties = op->ptr->data;
90 IDP_FreeProperty(op->properties);
91 MEM_freeN(op->properties);
94 if (op->reports && (op->reports->flag & RPT_FREE)) {
95 BKE_reports_clear(op->reports);
96 MEM_freeN(op->reports);
99 if (op->macro.first) {
100 wmOperator *opm, *opmnext;
101 for (opm = op->macro.first; opm; opm = opmnext) {
103 WM_operator_free(opm);
110 void WM_operator_free_all_after(wmWindowManager *wm, struct wmOperator *op)
114 wmOperator *op_next = op->next;
115 BLI_remlink(&wm->operators, op);
116 WM_operator_free(op);
122 * Use with extreme care!,
123 * properties, customdata etc - must be compatible.
125 * \param op Operator to assign the type to.
126 * \param ot OperatorType to assign.
128 void WM_operator_type_set(wmOperator *op, wmOperatorType *ot)
130 /* not supported for Python */
131 BLI_assert(op->py_instance == NULL);
134 op->ptr->type = ot->srna;
136 /* ensure compatible properties */
137 if (op->properties) {
140 WM_operator_properties_create_ptr(&ptr, ot);
142 WM_operator_properties_default(&ptr, false);
145 IDP_SyncGroupTypes(op->properties, ptr.data, true);
148 WM_operator_properties_free(&ptr);
152 static void wm_reports_free(wmWindowManager *wm)
154 BKE_reports_clear(&wm->reports);
155 WM_event_remove_timer(wm, NULL, wm->reports.reporttimer);
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)
162 wmWindowManager *wm = CTX_wm_manager(C);
165 BLI_addtail(&wm->operators, op);
167 /* only count registered operators */
169 wmOperator *op_prev = op->prev;
170 if (op->type->flag & OPTYPE_REGISTER) {
173 if (tot > MAX_OP_REGISTERED) {
174 BLI_remlink(&wm->operators, op);
175 WM_operator_free(op);
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);
186 void WM_operator_stack_clear(wmWindowManager *wm)
190 while ((op = BLI_pophead(&wm->operators))) {
191 WM_operator_free(op);
194 WM_main_add_notifier(NC_WM | ND_HISTORY, NULL);
198 * This function is needed in the case when an addon id disabled
199 * while a modal operator it defined is running.
201 void WM_operator_handlers_clear(wmWindowManager *wm, wmOperatorType *ot)
204 for (win = wm->windows.first; win; win = win->next) {
205 ListBase *lb[2] = {&win->handlers, &win->modalhandlers};
206 wmEventHandler *handler;
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);
223 /* ************ uiListType handling ************** */
225 static GHash *uilisttypes_hash = NULL;
227 uiListType *WM_uilisttype_find(const char *idname, bool quiet)
232 ult = BLI_ghash_lookup(uilisttypes_hash, idname);
239 printf("search for unknown uilisttype %s\n", idname);
245 bool WM_uilisttype_add(uiListType *ult)
247 BLI_ghash_insert(uilisttypes_hash, ult->idname, ult);
251 void WM_uilisttype_freelink(uiListType *ult)
255 ok = BLI_ghash_remove(uilisttypes_hash, ult->idname, NULL, MEM_freeN);
261 /* called on initialize WM_init() */
262 void WM_uilisttype_init(void)
264 uilisttypes_hash = BLI_ghash_str_new_ex("uilisttypes_hash gh", 16);
267 void WM_uilisttype_free(void)
269 GHashIterator gh_iter;
271 GHASH_ITER (gh_iter, uilisttypes_hash) {
272 uiListType *ult = BLI_ghashIterator_getValue(&gh_iter);
274 ult->ext.free(ult->ext.data);
278 BLI_ghash_free(uilisttypes_hash, NULL, MEM_freeN);
279 uilisttypes_hash = NULL;
282 /* ************ MenuType handling ************** */
284 static GHash *menutypes_hash = NULL;
286 MenuType *WM_menutype_find(const char *idname, bool quiet)
291 mt = BLI_ghash_lookup(menutypes_hash, idname);
297 printf("search for unknown menutype %s\n", idname);
302 bool WM_menutype_add(MenuType *mt)
304 BLI_ghash_insert(menutypes_hash, mt->idname, mt);
308 void WM_menutype_freelink(MenuType *mt)
312 ok = BLI_ghash_remove(menutypes_hash, mt->idname, NULL, MEM_freeN);
318 /* called on initialize WM_init() */
319 void WM_menutype_init(void)
321 /* reserve size is set based on blender default setup */
322 menutypes_hash = BLI_ghash_str_new_ex("menutypes_hash gh", 512);
325 void WM_menutype_free(void)
327 GHashIterator gh_iter;
329 GHASH_ITER (gh_iter, menutypes_hash) {
330 MenuType *mt = BLI_ghashIterator_getValue(&gh_iter);
332 mt->ext.free(mt->ext.data);
336 BLI_ghash_free(menutypes_hash, NULL, MEM_freeN);
337 menutypes_hash = NULL;
340 bool WM_menutype_poll(bContext *C, MenuType *mt)
342 if (mt->poll != NULL) {
343 return mt->poll(C, mt);
348 /* ****************************************** */
350 void WM_keymap_init(bContext *C)
352 wmWindowManager *wm = CTX_wm_manager(C);
354 /* create standard key configs */
355 if (!wm->defaultconf)
356 wm->defaultconf = WM_keyconfig_new(wm, "Blender");
358 wm->addonconf = WM_keyconfig_new(wm, "Blender Addon");
360 wm->userconf = WM_keyconfig_new(wm, "Blender User");
362 /* initialize only after python init is done, for keymaps that
363 * use python operators */
364 if (CTX_py_init_get(C) && (wm->initialized & WM_INIT_KEYMAP) == 0) {
365 /* create default key config, only initialize once,
366 * it's persistent across sessions */
367 if (!(wm->defaultconf->flag & KEYCONF_INIT_DEFAULT)) {
368 wm_window_keymap(wm->defaultconf);
369 ED_spacetypes_keymap(wm->defaultconf);
371 wm->defaultconf->flag |= KEYCONF_INIT_DEFAULT;
374 WM_keyconfig_update_tag(NULL, NULL);
375 WM_keyconfig_update(wm);
377 wm->initialized |= WM_INIT_KEYMAP;
381 void WM_check(bContext *C)
383 wmWindowManager *wm = CTX_wm_manager(C);
387 wm = CTX_data_main(C)->wm.first;
388 CTX_wm_manager_set(C, wm);
391 if (wm == NULL || BLI_listbase_is_empty(&wm->windows)) {
397 if ((wm->initialized & WM_INIT_WINDOW) == 0) {
399 WM_autosave_init(wm);
402 /* case: no open windows at all, for old file reads */
403 wm_window_ghostwindows_ensure(wm);
407 /* note: this runs in bg mode to set the screen context cb */
408 if ((wm->initialized & WM_INIT_WINDOW) == 0) {
409 ED_screens_initialize(wm);
410 wm->initialized |= WM_INIT_WINDOW;
414 void wm_clear_default_size(bContext *C)
416 wmWindowManager *wm = CTX_wm_manager(C);
421 wm = CTX_data_main(C)->wm.first;
422 CTX_wm_manager_set(C, wm);
425 if (wm == NULL || BLI_listbase_is_empty(&wm->windows)) {
429 for (win = wm->windows.first; win; win = win->next) {
438 /* on startup, it adds all data, for matching */
439 void wm_add_default(bContext *C)
441 wmWindowManager *wm = BKE_libblock_alloc(CTX_data_main(C), ID_WM, "WinMan", 0);
443 bScreen *screen = CTX_wm_screen(C); /* XXX from file read hrmf */
445 CTX_wm_manager_set(C, wm);
446 win = wm_window_new(C);
447 win->screen = screen;
448 screen->winid = win->winid;
449 BLI_strncpy(win->screenname, screen->id.name + 2, sizeof(win->screenname));
453 wm_window_make_drawable(wm, win);
457 /* context is allowed to be NULL, do not free wm itself (library.c) */
458 void wm_close_and_free(bContext *C, wmWindowManager *wm)
462 wmKeyConfig *keyconf;
464 if (wm->autosavetimer)
465 wm_autosave_timer_ended(wm);
467 while ((win = BLI_pophead(&wm->windows))) {
468 win->screen = NULL; /* prevent draw clear to use screen */
469 wm_draw_window_clear(win);
470 wm_window_free(C, wm, win);
473 while ((op = BLI_pophead(&wm->operators))) {
474 WM_operator_free(op);
477 while ((keyconf = BLI_pophead(&wm->keyconfigs))) {
478 WM_keyconfig_free(keyconf);
481 BLI_freelistN(&wm->queue);
483 BLI_freelistN(&wm->paintcursors);
485 WM_drag_free_list(&wm->drags);
489 if (C && CTX_wm_manager(C) == wm) CTX_wm_manager_set(C, NULL);
492 void wm_close_and_free_all(bContext *C, ListBase *wmlist)
496 while ((wm = wmlist->first)) {
497 wm_close_and_free(C, wm);
498 BLI_remlink(wmlist, wm);
499 BKE_libblock_free_data(&wm->id, true);
504 void WM_main(bContext *C)
506 /* Single refresh before handling events.
507 * This ensures we don't run operators before the depsgraph has been evaluated. */
508 wm_event_do_refresh_wm_and_depsgraph(C);
512 /* get events from ghost, handle window events, add to window queues */
513 wm_window_process_events(C);
515 /* per window, all events to the window, screen, area and region handlers */
516 wm_event_do_handlers(C);
518 /* events have left notes about changes, we handle and cache it */
519 wm_event_do_notifiers(C);
521 /* execute cached changes draw */