Introduction to Systems Programming CS 0449 Working with

  • Slides: 26
Download presentation
Introduction to Systems Programming (CS 0449) • Working with clipboard –(pg 333, Palm. OS

Introduction to Systems Programming (CS 0449) • Working with clipboard –(pg 333, Palm. OS Bible) – Chapter 9. –Palm. OS SDK Reference ->User Interface -> Clipboard

Popular Linking Error • Some have received strange error message: My. Palm. Main. o(.

Popular Linking Error • Some have received strange error message: My. Palm. Main. o(. text+0 x 1 ac): My. Palm. Main. c: 111: undefined reference to `Fld. Set. New. Text' • The cause: Linker could not find Fld. Set. New. Text() definition in the object files. • Possible cause: Fld. Set. New. Text() was declared as static • "static" for functions means "do not use outside the module" • Remove "static" if you want to make the function public.

Checking Supported Features • Features are – Hardware (Processor ID, graphics features, serial ports.

Checking Supported Features • Features are – Hardware (Processor ID, graphics features, serial ports. . . ) – OS features (version number. . . ) • Before using a feature check if it is present

Supported Features (1) • Determining Palm OS version (pg 306, Palm. OS Bible Ch.

Supported Features (1) • Determining Palm OS version (pg 306, Palm. OS Bible Ch. 9) UInt 32 version; Ftr. Get ( sys. Ftr. Creator, sys. Ftr. Num. ROMVersion, & version ); if ( sys. Get. ROMVer. Major ( version ) = = 5) { // version 5. xx. . . } else { // version 4. xx or lower. . . } Example: Format= 0 x. MMmfsbbb MM: Major version m: Minor version f: Bug fix version number s: Release stage 0: development 1: alpha 2: beta 3: release bbb: build number for nonreleases sys. Get. ROMVer. Major sys. Get. ROMVer. Minor sys. Get. ROMver. Fix sys. Get. ROMVer. Stage sys. Get. ROMVer. Build

Supported Features (2) • Checking CPU (pg 308, Palm. OS Bible – Ch. 9)

Supported Features (2) • Checking CPU (pg 308, Palm. OS Bible – Ch. 9) UInt 32 cpu. ID; Ftr. Get ( sys. Ftr. Creator, sys. Ftr. Num. Processor. ID, & cpu. ID ); if (cpu. ID = = sys. Ftr. Num. Processor 328 ) { // Motorola 68328 Dragon. Ball } if (cpu. ID = = sys. Ftr. Num. Processor. EZ ) { // Motorola 68 EZ 328 Dragon. Ball. EZ }

Supported Features (3) • Checking Backlight (pg 308, Palm. OS Bible, Ch. 9) UInt

Supported Features (3) • Checking Backlight (pg 308, Palm. OS Bible, Ch. 9) UInt 32 light; Ftr. Get ( sys. Ftr. Creator, sys. Ftr. Num. Backlight, & light ); if (light = = 0 x 00000001 ) { // Backlight present } else { // No backlight = 0 x 0000 }

Supported Features (4) • Battery Information (Managing Power) (pg 330, Palm. OS Bible, Ch.

Supported Features (4) • Battery Information (Managing Power) (pg 330, Palm. OS Bible, Ch. 9) UInt 16 Sys. Battery. Info (Boolean set, UInt 16 *warn. Threshold. P, UInt 16 *critical. Threshold. P, UInt 16 *max. Ticks. P, Sys. Battery. Kind *kind. P, Boolean *plugged. In, UInt 8 *percent. P)

Programming System Elements: Clipboard Chapter 9, pg 333, Palm. OS Programming Bible

Programming System Elements: Clipboard Chapter 9, pg 333, Palm. OS Programming Bible

What is a clipboard? • A way to exchange data within or between applications

What is a clipboard? • A way to exchange data within or between applications • What data? – text – bitmap – rich text (not in Palm. OS) –. . .

Clipboard Operations • From user perspective: – Make selection – Copy selection – Cut

Clipboard Operations • From user perspective: – Make selection – Copy selection – Cut selection – Paste selection

Easy Clipboard • For exchanging data with a text field – void Fld. Cut

Easy Clipboard • For exchanging data with a text field – void Fld. Cut (Field. Type *fld. Ptr) – void Fld. Copy (Field. Type *fld. Ptr) – void Fld. Paste (Field. Type *fld. Ptr). . . // in the menu event handler, switching on object ID case My. Menu. Paste. Id : fld. Ptr = Get. Object. Ptr( My. Field. Id ); Fld. Paste (fld. Ptr); break; . . .

Advanced Clipboard • Palm. OS clipboard formats enum clipboard. Formats { clipboard. Text, //

Advanced Clipboard • Palm. OS clipboard formats enum clipboard. Formats { clipboard. Text, // Textual data clipboard. Ink, // Reserved clipboard. Bitmap // Bitmap data }; typedef enum clipboard. Formats Clipboard. Format. Type;

Advanced Clipboard (2) • Adding item to clipboard void Clipboard. Add. Item ( const

Advanced Clipboard (2) • Adding item to clipboard void Clipboard. Add. Item ( const Clipboard. Format. Type format, const void *ptr, // pointer to the object UInt 16 length) // size of the object

Advanced Clipboard (3) • Getting item from clipboard Mem. Handle Clipboard. Get. Item (

Advanced Clipboard (3) • Getting item from clipboard Mem. Handle Clipboard. Get. Item ( const Clipboard. Format. Type format, UInt 16 *length) // size of the object

Advanced Clipboard (4) • Appending item to clipboard Err Clipboard. Append. Item (const Clipboard.

Advanced Clipboard (4) • Appending item to clipboard Err Clipboard. Append. Item (const Clipboard. Format. Type format, const void *ptr, // pointer to the object UInt 16 length) // size of the object

Manipulating Text • Using fonts (pg 309, Palm. OS Bible) Font. ID old. Font;

Manipulating Text • Using fonts (pg 309, Palm. OS Bible) Font. ID old. Font; old. Font = Fnt. Set. Font( large. Font ); • More font functions Fnt. Average. Char. Width w Fnt. Base. Line b Fnt. Char. Height c Fnt. Descender. Height d Fnt. Line. Height h Aqua w b h c d

Handling Pen Events • Pen events: – Pen. Down. Event – Pen. Up. Event

Handling Pen Events • Pen events: – Pen. Down. Event – Pen. Up. Event – Pen. Move. Event • Handled in application's form event handler

Pen Events (2) – Ref. Page 319 // in form event handler procedure—in Main.

Pen Events (2) – Ref. Page 319 // in form event handler procedure—in Main. Form. Handle. Event(Event. Type *event) Rectangle. Type g. Draw. Rect; Boolean handled=false; case pen. Down. Event : if ( Rct. Pt. In. Rectangle ( event->screen. X, event->screen. Y, &g. Draw. Rect )) { g. X = event -> screen. X; g. Y = event -> screen. Y; g. Pen. Down = true; //current state of stylus handled = true; } break; case pen. Move. Event : if ( Rct. Pt. In. Rectangle ( event->screen. X, event->screen. Y, &g. Draw. Rect ) && g. Pen. Down) { Int 16 new. X = event -> screen. X; Int 16 new. Y = event -> screen. Y; Win. Draw. Line(g. X, g. Y, new. X, new. Y) g. X = new. X; g. Y = new. Y; handled = true; } break; case pen. Up. Event : if ( Rct. Pt. In. Rectangle ( event->screen. X, event->screen. Y, &g. Draw. Rect ) && g. Pen. Down) { Int 16 new. X = event -> screen. X; Int 16 new. Y = event -> screen. Y; Win. Draw. Line(g. X, g. Y, new. X, new. Y) g. X = g. Draw. Rect. top. Left. x; g. Y = g. Draw. Rect/top. Left. y; g. Pen. Down = false; //current state of stylus handled = true; } break;

Handling Key Events • Every key press and graffiti stroke creates a key event

Handling Key Events • Every key press and graffiti stroke creates a key event • Key events are: – Key. Down. Event – Key. Up. Event • Key events may have: – character, hardware key, or virtual key code – modifiers (Shift, Caps. Lock, "powered. On. Key. Mask", . . . )

Intercepting Key Events static Boolean Main. Form. Handle. Event(Event. Type *event) { Boolean handled

Intercepting Key Events static Boolean Main. Form. Handle. Event(Event. Type *event) { Boolean handled = false; switch( event->e. Type ) { case key. Down. Event: if ( event -> data. key. Down. chr == vchr. Page. Down ) && ( event -> data. key. Down. modifiers & auto. Repeat. Key. Mask )) { Frm. Alert(My. Alert); return handled; handled = true; break; default: break; } return handled; Reference: Tables 9 -6 pages (321 -322) Ch. 9. }

Overriding/Intercepting Hardware application buttons static void App. Event. Loop(void ) {// call this before

Overriding/Intercepting Hardware application buttons static void App. Event. Loop(void ) {// call this before the Sys. Handle. Event() (see pg 323 Palm. OS Bible) Event. Type *event; UInt 16 error; do { Evt. Get. Event(&event, evt. Wait. Forever); //intercept hardware application buttons if (Special. Key. Down(&event)) continue; if (! sys. Handle. Event(&event)) if (!Menu. Handle. Event(0, &event), &error)) if (!Application. Handle. Event(&event)) Frm. Dispatch. Event(&event); } while (event. e. Type !=app. Stop. Event); } Boolean Special. Key. Down(Event. Type *event) { Boolean handled = false; if ( event ->e. Type != key. Down. Event ) return handled; if ( event -> data. key. Down. modifiers & powered. On. Key. Mask ) return handled; switch( event->data. key. Down. chr ) { case vchr. Hard 1: //Datebook hardware button // Handle the first hardware button handled = true; Modifier for Key. Down. Event-break; powered the system on. case vchr. Hard 2: //Address hardware button // Handle the first hardware button handled = true; break; case vchr. Hard 3, 4 //To. Do list and Memo. Pad hardware buttons …………. }}

Creating New Events • Events are put in an event queue by OS •

Creating New Events • Events are put in an event queue by OS • Application can add an event to the queue Event. Type new. Event ; new. Event. e. Type = key. Down. Event ; . . . // Prepare event parameters Evt. Add. Event. To. Queue( &new. Event );

Random Number Generator • To get a pseudo-random number between 0 and sys. Random.

Random Number Generator • To get a pseudo-random number between 0 and sys. Random. Max call: x = Sys. Random( seed. Value ); or x = Sys. Random( Tim. Get. Ticks() ); • My utility random number generator UInt 16 Random. Num( UInt n ) { return ( Sys. Random(0) / ( sys. Random. Max / n ) ) ; }

Identifying the Device • To identify the PDA call: Err Sys. Get. ROMToken (

Identifying the Device • To identify the PDA call: Err Sys. Get. ROMToken ( UInt 16 card. No, UInt 32 token, UInt 8 **data. P, UInt 16 *size. P) • Note: – – – check return for 0 (0=success) check data. P for NULL data. P is pointer to pointer! card. No is ROM card to be queried, usually 0 data. P is a pointer to text string

Example void Draw. Serial. Number(Int 16 x, Int 16 y){ char *buffer; UInt 16

Example void Draw. Serial. Number(Int 16 x, Int 16 y){ char *buffer; UInt 16 length; Err err; err = Sys. Get. ROMToken(0, sys. ROMToken. Snum, (Byte. Ptr*) &buffer, &length); if( (! err) && (buffer) && ((Byte) *buffer != 0 x. FF) ) // There is a valid serial number Win. Draw. Chars( buffer, length, x, y ); else // No valid serial number, draw error message Win. Draw. Chars( "No serial number", 16, x, y); }

Readings From "Palm OS Programming Bible" – Programming System Elements: (Chapter 9, Palm. OS

Readings From "Palm OS Programming Bible" – Programming System Elements: (Chapter 9, Palm. OS Bible)