Skip to content

Commit

Permalink
pen: Added Cocoa backend.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Aug 9, 2024
1 parent ade9c39 commit b2cf6d7
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Xcode/SDL/SDL.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,8 @@
F3FA5A242B59ACE000FEAD97 /* yuv_rgb_lsx.h in Headers */ = {isa = PBXBuildFile; fileRef = F3FA5A1B2B59ACE000FEAD97 /* yuv_rgb_lsx.h */; };
F3FA5A252B59ACE000FEAD97 /* yuv_rgb_common.h in Headers */ = {isa = PBXBuildFile; fileRef = F3FA5A1C2B59ACE000FEAD97 /* yuv_rgb_common.h */; };
FA73671D19A540EF004122E4 /* CoreVideo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA73671C19A540EF004122E4 /* CoreVideo.framework */; platformFilters = (ios, maccatalyst, macos, tvos, watchos, ); };
0000E5D7110DFF81FF660000 /* SDL_cocoapen.h in Headers */ = {isa = PBXBuildFile; fileRef = 00002F2F5496FA184A0F0000 /* SDL_cocoapen.h */; };
0000D5B526B85DE7AB1C0000 /* SDL_cocoapen.m in Sources */ = {isa = PBXBuildFile; fileRef = 0000CCA310B73A7B59910000 /* SDL_cocoapen.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -1076,6 +1078,8 @@
F59C710600D5CB5801000001 /* SDL.info */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = SDL.info; sourceTree = "<group>"; };
F5A2EF3900C6A39A01000001 /* BUGS.txt */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; name = BUGS.txt; path = ../../BUGS.txt; sourceTree = SOURCE_ROOT; };
FA73671C19A540EF004122E4 /* CoreVideo.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreVideo.framework; path = System/Library/Frameworks/CoreVideo.framework; sourceTree = SDKROOT; };
00002F2F5496FA184A0F0000 /* SDL_cocoapen.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SDL_cocoapen.h; path = SDL_cocoapen.h; sourceTree = "<group>"; };
0000CCA310B73A7B59910000 /* SDL_cocoapen.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SDL_cocoapen.m; path = SDL_cocoapen.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -1676,6 +1680,8 @@
A7D8A68323E2513E00DCD162 /* SDL_cocoavulkan.m */,
A7D8A69223E2513E00DCD162 /* SDL_cocoawindow.h */,
A7D8A68423E2513E00DCD162 /* SDL_cocoawindow.m */,
00002F2F5496FA184A0F0000 /* SDL_cocoapen.h */,
0000CCA310B73A7B59910000 /* SDL_cocoapen.m */,
);
path = cocoa;
sourceTree = "<group>";
Expand Down Expand Up @@ -2908,6 +2914,7 @@
0000494CC93F3E624D3C0000 /* SDL_systime.c in Sources */,
000095FA1BDE436CF3AF0000 /* SDL_time.c in Sources */,
0000140640E77F73F1DF0000 /* SDL_dialog_utils.c in Sources */,
0000D5B526B85DE7AB1C0000 /* SDL_cocoapen.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
32 changes: 32 additions & 0 deletions src/video/cocoa/SDL_cocoapen.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"

#ifndef SDL_cocoapen_h_
#define SDL_cocoapenm_h_

#include "SDL_cocoavideo.h"

extern int Cocoa_InitPen(SDL_VideoDevice *_this);
extern SDL_bool Cocoa_HandlePenEvent(SDL_CocoaWindowData *_data, NSEvent *event); // return SDL_FALSE if we didn't handle this event.
extern void Cocoa_QuitPen(SDL_VideoDevice *_this);

#endif /* SDL_cocoapen_h_ */
177 changes: 177 additions & 0 deletions src/video/cocoa/SDL_cocoapen.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"

#ifdef SDL_VIDEO_DRIVER_COCOA

#include "SDL_cocoapen.h"
#include "SDL_cocoavideo.h"

#include "../../events/SDL_pen_c.h"

int Cocoa_InitPen(SDL_VideoDevice *_this)
{
return 0;
}

typedef struct Cocoa_PenHandle
{
NSUInteger deviceid;
NSUInteger toolid;
SDL_PenID pen;
SDL_bool is_eraser;
} Cocoa_PenHandle;

typedef struct FindPenByDeviceAndToolIDData
{
NSUInteger deviceid;
NSUInteger toolid;
void *handle;
} FindPenByDeviceAndToolIDData;

static SDL_bool FindPenByDeviceAndToolID(void *handle, void *userdata)
{
const Cocoa_PenHandle *cocoa_handle = (const Cocoa_PenHandle *) handle;
FindPenByDeviceAndToolIDData *data = (FindPenByDeviceAndToolIDData *) userdata;

if (cocoa_handle->deviceid != data->deviceid) {
return SDL_FALSE;
} else if (cocoa_handle->toolid != data->toolid) {
return SDL_FALSE;
}
data->handle = handle;
return SDL_TRUE;
}

static Cocoa_PenHandle *Cocoa_FindPenByDeviceID(NSUInteger deviceid, NSUInteger toolid)
{
FindPenByDeviceAndToolIDData data;
data.deviceid = deviceid;
data.toolid = toolid;
data.handle = NULL;
SDL_FindPenByCallback(FindPenByDeviceAndToolID, &data);
return (Cocoa_PenHandle *) data.handle;
}

static void Cocoa_HandlePenProximityEvent(SDL_CocoaWindowData *_data, NSEvent *event)
{
const NSUInteger devid = [event deviceID];
const NSUInteger toolid = [event pointingDeviceID];

if (event.enteringProximity) { // new pen coming!
const NSPointingDeviceType devtype = [event pointingDeviceType];
const SDL_bool is_eraser = (devtype == NSPointingDeviceTypeEraser);
const SDL_bool is_pen = (devtype == NSPointingDeviceTypePen);
if (!is_eraser && !is_pen) {
return; // we ignore other things, which hopefully is right.
}

Cocoa_PenHandle *handle = (Cocoa_PenHandle *) SDL_calloc(1, sizeof (*handle));
if (!handle) {
return; // oh well.
}

// Cocoa offers almost none of this information.
SDL_PenInfo peninfo;
SDL_zero(peninfo);
peninfo.capabilities = SDL_PEN_CAPABILITY_PRESSURE | SDL_PEN_CAPABILITY_ROTATION | SDL_PEN_CAPABILITY_XTILT | SDL_PEN_CAPABILITY_YTILT | (is_eraser ? SDL_PEN_CAPABILITY_ERASER : 0);
peninfo.max_tilt = 90.0f;
peninfo.num_buttons = 2;
peninfo.subtype = is_eraser ? SDL_PEN_TYPE_ERASER : SDL_PEN_TYPE_PEN;

handle->deviceid = devid;
handle->toolid = toolid;
handle->is_eraser = is_eraser;
handle->pen = SDL_AddPenDevice(Cocoa_GetEventTimestamp([event timestamp]), NULL, &peninfo, handle);
if (!handle->pen) {
SDL_free(handle); // oh well.
}
} else { // old pen leaving!
Cocoa_PenHandle *handle = Cocoa_FindPenByDeviceID(devid, toolid);
if (handle) {
SDL_RemovePenDevice(Cocoa_GetEventTimestamp([event timestamp]), handle->pen);
SDL_free(handle);
}
}
}

static void Cocoa_HandlePenPointEvent(SDL_CocoaWindowData *_data, NSEvent *event)
{
const Uint32 timestamp = Cocoa_GetEventTimestamp([event timestamp]);
Cocoa_PenHandle *handle = Cocoa_FindPenByDeviceID([event deviceID], [event pointingDeviceID]);
if (!handle) {
return;
}

const SDL_PenID pen = handle->pen;
const NSEventButtonMask buttons = [event buttonMask];
const NSPoint tilt = [event tilt];
const NSPoint point = [event locationInWindow];
const SDL_bool is_touching = (buttons & NSEventButtonMaskPenTip) != 0;
SDL_Window *window = _data.window;

SDL_SendPenTouch(timestamp, pen, window, is_touching, handle->is_eraser ? 1 : 0);
SDL_SendPenMotion(timestamp, pen, window, (float) point.x, (float) (window->h - point.y));
SDL_SendPenButton(timestamp, pen, window, (buttons & NSEventButtonMaskPenLowerSide) ? SDL_PRESSED : SDL_RELEASED, 1);
SDL_SendPenButton(timestamp, pen, window, (buttons & NSEventButtonMaskPenUpperSide) ? SDL_PRESSED : SDL_RELEASED, 2);
SDL_SendPenAxis(timestamp, pen, window, SDL_PEN_AXIS_PRESSURE, [event pressure]);
SDL_SendPenAxis(timestamp, pen, window, SDL_PEN_AXIS_ROTATION, [event rotation]);
SDL_SendPenAxis(timestamp, pen, window, SDL_PEN_AXIS_XTILT, ((float) tilt.x) * 90.0f);
SDL_SendPenAxis(timestamp, pen, window, SDL_PEN_AXIS_YTILT, ((float) tilt.y) * 90.0f);
}

SDL_bool Cocoa_HandlePenEvent(SDL_CocoaWindowData *_data, NSEvent *event)
{
NSEventType type = [event type];

if ((type != NSEventTypeTabletPoint) && (type != NSEventTypeTabletProximity)) {
const NSEventSubtype subtype = [event subtype];
if (subtype == NSEventSubtypeTabletPoint) {
type = NSEventTypeTabletPoint;
} else if (subtype == NSEventSubtypeTabletProximity) {
type = NSEventTypeTabletProximity;
} else {
return SDL_FALSE; // not a tablet event.
}
}

if (type == NSEventTypeTabletPoint) {
Cocoa_HandlePenPointEvent(_data, event);
} else if (type == NSEventTypeTabletProximity) {
Cocoa_HandlePenProximityEvent(_data, event);
} else {
return SDL_FALSE; // not a tablet event.
}

return SDL_TRUE;
}

