Converting SetDisplay to Python

Converting SetDisplay to Python

os x yosemite, display, python logos

Overview


For years we’ve been using the old C program SetDisplay (the code can be seen here, as modified by one of our previous teammates). However we’ve recently been trying to move everything to Python, and to continue that effort I’ve set about porting the C code to Python. While we may lose some performance, we hope to gain portability, readability, and maintainability, which we feel are more valuable in the long term.

A good portion of SetDisplay relies on native Apple OS X APIs, which are not readily modified from a standard Python installation. Fortunately, there is a useful Python library called PyObjC, which exists as a bridge between Python and OS X’s internal configurations (in much the same way that Objective-C works). I intend to keep as much of the rewrite in native Python as possible, while using the PyObjC library to access system features which are not otherwise accessible in Python. (Note that PyObjC is shipped with the OS X built-in Python by default.)

The end result of this project will be a simple, easy-to-use Python script that can handle all of the abilities of the original SetDisplay system (and possibly more?). However, I’ve never used PyObjC before, so my first step will be a strict rewrite of SetDisplay into Python. I won’t fix any issues with the code, and when it’s run it ought to output everything exactly the same way the original SetDisplay did. I hope by doing this I’ll gain some insight into how PyObjC can be used better to our advantage.

Since we love open source software, the Python project can be found over here on GitHub.

Details


All right, let’s dive in. Looking over SetDisplay I see there’s a struct called displayMode. Let’s go ahead an reproduce that:

Then there are all of the methods. I’ll fill those in with blank placeholders for the moment. Let’s skip ahead to the main method. Glancing through, there are a lot of values initialized up at the top. Since we’re using Python, we probably won’t need to worry about a good portion of that. I’ll try not to declare variables too far in advance, so we’ll skip all of them for the moment. I’ll also skip over the command-line option-getting portion for now, and we’ll just dive into trying to get information about our displays.

Down on line 320 we have our first real use of one of the Apple libraries: CGGetOnlineDisplayList. Well… I have no idea how to implement this in Python. After poking around, I found this great article which even touches on some of the same methods we’ll have to use! I’ll paraphrase the relevant part for you here. In short, a lot of Objective-C methods take pointers as parameters, which don’t really exist in Python. Additionally, it’s common to set the output of an Objective-C method to an error variable that will be checked later. Ignoring the terribly unPythonic nature of this system (it’s practically barbaric; let’s be honest), we can replicate it pretty easily.

Here’s the C code from SetDisplay:

And here’s the same thing in Python:

You can see that there’s something funny going on with all those None  values and the tuple output, so I’ll break it down. First, looking at the method call, there are two None  types inserted where we had pointers in the Objective-C call. Python doesn’t have pointers, so we use None . But where do the values go? They’re the extra values that are outputted by the function! Since Python allows for returning tuples, the people who wrote PyObjC decided this was the best compromise they could come up with, and I’m inclined to agree. It’s simple, and it’s consistent throughout the PyObjC library without using pass-through variables like in C.

After the first section of coding, I have some working code! It doesn’t do much except print out the identifier for each attached display, but I’m happy with that. Here’s what I’ve got so far (remembering that all methods were filled in as blanks):

And some test output on my machine:

So it works! Now to add some more functionality. I’m going to try to start with the method modeForDisplay , which I’ve renamed to get_mode_for_display . I’ll post later with further updates.

No Comments

Leave a Reply