checkin from personal archive

This commit is contained in:
ehouse 2003-11-16 19:37:12 +00:00
parent c4e5a4927e
commit 3dc9c8520d
12 changed files with 7017 additions and 0 deletions

33
xwords4/palm/ownerhash.h Normal file
View file

@ -0,0 +1,33 @@
/* -*-mode: C; fill-column: 77; c-basic-offset: 4; -*- */
/*
* Copyright 1999 - 2002 by Eric House (fixin@peak.org). All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _OWNERHASH_H_
static unsigned long
my_hash( unsigned char* str ) {
unsigned char ch;
unsigned long result = 0;
while ( ( ch = *str++ ) != '\0' ) {
result += result << 2 ^ ~ch;
}
return result;
} /* my_hash */
#define HASH(s) my_hash(s)
#endif

340
xwords4/palm/palmdict.c Normal file
View file

@ -0,0 +1,340 @@
/* -*-mode: C; fill-column: 78; c-basic-offset: 4;-*- */
/*
* Copyright 1997-2001 by Eric House (fixin@peak.org). All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <PalmTypes.h>
#include <DataMgr.h>
#include <VFSMgr.h>
#include "dictnryp.h"
#include "dawg.h"
#include "palmdict.h"
#include "dictlist.h"
#include "dictui.h"
typedef struct DictStart {
unsigned long indexStart;
array_edge* array;
} DictStart;
#define NO_REFCOUNT -2
struct PalmDictionaryCtxt {
DictionaryCtxt super;
dawg_header* headerRecP;
DmOpenRef dbRef;
XP_U16 nRecords;
UInt16 cardNo; /* added to track VFS imported file for */
LocalID dbID; /* deletion later */
XP_UCHAR location; /* VFS or storage mem */
XP_S8 refCount;
PalmDictList* dl;
DictStart dictStarts[1];
};
static void palm_dictionary_destroy( DictionaryCtxt* dict );
static XP_U16 countSpecials( unsigned char* ptr, UInt16 nChars );
static void setupSpecials( MPFORMAL PalmDictionaryCtxt* ctxt,
Xloc_specialEntry* specialStart, XP_U16 nSpecials );
static void unlockAndRelease( MemPtr p, DmOpenRef dbRef, short recIndex );
DictionaryCtxt*
palm_dictionary_make( MPFORMAL XP_UCHAR* dictName, PalmDictList* dl )
{
Boolean found;
UInt16 cardNo;
LocalID dbID;
DmOpenRef dbRef;
PalmDictionaryCtxt tDictBuf;
PalmDictionaryCtxt* ctxt;
MemHandle tmpH;
dawg_header* headerRecP;
unsigned char* charPtr;
UInt16 nChars, nSpecials;
unsigned long offset = 0;
DictListEntry* dle;
Err err;
XP_U16 i;
/* check and see if there's already a dict for this name. If yes,
increment its refcount and return. */
if ( !!dictName && getDictWithName( dl, dictName, &dle ) ) {
PalmDictionaryCtxt* dict = (PalmDictionaryCtxt*)dle->dict;
if ( !!dict ) {
++dict->refCount;
return (DictionaryCtxt*)dict;
}
}
if ( !!dictName ) {
ctxt = &tDictBuf;
} else {
/* If the name's NULL, we still need to create a dict, as the server
may be called to fill it in from the stream later. */
ctxt = XP_MALLOC( mpool, sizeof(*ctxt) );
}
XP_MEMSET( ctxt, 0, sizeof(*ctxt) );
MPASSIGN( ctxt->super.mpool, mpool );
if ( !!dictName ) {
XP_ASSERT( XP_STRLEN((const char*)dictName) > 0 );
ctxt->super.name = dictName;
ctxt->super.destructor = palm_dictionary_destroy;
found = getDictWithName( dl, dictName, &dle );
if ( !found ) {
goto errExit;
}
if ( dle->location == DL_VFS ) {
err = VFSImportDatabaseFromFile( dle->u.vfsData.volNum,
(const char*)dle->path,
&cardNo, &dbID );
if ( err != errNone ) {
goto errExit;
}
} else {
cardNo = dle->u.dmData.cardNo;
dbID = dle->u.dmData.dbID;
}
ctxt->refCount = 1;
ctxt->dl = dl;
ctxt->dbID = dbID;
ctxt->cardNo = cardNo;
ctxt->location = dle->location;
ctxt->dbRef = dbRef = DmOpenDatabase( cardNo, dbID, dmModeReadOnly );
tmpH = DmGetRecord( dbRef, 0 ); // <- <eeh> should be a constant
ctxt->headerRecP = headerRecP = (dawg_header*)MemHandleLock( tmpH );
XP_ASSERT( MemHandleLockCount(tmpH) == 1 );
tmpH = DmQueryRecord( dbRef, headerRecP->charTableRecNum );
XP_ASSERT( !!tmpH );
charPtr = (unsigned char*)MemHandleLock( tmpH );
XP_ASSERT( MemHandleLockCount( tmpH ) == 1 );
ctxt->super.nFaces = nChars = MemPtrSize(charPtr);
ctxt->super.faces16 =
XP_MALLOC( mpool, nChars * sizeof(ctxt->super.faces16[0]));
XP_ASSERT( !!ctxt->super.faces16 );
for ( i = 0; i < nChars; ++i ) {
ctxt->super.faces16[i] = charPtr[i];
}
nSpecials = countSpecials( charPtr, nChars );
unlockAndRelease( charPtr, dbRef, headerRecP->charTableRecNum );
tmpH = DmGetRecord( dbRef, headerRecP->valTableRecNum );
charPtr = (unsigned char*)MemHandleLock(tmpH);
XP_ASSERT( MemHandleLockCount( tmpH ) == 1 );
ctxt->super.countsAndValues = charPtr + sizeof(Xloc_header);
/* for those dicts with special chars */
if ( nSpecials > 0 ) {
Xloc_specialEntry* specialStart = (Xloc_specialEntry*)
(ctxt->super.countsAndValues + (ctxt->super.nFaces*2));
setupSpecials( MPPARM(mpool) ctxt, specialStart, nSpecials );
}
if ( headerRecP->firstEdgeRecNum == 0 ) { /* emtpy dict */
ctxt->super.topEdge = NULL;
ctxt->nRecords = 0;
} else {
short index;
XP_U16 nRecords;
nRecords = DmNumRecords(dbRef) - headerRecP->firstEdgeRecNum;
/* alloacate now that we know the size. */
ctxt = XP_MALLOC( mpool,
sizeof(*ctxt) + (nRecords * sizeof(DictStart)));
XP_MEMCPY( ctxt, &tDictBuf, sizeof(tDictBuf) );
ctxt->nRecords = nRecords;
for ( index = 0; index < nRecords; ++index ) {
MemHandle record =
DmGetRecord( dbRef, index + headerRecP->firstEdgeRecNum );
ctxt->dictStarts[index].indexStart = offset;
/* cast to short to avoid libc call */
XP_ASSERT( MemHandleSize(record) < 0xFFFF );
XP_ASSERT( ((unsigned short)(MemHandleSize(record)) % 3 ) == 0);
offset += ((unsigned short)MemHandleSize(record)) / 3;
ctxt->dictStarts[index].array =
(array_edge*)MemHandleLock( record );
XP_ASSERT( MemHandleLockCount(record) == 1 );
}
XP_ASSERT( index == ctxt->nRecords );
ctxt->dictStarts[index].indexStart = 0xFFFFFFFFL;
ctxt->super.topEdge = ctxt->dictStarts[0].array;
}
setBlankTile( (DictionaryCtxt*)ctxt );
cacheDictForName( dl, dictName, (DictionaryCtxt*)ctxt );
} else {
ctxt->refCount = NO_REFCOUNT;
}
return (DictionaryCtxt*)ctxt;
errExit:
if ( !!ctxt ) {
XP_ASSERT( ctxt == &tDictBuf );
}
return NULL;
} /* palm_dictionary_make */
static XP_U16
countSpecials( unsigned char* ptr, UInt16 nChars )
{
XP_U16 result = 0;
while ( nChars-- ) {
unsigned char face = *ptr++;
if ( IS_SPECIAL(face) ) {
++result;
}
}
return result;
} /* countSpecials */
/* We need an array of ptrs to bitmaps plus an array of ptrs to the text
* equivalents.
*/
static void
setupSpecials( MPFORMAL PalmDictionaryCtxt* ctxt,
Xloc_specialEntry* specialStart, XP_U16 nSpecials )
{
XP_U16 i;
char* base = (char*)specialStart;
XP_UCHAR** chars = XP_MALLOC( mpool, nSpecials * sizeof(*chars) );
SpecialBitmaps* bitmaps = XP_MALLOC( mpool, nSpecials * sizeof(*bitmaps) );
XP_MEMSET( bitmaps, 0, nSpecials * sizeof(*bitmaps ) );
for ( i = 0; i < nSpecials; ++i ) {
chars[i] = specialStart->textVersion;
if ( specialStart->hasLarge ) {
bitmaps[i].largeBM = base + specialStart->hasLarge;
}
if ( specialStart->hasSmall ) {
bitmaps[i].smallBM = base + specialStart->hasSmall;
}
++specialStart;
}
ctxt->super.bitmaps = bitmaps;
ctxt->super.chars = chars;
} /* setupSpecials */
static void
unlockAndRelease( MemPtr p, DmOpenRef dbRef, short recIndex )
{
MemPtrUnlock( p );
DmReleaseRecord( dbRef, recIndex, false );
} /* unlockAndRelease */
static void
palm_dictionary_destroy( DictionaryCtxt* dict )
{
PalmDictionaryCtxt* ctxt = (PalmDictionaryCtxt*)dict;
if ( ctxt->refCount != NO_REFCOUNT && --ctxt->refCount == 0 ) {
short i;
DmOpenRef dbRef;
dawg_header* headerRecP;
if ( !!ctxt->super.name ) {
/* Remove from dict list */
removeFromDictCache( ctxt->dl, ctxt->super.name, dict );
}
dbRef = ctxt->dbRef;
headerRecP = ctxt->headerRecP;
XP_ASSERT( !!dbRef );
unlockAndRelease( ctxt->super.countsAndValues - sizeof(Xloc_header),
dbRef, headerRecP->valTableRecNum );
XP_FREE( dict->mpool, ctxt->super.faces16 );
for ( i = 0; i < ctxt->nRecords; ++i ) {
unlockAndRelease( ctxt->dictStarts[i].array,
dbRef, i + headerRecP->firstEdgeRecNum );
}
unlockAndRelease( headerRecP, dbRef, 0 );
DmCloseDatabase( dbRef );
if ( !!ctxt->super.name ) {
XP_FREE( dict->mpool, ctxt->super.name );
}
if ( !!ctxt->super.bitmaps ) {
XP_FREE( dict->mpool, ctxt->super.bitmaps );
}
if ( !!ctxt->super.chars ) {
XP_FREE( dict->mpool, ctxt->super.chars );
}
/* if we copied the db to memory we should nuke it, since user would
have copied it himself if he wanted it here permanently */
if ( ctxt->location == DL_VFS ) {
DmDeleteDatabase( ctxt->cardNo, ctxt->dbID );
}
XP_FREE( dict->mpool, dict );
}
} /* palm_dictionary_destroy */
#ifdef OVERRIDE_EDGE_FOR_INDEX
array_edge*
dict_edge_for_index( DictionaryCtxt* dict, XP_U32 index )
{
PalmDictionaryCtxt* ctxt = (PalmDictionaryCtxt*)dict;
array_edge* result;
if ( index == 0 ) {
result = NULL;
} else {
DictStart* headerP;
for ( headerP = &ctxt->dictStarts[1];
index >= headerP->indexStart;
++headerP ) {
/* do nothing */
}
--headerP;
index -= headerP->indexStart;
#ifdef NODE_CAN_4
index *= dict->nodeSize;
#else
index *= 3;
#endif
result = headerP->array + index;
}
return result;
} /* dict_edge_for_index */
#endif

34
xwords4/palm/palmdict.h Normal file
View file

@ -0,0 +1,34 @@
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */
/*
* Copyright 2001 by Eric House (fixin@peak.org). All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _PALMDICT_H_
#define _PALMDICT_H_
#include "dictnry.h"
#include "mempool.h"
#include "dictui.h"
DictionaryCtxt* palm_dictionary_make( MPFORMAL XP_UCHAR* dictName,
PalmDictList* dl );
/* might eventually belong in the superclass */
XP_UCHAR* dict_getName( DictionaryCtxt* dict );
typedef struct PalmDictionaryCtxt PalmDictionaryCtxt;
#endif /* _PALMDICT_H_ */

1120
xwords4/palm/palmdraw.c Normal file

File diff suppressed because it is too large Load diff

1048
xwords4/palm/palmir.c Normal file

File diff suppressed because it is too large Load diff

108
xwords4/palm/palmir.h Normal file
View file

@ -0,0 +1,108 @@
/* -*-mode: C; fill-column: 77; c-basic-offset: 4; -*- */
/*
* Copyright 1999 - 2001 by Eric House (fixin@peak.org). All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _PALMIR_H_
#define _PALMIR_H_
#include "comms.h"
#include "palmmain.h"
/* IR strategy. We'll attempt to keep the IR link up, but won't bring it up
* in the first place, or fix it once it goes down, until there's something
* to send.
*
* So when there's a packet to send, if the link is up
* (state==IR_STATE_CANDODATA) we just send it. Otherwise we do what's
* needed to get to that state. What state we're actually in (in the
* difficult case where we brought the link up before but the players have
* had their machines separated and the link's died) will, I hope, be
* tracable by the messages passed to each device's callback.
*
* This isn't going to make a game between three or four devices very easy
* to manage, but perhaps that will have to wait until whatever's wrong with
* shutting down and restarting links gets fixed.
*/
enum { /* values for IR_STATE */
IR_STATE_NONE = 0, /* nothing's up or been done */
IR_STATE_DISCOVERY_COMPLETE = 1,/* nothing to do til there's a message to
send */
/* Discovery */
IR_STATE_NOTHING_TO_DO = 2, /* exists just for testing against */
IR_STATE_NO_OTHER_FOUND = 3, /* first discovery attempt failed */
IR_STATE_DO_DISCOVERY = 4,
IR_STATE_REDO_DISCOVERY = 5,
IR_STATE_DISCOVERY_SENT = 6,
IR_STATE_DOLAP = 7,
/* IR Lap */
IR_STATE_LAP_SENT = 8,
IR_STATE_LAP_ESTAB = 9,//was IR_STATE_DOLMP,
/* LMP */
/* IR_STATE_CONNECTREQ_SENT, */
IR_STATE_LMPREQ_SENT = 10, /* was IR_STATE_CONNECTREQ_SENT */
IR_STATE_LMP_ESTAB = 11, //was IR_STATE_SEND_READY,
/* Send */
IR_STATE_SEND_DONE = 12,
IR_STATE_CAN_DISCONNECT = 13,
/* receive (doesn't require even discovery) */
IR_STATE_CONN_RECD = 14, /* triggered by LEVENT_LAP_CON_IND */
IR_STATE_LAP_RCV = 15, //was IR_STATE_DOLMP_RCV,
IR_STATE_LMPRCV_REQ_SENT = 16,
IR_STATE_CONN_INCOMMING = 17, /* triggered by LEVENT_LM_CON_IND */
IR_STATE_MESSAGE_RECD = 18
};
#define ir_work_exists(g) \
((g)->ir_state > IR_STATE_NOTHING_TO_DO || (getSendQueueHead(g)!=NULL))
MyIrPacket* getSendQueueHead( PalmAppGlobals* globals );
void ir_callback_out( IrConnect* con, IrCallBackParms* parms );
Boolean ir_do_work( PalmAppGlobals* globals );
void ir_show_status( PalmAppGlobals* globals );
void ir_cleanup( PalmAppGlobals* globals );
/* CommsCtxt send callback; a driver of sorts. Switches off global state to
decide how to actually send the message. */
XP_S16 palm_send( XP_U8* buf, XP_U16 len, CommsAddrRec* addr, void* closure );
void palm_send_on_close( XWStreamCtxt* stream, void* closure );
void receiveMove( ExgSocketPtr cmdPBP );
XP_Bool loadReceivedMove( PalmAppGlobals* globals, MemHandle moveData );
void palm_ir_receiveMove( PalmAppGlobals* globals, ExgSocketPtr socket );
void palm_ip_close( PalmAppGlobals* globals );
#ifdef BEYOND_IR
void checkHandleNetEvents( PalmAppGlobals* globals );
XP_Bool openNetLibIfNot( PalmAppGlobals* globals );
void palm_bind_socket( PalmAppGlobals* globals, XP_U16 newPort );
#endif
#ifdef XWFEATURE_STANDALONE_ONLY
# define palm_ir_send (TransportSend)NULL
#endif
#endif

3041
xwords4/palm/palmmain.c Normal file

File diff suppressed because it is too large Load diff

327
xwords4/palm/palmmain.h Normal file
View file

@ -0,0 +1,327 @@
/* -*-mode: C; fill-column: 76; c-basic-offset: 4; -*- */
/*
* Copyright 1999 - 2003 by Eric House (fixin@peak.org). All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _PALMMAIN_H_
#define _PALMMAIN_H_
#define AppType APPID
#define PrefID 0
#define VERSION_NUM 1
#include <PalmTypes.h>
#include <DataMgr.h>
#include <Window.h>
#include <List.h>
#include <Form.h>
#include <IrLib.h>
#ifdef BEYOND_IR
# include <NetMgr.h>
#endif
#include "game.h"
#include "util.h"
#include "mempool.h"
/* #include "prefsdlg.h" */
#include "xwcolors.h"
#include "plmtraps.h" /* if DIRECT_PALMOS_CALLS */
#include "xwords4defines.h"
#ifdef MEM_DEBUG
# define MEMPOOL globals->mpool,
#else
# define MEMPOOL
#endif
enum { ONEBIT, /* GREYSCALE, */COLOR };
typedef unsigned char GraphicsAbility; /* don't let above be 4 bytes */
typedef struct PalmAppGlobals PalmAppGlobals;
typedef XP_UCHAR* (*GetResStringFunc)( PalmAppGlobals* globals,
XP_U16 strID );
typedef struct PalmDrawCtx {
DrawCtxVTable* vtable;
PalmAppGlobals* globals;
void (*drawBitmapFunc)( DrawCtx* dc, Int16 resID, Int16 x, Int16 y );
GetResStringFunc getResStrFunc;
DrawingPrefs* drawingPrefs;
#ifdef DIRECT_PALMOS_CALLS
DrawRectFrameProc winDrawRectangleFrameTrap;
FillRectangleProc winFillRectangleTrap;
DrawCharsProc winDrawCharsTrap;
#endif
RectangleType oldScoreClip;
RectangleType oldTrayClip;
WinHandle numberWin;
XP_S16 trayOwner;
GraphicsAbility able;
union {
struct {
/* IndexedColorType black; */
/* IndexedColorType white; */
/* IndexedColorType playerColors[MAX_NUM_PLAYERS]; */
/* IndexedColorType bonusColors[BONUS_LAST-1]; */
XP_U8 reserved; /* make CW compiler happy */
} clr;
struct {
CustomPatternType valuePatterns[4];
} bnw;
} u;
MPSLOT
} PalmDrawCtx;
#define draw_drawBitmapAt(dc,id,x,y) \
(*((((PalmDrawCtx*)dc))->drawBitmapFunc))((dc),(id),(x),(y))
typedef struct ListData {
char** strings;
XP_U16 nItems;
XP_U16 nextIndex;
XP_S16 selIndex;
} ListData;
typedef struct XWords4PreferenceType {
Int16 versionNum;
Int16 curGameIndex; /* which game is currently open */
/* these are true global preferences */
Boolean showProgress;
Boolean showGrid;
Boolean showColors;
#ifndef DEBUG_OFF
Boolean showDebugstrs;
Boolean windowAvail;
Boolean logToMemo;
#endif
/* New for 0x0405 */
CommonPrefs cp;
} XWords4PreferenceType;
typedef struct MyIrConnect {
IrConnect irCon;
PalmAppGlobals* globals;
} MyIrConnect;
typedef XP_U8 IR_STATE; /* enums are in palmir.h */
#define IR_BUF_SIZE 256
typedef struct MyIrPacket MyIrPacket;
typedef struct ProgressCtxt {
RectangleType boundsRect;
XP_U16 curLine;
} ProgressCtxt;
/* I *hate* having to define these globally... */
typedef struct SavedGamesState {
struct PalmAppGlobals* globals;
FormPtr form;
ListPtr gamesList;
FieldPtr nameField;
char** stringPtrs;
Int16 nStrings;
Int16 displayGameIndex;
} SavedGamesState;
typedef struct PrefsDlgState {
ListPtr playerBdSizeList;
ListPtr phoniesList;
CommonPrefs cp;
XP_U16 gameSeconds;
XP_Bool stateTypeIsGlobal;
XP_U8 phoniesAction;
XP_U8 curBdSize;
XP_Bool showColors;
XP_Bool smartRobot;
XP_Bool showProgress;
XP_Bool showGrid;
XP_Bool hintsNotAllowed;
XP_Bool timerEnabled;
} PrefsDlgState;
typedef struct DictState {
ListPtr dictList;
ListData sLd;
XP_U16 nDicts;
} DictState;
typedef struct ConnDlgAddrs {
XP_U32 remoteIP;
XP_U16 remotePort;
XP_U16 localPort;
CommsConnType conType;
} ConnDlgAddrs;
typedef struct PalmNewGameState {
ListPtr playerNumList;
XP_UCHAR* passwds[MAX_NUM_PLAYERS];
XP_UCHAR* dictName;
XP_Bool isLocal[MAX_NUM_PLAYERS];
XP_Bool isRobot[MAX_NUM_PLAYERS];
XP_UCHAR shortDictName[32]; /* as long as a dict name can be */
XP_U8 curNPlayersLocal;
XP_U8 curNPlayersTotal;
XP_Bool forwardChange;
Connectedness curServerHilite;
#ifdef BEYOND_IR
ConnDlgAddrs connAddrs;
XP_Bool connsSettingChanged;
#endif
} PalmNewGameState;
typedef struct PalmDictList PalmDictList;
#ifdef BEYOND_IR
typedef struct NetLibStuff {
UInt16 netLibRef;
XP_U16 listenPort;
NetSocketRef socketRef;
} NetLibStuff;
#define socketIsOpen(g) ((g)->nlStuff.listenPort != 0)
#endif
#define MAX_DLG_PARAMS 2
struct PalmAppGlobals {
FormPtr mainForm;
PrefsDlgState* prefsDlgState;
#if defined OWNER_HASH || defined NO_REG_REQUIRED
SavedGamesState* savedGamesState;
#endif
XWGame game;
DrawCtx* draw;
XW_UtilCtxt util;
XP_U32 dlgParams[MAX_DLG_PARAMS];
VTableMgr* vtMgr;
XWords4PreferenceType gState;
DrawingPrefs drawingPrefs;
PalmDictList* dictList;
DmOpenRef boardDBP;
LocalID boardDBID;
DmOpenRef gamesDBP;
LocalID gamesDBID;
#ifdef BEYOND_IR
UInt16 exgLibraryRef; /* what library did user choose for sending? */
#endif
XP_UCHAR* stringsResPtr;
XP_U8* bonusResPtr[NUM_BOARD_SIZES];
Boolean penDown;
Boolean isNewGame;
Boolean stateTypeIsGlobal;
Boolean timeRequested;
Boolean hintPending;
Boolean isLefty;
Boolean dictuiForBeaming;
Boolean postponeDraw;
Boolean needsScrollbar;
Boolean msgReceivedDraw;
Boolean isFirstLaunch;
Boolean menuIsDown;
XP_Bool newGameIsNew;
GraphicsAbility able;
XP_U32 penTimerFireAt;
XP_U32 timerTimerFireAt;
XP_U16 prevScroll; /* for scrolling in 'ask' dialog */
UInt16 romVersion;
XP_U8 scrollValue; /* 0..2: scrolled position of board */
#ifdef SHOW_PROGRESS
ProgressCtxt progress;
#endif
CurGameInfo gameInfo; /* for the currently open, or new, game */
/* dialog/forms state */
PalmNewGameState newGameState;
DictState dictState;
struct ConnsDlgState* connState;
#ifdef BEYOND_IR
NetLibStuff nlStuff;
#endif
#ifdef DEBUG
UInt8 save_rLsap;
IR_STATE ir_state_prev;
XP_U16 yCount;
/* Boolean resetGame; */
#endif
MPSLOT
}; /* PalmAppGlobals */
/* custom events */
enum { dictSelectedEvent = firstUserEvent /* 0x6000 */
,newGameOkEvent
,newGameCancelEvent
,loadGameEvent
,boardRedrawEvt
,prefsChangedEvent
,openSavedGameEvent
#ifdef BEYOND_IR
,connsSettingChgEvent
#endif
};
DrawCtx* palm_drawctxt_make( MPFORMAL GraphicsAbility able,
PalmAppGlobals* globals,
GetResStringFunc getRSF,
DrawingPrefs* drawprefs );
void palm_drawctxt_destroy( DrawCtx* dctx );
void palm_warnf( char* format, ... );
Boolean askPassword( PalmAppGlobals* globals, const XP_UCHAR* name,
Boolean isNew, XP_UCHAR* retbuf, XP_U16* len );
Boolean palmaskFromStrId( PalmAppGlobals* globals, XP_U16 strId,
XP_S16 titleID, XP_S16 altID );
void freeSavedGamesData( MPFORMAL SavedGamesState* state );
void palm_util_requestTime( XW_UtilCtxt* uc ); /* so palmir can call */
void writeNameToGameRecord( PalmAppGlobals* globals, XP_S16 index,
char* newName, XP_U16 len );
#endif /* _PALMMAIN_H_ */

293
xwords4/palm/palmsavg.c Normal file
View file

@ -0,0 +1,293 @@
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */
/****************************************************************************
* *
* Copyright 1999, 2001 by Eric House (fixin@peak.org). All rights reserved. *
* *
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
****************************************************************************/
#if defined OWNER_HASH || defined NO_REG_REQUIRED
#include <PalmTypes.h>
#include <Form.h>
#include <DLServer.h>
#include "callback.h"
#include "strutils.h"
#include "palmsavg.h"
#include "palmmain.h"
#include "palmutil.h"
#include "gameutil.h"
#include "ownerhash.h"
#include "xwords4defines.h"
#include "LocalizedStrIncludes.h"
/* Prototypes */
static void populateGameList( SavedGamesState* state );
static void setFieldToSelText( SavedGamesState* state );
static void listDrawFunc(Int16 index, RectanglePtr bounds, char** itemsText);
#ifndef NO_REG_REQUIRED
Boolean
checkUserName()
{
#ifdef FOR_GREMLINS
return true;
#else
XP_UCHAR ownerName[dlkMaxUserNameLength + 1];
Boolean result;
Err err;
ownerName[0] = '\0';
/* Try and use the user's name */
err = DlkGetSyncInfo( NULL, NULL, NULL, ownerName, NULL, NULL );
XP_ASSERT( err == 0 );
result = HASH(ownerName)==((unsigned long)OWNER_HASH);
return result;
#endif
} /* checkUserName */
#endif
/*****************************************************************************
* Handler for dictionary info form.
****************************************************************************/
#define USE_POPULATE 1
Boolean
savedGamesHandleEvent( EventPtr event )
{
Boolean result;
PalmAppGlobals* globals;
SavedGamesState* state;
EventType eventToPost;
XP_S16 newGameIndex;
Int16* curGameIndexP;
char* newName;
CALLBACK_PROLOGUE();
result = false;
globals = getFormRefcon();
state = globals->savedGamesState;
curGameIndexP = &globals->gState.curGameIndex;
switch ( event->eType ) {
case frmOpenEvent:
if ( !state ) {
state = globals->savedGamesState = XP_MALLOC( globals->mpool,
sizeof(*state) );
}
XP_MEMSET( state, 0, sizeof(*state) );
state->globals = globals;
state->form = FrmGetActiveForm();
/* dictionary list setup */
state->gamesList = getActiveObjectPtr( XW_SAVEDGAMES_LIST_ID );
state->nameField = getActiveObjectPtr( XW_SAVEDGAMES_NAME_FIELD );
state->displayGameIndex = globals->gState.curGameIndex;
XP_ASSERT( state->displayGameIndex < countGameRecords(globals) );
/* must preceed drawing calls so they have a valid window */
FrmDrawForm( state->form );
/* LstSetDrawFunction must follow FrmDrawForm since populateGameList
must be called before listDrawFunc has valid globals; can't let
listDrawFunc get called from FrmDrawForm until set up correctly. */
LstSetDrawFunction( state->gamesList, listDrawFunc );
populateGameList( state );
setFieldToSelText( state );
break;
case frmUpdateEvent:
FrmDrawForm( state->form );
/* don't update field here! The keyboard may have been the form whose
disappearance triggered the frmUpdateEvent, and resetting the field
would undo the user's edits. Also causes a crash in Keyboard.c in
the ROMs for reasons I don't understand. */
break;
case lstSelectEvent:
state->displayGameIndex = LstGetSelection( state->gamesList );
setFieldToSelText( state );
result = true;
break;
case ctlSelectEvent:
result = true;
switch ( event->data.ctlEnter.controlID ) {
case XW_SAVEDGAMES_USE_BUTTON: /* write the new name to the selected
record */
newName = FldGetTextPtr( state->nameField );
if ( !!newName &&
(*newName != '\0') &&
0 != StrCompare(newName,
state->stringPtrs[state->displayGameIndex])){
XP_U16 len = FldGetTextLength( state->nameField );
writeNameToGameRecord( globals, state->displayGameIndex,
newName, len );
populateGameList( state );
setFieldToSelText( state );
}
break;
case XW_SAVEDGAMES_DUPE_BUTTON: /* copy the selected record */
newGameIndex = duplicateGameRecord( globals,
state->displayGameIndex );
state->displayGameIndex = newGameIndex;
if ( *curGameIndexP >= newGameIndex ) {
++*curGameIndexP;
}
populateGameList( state );
setFieldToSelText( state );
break;
case XW_SAVEDGAMES_DELETE_BUTTON: /* delete the selected record.
Refuse if it's open. */
if ( state->displayGameIndex == *curGameIndexP ) {
beep();
} else if ( palmaskFromStrId( globals, STR_CONFIRM_DEL_GAME,
-1, -1 ) ) {
XP_S16 index = state->displayGameIndex;
deleteGameRecord( globals, index );
if ( *curGameIndexP > index ) {
--*curGameIndexP;
}
if ( index == countGameRecords(globals) ) {
--index;
}
state->displayGameIndex = index;
populateGameList( state );
}
break;
case XW_SAVEDGAMES_OPEN_BUTTON: /* open the selected db if not already
open. */
if ( *curGameIndexP != state->displayGameIndex ) {
eventToPost.eType = openSavedGameEvent;
((OpenSavedGameData*)&eventToPost.data.generic)->newGameIndex
= state->displayGameIndex;
EvtAddEventToQueue( &eventToPost );
globals->postponeDraw = true;
}
case XW_SAVEDGAMES_DONE_BUTTON:
/* Update the app's idea of which record to save the current game
into -- in case any with lower IDs have been deleted. */
FrmReturnToForm( 0 );
freeSavedGamesData( MPPARM(globals->mpool) state );
break;
}
default:
break;
} /* switch */
CALLBACK_EPILOGUE();
return result;
} /* savedGamesHandleEvent */
static void
setFieldToSelText( SavedGamesState* state )
{
FieldPtr field = state->nameField;
char name[MAX_GAMENAME_LENGTH];
nameFromRecord( state->globals, state->displayGameIndex, name );
XP_ASSERT( XP_STRLEN(name) < MAX_GAMENAME_LENGTH );
XP_ASSERT( XP_STRLEN(name) > 0 );
FldSetSelection( field, 0, FldGetTextLength(field) );
FldInsert( field, name, XP_STRLEN(name) );
FldSetSelection( field, 0, FldGetTextLength(field) );
FrmSetFocus( state->form, FrmGetObjectIndex(state->form,
XW_SAVEDGAMES_NAME_FIELD) );
FldDrawField( field );
} /* setFieldToSelText */
void
freeSavedGamesData( MPFORMAL SavedGamesState* state )
{
if ( !!state->stringPtrs ) {
XP_FREE( mpool, state->stringPtrs );
state->stringPtrs = NULL;
}
} /* freeSavedGamesData */
static void
populateGameList( SavedGamesState* state )
{
XP_U16 nRecords;
PalmAppGlobals* globals = state->globals;
ListPtr gamesList = state->gamesList;
nRecords = countGameRecords( globals );
if ( state->nStrings != nRecords ) {
char** stringPtrs;
XP_U16 i;
LstEraseList( gamesList );
freeSavedGamesData( MPPARM(globals->mpool) state );
state->nStrings = nRecords;
state->stringPtrs = stringPtrs =
XP_MALLOC( globals->mpool,
(nRecords+1) * sizeof(state->stringPtrs[0] ) );
stringPtrs[0] = (char*)globals;
for ( i = 1; i <= nRecords; ++i ) {
stringPtrs[i] = "";
}
LstSetListChoices( gamesList, &state->stringPtrs[1], nRecords );
LstSetHeight( gamesList, XP_MIN(nRecords, 9) );
}
LstSetSelection( gamesList, state->displayGameIndex );
XP_ASSERT( state->displayGameIndex < nRecords );
LstMakeItemVisible( gamesList, state->displayGameIndex );
LstDrawList( gamesList );
} /* populateGameList */
static void
listDrawFunc( Int16 index, RectanglePtr bounds, char** itemsText )
{
char buf[MAX_GAMENAME_LENGTH];
XP_U16 len;
PalmAppGlobals* globals;
CALLBACK_PROLOGUE();
globals = (PalmAppGlobals*)itemsText[-1];
nameFromRecord( globals, index, buf );
len = XP_STRLEN( buf );
XP_ASSERT( len > 0 );
WinDrawChars( buf, len, bounds->topLeft.x, bounds->topLeft.y );
CALLBACK_EPILOGUE();
} /* listDrawFunc */
#endif /* OWNER_HASH */

39
xwords4/palm/palmsavg.h Normal file
View file

@ -0,0 +1,39 @@
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */
/****************************************************************************
* *
* Copyright 1999 - 2001 by Eric House (fixin@peak.org). All rights reserved. *
* *
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
****************************************************************************/
#ifndef _PALMSAVG_H_
#define _PALMSAVG_H_
typedef struct OpenSavedGameData {
Int16 newGameIndex;
} OpenSavedGameData;
Boolean savedGamesHandleEvent( EventPtr event );
#if defined NO_REG_REQUIRED
# define checkUserName() XP_TRUE
#elif defined OWNER_HASH
Boolean checkUserName();
#else
# define checkUserName() XP_FALSE
#endif
#endif

558
xwords4/palm/palmutil.c Normal file
View file

@ -0,0 +1,558 @@
// -*-mode: C; fill-column: 80; c-basic-offset: 4; -*-
/****************************************************************************
* *
* Copyright 1998-2001 by Eric House (fixin@peak.org). All rights reserved. *
* *
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
****************************************************************************/
#include <StringMgr.h>
#include <MemoryMgr.h>
#include <SoundMgr.h>
#include <TimeMgr.h>
#include <Form.h>
#include <unix_stdarg.h>
#include "strutils.h"
#include "palmutil.h"
#include "xwords4defines.h"
#include "palmmain.h"
#include "comtypes.h"
/*****************************************************************************
* This is meant to be replaced by an actual beep....
****************************************************************************/
#if defined FEATURE_BEEP || defined XW_FEATURE_UTILS
void beep( void ) {
SndPlaySystemSound( sndError );
} /* beep */
#endif
/*****************************************************************************
*
****************************************************************************/
MemPtr
getActiveObjectPtr( UInt16 objectID )
{
FormPtr form = FrmGetActiveForm();
XP_ASSERT( FrmGetObjectPtr(
form, FrmGetObjectIndex( form, objectID ) )!= NULL );
return FrmGetObjectPtr( form, FrmGetObjectIndex( form, objectID ) );
} /* getActiveObjectPtr */
/*****************************************************************************
*
****************************************************************************/
void
getObjectBounds( UInt16 objectID, RectangleType* rectP )
{
FormPtr form = FrmGetActiveForm();
FrmGetObjectBounds( form, FrmGetObjectIndex( form, objectID ), rectP );
} /* getObjectBounds */
#if defined XW_FEATURE_UTILS
/*****************************************************************************
*
****************************************************************************/
void
setObjectBounds( UInt16 objectID, RectangleType* rectP )
{
FormPtr form = FrmGetActiveForm();
FrmSetObjectBounds( form, FrmGetObjectIndex( form, objectID ), rectP );
} /* getObjectBounds */
/*****************************************************************************
*
****************************************************************************/
void
setBooleanCtrl( UInt16 objectID, Boolean isSet )
{
CtlSetValue( getActiveObjectPtr( objectID ), isSet );
} /* setBooleanCtrl */
void
setFieldEditable( FieldPtr fld, Boolean editable )
{
FieldAttrType attrs;
FldGetAttributes( fld, &attrs );
if ( attrs.editable != editable ) {
attrs.editable = editable;
FldSetAttributes( fld, &attrs );
}
} /* setFieldEditable */
void
disOrEnable( FormPtr form, UInt16 id, Boolean enable )
{
UInt16 index = FrmGetObjectIndex( form, id );
FormObjectKind typ;
if ( enable ) {
FrmShowObject( form, index );
} else {
FrmHideObject( form, index );
}
typ = FrmGetObjectType( form, index );
if ( typ == frmControlObj ) {
ControlPtr ctl = getActiveObjectPtr( id );
CtlSetEnabled( ctl, enable );
}
} /* disOrEnable */
void
disOrEnableSet( FormPtr form, const UInt16* ids, Boolean enable )
{
for ( ; ; ) {
XP_U16 id = *ids++;
if ( !id ) {
break;
}
disOrEnable( form, id, enable );
}
} /* disOrEnableSet */
/* Position a control at the horizontal center of its container.
*/
void
centerControl( FormPtr form, UInt16 id )
{
RectangleType cBounds, fBounds;
UInt16 index = FrmGetObjectIndex( form, id );
FrmGetObjectBounds( form, index, &cBounds );
FrmGetFormBounds( form, &fBounds );
cBounds.topLeft.x = (fBounds.extent.x - cBounds.extent.x) / 2;
FrmSetObjectBounds( form, index, &cBounds );
} /* centerButton */
/*****************************************************************************
*
****************************************************************************/
Boolean
getBooleanCtrl( UInt16 objectID )
{
return CtlGetValue( getActiveObjectPtr( objectID ) );
} /* getBooleanCtrl */
/*****************************************************************************
* Set up to build the string and ptr-to-string lists needed for the
* LstSetListChoices system call.
****************************************************************************/
void
initListData( MPFORMAL ListData* ld, XP_U16 nItems )
{
/* include room for the closure */
ld->strings = XP_MALLOC( mpool, ((nItems+1) * sizeof(*ld->strings)) );
ld->nItems = nItems;
ld->nextIndex = 0;
ld->selIndex = -1;
} /* initListData */
void
addListTextItem( MPFORMAL ListData* ld, XP_UCHAR* txt )
{
XP_UCHAR* copy = copyString( MPPARM(mpool) txt );
XP_ASSERT( ld->nextIndex < ld->nItems );
ld->strings[++ld->nextIndex] = (char*)copy; /* ++ skips 0th for closure */
} /* addListTextItem */
/*****************************************************************************
* Turn the list of offsets into ptrs, free the offsets list, and call
* LstSetListChoices
****************************************************************************/
void
setListChoices( ListData* ld, ListPtr list, void* closure )
{
ld->strings[0] = closure;
LstSetListChoices( list, &ld->strings[1], ld->nextIndex );
if ( ld->selIndex >= 0 ) {
LstSetSelection( list, ld->selIndex );
}
} /* setListChoices */
/*****************************************************************************
* Given a string, figure out which item it matches and save off that item's
* index so that at show time it can be used to set the selection. Note that
* anything that changes the order will invalidate this, but also that there's
* no harm in calling it again.
****************************************************************************/
void
setListSelection( ListData* ld, char* selName )
{
if ( !!selName ) {
XP_U16 i;
for ( i = 0; i < ld->nextIndex; ++i ) {
if ( StrCompare( ld->strings[i+1], selName ) == 0 ) {
ld->selIndex = i;
break;
}
}
} else {
ld->selIndex = 0;
}
} /* setListSelection */
/*****************************************************************************
* Meant to be called after all items are added to the list, this function
* sorts them in place. For now I'll build in a call to StrCompare; later
* it could be a callback passed in.
****************************************************************************/
void
sortList( ListData* ld )
{
XP_S16 i, j, smallest;
char** strings = ld->strings;
char* tmp;
for ( i = 1; i <= ld->nextIndex; ++i ) { /* skip 0th (closure) slot */
for ( smallest = i, j = i+1; j <= ld->nextIndex; ++j ) {
if ( StrCompare( strings[smallest], strings[j] ) > 0 ) {
smallest = j;
}
}
if ( smallest == i ) { /* we got to the end without finding anything */
break;
}
tmp = strings[i];
strings[i] = strings[smallest];
strings[smallest] = tmp;
}
} /* sortList */
/*****************************************************************************
* Dispose the memory. Docs don't say whether LstSetListChoices does this for
* me so I assume not. It'll crash if I'm wrong. :-)
****************************************************************************/
void
freeListData( MPFORMAL ListData* ld )
{
char** strings = ld->strings;
XP_U16 i;
for ( i = 0; i < ld->nextIndex; ++i ) {
XP_FREE( mpool, *++strings ); /* skip 0th */
}
XP_FREE( mpool, ld->strings );
} /* freeListData */
/*****************************************************************************
*
****************************************************************************/
void
setSelectorFromList( UInt16 triggerID, ListPtr list, short listSelIndex )
{
XP_ASSERT( list != NULL );
XP_ASSERT( getActiveObjectPtr( triggerID ) != NULL );
XP_ASSERT( !!LstGetSelectionText( list, listSelIndex ) );
CtlSetLabel( getActiveObjectPtr( triggerID ),
LstGetSelectionText( list, listSelIndex ) );
} /* setTriggerFromList */
#define MAX_GADGET_COUNT 3 /* the most we need at this point */
void
sizeGadgetsForStrings( FormPtr form, ListPtr list, XP_U16 firstGadgetID )
{
XP_U16 i;
XP_U16 nGadgets = LstGetNumberOfItems( list );
XP_U16 strWidths[MAX_GADGET_COUNT];
XP_U16 totalStrWidth, totalGadgetWidth;
XP_U16 left, extra;
RectangleType rects[MAX_GADGET_COUNT];
RectangleType* rp;
XP_ASSERT( nGadgets <= MAX_GADGET_COUNT );
totalStrWidth = totalGadgetWidth = 0;
for ( i = 0, rp = rects; i < nGadgets; ++i, ++rp ) {
XP_U16 len;
XP_UCHAR* txt = (XP_UCHAR*)LstGetSelectionText( list, i );
len = XP_STRLEN( (const char*)txt );
totalStrWidth += strWidths[i] = FntCharsWidth( (const char*)txt, len );
getObjectBounds( firstGadgetID+i, rp );
totalGadgetWidth += rp->extent.x;
}
XP_ASSERT( totalGadgetWidth >= totalStrWidth );
extra = (totalGadgetWidth - totalStrWidth) / nGadgets;
left = rects[0].topLeft.x;
for ( i = 0, rp = rects; i < nGadgets; ++i, ++rp ) {
UInt16 index;
UInt16 width;
rp->topLeft.x = left;
rp->extent.x = width = strWidths[i] + extra;
index = FrmGetObjectIndex( form, firstGadgetID+i );
FrmSetObjectBounds( form, index, rp );
left += width;
}
} /* sizeGadgetsForStrings */
XP_Bool
penInGadget( EventPtr event, UInt16* whichGadget )
{
UInt16 x = event->screenX;
UInt16 y = event->screenY;
FormPtr form = FrmGetActiveForm();
UInt16 nObjects, i;
XP_Bool result = XP_FALSE;
for ( i = 0, nObjects = FrmGetNumberOfObjects(form); i < nObjects; ++i ) {
if ( frmGadgetObj == FrmGetObjectType( form, i ) ) {
UInt16 objId = FrmGetObjectId( form, i );
if ( objId != REFCON_GADGET_ID ) {
RectangleType rect;
FrmGetObjectBounds( form, i, &rect );
if ( RctPtInRectangle( x, y, &rect ) ) {
*whichGadget = objId;
result = XP_TRUE;
break;
}
}
}
}
return result;
} /* penInGadget */
void
setFormRefcon( void* refcon )
{
UInt16 index;
FormPtr form = FrmGetFormPtr( XW_MAIN_FORM );
XP_ASSERT( !!form );
index = FrmGetObjectIndex( form, REFCON_GADGET_ID );
FrmSetGadgetData( form, index, refcon );
} /* setFormRefcon */
void*
getFormRefcon()
{
void* result = NULL;
FormPtr form = FrmGetFormPtr( XW_MAIN_FORM );
XP_ASSERT( !!form );
if ( !!form ) {
UInt16 index = FrmGetObjectIndex( form, REFCON_GADGET_ID );
result = FrmGetGadgetData( form, index );
}
return result;
} /* getFormRefcon */
#endif
#if defined FEATURE_REALLOC || defined XW_FEATURE_UTILS
XP_U8*
palm_realloc( XP_U8* in, XP_U16 size )
{
MemPtr ptr = (MemPtr)in;
XP_U32 oldsize = MemPtrSize( ptr );
MemPtr newptr = MemPtrNew( size );
XP_ASSERT( !!newptr );
XP_MEMCPY( newptr, ptr, oldsize );
MemPtrFree( ptr );
return newptr;
} /* palm_realloc */
XP_U16
palm_snprintf( XP_UCHAR* buf, XP_U16 len, XP_UCHAR* format, ... )
{
XP_U16 nChars;
/* PENDING use len to avoid writing too many chars */
va_list ap;
va_start( ap, format );
nChars = StrVPrintF( (char*)buf, (char*)format, ap );
va_end( ap );
XP_ASSERT( len >= nChars );
return nChars;
} /* palm_snprintf */
#endif
#ifdef DEBUG
void
palm_warnf( char* format, ... )
{
char buf[200];
va_list ap;
va_start( ap, format );
StrVPrintF( buf, format, ap );
va_end( ap );
#ifdef FOR_GREMLINS
/* If gremlins are active, we want all activity to stop here! That
means no cancellation */
{
FormPtr form;
FieldPtr field;
form = FrmInitForm( XW_GREMLIN_WARN_FORM_ID );
FrmSetActiveForm( form );
FrmSetEventHandler( form, doNothing );
field = getActiveObjectPtr( XW_GREMLIN_WARN_FIELD_ID );
FldSetTextPtr( field, buf );
FldRecalculateField( field, true );
FrmDrawForm( form );
#if 1
/* This next may freeze X (go to a console to kill POSE), but when
you look at the logs you'll see what event you were on when the
ASSERT fired. Otherwise the events just keep coming and POSE
fills up the log. */
while(1);
#endif
/* this should NEVER return */
(void)FrmDoDialog( form );
}
#else
(void)FrmCustomAlert( XW_ERROR_ALERT_ID, buf, " ", " " );
#endif
} /* palm_warnf */
void
palm_assert( Boolean b, int line, char* fileName )
{
if ( !b ) {
XP_LOGF( "ASSERTION FAILED: line %d in %s", line, fileName );
XP_WARNF( "ASSERTION FAILED: line %d in %s", line, fileName );
}
} /* palmassert */
static void
logToMemo( char* buf )
{
const XP_U16 MAX_MEMO_SIZE = 4000;
const XP_U16 MAX_NRECORDS = 200;
DmOpenRef ref;
UInt16 nRecords, index;
UInt16 len = XP_STRLEN( buf );
UInt16 hSize, slen;
MemHandle hand;
MemPtr ptr;
char tsBuf[20];
UInt16 tsLen;
DateTimeType dtType;
LocalID dbID;
TimSecondsToDateTime( TimGetSeconds(), &dtType );
StrPrintF( tsBuf, "\n%d:%d:%d-", dtType.hour, dtType.minute,
dtType.second );
tsLen = XP_STRLEN(tsBuf);
(void)DmCreateDatabase( CARD_0, "MemoDB", 'memo', 'DATA', false );
dbID = DmFindDatabase( CARD_0, "MemoDB" );
ref = DmOpenDatabase( CARD_0, dbID, dmModeWrite );
nRecords = DmNumRecordsInCategory( ref, dmAllCategories );
if ( nRecords == 0 ) {
index = dmMaxRecordIndex;
hSize = 0;
hand = DmNewRecord( ref, &index, 1 );
DmReleaseRecord( ref, index, true );
} else {
while ( nRecords > MAX_NRECORDS ) {
index = 0;
DmSeekRecordInCategory( ref, &index, 0, dmSeekForward,
dmAllCategories);
DmRemoveRecord( ref, index );
--nRecords;
}
index = 0;
DmSeekRecordInCategory( ref, &index, nRecords, dmSeekForward,
dmAllCategories);
hand = DmGetRecord( ref, index );
XP_ASSERT( !!hand );
hSize = MemHandleSize( hand ) - 1;
ptr = MemHandleLock( hand );
slen = XP_STRLEN(ptr);
MemHandleUnlock(hand);
if ( hSize > slen ) {
hSize = slen;
}
(void)DmReleaseRecord( ref, index, false );
}
if ( (hSize + len + tsLen) > MAX_MEMO_SIZE ) {
index = dmMaxRecordIndex;
hand = DmNewRecord( ref, &index, len + tsLen + 1 );
hSize = 0;
} else {
(void)DmResizeRecord( ref, index, len + hSize + tsLen + 1 );
hand = DmGetRecord( ref, index );
}
ptr = MemHandleLock( hand );
DmWrite( ptr, hSize, tsBuf, tsLen );
DmWrite( ptr, hSize + tsLen, buf, len + 1 );
MemHandleUnlock( hand );
DmReleaseRecord( ref, index, true );
DmCloseDatabase( ref );
} /* logToMemo */
void
palm_debugf( char* format, ...)
{
char buf[200];
va_list ap;
va_start( ap, format );
StrVPrintF( buf, format, ap );
va_end( ap );
logToMemo( buf );
} /* debugf */
/* if -0 isn't passed to compiler it wants to find this!!! */
void p_ignore(char* s, ...) {}
#ifdef FOR_GREMLINS
static Boolean
doNothing( EventPtr event )
{
return true;
} /* doNothing */
#endif
void
palm_logf( char* format, ... )
{
char buf[200];
va_list ap;
va_start( ap, format );
StrVPrintF( buf, format, ap );
va_end( ap );
logToMemo( buf );
} /* palm_logf */
#endif /* DEBUG */

76
xwords4/palm/palmutil.h Normal file
View file

@ -0,0 +1,76 @@
// -*-mode: C; fill-column: 78; c-basic-offset: 4; -*-
/****************************************************************************
* *
* Copyright 1998-1999, 2001 by Eric House (fixin@peak.org). All rights reserved. *
* *
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
****************************************************************************/
#ifndef _PALMUTIL_H_
#define _PALMUTIL_H_
#include <PalmTypes.h>
#include <List.h>
/* #include <Window.h> */
/* #include <FeatureMgr.h> */
/* #include <CharAttr.h> */
/* #include <DLServer.h> */
/* #include "xwdefines.h" */
/* #include "xwords.h" */
/* #include "xwdebug.h" */
#include "palmmain.h"
/* short myMemCmp( unsigned char* src1, unsigned char* src2, short size ); */
/* void userError( CharPtr* str ); */
/* void userErrorRes( short resId ); */
/* void userErrorResS( short resId, CharPtr data ); */
void beep( void );
MemPtr getActiveObjectPtr( UInt16 objectID );
void getObjectBounds( UInt16 objectID, RectangleType* rectP );
void setObjectBounds( UInt16 objectID, RectangleType* rectP );
void disOrEnable( FormPtr form, UInt16 id, Boolean enable );
void disOrEnableSet( FormPtr form, const UInt16* id, Boolean enable );
void centerControl( FormPtr form, UInt16 id );
void setBooleanCtrl( UInt16 objectID, Boolean isSet );
Boolean getBooleanCtrl( UInt16 objectID );
void setFieldEditable( FieldPtr fld, Boolean editable );
/* list item stuff */
void initListData( MPFORMAL ListData* ld, XP_U16 nItems );
void addListTextItem( MPFORMAL ListData* ld, XP_UCHAR* txt );
void setListChoices( ListData* ld, ListPtr list, void* closure );
void setListSelection( ListData* ld, char* selName );
void sortList( ListData* ld );
void freeListData( MPFORMAL ListData* ld );
/* this should work on either trigger or selector */
void setSelectorFromList( UInt16 selectorID, ListPtr list,
short listSelIndex );
void sizeGadgetsForStrings( FormPtr form, ListPtr list, XP_U16 firstGadgetID );
XP_Bool penInGadget( EventPtr event, UInt16* whichGadget );
void setFormRefcon( void* refcon );
void* getFormRefcon();
#endif