Skip to content

Instantly share code, notes, and snippets.

@saivert
Last active January 10, 2023 20:19
Show Gist options
  • Save saivert/b1de177baf17840579bf249f257d836c to your computer and use it in GitHub Desktop.
Save saivert/b1de177baf17840579bf249f257d836c to your computer and use it in GitHub Desktop.
@_exported import Glibc
@_exported import ddbswift_C
import Foundation
public struct dbapi {
public private(set) var apiptr: UnsafePointer<DB_functions_t>
public init(api: UnsafePointer<DB_functions_t>) {
apiptr = api
}
public func seek(_ pos: UInt32) {
_ = self.apiptr.pointee.sendmessage(UInt32(DB_EV_SEEK), 0, 10000, 0)
}
public func sendmessage(id: UInt32, ctx: UInt, p1: UInt32, p2: UInt32) -> Int32 {
self.apiptr.pointee.sendmessage(id, ctx, p1, p2)
}
}
public struct plugin {
private var db: dbapi
public init(api: dbapi) {
self.db = api
}
public func start() -> Int32 {
return 0
}
public func stop() -> Int32 {
return 0
}
public func message(id: UInt32, ctx: UInt, p1: UInt32, p2: UInt32) -> Int32 {
if id == DB_EV_SONGCHANGED {
db.seek(1000)
}
return 0
}
}
func start() -> Int32 {
plginstance.start()
}
func stop() -> Int32 {
plginstance.stop()
}
func message(id: UInt32, ctx: UInt, p1: UInt32, p2: UInt32) -> Int32 {
plginstance.message(id: id, ctx: ctx, p1: p1, p2: p2)
}
enum Constants {
static let name = NSString("Swift DDB")
static let descr = NSString("swift!!")
static let copyright = NSString("gpl")
}
var plug = DB_plugin_t(
type: Int32(DB_PLUGIN_MISC),
api_vmajor: Int16(DB_API_VERSION_MAJOR),
api_vminor: Int16(DB_API_VERSION_MINOR),
version_major: 0,
version_minor: 1,
flags: 0,
reserved1: 0,
reserved2: 0,
reserved3: 0,
id: strdup("someid"),
name: Constants.name.utf8String,
descr: Constants.descr.utf8String,
copyright: Constants.copyright.utf8String,
website: nil,
command: nil,
start: start,
stop: stop,
connect: nil,
disconnect: nil,
exec_cmdline: nil,
get_actions: nil,
message: message,
configdialog: nil
)
var deadbeef : dbapi!
var plginstance : plugin!
@_cdecl("libddbswift_load") func libddbswift_load(api: UnsafePointer<DB_functions_t>) -> UnsafeMutablePointer<DB_plugin_t> {
deadbeef = dbapi(api: api)
plginstance = plugin(api: deadbeef)
return UnsafeMutablePointer<DB_plugin_t>(&plug)
}
module ddbswift_C {
header "deadbeef.h"
export *
}
// swift-tools-version: 5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "ddbswift",
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "ddbswift",
type: .dynamic,
targets: ["ddbswift"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "ddbswift_C",
dependencies: [],
path: "Sources/C"
),
.target(
name: "ddbswift",
dependencies: [
.target(name: "ddbswift_C")
],
path: "Sources/Swift"
), .testTarget(
name: "ddbswiftTests",
dependencies: ["ddbswift"]),
]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment