Swift: avoid unnecessary unsafe conversions; fixes crash in debug builds

This commit is contained in:
rcombs 2021-03-26 02:32:49 -05:00
parent 4991ffa859
commit a0e56cbff9
2 changed files with 14 additions and 20 deletions

View File

@ -170,13 +170,7 @@ class CocoaCB: Common {
var controlCallback: mp_render_cb_control_fn = { ( v, ctx, e, request, d ) -> Int32 in var controlCallback: mp_render_cb_control_fn = { ( v, ctx, e, request, d ) -> Int32 in
let ccb = unsafeBitCast(ctx, to: CocoaCB.self) let ccb = unsafeBitCast(ctx, to: CocoaCB.self)
// the data pointer can be a null pointer, the libmpv control callback let data = UnsafeMutableRawPointer(d)
// provides nil instead of the 0 address like the usual control call of
// an internal vo, workaround to create a null pointer instead of nil
var data = UnsafeMutableRawPointer.init(bitPattern: 0).unsafelyUnwrapped
if let dunwrapped = d {
data = dunwrapped
}
guard let vo = v, let events = e else { guard let vo = v, let events = e else {
ccb.log.sendWarning("Unexpected nil value in Control Callback") ccb.log.sendWarning("Unexpected nil value in Control Callback")
@ -189,7 +183,7 @@ class CocoaCB: Common {
override func control(_ vo: UnsafeMutablePointer<vo>, override func control(_ vo: UnsafeMutablePointer<vo>,
events: UnsafeMutablePointer<Int32>, events: UnsafeMutablePointer<Int32>,
request: UInt32, request: UInt32,
data: UnsafeMutableRawPointer) -> Int32 data: UnsafeMutableRawPointer?) -> Int32
{ {
switch mp_voctrl(request) { switch mp_voctrl(request) {
case VOCTRL_PREINIT: case VOCTRL_PREINIT:

View File

@ -523,7 +523,7 @@ class Common: NSObject {
@objc func control(_ vo: UnsafeMutablePointer<vo>, @objc func control(_ vo: UnsafeMutablePointer<vo>,
events: UnsafeMutablePointer<Int32>, events: UnsafeMutablePointer<Int32>,
request: UInt32, request: UInt32,
data: UnsafeMutableRawPointer) -> Int32 data: UnsafeMutableRawPointer?) -> Int32
{ {
guard let mpv = mpv else { guard let mpv = mpv else {
log.sendWarning("Unexpected nil value in Control Callback") log.sendWarning("Unexpected nil value in Control Callback")
@ -573,11 +573,11 @@ class Common: NSObject {
} }
return VO_TRUE return VO_TRUE
case VOCTRL_GET_DISPLAY_FPS: case VOCTRL_GET_DISPLAY_FPS:
let fps = data.assumingMemoryBound(to: CDouble.self) let fps = data!.assumingMemoryBound(to: CDouble.self)
fps.pointee = currentFps() fps.pointee = currentFps()
return VO_TRUE return VO_TRUE
case VOCTRL_GET_HIDPI_SCALE: case VOCTRL_GET_HIDPI_SCALE:
let scaleFactor = data.assumingMemoryBound(to: CDouble.self) let scaleFactor = data!.assumingMemoryBound(to: CDouble.self)
let screen = getCurrentScreen() let screen = getCurrentScreen()
let factor = window?.backingScaleFactor ?? let factor = window?.backingScaleFactor ??
screen?.backingScaleFactor ?? 1.0 screen?.backingScaleFactor ?? 1.0
@ -590,7 +590,7 @@ class Common: NSObject {
disableDisplaySleep() disableDisplaySleep()
return VO_TRUE return VO_TRUE
case VOCTRL_SET_CURSOR_VISIBILITY: case VOCTRL_SET_CURSOR_VISIBILITY:
let cursorVisibility = data.assumingMemoryBound(to: CBool.self) let cursorVisibility = data!.assumingMemoryBound(to: CBool.self)
cursorVisibilityWanted = cursorVisibility.pointee cursorVisibilityWanted = cursorVisibility.pointee
DispatchQueue.main.async { DispatchQueue.main.async {
self.setCursorVisiblility(self.cursorVisibilityWanted) self.setCursorVisiblility(self.cursorVisibilityWanted)
@ -603,7 +603,7 @@ class Common: NSObject {
return VO_TRUE return VO_TRUE
} }
let icc = data.assumingMemoryBound(to: bstr.self) let icc = data!.assumingMemoryBound(to: bstr.self)
iccData.withUnsafeMutableBytes { (ptr: UnsafeMutableRawBufferPointer) in iccData.withUnsafeMutableBytes { (ptr: UnsafeMutableRawBufferPointer) in
guard let baseAddress = ptr.baseAddress, ptr.count > 0 else { return } guard let baseAddress = ptr.baseAddress, ptr.count > 0 else { return }
let u8Ptr = baseAddress.assumingMemoryBound(to: UInt8.self) let u8Ptr = baseAddress.assumingMemoryBound(to: UInt8.self)
@ -612,13 +612,13 @@ class Common: NSObject {
return VO_TRUE return VO_TRUE
case VOCTRL_GET_AMBIENT_LUX: case VOCTRL_GET_AMBIENT_LUX:
if lightSensor != 0 { if lightSensor != 0 {
let lux = data.assumingMemoryBound(to: Int32.self) let lux = data!.assumingMemoryBound(to: Int32.self)
lux.pointee = Int32(lmuToLux(lastLmu)) lux.pointee = Int32(lmuToLux(lastLmu))
return VO_TRUE; return VO_TRUE;
} }
return VO_NOTIMPL return VO_NOTIMPL
case VOCTRL_GET_UNFS_WINDOW_SIZE: case VOCTRL_GET_UNFS_WINDOW_SIZE:
let sizeData = data.assumingMemoryBound(to: Int32.self) let sizeData = data!.assumingMemoryBound(to: Int32.self)
let size = UnsafeMutableBufferPointer(start: sizeData, count: 2) let size = UnsafeMutableBufferPointer(start: sizeData, count: 2)
var rect = window?.unfsContentFrame ?? NSRect(x: 0, y: 0, width: 1280, height: 720) var rect = window?.unfsContentFrame ?? NSRect(x: 0, y: 0, width: 1280, height: 720)
if let screen = window?.currentScreen, !Bool(mpv.opts.hidpi_window_scale) { if let screen = window?.currentScreen, !Bool(mpv.opts.hidpi_window_scale) {
@ -629,7 +629,7 @@ class Common: NSObject {
size[1] = Int32(rect.size.height) size[1] = Int32(rect.size.height)
return VO_TRUE return VO_TRUE
case VOCTRL_SET_UNFS_WINDOW_SIZE: case VOCTRL_SET_UNFS_WINDOW_SIZE:
let sizeData = data.assumingMemoryBound(to: Int32.self) let sizeData = data!.assumingMemoryBound(to: Int32.self)
let size = UnsafeBufferPointer(start: sizeData, count: 2) let size = UnsafeBufferPointer(start: sizeData, count: 2)
var rect = NSMakeRect(0, 0, CGFloat(size[0]), CGFloat(size[1])) var rect = NSMakeRect(0, 0, CGFloat(size[0]), CGFloat(size[1]))
DispatchQueue.main.async { DispatchQueue.main.async {
@ -640,7 +640,7 @@ class Common: NSObject {
} }
return VO_TRUE return VO_TRUE
case VOCTRL_GET_DISPLAY_NAMES: case VOCTRL_GET_DISPLAY_NAMES:
let dnames = data.assumingMemoryBound(to: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>?.self) let dnames = data!.assumingMemoryBound(to: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>?.self)
var array: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>? = nil var array: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>? = nil
var count: Int32 = 0 var count: Int32 = 0
let displayName = getCurrentScreen()?.displayName ?? "Unknown" let displayName = getCurrentScreen()?.displayName ?? "Unknown"
@ -654,18 +654,18 @@ class Common: NSObject {
log.sendWarning("No Screen available to retrieve frame") log.sendWarning("No Screen available to retrieve frame")
return VO_NOTAVAIL return VO_NOTAVAIL
} }
let sizeData = data.assumingMemoryBound(to: Int32.self) let sizeData = data!.assumingMemoryBound(to: Int32.self)
let size = UnsafeMutableBufferPointer(start: sizeData, count: 2) let size = UnsafeMutableBufferPointer(start: sizeData, count: 2)
let frame = screen.convertRectToBacking(screen.frame) let frame = screen.convertRectToBacking(screen.frame)
size[0] = Int32(frame.size.width) size[0] = Int32(frame.size.width)
size[1] = Int32(frame.size.height) size[1] = Int32(frame.size.height)
return VO_TRUE return VO_TRUE
case VOCTRL_GET_FOCUSED: case VOCTRL_GET_FOCUSED:
let focus = data.assumingMemoryBound(to: CBool.self) let focus = data!.assumingMemoryBound(to: CBool.self)
focus.pointee = NSApp.isActive focus.pointee = NSApp.isActive
return VO_TRUE return VO_TRUE
case VOCTRL_UPDATE_WINDOW_TITLE: case VOCTRL_UPDATE_WINDOW_TITLE:
let titleData = data.assumingMemoryBound(to: Int8.self) let titleData = data!.assumingMemoryBound(to: Int8.self)
DispatchQueue.main.async { DispatchQueue.main.async {
let title = NSString(utf8String: titleData) as String? let title = NSString(utf8String: titleData) as String?
self.title = title ?? "Unknown Title" self.title = title ?? "Unknown Title"