Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] C bindings #84

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"repositoryURL": "https://github.com/tmandry/AXSwift.git",
"state": {
"branch": null,
"revision": "dbf34341fd9a5892a5f8a646699c82308ae40c42",
"version": "0.3.1"
"revision": "81dcc36aced905d6464cc25e35f8d13184bbf21c",
"version": "0.3.2"
}
},
{
Expand All @@ -24,8 +24,8 @@
"repositoryURL": "https://github.com/mxcl/PromiseKit.git",
"state": {
"branch": null,
"revision": "aea48ea1855f5d82e2dffa6027afce3aab8f3dd7",
"version": "6.13.3"
"revision": "00afea5360bf1c02ac83209c2c8991f73b5a4e7d",
"version": "6.16.1"
}
},
{
Expand Down
6 changes: 6 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ let package = Package(
.target(name: "SwindlerExample",
dependencies: ["Swindler"],
path: "SwindlerExample"),
.target(name: "SwindlerCExample",
dependencies: ["Swindler"],
path: "SwindlerCExample",
cSettings: [
.headerSearchPath("../Sources")
]),
.testTarget(
name: "SwindlerTests",
dependencies: ["Swindler", "PromiseKit", "Quick", "Nimble"],
Expand Down
12 changes: 6 additions & 6 deletions Sources/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ final class OSXApplicationDelegate<

self.windows.append(windowDelegate)
self.newWindowHandler.windowCreated(axElement)

return windowDelegate
}.recover { error -> Promise<WinDelegate?> in
// If this initialization of WinDelegate failed, the window is somehow invalid and we
Expand Down Expand Up @@ -369,11 +369,11 @@ extension OSXApplicationDelegate {
} else {
// We don't know about the element that has been passed. Wait until the window is
// initialized.
createWindowForElementIfNotExists(element)
.done { _ in property.refresh() }
.recover { err in
log.error("Error while updating window property: \(err)")
}
addWindowElement(element).done { _ in
property.refresh()
}.recover { err in
log.error("Error while updating window property: \(err)")
}

// In some cases, the element is actually IS the application element, but equality
// checks inexplicably return false. (This has been observed for Finder.) In this case
Expand Down
109 changes: 109 additions & 0 deletions Sources/Swindler-C.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//
// Swindler-C.h
// Swindler
//
// Created by Jeremy on 9/15/21.
//

#ifndef Swindler_C_h
#define Swindler_C_h

#include <sys/types.h>

/// Forward declarations
typedef struct CGRect CGRect;
typedef struct CGPoint CGPoint;
typedef struct CGSize CGSize;

/// Opaque structs for naming clarity
typedef struct SWState * SWStateRef;
typedef struct SWScreen * SWScreenRef;
typedef struct SWApplication * SWApplicationRef;
typedef struct SWWindow * SWWindowRef;
typedef struct SWSpace * SWSpaceRef;

/// Callback function type for SWStateInitializeAsync
typedef void (*SWStateCreatedCallback)(SWStateRef _Nullable );

/// Empty block used for chaining operations
typedef void (^_Nullable SWCompletionBlock)(void);

#pragma mark ---- State ----
/// Create an SWStateRef synchronously. Safe to call from main thread
SWStateRef _Nullable SWStateInitialize(void);

/// Promise-based creation
void SWStateInitializeAsync(SWStateCreatedCallback _Nonnull);

/// Not needed if you created stateRef in an autorelease pool
void SwindlerDestroy(SWStateRef _Nonnull stateRef);

uint32_t SWStateGetScreens(SWStateRef _Nonnull stateRef, SWScreenRef _Nullable * _Nullable screens);
SWScreenRef _Nullable SWStateGetMainScreen(SWStateRef _Nonnull stateRef);
uint32_t SWStateGetRunningApplications(SWStateRef _Nonnull stateRef, SWApplicationRef _Nullable * _Nullable apps);
uint32_t SWStateGetKnownWindows(SWStateRef _Nonnull stateRef, SWWindowRef _Nullable * _Nullable windows);
SWApplicationRef _Nullable SWStateGetFrontmostApplication(SWStateRef _Nonnull stateRef);
void SWStateSetFrontmostApplication(SWStateRef _Nonnull stateRef, SWApplicationRef _Nonnull appRef, SWCompletionBlock);


#pragma mark ---- Screens ----
CGRect SWScreenGetFrame(SWScreenRef _Nonnull screenRef);
const char * _Nullable SWScreenGetDebugDescription(SWScreenRef _Nonnull screenRef);
int SWScreenGetSpaceID(SWScreenRef _Nonnull screenRef);


#pragma mark ---- Applications ----
pid_t SWApplicationGetPid(SWApplicationRef _Nonnull appRef);
const char * _Nullable SWApplicationGetBundleIdentifier(SWApplicationRef _Nonnull appRef);
uint32_t SWStateGetKnownWindows(SWStateRef _Nonnull appRef, SWWindowRef _Nullable * _Nullable windows);
SWWindowRef _Nullable SWApplicationGetFocusedWindow(SWApplicationRef _Nonnull appRef);
SWWindowRef _Nullable SWApplicationGetMainWindow(SWApplicationRef _Nonnull appRef);
SWWindowRef _Nullable SWApplicationSetMainWindow(SWApplicationRef _Nonnull appRef, SWWindowRef _Nonnull windowRef, SWCompletionBlock);
_Bool SWApplicationGetIsHidden(SWApplicationRef _Nonnull appRef);
void SWApplicationSetIsHidden(SWApplicationRef _Nonnull, _Bool isHidden, SWCompletionBlock);


#pragma mark ---- Windows ----
SWApplicationRef _Nullable SWWindowGetApplication(SWWindowRef _Nonnull winRef);

const char * _Nullable SWWindowGetTitle(SWWindowRef _Nonnull winRef);
SWScreenRef _Nullable SWWindowGetScreen(SWWindowRef _Nonnull winRef);

CGPoint SWWindowGetPosition(SWWindowRef _Nonnull winRef);
void SWWindowSetPosition(SWWindowRef _Nonnull winRef, CGPoint position, SWCompletionBlock);

CGRect SWWindowGetFrame(SWWindowRef _Nonnull winRef);
void SWWindowSetFrame(SWWindowRef _Nonnull winRef, CGRect frame, SWCompletionBlock);

CGSize SWWindowGetSize(SWWindowRef _Nonnull winRef);
void SWWindowSetSize(SWWindowRef _Nonnull winRef, CGSize size, SWCompletionBlock);

_Bool SWWindowGetIsMinimized(SWWindowRef _Nonnull winRef);
void SWWindowSetIsMinimized(SWWindowRef _Nonnull winRef, _Bool isMinimized, SWCompletionBlock);

_Bool SWWindowGetIsFullscreen(SWWindowRef _Nonnull winRef);
void SWWindowSetIsFullscreen(SWWindowRef _Nonnull winRef, _Bool isFullscreen, SWCompletionBlock);


#pragma mark ---- Events ----
/// Space events
void SWStateOnSpaceWillChange(SWStateRef _Nonnull stateRef, void (^ _Nonnull handler)(_Bool external, uint32_t * _Nullable spaceIds, int count));
void SWStateOnSpaceDidChange(SWStateRef _Nonnull stateRef, void (^ _Nonnull handler)(_Bool external, uint32_t * _Nullable spaceIds, int count));

/// Application events
void SWStateOnFrontmostApplicationDidChange(SWStateRef _Nonnull stateRef, void (^ _Nonnull handler)(_Bool external, SWApplicationRef _Nullable from, SWApplicationRef _Nullable to));
void SWStateOnApplicationDidLaunch(SWStateRef _Nonnull stateRef, void (^ _Nonnull handler)(_Bool external, SWApplicationRef _Nullable));
void SWStateOnApplicationDidTerminate(void * _Nonnull stateRef, void (^ _Nonnull handler)(_Bool external, SWApplicationRef _Nullable));
void SWStateOnApplicationIsHiddenDidChange(SWStateRef _Nonnull stateRef, void (^ _Nonnull handler)(_Bool external, SWApplicationRef _Nullable, _Bool from, _Bool to));
void SWStateOnApplicationMainWindowDidChange(SWStateRef _Nonnull stateRef, void (^ _Nonnull handler)(_Bool external, SWApplicationRef _Nullable, SWWindowRef _Nullable from, SWWindowRef _Nullable to));
void SWStateOnApplicationFocusWindowDidChange(SWStateRef _Nonnull stateRef, void (^ _Nonnull handler)(_Bool external, SWApplicationRef _Nullable, SWWindowRef _Nullable from, SWWindowRef _Nullable to));

/// Window events
void SWStateOnWindowCreate(SWStateRef _Nonnull stateRef, void (^ _Nonnull handler)(_Bool external, SWWindowRef _Nullable));
void SWStateOnWindowDestroy(SWStateRef _Nonnull stateRef, void (^ _Nonnull handler)(_Bool external, SWWindowRef _Nullable));
void SWStateOnWindowDidResize(SWStateRef _Nonnull stateRef, void (^ _Nonnull handler)(_Bool external, SWWindowRef _Nullable, CGRect from, CGRect to));
void SWStateOnWindowDidChangeTitle(SWStateRef _Nonnull stateRef, void (^ _Nonnull handler)(_Bool external, SWWindowRef _Nullable, const char* _Nullable from, const char* _Nullable to));
void SWStateOnWindowMinimizeDidChange(SWStateRef _Nonnull stateRef, void (^ _Nonnull handler)(_Bool external, SWWindowRef _Nullable, _Bool from, _Bool to));


#endif /* Swindler_C_h */
Loading