How to test url in onNavigationRequested in QML

March 1st, 2013 No comments

I’ve fought this battle today with qml, I wanted to do something specific if the url contained a specific word. To do this the code below works, the key is the toString()

	          console.log("NavigationRequested: " + request.url + " navigationType=" + request.navigationType + " action="+request.action)
     		      progressIndicator.visible  = true

How to capture the entire screen in Cascades Blackberry

March 1st, 2013 1 comment

I had some issues recently in attempting to capture the entire screen to a jpeg image. I could capture our OpenGL view pretty easily but not the Cascades UI on-top. The main issue was getting a handle to the screen window at the top level. Below is the code I used to achieve the full-screen capture. Not that this is being performed within an custom OpenGL view and so I use the screen context for that to initialise the pixmap, this may not be the case for you. The image is essentially captured as a bmp, the bmp data is stored in a QByteArray and we use the handy QImage save feature to output as a jpeg. There may be some ways to improve this code but it does the trick for me.

screen_pixmap_t screen_pix;
screen_buffer_t screenshot_buf;
char *screenshot_ptr = NULL;
int screenshot_stride = 0;

int usage, format;
int size[2];

screen_create_pixmap(&screen_pix, m_screen_cxt);

screen_set_pixmap_property_iv(screen_pix, SCREEN_PROPERTY_USAGE, &usage);

format = SCREEN_FORMAT_RGBA8888;
screen_set_pixmap_property_iv(screen_pix, SCREEN_PROPERTY_FORMAT, &format);

size[0] = width;
size[1] = height;
screen_set_pixmap_property_iv(screen_pix, SCREEN_PROPERTY_BUFFER_SIZE, size);

