Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
// main.swift
// Jerboa
//
// Created by Emil Johansen on 19/01/2019.
// Copyright © 2019 AngryAnt. All rights reserved.
//
// https://en.wikipedia.org/wiki/Jerboa
//
import Foundation
class Jerboa
{
static let supportedDisplayCount: UInt32 = 64;
private static let displayKey = "DisplayID";
private static let userDefaults = UserDefaults.standard;
private static var workingDisplays = [CGDirectDisplayID] (repeating: 0, count: Int (supportedDisplayCount));
private static var current: CGDirectDisplayID = 0;
private static var displayCount = 0;
private class func readDisplays ()
{
current = CGDirectDisplayID (userDefaults.integer (forKey: displayKey));
var result: UInt32 = 0;
CGGetActiveDisplayList (Jerboa.supportedDisplayCount, &Jerboa.workingDisplays, &result);
displayCount = Int (result);
}
private class func jumpBy (delta: Int)
{
readDisplays ();
var currentIndex = Jerboa.workingDisplays.firstIndex (of: current) ?? 0;
currentIndex += delta;
if (currentIndex < 0)
{
currentIndex = displayCount - 1;
}
else if (currentIndex >= displayCount)
{
currentIndex = 0;
}
jumpTo (targetIndex: currentIndex);
}
private class func jumpTo (targetIndex : Int)
{
current = Jerboa.workingDisplays[targetIndex];
CGDisplayMoveCursorToPoint (
current,
CGPoint.init (x: CGDisplayPixelsWide (current) / 2,
y: CGDisplayPixelsHigh (current) / 2)
);
CGDisplayShowCursor (current);
CGDisplayShowCursor (current);
CGDisplayShowCursor (current);
userDefaults.set (current, forKey: displayKey);
}
class func startup ()
{
userDefaults.synchronize ();
userDefaults.integer (forKey: displayKey);
}
class func teardown ()
{
userDefaults.synchronize ();
}
class func forward ()
{
jumpBy (delta: 1);
}
class func backward ()
{
jumpBy (delta: -1);
}
class func first ()
{
readDisplays ();
jumpTo (targetIndex: 0);
}
class func last ()
{
readDisplays ();
jumpTo (targetIndex: displayCount - 1);
}
}
Jerboa.startup ();
for argument in CommandLine.arguments
{
switch (argument)
{
case "forward":
Jerboa.forward ();
break;
case "backward":
Jerboa.backward ();
break;
case "first":
Jerboa.first ();
break;
case "last":
Jerboa.last ();
break;
default:
Jerboa.forward ();
break;
}
}
Jerboa.teardown ();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.