Archive for the 'Macintosh' Category

Survey of Cross Platform GUI Architectures

I’m a Mac guy, but I’ve spent a large part of my time working for large software companies. That means that I usually work on a cross platform product that supports both Mac and Windows. The upshot of that is each product has to have a platform layer of some kind so that the correct Windows API’s are called on Windows and the correct Mac API’s are call on the Mac. In my tenure I’ve encountered several different ways of tackling this problem. Some worked very well, while others made me want to quit and become and mime.

The interesting thing to note here is that historically the Mac and Windows API’s weren’t all that different. Sure the Mac Toolbox calls started out in Pascal, but most apps use the C bindings these days. Both the Toolbox and Win32 have very similar ideas about windows, menus, and events, especially when comparing the Win32 API with the Carbon Toolbox. There’s definitely not a one to one mapping for everything, but it wasn’t all that hard to see where you could write a compatibility layer and support both.

There are a few different techniques/architectures for building cross platform frameworks. For this discussion I’ll ignore entire platforms like Java and Adobe Flash which employ virtual machines. I’ll limit discussion to natively compiled applications.

At the end of each section, I’ll rate the architecture. They’ll be evaluated based the cost of maintenance, the initial cost of development, and the user experience they advocate.

  1. Emulation

    This is where you attempt to make one platform look like the other. For example you implement all the Mac Toolbox API’s that you use on Windows using the Win32 API. That way, you can program to the Mac API’s and it will just “magically” work on Windows. However, given platform popularity this usually means implementing MFC or Win32 on the Mac. Note that this isn’t emulation like what emulators do, but simply implementing a platform API on a different platform.

    This technique is unfortunately popular. There are some assumptions that go along with this:

    • The only thing required to develop for a platform is writing to the appropriate API.
    • If you emulate MFC on the Mac you can hire cheap MFC programmers and get a Mac version for free. (I’ve had managers tell me this with a straight face.)
    • It is cheaper and easier to maintain an emulation layer for a platform than the other techniques.
    • The platform on which the emulation is taking place is the only platform that suffers.
    • There is no other way to port an existing Windows application to another platform.

    It turns out every one of these assumptions are wrong.

    The first assumption assumes that the emulation layer is going to take care of everything and a Windows programmer will never have to know anything about the Mac. Unfortunately I can guarantee the emulation layer won’t be able to take care of everything and the Windows programmers will have to know something about the Mac. They will have to at least be able to build it on the Mac and figure out if their feature actually works there. But more than likely they’ll also have to know some basic UI guidelines and a general idea of how Mac users expect things to work. This includes the correct menu layout, correct dialog control layout, and the correct modifier keys to use.

    The second assumption implies that you can get away with only hiring Windows programmers and they will never ever have to touch the Mac code. Ha! The Mac emulation layer will have to be upgraded and maintained throughout the entire lifecycle of the product, almost daily. The emulation layer definitely is not a write-once-use-many-times sort of thing. You’ll start using new parts of the Win32 API that need to be implemented on the Mac, find bugs in the current implementation, and need to take advantage of new technology on the Mac.

    The conclusion to draw from the first two assumptions is any engineer will have to know or learn some basics about the Mac. They will at least need to be able to read some basic Mac GUI code and debug it.

    Thirdly, many people look at Emulation compared to, say, Core/Edge and draw the conclusion that emulation is half as cheap because the framework only exists on the Mac (in Core/Edge the framework exists on both platforms). They completely forget about the size of the framework. In Code/Edge you typically have a thin framework on each platform, where in Emulation you have a heavy framework on the Mac. Sure you don’t have anything on Windows, but you spending a lot more time on the Mac just maintaining the framework there.

    The fourth assumption people just seem to forget about. I mean, if you’re emulating Windows on the Mac, only the Mac port is going to suffer, right? Nope. Your cheap Windows engineer is going to start writing some code using some newfangled Win32 API and realize that its not implemented on the Mac emulation layer. Oops. They’d implement it on the Mac themselves, but you only hired cheap Windows programmers, remember? At this point, the engineer has to decide if they will ditch the new Win32 API’s or write a Windows only feature. So the Windows app suffers too because it becomes difficult to use the new Windows API’s. In other words, you’re really hurting both the Mac and Windows ports and the users are going to notice that.

    The last assumption is that there’s no other way to support another platform. If you’ve got a mature, existing Windows program its not going to be pleasant to port it no matter what option you choose. However, as I outline next, Emulation has a lot of hidden costs that end up making it much more expensive than the other options.

    Emulation is also non-incremental. You have to get a substantial amount of the framework written before you can start using it. This means there’s a large up front cost. In my experience it takes about ten or more engineers for about year to implement something reasonably resembling MFC on the Mac. It varies depending on how much Mac expertise you have. There’s also a large cost of upgrading this framework because you can’t upgrade one window at a time to use HIViews (or whatever), they have to be all upgraded at once.

    The cost of adding a platform (e.g. Linux) is the same as the initial development. That’s because the platform being emulated (e.g. Windows) isn’t abstracted out in any way.

    Finally, the Emulation framework is never complete. No matter how hard you work you’ll never get the Mac to look and behave like Windows even at the API level. It simply can’t be done. There are controls that the Mac has, that Windows doesn’t, and vice-versa. There are highlight and system colors that don’t have counterparts. This will trip up your Windows engineers who expect the Mac to behave like Windows. Since they don’t have any experience on the Mac they don’t know what to do, and you’re stuck.

    This is by far the worst way to approach cross platform development, and you should avoid it at all costs. I’ve worked on some large scale projects that used this approach and it was horrendous. The Windows programmers are encouraged to be clueless about the Mac and to hold up that development. It became difficult to do anything with the emulation layer except tread water. This lead to frustration and burn out. In other words, this approach is a great way to lose all your Mac engineers.

    Maintenance: D
    Initial Cost: F
    User experience: C

  2. Bridge Pattern

    This is what Qt does. You write an entire application framework that entirely abstracts out all platform details, and provide a completely consistent cross platform interface. As a result, this architecture tends to like to do everything manually. Qt does not use native controls, but draws the controls itself. It is yet another API set to learn but its really the only one you need to know (almost). You write to this one API and it works on all the platforms.

    The benefit is obvious — cross platform consistency. You can just write to the framework and it will just magically work on every platform. Since the framework does everything manually, there is a lot of control to force the consistency.

    The are a few disadvantages to this approach. First and foremost, its usually expensive. Either you have to buy a framework that does this (like Qt) or develop it yourself. Unfortunately this approach doesn’t really lend itself to an incremental implementation. To do the most basic thing you need a fairly complete application framework on each of the platforms.

    Second, the framework might not be as native as you’d like on each of the platforms. Qt has this problem. To ensure the messaging and event system works completely the same on each platform, the framework will often reimplement large parts of the native platform. This makes it easier to program against, but hurts the user because it doesn’t feel completely native.

    Lastly, the framework is yet another API to learn. Both your Mac programmers and Windows programmers have to learn it, if they do not already know it. If, for whatever reason, you need to add a custom control, then you have to start digging around in platform specific code. In that case, the bridge abstraction does not help you. Therefore, having someone fluent in Qt but not Mac and/or Windows (or vice versa) isn’t useful.

    Maintenance: B
    Initial Cost: F
    User experience: C

  3. Core/Edge

    This is a fairly simple idea. You write all your application logic in Standard C++ (or whatever common language and library). This is the “core” part, and should make up the majority of the code. In a MVC architecture, it should optimally be the model and controller parts. You also write core interfaces (class and function declarations) to access the platform specific code, such as the UI (aka the view part). The platform specific code, called the edge code, is written using a native framework or API’s.

    This strategy has several advantages. First, it gives the user the native feel and behavior of the platform because you end up using the native framework or API. Core/Edge doesn’t favor one platform over the other, but treats each platform equally. That means each can use the appropriate technologies with out have to emulate the other platform. It also means if one platform has a technology that another doesn’t its easy to use it without hurting the other platform.

    Second, it can be implemented incrementally. Since you’re using the native API’s you just start building the app on each platform as you normally would. As you start getting into common logic, or model classes, or controller classes, you simply switch to Core code. That means you write in your standard language with its standard library. (By standard I mean the language that is implemented on both platforms, like C++.) Typically your files will be broken in Core files and Edge files. If Core code needs to call back into Edge code, it calls into a Core interface. The Core interface only takes types defined by the standard language or the Core itself. The Core interface is then implemented by Edge code.

    The disadvantage with this approach is that it is easy to degenerate into the Edge/Edge approach. Doing this means you end up duplicating a lot of controller logic on each platform, or perhaps even view code. Some care has to be taken in ensuring the maximum amount of code is written such that it is Core.

    Another disadvantage is GUI resources. Since a native framework is used, then each platform is going to have a duplicate set of resources. For a given dialog, it will have to be re-layed out for each platform. If a control needs to be added, it will have be done once for each platform. This actually also provides an upside, which is each platform can use the native layout.

    In my experience, Core/Edge is the best architecture to use. It has a relatively low cost, high code reuse, and a very native feel.

    Maintenance: B
    Initial Cost: A
    User experience: A

  4. Edge/Edge

    This is just what it sounds like. Usually there is some common application logic that is shared (as in Core/Edge) but the majority of UI controller code is just duplicated on each platform. There typically is not much effort to make sure any code compiles on both platforms, but each platform is built separately from fairly different codebases.

    The one advantage of this approach is that the application will feel very native, which the user will appreciate. The programmers for one platform can concentrate on making it highly integrated into that OS, and make use of new technologies.

    The major downside is very little code is leveraged or reused. That means most code ends up being duplicated, once for each platform. That can mean the application gets prohibitively expensive to maintain, and features cost a lot more to implement.

    Maintenance: D
    Initial Cost: B
    User experience: A