static void Cocoa_FreePenHandle(SDL_PenID instance_id, void *handle, void *userdata)
{
SDL_free(handle);
}

void Cocoa_QuitPen(SDL_VideoDevice *_this)
{
SDL_RemoveAllPenDevices(Cocoa_FreePenHandle, NULL);
}

#endif /* SDL_VIDEO_DRIVER_COCOA */
1 change: 1 addition & 0 deletions src/video/cocoa/SDL_cocoavideo.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "SDL_cocoamouse.h"
#include "SDL_cocoaopengl.h"
#include "SDL_cocoawindow.h"
#include "SDL_cocoapen.h"

#ifndef MAC_OS_X_VERSION_10_12
#define DECLARE_EVENT(name) static const NSEventType NSEventType##name = NS##name
Expand Down
3 changes: 3 additions & 0 deletions src/video/cocoa/SDL_cocoavideo.m
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ int Cocoa_VideoInit(SDL_VideoDevice *_this)
Cocoa_InitKeyboard(_this);
if (Cocoa_InitMouse(_this) < 0) {
return -1;
} else if (Cocoa_InitPen(_this) < 0) {
return -1;
}

// Assume we have a mouse and keyboard
Expand All @@ -227,6 +229,7 @@ void Cocoa_VideoQuit(SDL_VideoDevice *_this)
Cocoa_QuitModes(_this);
Cocoa_QuitKeyboard(_this);
Cocoa_QuitMouse(_this);
Cocoa_QuitPen(_this);
SDL_DestroyMutex(data.swaplock);
data.swaplock = NULL;
}
Expand Down
4 changes: 4 additions & 0 deletions src/video/cocoa/SDL_cocoawindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ typedef enum
/* Touch event handling */
- (void)handleTouches:(NSTouchPhase)phase withEvent:(NSEvent *)theEvent;

/* Tablet event handling (but these also come through on mouse events sometimes!) */
- (void)tabletProximity:(NSEvent *)theEvent;
- (void)tabletPoint:(NSEvent *)theEvent;

@end
/* *INDENT-ON* */

Expand Down
22 changes: 22 additions & 0 deletions src/video/cocoa/SDL_cocoawindow.m
Original file line number Diff line number Diff line change
Expand Up @@ -1568,6 +1568,10 @@ static int Cocoa_SendMouseButtonClicks(SDL_Mouse *mouse, NSEvent *theEvent, SDL_

- (void)mouseDown:(NSEvent *)theEvent
{
if (Cocoa_HandlePenEvent(_data, theEvent)) {
return; // pen code handled it.
}

SDL_Mouse *mouse = SDL_GetMouse();
int button;

Expand Down Expand Up @@ -1625,6 +1629,10 @@ - (void)otherMouseDown:(NSEvent *)theEvent

- (void)mouseUp:(NSEvent *)theEvent
{
if (Cocoa_HandlePenEvent(_data, theEvent)) {
return; // pen code handled it.
}

SDL_Mouse *mouse = SDL_GetMouse();
int button;

Expand Down Expand Up @@ -1672,6 +1680,10 @@ - (void)otherMouseUp:(NSEvent *)theEvent

- (void)mouseMoved:(NSEvent *)theEvent
{
if (Cocoa_HandlePenEvent(_data, theEvent)) {
return; // pen code handled it.
}

SDL_MouseID mouseID = SDL_DEFAULT_MOUSE_ID;
SDL_Mouse *mouse = SDL_GetMouse();
NSPoint point;
Expand Down Expand Up @@ -1887,6 +1899,16 @@ - (void)handleTouches:(NSTouchPhase)phase withEvent:(NSEvent *)theEvent
}
}

- (void)tabletProximity:(NSEvent *)theEvent
{
Cocoa_HandlePenEvent(_data, theEvent);
}

- (void)tabletPoint:(NSEvent *)theEvent
{
Cocoa_HandlePenEvent(_data, theEvent);
}

@end

@interface SDL3View : NSView
Expand Down
4 changes: 2 additions & 2 deletions test/testpen.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,15 @@ int SDL_AppEvent(void *appstate, const SDL_Event *event)
/*SDL_Log("Pen %" SDL_PRIu32 " button %d down!", event->pbutton.which, (int) event->pbutton.button);*/
pen = FindPen(event->ptouch.which);
if (pen) {
pen->buttons |= (1 << event->pbutton.button);
pen->buttons |= (1 << (event->pbutton.button-1));
}
return SDL_APP_CONTINUE;

case SDL_EVENT_PEN_BUTTON_UP:
/*SDL_Log("Pen %" SDL_PRIu32 " button %d up!", event->pbutton.which, (int) event->pbutton.button);*/
pen = FindPen(event->ptouch.which);
if (pen) {
pen->buttons &= ~(1 << event->pbutton.button);
pen->buttons &= ~(1 << (event->pbutton.button-1));
}
return SDL_APP_CONTINUE;

Expand Down

0 comments on commit b2cf6d7

Please sign in to comment.