Skip to content

Instantly share code, notes, and snippets.

@AngryAnt
Created January 31, 2019 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AngryAnt/efd1501aab794b49b47b551c4ce147fd to your computer and use it in GitHub Desktop.
Save AngryAnt/efd1501aab794b49b47b551c4ce147fd to your computer and use it in GitHub Desktop.
//
// 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