This list is not meant to be comprehensive, but to outline the architectures I have experienced. Applications can also use hybrid approaches that mix and match the architectures.

The future of cross platform architectures is going to be interesting. Recently the Mac and Windows API’s have been seriously diverging. Apple is pushing Cocoa, its Objective-C application framework. Not only is it in a different language, its much higher level than the Carbon API’s and has a different object model. Meanwhile Microsoft is pushing C# and .Net. Once again, a different language and a different object model. It is no longer as obvious as how to abstract out both API’s.

As a result, the Emulation architecture, and to a lesser extent the Bridge architecture, become a lot harder to implement. Cocoa is a much higher level than MFC, so implementing MFC in terms of it would be near impossible. The Bridge architecture would also have to find some high level abstractions to properly encapsulate both .Net and Cocoa. In reality, it would probably have to continue what it is doing now: using very low level API’s and do everything manually. Conversely, both the Core/Edge and Edge/Edge would be able to support both Cocoa and .Net. Since the framework is application specific and incremental, its abstractions can be changed to encapsulate the new native frameworks with relative ease.

I will freely admit I am very biased when it comes to cross platform GUI architectures. But that bias comes from me being forced to use each of these in large software systems. From the perspective of this Mac engineer, the Core/Edge architecture is by far the best.

Days of our Apple

