Monthly Archives: May 2016

Old C++ Code

I was inspired by a recent post by Brent Simmons to write about some ancient C++ code I wrote back in the min-90’s. At that time I was just learning to develop Windows applications in C and C++ was just starting to get some traction, not unlike Swift in the Mac and iOS community today.

When you created a Windows application you’d have to write a WindowProc (Windows Procedure) that would process all messages for a particular window. A Window Proc would receive a message and other parameters. You’d switch on the message and the WPARAM and LPARAM parameters would contain other information specific to a particular message. It was well documented, but it was quite ugly.

Believe it or not this is how a lot of your favorite Windows applications were originally written. Giant switch statements that dissected two other values to determine how to respond to different types of messages.

When I started learning C++ I was trying to find a way to create a Window Proc that didn’t have to implement every Windows message in the system, there must be thousands of them. Could you imagine a base C++ class that responded to every Windows API message in the system? I can’t. It would be a real mess to deal with.

It took me quite a while to come up with a way to do it. I happened upon the answer in the C++ FAQ. This would allow me to create a base class that provided one Window Proc that would look in a dispatch table (a map) to see if the Windows Message was handled by the Window in question.

Here’s what it looked like. I hope this gets the point across.

When you implemented a Window Proc class you would derive from a base class and provide it with a message dispatch table (at the top of the above gist.) The dispatch table would direct the base classes Window Proc to call the correct handler for a specific message.

Notice that this Window Proc is a straight C function. That’s how the Windows API operates. It’s a C based API, but it provides a mechanism to attach user provided data to a Window Procedure. That’s how this operates. When the Windows WM_CREATE message is sent it includes a this pointer to derived Window Class, which can be looked up later based on the HWND identifier.

In the gist above the code that calls pWindow->DispatchMessage knows how to look at the dispatch table and direct the message to the appropriate pointer to a method.

The code that knows how to send a message to a particular method is (this->*pHandler)(wParam, lParam). This is dereferencing a pointer to a method. All methods have the same signature, which is consistent with how the Windows API works.

Will Code C++ for Food
By the way. This code still builds and runs with up to date versions of Visual Studio on Windows 10. A lot of this code was written in 1995 and has been tweaked over time to keep it up to date with changes to the Windows API.

As I’ve said before, old code never dies.

If you’re feeling really brave you’re welcome to grab my crusty old C++ library and build the simple sample application on your Windows box. Please note, the code is provided as is without warranty, expressed or implied.

On Cycling

Slack Blog: “The key to embarking on the journey to an urban cycling life starts with promptly ignoring Captain Spandex and his merry band of avid cyclists. Instead, seek out like-minded individuals who just get on with it and ask for advice about routes. Get a bike — any old bike — and start practicing. On weekends, in the evenings, to get your head around it. Astronauts need fancy gear and training. The rest of us just need practice and encouragement.”

The piece linked above gives great advice to anyone commuting to work on a bicycle, but I love the paragraph I pasted above. There is one thing about Americans that always feels funny when it comes to cycling. I see folks out on their really expensive bikes outfitted with spandex and all the fancy gear for their commute and I wonder why? European bike culture is so far ahead of ours and most commuters wear their work clothes when they ride. Oh, they also tend to ride very simple bicycles. Why is that?

Would you wear a full fireproof suit, helmet, five point harness to drive to work? Not likely. Why get all duded up for a bike commute?

A Swift Only Future? – Core Libraries: “The version of Foundation makes use of many of the same underlying libraries (e.g. ICU and CoreFoundation) as Apple’s implementation, but has been built to be completely independent of the Objective-C runtime. Because of this, it is a substantial reimplementation of the same API, using pure Swift code layered on top of these common underlying libraries.”

This is the beginning of the radical departure from Cocoa I’m hoping for. A day when all Framework code for building Mac and iOS applications is void of Objective-C, or mostly void of it. Not that Objective-C and Cocoa are bad, they’re not. It’s just time to move on.

Yes, it will take years and years and I doubt Apple will pull the rug out from under us any time soon. Look how long they supported Carbon, about 12-years. Think of Objective-C and Cocoa on that same, or similar, path.

Eat your own dog food.Come to think of it, Apple will probably support Cocoa and Objective-C much in the way Microsoft continues to support the Windows API. It’s still around, you can still write apps using it — the Office Apps still use it as well as most awesome legacy apps (like Photoshop) — but Microsoft is now pushing their Universal App platform. A platform that is based on .Net. I see Swift, the new runtime, and whatever new Swift only Frameworks as Apple’s .Net effort.

Will there be pain? Absolutely. Is it possible to overcome it? Of course it is. It will take a lot of work and planning for companies to move to Swift and Framework X, but it will take years for Apple to get there, so we all have quite a bit of time.

I know our little group of developers at Agrian is writing 100% Swift today. We have a substantial investment in Objective-C and Cocoa (obviously), but all new code is written in Swift and we’re slowly refactoring areas of code that are easy to isolate into Swift. It’s been a fairly smooth transition.

From planning to chaos (“We’re screwed”) to literal tears of joy, Photoshop team members talk about the single toughest cycle in the app’s long history. – John Nack on Adobe, March, 2011

Large companies like Adobe and Microsoft have a considerable investment in C and C++ over the years to make their applications portable. Their move to Swift may — in many ways — be easier than the transition to Swift and Framework X for small shops that are 100% reliant on direct access to Cocoa.

With a common C++ core, a thin native UX layer and evolving PALs, Microsoft is building its Office apps so they work on different OSes with fairly little tweaking required. Zaika cited PowerPoint as an example, noting that only four percent of its tens of millions of lines are unique to the WinRT/Universal version of Office (the touch-first Office release some of us have been calling “Gemini” ). If the XAML code is excluded, the amount of shared code is 98.6 percent he said. The PowerPoint for Android code base includes 95 percent shared code, Zaika said. – All about Microsoft, October, 2014

Microsoft has a complete architecture of shared code in Office that is C++ and abstracts the app from the OS layer, I’d imagine Adobe’s layer is very similar. I’ve called this the “Grilled Cheese” approach.

Here’s hoping we see a fresh approach to building on iOS, tvOS, and OS X.

P.S. – If you ever feel down about a development cycle visit John Nack’s old Adobe blog and watch the video about the transition from Carbon to Cocoa. It’s very inspirational. These folks really went through the ringer to ship a Cocoa using Photoshop. Smart, smart, people.

Apple Minus the iPhone

Colorful Apple LogoiMore – Jason Snell: “Or to put it another way, Apple’s non-iPhone business is generating about $80 billion in revenue every year — roughly the same as Microsoft. The Mac generates $24 billion per year, and the iPad (at the moment) generates around $20 billion per year. Put together, that’s roughly the revenue of Hewlett-Packard. Starbucks just reported its most recent quarterly revenue total: $5 billion, or roughly what the Mac generated in revenue last quarter. Facebook’s blow-out quarter? $5.4 billion in revenue.”

All the talk about Apple’s demise is definitely overblown. All the stock market cares about are companies that are growing. Those types of investors don’t care about the long term outlook of the company. All they’re interested in is how much they can make over the next few months. They’re gamblers, just like someone sitting at a Vegas craps table, rolling the dice. You win some, you lose some.

Back in 2000 when Visio was purchased by Microsoft we were at the end of Microsoft’s stock market rise. It pretty much stagnated for 15-years, sure it was up and down from that point, but it was inevitable they would see a dip, then become a steady “old” company. Not always on the rise, but also not on the cusp of disappearing. It may have taken a number of years but Microsoft stock has risen to 2000 levels. Will it stay there, fall, or continue to rise? Meh, who knows. Wall Street certainly doesn’t have a clue (remember, it’s a crap shoot.)

Here’s a pretty picture that points out Microsoft’s stock struggles for the last 15-years.

Microsoft Stock last 15-yearsIn the end Apple will be fine. They’re not going to disappear. They’ve never been a Wall Street darling. They’ll continue to be Apple. That means they will continue to perplex investors — who will continue to call for Tim Cooks head — which in itself is a complete mystery for a company that makes gobs of money for its investors.

Maybe Apple is about to settle into being a steady company?

