~habibalamin/Commandeer

Fix `UnsafeMutableRawPointer storeBytes(of:as:)` no longer working

I am not sure when this stopped working, and my already-compiled copy of
the app still works as it always did, but I experienced the same problem
one of the app's prospective users, that a version built from source was
crashing (assuming a compiler update broke it).

At first, I thought the problem was a mismatch between the `userInfo` in
`AppDelegate` initialized as an `UnsafeMutablePointer` and the callback,
which takes an `UnsafeMutableRawPointer`. However, translating it to the
latter before doing anything with it, then using `storeBytes(of:as:)` in
`AppDelegate` made the app crash earlier, despite not being passed in to
the callback by that point. Using the `UnsafeMutablePointer` API doesn't
break anything, so we just convert it that direction instead, inside the
callback before using it.
1 files changed, 8 insertions(+), 5 deletions(-)

M Commandeer/CommandPressToEscapePressTransformer.swift
M Commandeer/CommandPressToEscapePressTransformer.swift => Commandeer/CommandPressToEscapePressTransformer.swift +8 -5
@@ 29,13 29,16 @@ func commandPressToEscapePressTransformer(proxy: CGEventTapProxy,
                                          refcon: UnsafeMutableRawPointer?)
                                          -> Unmanaged<CGEvent>?
{
  // `UnsafeMutableRawPointer` -> `UnsafeMutablePointer`
  let refcon = refcon?.bindMemory(to: CGEvent?.self, capacity: 1)

  if (event.type != .flagsChanged) {
    refcon?.storeBytes(of: nil, as: CGEvent?.self)
  } else if let currentKeyDown = refcon?.load(as: CGEvent?.self) {
    refcon?.initialize(to: nil)
  } else if let currentKeyDown = refcon?.pointee {
    if (keyEventNoFlagsPressed(event) &&
      keyEventCmdPressedAlone(currentKeyDown))
    {
      refcon?.storeBytes(of: nil, as: CGEvent?.self)
      refcon?.initialize(to: nil)

      if (event.timestamp - currentKeyDown.timestamp <= 200_000_000) // ns
      {


@@ 61,10 64,10 @@ func commandPressToEscapePressTransformer(proxy: CGEventTapProxy,
      // Don't treat this as a true cmd-pressed-alone to be stored,
      // treat it as whatever's already been stored.
    } else {
      refcon?.storeBytes(of: event, as: CGEvent?.self)
      refcon?.initialize(to: event)
    }
  } else {
    refcon?.storeBytes(of: event, as: CGEvent?.self)
    refcon?.initialize(to: event)
  }

  return Unmanaged.passRetained(event)