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_event_system.c
30 * Handle events and notifiers from GHOST input (mouse, keyboard, tablet, ndof).
32 * Also some operator reports utility functions.
38 #include "DNA_listBase.h"
39 #include "DNA_screen_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_windowmanager_types.h"
42 #include "DNA_userdef_types.h"
44 #include "MEM_guardedalloc.h"
48 #include "GHOST_C-api.h"
50 #include "BLI_blenlib.h"
51 #include "BLI_dynstr.h"
52 #include "BLI_utildefines.h"
55 #include "BKE_context.h"
56 #include "BKE_idprop.h"
57 #include "BKE_global.h"
59 #include "BKE_report.h"
60 #include "BKE_scene.h"
61 #include "BKE_screen.h"
63 #include "BKE_sound.h"
65 #include "ED_fileselect.h"
67 #include "ED_screen.h"
68 #include "ED_view3d.h"
72 #include "RNA_access.h"
74 #include "GPU_debug.h"
76 #include "UI_interface.h"
83 #include "wm_window.h"
84 #include "wm_event_system.h"
85 #include "wm_event_types.h"
87 #include "RNA_enum_types.h"
89 /* Motion in pixels allowed before we don't consider single/double click. */
90 #define WM_EVENT_CLICK_WIGGLE_ROOM 2
92 static void wm_notifier_clear(wmNotifier *note);
93 static void update_tablet_data(wmWindow *win, wmEvent *event);
95 static int wm_operator_call_internal(bContext *C, wmOperatorType *ot, PointerRNA *properties, ReportList *reports,
96 const short context, const bool poll_only);
98 /* ************ event management ************** */
100 wmEvent *wm_event_add_ex(wmWindow *win, const wmEvent *event_to_add, const wmEvent *event_to_add_after)
102 wmEvent *event = MEM_mallocN(sizeof(wmEvent), "wmEvent");
104 *event = *event_to_add;
106 update_tablet_data(win, event);
108 if (ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE)) {
109 /* We could have a preference to support relative tablet motion (we can't detect that). */
110 event->is_motion_absolute = (
111 (event->tablet_data != NULL) &&
112 (event->tablet_data->Active != GHOST_kTabletModeNone));
115 if (event_to_add_after == NULL) {
116 BLI_addtail(&win->queue, event);
119 /* note, strictly speaking this breaks const-correctness, however we're only changing 'next' member */
120 BLI_insertlinkafter(&win->queue, (void *)event_to_add_after, event);
125 wmEvent *wm_event_add(wmWindow *win, const wmEvent *event_to_add)
127 return wm_event_add_ex(win, event_to_add, NULL);
130 void wm_event_free(wmEvent *event)
132 if (event->customdata) {
133 if (event->customdatafree) {
134 /* note: pointer to listbase struct elsewhere */
135 if (event->custom == EVT_DATA_DRAGDROP) {
136 ListBase *lb = event->customdata;
137 WM_drag_free_list(lb);
140 MEM_freeN(event->customdata);
145 if (event->tablet_data) {
146 MEM_freeN((void *)event->tablet_data);
152 void wm_event_free_all(wmWindow *win)
156 while ((event = BLI_pophead(&win->queue))) {
157 wm_event_free(event);
161 void wm_event_init_from_window(wmWindow *win, wmEvent *event)
163 /* make sure we don't copy any owned pointers */
164 BLI_assert(win->eventstate->tablet_data == NULL);
166 *event = *(win->eventstate);
169 /* ********************* notifiers, listeners *************** */
171 static bool wm_test_duplicate_notifier(wmWindowManager *wm, unsigned int type, void *reference)
175 for (note = wm->queue.first; note; note = note->next)
176 if ((note->category | note->data | note->subtype | note->action) == type && note->reference == reference)
182 /* XXX: in future, which notifiers to send to other windows? */
183 void WM_event_add_notifier(const bContext *C, unsigned int type, void *reference)
186 wmWindowManager *wm = CTX_wm_manager(C);
189 if (wm_test_duplicate_notifier(wm, type, reference))
192 note = MEM_callocN(sizeof(wmNotifier), "notifier");
195 BLI_addtail(¬e->wm->queue, note);
197 note->window = CTX_wm_window(C);
199 ar = CTX_wm_region(C);
201 note->swinid = ar->swinid;
203 note->category = type & NOTE_CATEGORY;
204 note->data = type & NOTE_DATA;
205 note->subtype = type & NOTE_SUBTYPE;
206 note->action = type & NOTE_ACTION;
208 note->reference = reference;
211 void WM_main_add_notifier(unsigned int type, void *reference)
213 Main *bmain = G.main;
214 wmWindowManager *wm = bmain->wm.first;
217 if (!wm || wm_test_duplicate_notifier(wm, type, reference))
220 note = MEM_callocN(sizeof(wmNotifier), "notifier");
223 BLI_addtail(¬e->wm->queue, note);
225 note->category = type & NOTE_CATEGORY;
226 note->data = type & NOTE_DATA;
227 note->subtype = type & NOTE_SUBTYPE;
228 note->action = type & NOTE_ACTION;
230 note->reference = reference;
234 * Clear notifiers by reference, Used so listeners don't act on freed data.
236 void WM_main_remove_notifier_reference(const void *reference)
238 Main *bmain = G.main;
239 wmWindowManager *wm = bmain->wm.first;
242 wmNotifier *note, *note_next;
244 for (note = wm->queue.first; note; note = note_next) {
245 note_next = note->next;
247 if (note->reference == reference) {
248 /* don't remove because this causes problems for #wm_event_do_notifiers
249 * which may be looping on the data (deleting screens) */
250 wm_notifier_clear(note);
256 void WM_main_remap_editor_id_reference(ID *old_id, ID *new_id)
258 Main *bmain = G.main;
261 for (sc = bmain->screen.first; sc; sc = sc->id.next) {
264 for (sa = sc->areabase.first; sa; sa = sa->next) {
267 for (sl = sa->spacedata.first; sl; sl = sl->next) {
268 ED_spacedata_id_remap(sa, sl, old_id, new_id);
274 static void wm_notifier_clear(wmNotifier *note)
276 /* NULL the entire notifier, only leaving (next, prev) members intact */
277 memset(((char *)note) + sizeof(Link), 0, sizeof(*note) - sizeof(Link));
281 * Was part of #wm_event_do_notifiers, split out so it can be called once before entering the #WM_main loop.
282 * This ensures operators don't run before the UI and depsgraph are initialized.
284 void wm_event_do_refresh_wm_and_depsgraph(bContext *C)
286 wmWindowManager *wm = CTX_wm_manager(C);
287 uint64_t win_combine_v3d_datamask = 0;
289 /* combine datamasks so 1 win doesn't disable UV's in another [#26448] */
290 for (wmWindow *win = wm->windows.first; win; win = win->next) {
291 win_combine_v3d_datamask |= ED_view3d_screen_datamask(win->screen);
294 /* cached: editor refresh callbacks now, they get context */
295 for (wmWindow *win = wm->windows.first; win; win = win->next) {
298 CTX_wm_window_set(C, win);
299 for (sa = win->screen->areabase.first; sa; sa = sa->next) {
300 if (sa->do_refresh) {
301 CTX_wm_area_set(C, sa);
302 ED_area_do_refresh(C, sa);
306 /* XXX make lock in future, or separated derivedmesh users in scene */
307 if (G.is_rendering == false) {
308 /* depsgraph & animation: update tagged datablocks */
309 Main *bmain = CTX_data_main(C);
311 /* copied to set's in scene_update_tagged_recursive() */
312 win->screen->scene->customdata_mask = win_combine_v3d_datamask;
314 /* XXX, hack so operators can enforce datamasks [#26482], gl render */
315 win->screen->scene->customdata_mask |= win->screen->scene->customdata_mask_modal;
317 BKE_scene_update_tagged(bmain->eval_ctx, bmain, win->screen->scene);
321 CTX_wm_window_set(C, NULL);
324 /* called in mainloop */
325 void wm_event_do_notifiers(bContext *C)
327 wmWindowManager *wm = CTX_wm_manager(C);
328 wmNotifier *note, *next;
334 /* cache & catch WM level notifiers, such as frame change, scene/screen set */
335 for (win = wm->windows.first; win; win = win->next) {
336 bool do_anim = false;
338 CTX_wm_window_set(C, win);
340 for (note = wm->queue.first; note; note = next) {
343 if (note->category == NC_WM) {
344 if (ELEM(note->data, ND_FILEREAD, ND_FILESAVE)) {
346 wm_window_title(wm, win);
348 else if (note->data == ND_DATACHANGED)
349 wm_window_title(wm, win);
351 if (note->window == win) {
352 if (note->category == NC_SCREEN) {
353 if (note->data == ND_SCREENBROWSE) {
354 /* free popup handlers only [#35434] */
355 UI_popup_handlers_remove_all(C, &win->modalhandlers);
358 ED_screen_set(C, note->reference); // XXX hrms, think this over!
359 CLOG_INFO(WM_LOG_EVENTS, 1, "screen set %p", note->reference);
361 else if (note->data == ND_SCREENDELETE) {
362 ED_screen_delete(C, note->reference); // XXX hrms, think this over!
363 CLOG_INFO(WM_LOG_EVENTS, 1, "screen delete %p", note->reference);
368 if (note->window == win ||
369 (note->window == NULL && (note->reference == NULL || note->reference == win->screen->scene)))
371 if (note->category == NC_SCENE) {
372 if (note->data == ND_FRAME)
376 if (ELEM(note->category, NC_SCENE, NC_OBJECT, NC_GEOM, NC_WM)) {
377 ED_info_stats_clear(win->screen->scene);
378 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_INFO, NULL);
383 /* XXX, quick frame changes can cause a crash if framechange and rendering
384 * collide (happens on slow scenes), BKE_scene_update_for_newframe can be called
385 * twice which can depgraph update the same object at once */
386 if (G.is_rendering == false) {
388 /* depsgraph gets called, might send more notifiers */
389 ED_update_for_newframe(CTX_data_main(C), win->screen->scene, 1);
394 /* the notifiers are sent without context, to keep it clean */
395 while ((note = BLI_pophead(&wm->queue))) {
396 for (win = wm->windows.first; win; win = win->next) {
398 /* filter out notifiers */
399 if (note->category == NC_SCREEN && note->reference && note->reference != win->screen) {
402 else if (note->category == NC_SCENE && note->reference && note->reference != win->screen->scene) {
409 /* XXX context in notifiers? */
410 CTX_wm_window_set(C, win);
412 /* printf("notifier win %d screen %s cat %x\n", win->winid, win->screen->id.name + 2, note->category); */
413 ED_screen_do_listen(C, note);
415 for (ar = win->screen->regionbase.first; ar; ar = ar->next) {
416 ED_region_do_listen(win->screen, NULL, ar, note);
419 for (sa = win->screen->areabase.first; sa; sa = sa->next) {
420 ED_area_do_listen(win->screen, sa, note);
421 for (ar = sa->regionbase.first; ar; ar = ar->next) {
422 ED_region_do_listen(win->screen, sa, ar, note);
431 wm_event_do_refresh_wm_and_depsgraph(C);
434 static int wm_event_always_pass(const wmEvent *event)
436 /* some events we always pass on, to ensure proper communication */
437 return ISTIMER(event->type) || (event->type == WINDEACTIVATE);
440 /* ********************* ui handler ******************* */
442 static int wm_handler_ui_call(bContext *C, wmEventHandler *handler, const wmEvent *event, int always_pass)
444 ScrArea *area = CTX_wm_area(C);
445 ARegion *region = CTX_wm_region(C);
446 ARegion *menu = CTX_wm_menu(C);
447 static bool do_wheel_ui = true;
448 const bool is_wheel = ELEM(event->type, WHEELUPMOUSE, WHEELDOWNMOUSE, MOUSEPAN);
451 /* UI code doesn't handle return values - it just always returns break.
452 * to make the DBL_CLICK conversion work, we just don't send this to UI, except mouse clicks */
453 if (((handler->flag & WM_HANDLER_ACCEPT_DBL_CLICK) == 0) &&
454 (event->type != LEFTMOUSE) &&
455 (event->val == KM_DBL_CLICK))
457 return WM_HANDLER_CONTINUE;
460 /* UI is quite aggressive with swallowing events, like scrollwheel */
461 /* I realize this is not extremely nice code... when UI gets keymaps it can be maybe smarter */
462 if (do_wheel_ui == false) {
464 return WM_HANDLER_CONTINUE;
465 else if (wm_event_always_pass(event) == 0)
469 /* we set context to where ui handler came from */
470 if (handler->ui_area) CTX_wm_area_set(C, handler->ui_area);
471 if (handler->ui_region) CTX_wm_region_set(C, handler->ui_region);
472 if (handler->ui_menu) CTX_wm_menu_set(C, handler->ui_menu);
474 retval = handler->ui_handle(C, event, handler->ui_userdata);
476 /* putting back screen context */
477 if ((retval != WM_UI_HANDLER_BREAK) || always_pass) {
478 CTX_wm_area_set(C, area);
479 CTX_wm_region_set(C, region);
480 CTX_wm_menu_set(C, menu);
483 /* this special cases is for areas and regions that get removed */
484 CTX_wm_area_set(C, NULL);
485 CTX_wm_region_set(C, NULL);
486 CTX_wm_menu_set(C, NULL);
489 if (retval == WM_UI_HANDLER_BREAK)
490 return WM_HANDLER_BREAK;
492 /* event not handled in UI, if wheel then we temporarily disable it */
496 return WM_HANDLER_CONTINUE;
499 static void wm_handler_ui_cancel(bContext *C)
501 wmWindow *win = CTX_wm_window(C);
502 ARegion *ar = CTX_wm_region(C);
503 wmEventHandler *handler, *nexthandler;
508 for (handler = ar->handlers.first; handler; handler = nexthandler) {
509 nexthandler = handler->next;
511 if (handler->ui_handle) {
514 wm_event_init_from_window(win, &event);
515 event.type = EVT_BUT_CANCEL;
516 handler->ui_handle(C, &event, handler->ui_userdata);
521 /* ********************* operators ******************* */
523 int WM_operator_poll(bContext *C, wmOperatorType *ot)
525 wmOperatorTypeMacro *otmacro;
527 for (otmacro = ot->macro.first; otmacro; otmacro = otmacro->next) {
528 wmOperatorType *ot_macro = WM_operatortype_find(otmacro->idname, 0);
530 if (0 == WM_operator_poll(C, ot_macro))
534 /* python needs operator type, so we added exception for it */
536 return ot->pyop_poll(C, ot);
543 /* sets up the new context and calls 'wm_operator_invoke()' with poll_only */
544 int WM_operator_poll_context(bContext *C, wmOperatorType *ot, short context)
546 return wm_operator_call_internal(C, ot, NULL, NULL, context, true);
550 * Sets the active region for this space from the context.
552 * \see #BKE_area_find_region_active_win
554 void WM_operator_region_active_win_set(bContext *C)
556 ScrArea *sa = CTX_wm_area(C);
558 ARegion *ar = CTX_wm_region(C);
559 if (ar && ar->regiontype == RGN_TYPE_WINDOW) {
560 sa->region_active_win = BLI_findindex(&sa->regionbase, ar);
565 /* for debugging only, getting inspecting events manually is tedious */
566 void WM_event_print(const wmEvent *event)
569 const char *unknown = "UNKNOWN";
570 const char *type_id = unknown;
571 const char *val_id = unknown;
573 RNA_enum_identifier(rna_enum_event_type_items, event->type, &type_id);
574 RNA_enum_identifier(rna_enum_event_value_items, event->val, &val_id);
576 printf("wmEvent type:%d / %s, val:%d / %s,\n"
577 " shift:%d, ctrl:%d, alt:%d, oskey:%d, keymodifier:%d,\n"
578 " mouse:(%d,%d), ascii:'%c', utf8:'%.*s', keymap_idname:%s, pointer:%p\n",
579 event->type, type_id, event->val, val_id,
580 event->shift, event->ctrl, event->alt, event->oskey, event->keymodifier,
581 event->x, event->y, event->ascii,
582 BLI_str_utf8_size(event->utf8_buf), event->utf8_buf,
583 event->keymap_idname, (const void *)event);
585 #ifdef WITH_INPUT_NDOF
586 if (ISNDOF(event->type)) {
587 const wmNDOFMotionData *ndof = event->customdata;
588 if (event->type == NDOF_MOTION) {
589 printf(" ndof: rot: (%.4f %.4f %.4f), tx: (%.4f %.4f %.4f), dt: %.4f, progress: %u\n",
590 UNPACK3(ndof->rvec), UNPACK3(ndof->tvec), ndof->dt, ndof->progress);
593 /* ndof buttons printed already */
596 #endif /* WITH_INPUT_NDOF */
598 if (event->tablet_data) {
599 const wmTabletData *wmtab = event->tablet_data;
600 printf(" tablet: active: %d, pressure %.4f, tilt: (%.4f %.4f)\n",
601 wmtab->Active, wmtab->Pressure, wmtab->Xtilt, wmtab->Ytilt);
605 printf("wmEvent - NULL\n");
610 * Show the report in the info header.
612 void WM_report_banner_show(void)
614 wmWindowManager *wm = G.main->wm.first;
615 ReportList *wm_reports = &wm->reports;
616 ReportTimerInfo *rti;
618 /* After adding reports to the global list, reset the report timer. */
619 WM_event_remove_timer(wm, NULL, wm_reports->reporttimer);
621 /* Records time since last report was added */
622 wm_reports->reporttimer = WM_event_add_timer(wm, wm->winactive, TIMERREPORT, 0.05);
624 rti = MEM_callocN(sizeof(ReportTimerInfo), "ReportTimerInfo");
625 wm_reports->reporttimer->customdata = rti;
628 bool WM_event_is_last_mousemove(const wmEvent *event)
630 while ((event = event->next)) {
631 if (ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE)) {
638 #ifdef WITH_INPUT_NDOF
639 void WM_ndof_deadzone_set(float deadzone)
641 GHOST_setNDOFDeadZone(deadzone);
645 static void wm_add_reports(ReportList *reports)
647 /* if the caller owns them, handle this */
648 if (reports->list.first && (reports->flag & RPT_OP_HOLD) == 0) {
649 wmWindowManager *wm = G.main->wm.first;
651 /* add reports to the global list, otherwise they are not seen */
652 BLI_movelisttolist(&wm->reports.list, &reports->list);
654 WM_report_banner_show();
658 void WM_report(ReportType type, const char *message)
662 BKE_reports_init(&reports, RPT_STORE);
663 BKE_report(&reports, type, message);
665 wm_add_reports(&reports);
667 BKE_reports_clear(&reports);
670 void WM_reportf(ReportType type, const char *format, ...)
675 ds = BLI_dynstr_new();
676 va_start(args, format);
677 BLI_dynstr_vappendf(ds, format, args);
680 char *str = BLI_dynstr_get_cstring(ds);
681 WM_report(type, str);
687 /* (caller_owns_reports == true) when called from python */
688 static void wm_operator_reports(bContext *C, wmOperator *op, int retval, bool caller_owns_reports)
690 if (caller_owns_reports == false) { /* popup */
691 if (op->reports->list.first) {
692 /* FIXME, temp setting window, see other call to UI_popup_menu_reports for why */
693 wmWindow *win_prev = CTX_wm_window(C);
694 ScrArea *area_prev = CTX_wm_area(C);
695 ARegion *ar_prev = CTX_wm_region(C);
697 if (win_prev == NULL)
698 CTX_wm_window_set(C, CTX_wm_manager(C)->windows.first);
700 UI_popup_menu_reports(C, op->reports);
702 CTX_wm_window_set(C, win_prev);
703 CTX_wm_area_set(C, area_prev);
704 CTX_wm_region_set(C, ar_prev);
708 if (retval & OPERATOR_FINISHED) {
709 CLOG_STR_INFO_N(WM_LOG_OPERATORS, 1, WM_operator_pystring(C, op, false, true));
711 if (caller_owns_reports == false) {
712 BKE_reports_print(op->reports, RPT_DEBUG); /* print out reports to console. */
715 if (op->type->flag & OPTYPE_REGISTER) {
716 if (G.background == 0) { /* ends up printing these in the terminal, gets annoying */
717 /* Report the python string representation of the operator */
718 char *buf = WM_operator_pystring(C, op, false, true);
719 BKE_report(CTX_wm_reports(C), RPT_OPERATOR, buf);
725 /* if the caller owns them, handle this */
726 wm_add_reports(op->reports);
730 * This function is mainly to check that the rules for freeing
731 * an operator are kept in sync.
733 static bool wm_operator_register_check(wmWindowManager *wm, wmOperatorType *ot)
735 /* Check undo flag here since undo operators are also added to the list,
736 * to support checking if the same operator is run twice. */
737 return wm && (wm->op_undo_depth == 0) && (ot->flag & (OPTYPE_REGISTER | OPTYPE_UNDO));
740 static void wm_operator_finished(bContext *C, wmOperator *op, const bool repeat, const bool store)
742 wmWindowManager *wm = CTX_wm_manager(C);
744 op->customdata = NULL;
747 WM_operator_last_properties_store(op);
750 /* we don't want to do undo pushes for operators that are being
751 * called from operators that already do an undo push. usually
752 * this will happen for python operators that call C operators */
753 if (wm->op_undo_depth == 0) {
754 if (op->type->flag & OPTYPE_UNDO)
755 ED_undo_push_op(C, op);
756 else if (op->type->flag & OPTYPE_UNDO_GROUPED)
757 ED_undo_grouped_push_op(C, op);
761 if (G.debug & G_DEBUG_WM) {
762 char *buf = WM_operator_pystring(C, op, false, true);
763 BKE_report(CTX_wm_reports(C), RPT_OPERATOR, buf);
767 if (wm_operator_register_check(wm, op->type)) {
768 /* take ownership of reports (in case python provided own) */
769 op->reports->flag |= RPT_FREE;
771 wm_operator_register(C, op);
772 WM_operator_region_active_win_set(C);
775 WM_operator_free(op);
780 /* if repeat is true, it doesn't register again, nor does it free */
781 static int wm_operator_exec(bContext *C, wmOperator *op, const bool repeat, const bool store)
783 wmWindowManager *wm = CTX_wm_manager(C);
784 int retval = OPERATOR_CANCELLED;
786 CTX_wm_operator_poll_msg_set(C, NULL);
788 if (op == NULL || op->type == NULL)
791 if (0 == WM_operator_poll(C, op->type))
794 if (op->type->exec) {
795 if (op->type->flag & OPTYPE_UNDO) {
800 op->flag |= OP_IS_REPEAT;
802 retval = op->type->exec(C, op);
803 OPERATOR_RETVAL_CHECK(retval);
805 op->flag &= ~OP_IS_REPEAT;
808 if (op->type->flag & OPTYPE_UNDO && CTX_wm_manager(C) == wm) {
813 /* XXX Disabled the repeat check to address part 2 of #31840.
814 * Carefully checked all calls to wm_operator_exec and WM_operator_repeat, don't see any reason
815 * why this was needed, but worth to note it in case something turns bad. (mont29) */
816 if (retval & (OPERATOR_FINISHED | OPERATOR_CANCELLED) /* && repeat == 0 */)
817 wm_operator_reports(C, op, retval, false);
819 if (retval & OPERATOR_FINISHED) {
820 wm_operator_finished(C, op, repeat, store && wm->op_undo_depth == 0);
822 else if (repeat == 0) {
823 /* warning: modal from exec is bad practice, but avoid crashing. */
824 if (retval & (OPERATOR_FINISHED | OPERATOR_CANCELLED)) {
825 WM_operator_free(op);
829 return retval | OPERATOR_HANDLED;
833 /* simply calls exec with basic checks */
834 static int wm_operator_exec_notest(bContext *C, wmOperator *op)
836 int retval = OPERATOR_CANCELLED;
838 if (op == NULL || op->type == NULL || op->type->exec == NULL)
841 retval = op->type->exec(C, op);
842 OPERATOR_RETVAL_CHECK(retval);
848 * for running operators with frozen context (modal handlers, menus)
850 * \param store Store settings for re-use.
852 * warning: do not use this within an operator to call its self! [#29537] */
853 int WM_operator_call_ex(bContext *C, wmOperator *op,
856 return wm_operator_exec(C, op, false, store);
859 int WM_operator_call(bContext *C, wmOperator *op)
861 return WM_operator_call_ex(C, op, false);
865 * This is intended to be used when an invoke operator wants to call exec on its self
866 * and is basically like running op->type->exec() directly, no poll checks no freeing,
867 * since we assume whoever called invoke will take care of that
869 int WM_operator_call_notest(bContext *C, wmOperator *op)
871 return wm_operator_exec_notest(C, op);
875 * Execute this operator again, put here so it can share above code
877 int WM_operator_repeat(bContext *C, wmOperator *op)
879 return wm_operator_exec(C, op, true, true);
882 * \return true if #WM_operator_repeat can run
883 * simple check for now but may become more involved.
884 * To be sure the operator can run call `WM_operator_poll(C, op->type)` also, since this call
885 * checks if WM_operator_repeat() can run at all, not that it WILL run at any time.
887 bool WM_operator_repeat_check(const bContext *UNUSED(C), wmOperator *op)
889 if (op->type->exec != NULL) {
893 /* for macros, check all have exec() we can call */
894 wmOperatorTypeMacro *otmacro;
895 for (otmacro = op->opm->type->macro.first; otmacro; otmacro = otmacro->next) {
896 wmOperatorType *otm = WM_operatortype_find(otmacro->idname, 0);
897 if (otm && otm->exec == NULL) {
907 bool WM_operator_is_repeat(const bContext *C, const wmOperator *op)
909 /* may be in the operators list or not */
911 if (op->prev == NULL && op->next == NULL) {
912 wmWindowManager *wm = CTX_wm_manager(C);
913 op_prev = wm->operators.last;
918 return (op_prev && (op->type == op_prev->type));
921 static wmOperator *wm_operator_create(wmWindowManager *wm, wmOperatorType *ot,
922 PointerRNA *properties, ReportList *reports)
924 /* XXX operatortype names are static still. for debug */
925 wmOperator *op = MEM_callocN(sizeof(wmOperator), ot->idname);
927 /* XXX adding new operator could be function, only happens here now */
929 BLI_strncpy(op->idname, ot->idname, OP_MAX_TYPENAME);
931 /* initialize properties, either copy or create */
932 op->ptr = MEM_callocN(sizeof(PointerRNA), "wmOperatorPtrRNA");
933 if (properties && properties->data) {
934 op->properties = IDP_CopyProperty(properties->data);
937 IDPropertyTemplate val = {0};
938 op->properties = IDP_New(IDP_GROUP, &val, "wmOperatorProperties");
940 RNA_pointer_create(&wm->id, ot->srna, op->properties, op->ptr);
942 /* initialize error reports */
944 op->reports = reports; /* must be initialized already */
947 op->reports = MEM_mallocN(sizeof(ReportList), "wmOperatorReportList");
948 BKE_reports_init(op->reports, RPT_STORE | RPT_FREE);
951 /* recursive filling of operator macro list */
952 if (ot->macro.first) {
953 static wmOperator *motherop = NULL;
954 wmOperatorTypeMacro *otmacro;
957 /* ensure all ops are in execution order in 1 list */
958 if (motherop == NULL) {
964 /* if properties exist, it will contain everything needed */
966 otmacro = ot->macro.first;
968 RNA_STRUCT_BEGIN (properties, prop)
974 /* skip invalid properties */
975 if (STREQ(RNA_property_identifier(prop), otmacro->idname)) {
976 wmOperatorType *otm = WM_operatortype_find(otmacro->idname, 0);
977 PointerRNA someptr = RNA_property_pointer_get(properties, prop);
978 wmOperator *opm = wm_operator_create(wm, otm, &someptr, NULL);
980 IDP_ReplaceGroupInGroup(opm->properties, otmacro->properties);
982 BLI_addtail(&motherop->macro, opm);
983 opm->opm = motherop; /* pointer to mom, for modal() */
985 otmacro = otmacro->next;
991 for (otmacro = ot->macro.first; otmacro; otmacro = otmacro->next) {
992 wmOperatorType *otm = WM_operatortype_find(otmacro->idname, 0);
993 wmOperator *opm = wm_operator_create(wm, otm, otmacro->ptr, NULL);
995 BLI_addtail(&motherop->macro, opm);
996 opm->opm = motherop; /* pointer to mom, for modal() */
1004 WM_operator_properties_sanitize(op->ptr, 0);
1009 static void wm_region_mouse_co(bContext *C, wmEvent *event)
1011 ARegion *ar = CTX_wm_region(C);
1013 /* compatibility convention */
1014 event->mval[0] = event->x - ar->winrct.xmin;
1015 event->mval[1] = event->y - ar->winrct.ymin;
1018 /* these values are invalid (avoid odd behavior by relying on old mval values) */
1019 event->mval[0] = -1;
1020 event->mval[1] = -1;
1024 #if 1 /* may want to disable operator remembering previous state for testing */
1026 static bool operator_last_properties_init_impl(wmOperator *op, IDProperty *last_properties)
1028 bool changed = false;
1029 IDPropertyTemplate val = {0};
1030 IDProperty *replaceprops = IDP_New(IDP_GROUP, &val, "wmOperatorProperties");
1031 PropertyRNA *iterprop;
1033 CLOG_INFO(WM_LOG_OPERATORS, 1, "loading previous properties for '%s'", op->type->idname);
1035 iterprop = RNA_struct_iterator_property(op->type->srna);
1037 RNA_PROP_BEGIN (op->ptr, itemptr, iterprop)
1039 PropertyRNA *prop = itemptr.data;
1040 if ((RNA_property_flag(prop) & PROP_SKIP_SAVE) == 0) {
1041 if (!RNA_property_is_set(op->ptr, prop)) { /* don't override a setting already set */
1042 const char *identifier = RNA_property_identifier(prop);
1043 IDProperty *idp_src = IDP_GetPropertyFromGroup(last_properties, identifier);
1045 IDProperty *idp_dst = IDP_CopyProperty(idp_src);
1047 /* note - in the future this may need to be done recursively,
1048 * but for now RNA doesn't access nested operators */
1049 idp_dst->flag |= IDP_FLAG_GHOST;
1051 /* add to temporary group instead of immediate replace,
1052 * because we are iterating over this group */
1053 IDP_AddToGroup(replaceprops, idp_dst);
1061 IDP_MergeGroup(op->properties, replaceprops, true);
1062 IDP_FreeProperty(replaceprops);
1063 MEM_freeN(replaceprops);
1067 bool WM_operator_last_properties_init(wmOperator *op)
1069 bool changed = false;
1070 if (op->type->last_properties) {
1071 changed |= operator_last_properties_init_impl(op, op->type->last_properties);
1072 for (wmOperator *opm = op->macro.first; opm; opm = opm->next) {
1073 IDProperty *idp_src = IDP_GetPropertyFromGroup(op->type->last_properties, opm->idname);
1075 changed |= operator_last_properties_init_impl(opm, idp_src);
1082 bool WM_operator_last_properties_store(wmOperator *op)
1084 if (op->type->last_properties) {
1085 IDP_FreeProperty(op->type->last_properties);
1086 MEM_freeN(op->type->last_properties);
1087 op->type->last_properties = NULL;
1090 if (op->properties) {
1091 CLOG_INFO(WM_LOG_OPERATORS, 1, "storing properties for '%s'", op->type->idname);
1092 op->type->last_properties = IDP_CopyProperty(op->properties);
1095 if (op->macro.first != NULL) {
1096 for (wmOperator *opm = op->macro.first; opm; opm = opm->next) {
1097 if (opm->properties) {
1098 if (op->type->last_properties == NULL) {
1099 op->type->last_properties = IDP_New(IDP_GROUP, &(IDPropertyTemplate){0}, "wmOperatorProperties");
1101 IDProperty *idp_macro = IDP_CopyProperty(opm->properties);
1102 STRNCPY(idp_macro->name, opm->idname);
1103 IDP_ReplaceInGroup(op->type->last_properties, idp_macro);
1108 return (op->type->last_properties != NULL);
1113 bool WM_operator_last_properties_init(wmOperator *UNUSED(op))
1118 bool WM_operator_last_properties_store(wmOperator *UNUSED(op))
1126 * Also used for exec when 'event' is NULL.
1128 static int wm_operator_invoke(
1129 bContext *C, wmOperatorType *ot, wmEvent *event,
1130 PointerRNA *properties, ReportList *reports, const bool poll_only)
1132 int retval = OPERATOR_PASS_THROUGH;
1134 /* this is done because complicated setup is done to call this function that is better not duplicated */
1136 return WM_operator_poll(C, ot);
1138 if (WM_operator_poll(C, ot)) {
1139 wmWindowManager *wm = CTX_wm_manager(C);
1140 wmOperator *op = wm_operator_create(wm, ot, properties, reports); /* if reports == NULL, they'll be initialized */
1141 const bool is_nested_call = (wm->op_undo_depth != 0);
1143 if (event != NULL) {
1144 op->flag |= OP_IS_INVOKE;
1147 /* initialize setting from previous run */
1148 if (!is_nested_call) { /* not called by py script */
1149 WM_operator_last_properties_init(op);
1152 if ((event == NULL) || (event->type != MOUSEMOVE)) {
1153 CLOG_INFO(WM_LOG_HANDLERS, 2,
1154 "handle evt %d win %d op %s",
1155 event ? event->type : 0, CTX_wm_screen(C)->subwinactive, ot->idname);
1158 if (op->type->invoke && event) {
1159 wm_region_mouse_co(C, event);
1161 if (op->type->flag & OPTYPE_UNDO)
1162 wm->op_undo_depth++;
1164 retval = op->type->invoke(C, op, event);
1165 OPERATOR_RETVAL_CHECK(retval);
1167 if (op->type->flag & OPTYPE_UNDO && CTX_wm_manager(C) == wm)
1168 wm->op_undo_depth--;
1170 else if (op->type->exec) {
1171 if (op->type->flag & OPTYPE_UNDO)
1172 wm->op_undo_depth++;
1174 retval = op->type->exec(C, op);
1175 OPERATOR_RETVAL_CHECK(retval);
1177 if (op->type->flag & OPTYPE_UNDO && CTX_wm_manager(C) == wm)
1178 wm->op_undo_depth--;
1181 /* debug, important to leave a while, should never happen */
1182 CLOG_ERROR(WM_LOG_OPERATORS, "invalid operator call '%s'", op->idname);
1185 /* Note, if the report is given as an argument then assume the caller will deal with displaying them
1186 * currently python only uses this */
1187 if (!(retval & OPERATOR_HANDLED) && (retval & (OPERATOR_FINISHED | OPERATOR_CANCELLED))) {
1188 /* only show the report if the report list was not given in the function */
1189 wm_operator_reports(C, op, retval, (reports != NULL));
1192 if (retval & OPERATOR_HANDLED) {
1193 /* do nothing, wm_operator_exec() has been called somewhere */
1195 else if (retval & OPERATOR_FINISHED) {
1196 const bool store = !is_nested_call;
1197 wm_operator_finished(C, op, false, store);
1199 else if (retval & OPERATOR_RUNNING_MODAL) {
1200 /* take ownership of reports (in case python provided own) */
1201 op->reports->flag |= RPT_FREE;
1203 /* grab cursor during blocking modal ops (X11)
1204 * Also check for macro
1206 if (ot->flag & OPTYPE_BLOCKING || (op->opm && op->opm->type->flag & OPTYPE_BLOCKING)) {
1207 int bounds[4] = {-1, -1, -1, -1};
1210 if (event == NULL) {
1214 wrap = (U.uiflag & USER_CONTINUOUS_MOUSE) &&
1215 ((op->opm->flag & OP_IS_MODAL_GRAB_CURSOR) || (op->opm->type->flag & OPTYPE_GRAB_CURSOR));
1218 wrap = (U.uiflag & USER_CONTINUOUS_MOUSE) &&
1219 ((op->flag & OP_IS_MODAL_GRAB_CURSOR) || (ot->flag & OPTYPE_GRAB_CURSOR));
1222 /* exception, cont. grab in header is annoying */
1224 ARegion *ar = CTX_wm_region(C);
1225 if (ar && ar->regiontype == RGN_TYPE_HEADER) {
1231 const rcti *winrect = NULL;
1232 ARegion *ar = CTX_wm_region(C);
1233 ScrArea *sa = CTX_wm_area(C);
1235 if (ar && ar->regiontype == RGN_TYPE_WINDOW &&
1236 BLI_rcti_isect_pt_v(&ar->winrct, &event->x))
1238 winrect = &ar->winrct;
1240 else if (sa && BLI_rcti_isect_pt_v(&sa->totrct, &event->x)) {
1241 winrect = &sa->totrct;
1245 bounds[0] = winrect->xmin;
1246 bounds[1] = winrect->ymax;
1247 bounds[2] = winrect->xmax;
1248 bounds[3] = winrect->ymin;
1252 WM_cursor_grab_enable(CTX_wm_window(C), wrap, false, bounds);
1255 /* cancel UI handlers, typically tooltips that can hang around
1256 * while dragging the view or worse, that stay there permanently
1257 * after the modal operator has swallowed all events and passed
1258 * none to the UI handler */
1259 wm_handler_ui_cancel(C);
1262 WM_operator_free(op);
1270 * #WM_operator_name_call is the main accessor function
1271 * this is for python to access since its done the operator lookup
1273 * invokes operator in context
1275 static int wm_operator_call_internal(
1276 bContext *C, wmOperatorType *ot, PointerRNA *properties, ReportList *reports,
1277 const short context, const bool poll_only)
1283 CTX_wm_operator_poll_msg_set(C, NULL);
1287 wmWindow *window = CTX_wm_window(C);
1290 case WM_OP_INVOKE_DEFAULT:
1291 case WM_OP_INVOKE_REGION_WIN:
1292 case WM_OP_INVOKE_REGION_PREVIEW:
1293 case WM_OP_INVOKE_REGION_CHANNELS:
1294 case WM_OP_INVOKE_AREA:
1295 case WM_OP_INVOKE_SCREEN:
1296 /* window is needed for invoke, cancel operator */
1297 if (window == NULL) {
1299 CTX_wm_operator_poll_msg_set(C, "Missing 'window' in context");
1304 event = window->eventstate;
1314 case WM_OP_EXEC_REGION_WIN:
1315 case WM_OP_INVOKE_REGION_WIN:
1316 case WM_OP_EXEC_REGION_CHANNELS:
1317 case WM_OP_INVOKE_REGION_CHANNELS:
1318 case WM_OP_EXEC_REGION_PREVIEW:
1319 case WM_OP_INVOKE_REGION_PREVIEW:
1321 /* forces operator to go to the region window/channels/preview, for header menus
1322 * but we stay in the same region if we are already in one
1324 ARegion *ar = CTX_wm_region(C);
1325 ScrArea *area = CTX_wm_area(C);
1326 int type = RGN_TYPE_WINDOW;
1329 case WM_OP_EXEC_REGION_CHANNELS:
1330 case WM_OP_INVOKE_REGION_CHANNELS:
1331 type = RGN_TYPE_CHANNELS;
1334 case WM_OP_EXEC_REGION_PREVIEW:
1335 case WM_OP_INVOKE_REGION_PREVIEW:
1336 type = RGN_TYPE_PREVIEW;
1339 case WM_OP_EXEC_REGION_WIN:
1340 case WM_OP_INVOKE_REGION_WIN:
1342 type = RGN_TYPE_WINDOW;
1346 if (!(ar && ar->regiontype == type) && area) {
1348 if (type == RGN_TYPE_WINDOW) {
1349 ar1 = BKE_area_find_region_active_win(area);
1352 ar1 = BKE_area_find_region_type(area, type);
1356 CTX_wm_region_set(C, ar1);
1359 retval = wm_operator_invoke(C, ot, event, properties, reports, poll_only);
1361 /* set region back */
1362 CTX_wm_region_set(C, ar);
1366 case WM_OP_EXEC_AREA:
1367 case WM_OP_INVOKE_AREA:
1369 /* remove region from context */
1370 ARegion *ar = CTX_wm_region(C);
1372 CTX_wm_region_set(C, NULL);
1373 retval = wm_operator_invoke(C, ot, event, properties, reports, poll_only);
1374 CTX_wm_region_set(C, ar);
1378 case WM_OP_EXEC_SCREEN:
1379 case WM_OP_INVOKE_SCREEN:
1381 /* remove region + area from context */
1382 ARegion *ar = CTX_wm_region(C);
1383 ScrArea *area = CTX_wm_area(C);
1385 CTX_wm_region_set(C, NULL);
1386 CTX_wm_area_set(C, NULL);
1387 retval = wm_operator_invoke(C, ot, event, properties, reports, poll_only);
1388 CTX_wm_area_set(C, area);
1389 CTX_wm_region_set(C, ar);
1393 case WM_OP_EXEC_DEFAULT:
1394 case WM_OP_INVOKE_DEFAULT:
1395 return wm_operator_invoke(C, ot, event, properties, reports, poll_only);
1403 /* invokes operator in context */
1404 int WM_operator_name_call_ptr(bContext *C, wmOperatorType *ot, short context, PointerRNA *properties)
1406 BLI_assert(ot == WM_operatortype_find(ot->idname, true));
1407 return wm_operator_call_internal(C, ot, properties, NULL, context, false);
1409 int WM_operator_name_call(bContext *C, const char *opstring, short context, PointerRNA *properties)
1411 wmOperatorType *ot = WM_operatortype_find(opstring, 0);
1413 return WM_operator_name_call_ptr(C, ot, context, properties);
1420 * Call an existent menu. The menu can be created in C or Python.
1422 void WM_menu_name_call(bContext *C, const char *menu_name, short context)
1424 wmOperatorType *ot = WM_operatortype_find("WM_OT_call_menu", false);
1426 WM_operator_properties_create_ptr(&ptr, ot);
1427 RNA_string_set(&ptr, "name", menu_name);
1428 WM_operator_name_call_ptr(C, ot, context, &ptr);
1429 WM_operator_properties_free(&ptr);
1433 * Similar to #WM_operator_name_call called with #WM_OP_EXEC_DEFAULT context.
1435 * - #wmOperatorType is used instead of operator name since python already has the operator type.
1436 * - `poll()` must be called by python before this runs.
1437 * - reports can be passed to this function (so python can report them as exceptions).
1439 int WM_operator_call_py(
1440 bContext *C, wmOperatorType *ot, short context,
1441 PointerRNA *properties, ReportList *reports, const bool is_undo)
1443 int retval = OPERATOR_CANCELLED;
1447 op = wm_operator_create(wm, ot, properties, reports);
1449 if (op->type->exec) {
1450 if (is_undo && op->type->flag & OPTYPE_UNDO)
1451 wm->op_undo_depth++;
1453 retval = op->type->exec(C, op);
1454 OPERATOR_RETVAL_CHECK(retval);
1456 if (is_undo && op->type->flag & OPTYPE_UNDO && CTX_wm_manager(C) == wm)
1457 wm->op_undo_depth--;
1460 CLOG_WARN(WM_LOG_OPERATORS, "\"%s\" operator has no exec function, Python cannot call it", op->type->name);
1465 /* not especially nice using undo depth here, its used so py never
1466 * triggers undo or stores operators last used state.
1468 * we could have some more obvious way of doing this like passing a flag.
1470 wmWindowManager *wm = CTX_wm_manager(C);
1471 if (!is_undo && wm) wm->op_undo_depth++;
1473 retval = wm_operator_call_internal(C, ot, properties, reports, context, false);
1475 if (!is_undo && wm && (wm == CTX_wm_manager(C))) wm->op_undo_depth--;
1481 /* ********************* handlers *************** */
1483 /* future extra customadata free? */
1484 void wm_event_free_handler(wmEventHandler *handler)
1489 /* only set context when area/region is part of screen */
1490 static void wm_handler_op_context(bContext *C, wmEventHandler *handler, const wmEvent *event)
1492 bScreen *screen = CTX_wm_screen(C);
1494 if (screen && handler->op) {
1495 if (handler->op_area == NULL)
1496 CTX_wm_area_set(C, NULL);
1500 for (sa = screen->areabase.first; sa; sa = sa->next)
1501 if (sa == handler->op_area)
1504 /* when changing screen layouts with running modal handlers (like render display), this
1505 * is not an error to print */
1506 if (handler->op == NULL) {
1507 CLOG_ERROR(WM_LOG_HANDLERS, "internal error: handler (%s) has invalid area", handler->op->type->idname);
1512 wmOperator *op = handler->op ? (handler->op->opm ? handler->op->opm : handler->op) : NULL;
1513 CTX_wm_area_set(C, sa);
1515 if (op && (op->flag & OP_IS_MODAL_CURSOR_REGION)) {
1516 ar = BKE_area_find_region_xy(sa, handler->op_region_type, event->x, event->y);
1518 handler->op_region = ar;
1526 for (ar = sa->regionbase.first; ar; ar = ar->next) {
1527 if (ar == handler->op_region) {
1533 /* XXX no warning print here, after full-area and back regions are remade */
1535 CTX_wm_region_set(C, ar);
1541 /* called on exit or remove area, only here call cancel callback */
1542 void WM_event_remove_handlers(bContext *C, ListBase *handlers)
1544 wmEventHandler *handler;
1545 wmWindowManager *wm = CTX_wm_manager(C);
1547 /* C is zero on freeing database, modal handlers then already were freed */
1548 while ((handler = BLI_pophead(handlers))) {
1550 wmWindow *win = CTX_wm_window(C);
1551 if (handler->op->type->cancel) {
1552 ScrArea *area = CTX_wm_area(C);
1553 ARegion *region = CTX_wm_region(C);
1555 wm_handler_op_context(C, handler, win->eventstate);
1557 if (handler->op->type->flag & OPTYPE_UNDO)
1558 wm->op_undo_depth++;
1560 handler->op->type->cancel(C, handler->op);
1562 if (handler->op->type->flag & OPTYPE_UNDO)
1563 wm->op_undo_depth--;
1565 CTX_wm_area_set(C, area);
1566 CTX_wm_region_set(C, region);
1569 WM_cursor_grab_disable(win, NULL);
1570 WM_operator_free(handler->op);
1572 else if (handler->ui_remove) {
1573 ScrArea *area = CTX_wm_area(C);
1574 ARegion *region = CTX_wm_region(C);
1575 ARegion *menu = CTX_wm_menu(C);
1577 if (handler->ui_area) CTX_wm_area_set(C, handler->ui_area);
1578 if (handler->ui_region) CTX_wm_region_set(C, handler->ui_region);
1579 if (handler->ui_menu) CTX_wm_menu_set(C, handler->ui_menu);
1581 handler->ui_remove(C, handler->ui_userdata);
1583 CTX_wm_area_set(C, area);
1584 CTX_wm_region_set(C, region);
1585 CTX_wm_menu_set(C, menu);
1588 wm_event_free_handler(handler);
1592 /* do userdef mappings */
1593 int WM_userdef_event_map(int kmitype)
1597 return (U.flag & USER_LMOUSESELECT) ? LEFTMOUSE : RIGHTMOUSE;
1599 return (U.flag & USER_LMOUSESELECT) ? RIGHTMOUSE : LEFTMOUSE;
1601 return (U.flag & USER_LMOUSESELECT) ? EVT_TWEAK_R : EVT_TWEAK_L;
1603 return (U.flag & USER_LMOUSESELECT) ? EVT_TWEAK_L : EVT_TWEAK_R;
1605 return (U.uiflag & USER_WHEELZOOMDIR) ? WHEELUPMOUSE : WHEELDOWNMOUSE;
1607 return (U.uiflag & USER_WHEELZOOMDIR) ? WHEELDOWNMOUSE : WHEELUPMOUSE;
1614 static int wm_eventmatch(const wmEvent *winevent, wmKeyMapItem *kmi)
1616 int kmitype = WM_userdef_event_map(kmi->type);
1618 if (kmi->flag & KMI_INACTIVE) return 0;
1620 /* the matching rules */
1621 if (kmitype == KM_TEXTINPUT)
1622 if (winevent->val == KM_PRESS) { /* prevent double clicks */
1623 /* NOT using ISTEXTINPUT anymore because (at least on Windows) some key codes above 255
1624 * could have printable ascii keys - BUG [#30479] */
1625 if (ISKEYBOARD(winevent->type) && (winevent->ascii || winevent->utf8_buf[0])) return 1;
1628 if (kmitype != KM_ANY) {
1629 if (ELEM(kmitype, TABLET_STYLUS, TABLET_ERASER)) {
1630 const wmTabletData *wmtab = winevent->tablet_data;
1634 else if (winevent->type != LEFTMOUSE) /* tablet events can occur on hover + keypress */
1636 else if ((kmitype == TABLET_STYLUS) && (wmtab->Active != EVT_TABLET_STYLUS))
1638 else if ((kmitype == TABLET_ERASER) && (wmtab->Active != EVT_TABLET_ERASER))
1642 if (winevent->type != kmitype)
1647 if (kmi->val != KM_ANY)
1648 if (winevent->val != kmi->val) return 0;
1650 /* modifiers also check bits, so it allows modifier order */
1651 if (kmi->shift != KM_ANY)
1652 if (winevent->shift != kmi->shift && !(winevent->shift & kmi->shift)) return 0;
1653 if (kmi->ctrl != KM_ANY)
1654 if (winevent->ctrl != kmi->ctrl && !(winevent->ctrl & kmi->ctrl)) return 0;
1655 if (kmi->alt != KM_ANY)
1656 if (winevent->alt != kmi->alt && !(winevent->alt & kmi->alt)) return 0;
1657 if (kmi->oskey != KM_ANY)
1658 if (winevent->oskey != kmi->oskey && !(winevent->oskey & kmi->oskey)) return 0;
1660 /* only keymap entry with keymodifier is checked, means all keys without modifier get handled too. */
1661 /* that is currently needed to make overlapping events work (when you press A - G fast or so). */
1662 if (kmi->keymodifier)
1663 if (winevent->keymodifier != kmi->keymodifier) return 0;
1669 /* operator exists */
1670 static void wm_event_modalkeymap(const bContext *C, wmOperator *op, wmEvent *event, bool *dbl_click_disabled)
1672 /* support for modal keymap in macros */
1676 if (op->type->modalkeymap) {
1677 wmKeyMap *keymap = WM_keymap_active(CTX_wm_manager(C), op->type->modalkeymap);
1680 for (kmi = keymap->items.first; kmi; kmi = kmi->next) {
1681 if (wm_eventmatch(event, kmi)) {
1683 event->prevtype = event->type;
1684 event->prevval = event->val;
1685 event->type = EVT_MODAL_MAP;
1686 event->val = kmi->propvalue;
1693 /* modal keymap checking returns handled events fine, but all hardcoded modal
1694 * handling typically swallows all events (OPERATOR_RUNNING_MODAL).
1695 * This bypass just disables support for double clicks in hardcoded modal handlers */
1696 if (event->val == KM_DBL_CLICK) {
1697 event->val = KM_PRESS;
1698 *dbl_click_disabled = true;
1704 * Check whether operator is allowed to run in case interface is locked,
1705 * If interface is unlocked, will always return truth.
1707 static bool wm_operator_check_locked_interface(bContext *C, wmOperatorType *ot)
1709 wmWindowManager *wm = CTX_wm_manager(C);
1711 if (wm->is_interface_locked) {
1712 if ((ot->flag & OPTYPE_LOCK_BYPASS) == 0) {
1720 /* bad hacking event system... better restore event type for checking of KM_CLICK for example */
1721 /* XXX modal maps could use different method (ton) */
1722 static void wm_event_modalmap_end(wmEvent *event, bool dbl_click_disabled)
1724 if (event->type == EVT_MODAL_MAP) {
1725 event->type = event->prevtype;
1726 event->prevtype = 0;
1727 event->val = event->prevval;
1730 else if (dbl_click_disabled)
1731 event->val = KM_DBL_CLICK;
1735 /* Warning: this function removes a modal handler, when finished */
1736 static int wm_handler_operator_call(bContext *C, ListBase *handlers, wmEventHandler *handler,
1737 wmEvent *event, PointerRNA *properties)
1739 int retval = OPERATOR_PASS_THROUGH;
1741 /* derived, modal or blocking operator */
1743 wmOperator *op = handler->op;
1744 wmOperatorType *ot = op->type;
1746 if (!wm_operator_check_locked_interface(C, ot)) {
1747 /* Interface is locked and operator is not allowed to run,
1748 * nothing to do in this case.
1751 else if (ot->modal) {
1752 /* we set context to where modal handler came from */
1753 wmWindowManager *wm = CTX_wm_manager(C);
1754 ScrArea *area = CTX_wm_area(C);
1755 ARegion *region = CTX_wm_region(C);
1756 bool dbl_click_disabled = false;
1758 wm_handler_op_context(C, handler, event);
1759 wm_region_mouse_co(C, event);
1760 wm_event_modalkeymap(C, op, event, &dbl_click_disabled);
1762 if (ot->flag & OPTYPE_UNDO)
1763 wm->op_undo_depth++;
1765 /* warning, after this call all context data and 'event' may be freed. see check below */
1766 retval = ot->modal(C, op, event);
1767 OPERATOR_RETVAL_CHECK(retval);
1769 /* when this is _not_ the case the modal modifier may have loaded
1770 * a new blend file (demo mode does this), so we have to assume
1771 * the event, operator etc have all been freed. - campbell */
1772 if (CTX_wm_manager(C) == wm) {
1774 wm_event_modalmap_end(event, dbl_click_disabled);
1776 if (ot->flag & OPTYPE_UNDO)
1777 wm->op_undo_depth--;
1779 if (retval & (OPERATOR_CANCELLED | OPERATOR_FINISHED)) {
1780 wm_operator_reports(C, op, retval, false);
1783 /* not very common, but modal operators may report before finishing */
1784 if (!BLI_listbase_is_empty(&op->reports->list)) {
1785 wm_add_reports(op->reports);
1789 /* important to run 'wm_operator_finished' before NULLing the context members */
1790 if (retval & OPERATOR_FINISHED) {
1791 wm_operator_finished(C, op, false, true);
1794 else if (retval & (OPERATOR_CANCELLED | OPERATOR_FINISHED)) {
1795 WM_operator_free(op);
1799 /* putting back screen context, reval can pass trough after modal failures! */
1800 if ((retval & OPERATOR_PASS_THROUGH) || wm_event_always_pass(event)) {
1801 CTX_wm_area_set(C, area);
1802 CTX_wm_region_set(C, region);
1805 /* this special cases is for areas and regions that get removed */
1806 CTX_wm_area_set(C, NULL);
1807 CTX_wm_region_set(C, NULL);
1810 /* remove modal handler, operator itself should have been canceled and freed */
1811 if (retval & (OPERATOR_CANCELLED | OPERATOR_FINISHED)) {
1812 WM_cursor_grab_disable(CTX_wm_window(C), NULL);
1814 BLI_remlink(handlers, handler);
1815 wm_event_free_handler(handler);
1817 /* prevent silly errors from operator users */
1818 //retval &= ~OPERATOR_PASS_THROUGH;
1823 CLOG_ERROR(WM_LOG_HANDLERS, "missing modal '%s'", op->idname);
1827 wmOperatorType *ot = WM_operatortype_find(event->keymap_idname, 0);
1830 if (wm_operator_check_locked_interface(C, ot)) {
1831 retval = wm_operator_invoke(C, ot, event, properties, NULL, false);
1835 /* Finished and pass through flag as handled */
1837 /* Finished and pass through flag as handled */
1838 if (retval == (OPERATOR_FINISHED | OPERATOR_PASS_THROUGH))
1839 return WM_HANDLER_HANDLED;
1841 /* Modal unhandled, break */
1842 if (retval == (OPERATOR_PASS_THROUGH | OPERATOR_RUNNING_MODAL))
1843 return (WM_HANDLER_BREAK | WM_HANDLER_MODAL);
1845 if (retval & OPERATOR_PASS_THROUGH)
1846 return WM_HANDLER_CONTINUE;
1848 return WM_HANDLER_BREAK;
1851 /* fileselect handlers are only in the window queue, so it's safe to switch screens or area types */
1852 static int wm_handler_fileselect_do(bContext *C, ListBase *handlers, wmEventHandler *handler, int val)
1854 wmWindowManager *wm = CTX_wm_manager(C);
1856 int action = WM_HANDLER_CONTINUE;
1859 case EVT_FILESELECT_FULL_OPEN:
1863 /* sa can be null when window A is active, but mouse is over window B */
1864 /* in this case, open file select in original window A */
1865 if (handler->op_area == NULL) {
1866 bScreen *screen = CTX_wm_screen(C);
1867 sa = (ScrArea *)screen->areabase.first;
1870 sa = handler->op_area;
1874 /* ensure the first area becomes the file browser, because the second one is the small
1875 * top (info-)area which might be too small (in fullscreens we have max two areas) */
1879 ED_area_newspace(C, sa, SPACE_FILE, true); /* 'sa' is modified in-place */
1880 /* we already had a fullscreen here -> mark new space as a stacked fullscreen */
1881 sa->flag |= (AREA_FLAG_STACKED_FULLSCREEN | AREA_FLAG_TEMP_TYPE);
1883 else if (sa->spacetype == SPACE_FILE) {
1884 sa = ED_screen_state_toggle(C, CTX_wm_window(C), sa, SCREENMAXIMIZED);
1887 sa = ED_screen_full_newspace(C, sa, SPACE_FILE); /* sets context */
1890 /* note, getting the 'sa' back from the context causes a nasty bug where the newly created
1891 * 'sa' != CTX_wm_area(C). removed the line below and set 'sa' in the 'if' above */
1892 /* sa = CTX_wm_area(C); */
1894 /* settings for filebrowser, sfile is not operator owner but sends events */
1895 sfile = (SpaceFile *)sa->spacedata.first;
1896 sfile->op = handler->op;
1898 ED_fileselect_set_params(sfile);
1900 action = WM_HANDLER_BREAK;
1904 case EVT_FILESELECT_EXEC:
1905 case EVT_FILESELECT_CANCEL:
1906 case EVT_FILESELECT_EXTERNAL_CANCEL:
1908 /* remlink now, for load file case before removing*/
1909 BLI_remlink(handlers, handler);
1911 if (val != EVT_FILESELECT_EXTERNAL_CANCEL) {
1912 ScrArea *sa = CTX_wm_area(C);
1915 ED_screen_full_prevspace(C, sa);
1917 /* user may have left fullscreen */
1919 ED_area_prevspace(C, sa);
1923 wm_handler_op_context(C, handler, CTX_wm_window(C)->eventstate);
1925 /* needed for UI_popup_menu_reports */
1927 if (val == EVT_FILESELECT_EXEC) {
1930 if (handler->op->type->flag & OPTYPE_UNDO)
1931 wm->op_undo_depth++;
1933 retval = handler->op->type->exec(C, handler->op);
1935 /* XXX check this carefully, CTX_wm_manager(C) == wm is a bit hackish */
1936 if (handler->op->type->flag & OPTYPE_UNDO && CTX_wm_manager(C) == wm)
1937 wm->op_undo_depth--;
1939 /* XXX check this carefully, CTX_wm_manager(C) == wm is a bit hackish */
1940 if (CTX_wm_manager(C) == wm && wm->op_undo_depth == 0) {
1941 if (handler->op->type->flag & OPTYPE_UNDO)
1942 ED_undo_push_op(C, handler->op);
1943 else if (handler->op->type->flag & OPTYPE_UNDO_GROUPED)
1944 ED_undo_grouped_push_op(C, handler->op);
1947 if (handler->op->reports->list.first) {
1949 /* FIXME, temp setting window, this is really bad!
1950 * only have because lib linking errors need to be seen by users :(
1951 * it can be removed without breaking anything but then no linking errors - campbell */
1952 wmWindow *win_prev = CTX_wm_window(C);
1953 ScrArea *area_prev = CTX_wm_area(C);
1954 ARegion *ar_prev = CTX_wm_region(C);
1956 if (win_prev == NULL)
1957 CTX_wm_window_set(C, CTX_wm_manager(C)->windows.first);
1959 BKE_report_print_level_set(handler->op->reports, RPT_WARNING);
1960 UI_popup_menu_reports(C, handler->op->reports);
1962 /* XXX - copied from 'wm_operator_finished()' */
1963 /* add reports to the global list, otherwise they are not seen */
1964 BLI_movelisttolist(&CTX_wm_reports(C)->list, &handler->op->reports->list);
1966 /* more hacks, since we meddle with reports, banner display doesn't happen automatic */
1967 WM_report_banner_show();
1969 CTX_wm_window_set(C, win_prev);
1970 CTX_wm_area_set(C, area_prev);
1971 CTX_wm_region_set(C, ar_prev);
1974 /* for WM_operator_pystring only, custom report handling is done above */
1975 wm_operator_reports(C, handler->op, retval, true);
1977 if (retval & OPERATOR_FINISHED) {
1978 WM_operator_last_properties_store(handler->op);
1981 if (retval & (OPERATOR_CANCELLED | OPERATOR_FINISHED)) {
1982 WM_operator_free(handler->op);
1986 if (handler->op->type->cancel) {
1987 if (handler->op->type->flag & OPTYPE_UNDO)
1988 wm->op_undo_depth++;
1990 handler->op->type->cancel(C, handler->op);
1992 if (handler->op->type->flag & OPTYPE_UNDO)
1993 wm->op_undo_depth--;
1996 WM_operator_free(handler->op);
1999 CTX_wm_area_set(C, NULL);
2001 wm_event_free_handler(handler);
2003 action = WM_HANDLER_BREAK;
2011 static int wm_handler_fileselect_call(bContext *C, ListBase *handlers, wmEventHandler *handler, const wmEvent *event)
2013 int action = WM_HANDLER_CONTINUE;
2015 if (event->type != EVT_FILESELECT)
2017 if (handler->op != (wmOperator *)event->customdata)
2020 return wm_handler_fileselect_do(C, handlers, handler, event->val);
2023 static bool handler_boundbox_test(wmEventHandler *handler, const wmEvent *event)
2025 if (handler->bbwin) {
2026 if (handler->bblocal) {
2027 rcti rect = *handler->bblocal;
2028 BLI_rcti_translate(&rect, handler->bbwin->xmin, handler->bbwin->ymin);
2030 if (BLI_rcti_isect_pt_v(&rect, &event->x))
2032 else if (event->type == MOUSEMOVE && BLI_rcti_isect_pt_v(&rect, &event->prevx))
2038 if (BLI_rcti_isect_pt_v(handler->bbwin, &event->x))
2040 else if (event->type == MOUSEMOVE && BLI_rcti_isect_pt_v(handler->bbwin, &event->prevx))
2049 static int wm_action_not_handled(int action)
2051 return action == WM_HANDLER_CONTINUE || action == (WM_HANDLER_BREAK | WM_HANDLER_MODAL);
2054 static int wm_handlers_do_intern(bContext *C, wmEvent *event, ListBase *handlers)
2056 const bool do_debug_handler = (G.debug & G_DEBUG_HANDLERS) &&
2057 /* comment this out to flood the console! (if you really want to test) */
2058 !ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE)
2060 # define PRINT if (do_debug_handler) printf
2062 wmWindowManager *wm = CTX_wm_manager(C);
2063 wmEventHandler *handler, *nexthandler;
2064 int action = WM_HANDLER_CONTINUE;
2067 if (handlers == NULL) {
2071 /* modal handlers can get removed in this loop, we keep the loop this way
2073 * note: check 'handlers->first' because in rare cases the handlers can be cleared
2074 * by the event thats called, for eg:
2076 * Calling a python script which changes the area.type, see [#32232] */
2077 for (handler = handlers->first; handler && handlers->first; handler = nexthandler) {
2079 nexthandler = handler->next;
2081 /* during this loop, ui handlers for nested menus can tag multiple handlers free */
2082 if (handler->flag & WM_HANDLER_DO_FREE) {
2085 else if (handler_boundbox_test(handler, event)) { /* optional boundbox */
2086 /* in advance to avoid access to freed event on window close */
2087 always_pass = wm_event_always_pass(event);
2089 /* modal+blocking handler */
2090 if (handler->flag & WM_HANDLER_BLOCKING)
2091 action |= WM_HANDLER_BREAK;
2093 if (handler->keymap) {
2094 wmKeyMap *keymap = WM_keymap_active(wm, handler->keymap);
2097 PRINT("%s: checking '%s' ...", __func__, keymap->idname);
2099 if (WM_keymap_poll(C, keymap)) {
2103 for (kmi = keymap->items.first; kmi; kmi = kmi->next) {
2104 if (wm_eventmatch(event, kmi)) {
2106 PRINT("%s: item matched '%s'\n", __func__, kmi->idname);
2108 /* weak, but allows interactive callback to not use rawkey */
2109 event->keymap_idname = kmi->idname;
2111 action |= wm_handler_operator_call(C, handlers, handler, event, kmi->ptr);
2112 if (action & WM_HANDLER_BREAK) {
2113 /* not always_pass here, it denotes removed handler */
2114 CLOG_INFO(WM_LOG_HANDLERS, 2, "handled! '%s'", kmi->idname);
2118 if (action & WM_HANDLER_HANDLED) {
2119 CLOG_INFO(WM_LOG_HANDLERS, 2, "handled - and pass on! '%s'", kmi->idname);
2122 CLOG_INFO(WM_LOG_HANDLERS, 2, "un-handled '%s'", kmi->idname);
2132 else if (handler->ui_handle) {
2133 if (!wm->is_interface_locked) {
2134 action |= wm_handler_ui_call(C, handler, event, always_pass);
2137 else if (handler->type == WM_HANDLER_FILESELECT) {
2138 if (!wm->is_interface_locked) {
2139 /* screen context changes here */
2140 action |= wm_handler_fileselect_call(C, handlers, handler, event);
2143 else if (handler->dropboxes) {
2144 if (!wm->is_interface_locked && event->type == EVT_DROP) {
2145 wmDropBox *drop = handler->dropboxes->first;
2146 for (; drop; drop = drop->next) {
2147 /* other drop custom types allowed */
2148 if (event->custom == EVT_DATA_DRAGDROP) {
2149 ListBase *lb = (ListBase *)event->customdata;
2152 for (drag = lb->first; drag; drag = drag->next) {
2153 if (drop->poll(C, drag, event)) {
2154 drop->copy(drag, drop);
2156 /* free the drags before calling operator */
2157 WM_drag_free_list(lb);
2159 event->customdata = NULL;
2162 WM_operator_name_call_ptr(C, drop->ot, drop->opcontext, drop->ptr);
2163 action |= WM_HANDLER_BREAK;
2165 /* XXX fileread case */
2166 if (CTX_wm_window(C) == NULL)
2169 /* escape from drag loop, got freed */
2178 /* modal, swallows all */
2179 action |= wm_handler_operator_call(C, handlers, handler, event, NULL);
2182 if (action & WM_HANDLER_BREAK) {
2184 action &= ~WM_HANDLER_BREAK;
2190 /* XXX fileread case, if the wm is freed then the handler's
2191 * will have been too so the code below need not run. */
2192 if (CTX_wm_window(C) == NULL) {
2196 /* XXX code this for all modal ops, and ensure free only happens here */
2198 /* modal ui handler can be tagged to be freed */
2199 if (BLI_findindex(handlers, handler) != -1) { /* could be freed already by regular modal ops */
2200 if (handler->flag & WM_HANDLER_DO_FREE) {
2201 BLI_remlink(handlers, handler);
2202 wm_event_free_handler(handler);
2207 if (action == (WM_HANDLER_BREAK | WM_HANDLER_MODAL))
2208 wm_cursor_arrow_move(CTX_wm_window(C), event);
2215 /* this calls handlers twice - to solve (double-)click events */
2216 static int wm_handlers_do(bContext *C, wmEvent *event, ListBase *handlers)
2218 int action = wm_handlers_do_intern(C, event, handlers);
2221 if (CTX_wm_window(C) == NULL)
2224 if (ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE)) {
2225 if (event->check_drag) {
2226 wmWindow *win = CTX_wm_window(C);
2227 if ((abs(event->x - win->eventstate->prevclickx)) >= U.tweak_threshold ||
2228 (abs(event->y - win->eventstate->prevclicky)) >= U.tweak_threshold)
2230 short val = event->val;
2231 short type = event->type;
2232 event->val = KM_CLICK_DRAG;
2233 event->type = win->eventstate->type;
2235 CLOG_INFO(WM_LOG_HANDLERS, 1, "handling PRESS_DRAG");
2237 action |= wm_handlers_do_intern(C, event, handlers);
2242 win->eventstate->check_click = 0;
2243 win->eventstate->check_drag = 0;
2247 else if (!ELEM(event->type, EVENT_NONE) && !ISTIMER(event->type)) {
2248 /* test for CLICK events */
2249 if (wm_action_not_handled(action)) {
2250 wmWindow *win = CTX_wm_window(C);
2252 /* eventstate stores if previous event was a KM_PRESS, in case that
2253 * wasn't handled, the KM_RELEASE will become a KM_CLICK */
2255 if (win && event->val == KM_PRESS) {
2256 win->eventstate->check_click = true;
2257 win->eventstate->check_drag = true;
2260 if (win && win->eventstate->prevtype == event->type) {
2262 if ((event->val == KM_RELEASE) &&
2263 (win->eventstate->prevval == KM_PRESS) &&
2264 (win->eventstate->check_click == true))
2266 if ((abs(event->x - win->eventstate->prevclickx)) <= WM_EVENT_CLICK_WIGGLE_ROOM &&
2267 (abs(event->y - win->eventstate->prevclicky)) <= WM_EVENT_CLICK_WIGGLE_ROOM)
2269 event->val = KM_CLICK;
2271 CLOG_INFO(WM_LOG_HANDLERS, 1, "handling CLICK");
2273 action |= wm_handlers_do_intern(C, event, handlers);
2275 event->val = KM_RELEASE;
2278 win->eventstate->check_click = 0;
2279 win->eventstate->check_drag = 0;
2282 else if (event->val == KM_DBL_CLICK) {
2283 event->val = KM_PRESS;
2284 action |= wm_handlers_do_intern(C, event, handlers);
2286 /* revert value if not handled */
2287 if (wm_action_not_handled(action)) {
2288 event->val = KM_DBL_CLICK;
2294 wmWindow *win = CTX_wm_window(C);
2297 win->eventstate->check_click = 0;
2304 static int wm_event_inside_i(wmEvent *event, rcti *rect)
2306 if (wm_event_always_pass(event))
2308 if (BLI_rcti_isect_pt_v(rect, &event->x))
2313 static ScrArea *area_event_inside(bContext *C, const int xy[2])
2315 bScreen *screen = CTX_wm_screen(C);
2319 for (sa = screen->areabase.first; sa; sa = sa->next)
2320 if (BLI_rcti_isect_pt_v(&sa->totrct, xy))
2325 static ARegion *region_event_inside(bContext *C, const int xy[2])
2327 bScreen *screen = CTX_wm_screen(C);
2328 ScrArea *area = CTX_wm_area(C);
2332 for (ar = area->regionbase.first; ar; ar = ar->next)
2333 if (BLI_rcti_isect_pt_v(&ar->winrct, xy))
2338 static void wm_paintcursor_tag(bContext *C, wmPaintCursor *pc, ARegion *ar)
2341 for (; pc; pc = pc->next) {
2342 if (pc->poll == NULL || pc->poll(C)) {
2343 wmWindow *win = CTX_wm_window(C);
2344 WM_paint_cursor_tag_redraw(win, ar);
2350 /* called on mousemove, check updates for paintcursors */
2351 /* context was set on active area and region */
2352 static void wm_paintcursor_test(bContext *C, const wmEvent *event)
2354 wmWindowManager *wm = CTX_wm_manager(C);
2356 if (wm->paintcursors.first) {
2357 ARegion *ar = CTX_wm_region(C);
2360 wm_paintcursor_tag(C, wm->paintcursors.first, ar);
2362 /* if previous position was not in current region, we have to set a temp new context */
2363 if (ar == NULL || !BLI_rcti_isect_pt_v(&ar->winrct, &event->prevx)) {
2364 ScrArea *sa = CTX_wm_area(C);
2366 CTX_wm_area_set(C, area_event_inside(C, &event->prevx));
2367 CTX_wm_region_set(C, region_event_inside(C, &event->prevx));
2369 wm_paintcursor_tag(C, wm->paintcursors.first, CTX_wm_region(C));
2371 CTX_wm_area_set(C, sa);
2372 CTX_wm_region_set(C, ar);
2377 static void wm_event_drag_test(wmWindowManager *wm, wmWindow *win, wmEvent *event)
2379 if (BLI_listbase_is_empty(&wm->drags)) {
2383 if (event->type == MOUSEMOVE || ISKEYMODIFIER(event->type)) {
2384 win->screen->do_draw_drag = true;
2386 else if (event->type == ESCKEY) {
2387 WM_drag_free_list(&wm->drags);
2389 win->screen->do_draw_drag = true;
2391 else if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
2392 event->type = EVT_DROP;
2394 /* create customdata, first free existing */
2395 if (event->customdata) {
2396 if (event->customdatafree)
2397 MEM_freeN(event->customdata);
2400 event->custom = EVT_DATA_DRAGDROP;
2401 event->customdata = &wm->drags;
2402 event->customdatafree = 1;
2404 /* clear drop icon */
2405 win->screen->do_draw_drag = true;
2407 /* restore cursor (disabled, see wm_dragdrop.c) */
2408 // WM_cursor_modal_restore(win);
2411 /* overlap fails otherwise */
2412 if (win->screen->do_draw_drag)
2413 if (win->drawmethod == USER_DRAW_OVERLAP)
2414 win->screen->do_draw = true;
2418 /* filter out all events of the pie that spawned the last pie unless it's a release event */
2419 static bool wm_event_pie_filter(wmWindow *win, const wmEvent *event)
2421 if (win->lock_pie_event && win->lock_pie_event == event->type) {
2422 if (event->val == KM_RELEASE) {
2423 win->lock_pie_event = EVENT_NONE;
2435 /* called in main loop */
2436 /* goes over entire hierarchy: events -> window -> screen -> area -> region */
2437 void wm_event_do_handlers(bContext *C)
2439 wmWindowManager *wm = CTX_wm_manager(C);
2442 /* update key configuration before handling events */
2443 WM_keyconfig_update(wm);
2445 for (win = wm->windows.first; win; win = win->next) {
2448 if (win->screen == NULL)
2449 wm_event_free_all(win);
2451 Scene *scene = win->screen->scene;
2454 int is_playing_sound = BKE_sound_scene_playing(win->screen->scene);
2456 if (is_playing_sound != -1) {
2457 bool is_playing_screen;
2458 CTX_wm_window_set(C, win);
2459 CTX_wm_screen_set(C, win->screen);
2460 CTX_data_scene_set(C, scene);
2462 is_playing_screen = (ED_screen_animation_playing(wm) != NULL);
2464 if (((is_playing_sound == 1) && (is_playing_screen == 0)) ||
2465 ((is_playing_sound == 0) && (is_playing_screen == 1)))
2467 ED_screen_animation_play(C, -1, 1);
2470 if (is_playing_sound == 0) {
2471 const float time = BKE_sound_sync_scene(scene);
2472 if (isfinite(time)) {
2473 int ncfra = time * (float)FPS + 0.5f;
2474 if (ncfra != scene->r.cfra) {
2475 scene->r.cfra = ncfra;
2476 ED_update_for_newframe(CTX_data_main(C), scene, 1);
2477 WM_event_add_notifier(C, NC_WINDOW, NULL);
2482 CTX_data_scene_set(C, NULL);
2483 CTX_wm_screen_set(C, NULL);
2484 CTX_wm_window_set(C, NULL);
2489 while ( (event = win->queue.first) ) {
2490 int action = WM_HANDLER_CONTINUE;
2492 if (G.debug & (G_DEBUG_HANDLERS | G_DEBUG_EVENTS) && !ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE)) {
2493 printf("\n%s: Handling event\n", __func__);
2495 WM_event_print(event);
2498 /* take care of pie event filter */
2499 if (wm_event_pie_filter(win, event)) {
2500 if (!ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE)) {
2501 CLOG_INFO(WM_LOG_HANDLERS, 1, "event filtered due to pie button pressed");
2503 BLI_remlink(&win->queue, event);
2504 wm_event_free(event);
2508 CTX_wm_window_set(C, win);
2510 /* Clear tool-tip on mouse move. */
2511 if (win->screen->tool_tip && win->screen->tool_tip->exit_on_event) {
2512 if (ISMOUSE(event->type)) {
2513 WM_tooltip_clear(C, win);
2517 /* we let modal handlers get active area/region, also wm_paintcursor_test needs it */
2518 CTX_wm_area_set(C, area_event_inside(C, &event->x));
2519 CTX_wm_region_set(C, region_event_inside(C, &event->x));
2521 /* MVC demands to not draw in event handlers... but we need to leave it for ogl selecting etc */
2522 wm_window_make_drawable(wm, win);
2524 wm_region_mouse_co(C, event);
2527 /* first we do priority handlers, modal + some limited keymaps */
2528 action |= wm_handlers_do(C, event, &win->modalhandlers);
2531 if (CTX_wm_window(C) == NULL)
2534 /* check for a tooltip */
2536 bScreen *screen = CTX_wm_window(C)->screen;
2537 if (screen->tool_tip && screen->tool_tip->timer) {
2538 if ((event->type == TIMER) && (event->customdata == screen->tool_tip->timer)) {
2539 WM_tooltip_init(C, win);
2544 /* check dragging, creates new event or frees, adds draw tag */
2545 wm_event_drag_test(wm, win, event);
2547 /* builtin tweak, if action is break it removes tweak */
2548 wm_tweakevent_test(C, event, action);
2550 if ((action & WM_HANDLER_BREAK) == 0) {
2554 /* Note: setting subwin active should be done here, after modal handlers have been done */
2555 if (event->type == MOUSEMOVE) {
2556 /* state variables in screen, cursors. Also used in wm_draw.c, fails for modal handlers though */
2557 ED_screen_set_subwinactive(C, event);
2558 /* for regions having custom cursors */
2559 wm_paintcursor_test(C, event);
2561 #ifdef WITH_INPUT_NDOF
2562 else if (event->type == NDOF_MOTION) {
2563 win->addmousemove = true;
2567 for (sa = win->screen->areabase.first; sa; sa = sa->next) {
2568 /* after restoring a screen from SCREENMAXIMIZED we have to wait
2569 * with the screen handling till the region coordinates are updated */
2570 if (win->screen->skip_handling == true) {
2571 /* restore for the next iteration of wm_event_do_handlers */
2572 win->screen->skip_handling = false;
2576 /* update azones if needed - done here because it needs to be independent from redraws */
2577 if (sa->flag & AREA_FLAG_ACTIONZONES_UPDATE) {
2578 ED_area_azones_update(sa, &event->x);
2581 if (wm_event_inside_i(event, &sa->totrct)) {
2582 CTX_wm_area_set(C, sa);
2584 if ((action & WM_HANDLER_BREAK) == 0) {
2585 for (ar = sa->regionbase.first; ar; ar = ar->next) {
2586 if (wm_event_inside_i(event, &ar->winrct)) {
2587 CTX_wm_region_set(C, ar);
2589 /* call even on non mouse events, since the */
2590 wm_region_mouse_co(C, event);
2592 if (!BLI_listbase_is_empty(&wm->drags)) {
2593 /* does polls for drop regions and checks uibuts */
2594 /* need to be here to make sure region context is true */
2595 if (ELEM(event->type, MOUSEMOVE, EVT_DROP) || ISKEYMODIFIER(event->type)) {
2596 wm_drags_check_ops(C, event);
2600 action |= wm_handlers_do(C, event, &ar->handlers);
2602 /* fileread case (python), [#29489] */
2603 if (CTX_wm_window(C) == NULL)
2606 if (action & WM_HANDLER_BREAK)
2612 CTX_wm_region_set(C, NULL);
2614 if ((action & WM_HANDLER_BREAK) == 0) {
2615 wm_region_mouse_co(C, event); /* only invalidates event->mval in this case */
2616 action |= wm_handlers_do(C, event, &sa->handlers);
2618 CTX_wm_area_set(C, NULL);
2620 /* NOTE: do not escape on WM_HANDLER_BREAK, mousemove needs handled for previous area */
2624 if ((action & WM_HANDLER_BREAK) == 0) {
2625 /* also some non-modal handlers need active area/region */
2626 CTX_wm_area_set(C, area_event_inside(C, &event->x));
2627 CTX_wm_region_set(C, region_event_inside(C, &event->x));
2629 wm_region_mouse_co(C, event);
2631 action |= wm_handlers_do(C, event, &win->handlers);
2634 if (CTX_wm_window(C) == NULL)
2640 /* update previous mouse position for following events to use */
2641 win->eventstate->prevx = event->x;
2642 win->eventstate->prevy = event->y;
2644 /* unlink and free here, blender-quit then frees all */
2645 BLI_remlink(&win->queue, event);
2646 wm_event_free(event);
2650 /* only add mousemove when queue was read entirely */
2651 if (win->addmousemove && win->eventstate) {
2652 wmEvent tevent = *(win->eventstate);
2653 // printf("adding MOUSEMOVE %d %d\n", tevent.x, tevent.y);
2654 tevent.type = MOUSEMOVE;
2655 tevent.prevx = tevent.x;
2656 tevent.prevy = tevent.y;
2657 wm_event_add(win, &tevent);
2658 win->addmousemove = 0;
2661 CTX_wm_window_set(C, NULL);
2664 /* update key configuration after handling events */
2665 WM_keyconfig_update(wm);
2667 GPU_ASSERT_NO_GL_ERRORS("wm_event_do_handlers");
2670 /* ********** filesector handling ************ */
2672 void WM_event_fileselect_event(wmWindowManager *wm, void *ophandle, int eventval)
2674 /* add to all windows! */
2677 for (win = wm->windows.first; win; win = win->next) {
2678 wmEvent event = *win->eventstate;
2680 event.type = EVT_FILESELECT;
2681 event.val = eventval;
2682 event.customdata = ophandle; // only as void pointer type check
2684 wm_event_add(win, &event);
2688 /* operator is supposed to have a filled "path" property */
2689 /* optional property: filetype (XXX enum?) */
2692 * The idea here is to keep a handler alive on window queue, owning the operator.
2693 * The filewindow can send event to make it execute, thus ensuring
2694 * executing happens outside of lower level queues, with UI refreshed.
2695 * Should also allow multiwin solutions
2697 void WM_event_add_fileselect(bContext *C, wmOperator *op)
2699 wmEventHandler *handler, *handlernext;
2700 wmWindowManager *wm = CTX_wm_manager(C);
2701 wmWindow *win = CTX_wm_window(C);
2703 /* only allow 1 file selector open per window */
2704 for (handler = win->modalhandlers.first; handler; handler = handlernext) {
2705 handlernext = handler->next;
2707 if (handler->type == WM_HANDLER_FILESELECT) {
2708 bScreen *screen = CTX_wm_screen(C);
2711 /* find the area with the file selector for this handler */
2712 for (sa = screen->areabase.first; sa; sa = sa->next) {
2713 if (sa->spacetype == SPACE_FILE) {
2714 SpaceFile *sfile = sa->spacedata.first;
2716 if (sfile->op == handler->op) {
2717 CTX_wm_area_set(C, sa);
2718 wm_handler_fileselect_do(C, &win->modalhandlers, handler, EVT_FILESELECT_CANCEL);
2724 /* if not found we stop the handler without changing the screen */
2726 wm_handler_fileselect_do(C, &win->modalhandlers, handler, EVT_FILESELECT_EXTERNAL_CANCEL);
2730 handler = MEM_callocN(sizeof(wmEventHandler), "fileselect handler");
2732 handler->type = WM_HANDLER_FILESELECT;
2734 handler->op_area = CTX_wm_area(C);
2735 handler->op_region = CTX_wm_region(C);
2737 BLI_addhead(&win->modalhandlers, handler);
2739 /* check props once before invoking if check is available
2740 * ensures initial properties are valid */
2741 if (op->type->check) {
2742 op->type->check(C, op); /* ignore return value */
2745 WM_event_fileselect_event(wm, op, EVT_FILESELECT_FULL_OPEN);
2749 /* lets not expose struct outside wm? */
2750 static void WM_event_set_handler_flag(wmEventHandler *handler, int flag)
2752 handler->flag = flag;
2756 wmEventHandler *WM_event_add_modal_handler(bContext *C, wmOperator *op)
2758 wmEventHandler *handler = MEM_callocN(sizeof(wmEventHandler), "event modal handler");
2759 wmWindow *win = CTX_wm_window(C);
2761 /* operator was part of macro */
2763 /* give the mother macro to the handler */
2764 handler->op = op->opm;
2765 /* mother macro opm becomes the macro element */
2766 handler->op->opm = op;
2771 handler->op_area = CTX_wm_area(C); /* means frozen screen context for modal handlers! */
2772 handler->op_region = CTX_wm_region(C);
2773 handler->op_region_type = handler->op_region ? handler->op_region->regiontype : -1;
2775 BLI_addhead(&win->modalhandlers, handler);
2780 wmEventHandler *WM_event_add_keymap_handler(ListBase *handlers, wmKeyMap *keymap)
2782 wmEventHandler *handler;
2785 CLOG_WARN(WM_LOG_HANDLERS, "called with NULL keymap");
2789 /* only allow same keymap once */
2790 for (handler = handlers->first; handler; handler = handler->next)
2791 if (handler->keymap == keymap)
2794 handler = MEM_callocN(sizeof(wmEventHandler), "event keymap handler");
2795 BLI_addtail(handlers, handler);
2796 handler->keymap = keymap;
2801 /* priorities not implemented yet, for time being just insert in begin of list */
2802 wmEventHandler *WM_event_add_keymap_handler_priority(ListBase *handlers, wmKeyMap *keymap, int UNUSED(priority))
2804 wmEventHandler *handler;
2806 WM_event_remove_keymap_handler(handlers, keymap);
2808 handler = MEM_callocN(sizeof(wmEventHandler), "event keymap handler");
2809 BLI_addhead(handlers, handler);
2810 handler->keymap = keymap;
2815 wmEventHandler *WM_event_add_keymap_handler_bb(ListBase *handlers, wmKeyMap *keymap, const rcti *bblocal, const rcti *bbwin)
2817 wmEventHandler *handler = WM_event_add_keymap_handler(handlers, keymap);
2820 handler->bblocal = bblocal;
2821 handler->bbwin = bbwin;
2826 void WM_event_remove_keymap_handler(ListBase *handlers, wmKeyMap *keymap)
2828 wmEventHandler *handler;
2830 for (handler = handlers->first; handler; handler = handler->next) {
2831 if (handler->keymap == keymap) {
2832 BLI_remlink(handlers, handler);
2833 wm_event_free_handler(handler);
2839 wmEventHandler *WM_event_add_ui_handler(
2840 const bContext *C, ListBase *handlers,
2841 wmUIHandlerFunc ui_handle, wmUIHandlerRemoveFunc ui_remove,
2842 void *userdata, const char flag)
2844 wmEventHandler *handler = MEM_callocN(sizeof(wmEventHandler), "event ui handler");
2845 handler->ui_handle = ui_handle;
2846 handler->ui_remove = ui_remove;
2847 handler->ui_userdata = userdata;
2849 handler->ui_area = CTX_wm_area(C);
2850 handler->ui_region = CTX_wm_region(C);
2851 handler->ui_menu = CTX_wm_menu(C);
2854 handler->ui_area = NULL;
2855 handler->ui_region = NULL;
2856 handler->ui_menu = NULL;
2859 BLI_assert((flag & WM_HANDLER_DO_FREE) == 0);
2860 handler->flag = flag;
2862 BLI_addhead(handlers, handler);
2867 /* set "postpone" for win->modalhandlers, this is in a running for () loop in wm_handlers_do() */
2868 void WM_event_remove_ui_handler(
2870 wmUIHandlerFunc ui_handle, wmUIHandlerRemoveFunc ui_remove,
2871 void *userdata, const bool postpone)
2873 wmEventHandler *handler;
2875 for (handler = handlers->first; handler; handler = handler->next) {
2876 if ((handler->ui_handle == ui_handle) &&
2877 (handler->ui_remove == ui_remove) &&
2878 (handler->ui_userdata == userdata))
2880 /* handlers will be freed in wm_handlers_do() */
2882 handler->flag |= WM_HANDLER_DO_FREE;
2885 BLI_remlink(handlers, handler);
2886 wm_event_free_handler(handler);