Apple is not the Gatekeeper of Podcasting

New York Times: “Late last month, Apple brought seven leading podcast professionals to the company’s campus in Cupertino, Calif., to air their case to a room full of employees, according to two people who were there. The people would speak only on the condition of anonymity because they had signed nondisclosure agreements. The company made no promises, the people said, but several pressing issues for podcasters were discussed in frank terms.”

Here’s the thing, Apple did not invent Podcasting — Dave Winer and Adam Curry did — Apple just has a really good Podcast directory. Anyone could do the same thing. In fact you don’t need to be part of Apple’s Podcast Directory to have a Podcast. All you need is a place to put your audio files so people can download them. Yes, that’s it.

One other note. I know there are services to help with podcasting, and I’m not going to advise against using one, especially if you are not a technical person. Just understand you don’t need a large corporation like Apple to publish your podcast on the internet.

Also note. I’m not bagging on Apple. They can’t help it if they created something really useful and people believe they own the gateway to podcasting. It just kind of happened that way.

Dream a Little Dream

A wonderful bouquet of flowers.Bitwise Industries: “In downtown alone, hundreds of lofts will become thousands. Eateries will line the streets. Nightlife will spring up. And on Sunday mornings, the thirty-somethings will sit outside sipping coffee from that shop on Fulton Street and tell tales of how people used to scoff at the notion of downtown revitalization.”

I love this vision. I’ve been dreaming of a vibrant Fresno downtown since we moved back from Seattle in 1997. I loved stepping outside the office and having everything within walking distance. Restaurants and shops, so many to choose from. Scores of people going about their business. It was a beautiful experience and I miss it.

If you’re looking to relocate or build you should consider downtown Fresno. There are amazing old buildings everywhere waiting for someone to show them a little TLC and make them wonderful again. If you need someone to show you, drop me a line, I’ll show you some of my favorites.

Oh, Bitwise has also gone a long way toward fixing this issue. The valley is now very different because of their efforts.

Thank you Irma and Jake — and everyone else involved behind the scenes – you’ve given me hope for a revitalized downtown.

P.S. – Can you guys try to convince Alamo Drafthouse to add a location downtown? I know the perfect space for it! Thanks!

Dumpster Bike: Day 1

When I was a kid my brothers and I were into BMX racing. We were good, big fish in a little pond you might say. We rode for Dick’s Bicycles here in Exeter, California. We were co-sponsored by Vans so our team racing shoes were supplied to us. We just picked a style, back then it was high or low tops. They were blue and gold like the rest of our team uniform. Jerry, my middle brother, was co-sponsored by SE Racing so he had the privilege of riding new gear before SE released it. 

Anywho, long story short, we know bicycles and spent many a day tearing them down, re-greasing, and reassembling them. Good time.

Move ahead almost 40 years and I still admire nice bikes. Especially older steel frames with beautiful lugs. I’ve wanted to rebuild and old steel lug frame into a fixed gear cruiser for a few years know, but I’ve never been able to happen upon a nice steel lug frame (I have one that might be worth something, so I’m afraid to strip it down to bare metal.) 

Recently we had a City cleanup day. Basically you can take anything you want to our local City Yard, pay a $5US fee, and the city properly disposes of it. This year I needed to cut down a dead tree so the cleanup day way the perfect opportunity to dispose of it. 

While I was there I thought to look for some bike frames and parts. I happened across a 2007 Giant Boulder SE. Frame, forks, and cranks just laying on the ground. I adopted it. 

Giant Boulder SE

I spent day one stripping off the chain, derailers, and handlebars. I was unfamiliar with how the forks and headset were assembled so they’re still in the frame and the bottom bracket is pretty rusty so I need a crank puller to remove the cranks. A guy at our local bike shop taught me how to take apart the headset so I can get that taken care of next. 

Once I get this thing stripped down I plan on stripping all the paint and giving it a nice fresh coat of Burnt Orange paint. Then I’ll reassemble it into a nice casual to ride bike. Nothin fancy.

I plan on keeping a little journal as I fix up what I’m calling “Fahrni Dumpster Bike Mark 0.”