Skip to content

Commit

Permalink
split bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
Jomy10 committed Nov 28, 2024
1 parent 31b7edd commit 07a52c0
Show file tree
Hide file tree
Showing 16 changed files with 218 additions and 103 deletions.
130 changes: 75 additions & 55 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,26 @@

import PackageDescription

let targets: [Target]
#if canImport(FoundationNetworking)
targets = [
.systemLibrary(
name: "C_ncurses",
pkgConfig: "ncurses",
providers: [
.brew(["ncurses"]),
.apt(["libncurses5-dev"]),
.yum(["ncurses-devel"])
]),

.systemLibrary(
name: "C_ncursesw",
pkgConfig: "ncursesw",
providers: [
.brew(["ncurses"]),
.apt(["libncursesw5-dev"]),
.yum(["ncurses-devel"])
var targets: [Target] = [
.systemLibrary(
name: "C_ncurses",
pkgConfig: "ncurses",
providers: [
.apt(["libncurses5-dev"]),
]),
.target(
name: "C_ncursesBinds",
dependencies: ["C_ncurses"],
cSettings: [
.headerSearchPath("../C_ncurses")
]),

.target(
name: "SwiftCurses",
dependencies: [
"C_ncurses",
"C_ncursesw",
]),

"C_ncursesBinds"
],
swiftSettings: []),
.executableTarget(
name: "Examples",
dependencies: ["SwiftCurses"],
Expand All @@ -41,46 +33,74 @@ targets = [
"-Xfrontend",
"-warn-long-expression-type-checking=100"
])]),

.testTarget(
name: "ncursesTests",
dependencies: ["SwiftCurses"]),
]

#if os(macOS)
targets
.first(where: { $0.name == "SwiftCurses" })!
.swiftSettings!
.append(.define("SWIFTCURSES_OPAQUE"))
#else
targets = [
.systemLibrary(
name: "C_ncurses",
providers: [
.brew(["ncurses"]),
.apt(["libncurses5-dev", "libncursesw5-dev"]),
.yum(["ncurses-devel"])
]),
targets.append(
.systemLibrary(
name: "C_ncursesw",
pkgConfig: "ncursesw",
providers: [
.brew(["ncurses"]),
.apt(["libncursesw5-dev"]),
.yum(["ncurses-devel"])
])
)
targets
.first(where: { $0.name == "SwiftCurses" })!
.dependencies
.append("C_ncursesw")
#endif

.target(
name: "SwiftCurses",
dependencies: [
"C_ncurses",
],
swiftSettings: [
.define("SWIFTCURSES_OPAQUE")
]),
//#if canImport(FoundationNetworking)
//targets = [

.executableTarget(
name: "Examples",
dependencies: ["SwiftCurses"],
exclude: ["README.md"],
swiftSettings: [.unsafeFlags([
"-Xfrontend",
"-warn-long-function-bodies=100",
"-Xfrontend",
"-warn-long-expression-type-checking=100"
])]),

.testTarget(
name: "ncursesTests",
dependencies: ["SwiftCurses"]),
]
#endif
//]
//#else
//targets = [
// .systemLibrary(
// name: "C_ncurses",
// providers: [
// .brew(["ncurses"]),
// .apt(["libncurses5-dev", "libncursesw5-dev"]),
// .yum(["ncurses-devel"])
// ]),

// .target(
// name: "SwiftCurses",
// dependencies: [
// "C_ncurses",
// "C_ncursesBinds"
// ],
// swiftSettings: [
// .define("SWIFTCURSES_OPAQUE")
// ]),

// .executableTarget(
// name: "Examples",
// dependencies: ["SwiftCurses"],
// exclude: ["README.md"],
// swiftSettings: [.unsafeFlags([
// "-Xfrontend",
// "-warn-long-function-bodies=100",
// "-Xfrontend",
// "-warn-long-expression-type-checking=100"
// ])]),

// .testTarget(
// name: "ncursesTests",
// dependencies: ["SwiftCurses"]),
//]
//#endif

let package = Package(
name: "SwiftCurses",
Expand Down
3 changes: 3 additions & 0 deletions Sources/C_ncurses/c_ncurses.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define NCURSES_WIDECHAR 1
#include <ncurses.h>
#include <stdlib.h>
2 changes: 1 addition & 1 deletion Sources/C_ncurses/module.modulemap
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module ncurses [system] {
header "ncurses.h"
header "c_ncurses.h"
link "ncurses"
export *
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
#define NCURSES_WIDECHAR 1
#include <ncurses.h>
#include <c_ncurses.h>
#include <stdlib.h> // Required for wctomb
#include "include/C_ncursesBinds.h"

#ifndef wget_wch
int wget_wch(WINDOW *win, wint_t *ch);
#endif
#ifndef waddwstr
int waddwstr(WINDOW *win, const wint_t *ch);
#endif

// Swift does not pick up the `get_wch` method
static inline int swift_wget_wch(WINDOW* win, wchar_t* ch) {
inline int swift_wget_wch(WINDOW* win, wchar_t* ch) {
return wget_wch(win, ch);
}

static inline int swift_waddwstr(WINDOW* win, const wchar_t* ch) {
inline int swift_waddwstr(WINDOW* win, const wchar_t* ch) {
return waddwstr(win, ch);
}

struct swift_YX {
int y, x;
};

static inline struct swift_YX swift_getbegyx(WINDOW* win) {
inline struct swift_YX swift_getbegyx(WINDOW* win) {
struct swift_YX yx;
getbegyx(win, yx.y, yx.x);
return yx;
}

static inline struct swift_YX swift_getmaxyx(WINDOW* win) {
inline struct swift_YX swift_getmaxyx(WINDOW* win) {
struct swift_YX yx;
getmaxyx(win, yx.y, yx.x);
return yx;
}

static inline struct swift_YX swift_getyx(WINDOW* win) {
inline struct swift_YX swift_getyx(WINDOW* win) {
struct swift_YX yx;
getyx(win, yx.y, yx.x);
return yx;
}

static inline struct swift_YX swift_getparyx(WINDOW* win) {
inline struct swift_YX swift_getparyx(WINDOW* win) {
struct swift_YX yx;
getparyx(win, yx.y, yx.x);
return yx;
Expand All @@ -54,7 +59,7 @@ const int swift_A_PROTECT = A_PROTECT;
const int swift_A_INVIS = A_INVIS;
const int swift_A_ALTCHARSET = A_ALTCHARSET;
const int swift_A_CHARTEXT = A_CHARTEXT;
static inline int swift_COLOR_PAIR(int n) {
inline int swift_COLOR_PAIR(int n) {
return COLOR_PAIR(n);
}
#ifdef A_ITALIC
Expand All @@ -67,11 +72,6 @@ const int swift_A_ITALIC = NCURSES_BITS(1U,23);
// Wide char function
//====================

struct swift_wcharBytesReturnType {
const int8_t* bytes;
int len;
};

struct swift_wcharBytesReturnType wcharToBytes(wchar_t wc) {
static char charBuffer[10];
int len = wctomb(charBuffer, wc);
Expand All @@ -86,7 +86,7 @@ struct swift_wcharBytesReturnType wcharToBytes(wchar_t wc) {
//======

// fn keys
static inline int swift_key_f(int n) {
inline int swift_key_f(int n) {
return KEY_F(n);
}

Expand Down
80 changes: 80 additions & 0 deletions Sources/C_ncursesBinds/include/C_ncursesBinds.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#ifndef _SWIFTCURSES_BINDS
#define _SWIFTCURSES_BINDS

#include <ncurses.h>
#include <wchar.h>

int swift_wget_wch(WINDOW* win, wchar_t* ch);
int swift_waddwstr(WINDOW* win, const wchar_t* ch);

struct swift_YX {
int y, x;
};

struct swift_YX swift_getbegyx(WINDOW* win);
struct swift_YX swift_getmaxyx(WINDOW* win);
struct swift_YX swift_getyx(WINDOW* win);
struct swift_YX swift_getparyx(WINDOW* win);

//============
// Attributes
//============

extern const int swift_A_NORMAL;
extern const int swift_A_STANDOUT;
extern const int swift_A_UNDERLINE;
extern const int swift_A_REVERSE;
extern const int swift_A_BLINK;
extern const int swift_A_DIM;
extern const int swift_A_BOLD;
extern const int swift_A_PROTECT;
extern const int swift_A_INVIS;
extern const int swift_A_ALTCHARSET;
extern const int swift_A_CHARTEXT;
int swift_COLOR_PAIR(int n);
extern const int swift_A_ITALIC;

//====================
// Wide char function
//====================

struct swift_wcharBytesReturnType {
const int8_t* bytes;
int len;
};

struct swift_wcharBytesReturnType wcharToBytes(wchar_t wc);

//======
// Keys
//======

int swift_key_f(int n);

extern const mmask_t swift_button1Pressed; // mouse button 1 down
extern const mmask_t swift_button1Released; // mouse button 1 up
extern const mmask_t swift_button1Clicked; // mouse button 1 clicked
extern const mmask_t swift_button1DoubleClicked; // mouse button 1 double clicked
extern const mmask_t swift_button1TripleClicked; // mouse button 1 triple clicked
extern const mmask_t swift_button2Pressed; // mouse button 2 down
extern const mmask_t swift_button2Released; // mouse button 2 up
extern const mmask_t swift_button2Clicked; // mouse button 2 clicked
extern const mmask_t swift_button2DoubleClicked; // mouse button 2 double clicked
extern const mmask_t swift_button2TripleClicked; // mouse button 2 triple clicked
extern const mmask_t swift_button3Pressed; // mouse button 3 down
extern const mmask_t swift_button3Released; // mouse button 3 up
extern const mmask_t swift_button3Clicked; // mouse button 3 clicked
extern const mmask_t swift_button3DoubleClicked; // mouse button 3 double clicked
extern const mmask_t swift_button3TripleClicked; // mouse button 3 triple clicked
extern const mmask_t swift_button4Pressed; // mouse button 4 down
extern const mmask_t swift_button4Released; // mouse button 4 up
extern const mmask_t swift_button4Clicked; // mouse button 4 clicked
extern const mmask_t swift_button4DoubleClicked; // mouse button 4 double clicked
extern const mmask_t swift_button4TripleClicked; // mouse button 4 triple clicked
extern const mmask_t swift_buttonShift; // shift was down during button state change
extern const mmask_t swift_buttonCtrl; // control was down during button state change
extern const mmask_t swift_buttonAlt; // alt was down during button state change
extern const mmask_t swift_allMouseEvents; // report all button state changes
extern const mmask_t swift_reportMousePosition; // report mouse movement

#endif
25 changes: 13 additions & 12 deletions Sources/SwiftCurses/Window+attributes.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ncurses
import C_ncursesBinds

public enum Attribute: Sendable, Hashable {
case normal
Expand All @@ -21,31 +22,31 @@ extension Attribute {
var value: Int32 {
switch self {
case .normal:
return ncurses.swift_A_NORMAL
return swift_A_NORMAL
case .standout:
return ncurses.swift_A_STANDOUT
return swift_A_STANDOUT
case .underline:
return ncurses.swift_A_UNDERLINE
return swift_A_UNDERLINE
case .reverse:
return ncurses.swift_A_REVERSE
return swift_A_REVERSE
case .blink:
return ncurses.swift_A_BLINK
return swift_A_BLINK
case .dim:
return ncurses.swift_A_DIM
return swift_A_DIM
case .bold:
return ncurses.swift_A_BOLD
return swift_A_BOLD
case .protect:
return ncurses.swift_A_PROTECT
return swift_A_PROTECT
case .invisible:
return ncurses.swift_A_INVIS
return swift_A_INVIS
case .altCharset:
return ncurses.swift_A_ALTCHARSET
return swift_A_ALTCHARSET
case .chartext:
return ncurses.swift_A_CHARTEXT
return swift_A_CHARTEXT
case .colorPair(let n):
return swift_COLOR_PAIR(n)
case .italic:
return ncurses.swift_A_ITALIC
return swift_A_ITALIC
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftCurses/Window+border.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ncurses
import C_ncursesBinds

// https://invisible-island.net/ncurses/man/curs_border.3x.html
// X/Open does not define any error conditions. This implementation returns an error if the window pointer is null.
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftCurses/Window+clear.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ncurses
import C_ncursesBinds

/// Clear until option for `WindowProtocol.clear(until: ClearUntil)` method
public enum ClearUntil: Sendable, Hashable {
Expand Down
Loading

0 comments on commit 07a52c0

Please sign in to comment.