Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

.NET

Photon and QNX


MAY95: Photon and QNX

Photon and QNX

Visual Basic-like development for a real-time operating system

Peter D. Varhol

Peter is chair of the graduate department of computer science and mathematics at Rivier College in New Hampshire. He can be reached at [email protected].


Photon is a windowing system specifically designed for the QNX operating system. Photon is particularly unique because it is a GUI built around a graphical microkernel. In fact, the Photon microkernel is primarily a resource manager, creating a graphical event space and managing regions within the event space and events as they occur between and within regions. QNX refers to graphical events as "photons." The Photon microkernel is about 20 Kbytes of code, plus an additional 40 Kbytes of data. Other key parts include shared libraries, VGA (or other) graphics drivers, and a pen-input/touch-screen driver. The total amount of memory needed for code and data is about 250 Kbytes.

Interestingly, Photon was designed on the same principles as the QNX microkernel itself. QNX, originally developed for process-control applications, is a POSIX-compliant, 32-bit real-time operating system (that can also be installed as a 16-bit kernel for processors below the 386). Even though QNX has a UNIX look and feel, it is definitely not UNIX--it uses no USL source code and requires no UNIX license.

At the heart of QNX is an extremely small (about 10 Kbytes) microkernel that supports four main functions: interprocess communication, network communication, process scheduling, and interrupt dispatch. In contrast to larger operating-system kernels, the QNX microkernel has only 14 system calls. QNX is a message-passing operating system, utilizing blocking versions of Send, Receive, and Reply function calls for message management. Messages don't queue; rather, the message facility is a process-to-process copy, which QNX claims provides performance comparable to traditional function calls.

Why would you want to do windowing programming on QNX? Primarily to develop applications for resource-constrained environments--embedded systems, hand-held computers, and the like. How do you do windowing programming on QNX? With Photon and its development environment. Of course, Photon doesn't assume that the resulting software is going to be a windowing system, and it doesn't include the window manager within the base code. Since most embedded systems don't include multiple windows, the memory and storage requirements can be kept as low as possible. The Photon Motif-like window manager adds 30 Kbytes of code and 64 Kbytes of data.

QNX also comes with an implementation of the X Window System that utilizes the QNX message-passing scheme for communication between QNX kernels, and TCP/IP for communication with other X servers. It is a full X11R5 implementation, with the Motif window manager, scalable fonts, and font server. As a subset of the X Window System, Photon provides a rich widget library that operates much like the X widget set. Photon also includes a code-generating, visual application-development environment called "Application Builder."

What is the purpose of a graphical windowing system on a real-time operating system like QNX? The answer is in process-control systems that require human intervention. A manufacturing process, for example, can be monitored and controlled by a touch screen at a supervisor's station. The X Window System would form the basis for the touch-screen user interface. For this purpose, QNX includes touch-screen drivers both for its X server implementation and for Photon.

Running Photon and Application Builder

QNX runs all of its device drivers as user processes, which makes it possible to dynamically load drivers when you need them. This is what I did when loading both X and Photon for the CD-ROM (for X only) and the mouse. The drivers for these devices were not in my default installation, so I simply started them from the command line before installing the windowing systems. The drivers communicate with their devices through the microkernel, the only part of the operating system that runs in kernel space. You might think that running device drivers in user space would cause unacceptable performance penalties, but that doesn't seem to be the case.

For UNIX-like software, Photon is remarkably easy to install. I simply installed the requisite drivers, called the install program on the floppy or CD-ROM, and launched. Since QNX runs everything as a process, I only had to rebuild the kernel when I upgraded the base operating system before starting.

A good example of this ease was when I first used Photon and the Application Builder would not launch. A cursory glance at the Photon documentation revealed that I had to run it in Super-VGA mode. All I had to do was return to the QNX command line, run the Super-VGA driver (my hardware supported up to 1024x768), and return to Photon. This is hardly the exercise in masochism it would have been under UNIX.

Photon itself is simply a graphical screen with a small Photon icon in the upper-left corner. In all other respects, it looks like X, with pop-up menus and a terminal window. You navigate through it much the same as you would in X, using the right mouse button to bring up the menu and the left button to make selections.

In many ways, the Application Builder development tool is similar to Visual Basic. For controls ("objects" in Visual Basic, "widgets" in this environment), it includes push buttons, bitmaps, toggle buttons, labels, text boxes, on/off buttons, windows, scroll bars, and (interestingly) signature boxes. There are also several controls that let you easily navigate the environment and move controls around on the screen. Figure 1 shows a sample Application Builder workspace.

The Application Builder works with Watcom's 32-bit C compiler for QNX, which has long been available for text-based development in the operating system. The compiler is still the same, since Application Builder generates a set of C source files, which are then compiled through the Watcom compiler.

Building a Graphical Application

The project I developed with Photon was an online time clock, on which users would use a mouse or touch screen to log a particular type of activity and the time it takes to perform that activity. Workers who have to charge the efforts to different contracts would use such an application. This app seemed straightforward to implement and was a typical application for a handheld device. I had done a similar application in Visual Basic and was interested in comparing the two development environments.

Being used to Visual Basic, I wanted to use a drop box to choose a project, but there was not one to be had in Application Builder. Instead, the Programmer's Guide suggested using a text box mapped to a pair of up and down arrows. When the user clicks on the arrows, the text box can be made to scroll through a list of text items. The arrows are not an existing widget; rather, you use a bitmap widget, make the bitmap selectable, and draw an arrow on it using the drawing tool.

I did the same thing for choosing the amount of time spent on each project, including in the text list times at 15-minute increments. The user would simply scroll down the list until the correct time period appeared in the window. Once the project name and the number of hours had been selected, the user would click on a Record button. The Record button has an associated C function which pairs the two values together and writes them to an ASCII file.

Last, I put in a Help button and Help dialog. First, I built the Help dialog box. Dialog widgets are available in Photon by selecting a Dialogs item from the Application menu. Once in the dialog window, I used a pane from the widget-control tool box as a container for the text and placed a Close button immediately beneath the pane.

Back at the main development screen, I used the Callbacks dialog box associated with the Help button to set the link type to Dialog, then entered the name of the ASCII file containing my Help text. This will call that file and display it in the pane in the Help dialog when the user clicks on the Help button. This means I can change my Help text without having to change and recompile the entire application. You could make the Help context specific by associating a function with the Help button, but I left my Help generic.

Compiling the Application

Application Builder uses C, compiled by the Watcom compiler, to write event-handling code. While many of the events were the same between my Visual Basic implementation and the Application Builder version, it was rarely possible to do a one-to-one translation between the two.

Application Builder also uses the concept of the callback function, rather than the event handler, in invoking event code. Example 1(a) is sample code associated with quitting a Visual Basic application. An analogous quit_process callback function for an Application Builder application might look like Example 1(b).

The process is the same. Most Windows programmers are familiar with the callback function. For example, when writing DLLs, the DLL must have a WEP (Windows Exit Procedure), which is, in effect, a callback. However, most Windows development languages generate default callbacks, which work unless you want do something other than simply returning to the calling routine.

I also had to develop functions that manipulated the text lists based on the user clicking on the arrows, and provide a function with the Record button to take the data from the text boxes and write them to a file. The event-handling (that is, callback) code for the arrow buttons is shown in Example 2.

A completed Application Builder application consists of a set of object files, a set of callback functions, and a number of associated source and header files necessary to make the application behave properly. Many of the necessary files are generated by Application Builder for the compile process. These include a listing of the sources, headers, and object files (similar to a MAK file in Visual Basic), a make file, function prototypes, and widget descriptions and links to the appropriate widget libraries.

The compiled application is stand alone; you don't need any other files on the QNX system to run the application. Unlike Visual Basic, a bulky, run-time DLL is not necessary. This comes in handy for developing Photon applications for handheld devices, where memory and storage is at a premium. The executable file for my time recorder was about 130 Kbytes; this, plus a minimum QNX and Photon installation, could easily run in 512 Kbytes of memory.

Taking Stock of Photon and Application Builder

While the programming models for Application Builder and Visual Basic are similar, the terminology is substantially different. Without exposure to X Window programming concepts, the transition would have been more difficult. Anyone expecting to work with the Application Builder in the same manner as Visual Basic will quickly become frustrated.

I am not a fan of the X look and feel, but then, there are very few graphical desktop managers that I take to naturally. Even with the Application Builder, programming for Photon seems much like programming for X, right down to widgets and callback functions.

This doesn't mean that it's difficult, however. I kept working with the fear that I would come up against an incomprehensible X-like structure, and it never happened. Application Builder takes over many of the programming chores you might have to do in X itself. I do a lot of work in Visual Basic, which clearly has more object classes, more properties, and more events to respond to, but then, the Windows and Visual Basic environments consume at least 30 Mbytes on my hard disk. As long as you don't set your expectations by these much bulkier products, you'll appreciate QNX, Photon, and the Application Builder for what they are--small, elegant tools for working in and developing applications for process control and real-time systems.

For More Information

Photon Window System

QNX Operating System

QNX Software Systems

175 Terence Matthews Crescent

Kanata, ON

Canada K2M 1W8

613-591-0931

Figure 1 The Application Builder workspace.

Example 1: (a) Code typically used to quit from a Visual Basic app; (b) in Application Builder, a callback function is used to end the application.

(a)
Sub DoneButton_Click ()
    End
End Sub

(b)
int quit_process(PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
{
   exit(EXIT_SUCCESS);
}

Example 2: Sample event handler for the bitmap arrow buttons that scroll the text field.

lint label = 0;
int label_change(PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
{
char  *txt_label, buffer[10];
ptArg_T  args[2];
/*Determine which arrow was clicked*/
switch(ApName(widget))  {
        case ABN_prev_label;
                if (--label 0) label = 0
                break;
        case ABN_prev_label;
                if (--label 0) label = 0
                break;
        }
/* Initialize arguments for selecting appropriate text label; set label */
PtSetArg (&args[0], Pt_ARG_TEXT_STRING, &txt_label, 0);
PtGetResources(ABW_label, 1, &args);
label = atoi(txt_label);
if (label 0) label = 0;
if (label 254) label = 254;
PtSetArg(&args[0], Pt_ARG_FILL_LABEL, label | Pg_INDEX_LABEL, 0);
PtSetArg(&args[1], Pt_ARG_LABEL, label | Pg_INDEX_LABEL, 0);
PtSetResources(ABW_label_rect, 2, &args);
sprintf(buffer, "%d", label);
PtSetArg(&args[0], Pt_ARG_TXT_STRING, buffer, 0);
PtSetResources(ABW_label, 1, &args);
return(Pt_CONTINUE);
}


Copyright © 1995, Dr. Dobb's Journal


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.