From 0905103b7e23fc791900aa63587ee6dace11dc53 Mon Sep 17 00:00:00 2001
From: Ahmet Goksu <ahmet@goksu.in>
Date: Mon, 5 Aug 2024 01:54:13 +0300
Subject: [PATCH] make rules works, a draft swift code developed

---
 graph/cocoa/grcocoa.h     |  17 ++++++
 graph/cocoa/grcocoa.swift | 105 ++++++++++++++++++++++++++++++++++++++
 graph/cocoa/rules.mk      |  30 +++++++++++
 graph/grinit.c            |   4 ++
 4 files changed, 156 insertions(+)
 create mode 100644 graph/cocoa/grcocoa.h
 create mode 100644 graph/cocoa/grcocoa.swift
 create mode 100644 graph/cocoa/rules.mk

diff --git a/graph/cocoa/grcocoa.h b/graph/cocoa/grcocoa.h
new file mode 100644
index 00000000..cf7328e9
--- /dev/null
+++ b/graph/cocoa/grcocoa.h
@@ -0,0 +1,17 @@
+#ifndef GRCOCOA_H_
+#define GRCOCOA_H_
+
+#include "../grobjs.h"
+#include "../grdevice.h"
+#include <Cocoa/Cocoa.h>
+
+int gr_cocoa_device_init(void);
+void gr_cocoa_device_done(void);
+int gr_cocoa_surface_init(grSurface *surface, grBitmap *bitmap);
+void gr_cocoa_surface_done(grSurface *surface);
+void gr_cocoa_surface_refresh_rectangle(grSurface *surface, int x, int y, int w, int h);
+int gr_cocoa_surface_listen_event(grSurface *surface, int event_mask, grEvent *event);
+
+extern grDevice gr_cocoa_device;
+
+#endif /* GRCOCOA_H_ */
diff --git a/graph/cocoa/grcocoa.swift b/graph/cocoa/grcocoa.swift
new file mode 100644
index 00000000..57be3606
--- /dev/null
+++ b/graph/cocoa/grcocoa.swift
@@ -0,0 +1,105 @@
+import Cocoa
+
+struct GrCocoaSurface {
+    var root: grSurface
+    var window: NSWindow?
+    var bitmapRep: NSBitmapImageRep?
+    var imageView: NSImageView?
+
+    init(root: grSurface) {
+        self.root = root
+    }
+
+    mutating func initializeSurface(bitmap: inout grBitmap) -> Bool {
+        let width = Int(bitmap.width)
+        let height = Int(bitmap.rows)
+
+        self.bitmapRep = NSBitmapImageRep(
+            bitmapDataPlanes: nil,
+            pixelsWide: width,
+            pixelsHigh: height,
+            bitsPerSample: 8,
+            samplesPerPixel: 4,
+            hasAlpha: true,
+            isPlanar: false,
+            colorSpaceName: .deviceRGB,
+            bitmapFormat: [],
+            bytesPerRow: width * 4,
+            bitsPerPixel: 32
+        )
+
+        self.window = NSWindow(
+            contentRect: NSMakeRect(0, 0, CGFloat(width), CGFloat(height)),
+            styleMask: [.titled, .closable, .resizable],
+            backing: .buffered,
+            defer: false
+        )
+        self.window?.title = "FreeType Cocoa Demo"
+
+        self.imageView = NSImageView(frame: NSMakeRect(0, 0, CGFloat(width), CGFloat(height)))
+        self.imageView?.image = NSImage(size: NSSize(width: width, height: height))
+        self.window?.contentView?.addSubview(self.imageView!)
+        self.window?.makeKeyAndOrderFront(nil)
+
+        self.root.bitmap = bitmap
+        return true
+    }
+
+    mutating func releaseSurface() {
+        self.window?.close()
+        self.bitmapRep = nil
+    }
+
+    func refreshRectangle(x: Int, y: Int, width: Int, height: Int) {
+        // update bitmap data here
+
+        self.imageView?.image?.addRepresentation(self.bitmapRep!)
+        let rect = NSRect(x: CGFloat(x), y: CGFloat(y), width: CGFloat(width), height: CGFloat(height))
+        self.imageView?.setNeedsDisplay(rect)
+    }
+
+    func listenEvent(eventMask: Int32, event: UnsafeMutablePointer<grEvent>) -> Int32 {
+        // event handling here
+        return 0
+    }
+}
+
+// Expose functions to C
+@_cdecl("gr_cocoa_device_init")
+func gr_cocoa_device_init() -> Int32 {
+    // Initialization code for Cocoa
+    return 0
+}
+
+@_cdecl("gr_cocoa_device_done")
+func gr_cocoa_device_done() {
+    // Cleanup code for Cocoa
+}
+
+@_cdecl("gr_cocoa_surface_init")
+func gr_cocoa_surface_init(surface: UnsafeMutablePointer<grSurface>?, bitmap: UnsafeMutablePointer<grBitmap>?) -> Int32 {
+    guard let surface = surface, let bitmap = bitmap else { return 0 }
+    var cocoaSurface = GrCocoaSurface(root: surface.pointee)
+    return cocoaSurface.initializeSurface(bitmap: &bitmap.pointee) ? 1 : 0
+}
+
+@_cdecl("gr_cocoa_surface_done")
+func gr_cocoa_surface_done(surface: UnsafeMutablePointer<grSurface>?) {
+    guard let surface = surface else { return }
+    var cocoaSurface = GrCocoaSurface(root: surface.pointee)
+    cocoaSurface.releaseSurface()
+}
+
+@_cdecl("gr_cocoa_surface_refresh_rectangle")
+func gr_cocoa_surface_refresh_rectangle(surface: UnsafeMutablePointer<grSurface>?, x: Int32, y: Int32, width: Int32, height: Int32) {
+    guard let surface = surface else { return }
+    let cocoaSurface = GrCocoaSurface(root: surface.pointee)
+    cocoaSurface.refreshRectangle(x: Int(x), y: Int(y), width: Int(width), height: Int(height))
+}
+
+@_cdecl("gr_cocoa_surface_listen_event")
+func gr_cocoa_surface_listen_event(surface: UnsafeMutablePointer<grSurface>?, eventMask: Int32, event: UnsafeMutablePointer<grEvent>?) -> Int32 {
+    guard let surface = surface, let event = event else { return 0 }
+    let cocoaSurface = GrCocoaSurface(root: surface.pointee)
+    return cocoaSurface.listenEvent(eventMask: eventMask, event: event)
+}
diff --git a/graph/cocoa/rules.mk b/graph/cocoa/rules.mk
new file mode 100644
index 00000000..5ba74140
--- /dev/null
+++ b/graph/cocoa/rules.mk
@@ -0,0 +1,30 @@
+#**************************************************************************
+#*
+#*  Cocoa specific rules file, used to compile the Cocoa graphics driver
+#*  to the graphics subsystem
+#*
+#**************************************************************************
+
+# Compiler and Flags
+SWIFT := swiftc
+SWIFTFLAGS := -import-objc-header $(GRAPH)/cocoa/grcocoa.h
+
+
+# Cocoa specific settings
+ifeq ($(shell uname -s), Darwin)
+  SWIFT_OBJS += $(OBJ_DIR_2)/grcocoa.o
+  FRAMEWORKS = -framework Cocoa
+  LDFLAGS += $(FRAMEWORKS)
+endif
+
+# Targets
+all: cocoa
+
+cocoa: $(SWIFT_OBJS)
+	@echo "Building Cocoa driver"
+
+$(OBJ_DIR_2)/grcocoa.o: $(GRAPH)/cocoa/grcocoa.swift
+	$(SWIFT) $(SWIFTFLAGS) -c $< -o $@
+
+clean:
+	rm -rf $(OBJ_DIR_2)/*.o
diff --git a/graph/grinit.c b/graph/grinit.c
index 0d665d55..0bfc5987 100644
--- a/graph/grinit.c
+++ b/graph/grinit.c
@@ -29,6 +29,10 @@
 #include "mac/grmac.h"
 #endif
 
+#ifdef DEVICE_COCOA
+#include "cocoa/grcocoa.h"
+#endif
+
 #ifdef DEVICE_ALLEGRO
 #include "allegro/gralleg.h"
 #endif
-- 
GitLab