screen_get_pixmap_property_pv(screen_pix, SCREEN_PROPERTY_RENDER_BUFFERS,
screen_get_buffer_property_pv(screenshot_buf, SCREEN_PROPERTY_POINTER,
screen_get_buffer_property_iv(screenshot_buf, SCREEN_PROPERTY_STRIDE,

screen_read_window(GLOBALS_PTR()->app->app()->mainWindow()->handle(), screenshot_buf, 0, NULL ,0);

QByteArray array;

int nbytes = size[0] * size[1] * 4;
write_bitmap_header(nbytes, array, size);

for (int i = 0; i < size[1]; i++)
	array.append(screenshot_ptr + i * screenshot_stride, size[0] * 4);

QImage image = QImage::fromData(array, "BMP");
QFile outFile("shared/photos/temp1.jpeg");;, "JPEG");


You'll also want the write_bitmap_header, here it is:

void write_bitmap_header(int nbytes, QByteArray& ba, const int size[])
	char header[54];

	/* Set standard bitmap header */
	header[0] = 'B';
	header[1] = 'M';
	header[2] = nbytes & 0xff;
	header[3] = (nbytes >> 8) & 0xff;
	header[4] = (nbytes >> 16) & 0xff;
	header[5] = (nbytes >> 24) & 0xff;
	header[6] = 0;
	header[7] = 0;
	header[8] = 0;
	header[9] = 0;
	header[10] = 54;
	header[11] = 0;
	header[12] = 0;
	header[13] = 0;
	header[14] = 40;
	header[15] = 0;
	header[16] = 0;
	header[17] = 0;
	header[18] = size[0] & 0xff;
	header[19] = (size[0] >> 8) & 0xff;
	header[20] = (size[0] >> 16) & 0xff;
	header[21] = (size[0] >> 24) & 0xff;
	header[22] = -size[1] & 0xff;
	header[23] = (-size[1] >> 8) & 0xff;
	header[24] = (-size[1] >> 16) & 0xff;
	header[25] = (-size[1] >> 24) & 0xff;
	header[26] = 1;
	header[27] = 0;
	header[28] = 32;
	header[29] = 0;
	header[30] = 0;
	header[31] = 0;
	header[32] = 0;
	header[33] = 0;
	header[34] = 0; /* image size*/
	header[35] = 0;
	header[36] = 0;
	header[37] = 0;
	header[38] = 0x9;
	header[39] = 0x88;
	header[40] = 0;
	header[41] = 0;
	header[42] = 0x9l;
	header[43] = 0x88;
	header[44] = 0;
	header[45] = 0;
	header[46] = 0;
	header[47] = 0;
	header[48] = 0;
	header[49] = 0;
	header[50] = 0;
	header[51] = 0;
	header[52] = 0;
	header[53] = 0;

	ba.append(header, sizeof(header));
Categories: C/C++, Cascades - Blackberry Tags:

NSURLConnection thread callbacks

February 7th, 2013 No comments

I was doing some fishing to see what threads the callbacks for started connections & finished transfers occur and turns out that these callbacks get called on the thread that they were called from.

Didn’t really see this in the documentation so felt I should share this.

Categories: C/C++ Tags:

Personal C++ Style Guide

January 24th, 2013 No comments

I’ve been thinking recently that I should write a style guide for personal C++ code. I’ve found the need to do this as I’ve found myself doing different things at different times and contradicting myself in several places. Also the lack of a clearly defined style guide at companies I think has harmed the code style in general. Note however that if I am working in existing source there is no choice but to assume the same style as the existing code. Imposing a new style of code in existing code makes the code unreadable and is bad practice.
I’ve read a lot of these at other locations and taken the ones I agree with. Please note that this is an evolving document that will be added to as I find more cases that I feel need style definitions.


  • The #define pre-processor directive shall not be used to define constant values. Instead, the
    const qualifier shall be applied to variable declarations to specify constant values.
  • When using #include, use
    for platform or other project includes and “header.h” for same project includes


  • When using #include, use
    for platform or other project includes and “header.h” for same project includes
  • Declarations of classes that are only accessed via pointers (*) or references (&) should be
    supplied by forward headers that contain only forward declarations.

General Code Rules

  • Source lines will be kept to a length of 120 characters or less.
  • Each expression-statement will be on a separate line. The following is not allowed:
    if(var == a) val = 0;
  • Braces (“{}”) which enclose a block will have nothing else on the line except comments (if
  • Braces (“{}”) which enclose a block will be placed in the same column, on separate lines
    directly before and after the block.
  • C++ style casts (const_cast, reinterpret_cast, and static_cast) shall be used instead of the
    traditional C-style casts.
  • All switch statements that do not intend to test for every enumeration value shall contain a
    final default clause.
  • The increment expression in a for loop will perform no action other than to change a single
    loop parameter to the next value for the loop.
  • Floating point variables shall not be tested for exact equality or inequality. We should use epsilon and use subtraction for tests on comparison to equality
  • C++ exceptions shall not be used (i.e. throw, catch and try shall not be used.)
  • Operators should have a space at either side of them e.g.
    // wrong

Filename Naming Conventions

  • All filenames will start with an uppercase letter and each consequent word have an uppercase letter e.g. PackageManager.h
  • Prefixes should not be used, they cause the reuse of a class difficult at a later point e.g. MTPackageManager.

Class Naming Conventions

  • All classes will start with an uppercase letter and each consequent word have an uppercase letter e.g. SteamCommunicator

Variable Naming Conventions

  • Local variables should always start with a lowercase letter
  • Always attempt to use the smallest datatype possible, i.e use a short instead of an integer where possible.
  • Member variables should have a 'm' prefix, e.g. m_count
  • Const members (or anonymous members) should have a 'k' prefix
  • Static variables should have a 's' prefix. Static members should have a 'm' prefix.
  • Global variables should have a 'g' prefix.
  • All variables should be initialised if possible in the constructor
  • Only 1 variable should be defined per line e.g
    // incorrect
    int32 first button_on_top_of_the_left_box, i;


  • Pointers should have the 'p' prefix.
  • The dereference operator ‘*’ and the address-of operator ‘&’ will be directly connected with the type-specifier.
    // incorrect
    int *pInt;
    // Correct
    int* pInt; 
    // incorrect 
    int &rInt; 
    // correct
    int& rInt;

Function Naming Conventions

  • Functions should start with a lowercase letter and each consequent word have an uppercase letter e.g. listProducts
  • Underscores shall NOT be used
  • Any immutable/read-only parameters shall use a const reference. The exception to this rule is PODs which can be const but need not be a reference.
  • Any reference non-const parameters shall use the 'r' prefix. This is to indicate to the method's code that the parameter is effectively an 'out' or return value
  • Any parameter that is a pointer or smart point should use the 'p' prefix.
  • A member function that does not affect the state of an object (its instance variables) will be declared const.
  • A class will have friends only when a function or object requires access to the private elements of the class, but is unable to be a member of the class for logical or efficiency reasons.
  • Functions shall always be declared at file scope or in anonymous namespaces.
  • Functions with more than 7 arguments will not be used.
  • Functions will have a single exit point, functions should not return early or have multiple exit points. This can be confusing when debugging and difficult to understand
  • If a function returns a status code or boolean, this status or boolean MUST be tested and at least logged if something unexpected happens
  • Only functions with 1 or 2 statements should be considered candidates for inline functions.
  • Trivial accessor and mutator functions should be inlined and the number of accessor and mutator functions should be minimized.

Constructors & Destructor

  • Unnecessary default constructors shall not be defined.
  • Initialization of nonstatic class members will be performed through the member initialization
    list rather than through assignment in the body of a constructor.
  • Members of the initialization list shall be listed in the order in which they are declared in the
    class. If possible the compiler will issue warnings for variables that are defined out of order.
  • A copy constructor and an assignment operator shall be declared for classes that contain
    pointers to data items or nontrivial destructors.
  • A copy constructor shall copy all data members and bases that affect the class invariant (a
    data element representing a cache, for example, would not need to be copied).
  • All base classes with a virtual function shall define a virtual destructor.
  • Avoid doing complex initialization in constructors (in particular, initialization that can fail or that requires virtual method calls).
  • Use the C++ keyword explicit for constructors with one argument.
  • ou must define a default constructor if your class defines member variables and has no other constructors. Otherwise the compiler will do it for you, badly.

Operators & Overloads

  • The default copy and assignment operators will be used for classes when those operators
    offer reasonable semantics.
  • An assignment operator shall return a reference to *this.
  • An assignment operator shall assign all data members and bases that affect the class invariant
    (a data element representing a cache, for example, would not need to be copied).
  • When two operators are opposites (such as == and !=), both will be defined and one will be
    defined in terms of the other.


  • Typedefs should start with an uppercase letter and each consequent word should have an uppercase letter. The name of the type should indicate the type of the object eg. ProductMap.
  • Container typedefs should always have an interator and const iterators defined:
    typedef ProductMap::iterator ProductMapIter
    typedef ProductMap::const_iterator ProductMapConstIter


  • Enums should always start with an uppercase and each subsequent word start with an uppercase letter e.g. DisplayTypes
  • Enum types should have an 'e' prefix and use the definition name at the start of the name e.g.
    enum DisplayType
  • The enum should always contain a 'max' enum type, this is to facilitate iterating through the enums.

If statements

  • When mutable variables are compared with a constant (or NULL) the style should always be as follows:
    if(12 == x)
  • If statements should always have curly brackets and should start on the line after the if statement.

For statements

  • For statements should always have curly brackets and they should start on the line after the for statement


  • Standard ('//') comments and comment blocks ('/** ... **/') are the only comments allowed
  • Code commented should should be deleted, this is the whole point of source control.
  • PRE and POST conditions of functions should be commented in the header for a class
    // PRE(pPointer)
    // POST(pPointer)
    void doSomething(pPointer);

I've attached my personal C++ and Objective C template classes which are effectively skeletons and to be used as a starting point for new classes.


Categories: C/C++ Tags: , ,

Eclipse Juno crashing on exporting signed APK on OSX Lion

January 16th, 2013 No comments

This has been causing me lots of grief for a while now. Turns out that disabling the ‘Build Automatically’ option in Eclipse appears to stop the crashes. Hope this helps someone somewhere sometime.

C++ #defines in Objective C

January 4th, 2013 1 comment

If you have C++ defines in XCode, note that they won’t work in .m files. You need to change the file to be a .mm file instead so that it is compiled using the C++ preprocessor.

Alternatively you can just make a C define instead, XCode provides this functionality although I didn’t want to do this due to a project configuration.

Show hidden files on OSX

December 12th, 2012 No comments

It is pretty simple to show all files on OSX, just launch terminal paste in the following:

defaults write AppleShowAllFiles YES

Relaunch finder and you’re good to go :)

Finding the location of Unity3d Log Files (IE9)

October 3rd, 2012 No comments

So today I had an issue that Internet Explorer 9 wasn’t putting Unity logfiles in the temp directory as documented here

Unsure as to where Unity had been putting the logfiles I turned to my trusty friend

Within ProcessExplorer there is a great little feature in the ‘Find’ menu called Find Handle or DLL. Here you can see what process has a DLL or file handle open. Knowing that all Unity logs start with the prefix ‘log_’ I tried this prefix and found that it was putting the logfiles in C:\Users\Eddie\AppData\Local\Temp\Low\UnityWebPlayer\log! The Low folder is used for Low integrity processes, read more about this here, I didn’t realise I had not allowed UnityWebPlayer to run at an elevated level.

Thanks Process Explorer!

Categories: Unity3d Tags:

Setting up SourceGear DiffMerge with Perforce

September 12th, 2012 6 comments

After spending some time Googling around looking (and failing) to find the correct way to setup SourceGear DiffMerge with Perforce I decided to post up the arguments required to get it working – more for my own reference or crappy Google skills than for any other reason

Go to Edit->Preferences and select the Diff tab
Click Other Application
Browse to the DiffMerge.exe
In Arguments enter:

%1 %2

Now click on the Merge tab
Again setup the Other Application and navigate to DiffMerge.exe
In Arguments enter:

/r %r %1 %b %2

Sit back and enjoy the diff-ness

Categories: Perforce Tags:

Unity Asset Server and SourceGear DiffMerge (64 bit machine)

September 12th, 2012 3 comments

If you, like me, like to use SourceGear’s DiffMerge in Unity on a 64 bit machine you might be in trouble. DiffMerge’s installer now will only let you install the 64 bit version of DiffMerge. Unity is a 32 bit program and doesn’t pick up on diff tools that are 64 bit. So what you need to do is go back to the archives at SourceGear and get an installer for an older version that allows you to install a 32 bit version on a 64 bit machine. Bit of a hack but after doing it you should now see DiffMerge as one of the accepted diff tools in Edit->Preferences->External Tools

The link to the version of DiffMerge I use is 3.3.0, located here:

Categories: Unity3d Tags: