Refactor: use const event data, use static_casts

In some cases processing events was modifying them, as there can be
multiple event consumers, manipulating events isn't correct.
Even though in practice it didn't cause issues, it's straightforward
not to do this and makes logic easier to reason about.
This commit is contained in:
Campbell Barton 2023-10-08 15:23:39 +11:00
parent 0bf7d15fa6
commit fc78182cc8
11 changed files with 76 additions and 87 deletions

View File

@ -35,26 +35,26 @@ class GHOST_IEvent {
* Returns the event type.
* \return The event type.
*/
virtual GHOST_TEventType getType() = 0;
virtual GHOST_TEventType getType() const = 0;
/**
* Returns the time this event was generated.
* \return The event generation time.
*/
virtual uint64_t getTime() = 0;
virtual uint64_t getTime() const = 0;
/**
* Returns the window this event was generated on,
* or nullptr if it is a 'system' event.
* \return The generating window.
*/
virtual GHOST_IWindow *getWindow() = 0;
virtual GHOST_IWindow *getWindow() const = 0;
/**
* Returns the event data.
* \return The event data.
*/
virtual GHOST_TEventDataPtr getData() = 0;
virtual GHOST_TEventDataPtr getData() const = 0;
#ifdef WITH_CXX_GUARDEDALLOC
MEM_CXX_CLASS_ALLOC_FUNCS("GHOST:GHOST_IEvent")

View File

@ -535,7 +535,7 @@ typedef enum {
GHOST_kAxisY = (1 << 1),
} GHOST_TAxisFlag;
typedef void *GHOST_TEventDataPtr;
typedef const void *GHOST_TEventDataPtr;
typedef struct {
/** The x-coordinate of the cursor position. */
@ -589,6 +589,8 @@ typedef enum {
GHOST_kDragnDropTypeBitmap /* Bitmap image data. */
} GHOST_TDragnDropTypes;
typedef void *GHOST_TDragnDropDataPtr;
typedef struct {
/** The x-coordinate of the cursor position. */
int32_t x;
@ -597,7 +599,7 @@ typedef struct {
/** The dropped item type */
GHOST_TDragnDropTypes dataType;
/** The "dropped content" */
GHOST_TEventDataPtr data;
GHOST_TDragnDropDataPtr data;
} GHOST_TEventDragnDropData;
/**

View File

@ -31,7 +31,7 @@ class GHOST_Event : public GHOST_IEvent {
* Returns the event type.
* \return The event type.
*/
GHOST_TEventType getType()
GHOST_TEventType getType() const
{
return m_type;
}
@ -40,7 +40,7 @@ class GHOST_Event : public GHOST_IEvent {
* Returns the time this event was generated.
* \return The event generation time.
*/
uint64_t getTime()
uint64_t getTime() const
{
return m_time;
}
@ -50,7 +50,7 @@ class GHOST_Event : public GHOST_IEvent {
* or nullptr if it is a 'system' event.
* \return The generating window.
*/
GHOST_IWindow *getWindow()
GHOST_IWindow *getWindow() const
{
return m_window;
}
@ -59,7 +59,7 @@ class GHOST_Event : public GHOST_IEvent {
* Returns the event data.
* \return The event data.
*/
GHOST_TEventDataPtr getData()
GHOST_TEventDataPtr getData() const
{
return m_data;
}

View File

@ -61,7 +61,7 @@ class GHOST_EventDragnDrop : public GHOST_Event {
GHOST_IWindow *window,
int x,
int y,
GHOST_TEventDataPtr data)
GHOST_TDragnDropDataPtr data)
: GHOST_Event(time, type, window)
{
m_dragnDropEventData.x = x;

View File

@ -40,6 +40,9 @@ bool GHOST_EventPrinter::processEvent(GHOST_IEvent *event)
if (event->getType() == GHOST_kEventWindowUpdate) {
return false;
}
GHOST_TEventDataPtr data = event->getData();
std::cout << "GHOST_EventPrinter::processEvent, time: " << int32_t(event->getTime())
<< ", type: ";
@ -59,30 +62,26 @@ bool GHOST_EventPrinter::processEvent(GHOST_IEvent *event)
break;
}
case GHOST_kEventCursorMove: {
GHOST_TEventCursorData *cursorData =
(GHOST_TEventCursorData *)((GHOST_IEvent *)event)->getData();
const GHOST_TEventCursorData *cursorData = static_cast<const GHOST_TEventCursorData *>(data);
std::cout << "GHOST_kEventCursorMove, (x,y): (" << cursorData->x << "," << cursorData->y
<< ")";
handled = true;
break;
}
case GHOST_kEventButtonDown: {
GHOST_TEventButtonData *buttonData =
(GHOST_TEventButtonData *)((GHOST_IEvent *)event)->getData();
const GHOST_TEventButtonData *buttonData = static_cast<const GHOST_TEventButtonData *>(data);
std::cout << "GHOST_kEventButtonDown, button: " << buttonData->button;
handled = true;
break;
}
case GHOST_kEventButtonUp: {
GHOST_TEventButtonData *buttonData =
(GHOST_TEventButtonData *)((GHOST_IEvent *)event)->getData();
const GHOST_TEventButtonData *buttonData = static_cast<const GHOST_TEventButtonData *>(data);
std::cout << "GHOST_kEventCursorButtonUp, button: " << buttonData->button;
handled = true;
break;
}
case GHOST_kEventWheel: {
GHOST_TEventWheelData *wheelData =
(GHOST_TEventWheelData *)((GHOST_IEvent *)event)->getData();
const GHOST_TEventWheelData *wheelData = static_cast<const GHOST_TEventWheelData *>(data);
std::cout << "GHOST_kEventWheel, z: " << wheelData->z;
handled = true;
break;
@ -93,7 +92,7 @@ bool GHOST_EventPrinter::processEvent(GHOST_IEvent *event)
#ifdef WITH_INPUT_NDOF
case GHOST_kEventNDOFMotion: {
const GHOST_TEventNDOFMotionData *ndof_motion =
(GHOST_TEventNDOFMotionData *)((GHOST_IEvent *)event)->getData();
static_cast<const GHOST_TEventNDOFMotionData *>(data);
std::cout << "GHOST_kEventNDOFMotion: ";
std::cout << std::fixed << std::setprecision(2) <<
/* Translation. */
@ -106,7 +105,7 @@ bool GHOST_EventPrinter::processEvent(GHOST_IEvent *event)
}
case GHOST_kEventNDOFButton: {
const GHOST_TEventNDOFButtonData *ndof_button =
(GHOST_TEventNDOFButtonData *)((GHOST_IEvent *)event)->getData();
static_cast<const GHOST_TEventNDOFButtonData *>(data);
std::cout << "GHOST_kEventNDOFButton: " << getButtonActionString(ndof_button->action)
<< " button=" << ndof_button->button;
handled = true;
@ -115,13 +114,13 @@ bool GHOST_EventPrinter::processEvent(GHOST_IEvent *event)
#endif /* WITH_INPUT_NDOF */
case GHOST_kEventKeyDown: {
GHOST_TEventKeyData *keyData = (GHOST_TEventKeyData *)((GHOST_IEvent *)event)->getData();
const GHOST_TEventKeyData *keyData = static_cast<const GHOST_TEventKeyData *>(data);
std::cout << "GHOST_kEventKeyDown, key: " << getKeyString(keyData->key);
handled = true;
break;
}
case GHOST_kEventKeyUp: {
GHOST_TEventKeyData *keyData = (GHOST_TEventKeyData *)((GHOST_IEvent *)event)->getData();
const GHOST_TEventKeyData *keyData = static_cast<const GHOST_TEventKeyData *>(data);
std::cout << "GHOST_kEventKeyUp, key: " << getKeyString(keyData->key);
handled = true;
break;
@ -138,8 +137,8 @@ bool GHOST_EventPrinter::processEvent(GHOST_IEvent *event)
CASE_TYPE(GHOST_kEventWindowDPIHintChanged);
case GHOST_kEventDraggingEntered: {
GHOST_TEventDragnDropData *dragnDropData =
(GHOST_TEventDragnDropData *)((GHOST_IEvent *)event)->getData();
const GHOST_TEventDragnDropData *dragnDropData =
static_cast<const GHOST_TEventDragnDropData *>(data);
std::cout << "GHOST_kEventDraggingEntered, dragged object type : "
<< dragnDropData->dataType;
std::cout << " mouse at x=" << dragnDropData->x << " y=" << dragnDropData->y;
@ -147,8 +146,8 @@ bool GHOST_EventPrinter::processEvent(GHOST_IEvent *event)
break;
}
case GHOST_kEventDraggingUpdated: {
GHOST_TEventDragnDropData *dragnDropData =
(GHOST_TEventDragnDropData *)((GHOST_IEvent *)event)->getData();
const GHOST_TEventDragnDropData *dragnDropData =
static_cast<const GHOST_TEventDragnDropData *>(data);
std::cout << "GHOST_kEventDraggingUpdated, dragged object type : "
<< dragnDropData->dataType;
std::cout << " mouse at x=" << dragnDropData->x << " y=" << dragnDropData->y;
@ -156,15 +155,15 @@ bool GHOST_EventPrinter::processEvent(GHOST_IEvent *event)
break;
}
case GHOST_kEventDraggingExited: {
GHOST_TEventDragnDropData *dragnDropData =
(GHOST_TEventDragnDropData *)((GHOST_IEvent *)event)->getData();
const GHOST_TEventDragnDropData *dragnDropData =
static_cast<const GHOST_TEventDragnDropData *>(data);
std::cout << "GHOST_kEventDraggingExited, dragged object type : " << dragnDropData->dataType;
handled = true;
break;
}
case GHOST_kEventDraggingDropDone: {
GHOST_TEventDragnDropData *dragnDropData =
(GHOST_TEventDragnDropData *)((GHOST_IEvent *)event)->getData();
const GHOST_TEventDragnDropData *dragnDropData =
static_cast<const GHOST_TEventDragnDropData *>(data);
std::cout << "GHOST_kEventDraggingDropDone,";
std::cout << " mouse at x=" << dragnDropData->x << " y=" << dragnDropData->y;
switch (dragnDropData->dataType) {
@ -192,10 +191,8 @@ bool GHOST_EventPrinter::processEvent(GHOST_IEvent *event)
break;
}
case GHOST_kEventOpenMainFile: {
GHOST_TEventDataPtr eventData = ((GHOST_IEvent *)event)->getData();
if (eventData) {
std::cout << "GHOST_kEventOpenMainFile for path : " << (char *)eventData;
if (data) {
std::cout << "GHOST_kEventOpenMainFile for path: " << static_cast<const char *>(data);
}
else {
std::cout << "GHOST_kEventOpenMainFile with no path specified!!";

View File

@ -35,7 +35,7 @@ class GHOST_EventString : public GHOST_Event {
~GHOST_EventString()
{
if (m_data) {
free(m_data);
free((void *)m_data);
}
}
};

View File

@ -1204,7 +1204,7 @@ GHOST_TSuccess GHOST_SystemCocoa::handleDraggingEvent(GHOST_TEventType eventType
NSArray *droppedArray;
size_t pastedTextSize;
NSString *droppedStr;
GHOST_TEventDataPtr eventData;
GHOST_TDragnDropDataPtr eventData;
int i;
if (!data) {
@ -1246,7 +1246,7 @@ GHOST_TSuccess GHOST_SystemCocoa::handleDraggingEvent(GHOST_TEventType eventType
strArray->strings[i] = temp_buff;
}
eventData = (GHOST_TEventDataPtr)strArray;
eventData = (GHOST_TDragnDropDataPtr)strArray;
break;
case GHOST_kDragnDropTypeString:
@ -1263,7 +1263,7 @@ GHOST_TSuccess GHOST_SystemCocoa::handleDraggingEvent(GHOST_TEventType eventType
temp_buff, [droppedStr cStringUsingEncoding:NSUTF8StringEncoding], pastedTextSize);
temp_buff[pastedTextSize] = '\0';
eventData = (GHOST_TEventDataPtr)temp_buff;
eventData = (GHOST_TDragnDropDataPtr)temp_buff;
break;
case GHOST_kDragnDropTypeBitmap: {
@ -1395,7 +1395,7 @@ GHOST_TSuccess GHOST_SystemCocoa::handleDraggingEvent(GHOST_TEventType eventType
[droppedImg release];
}
eventData = (GHOST_TEventDataPtr)ibuf;
eventData = (GHOST_TDragnDropDataPtr)ibuf;
break;
}

View File

@ -5449,7 +5449,10 @@ static bool wm_event_is_ignorable_key_press(const wmWindow *win, const wmEvent &
return wm_event_is_same_key_press(last_event, event);
}
void wm_event_add_ghostevent(wmWindowManager *wm, wmWindow *win, const int type, void *customdata)
void wm_event_add_ghostevent(wmWindowManager *wm,
wmWindow *win,
const int type,
const void *customdata)
{
if (UNLIKELY(G.f & G_FLAG_EVENT_SIMULATE)) {
return;
@ -5515,9 +5518,11 @@ void wm_event_add_ghostevent(wmWindowManager *wm, wmWindow *win, const int type,
switch (type) {
/* Mouse move, also to inactive window (X11 does this). */
case GHOST_kEventCursorMove: {
GHOST_TEventCursorData *cd = static_cast<GHOST_TEventCursorData *>(customdata);
const GHOST_TEventCursorData *cd = static_cast<const GHOST_TEventCursorData *>(customdata);
copy_v2_v2_int(event.xy, &cd->x);
wm_cursor_position_from_ghost_screen_coords(win, &event.xy[0], &event.xy[1]);
wm_stereo3d_mouse_offset_apply(win, event.xy);
wm_tablet_data_from_ghost(&cd->tablet, &event.tablet);
@ -5556,12 +5561,15 @@ void wm_event_add_ghostevent(wmWindowManager *wm, wmWindow *win, const int type,
break;
}
case GHOST_kEventTrackpad: {
GHOST_TEventTrackpadData *pd = static_cast<GHOST_TEventTrackpadData *>(customdata);
const GHOST_TEventTrackpadData *pd = static_cast<const GHOST_TEventTrackpadData *>(
customdata);
int delta[2] = {pd->deltaX, -pd->deltaY};
switch (pd->subtype) {
case GHOST_kTrackpadEventMagnify:
event.type = MOUSEZOOM;
pd->deltaX = -pd->deltaX;
pd->deltaY = -pd->deltaY;
delta[0] = -delta[0];
delta[1] = -delta[1];
break;
case GHOST_kTrackpadEventSmartMagnify:
event.type = MOUSESMARTZOOM;
@ -5575,8 +5583,9 @@ void wm_event_add_ghostevent(wmWindowManager *wm, wmWindow *win, const int type,
break;
}
event.xy[0] = event_state->xy[0] = pd->x;
event.xy[1] = event_state->xy[1] = pd->y;
copy_v2_v2_int(event.xy, &pd->x);
wm_cursor_position_from_ghost_screen_coords(win, &event.xy[0], &event.xy[1]);
copy_v2_v2_int(event_state->xy, event.xy);
event.val = KM_NOTHING;
/* The direction is inverted from the device due to system preferences. */
@ -5584,13 +5593,13 @@ void wm_event_add_ghostevent(wmWindowManager *wm, wmWindow *win, const int type,
event.flag |= WM_EVENT_SCROLL_INVERT;
}
wm_event_add_trackpad(win, &event, pd->deltaX, -pd->deltaY);
wm_event_add_trackpad(win, &event, delta[0], delta[1]);
break;
}
/* Mouse button. */
case GHOST_kEventButtonDown:
case GHOST_kEventButtonUp: {
GHOST_TEventButtonData *bd = static_cast<GHOST_TEventButtonData *>(customdata);
const GHOST_TEventButtonData *bd = static_cast<const GHOST_TEventButtonData *>(customdata);
/* Get value and type from Ghost. */
event.val = (type == GHOST_kEventButtonDown) ? KM_PRESS : KM_RELEASE;
@ -5653,7 +5662,7 @@ void wm_event_add_ghostevent(wmWindowManager *wm, wmWindow *win, const int type,
/* Keyboard. */
case GHOST_kEventKeyDown:
case GHOST_kEventKeyUp: {
GHOST_TEventKeyData *kd = static_cast<GHOST_TEventKeyData *>(customdata);
const GHOST_TEventKeyData *kd = static_cast<const GHOST_TEventKeyData *>(customdata);
event.type = convert_key(kd->key);
if (UNLIKELY(event.type == EVENT_NONE)) {
break;
@ -5813,7 +5822,8 @@ void wm_event_add_ghostevent(wmWindowManager *wm, wmWindow *win, const int type,
}
case GHOST_kEventWheel: {
GHOST_TEventWheelData *wheelData = static_cast<GHOST_TEventWheelData *>(customdata);
const GHOST_TEventWheelData *wheelData = static_cast<const GHOST_TEventWheelData *>(
customdata);
if (wheelData->z > 0) {
event.type = WHEELUPMOUSE;
@ -5840,7 +5850,8 @@ void wm_event_add_ghostevent(wmWindowManager *wm, wmWindow *win, const int type,
}
case GHOST_kEventNDOFButton: {
GHOST_TEventNDOFButtonData *e = static_cast<GHOST_TEventNDOFButtonData *>(customdata);
const GHOST_TEventNDOFButtonData *e = static_cast<const GHOST_TEventNDOFButtonData *>(
customdata);
event.type = NDOF_BUTTON_INDEX_AS_EVENT(e->button);

View File

@ -983,6 +983,7 @@ static bool ghost_event_proc(GHOST_EventHandle evt, GHOST_TUserDataPtr ps_void)
{
PlayState *ps = (PlayState *)ps_void;
const GHOST_TEventType type = GHOST_GetEventType(evt);
GHOST_TEventDataPtr data = GHOST_GetEventData(evt);
/* Convert ghost event into value keyboard or mouse. */
const int val = ELEM(type, GHOST_kEventKeyDown, GHOST_kEventButtonDown);
GHOST_SystemHandle ghost_system = ps->ghost_data.system;
@ -997,9 +998,7 @@ static bool ghost_event_proc(GHOST_EventHandle evt, GHOST_TUserDataPtr ps_void)
switch (type) {
case GHOST_kEventKeyDown:
case GHOST_kEventKeyUp: {
GHOST_TEventKeyData *key_data;
key_data = (GHOST_TEventKeyData *)GHOST_GetEventData(evt);
const GHOST_TEventKeyData *key_data = static_cast<const GHOST_TEventKeyData *>(data);
switch (key_data->key) {
case GHOST_kKeyEsc:
ps->loading = false;
@ -1027,9 +1026,7 @@ static bool ghost_event_proc(GHOST_EventHandle evt, GHOST_TUserDataPtr ps_void)
switch (type) {
case GHOST_kEventKeyDown:
case GHOST_kEventKeyUp: {
GHOST_TEventKeyData *key_data;
key_data = (GHOST_TEventKeyData *)GHOST_GetEventData(evt);
const GHOST_TEventKeyData *key_data = static_cast<const GHOST_TEventKeyData *>(data);
switch (key_data->key) {
case GHOST_kKeyA:
if (val) {
@ -1324,8 +1321,7 @@ static bool ghost_event_proc(GHOST_EventHandle evt, GHOST_TUserDataPtr ps_void)
}
case GHOST_kEventButtonDown:
case GHOST_kEventButtonUp: {
GHOST_TEventButtonData *bd = reinterpret_cast<GHOST_TEventButtonData *>(
GHOST_GetEventData(evt));
const GHOST_TEventButtonData *bd = static_cast<const GHOST_TEventButtonData *>(data);
int cx, cy, sizex, sizey;
playanim_window_get_size(ghost_window, &sizex, &sizey);
@ -1368,8 +1364,7 @@ static bool ghost_event_proc(GHOST_EventHandle evt, GHOST_TUserDataPtr ps_void)
}
case GHOST_kEventCursorMove: {
if (ps->ghost_data.qual & WS_QUAL_LMOUSE) {
GHOST_TEventCursorData *cd = reinterpret_cast<GHOST_TEventCursorData *>(
GHOST_GetEventData(evt));
const GHOST_TEventCursorData *cd = static_cast<const GHOST_TEventCursorData *>(data);
int cx, cy;
/* Ignore 'in-between' events, since they can make scrubbing lag.
@ -1431,11 +1426,10 @@ static bool ghost_event_proc(GHOST_EventHandle evt, GHOST_TUserDataPtr ps_void)
break;
}
case GHOST_kEventDraggingDropDone: {
GHOST_TEventDragnDropData *ddd = reinterpret_cast<GHOST_TEventDragnDropData *>(
GHOST_GetEventData(evt));
const GHOST_TEventDragnDropData *ddd = static_cast<const GHOST_TEventDragnDropData *>(data);
if (ddd->dataType == GHOST_kDragnDropTypeFilenames) {
GHOST_TStringArray *stra = reinterpret_cast<GHOST_TStringArray *>(ddd->data);
const GHOST_TStringArray *stra = static_cast<const GHOST_TStringArray *>(ddd->data);
int a;
for (a = 0; a < stra->count; a++) {

View File

@ -1486,7 +1486,7 @@ static bool ghost_event_proc(GHOST_EventHandle evt, GHOST_TUserDataPtr C_void_pt
}
case GHOST_kEventOpenMainFile: {
const char *path = static_cast<const char *>(GHOST_GetEventData(evt));
const char *path = static_cast<const char *>(data);
if (path) {
wmOperatorType *ot = WM_operatortype_find("WM_OT_open_mainfile", false);
@ -1506,8 +1506,8 @@ static bool ghost_event_proc(GHOST_EventHandle evt, GHOST_TUserDataPtr C_void_pt
break;
}
case GHOST_kEventDraggingDropDone: {
GHOST_TEventDragnDropData *ddd = static_cast<GHOST_TEventDragnDropData *>(
GHOST_GetEventData(evt));
const GHOST_TEventDragnDropData *ddd = static_cast<const GHOST_TEventDragnDropData *>(
data);
/* Ensure the event state matches modifiers (window was inactive). */
wm_window_update_eventstate_modifiers(wm, win);
@ -1522,9 +1522,8 @@ static bool ghost_event_proc(GHOST_EventHandle evt, GHOST_TUserDataPtr C_void_pt
event.val = KM_NOTHING;
copy_v2_v2_int(event.prev_xy, event.xy);
wm_cursor_position_from_ghost_screen_coords(win, &ddd->x, &ddd->y);
event.xy[0] = ddd->x;
event.xy[1] = ddd->y;
copy_v2_v2_int(event.xy, &ddd->x);
wm_cursor_position_from_ghost_screen_coords(win, &event.xy[0], &event.xy[1]);
/* The values from #wm_window_update_eventstate may not match (under WAYLAND they don't)
* Write this into the event state. */
@ -1552,7 +1551,7 @@ static bool ghost_event_proc(GHOST_EventHandle evt, GHOST_TUserDataPtr C_void_pt
/* add drag data to wm for paths: */
if (ddd->dataType == GHOST_kDragnDropTypeFilenames) {
GHOST_TStringArray *stra = static_cast<GHOST_TStringArray *>(ddd->data);
const GHOST_TStringArray *stra = static_cast<const GHOST_TStringArray *>(ddd->data);
for (int a = 0; a < stra->count; a++) {
printf("drop file %s\n", stra->strings[a]);
@ -1589,20 +1588,6 @@ static bool ghost_event_proc(GHOST_EventHandle evt, GHOST_TUserDataPtr C_void_pt
break;
}
case GHOST_kEventTrackpad: {
GHOST_TEventTrackpadData *pd = static_cast<GHOST_TEventTrackpadData *>(data);
wm_cursor_position_from_ghost_screen_coords(win, &pd->x, &pd->y);
wm_event_add_ghostevent(wm, win, type, data);
break;
}
case GHOST_kEventCursorMove: {
GHOST_TEventCursorData *cd = static_cast<GHOST_TEventCursorData *>(data);
wm_cursor_position_from_ghost_screen_coords(win, &cd->x, &cd->y);
wm_event_add_ghostevent(wm, win, type, data);
break;
}
case GHOST_kEventButtonDown:
case GHOST_kEventButtonUp: {
if (win->active == 0) {

View File

@ -134,7 +134,7 @@ void wm_event_do_handlers(bContext *C);
/**
* Windows store own event queues #wmWindow.event_queue (no #bContext here).
*/
void wm_event_add_ghostevent(wmWindowManager *wm, wmWindow *win, int type, void *customdata);
void wm_event_add_ghostevent(wmWindowManager *wm, wmWindow *win, int type, const void *customdata);
#ifdef WITH_XR_OPENXR
void wm_event_add_xrevent(wmWindow *win, wmXrActionData *actiondata, short val);
#endif