It’s taken me a little longer than expected to get around to it – but if you load up Tube Notify on your iPhone now (no update to the application is required) you’ll see the DLR status has now appeared. Sorry it’s taken so long!
Category: Programming
The Palm Pré is a nifty little phone. However, the fact that you can view PDFs on it, albeit useful, is under scrutiny as it appears Palm failed to check the licenses of the software they were using.
When working on closed/embedded projects that you distribute, the GPL requires you to make you application open source as well – the LGPL requires you use the library in a DLL/.so form – it’s all fairly simple. License checking is time consuming but worthwhile as it means you end up with a product that you have at least tried to protect from potential lawsuits.
That also said, open source developers should consider dual licensing (a bit like Nokia with Qt). This way, if a large company wants to use your code, you make a reasonable amount of money out of it to fund the GPL/LGPL versions.
Doing it that way may well mean breaking Stallman’s view of the world – but there is his view… and the way the world actually works.
Lawsuit alleges Palm Pre violates copyright – Artifex, copyright, gpl, GPL violation – Techworld.
A little while ago now, I looked at how d3dx is actually quite slow and looked at why that was and that it’s better to use Direct3D directly to do such things. Generally when one wants to take a screenshot, it needs to beveryfast as to not interfere with the gameplay. This post breaks down what to do when presented with an anti-aliased display so that screenshot taking is nice and quick:
LPDIRECT3DSURFACE9 pd3dsBack = NULL;
if ( SUCCEEDED ( device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &pd3dsBack) ))
{
D3DSURFACE_DESC desc;
pd3dsBack->GetDesc(&desc);
LPDIRECT3DSURFACE9 pd3dsCopy = NULL;
if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
{
if (SUCCEEDED(device->CreateRenderTarget(desc.Width, desc.Height, desc.Format, D3DMULTISAMPLE_NONE, 0, FALSE, &pd3dsCopy, NULL)))
{
if (SUCCEEDED(device->StretchRect(pd3dsBack, NULL, pd3dsCopy, NULL, D3DTEXF_NONE)))
{
pd3dsBack->Release();
pd3dsBack = pd3dsCopy;
}
else
pd3dsCopy->Release();
}
}
}
The first thing you’ll notice is we grab the back buffer and query whether it has multi-sampling. If it is, we create a new render target and then render the scene onto it. This is far quicker to do than trying to take a shot of a multi-sampled scene as you’ve forced the GPU to do the rendering down onto a surface with no multi-sampling. Job done! That way you can then continue with LockRect() etc. and take the shot as you would do normally.
This makes taking shots of multi-sampled scenes not more than 1-2ms longer than those that aren’t. Enjoy!
Over the last 48 hours, I offered Tube Notify for free on the App Store. I did this for one reason and one reason only. One has to spend money to make money. In this case, I wanted to get feedback on the app. The problem with the App Store is you can’t offer time limited demos to potential customers. Instead they make their purchase decision based on other factors. The main one being… Feedback.
150 people took advantage of the fact the app was free – that’s pretty good considering the fact that it’s no longer on the front page of the Travel Section. Out of that, I got 4 feedback reports and I now have a listed average! That average being 3/5. Can’t really grumble with that – so if you got the app for free and gave feedback, thank you.
Now to get to work on the next update for it…
After 11 days of waiting, my first ever App hits iTunes. Regular readers of my posts (there are some, really) will have noticed what it was all about after seeing my Push on the iPhone post which covered doing Apple Push Notifications using Django on the server side. I did get around to pushing up a nice page on github to cover it and you can find that over at the django-iphone-push page.
The app is called “Tube Status Push Notifications” and it is… available in the App Store for the small price of 59p. It would have been free, but paying for the server infrastructure that runs it means that I have to recoup at least some of the costs. You can find out more about the app over on the Tube Notify page.
During the approval process I was a bit put off by the fact that Tube Deluxe managed to get push notifications out the door first. I wrote this app while push notifications were quite new. I also did it as a learning exercise writing an application I wanted. When Tube Deluxe 4.0 came out, I did give it a go. It has some things better, some things worse. Here, however, is the main thing – Tube Notify takes < 10 seconds to load up, even on GPRS (the little round dot at the top). I built the app for speed on loading, no one wants to wait to read information.
So, in short, a different application that serves a different purpose. I didn’t write it to make a lot of money, I wrote it to learn the iPhone SDK and for myself. I then gave it to others to test for a few weeks and then here we are! I hope others enjoy using it in the field as much as I do!
I have recently started doing development on the iPhone. It’s great fun. I am particularly interested in the Push aspect of this. I’m also a big fan of Python.
There’s a number of RESTful things that can be done with Ruby on Rails. However, the whole ‘packaged application’ thing is quite new to that framework. To Django, it’s the staple diet of how to get things done. So I’m currently part way through a generic application for Django for sending APN (Apple Push Notification) requests as well as dealing with the feedback connection.
Currently I have the push side working quite well. Just a warning, this is my first stab at this and it requires Python 2.6, or the relevant backports for ssl and json installed on 2.5.
from django.db import models
from django.conf import settings
from socket import socket
import datetime
import struct
import ssl
import binascii
import json
class iPhone(models.Model):
"""
Represents an iPhone used to push
udid - the iPhone Unique Push Identifier (64 chars of hex)
last_notified_at - when was a notification last sent to the phone
test_phone - is this a phone that should be included in test runs
notes - just a small notes field so that we can put in things like "Lee's iPhone"
failed_phone - Have we had feedback about this phone? If so, flag it.
"""
udid = models.CharField(blank=False, max_length=64)
last_notified_at = models.DateTimeField(blank=True, default=datetime.datetime.now)
test_phone = models.BooleanField(default=False)
notes = models.CharField(blank=True, max_length=100)
failed_phone = models.BooleanField(default=False)
class Admin:
list_display = ('',)
search_fields = ('',)
def send_message(self, alert, badge=0, sound="chime", sandbox=True,
custom_params={}, action_loc_key=None, loc_key=None,
loc_args=[], passed_socket=None):
"""
Send a message to an iPhone using the APN server, returns whether
it was successful or not.
alert - The message you want to send
badge - Numeric badge number you wish to show, 0 will clear it
sound - chime is shorter than default! Replace with None/"" for no sound
sandbox - Are you sending to the sandbox or the live server
custom_params - A dict of custom params you want to send
action_loc_key - As per APN docs
loc_key - As per APN docs
loc_args - As per APN docs, make sure you use a list
passed_socket - Rather than open/close a socket, use an already open one
This requires IPHONE_APN_PUSH_CERT in settings.py to be the full
path to the cert/pk .pem file.
"""
aps_payload = {}
alert_payload = alert
if action_loc_key or loc_key or loc_args:
alert_payload = {'body' : alert}
if action_loc_key:
alert_payload['action-loc-key'] = action_loc_key
if loc_key:
alert_payload['loc-key'] = loc_key
if loc_args:
alert_payload['loc-args'] = loc_args
aps_payload['alert'] = alert_payload
if badge:
aps_payload['badge'] = badge
if sound:
aps_payload['sound'] = sound
payload = custom_params
payload['aps'] = aps_payload
s_payload = json.dumps(payload, separators=(',',':'))
fmt = "!cH32sH%ds" % len(s_payload)
command = '\x00'
msg = struct.pack(fmt, command, 32, binascii.unhexlify(self.udid), len(s_payload), s_payload)
if passed_socket:
passed_socket.write(msg)
else:
host_name = 'gateway.sandbox.push.apple.com' if sandbox else 'gateway.push.apple.com'
s = socket()
c = ssl.wrap_socket(s,
ssl_version=ssl.PROTOCOL_SSLv3,
certfile=settings.IPHONE_APN_PUSH_CERT)
c.connect((host_name, 2195))
c.write(msg)
c.close()
return True
def __unicode__(self):
return u"iPhone %s" % self.udid
So how do we use it? Well rather easy. First you need to set up your certificates with Apple. You’ll need to set up a specific AppID and provisioning profile for your application (i.e. you can’t use *).
The guide on Apple’s site covers how to do this. Python needs it in a combined PEM format to work. Other sites claim you have to export from Keychain Access and that you’ll need to convert both to .pem. I only had to convert the private key:
openssl pkcs12 -in pkey.p12 -out pkey.pem -nodes -clcerts cat cert.pem pkey.pem > iphone_ck.pem
I then altered my settings.py to have the new entry I added:
IPHONE_APN_PUSH_CERT = os.path.join(PROJECT_ROOT, "iphone_ck.pem")
Note the full path. I always have this at the top of settings.py to make my like easier:
import os PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
Now, it’s just case of making your iPhone app register with push and getting a unique ID. You have to use your real phone to do this. If you’re in a hurry and just want to test it out you can cheat and just pop this in applicationDidFinishLaunching delegate:
// Register for push notifications [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
Then you probably want this to get the ID out into the console:
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
// Registration was successful so we'll
// set up our device token etc.
deviceToken = devToken;
NSLog(@"devToken=%@",deviceToken);
self.registered = YES;
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
// This is expected on the emulator so that's fine
NSLog(@"Error in registration. Error: %@", err);
}
Then you’ll be good to go! Run the app on the phone and the console will output the ID. Created an iPhone object and send a message. Job done.

Visual Studio post version 6 has an interesting time with its runtime library. For those that remember back that far, Visual C++ 6 used a library called msvcrt.dll / msvcrtd.dll. These libraries are included, to this day, in all versions of Windows and remain perfect compatible with Visual C++ 6 compiled binaries.
A lot of other analysis has gone into this on other blogs. I won’t repeat all of it here, but be sure to check out the blog of the KovoIRC developer.
Let’s start out with a simple program. It’s quite basic, but it’s a good starting point. I have made a small Win32 Console Project in Visual Studio making sure that I have made a directory made for the solution and the project directory sits within it.
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
char* x = new char[100];
memcpy(x, "Hello World", 11);
puts(x);
delete [] x;
return 0;
}
Your first question is probably; Why are you creating a buffer using new then using memcpy and delete? The answer is that I want to make sure I use the new/delete operators. sprintf isn’t available, so I can’t use that.

Visual Studio Runtime Picker
Let’s statically link the runtime with it and produce a release executable. That produces a 53KB EXE. Ok, that’s not fair, there’s features in other runtimes we can’t use. So let’s turn off the security checking etc. Still 53KB.

Normal Static Linked Binary Size
Now we have our base point. Let’s do the following:
- Convert the solution directory to a GIT repo
- Add the git submodule from git://github.com/leepa/libctiny.git
- Add the new sub project to the solution in Visual Studio
- Add a Project Dependency to the HelloBlogPost project so it depends on MiniCrt
Git makes managing these things insanely easy. By using a git submodule you get to ensure you stay at the point you want to, but update it easily if needs be. Basically, it’s a great way of including 3rd party libraries in a project.
To do all this we can do the following (assuming msysGIT in Windows).
git.exe init # You probably now want to do your # adds/commits and .gitignore # stuff git.exe submodule add -- "git://github.com/leepa/libctiny.git" "libctiny"
Ace, now let’s try and compile again… 4KB. That’s a bit better isn’t it? The same program but a much small runtime footprint.

EXE Size with libctiny
Isn’t that much better? For when you just need to do simple executable files, this can’t be beat. I can’t take credit for the library, I just added it a public Git repository and fixed a couple of new intrinsic functions that Visual Studio 2008 defines (ohama @ Google updated it to VS2005).
Credits: Under the Hood: Reduce EXE and DLL Size with LIBCTINY.LIB & omaha – Software installer and auto-updater for Windows.
A work colleague coined a fantastic term for code that just isn’t DRY:
Constantly Repeating Yourself – CRY
Not only does it describe one’s reaction to amazingly shoddy code, that just oozes of a developer spending all day using Copy and Paste, but seems an appropriate opposite.
Back to refactoring CRY code with me!
This sort of thing drives me barmy – why can’t people set a standard on tabs?
The above snippet is taken from eJabberd (a Jabber Server written in Erlang). Notice how some lines are soft tabs, others are hard. The causes havoc when dealing with merging in source control (I don’t care if you use a new fangled Git approach). Whitespace (both tabs and line endings) must be consistent on programming projects. Much pain ensues if they are not!
Personally I set rules in SVN commit hooks. This means that if a developer has anything ‘bad’ like Windows Line Endings on a Python project, then it is dealt with accordingly.
Saves a lot of merge pain!




