Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Page 1
BREW Programming Primer 2.1
Follow the steps below to create a BREW application. The numbers in the diagram correspond to the steps.
1. Use the BREW Application Wizard, an add-in to Microsoft Visual Studio, to create the workspace file
and the project file. The project file contains a skeletal source file and two additional files
(AEEAppGen.c and AEEModGen.c) that are essential to all BREW applications.
2. Create a Module Information File (MIF) with the BREW MIF Editor, which includes all module-specific
and application-specific information required to load the applet.
3. Use the BREW Resource Editor to create resources, such as strings, images, dialogs, and controls
to be used by the application.
4. Edit, compile and build the source files generated by the BREW Application Wizard and the resource
header files to create the application DLL file.
5. Launch the BREW Emulator, specifying the MIF and applet directories and test your application.
Page 2
Updated: March 13, 2007
Page 3
BREW Programming Primer 2.1
Sample Problem
To introduce you to BREW and some of its main concepts such as BREW APIs and event handling, this
tutorial helps you create a simple application called myfirstapp. The process of creating a BREW application
will familiarize you with some of the BREW tools used to support writing BREW applications.
If you have some experience with the BREW development environment, you might want to skip to the BREW
Application Wizard section.
Page 4
BREW Programming Primer 2.1
These documents are included with the BREW SDK and/or available at the BREW Developer's Technical web
site
Page 5
BREW Programming Primer 2.1
Page 6
BREW Programming Primer 2.1
BREW Setup
Other than your C compiler and third party tools, the components you need to install are
BREW SDK
NOTE: Be sure that you install Visual Studio before installing the BREW SDK. If you reinstall Visual Studio,
you need to reinstall the BREW SDK and the BREW SDK Tools. If you do not have Visual Studio, installing
the BREW SDK Tools will not install the BREW Add-ins for Visual Studio.
BREWDIR
During the installation process, you are prompted to set or update the BREWDIR Environment variable, or
leave it unchanged. The BREW directory is needed for referencing included files or libraries. You should
select the Set/Update the BREWDIR environment variable to point to the current installation and make it
the default SDK installation.
Throughout this primer BREWDIR is used to represent the SDK installation directory references to directory
paths. <BREWDIR\examples\myfirstapp> refers to the current BREW installation, for example, <C:\Program
Files\BREW SDK v2.1.3\sdk>.
When naming your BREW files, avoid spaces and uppercase letters in all of your BREW filenames.
Page 7
BREW Programming Primer 2.1
Fundamental Concepts
This section explains BREW fundamental concepts. If you are familiar with the BREW environment, or would
like to get started on myfirstapp right away, skip to Creating myfirstapp.
The basic elements of a BREW application are BREW classes and the BREW shell, module, applet, event
handler, ClassIDs, resources, and Module Information Files (MIF).
BREW APIs
The BREW API is included with the BREW SDK, and represents a group of interface classes with their own
set of functions to use in your applications. BREW interfaces are initialized and memory is allocated only
when the interface is needed. Each interface has a unique ClassID, and the name of each interface in the
BREW API begins with the letter I. The BREW API Reference lists the available interfaces and their functions
for the specific BREW versions in alphabetical order. The API Reference is included with the documents in
the BREW SDK User Docs.
BREW devices support an Application Execution Environment (AEE) to load and execute BREW
applications. The AEE is the foundation of BREW; its applications use the IShell interface pointer to access
shell services. The BREW shell is an interface, which is loaded when your application first runs. IShell
permits access to a wide variety of lower-level services provided by the device. The BREW shell handles
system services such as application management, resource management functions, device and application
configuration and management, and access to all external services.
BREW Module
A BREW module is a binary file containing the code for applications or extensions. Each module can contain
one or more applets (instance of the IApplet interface) or non-applet classes, and must have a corresponding
MIF associated with it. The module is loaded by the BREW shell. Fundamentally this module exports a
single defined entry point so that the BREW shell can call into your application's CreateInstance function (the
entry point for your application). The CreateInstance function creates an event handler and allocates
application memory, and creates an instance of the IDisplay interface and other miscellaneous items.
Each module can be initially developed in the BREW SDK, starting as a stand-alone Windows DLL that plugs
into the BREW Emulator for debugging and testing. Each application has a ClassID and a set of privileges
associated with it. These are defined in a MIF.
Event Handler
The BREW application model is based on an event-driven programming model. All BREW applications must
contain an event handling function. After an application is loaded, the BREW layer passes all input to this
function as events. An event may be a key-press, a dialog change, or a control change.
Your application is expected to quickly determine how to handle events. All processing must be completed
before the next event can be received. With BREW, substantial delays in processing events may result in the
application being shut down to safeguard the device.
ClassIDs
Page 8
ClassIDs are a unique 32-bit ID identifying BREW applications, BREW extensions, privilege levels, or BREW
interfaces. The ClassID associated with an interface is used to request the interface from the system shell.
The ClassID is stored in a BREW ClassID (BID) file, which is required for compilation. The BID file is created
by the MIF Editor when generating a ClassID locally. There are some distinctions between ClassIDs; those
associated with an application, those associated with BREW classes, and those associated with BREW
Extensions. Uniquely identifying ClassIDs becomes important when your application is ready to be made
public or commercial. A unique ClassID for an application or extension can be generated from the BREW
ClassID Generator via the BREW web site. However, for the purposes of this primer, you will generate the
ClassID for myfirstapp locally.
Resources
Many modules include resources, strings, icons and dialog boxes. Rather than hard-coding them into the
module, resources can be created in a separate resource file. This file is created by the BREW Resource
Editor, which allows you to assign ClassIDs to these resources and create a header file containing the
ClassIDs. See the BREW SDK Tools User Docs, included in the documents installed with the BREW SDK
Tools.
Created using the MIF Editor, the MIF contains information about the contents of the module, such as
supported classes, supported applications, application privileges, application details (like the title and icon)
and the author of the application. The MIF also specifies which classes are exported for use by other
modules. All modules must contain a MIF. As the device starts up, the MIF describes the module to the
device.
Page 9
BREW Programming Primer 2.1
Creating myfirstapp
You will use the BREW Application Wizard to create the myfirstapp project files and then add the code to
accomplish these tasks:
Draw a string in the center of the device screen that will familiarize you with writing to the display.
Draw a thin, horizontal rectangle that appears on the screen as a line, dividing the screen into two
halves to familiarize you with creating graphics.
Define and load two text strings from a resource file to familiarize you with using resources.
Load a bitmap image (representing a cursor) from the resource file and draw the cursor centered in
the bottom half of the screen. This will familiarize you with using graphics in your applications.
Enable the user to move the cursor bitmap using the up, down, left, and right arrow keys on the
device, which will teach you how to handle user input events.
The MIF Editor will generate a MIF file, which is required to compile your application and explain the
application to your device. The Resource Editor will create resource files that contain character strings,
objects, and dialogs that are loaded at runtime, and the Emulator will allow you to test your application, using
the default device skin.
Page 10
BREW Programming Primer 2.1
1. Start Visual Studio and click File > New > Project.
5. Next to Location, enter a path to the directory where you want to store the project files. For this exercise,
browse to the location <BREWDIR\Examples>.
7. Click OK.
Page 11
8. Leave all of the check boxes unchecked in this first window, and click Options.
NOTE: Some versions of the BREW Application Wizard require you to navigate to the Options window by
selecting Next, then select Options.
Page 12
NOTE: At this point, an experienced BREW developer has two options, either to bypass the BREW
MIF Editor and complete the BREW Application Wizard to build the BREW application project file,
leaving the MIF creation for later, or to create the MIF now. In this primer, the MIF is created later.
9. Click Finish.
NOTE: The radio button for generating source file comments is selected, because the source code for
the purposes of this tutorial is commented. You can decide whether you want the comments to show
in your code.
The Visual Studio window reopens with your new BREW application project loaded in the workspace.
The Application Wizard created the following files: the project file (myfirstapp.vcproj), the workspace
file (myfirstapp.sln), and the application source file (myfirstapp.c). You will be modifying the
myfirstapp.c file.
NOTE: AEEAppGen.c and AEEModGen.c are two files included with the BREW SDK required by
BREW applications. These two files are automatically included in your new BREW project.
Take a moment to read the comments generated by the Application Wizard in myfirstapp.c to
familiarize yourself with the code.
You may need to enable the BREW Add-Ins toolbar in Visual Studio. This can be done by clicking
Tools > Add-in Manager and select BREW Addins for Visual Studio 7.1, or click Tools >
Customize and select the Toolbars tab, and select BREW Addins.
Page 13
Copyright 2006 QUALCOMM Incorporated
Proprietary Statement
Updated: March 13, 2007
Page 14
BREW Programming Primer 2.1
BREW MIFs
At this point you need to create the Module Information File (MIF) for myfirstapp. As shown in the diagram
below, you will provide the application name and local ClassID to the MIF Editor. The MIF Editor build
command generates the MFX file, which is compiled to create the MIF. This diagram graphically shows the
relationship of the MIF Editor tool with the file types associated it, and whether they are used as source files
or generated by the tool.
The table shows the location and a description of the files used by the MIF Editor.
MIF Editor .mfx Examples\myfir Text source file in XML format used as an
stapp intermediate file to save its contents while
being developed. It is compiled into the MIF
Page 15
The BID file is listed under the #include statements in myfirstapp:
#include "myfirstapp.bid"
Create the MIF file with the MIF editor
1. Launch the MIF Editor. In Visual Studio you can launch the MIF Editor from the toolbar as seen
below.
3. Click Locally.
Enter the number 10101010 for the ClassID and myfirstapp for the Class Name. If you are following
this process for multiple applications you will need to use a different number for the ClassID for each
application
4. Click OK.
5. A dialog box opens asking whether you want to generate the BREW ClassID locally, click Yes.
Page 16
The Save As dialog box opens.
7. Under File name, you should see myfirstapp.bid. In the Save in box navigate to the
<BREWDIR\Examples\myfirstapp> directory and click Save to save your file as myfirstapp.bid.
The ClassID you defined is added to the main MIF Editor window.
NOTE: For myfirstapp it is not necessary to enter information on any of the other MIF Editor tabs.
A Save BREW MIF File As dialog box opens. Enter the file name myfirstapp, and make sure you are
saving the file in the <BREWDIR\Examples> directory (the parent directory for the myfirstapp folder).
Click Save.
9. A window opens telling you that you have successfully generated your MIF file. Click OK. At this point
the MFX file has been compiled into the MIF and both the MFX and MIF files have been saved.
10. You should verify that you have generated the files you need for myfirstapp. The MIF file for myfirstapp
(myfirstapp.mif) should be in the Examples directory outside of the myfirstapp folder.
Page 17
BREW Programming Primer 2.1
BREW Emulator
The Emulator presents an image of a selected device and emulates the running of your application as it will
appear on the actual device. You can load your application in the Emulator by specifying the applet directory
within the Emulator GUI that contains the DLL generated when you compile your application (myfirstapp).
When you run the Emulator, the BREW Application Manager presents a series of applications contained in
the applet directory. When you select your application from the list the Emulator executes the application's
DLL showing its initial screen on the device. The diagram shows the files you need to test myfirstapp on the
Emulator. The Emulator uses the MIF file, the DLL, the optional BAR file and the device skins.
You should check that you have set up your development environment correctly by compiling and building the
Page 18
skeleton code generated by the Application Wizard and running it on the Emulator before adding any code.
The skeleton code does not do anything until you modify myfirstapp. After completing each step in writing the
source code for myfirstapp, check your progress by compiling and building your application in Visual Studio
and running it on the Emulator.
Some device skins may not display your application if you have not provided a valid icon within the MIF.
Myfirstapp does not provide a valid icon within the MIF. Try switching to another device skin if you are unable
to see the myfirstapp application in the Emulator menu.
NOTE: Before you run myfirstapp on the Emulator you should be sure all the myfirstapp files are saved in the
correct directory. The myfirstapp.mif and myfirstapp.mfx should be saved in the <BREWDIR\Examples>
directory and all other myfirstapp files should be saved in <BREWDIR\Examples\myfirstapp> outside the
Debug folder.
NOTE: The first time you open the Emulator using this method you will need to specify the
BREW_Emulator.exe as your executable. You can browse to the Emulator executable at <BREWDIR\Bin>.
2. You can also initiate the Emulator from the Windows start menu by selecting start > Programs > BREW
SDK v2.1.3 > BREW Emulator.
3. This can also be done from the Emulator icon in the Visual Studio toolbar.
4. Within the Properties tab make sure that the applet directory is set to <BREWDIR\Examples>. Click
File > Change Applet Dir to change the applet directory.
5. Under the Properties tab be sure that the MIF directory is the same as the applet directory.
6. Click the right or left arrow key on the Emulator keypad until myfirstapp appears.
NOTE: The main image displayed on the Emulator's device screen is the default applet icon, because
you did not select an image when you created the MIF.
Page 19
7. The button to select the application may vary depending on your device skin. The code generated by the
AppWizard does not do anything until you add more functionality. You should be able to see the icon, but will
not get a response when you click on it.
8. Close your application once you have tested it on the Emulator to release the DLL before you compile
your application again.
NOTE: If you see the message "This application has been disabled. Would you like to restore it?" when you
attempt to run your application on the Emulator, it is likely that you do not have all of the application files in
the correct location, or you may be missing the application DLL.
Be sure that you have generated the application DLL by compiling and building the application in Visual
Studio.
The myfirstapp.mif should be saved in the <BREWDIR\Examples> directory. All other myfirstapp files,
including myfirstapp.dll, should be saved in <BREWDIR\Examples\myfirstapp>, outside the Debug folder.
Page 20
BREW Programming Primer 2.1
BREW Resources
As you write myfirstapp you will also need to create resource files with the BREW Resource Editor that
contain character strings, objects, and dialogs which are loaded at runtime. Rather than hard-coding
resources into your application source code consider using resources whenever possible. This diagram
graphically shows the relationship of the Resource Editor tool with the file types associated it, and whether
they are used as source files or generated by the tool.
The table shows the location and a description of the files used by the Resource Editor.
File formats for BREW
File Folder Purpose/Description
extension
Page 21
One of the objectives for the creation of myfirstapp is to create a resource file that contains two character
strings and a cursor image.
1. In Visual Studio click the Resource Editor icon on the BREW Add-ins toolbar to start the Resource
Editor. To start the Resource Editor from the start menu, select start > Programs > BREW SDK
Tools > BREW Resource Editor.
2. If the Properties pane on the right in the Resource Editor is not already showing, select View >
Properties in the Resource Editor.
NOTE: You can also right-click on Strings in the left-hand pane and select New String.
4. In the Properties pane on the right side of the screen, type Use arrow keys in the Data field. Note the
entry for each property in the table.
Property Entry
ID 1001
Name IDS_STRING_1001
Page 22
Source Text
Data This field contains the actual string content for the resour
Encoding Unicode
5. When you have completed your entries for this resource, click Apply.
The string resources you have created appear in the main pane of the Resource Editor window.
6. To add your second string repeat the previous steps and type to move cursor in the data field. Note the
entry for each property in the table.
Property Entry
ID 1002
Name IDS_STRING_1002
Source Text
Encoding Unicode
7. When you have completed your entries for this resource click Apply.
The string resources you have created appear in the main pane of the Resource Editor window.
1. With the Resource Editor window open select Resource > New Object.
NOTE: You can also right-click on Objects and select New Object.
A default object line appears in the middle pane and the Properties pane to the right shows property
information for the object.
Page 23
2. In the Properties pane on the right click in the Data field, then click the Locate button (...) and navigate
to <BREWDIR\bitmaps> and load cursor-1.bmp. The object is issued a default name IDI_OBJECT_5001
which you will change to IDI_IMG_CURSOR. Note the entry for each property in the table.
Property Entry
ID 5001
Name IDI_IMG_CURSOR
Source File
Data cursor-1.bmp
After selecting the bitmap in the Data field notice that the fields under Preview in the Properties pane
on the right have been pre-filled with values for the bitmap you selected and the Object Preview shows
the cursor image.
Page 24
3. When you have completed your entries for the object click Apply, and save your file as myfirstapp in the
<BREWDIR\Examples\myfirstapp> directory.
NOTE: Be sure to use only lowercase letters with no spaces when naming your resource files. Do not add an
extension to the name of the file at this step because the Resource Editor automatically saves myfirstapp as
myfirstapp.brx. This is the file you will compile to create you resource file and header file.
1. In the Resource Editor window select Build > Compile Resource Script.
A message box opens informing you that the BREW Applet Resource file (BAR) and BREW Resource
Header file (BRH) have been created. They are stored in the same directory.
NOTE: Be sure that you have included the resource file you have just created (myfirstapp.brh) in your
Visual Studio solution, or your application will not compile.
Page 25
BREW Programming Primer 2.1
Now that you have created your base project you are ready to start writing the actual source code for
myfirstapp. The coding portion of the primer is divided into the following sections or steps. When you have
finished a step, compile and build your application, then run it on the Emulator. If you are interested in simply
displaying text on the Emulator screen, step 1 is essentially a "Hello World" program that requires no
resources.
Step 1: Draw a string in the center of the device screen that will familiarize you with writing to the
display.
Step 2: Draw a thin, horizontal rectangle, dividing the screen into two halves to familiarize you with
creating graphics.
Step 3: Define and load two text strings from a resource file to familiarize you with using resources.
Step 4: Load a bitmap image (representing a cursor) from the resource file and draw the cursor
centered in the bottom half of the screen. This will familiarize you with using graphics in your
applications.
Step 5: Enable the user to move the cursor bitmap using the up, down, left, and right arrow keys on
the device, which will teach you how to handle user input events.
If you previously installed an older version of the BREW Visual Studio Add-ins on your computer, the
Application Wizard may generate code slightly different than the code used in this example. In the applet
structure, your code generated by the Application Wizard should look like the following:
typedef struct _myfirstapp {
AEEApplet a ; // First element of this structure must be
AEEApplet
AEEDeviceInfo DeviceInfo; // Always have access to the hardware
device information
IDisplay *pIDisplay; // Give a standard way to access the
Display interface
IShell *pIShell; // Give a standard way to access the Shell
interface
}myfirstapp;
If you are missing the following two lines, be sure to add them in so your code matches the sample shown
above.
IDisplay *pIDisplay; // give a standard way to access the Display
interface
IShell *pIShell; // give a standard way to access the Shell
interface
Your code generated by the Application Wizard for myfirstapp_InitAppData should look like the following:
boolean myfirstapp_InitAppData(myfirstapp* pMe)
{
pMe->DeviceInfo.wStructSize = sizeof(pMe->DeviceInfo);
ISHELL_GetDeviceInfo(pMe->a.m_pIShell,&pMe->DeviceInfo);
pMe->pIDisplay = pMe->a.m_pIDisplay;
Page 26
pMe->pIShell = pMe->a.m_pIShell;
return TRUE;
}
If you are missing the following lines of code, be sure to paste them in so your code matches that shown
above.
pMe->pIDisplay = pMe->a.m_pIDisplay;
pMe->pIShell = pMe->a.m_pIShell;
NOTE: Some comments generated by the Application Wizard are not duplicated in this section.
Page 27
BREW Programming Primer 2.1
When you ran the Application Wizard, it provided you with the minimum underlying structure of the
myfirstapp_HandleEvent function with commented placeholders for you to type your application-specific code
for handling events. Following is the code provided by the Application Wizard.
myfirstapp_DrawScreen(pMe);
myfirstapp_DrawScreen(pMe);
For myfirstapp, you must add a new function prototype to support a new function you will be adding to the
code.
/*-------------------------------------------------------------------
Function Prototypes
-------------------------------------------------------------------*/
static boolean myfirstapp_HandleEvent(myfirstapp* pMe, AEEEvent eCode,
uint16 wParam,uint32 dwParam);
boolean myfirstapp_InitAppData(myfirstapp* pMe);
void myfirstapp_FreeAppData(myfirstapp* pMe);
(1)
Page 28
1. Scroll down in your source code and type the following after the FreeAppData function prototype (1):
1. Scroll down to the end of the code generated by the Application Wizard and add the following code:
At this point you need to be sure you have created the MIF using the MIF Editor before you compile your
application. Once you have created the MIF, compiled and built your application, you can proceed to the
Emulator section and run your work on the Emulator to test what you have done so far. When you test your
code on the Emulator you should see the following display:
Page 29
Copyright 2006 QUALCOMM Incorporated
Proprietary Statement
Updated: March 13, 2007
Page 30
BREW Programming Primer 2.1
The applet structure information, located below the Includes and Variable Definitions section of the source
code, is shown below.
typedef struct _myfirstapp {
AEEApplet a ; // First element of this structure must be
AEEApplet
AEEDeviceInfo DeviceInfo; // Always have access to the hardware device
information
IDisplay *pIDisplay; // Give a standard way to access the Display
interface
IShell *pIShell; // Give a standard way to access the Shell
interface
(1) // Add your own variables here...
} myfirstapp;
To add to the application-specific data structure information at (1)
The code you will be modifying for the myfirstapp_DrawScreen() function is shown below.
1. Add the following lines to the beginning of the myfirstapp_DrawScreen() function (1):
AEERect rc;
Page 31
AECHAR szBuf[30] = {0};
2. Add the following lines above the line IDISPLAY_Update (pMe->pIDisplay) (2):
Next you need to modify the myfirstapp_InitAppData function, which is used to initialize applet-specific
data. Below is the initial source code generated by the Application Wizard.
2. Save your work, compile and build your application, and test your application on the Emulator. You
should see the following display.
Page 32
Copyright 2006 QUALCOMM Incorporated
Proprietary Statement
Updated: March 13, 2007
Page 33
BREW Programming Primer 2.1
For the following code additions to compile you'll need to create your resource file using the Resource Editor
and return to this section and proceed. To load two strings from the resource file you'll need to add an
#include line and modify the myfirstapp_DrawScreen function.
When you ran the Application Wizard it automatically generated the following #include statements in your
source code:
/*=======================================================================
==
INCLUDES AND VARIABLE DEFINITIONS
========================================================================
*/
#include "AEEModGen.h" // Module interface definitions for IModule
interface
#include "AEEAppGen.h" // Applet interface definitions for IApplet
interface
#include "AEEShell.h" // Shell interface definitions for IShell BREW
interface
#include "myfirstapp.bid" // Identifies the BID file, which contains the
locally generated ClassID created with the MIF Editor
As you create a BREW application you need to add another #include statement to handle the other BREW
interfaces required by the application and definitions for any global constants needed by the application.
1. Scroll down to the INCLUDES AND VARIABLE DEFINITIONS section shown above and add the
following #include line after the AEEShell.h line.
#include "myfirstapp.brh" // Resource ID definitions
NOTE: The header file you generate using the Resource Editor, myfirstapp.brh, will be added to get
the resource IDs for the applet. The myfirstapp.bid file you create using the MIF Editor contains the
definitions of the unique ClassIDs for the applet.
Page 34
NULL, // No clipping
IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);
IDISPLAY_ClearScreen(pMe->pIDisplay); // Erases whole screen
SETAEERECT( &rc, 0, pMe->m_cyHeight/2-2, pMe->m_cxWidth, 2);
IDISPLAY_DrawRect(pMe->pIDisplay, &rc, 0, 1, IDF_RECT_FILL);
(1)
IDISPLAY_Update(pMe->pIDisplay);
}
ISHELL_LoadResString(pMe->pIShell, MYFIRSTAPP_RES_FILE,IDS_STRING_1001,
szBuf, sizeof(szBuf));
IDISPLAY_DrawText(pMe->pIDisplay, AEE_FONT_NORMAL, szBuf, -1,
pMe->m_cxWidth/5, pMe->m_cyHeight/8, 0, IDF_ALIGN_CENTER);
ISHELL_LoadResString(pMe->pIShell, MYFIRSTAPP_RES_FILE,
IDS_STRING_1002,szBuf, sizeof(szBuf));
IDISPLAY_DrawText(pMe->pIDisplay, AEE_FONT_NORMAL, szBuf, -1,
pMe->m_cxWidth/5, pMe->m_cyHeight/5, 0, IDF_ALIGN_CENTER);
2. Save your work, compile and build your application, and test it on the Emulator. You should see the
following display:
Page 35
BREW Programming Primer 2.1
The code in the myfirstapp_DrawScreen function, which you will be modifying appears below.
IDISPLAY_DrawText(pMe->pIDisplay, AEE_FONT_NORMAL, szBuf, -1,
pMe->m_cxWidth/5, pMe->m_cyHeight/5, 0,IDF_ALIGN_CENTER);
SETAEERECT (&rc, 0, pMe->m_cyHeight/2-2, pMe->m_cxWidth, 2);
IDISPLAY_DrawRect (pMe->pIDisplay, &rc, 0, 1, IDF_RECT_FILL);
ISHELL_LoadResString(pMe->pIShell, MYFIRSTAPP2_RES_FILE, IDS_STRING_1001,
szBuf, sizeof(szBuf));
IDISPLAY_DrawText(pMe->pIDisplay, AEE_FONT_NORMAL, szBuf, -1,
pMe->m_cxWidth/5, pMe->m_cyHeight/8, 0, IDF_ALIGN_CENTER);
ISHELL_LoadResString(pMe->pIShell, MYFIRSTAPP2_RES_FILE, IDS_STRING_1002,
szBuf, sizeof(szBuf));
IDISPLAY_DrawText(pMe->pIDisplay, AEE_FONT_NORMAL, szBuf, -1,
pMe->m_cxWidth/5, pMe->m_cyHeight/5, 0, IDF_ALIGN_CENTER);
(1)
IDISPLAY_Update (pMe->pIDisplay);
To modify the myfirstapp_DrawScreen function
pMe->pIImage =
ISHELL_LoadResImage(pMe->pIShell,MYFIRSTAPP_RES_FILE,IDI_IMG_CURSOR);
IIMAGE_Draw (pMe->pIImage, pMe->m_nCursorX, pMe->m_nCursorY);
The added statement loads the image from the resource file and stores a pointer to the image in the pIImage
applet structure data.
Below is the code in myfirstapp for the myfirstapp_InitAppData function, including the additions made in the
preceding steps.
boolean myfirstapp_InitAppData(myfirstapp* pMe)
{
pMe->DeviceInfo.wStructSize = sizeof(pMe->DeviceInfo);
ISHELL_GetDeviceInfo(pMe->a.m_pIShell, &pMe->DeviceInfo);
pMe->pIDisplay = pMe->a.m_pIDisplay;
pMe->pIShell = pMe->a.m_pIShell;
pMe->m_cxWidth = pMe->DeviceInfo.cxScreen; // Cache the width of the
device screen
pMe->m_cyHeight = pMe->DeviceInfo.cyScreen; // Cache the height of the
device screen
pMe->pIImage = NULL;
(1)
// If there have been no failures up to this point then return
success return TRUE;
return TRUE;
}
Page 36
To modify the myfirstapp_InitAppData function
pMe->m_nCursorX = pMe->m_cxWidth/2;
pMe->m_nCursorY = pMe->m_cyHeight*2/3;
The myfirstapp_FreeAppData function frees application data stored in the applet data structure. The source
code you will be modifying for the myfirstapp_FreeAppdata function appears below.
NOTE: Do not call the myfirstapp_FreeAppData function directly in your application, since the BREW layer
will automatically call it.
// this function is called when your application is exiting
void myfirstapp_FreeAppData(myfirstapp* pMe)
{
// insert your code here for freeing any resources you have
allocated...
// example to use for releasing each interface:
// if ( pMe->pIMenuCtl != NULL ) // Check for NULL first
// {
// IMENUCTL_Release(pMe->pIMenuCtl) // Release the interface
// pMe->pIMenuCtl = NULL; // Set to NULL so no
problems trying to free later
// }
}
To modify myfirstapp_FreeAppdata function
if (pMe->pIImage != NULL)
{
IIMAGE_Release (pMe->pIImage);
pMe->pIImage = NULL;
}
Save your work, compile and build your application, and test your application on the Emulator. You should
see the following display;
Page 37
Copyright 2006 QUALCOMM Incorporated
Proprietary Statement
Updated: March 13, 2007
Page 38
BREW Programming Primer 2.1
Myfirstapp_Move is a function that allows the user to move the cursor on the device screen. The function
uses an applet data structure pointer (pMe) and two integer variables corresponding to incremental
movements of the cursor in the x and y directions.
For myfirstapp you must add a new function prototype to support the new function you will be adding to the
code.
1. Scroll down in your source code and type the following after the myfirstapp_DrawScreen function
prototype:
1. Scroll down to the end of the code generated by the Application Wizard and add the following code:
Page 39
To modify the myfirstapp_HandleEvent function
2. Save your work, compile and build your application, and test it in the Emulator.
3. Click the up, down, left, or right arrow keys to move the cursor around the bottom half of the screen.
Page 40
BREW Programming Primer 2.1
Emulation Debugging
There are two primary strategies available for debugging program behavior in BREW. The first is to use the
Helper function DBGPRINTF() for simple error-checking print statements; the other is to debug the application
with Visual Studio while it is running in the Emulator.
NOTE: Be sure to check that debug information is turned on in Visual Studio. Click Project > myfirstapp
Properties > Linker > Debugging > Generate Debug Info.
A warning window opens the first time you debug your application.
2. Click OK.
A dialog box instructing you to select your executable program appears. The first time you debug a
project you need to specify the Emulator application for debugging.
Control returns to Visual Studio when the Emulator fails or as soon as failure reaches the line of code
containing the breakpoint if breakpoints were specified.
NOTE: This tool greatly facilitates debugging memory access and non-failing code logic issues, as
Page 41
Visual Studio indicates exactly which line of code is responsible for the failure. To specify a breakpoint
right-click on the desired line of code and select Insert Breakpoint from the resulting pop-up menu.
5. If a breakpoint was specified and after the breakpoint is reached use Visual Studio's debugging tools
to inspect relevant variables and function call parameters. Review your code to diagnose the causes
of your program's undesired behavior.
At this point it is assumed that you have successfully written myfirstapp and tested it on the Emulator.
Page 42
BREW Programming Primer 2.1
<BREWDIR\Examples> directory
In the BREW SDK the <BREWDIR\Examples> directory contains several sample applications written by
in-house QUALCOMM developers. The sample applications demonstrate the use of many interfaces and
controls commonly used in BREW application development. Each sample application includes the source
code as well as the files you will need to run the application on the Emulator.
A quick way to access the Examples directory within Windows is to click Start > Programs > BREW SDK
> BREW Examples.
The BREW Sample Applications Guide, accessed by clicking Start > Programs > BREW SDK > BREW
SDK Docs > BREW Sample Applications Guide, examines the sample BREW applications.
In the Sample Applications Guide, read the descriptions of what these applications allow the device user to
do, try them out on the Emulator to see how they really work, and look at the source code for each. The
source code is heavily commented to aid in your exploration.
Page 43
BREW Programming Primer 2.1
BREW Forums
BREW Forums provides BREW developers a web site to discuss using BREW technical and
business-related solutions with other BREW application developers. Go to http://brewforums.qualcomm.com/
Page 44
BREW Programming Primer 2.1
BREW Training
QUALCOMM offers a variety of training packages designed to benefit developers, managers, and trainers. For
more information see the BREW web site.
Page 45
BREW Programming Primer 2.1
It is important to include the EVT_APP_SUSPEND and EVT_APP_RESUME event handlers within your
application's HandleEvent function so that your application knows how to handle events that might interrupt
its operation. For example, when an SMS message or an incoming phone call is received, your application
needs to know whether it should resume where it left off at the end of the call or if it should start over.
When BREW suspends an applet it sends the EVT_APP_SUSPEND event. You can have BREW handle the
event in a couple of ways depending on the type of application you are developing.
1. You can set the applet to return TRUE to the event, indicating that the applet has processed the
Suspend and has not been unloaded from memory. This is how myfirstapp works.
2. You can also have the applet return FALSE to the event, indicating that you do not want the applet to
process the Suspend. When this occurs BREW sends an EVT_APP_STOP event and the applet is
removed from memory.
Page 46
BREW Programming Primer 2.1
Page 47
BREW Programming Primer 2.1
Appendix
Additional information on device skins, BREW APIs, and BREW Extensions are included in the appendix.
Device Skins
BREW APIs
Extensions
Page 48
BREW Programming Primer 2.1
Device Skins
The Emulator is installed with a default set of device skins, QUALCOMM Skin Configurator (QSC) files,
located in the Devices folder of your BREW installation. Additional device skins are available for download by
authenticated developers from the QUALCOMM BREW Devices page.
NOTE: Device skins allow you to test your application using the appearance and limitations of the intended
device, but they do not completely replicate the specific device environment. The Emulator and its device
skins should not replace testing your application directly on the device. Many phones have known issues that
are not reproduced in the Emulator.
Page 49
BREW Programming Primer 2.1
BREW APIs
BREW is a library of functions, Application Programming Interfaces (APIs), used to write applications for
mobile devices. To use the methods in a particular BREW API the corresponding header file must be
included in your application.
To prevent the introduction of static and global data do not link standard C libraries to your BREW
application. Standard C library routines such as strcat(), malloc(), and sprintf() cannot be used within BREW.
Helper functions are provided for the most commonly used C functions.
For any specific usage details on BREW Helper functions and the various BREW APIs, consult the BREW
API Reference.
Page 50
BREW Programming Primer 2.1
Extensions
Extensions are reusable components, which extend the BREW API. They can be thought of as software
libraries encapsulated as classes for use in applications that you and others develop. They are self-developed
extended interfacesclasses defined by developers in modules, and are then shared with other
developers. These modules are obtained when the classes are required. They are not used as a stand-alone
application; rather extensions are handled within the context of the application using the extension.
Page 51