Programming tips for Sony Clié

 

avaricum.h

Available functions

31/3/2

  • static void AppStartSony(void);
    // Initialize all constants : To be included in AppStart
  • static void AppSonyStop(void);
    // Release pointers : To be included in AppStop
  • static void * GetObjectPtr(UInt16 objectID);
    // Get a pointer from a resource ID
  • static MemHandle DupString (char *pText);
    // Get an handle from a string
  • static UInt16 LoadLibHR();
    // Load Hires library
  • static void ListFileRep(UInt16 OpenListFileList);
    // Write the content of the directory bsrep to the UI list ID OpenListFileList
  • static void VFSExist();
    // Check if VFS is available => VFSOK=true
  • static void DisplayStr(char *strP,UInt16 temps);
    // Display a string during the time temps
  • static void HRFldDrawField(FieldType* fld);
    // Redraw a low res field in Hires using the same font
Copy and paste this text in a file named avaricum.h or download the sample project (CodeWarrior R6)
/***********************************************************************
*
* Include file to manage Sony CliÈ
*
* www.avaricum.net
*
* COPYLEFT 2002
*
*
***********************************************************************/

#include <PalmOS.h>
#include <SysEvtMgr.h>
#include <FileStream.h>
#include <Font.h>
#include <FontSelect.h>
#include "StarterRsc.h"
#include <SonyCLIE.h>
#include <vfsMgr.h>

// Common variables

static Boolean cliePresent=false;
static UInt16 LibrHR;
static char* lfile=NULL;
static UInt32 Iter1=vfsIteratorStart;
static FileRef dirRef1;
static UInt16 RefNum1;
static char bsrep[256]="/Palm/Launcher";
static MemHandle choice2=NULL;
static Boolean VFSOK=false;

// Available function

static void AppStartSony(void);
// Initialize all constants : To be included in AppStart
static void AppSonyStop(void);
// Release pointers : To be included in AppStop
static void * GetObjectPtr(UInt16 objectID);
// Get a pointer from a resource ID
static MemHandle DupString (char *pText);
// Get an handle from a string
static UInt16 LoadLibHR();
// Load Hires library
static void ListFileRep(UInt16 OpenListFileList);
// Write the content of the directory bsrep to the UI list ID OpenListFileList
static void VFSExist();
// Check if VFS is available => VFSOK=true
static void DisplayStr(char *strP,UInt16 temps);
// Display a string during the time temps
static void HRFldDrawField(FieldType* fld);
// Redraw a low res field in Hires using the same font


// Source code of these functions


static void * GetObjectPtr(UInt16 objectID)
{
FormPtr frmP;

frmP = FrmGetActiveForm();
return FrmGetObjectPtr(frmP, FrmGetObjectIndex(frmP, objectID));
}


static UInt16 LoadLibHR() {

SonySysFtrSysInfoP sonySysFtrSysInfoP;
Err error = 0;
Err status = 0;
UInt16 refNum;
if ((error = FtrGet(sonySysFtrCreator,
sonySysFtrNumSysInfoP, (UInt32*)&sonySysFtrSysInfoP))) {

} else {
if (sonySysFtrSysInfoP->libr & sonySysFtrSysInfoLibrHR) {
/* HR available */
if ((error = SysLibFind(sonySysLibNameHR, &refNum))){
if (error == sysErrLibNotFound) {
/* couldn't find lib */
error = SysLibLoad( 'libr', sonySysFileCHRLib, &refNum );
cliePresent=true;
}
}
if (!error ) {
/* Now we can use HR lib */

}
}
}
return refNum;
}


static void ListFileRep(UInt16 OpenListFileList) {


UInt16 nchoice;


Boolean latestVer=false;
UInt16 card=0;
LocalID currentDB = 0;
UInt16 pos0=0;
static char tmpname[256]="";

UInt16 iii;
static char StrLow[256]="";


UInt32 Iter=vfsIteratorStart;

FileInfoType info;
char filename[256];

Iter1=vfsIteratorStart;
nchoice=0;

StrCopy(lfile,"");
nchoice=0;


VFSVolumeEnumerate(&RefNum1,&Iter);

VFSFileOpen(RefNum1,bsrep,vfsModeRead,&dirRef1);

Iter1=vfsIteratorStart;

iii=0;
info.nameP=filename;info.nameBufLen=sizeof(filename);
pos0=0;
while ((Iter1 !=vfsIteratorStop) && (nchoice<99)) {
iii++;

VFSDirEntryEnumerate(dirRef1,&Iter1,&info);
if ((info.attributes & vfsFileAttrDirectory)) {StrCopy(tmpname," ");tmpname[0]=26; } else StrCopy(tmpname,"");

StrCat(tmpname,info.nameP);


StrCat(lfile,tmpname);StrCat(lfile,"\n");

pos0=pos0+StrLen(tmpname)+1;nchoice++;}


if(nchoice==0) { StrCopy(tmpname,"Empty");

StrCat(lfile,tmpname);StrCat(lfile,"\n");

pos0=pos0+StrLen(tmpname)+1;nchoice++;}


for(iii=0;iii<=pos0;iii++) if (lfile[iii]=='\n') lfile[iii]='\0';

choice2=SysFormPointerArrayToStrings(lfile,nchoice);

LstSetListChoices(GetObjectPtr(OpenListFileList),MemHandleLock(choice2),nchoice);
LstDrawList(GetObjectPtr(OpenListFileList));

}

static MemHandle DupString (char *pText)
{
MemHandle hNew;

if (pText == NULL) // Bad arg
return (NULL);

if ((hNew = MemHandleNew(StrLen(pText) + 1)) == NULL) // No memory
return (NULL);

StrCopy (MemHandleLock(hNew), pText);
MemHandleUnlock (hNew);

return ((MemHandle)hNew);
}

static void VFSExist() {
UInt32 vfsMgrVersion;
Err err;
err = FtrGet(sysFileCVFSMgr, vfsFtrIDVersion,
&vfsMgrVersion);
if (err){
VFSOK=false;


} else {
VFSOK=true;

}

}


static void DisplayStr(char *strP,UInt16 temps)
{
UInt16 lineW, lineH;
WinHandle drawH, dispH, saveH;
RectangleType bounds;
UInt16 x, y;
UInt16 len;
UInt16 err;

len = StrLen(strP);
lineW = FntLineWidth(strP, len);
lineH = FntLineHeight();

x = bounds.topLeft.x = ((148 - lineW) / 2) - 1;
y = bounds.topLeft.y = ((148 - lineH) / 2) - 1;
bounds.extent.x = lineW + 12;
bounds.extent.y = lineH + 12;

drawH = WinGetDrawWindow();
dispH = WinGetDisplayWindow();
(void) WinSetDrawWindow(dispH);
saveH = WinSaveBits(&bounds, &err);

WinDrawRectangle(&bounds, 5);
bounds.topLeft.x += 2;
bounds.topLeft.y += 2;
bounds.extent.x -= 4;
bounds.extent.y -= 4;
WinEraseRectangle(&bounds, 5);

WinDrawChars(strP, len, x+6, y+6);
SysTaskDelay(temps);
WinRestoreBits(saveH, x, y);
WinSetDrawWindow(drawH);
}


static void AppStartSony(void)
{

UInt32 width, height;
Err error;
UInt32 val;
UInt32 Iter=vfsIteratorStart;

lfile=MemPtrNew(32*100);

if(!FtrGet(sysFtrCreator, sysFtrNumOEMCompanyID, &val)) {
if (val == sonyHwrOEMCompanyID_Sony) {
/* device might be CLIE */

LibrHR=LoadLibHR();

error = HROpen(LibrHR);
width = hrWidth;
height = hrHeight;
error = HRWinScreenMode ( LibrHR, winScreenModeSet,
&width, &height, NULL, NULL );
cliePresent=true;

} else {


/* device might not be CLIE */
}
} else {
/* something wrong ... */
}

VFSExist();
if(VFSOK) VFSVolumeEnumerate(&RefNum1,&Iter);
}


static void HRFldDrawField(FieldType* fld) {

RectangleType dimF;
FontID font;
char Txt[1000];
char tmpc[1000];
UInt16 nlines,i,j,ww;

FldGetBounds(fld,&dimF);
font=FldGetFont(fld);
StrCopy(Txt,FldGetTextPtr(fld));
FldReleaseFocus(fld);
FldSetUsable(fld,false);
dimF.extent.x=dimF.extent.x*2;dimF.extent.y=dimF.extent.y*2;
dimF.topLeft.x=dimF.topLeft.x*2;dimF.topLeft.y=dimF.topLeft.y*2;
HRWinEraseRectangle(LibrHR,&dimF, 0);
HRFntSetFont(LibrHR,font);
nlines=dimF.extent.x/FntCharHeight();

ww=0;i=0;
while((StrLen(Txt)>0) && (i<=nlines)){
ww=FntWordWrap(Txt,dimF.extent.x);
if(StrLen(Txt)<=ww) {StrCopy(tmpc,Txt);
HRWinDrawChars(LibrHR,tmpc,StrLen(tmpc),dimF.topLeft.x,dimF.topLeft.y);}
else {StrNCopy(tmpc,Txt,ww);tmpc[ww+1]=0;HRWinDrawChars(LibrHR,tmpc,StrLen(tmpc)-1,dimF.topLeft.x,dimF.topLeft.y);}

dimF.topLeft.y=dimF.topLeft.y+FntCharHeight();
i++;
for(j=ww;j<StrLen(Txt)+1;j++) Txt[j-ww]=Txt[j];}


}


static void AppSonyStop(){


DisplayStr("Release Ptr:lfile",50);
if(lfile!=NULL) MemPtrFree(lfile);
}