From 4666b07f3b84e61faa0a384eb272eeac0413480b Mon Sep 17 00:00:00 2001 From: Sawyer Bergeron Date: Fri, 3 Apr 2020 21:49:26 -0400 Subject: [PATCH 1/4] Accomodate new pan event changes --- src/sdl2.re | 49 +++++++++++++++++++++++++++----------------- src/sdl2_wrapper.cpp | 38 +++++++++++++++++++++++++--------- 2 files changed, 58 insertions(+), 29 deletions(-) diff --git a/src/sdl2.re b/src/sdl2.re index b77d376..802c2d0 100644 --- a/src/sdl2.re +++ b/src/sdl2.re @@ -313,6 +313,12 @@ module Keycode = { let left = 1073741904; }; +module Axis = { + type t = + | Horizontal + | Vertical; +}; + module WheelType = { type t = | Last @@ -391,6 +397,21 @@ module Event = { isFlipped: bool, }; + module PanElements = { + type t = + | Interrupt + | Fling + | Pan(float); + } + + type panEvent = { + windowID: int, + timestamp: int, + source: WheelType.t, + axis: Axis.t, + action: PanElements.t, + } + type mousePan = { windowID: int, deltaX: int, @@ -471,7 +492,7 @@ module Event = { | WindowClosed(windowEvent) | WindowTakeFocus(windowEvent) | WindowHitTest(windowEvent) - | MousePan(mousePan) + | Pan(panEvent) // An event that hasn't been implemented yet | Unknown; @@ -488,26 +509,16 @@ module Event = { deltaY, isFlipped ? 1 : 0, ) - | MousePan({ + | Pan({ windowID, - deltaX, - deltaY, - containsX, - containsY, - isFling, - isInterrupt, + timestamp, + action, _, - }) => - Printf.sprintf( - "Pan event: %d %d %d %d %d %d %d", - windowID, - deltaX, - deltaY, - if (containsX) {1} else {0}, - if (containsY) {1} else {0}, - if (isFling) {1} else {0}, - if (isInterrupt) {1} else {0}, - ) + }) => switch (action) { + | Interrupt => Printf.sprintf("Interrupt event at timestamp %d", timestamp) + | Fling => Printf.sprintf("Fling event at timestamp %d", timestamp) + | Pan(delta) => Printf.sprintf("Pan event at timestamp %d with delta %f", timestamp, delta); + } | MouseButtonUp({windowID, button, _}) => Printf.sprintf( "MouseButtonUp windowId: %d button: %s", diff --git a/src/sdl2_wrapper.cpp b/src/sdl2_wrapper.cpp index a60f650..ddbe62a 100644 --- a/src/sdl2_wrapper.cpp +++ b/src/sdl2_wrapper.cpp @@ -717,17 +717,35 @@ CAMLprim value Val_SDL_Event(SDL_Event *event) { case SDL_PANEVENT: v = caml_alloc(1, 24); - vInner = caml_alloc(9, 0); + vInner = caml_alloc(5 * 8, 0); Store_field(vInner, 0, Val_int(event->window.windowID)); - Store_field(vInner, 1, Val_int(event->pan.x)); - Store_field(vInner, 2, Val_int(event->pan.y)); - Store_field(vInner, 3, Val_bool(event->pan.contains_x)); - Store_field(vInner, 4, Val_bool(event->pan.contains_y)); - Store_field(vInner, 5, Val_bool(event->pan.fling)); - Store_field(vInner, 6, Val_bool(event->pan.interrupt)); - // verify this is the correct way of representing a ref to some WheelType.t - Store_field(vInner, 7, Val_int(event->pan.source_type)); - Store_field(vInner, 8, Val_int(event->pan.timestamp)); + Store_field(vInner, 1, Val_int(event->pan.timestamp)); + + Store_field(vInner, 2, Val_int(event->pan.source)); + int axis; + if( event->pan.axis == SDL_PAN_AXIS_VERTICAL ) { + axis = 0; + } else { + axis = 1; + } + + Store_field(vInner, 3, Val_int(axis)); + + if( event->pan.pantype == SDL_PANEVENTTYPE_PAN ) { + CAMLlocal1(panElement); + + // tagged 0 for only non-constant constructor Pan(float) + panElement = caml_alloc(1, 0); + + Store_double_field(panElement, 0, event->pan.contents.pan.delta); + Store_field(vInner, 4, panElement); + } else { + if( event->pan.pantype == SDL_PANEVENTTYPE_INTERRUPT ) { + Store_field(vInner, 4, Val_int(0)); + } else if( event->pan.pantype == SDL_PANEVENTTYPE_FLING ) { + Store_field(vInner, 4, Val_int(1)); + } + } Store_field(v, 0, vInner); break; From 66bcd7ce01501b148d9c8dbbcbbeaf49064e7b18 Mon Sep 17 00:00:00 2001 From: Sawyer Bergeron Date: Sat, 4 Apr 2020 19:44:30 -0400 Subject: [PATCH 2/4] Fix variant structure in FFI --- src/sdl2_wrapper.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/sdl2_wrapper.cpp b/src/sdl2_wrapper.cpp index ddbe62a..a9a8613 100644 --- a/src/sdl2_wrapper.cpp +++ b/src/sdl2_wrapper.cpp @@ -732,12 +732,14 @@ CAMLprim value Val_SDL_Event(SDL_Event *event) { Store_field(vInner, 3, Val_int(axis)); if( event->pan.pantype == SDL_PANEVENTTYPE_PAN ) { - CAMLlocal1(panElement); - // tagged 0 for only non-constant constructor Pan(float) + CAMLlocal2(panElement, floatElement); panElement = caml_alloc(1, 0); - Store_double_field(panElement, 0, event->pan.contents.pan.delta); + floatElement = caml_copy_double(event->pan.contents.pan.delta); + + Store_field(panElement, 0, floatElement); + Store_field(vInner, 4, panElement); } else { if( event->pan.pantype == SDL_PANEVENTTYPE_INTERRUPT ) { From d8d6e31435e945607333598e14fe47b592b51ec0 Mon Sep 17 00:00:00 2001 From: Sawyer Bergeron Date: Tue, 7 Apr 2020 11:23:34 -0400 Subject: [PATCH 3/4] Incorporate changes from review --- src/sdl2.re | 16 ++-------------- src/sdl2_wrapper.cpp | 38 ++++++++++++++++++++------------------ 2 files changed, 22 insertions(+), 32 deletions(-) diff --git a/src/sdl2.re b/src/sdl2.re index 802c2d0..ed21d7c 100644 --- a/src/sdl2.re +++ b/src/sdl2.re @@ -315,8 +315,8 @@ module Keycode = { module Axis = { type t = - | Horizontal - | Vertical; + | Vertical + | Horizontal; }; module WheelType = { @@ -412,18 +412,6 @@ module Event = { action: PanElements.t, } - type mousePan = { - windowID: int, - deltaX: int, - deltaY: int, - containsX: bool, - containsY: bool, - isFling: bool, - isInterrupt: bool, - source: WheelType.t, - timestamp: int, - }; - type mouseButtonEvent = { windowID: int, button: MouseButton.t, diff --git a/src/sdl2_wrapper.cpp b/src/sdl2_wrapper.cpp index a9a8613..a578757 100644 --- a/src/sdl2_wrapper.cpp +++ b/src/sdl2_wrapper.cpp @@ -717,36 +717,38 @@ CAMLprim value Val_SDL_Event(SDL_Event *event) { case SDL_PANEVENT: v = caml_alloc(1, 24); - vInner = caml_alloc(5 * 8, 0); + vInner = caml_alloc(40, 0); Store_field(vInner, 0, Val_int(event->window.windowID)); Store_field(vInner, 1, Val_int(event->pan.timestamp)); Store_field(vInner, 2, Val_int(event->pan.source)); int axis; - if( event->pan.axis == SDL_PAN_AXIS_VERTICAL ) { - axis = 0; - } else { - axis = 1; + switch (event->pan.axis) { + case SDL_PAN_AXIS_VERTICAL: axis = 0; break; + case SDL_PAN_AXIS_HORIZONTAL: axis = 1; break; } Store_field(vInner, 3, Val_int(axis)); - if( event->pan.pantype == SDL_PANEVENTTYPE_PAN ) { - // tagged 0 for only non-constant constructor Pan(float) - CAMLlocal2(panElement, floatElement); - panElement = caml_alloc(1, 0); + switch (event->pan.pantype) { + case SDL_PANEVENTTYPE_INTERRUPT: + // + Store_field(vInner, 4, Val_int(0)); + break; + case SDL_PANEVENTTYPE_FLING: + // + Store_field(vInner, 4, Val_int(1)); + break; + case SDL_PANEVENTTYPE_PAN: + CAMLlocal2(panElement, floatElement); + panElement = caml_alloc(1, 0); - floatElement = caml_copy_double(event->pan.contents.pan.delta); + floatElement = caml_copy_double(event->pan.contents.pan.delta); - Store_field(panElement, 0, floatElement); + Store_field(panElement, 0, floatElement); - Store_field(vInner, 4, panElement); - } else { - if( event->pan.pantype == SDL_PANEVENTTYPE_INTERRUPT ) { - Store_field(vInner, 4, Val_int(0)); - } else if( event->pan.pantype == SDL_PANEVENTTYPE_FLING ) { - Store_field(vInner, 4, Val_int(1)); - } + Store_field(vInner, 4, panElement); + break; } Store_field(v, 0, vInner); From b9326911e681c33b341799e6075c3d2a25e6e5d4 Mon Sep 17 00:00:00 2001 From: Sawyer Bergeron Date: Tue, 7 Apr 2020 17:14:56 -0400 Subject: [PATCH 4/4] Fix treating caml alloc as malloc --- src/sdl2_wrapper.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sdl2_wrapper.cpp b/src/sdl2_wrapper.cpp index a578757..7f49c91 100644 --- a/src/sdl2_wrapper.cpp +++ b/src/sdl2_wrapper.cpp @@ -717,7 +717,7 @@ CAMLprim value Val_SDL_Event(SDL_Event *event) { case SDL_PANEVENT: v = caml_alloc(1, 24); - vInner = caml_alloc(40, 0); + vInner = caml_alloc(5, 0); Store_field(vInner, 0, Val_int(event->window.windowID)); Store_field(vInner, 1, Val_int(event->pan.timestamp));