[Bf-blender-cvs] [d2de6e3b510] temp-wm-event-api: Experimental event playback, use for testing events

Campbell Barton noreply at git.blender.org
Wed Jan 30 08:39:19 CET 2019


Commit: d2de6e3b5109351e47fe8f2c0d3b8612d6a905a2
Author: Campbell Barton
Date:   Wed Jan 30 16:58:41 2019 +1100
Branches: temp-wm-event-api
https://developer.blender.org/rBd2de6e3b5109351e47fe8f2c0d3b8612d6a905a2

Experimental event playback, use for testing events

Useful for the undo system which cant easily be tested any other way.

===================================================================

M	source/blender/blenkernel/BKE_global.h
M	source/blender/makesrna/intern/rna_wm_api.c
M	source/blender/python/intern/bpy_app.c
M	source/blender/windowmanager/WM_api.h
M	source/blender/windowmanager/intern/wm_event_system.c
M	source/blender/windowmanager/intern/wm_window.c
M	source/creator/creator_args.c
A	tests/python/event_simulate/modules/easy_keys.py
A	tests/python/event_simulate/sculpt_undo.py

===================================================================

diff --git a/source/blender/blenkernel/BKE_global.h b/source/blender/blenkernel/BKE_global.h
index 0b1a45028c1..646547d3520 100644
--- a/source/blender/blenkernel/BKE_global.h
+++ b/source/blender/blenkernel/BKE_global.h
@@ -159,10 +159,13 @@ enum {
 	G_DEBUG_IO =        (1 << 17),  /* IO Debugging (for Collada, ...)*/
 	G_DEBUG_GPU_SHADERS = (1 << 18),  /* GLSL shaders */
 	G_DEBUG_GPU_FORCE_WORKAROUNDS = (1 << 19),  /* force gpu workarounds bypassing detections. */
+	G_DEBUG_EVENTS_SIMULATE = (1 << 20),  /* input/window/screen events */
 };
 
-#define G_DEBUG_ALL  (G_DEBUG | G_DEBUG_FFMPEG | G_DEBUG_PYTHON | G_DEBUG_EVENTS | G_DEBUG_WM | G_DEBUG_JOBS | \
-                      G_DEBUG_FREESTYLE | G_DEBUG_DEPSGRAPH | G_DEBUG_GPU_MEM | G_DEBUG_IO | G_DEBUG_GPU_SHADERS)
+#define G_DEBUG_ALL \
+	(G_DEBUG | G_DEBUG_FFMPEG | G_DEBUG_PYTHON | G_DEBUG_EVENTS | G_DEBUG_WM | G_DEBUG_JOBS | \
+	 G_DEBUG_FREESTYLE | G_DEBUG_DEPSGRAPH | G_DEBUG_GPU_MEM | G_DEBUG_IO | G_DEBUG_GPU_SHADERS | \
+	 G_DEBUG_EVENTS_SIMULATE)
 
 
 /* G.fileflags */
diff --git a/source/blender/makesrna/intern/rna_wm_api.c b/source/blender/makesrna/intern/rna_wm_api.c
index 9d93bcba508..6b39d5a02cb 100644
--- a/source/blender/makesrna/intern/rna_wm_api.c
+++ b/source/blender/makesrna/intern/rna_wm_api.c
@@ -31,6 +31,7 @@
 
 #include <stdlib.h>
 #include <stdio.h>
+#include <ctype.h>
 
 #include "BLI_utildefines.h"
 
@@ -475,6 +476,58 @@ static PointerRNA rna_WindoManager_operator_properties_last(const char *idname)
 	return PointerRNA_NULL;
 }
 
+static wmEvent *rna_Window_event_add_simulate(
+        wmWindow *win, ReportList *reports,
+        int type, int value, const char *unicode,
+        int x, int y,
+        bool shift, bool ctrl, bool alt, bool oskey)
+{
+	if ((G.debug & G_DEBUG_EVENTS_SIMULATE) == 0) {
+		BKE_report(reports, RPT_ERROR, "Not running with '--debug-events-simulate' enabled");
+		return NULL;
+	}
+	if (!ELEM(value, KM_PRESS, KM_RELEASE, KM_NOTHING)) {
+		BKE_report(reports, RPT_ERROR, "Only value: PRESS/RELEASE/NOTHING are supported");
+		return NULL;
+	}
+
+	char ascii = 0;
+	if (unicode != NULL) {
+		int len = BLI_str_utf8_size(unicode);
+		if (len == -1 || unicode[len] != '\0') {
+			BKE_report(reports, RPT_ERROR, "Only a single character supported");
+			return NULL;
+		}
+		if (len == 1 && isascii(unicode[0])) {
+			ascii = unicode[0];
+		}
+	}
+
+	wmEvent e = {NULL};
+	e.type = type;
+	e.val = value;
+	e.x = x;
+	e.y = y;
+	e.shift = shift;
+	e.ctrl = ctrl;
+	e.alt = alt;
+	e.oskey = oskey;
+
+	const wmEvent *evt = win->eventstate;
+	e.prevx = evt->x;
+	e.prevy = evt->y;
+	e.prevval = evt->val;
+	e.prevtype = evt->type;
+
+
+	if (unicode != NULL) {
+		e.ascii = ascii;
+		STRNCPY(e.utf8_buf, unicode);
+	}
+
+	return WM_event_add_simulate(win, &e);
+}
+
 #else
 
 #define WM_GEN_INVOKE_EVENT (1 << 0)
@@ -531,6 +584,26 @@ void RNA_api_window(StructRNA *srna)
 
 	RNA_def_function(srna, "cursor_modal_restore", "WM_cursor_modal_restore");
 	RNA_def_function_ui_description(func, "Restore the previous cursor after calling ``cursor_modal_set``");
+
+	/* Arguments match 'rna_KeyMap_item_new'. */
+	func = RNA_def_function(srna, "event_simulate", "rna_Window_event_add_simulate");
+	RNA_def_function_flag(func, FUNC_USE_REPORTS);
+	parm = RNA_def_enum(func, "type", rna_enum_event_type_items, 0, "Type", "");
+	RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
+	parm = RNA_def_enum(func, "value", rna_enum_event_value_items, 0, "Value", "");
+	RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
+	parm = RNA_def_string(func, "unicode", NULL, 0, "", "");
+	RNA_def_parameter_clear_flags(parm, PROP_NEVER_NULL, 0);
+
+	RNA_def_int(func, "x", 0, INT_MIN, INT_MAX, "", "", INT_MIN, INT_MAX);
+	RNA_def_int(func, "y", 0, INT_MIN, INT_MAX, "", "", INT_MIN, INT_MAX);
+
+	RNA_def_boolean(func, "shift", 0, "Shift", "");
+	RNA_def_boolean(func, "ctrl", 0, "Ctrl", "");
+	RNA_def_boolean(func, "alt", 0, "Alt", "");
+	RNA_def_boolean(func, "oskey", 0, "OS Key", "");
+	parm = RNA_def_pointer(func, "event", "Event", "Item", "Added key map item");
+	RNA_def_function_return(func, parm);
 }
 
 void RNA_api_wm(StructRNA *srna)
diff --git a/source/blender/python/intern/bpy_app.c b/source/blender/python/intern/bpy_app.c
index fe8eb768b3a..749ec4035ca 100644
--- a/source/blender/python/intern/bpy_app.c
+++ b/source/blender/python/intern/bpy_app.c
@@ -265,6 +265,16 @@ static int bpy_app_debug_set(PyObject *UNUSED(self), PyObject *value, void *clos
 	return 0;
 }
 