It has recently come to my attention that there are currently no Apple based soap operas. Sure, there’s As the Apple Turns, but it hasn’t been updated in many months. I also have my doubts as to it being a real soap opera; nobody ever got amnesia. What kind of soap opera is that? There’s also Crazy Apple Rumors, which occasionally reads like a soap opera, but once again, no amnesia.

In order to fill this desperate need, and some free time, I am prepared to take up this gauntlet. We all have to make sacrifices sometime.

The scene opens with a product meeting about the new iPhone. Steve Jobs, Tony Fadell, and Tim Cook are all in attendance.

Jobs: Tony, how are we doing on the iPhone with a 52″ screen and telepathic interface?

Tony: Steve…

Jobs: Yes?

Tony: Its no use hiding behind your technical jargon.

Jobs: What?

Tony: Steve… don’t you know I love you?

Jobs: No! Its not possible!

Tony: Yes, it true!

Jobs: No, it can’t be. For you see… I’m not really Steve…

Tony: *gasp*

Jobs: I’m Steve’s evil twin quintuplet, Stewart!

Tony: You don’t mean… *gasp*

Jobs: Yes… *sob* Yes. I have amnesia!

Tony: *gasp*

Gil Amelio: *gasp*

Jobs: Who let you in?

Gil Amelio: Sorry. Here’s your pizza.

Jobs: Anyway… Tim, you’re looking a little large lately.

Tim: Yes… I’m… I’m pregnant.

Tony: *gasp*

Gil Amelio: *gasp*

Jobs: Are you done yet?

Gil Amelio: Um, yes. Sorry. I’m leaving.

Tim: Yes, Steve. I’m… I’m pregnant.

Tony: But… how??

Tim: Well, you see Tony, when a man loves a woman, they…

Tony: No, I mean, how does a man get pregnant? Don’t you need a uterus or something?

Tim: Oh. I’m not sure. But it happened to Arnold Schwarzenegger one time, so I’m sure its possible.

Jobs: Who’s the… father? …mother? …whatever… you know.

Tim: I don’t know. For you see… I have amnesia!

Tony: *gasp*

Jobs: Do you have to keep doing that?

Tony: Um… yes, actually. You put that in my contract. See? “Must gasp at any and all amnesia revelations.” Seemed a little weird at the time, but makes perfect sense now.

Jobs: Oh, right. I forgot… for you see.. I have amnesia!

Tony: *gasp*

Tim: Yes, yes, we covered that already. Shouldn’t we get back to the iPhone thingie?

Jobs: *sigh* Sure why not. What’s the problem again?

Tony: Um, we don’t know how to make one.

Jobs: Oh. Hmm.. that’s sort of a buzzkill. Does anyone else have any shocking revelations instead?

Jobs Evil Twin #4: Yes… I do.

Tony: *gasp* I thought you were dead!

Jobs Evil Twin #4: I was… in my heart. For you see… I have always loved you, Tim.

Tim: *sobs* I… I know… but our love is forbidden.

Jobs Evil Twin #4: Yes. That’s why I went into exile… to protect your feelings.

Jobs: Sounds boring.

Jobs Evil Twin #4: Um, yes. That’s why I made this.

Evil Twin #4 produces an iPhone.

Jobs: Sweet. I guess we’re done here.

Evil Twin #4: But wait… doesn’t anyone care that I have amnesia??

Tony: Not really.

Tune in next time, when someone else forgets they have an evil twin brother!

I probably need professional help.

Always Outnumbered

Its hard being a Mac guy. Even if you’re not an engineer you have to deal with no tech support, lack of Mac friendly services, and Windows users teasing you that Photoshop is the only game available on the Mac. psh. They always forget about Microsoft Excel.

So what’s different when you’re a Mac engineer on a cross platform product? A lot. Let me tell you, if you’re going to be a Mac user, you might as well be an engineer. Its that good.

First, IT is thrilled when you come around and line up to help you. Back when I first joined Macromedia they were just getting around to installing Airport networks. In addition to setting up the base stations they obviously had to get wireless cards for all the computers and get them installed. During that time I had one of the Pismo Powerbooks which required a somewhat involved procedure to get the Airport card installed. You had to pry up the keyboard, use a screwdriver to remove the harddrive, and then somehow wedge the darn thing into place without bending any of the pins. I’m pretty sure the packaged instructions also suggested sacrificing a squirrel to ensure it’d fit, but I wasn’t sure how I’d explain all the blood to facilities. I mean, it wasn’t Thursday.

When I first got my Airport card I was just going to install it myself until I realized what was involved. It also occurred to me that we had an IT guy whose sole job was to be “the Mac guy.” I’ll call him Dick, because it just seems appropriate. Dick didn’t work on PC’s or the network, he only worked on Macs. That was it; his sole purpose in life. I thought I could drop off the PowerBook to get the Airport card installed and continue doing “engineering things”[1]. I moseyed on down to Dick’s office and cheerfully asked if he’d install the Airport card for me. Without even looking up, he said “Most Mac users are more self reliant.”

Feel the love.

Oddly enough, Dick was one of the first people let go when the layoffs came. I don’t know why.

But don’t worry, the loving support of your corporate family doesn’t end there. As the sole Mac engineer[2] you get to be everybody’s mother. You get to clean up their messes in the Mac code when they didn’t feel like doing it right. This experience is heightened by it being like an Easter Egg hunt. They’re not going to tell you about their mistakes, you get to go look for them when the bug reports roll in. And no, rubbing their noses in the naughty code doesn’t improve the Windows engineers’ behavior. It only seems to encourage them.

However, the biggest gift your engineering brothers can give you is the opportunity to interact with the Mothership, and impress the smart people at Apple. When Apple releases a new version of Mac OS X sometimes existing versions of applications break. This is often because engineers were relying on undocumented or unsupported behavior. When Tiger was released I had the great fortune of finding one of those special “Easter Eggs” that one of my fellow engineers had thoughtfully left for me.

The easter egg was pretty simple. Mac OS X is now a version of Unix and uses the Unix path separator, “/”. In a previous life, the Mac had its very own path separator, “:”. The result is sometimes Mac OS X wants an old school Mac paths, but other times it insists on the new style Unix paths. In this case Dreamweaver was given a Unix path but needed a Mac path. The solution is simple: you give the Mac your Unix path and tell its a Unix path and ask for a Mac path back. Well, this was apparently beyond my fellow engineer, who was apparently convinced the correct way to handle the situation was to give the Mac his Unix path, but call it a Mac path, and ask for a Windows style path back. For reasons that probably baffled him, this didn’t do exactly what he wanted. So he wrote some special code to hack up the resulting path so it would work on his machine.

The problem here is that this was all in shipping code when Tiger came out. Management didn’t want to go back and re-release the previous version with this one bug fix for obvious reasons. The only solution they saw was to ask me to ask Apple to change their API’s to do something random instead of what they were documented to do. And let me tell you, Apple was thrilled to change their code. I ended up speaking with an Apple engineer about it:

Me: Hey, do you know that API that converts Unix paths into Mac paths?

Apple Engineer: You mean the one that’s so well designed and documented that a brain-dead chimp could figure it out?

Me: Um… yeah, that one. We… we need you to make that not do what it supposed to do.

Apple Engineer: ….

Me: Yeah, so a Windows engineer got into the code and…

Apple Engineer: Are you on crack?

Me: No, no, a Windows engineer wrote the code. They didn’t know what they were doing.

Apple Engineer: …right….

Me: Honest, I would never write code like that!

Apple Engineer: But you need an API to do the opposite of what its documented and designed to do?

Me: Right, right. But its not for me. Its for a Windows engineer.

Apple Engineer: ….

Me: ….

Apple Engineer: …

Me: Are you guys hiring?

Apple Engineer: click

Me: Hello?

I’m expecting a call from them any day now.

Being a Mac engineer means your fellow engineers will always keep you on your toes and have the upmost respect for you. I often had to update the Mac tools to get new features or improved support. This required the other engineers to upgrade the tools on their machines as well to maintain the ability to build the product. So after making the appropriate changes and checking them in, I’d send out an email with instructions. They were promptly ignored. Without fail, a few days later the following conversation would occur at least once:

Fellow Engineer: (Agitated, as if something’s wrong) Hey, did you know the Mac doesn’t build?

Me: hmm… well we had an automated build this morning and I just sync’d and built. Maybe its something on your machine?

Fellow Engineer: No, no. I reinstalled all the tools on my machine and did a clean sync. Something is definitely broken.

Me: OK, what errors are you getting?

Fellow Engineer: (Proceeds to describe the exact errors I outlined in my email that you would get if you didn’t upgrade)

Me: Did you follow the instructions in my email that I sent out?

Fellow Engineer: What email?

Me: The one titled “Follow these instructions if you ever want to build on the Mac again”.

Fellow Engineer: Oh….. was that important?

Me: If you want to build on the Mac, it is.

Fellow Engineer: Oh…. I’m pretty sure I deleted that immediately.

Me: (sighs) Do you want me to resend you those instructions?

Fellow Engineer: Well… I just have this Mac specific bug here. Maybe you could take it?

Me: I don’t know…

Fellow Engineer: Great! I’ll go assign it to you now!

Me: (mumbles obscenities)

Fortunately upper management is always there to offer guidance, like any good parent would. At one point we were getting unsubstantiated reports of performance problems. Upper management knew the best way to handle it was to ask Apple what the problem was. Being the naive engineer, I suggested that we might actually try to figure out what was allegedly slow and work on it. I was assured that wasn’t acceptable. We definitely needed to ask Apple what the problem was before we did anything. I’m not sure what my email was supposed to say. Maybe “Hi. We don’t have a clue. Do you have an app that fixes that?”

But in the end, its the customers that make it all worth while. They always have encouraging things to say. Like “Who wrote this? A trained squirrel? Hire a real Mac programmer!” It warms my heart. But without a doubt, I always enjoy the interaction with customers that I get on the beta lists. After spending months on a feature you have uplifting conversations like this:

Customer: This feature you just spent the past six months on sucks!

Me: Oh. Well what’s wrong with it?

Customer: It uses blue. Blue! What were you thinking?!?!

Me: Oh… ok. Um, what should have we used?

Customer: I don’t know! But not blue! You guys suck! I hate you!

Me: Well its just a preference. You can change it to whatever color you want.

Customer: But what about the newbies? huh? The blue is just going to confuse them and they’re not going to know how to change it!

Me: (sighs) OK. Write up a bug report and we’ll find something to change it to.

Customer: What?!?! I don’t have that sort of time!

Me: OK, OK. I’ll write it up.

Customer: Hey! The bug report doesn’t say exactly what I want it to!

Me: Well I had to write it up for you…

Customer: So? You guys don’t care about customers at all! I wouldn’t buy this with your money!

Me: …

Customer: I’m getting the software for free, right?

Me: (sighs) Yes.

Customer: Awesome!

Why would you ever want to do anything else?

[1] Its not my fault that doing “engineering things” sometimes looks like playing Quake.
[2] What? You think you’re going to get help? Didn’t you read the previous paragraph? “Most Mac users are more self reliant!”