Skip to content

Commit 85e2e32

Browse files
authored
Create new core:sys/darwin/CoreGraphics package (#6147)
* Add Darwin mouse cursor-related bindings Create CoreGraphics package in the process * Remove unneeded semicolons * Define some CG types in CoreFoundation instead This matches where Darwin's C header files defines these types
1 parent ddc2559 commit 85e2e32

File tree

5 files changed

+115
-12
lines changed

5 files changed

+115
-12
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package CoreFoundation
2+
3+
CGFloat :: distinct (f32 when size_of(uint) == 4 else f64)
4+
5+
CGPoint :: struct {
6+
x: CGFloat,
7+
y: CGFloat,
8+
}
9+
10+
CGRect :: struct {
11+
using origin: CGPoint,
12+
using size: CGSize,
13+
}
14+
15+
CGSize :: struct {
16+
width: CGFloat,
17+
height: CGFloat,
18+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package CoreGraphics
2+
3+
import "core:c"
4+
import CF "core:sys/darwin/CoreFoundation"
5+
6+
@(require)
7+
foreign import "system:CoreGraphics.framework"
8+
9+
@(link_prefix="CG", default_calling_convention="c")
10+
foreign CoreGraphics {
11+
AssociateMouseAndMouseCursorPosition :: proc(connected: b32) -> Error ---
12+
DisplayIDToOpenGLDisplayMask :: proc(display: DirectDisplayID) -> OpenGLDisplayMask ---
13+
DisplayMoveCursorToPoint :: proc(display: DirectDisplayID, point: Point) -> Error ---
14+
EventSourceKeyState :: proc(stateID: EventSourceStateID, key: KeyCode) -> bool ---
15+
GetActiveDisplayList :: proc(maxDisplays: c.uint32_t, activeDisplays: [^]DirectDisplayID, displayCount: ^c.uint32_t) -> Error ---
16+
GetDisplaysWithOpenGLDisplayMask :: proc(mask: OpenGLDisplayMask, maxDisplays: c.uint32_t, displays: [^]DirectDisplayID, matchingDisplayCount: ^c.uint32_t) -> Error ---
17+
GetDisplaysWithPoint :: proc(point: Point, maxDisplays: c.uint32_t, displays: [^]DirectDisplayID, matchingDisplayCount: ^c.uint32_t) -> Error ---
18+
GetDisplaysWithRect :: proc(rect: Rect, maxDisplays: c.uint32_t, displays: [^]DirectDisplayID, matchingDisplayCount: ^c.uint32_t) -> Error ---
19+
GetOnlineDisplayList :: proc(maxDisplays: c.uint32_t, onlineDisplays: [^]DirectDisplayID, displayCount: ^c.uint32_t) -> Error ---
20+
MainDisplayID :: proc() -> DirectDisplayID ---
21+
OpenGLDisplayMaskToDisplayID :: proc(mask: OpenGLDisplayMask) -> DirectDisplayID ---
22+
WarpMouseCursorPosition :: proc(newCursorPosition: Point) -> Error ---
23+
}
24+
25+
DirectDisplayID :: c.uint32_t
26+
27+
Error :: enum c.int32_t {
28+
Success = 0,
29+
Failure = 1000,
30+
IllegalArgument = 1001,
31+
InvalidConnection = 1002,
32+
InvalidContext = 1003,
33+
CannotComplete = 1004,
34+
NotImplemented = 1006,
35+
RangeCheck = 1007,
36+
TypeCheck = 1008,
37+
InvalidOperation = 1010,
38+
NoneAvailable = 1011,
39+
}
40+
41+
EventSourceStateID :: enum c.int32_t {
42+
Private = -1,
43+
CombinedSessionState = 0,
44+
HIDSystemState = 1,
45+
}
46+
47+
Float :: CF.CGFloat
48+
49+
KeyCode :: c.uint16_t
50+
51+
OpenGLDisplayMask :: c.uint32_t
52+
53+
Point :: CF.CGPoint
54+
55+
Rect :: CF.CGRect
56+
57+
Size :: CF.CGSize

core/sys/darwin/Foundation/NSCursor.odin

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@ package objc_Foundation
33
@(objc_class="NSCursor")
44
Cursor :: struct {using _: Object}
55

6+
@(objc_type=Cursor, objc_name="hide", objc_is_class_method=true)
7+
Cursor_hide :: proc() {
8+
msgSend(nil, Cursor, "hide")
9+
}
10+
@(objc_type=Cursor, objc_name="unhide", objc_is_class_method=true)
11+
Cursor_unhide :: proc() {
12+
msgSend(nil, Cursor, "unhide")
13+
}
14+
@(objc_type=Cursor, objc_name="setHiddenUntilMouseMoves", objc_is_class_method=true)
15+
Cursor_setHiddenUntilMouseMoves :: proc(flag: BOOL) {
16+
msgSend(nil, Cursor, "setHiddenUntilMouseMoves:", flag)
17+
}
18+
619
@(objc_type=Cursor, objc_name="set")
720
Cursor_set :: proc(self: ^Cursor) {
821
msgSend(EventType, self, "set")

core/sys/darwin/Foundation/NSTypes.odin

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package objc_Foundation
22

33
import "base:intrinsics"
4+
import CF "core:sys/darwin/CoreFoundation"
45

56
@(private) msgSend :: intrinsics.objc_send
67

@@ -34,17 +35,11 @@ ComparisonResult :: enum Integer {
3435

3536
NotFound :: IntegerMax
3637

37-
Float :: distinct (f32 when size_of(uint) == 4 else f64)
38+
Float :: CF.CGFloat
3839

39-
Point :: struct {
40-
x: Float,
41-
y: Float,
42-
}
40+
Point :: CF.CGPoint
4341

44-
Size :: struct {
45-
width: Float,
46-
height: Float,
47-
}
42+
Size :: CF.CGSize
4843

4944
when size_of(UInteger) == 8 {
5045
_UINTEGER_ENCODING :: "Q"

core/sys/darwin/Foundation/NSWindow.odin

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
package objc_Foundation
22

33
import "core:strings"
4+
import CF "core:sys/darwin/CoreFoundation"
45
import "base:runtime"
56
import "base:intrinsics"
67

7-
Rect :: struct {
8-
using origin: Point,
9-
using size: Size,
8+
Rect :: CF.CGRect
9+
MaxX :: proc(aRect: Rect) -> Float {
10+
return aRect.origin.x + aRect.size.width
11+
}
12+
MaxY :: proc(aRect: Rect) -> Float {
13+
return aRect.origin.y + aRect.size.height
14+
}
15+
MidX :: proc(aRect: Rect) -> Float {
16+
return aRect.origin.x + aRect.size.width*0.5
17+
}
18+
MidY :: proc(aRect: Rect) -> Float {
19+
return aRect.origin.y + aRect.size.height*0.5
20+
}
21+
MinX :: proc(aRect: Rect) -> Float {
22+
return aRect.origin.x
23+
}
24+
MinY :: proc(aRect: Rect) -> Float {
25+
return aRect.origin.y
1026
}
1127

1228
Depth :: enum UInteger {
@@ -988,3 +1004,7 @@ Window_contentRectForFrameRectInstance :: proc "c" (self: ^Window, frameRect: Re
9881004
Window_frameRectForContentRectInstance :: proc "c" (self: ^Window, contentRect: Rect) -> Rect {
9891005
return msgSend(Rect, self, "frameRectForContentRect:", contentRect)
9901006
}
1007+
@(objc_type = Window, objc_name = "screen")
1008+
Window_screen :: proc "c" (self: ^Window) -> ^Screen {
1009+
return msgSend(^Screen, self, "screen")
1010+
}

0 commit comments

Comments
 (0)