+static int bpy_app_debug_set__only_disable(PyObject *UNUSED(self), PyObject *value, void *closure)
+{
+	const int param = PyObject_IsTrue(value);
+	if (param) {
+		PyErr_SetString(PyExc_ValueError, "This bpy.app.debug option can only be disabled");
+		return -1
+	}
+	return bpy_app_debug_set(NULL, value, closure);
+}
+
 #define BROKEN_BINARY_PATH_PYTHON_HACK
 
 PyDoc_STRVAR(bpy_app_binary_path_python_doc,
@@ -394,6 +404,7 @@ static PyGetSetDef bpy_app_getsets[] = {
 	{(char *)"debug_freestyle", bpy_app_debug_get, bpy_app_debug_set, (char *)bpy_app_debug_doc, (void *)G_DEBUG_FREESTYLE},
 	{(char *)"debug_python",    bpy_app_debug_get, bpy_app_debug_set, (char *)bpy_app_debug_doc, (void *)G_DEBUG_PYTHON},
 	{(char *)"debug_events",    bpy_app_debug_get, bpy_app_debug_set, (char *)bpy_app_debug_doc, (void *)G_DEBUG_EVENTS},
+	{(char *)"debug_events_simulate", bpy_app_debug_get, bpy_app_debug_set__only_disable, (char *)bpy_app_debug_doc, (void *)G_DEBUG_EVENTS_SIMULATE},
 	{(char *)"debug_handlers",  bpy_app_debug_get, bpy_app_debug_set, (char *)bpy_app_debug_doc, (void *)G_DEBUG_HANDLERS},
 	{(char *)"debug_wm",        bpy_app_debug_get, bpy_app_debug_set, (char *)bpy_app_debug_doc, (void *)G_DEBUG_WM},
 	{(char *)"debug_depsgraph", bpy_app_debug_get, bpy_app_debug_set, (char *)bpy_app_debug_doc, (void *)G_DEBUG_DEPSGRAPH},
diff --git a/source/blender/windowmanager/WM_api.h b/source/blender/windowmanager/WM_api.h
index 16a9144e8b0..1b424ecb4c3 100644
--- a/source/blender/windowmanager/WM_api.h
+++ b/source/blender/windowmanager/WM_api.h
@@ -623,6 +623,9 @@ bool        WM_event_is_tablet(const struct wmEvent *event);
 bool        WM_event_is_ime_switch(const struct wmEvent *event);
 #endif
 
+/* For testing only 'G_DEBUG_EVENTS_SIMULATE' */
+struct wmEvent *WM_event_add_simulate(struct wmWindow *win, const struct wmEvent *event_to_add);
+
 const char *WM_window_cursor_keymap_status_get(const struct wmWindow *win, int button_index, int type_index);
 void WM_window_cursor_keymap_status_refresh(struct bContext *C, struct wmWindow *win);
 
diff --git a/source/blender/windowmanager/intern/wm_event_system.c b/source/blender/windowmanager/intern/wm_event_system.c
index 64b3ca6a300..876504ae5df 100644
--- a/source/blender/windowmanager/intern/wm_event_system.c
+++ b/source/blender/windowmanager/intern/wm_event_system.c
@@ -134,6 +134,18 @@ wmEvent *wm_event_add(wmWindow *win, const wmEvent *event_to_add)
 	return wm_event_add_ex(win, event_to_add, NULL);
 }
 
+wmEvent *WM_event_add_simulate(wmWindow *win, const wmEvent *event_to_add)
+{
+	if ((G.debug & G_DEBUG_EVENTS_SIMULATE) == 0) {
+		BLI_assert(0);
+		return NULL;
+	}
+	wmEvent *event = wm_event_add(win, event_to_add);
+	win->eventstate->x = event->x;
+	win->eventstate->y = event->y;
+	return event;
+}
+
 void wm_event_free(wmEvent *event)
 {
 	if (event->customdata) {
@@ -3906,6 +3918,10 @@ void wm_event_add_ghostevent(wmWindowManager *wm, wmWindow *win, int type, int U
 {
 	wmWindow *owin;
 
+	if (UNLIKELY(G.debug & G_DEBUG_EVENTS_SIMULATE)) {
+		return;
+	}
+
 	/* Having both, event and evt, can be highly confusing to work with, but is necessary for
 	 * our current event system, so let's clear things up a bit:
 	 * - data added to event only will be handled immediately, but will not be copied to the next event
diff --git a/source/blender/windowmanager/intern/wm_window.c b/source/blender/windowmanager/intern/wm_window.c
index 5d5be86949c..40488d2f9dd 100644
--- a/source/blender/windowmanager/intern/wm_window.c
+++ b/source/blender/windowmanager/intern/wm_window.c
@@ -1055,6 +1055,11 @@ void wm_cursor_position_to_ghost(wmWindow *win, int *x, int *y)
 
 void wm_get_cursor_position(wmWindow *win, int *x, int *y)
 {
+	if (UNLIKELY(G.debug & G_DEBUG_EVENTS_SIMULATE)) {
+		*x = win->eventstate->x;
+		*y = win->eventstate->y;
+		return;
+	}
 	GHOST_GetCursorPosition(g_system, x, y);
 	wm_cursor_position_from_ghost(win, x, y);
 }
diff --git a/source/creator/creator_args.c b/source/creator/creator_args.c
index e4569a7f0c6..02e8a38316e 100644
--- a/source/creator/creator_args.c
+++ b/source/creator/creator_args.c
@@ -535,6 +535,7 @@ static int arg_handle_print_help(int UNUSED(argc), const char **UNUSED(argv), vo
 
 	printf("\n");
 	BLI_argsPrintArgDoc(ba, "--debug-events");
+	BLI_argsPrintArgDoc(ba, "--debug-events-simulate");
 #ifdef WITH_FFMPEG
 	BLI_argsPrintArgDoc(ba, "--debug-ffmpeg");
 #endif
@@ -867,6 +868,8 @@ static const char arg_handle_debug_mode_generic_set_doc_python[] =
 "\n\tEnable debug messages for Python.";
 static const char arg_handle_debug_mode_generic_set_doc_events[] =
 "\n\tEnable debug messages for the event system.";
+static const char arg_handle_debug_mode_generic_set_doc_events_simulate[] =
+"\n\tEnable event simulation API: 'bpy.types.Window.event_simulate'.";
 static const char arg_handle_debug_mode_generic_set_doc_handlers[] =
 "\n\tEnable debug messages for event handling.";
 static const char arg_handle_debug_mode_generic_set_doc_wm[] =
@@ -1903,6 +1906,8 @@ void main_args_setup(bContext *C, bArgs *ba)
 	            CB_EX(arg_handle_debug_mode_generic_set, python), (void *)G_DEBUG_PYTHON);
 	BLI_argsAdd(ba, 1, NULL, "--debug-events",
 	            CB_EX(arg_handle_debug_mode_generic_set, events), (void *)G_DEBUG_EVENTS);
+	BLI_argsAdd(ba, 1, NULL, "--debug-events-simulate",
+	            CB_EX(arg_handle_debug_mode_generic_set, events_simulate), (void *)G_DEBUG_EVENTS_SIMULATE);
 	BLI_argsAdd(ba, 1, NULL, "--debug-handlers",
 	            CB_EX(arg_handle_debug_mode_generic_set, handlers), (void *)G_DEBUG_HANDLERS);
 	BLI_argsAdd(ba, 1, NULL, "--debug-wm",
diff --git a/tests/python/event_simulate/modules/easy_keys.py b/tests/python/event_simulate/modules/easy_keys.py
new

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list