home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2002 July & August
/
PCWorld_2002-07-08_cd.bin
/
Software
/
Vyzkuste
/
batterypanel
/
source
/
main.c
< prev
next >
Wrap
C/C++ Source or Header
|
2000-10-11
|
29KB
|
1,055 lines
//=============================================================================
// $Id$
//=============================================================================
/*
Copyright (C) 2000 Remo Hofer <cocos.nucifera@gmx.net>
For more information see http://www.geocities.com/SiliconValley/Cable/5206/
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 header
#include <PalmOS.h>
#include "callback.h"
#include "rsrc.h"
#include "rsrcfix.h"
/*
* DEBUG >= 2 Include code for silent errorcheck. Does only interact with
* the user in the case of an error.
* DEBUG >= 4 Include code for silent logging to the emulator log.
* DEBUG >= 6 Include code that may interact with (display info to) the
* user without making the program unusable.
* DEBUG >= 8 Include code that may make the program almost unuseable.
*/
#define DEBUG 4
#define PalmOS20 sysMakeROMVersion(2,0,0,sysROMStageRelease,0)
#define PalmOS30 sysMakeROMVersion(3,0,0,sysROMStageRelease,0)
#define PalmOS31 sysMakeROMVersion(3,1,0,sysROMStageRelease,0)
#define PalmOS32 sysMakeROMVersion(3,2,0,sysROMStageRelease,0)
#define PalmOS33 sysMakeROMVersion(3,3,0,sysROMStageRelease,0)
#define Creator 'Bttr'
#define PrefsId 0
#define PrefsVersion 1
#define MinRequiredVersion PalmOS20
#define RefreshDelay (SysTicksPerSecond()) /* 1 second */
#define RepeatDelay (SysTicksPerSecond() / 5) /* 0.2 seconds */
// defaults
#define DefaultAlkalineWarn 200
#define DefaultAlkalineCritical 160
#define DefaultLiIonWarn 376
#define DefaultLiIonCritical 371
#define DefaultLiIon1400Warn 364
#define DefaultLiIon1400Critical 200
typedef enum {
initValues,
redrawForm,
testValues
} FormUpdateReason;
typedef struct {
// SysBatteryKind kind;
UInt16 warn;
UInt16 critical;
Boolean acceptRisk;
} PrefsType;
// globals
static Boolean calledFromApp = false;
static SysDBListItemType *panelIDsP = NULL; // ptr to panel pick list items
static UInt16 oldVoltage, oldWarn, oldCritical, oldTimeout,
dialogVoltage, dialogMin, dialogMax;
static UInt8 oldPercent;
static Boolean oldPlugged;
static SysBatteryKind oldKind;
static Coord oldLevel;
static Char s[64];
static Char decimalPoint[2];
static SysBatteryKind indexToKind[4] = {
sysBatteryKindAlkaline, sysBatteryKindNiCad,
sysBatteryKindRechAlk, sysBatteryKindNiMH };
static UInt16 kindToIndex[5] = { 0, 1, 0xFF, 2, 3 };
//-----------------------------------------------------------------------------
static UInt32 GetRomVersion(void) {
UInt32 romVersion;
FtrGet(sysFtrCreator, sysFtrNumROMVersion, &romVersion);
return(romVersion);
}
//-----------------------------------------------------------------------------
#if DEBUG >= 4
#include <stdarg.h>
#include "gccHostControl.h"
void EmulatorLog(Char * format,...) {
va_list args;
UInt32 dummy;
Char s[64];
va_start(args, format);
StrVPrintF(s, format, args);
va_end(args);
if ((GetRomVersion() >= PalmOS30) && (FtrGet('pose', 0, &dummy) == 0)) {
HostFPutS(s, HostLogFile());
}
#if DEBUG >= 8
WinDrawChars(s, StrLen(s), 0, 0);
#endif
}
#endif
//-----------------------------------------------------------------------------
static void * FormGetObjectPtr(FormType * formP, UInt16 objId) {
#if DEBUG >= 4
EmulatorLog("FormGetObjectPtr");
#endif
return (FrmGetObjectPtr(formP, FrmGetObjectIndex(formP, objId)));
}
//-----------------------------------------------------------------------------
static void FormShowObject(FormType * formP, UInt16 objId) {
#if DEBUG >= 4
EmulatorLog("FormShowObject");
#endif
FrmShowObject(formP, FrmGetObjectIndex(formP, objId));
}
//-----------------------------------------------------------------------------
static void FormHideObject(FormType * formP, UInt16 objId) {
#if DEBUG >= 4
EmulatorLog("FormHideObject");
#endif
FrmHideObject(formP, FrmGetObjectIndex(formP, objId));
}
//-----------------------------------------------------------------------------
static void PanelFreeData(void) {
#if DEBUG >= 4
EmulatorLog("PanelFreeData");
#endif
if (panelIDsP) {
MemPtrFree(panelIDsP);
panelIDsP = NULL;
}
}
//-----------------------------------------------------------------------------
static void PanelDone(void) {
EventType newEvent;
#if DEBUG >= 4
EmulatorLog("PanelDone");
#endif
// by simply exiting we return to the prior which called us
MemSet(&newEvent, sizeof(EventType), 0);
newEvent.eType = appStopEvent;
EvtAddEventToQueue(&newEvent);
}
//-----------------------------------------------------------------------------
static void PanelSelect(UInt16 index) {
UInt16 currentAppCardNo;
LocalID currentAppDBID;
#if DEBUG >= 4
EmulatorLog("PanelSelect");
#endif
if (panelIDsP) {
// Switch to the panel if it isn't this one
SysCurAppDatabase(¤tAppCardNo, ¤tAppDBID);
if ((panelIDsP[index].dbID != currentAppDBID) ||
(panelIDsP[index].cardNo != currentAppCardNo)) {
SysUIAppSwitch(
panelIDsP[index].cardNo, panelIDsP[index].dbID,
sysAppLaunchCmdNormalLaunch, 0);
}
}
}
//-----------------------------------------------------------------------------
static void PanelDrawItem(Int16 itemNum, RectanglePtr bounds, Char ** dummy) {
Char * itemText;
CALLBACK_PROLOGUE
#if DEBUG >= 4
EmulatorLog("PanelDrawItem");
#endif
if (panelIDsP) {
itemText = panelIDsP[itemNum].name;
WinDrawChars(itemText, StrLen(itemText),
bounds->topLeft.x, bounds->topLeft.y);
}
CALLBACK_EPILOGUE
}
//-----------------------------------------------------------------------------
// Create a list of panels available and select the current one.
// panelIDsP is set.
static void PanelInit(Boolean calledFromApp, FormPtr f,
UInt16 idPopup, UInt16 idList, UInt16 idLabel, UInt16 idDone) {
#if DEBUG >= 4
EmulatorLog("PanelInit");
#endif
if (calledFromApp) {
// hide panel popup and show done button
FormShowObject(f, idLabel);
FormShowObject(f, idDone);
} else {
// create panel list and show popup trigger
ListPtr listP;
UInt16 currentAppCardNo;
LocalID currentAppDBID;
MemHandle panelIDsH; // MemHandle to pick list items
UInt16 panelCount, i;
PrefActivePanelParamsType prefs;
// create the panel list data
if (SysCreatePanelList(&panelCount, &panelIDsH)) {
panelIDsP = MemHandleLock(panelIDsH);
listP = FormGetObjectPtr(f, idList);
// Now set the list to hold the number of panels found. There
// is no array of text to use.
LstSetListChoices(listP, NULL, panelCount);
// Now resize the list to the number of panel found
LstSetHeight(listP, panelCount);
// Because there is no array of text to use, we need a function
// to interpret the panelIDsP list and draw the list items.
LstSetDrawFunction(listP, PanelDrawItem);
// Now we should select the item in the list which matches the
// current app.
SysCurAppDatabase(¤tAppCardNo, ¤tAppDBID);
for (i = 0; i < panelCount; i++) {
if (panelIDsP[i].dbID == currentAppDBID &&
panelIDsP[i].cardNo == currentAppCardNo) {
LstSetSelection(listP, i);
CtlSetLabel(
FormGetObjectPtr(f, idPopup),
panelIDsP[i].name);
break;
}
}
// draw the popup trigger
FormShowObject(f, idPopup);
// set this panel as the active one next time the
// Prefs app is opened
prefs.activePanel = Creator;
AppCallWithCommand(sysFileCPreferences,
prefAppLaunchCmdSetActivePanel, &prefs);
}
}
}
//-----------------------------------------------------------------------------
static UInt16 GlueSysBatteryInfo(Boolean set, UInt16 * warnThresholdP,
UInt16 *criticalThresholdP, Int16 *maxTicksP, SysBatteryKind* kindP,
Boolean *pluggedIn, UInt8 *percentP) {
UInt16 result;
#if DEBUG >= 4
EmulatorLog("GlueSysBatteryInfo");
#endif
if (GetRomVersion() >= PalmOS30) {
return (SysBatteryInfo(set, warnThresholdP, criticalThresholdP,
maxTicksP, kindP, pluggedIn, percentP));
} else {
result = SysBatteryInfoV20(set, warnThresholdP, criticalThresholdP,
maxTicksP, kindP, pluggedIn);
if ((!set) && (percentP != NULL)) {
// interpolate between 205 and 280
// values are taken (more or less) from the alkaline table
// in the PalmOS 3.0 source (ignoring the top and low 10%)
*percentP = min(((max(result,205)-205) * 100 + (280-205)/2)
/ (280-205), 100);
}
return (result);
}
}
//-----------------------------------------------------------------------------
static void UpdatePopup(FormPtr f, UInt16 popupId, UInt16 listId,
UInt16 index) {
#if DEBUG >= 4
EmulatorLog("UpdatePopup");
#endif
CtlSetLabel(
FormGetObjectPtr(f, popupId),
LstGetSelectionText(FormGetObjectPtr(f, listId), index));
}
//-----------------------------------------------------------------------------
void UpdateControl(FormPtr f, UInt16 id, Char * s) {
ControlType *ctl;
Char *label;
#if DEBUG >= 4
EmulatorLog("UpdateControl");
#endif
ctl = FormGetObjectPtr(f, id),
label = (Char *) CtlGetLabel(ctl);
StrCopy(label, s);
CtlSetLabel(ctl, label);
}
//-----------------------------------------------------------------------------
static void UpdateLabel(FormPtr f, UInt16 id, Char * s) {
#if DEBUG >= 4
EmulatorLog("UpdateLabel");
#endif
FormHideObject(f, id);
FrmCopyLabel(f, id, s);
FormShowObject(f, id);
}
//-----------------------------------------------------------------------------
static void UpdateButton(FormPtr f, UInt16 id,
Boolean enabled, Boolean up) {
ControlType *ctl;
ctl = FormGetObjectPtr(f, id);
if (CtlEnabled(ctl) != enabled) {
CtlSetEnabled(ctl, enabled);
if (up) { if (enabled) StrCopy(s, "\001"); else StrCopy(s, "\003"); }
else { if (enabled) StrCopy(s, "\002"); else StrCopy(s, "\004"); }
UpdateLabel(f, id, s);
}
}
//-----------------------------------------------------------------------------
static void VoltageToA(Char * s, UInt16 v) {
#if DEBUG >= 4
EmulatorLog("VoltageToA");
#endif
StrPrintF(s, "%u%s%u%u V", v/100, decimalPoint, (v/10)%10, v%10);
}
//-----------------------------------------------------------------------------
static void UpdateForm(FormPtr f, FormUpdateReason reason) {
UInt16 voltage, warn, critical, timeout, index;
UInt8 percent;
Boolean plugged, changeable;
SysBatteryKind kind;
RectangleType r1, r2, save;
Coord full, level, x, y;
MemHandle h;
#if DEBUG >= 4
EmulatorLog("UpdateForm");
#endif
// get the data
voltage = GlueSysBatteryInfo(false, &warn, &critical,
&timeout, &kind, &plugged, &percent);
// update the percentage
if ((oldPercent != percent) || (reason == initValues)) {
oldPercent = percent;
StrIToA(s, percent); // implicit typecast!
StrCat(s, "%");
UpdateLabel(f, idPercentLabel, s);
}
// update graphical battery display, either when the level changed,
// or when we got an update event
FrmGetObjectBounds(f, FrmGetObjectIndex(f, idBitmapBounds), &r1);
full = r1.extent.x;
level = min(((((full-4)*percent)+50)/100)+1, full-3); // 1 <-> (full-3)
if (FrmVisible(f) && ((reason == redrawForm) || (oldLevel != level))) {
oldLevel = level;
WinGetClip(&save);
RctCopyRectangle(&r1, &r2);
x = r1.topLeft.x;
y = r1.topLeft.y;
// draw full part of display
if (level > 0) {
r1.extent.x = level;
WinClipRectangle(&r1);
WinSetClip(&r1);
h = DmGetResource(bitmapRsc, idFullBitmap);
WinDrawBitmap(MemHandleLock(h), x, y);
MemHandleUnlock(h);
DmReleaseResource(h);
WinSetClip(&save);
}
// draw empty part of display
r2.topLeft.x = x + level;
r2.extent.x = full - level;
if (r2.extent.x > 0) {
WinClipRectangle(&r2);
WinSetClip(&r2);
h = DmGetResource(bitmapRsc, idEmptyBitmap);
WinDrawBitmap(MemHandleLock(h), x, y);
MemHandleUnlock(h);
DmReleaseResource(h);
}
WinSetClip(&save);
}
// update the voltage
if ((oldVoltage != voltage) || (reason == initValues)) {
oldVoltage = voltage;
VoltageToA(s, voltage);
UpdateLabel(f, idVoltageLabel, s);
}
// update the battery kind
if ((oldKind != kind) || (reason == initValues)) {
oldKind = kind;
switch (kind) {
case sysBatteryKindAlkaline:
case sysBatteryKindNiCad:
case sysBatteryKindRechAlk:
case sysBatteryKindNiMH:
changeable = true;
index = kindToIndex[kind];
break;
case sysBatteryKindLiIon:
changeable = false;
index = idLiIonKindStr;
break;
case sysBatteryKindLiIon1400:
changeable = false;
index = idLiIon1400KindStr;
break;
default:
changeable = false;
index = idUnknownKindStr;
break;
}
if (changeable) {
FormHideObject(f, idKindLabel);
UpdatePopup(f, idKindPopup, idKindList, index);
FormShowObject(f, idKindPopup);
} else {
FormHideObject(f, idKindPopup);
SysCopyStringResource(s, index);
UpdateLabel(f, idKindLabel, s);
FormShowObject(f, idKindLabel);
}
}
// update plugged in
if ((oldPlugged != plugged) || (reason == initValues)) {
oldPlugged = plugged;
if (plugged) SysCopyStringResource(s, idYesStr);
else SysCopyStringResource(s, idNoStr);
UpdateLabel(f, idPluggedLabel, s);
}
// update warning threshold
if ((oldWarn != warn) || (reason == initValues)) {
oldWarn = warn;
VoltageToA(s, warn);
UpdateControl(f, idWarnSelector, s);
}
// update critical threshold
if ((oldCritical != critical) || (reason == initValues)) {
oldCritical = critical;
VoltageToA(s, critical);
UpdateControl(f, idCriticalSelector, s);
}
// update warning timeout
if ((oldTimeout != timeout) || (reason == initValues)) {
oldTimeout = timeout;
StrPrintF(s, "%u s", timeout / SysTicksPerSecond());
UpdateLabel(f, idTicksLabel, s);
}
}
//-----------------------------------------------------------------------------
static Boolean PopupList(UInt16 * value,
FormPtr f, UInt16 triggerId, UInt16 listId) {
Int16 newi, oldi;
ListPtr list;
Coord x, y;
#if DEBUG >= 4
EmulatorLog("PopupList");
#endif
list = FormGetObjectPtr(f, listId);
FrmGetObjectPosition(f, FrmGetObjectIndex(f, triggerId), &x, &y);
LstSetPosition(list, x, y);
oldi = *value;
LstSetSelection(list, oldi);
newi = LstPopupList(list);
if ((newi >= 0) && (newi != oldi)) {
*value = newi;
UpdatePopup(f, triggerId, listId, *value);
return (true);
}
return (false);
}
//-----------------------------------------------------------------------------
// no globals
static void InitPrefs(PrefsType * p) {
#if DEBUG >= 4
EmulatorLog("InitPrefs");
#endif
// p->kind = sysBatteryKindAlkaline;
p->warn = 0;
p->critical = 0;
p->acceptRisk = false;
}
//-----------------------------------------------------------------------------
// no globals
static Boolean LoadPrefs(PrefsType * p) {
UInt16 size;
UInt16 version;
#if DEBUG >= 4
EmulatorLog("LoadPrefs");
#endif
size = sizeof(PrefsType);
version = PrefGetAppPreferences(Creator, PrefsId, p, &size, true);
if ((version != PrefsVersion) || (size != sizeof(PrefsType))) {
InitPrefs(p);
return (false);
}
return (true);
}
//-----------------------------------------------------------------------------
// no globals
static void SavePrefs(Boolean setRisk, Boolean acceptRisk) {
PrefsType p;
SysBatteryKind kind, prefkind;
UInt16 warn;
UInt16 critical;
#if DEBUG >= 4
EmulatorLog("SavePrefs");
#endif
GlueSysBatteryInfo(false, &warn, &critical, NULL, &kind, NULL, NULL);
LoadPrefs(&p);
// only write prefs, if something has changed
if (/*(p.kind != kind) ||*/ (p.warn != warn) || (p.critical != critical) ||
(setRisk && (p.acceptRisk != acceptRisk))) {
// p.kind = kind;
p.warn = warn;
p.critical = critical;
if (setRisk) p.acceptRisk = acceptRisk;
PrefSetAppPreferences(Creator, PrefsId, PrefsVersion,
&p, sizeof(PrefsType), true);
}
// save the battery kind in the system preferences
prefkind = (SysBatteryKind) PrefGetPreference(prefSysBatteryKind);
if (prefkind != kind) {
PrefSetPreference(prefSysBatteryKind, (SysBatteryKind) kind);
}
}
//-----------------------------------------------------------------------------
static Boolean WarnUserRisk(void) {
PrefsType prefs;
#if DEBUG >= 4
EmulatorLog("WarnUserRisk");
#endif
LoadPrefs(&prefs);
if (prefs.acceptRisk) return (true);
if (FrmAlert(idUserRiskAlert) == 0) {
SavePrefs(true, true);
return (true);
}
return (false);
}
//-----------------------------------------------------------------------------
static void UpdateVoltageDialog(FormPtr f) {
StrIToA(s, dialogVoltage / 100);
UpdateLabel(f, idVoltage100Button, s);
StrIToA(s, (dialogVoltage / 10) % 10);
UpdateLabel(f, idVoltage10Button, s);
StrIToA(s, dialogVoltage % 10);
UpdateLabel(f, idVoltage1Button, s);
}
//-----------------------------------------------------------------------------
static Boolean VoltageDialogHandleEvent(EventPtr event) {
FormPtr formPtr;
Int16 delta, newvalue;
CALLBACK_PROLOGUE
#if DEBUG >= 4
EmulatorLog("VoltageDialogHandleEvent: %u", event->eType);
#endif
formPtr = FrmGetFormPtr(idVoltageForm);
switch (FrmGetObjectId(formPtr,
FrmGetControlGroupSelection(formPtr, idVoltageGroup))) {
case idVoltage100Button:
delta = 100;
break;
case idVoltage10Button:
delta = 10;
break;
default:
delta = 1;
}
// handle the buton up / down events (but return false)
if (event->eType == ctlRepeatEvent) {
event->data.ctlRepeat.time += RepeatDelay;
if (event->data.ctlRepeat.controlID == idVoltageDownButton) {
newvalue = dialogVoltage - delta;
if (newvalue >= dialogMin) {
dialogVoltage = newvalue;
UpdateVoltageDialog(formPtr);
}
} else if (event->data.ctlRepeat.controlID == idVoltageUpButton) {
newvalue = dialogVoltage + delta;
if (newvalue <= dialogMax) {
dialogVoltage = newvalue;
UpdateVoltageDialog(formPtr);
}
}
}
// enable or disable the up button
UpdateButton(formPtr, idVoltageUpButton,
(dialogVoltage+delta <= dialogMax), true);
UpdateButton(formPtr, idVoltageDownButton,
(dialogVoltage >= dialogMin+delta), false);
CALLBACK_EPILOGUE
return (false);
}
//-----------------------------------------------------------------------------
static Boolean VoltageDialog(UInt16 * volts, UInt16 min, UInt16 max,
UInt16 titleId) {
FormPtr formPtr;
#if DEBUG >= 4
EmulatorLog("VoltageDialog");
#endif
dialogVoltage = *volts;
dialogMin = min;
dialogMax = max(max, *volts);
formPtr = FrmInitForm(idVoltageForm);
FrmSetEventHandler(formPtr, VoltageDialogHandleEvent);
FrmSetControlGroupSelection(formPtr, idVoltageGroup, idVoltage1Button);
SysCopyStringResource(s, titleId);
FrmCopyTitle(formPtr, s);
UpdateLabel(formPtr, idVoltageSeparatorLabel, decimalPoint);
UpdateVoltageDialog(formPtr);
if (FrmDoDialog(formPtr) == idVoltageOkButton) {
FrmDeleteForm(formPtr);
if (dialogVoltage != *volts) {
*volts = dialogVoltage;
return (true);
} else {
return (false);
}
}
FrmDeleteForm(formPtr);
return (false);
}
//-----------------------------------------------------------------------------
static Boolean MainFormHandleEvent(EventPtr event) {
FormPtr formPtr;
MemHandle h;
SysBatteryKind kind;
UInt32 romVersion;
UInt16 index;
UInt16 warn, critical, voltage;
ListPtr list;
Boolean reducedList;
Boolean handled = false;
CALLBACK_PROLOGUE
#if DEBUG >= 4
EmulatorLog("MainFormHandleEvent: %u", event->eType);
#endif
formPtr = FrmGetFormPtr(idMainForm);
switch (event->eType) {
case frmOpenEvent:
// init all form elements
UpdateForm(formPtr, initValues);
// setup the version string
h = DmGetResource(verRsc, idVersion);
FrmCopyLabel(formPtr, idVersionLabel, MemHandleLock(h));
MemHandleUnlock(h);
DmReleaseResource(h);
// draw the form
FrmDrawForm(formPtr);
// update the image display (draw the battery icon)
UpdateForm(formPtr, redrawForm);
// Now that the form is displayed, generate the panel pick list.
// By occuring here after everything is displayed the time required
// isn't noticed by the user.
PanelInit(calledFromApp, formPtr, idPanelPopup, idPanelList,
idPanelLabel, idDoneButton);
handled = true;
break;
case frmUpdateEvent:
FrmDrawForm(formPtr);
UpdateForm(formPtr, redrawForm);
handled = true;
break;
case frmCloseEvent:
PanelFreeData();
SavePrefs(false, false);
// handled = false; // give std form handler a chance to cleanup
break;
case nilEvent:
// update all form elements
UpdateForm(formPtr, testValues);
// handled = false;
break;
case menuEvent:
switch (event->data.menu.itemID) {
case idDefaultMenu:
// reset prefs to defaults
GlueSysBatteryInfo(false, NULL, NULL, NULL, &kind, NULL, NULL);
// get the defaults depending of battery kind
switch (kind) {
case sysBatteryKindLiIon:
warn = DefaultLiIonWarn;
critical = DefaultLiIonCritical;
break;
case sysBatteryKindLiIon1400:
warn = DefaultLiIon1400Warn;
critical = DefaultLiIon1400Critical;
break;
default:
kind = sysBatteryKindAlkaline;
warn = DefaultAlkalineWarn;
critical = DefaultAlkalineCritical;
break;
}
// change to default levels
GlueSysBatteryInfo(true, &warn, &critical, NULL,NULL,NULL,NULL);
// change to default kind; this may as well reset the levels
GlueSysBatteryInfo(true, NULL,NULL,NULL, &kind, NULL,NULL);
// reset the acceptRisk state
SavePrefs(true, false);
// update the form
UpdateForm(formPtr, testValues);
handled = true;
break;
case idThanksMenu:
// show about form
FrmHelp(idThanksStr);
handled = true;
break;
case idAboutMenu:
// show about form
FrmHelp(idAboutStr);
handled = true;
break;
}
break;
case ctlSelectEvent:
// one of the controls (popup/selector trigger) is selected
switch (event->data.ctlSelect.controlID) {
case idKindPopup:
GlueSysBatteryInfo(false, NULL, NULL, NULL, &kind, NULL, NULL);
index = min(kindToIndex[kind], 3);
list = FormGetObjectPtr(formPtr, idKindList);
reducedList = false;
romVersion = GetRomVersion();
if (romVersion < PalmOS33) {
if ((romVersion < PalmOS32) && (romVersion >= PalmOS31)) {
// OS 3.1 with Alkaline, NiCad, and Rech. Alkaline
LstSetListChoices(list, list->itemsText, 3);
LstSetHeight(list, 3);
index = min(kindToIndex[kind], 2);
reducedList = true;
} else {
// old OS (2.0 3.0 3.2) with only Alkaline and NiCad
LstSetListChoices(list, list->itemsText, 2);
LstSetHeight(list, 2);
index = min(kindToIndex[kind], 1);
reducedList = true;
}
}
// new OS (>=3.3) with Alkaline, NiCad, Rech. Alkaline, and NiMH
if (PopupList(&index, formPtr, idKindPopup, idKindList)) {
kind = indexToKind[index];
GlueSysBatteryInfo(true, NULL,NULL,NULL, &kind, NULL,NULL);
UpdateForm(formPtr, testValues);
}
if (reducedList) {
// restore the list
LstSetListChoices(list, list->itemsText, 4);
LstSetHeight(list, 4);
}
handled = true;
break;
case idWarnSelector:
case idCriticalSelector:
if (event->data.ctlSelect.controlID == idCriticalSelector) {
voltage = GlueSysBatteryInfo(false, NULL, &warn,
NULL, NULL, NULL, NULL);
index = idSetCriticalStr;
} else {
voltage = GlueSysBatteryInfo(false, &warn,
NULL, NULL, NULL, NULL, NULL);
index = idSetWarnStr;
}
if (WarnUserRisk()) {
//??? if (VoltageDialog(&warn, 0, voltage-2, index)) {
if (VoltageDialog(&warn, 0, 1500, index)) {
if (event->data.ctlSelect.controlID == idCriticalSelector){
GlueSysBatteryInfo(true, NULL, &warn,
NULL, NULL, NULL, NULL);
} else {
GlueSysBatteryInfo(true, &warn,
NULL, NULL, NULL, NULL, NULL);
}
UpdateForm(formPtr, testValues);
}
}
handled = true;
break;
case idDoneButton:
// if calledFromApp this is the way to end the program
PanelDone();
handled = true;
break;
}
break;
case popSelectEvent:
switch (event->data.popSelect.listID) {
case idPanelList:
// a new panel has been selected
PanelSelect(event->data.popSelect.selection);
handled = true;
break;
}
break;
default:
}
CALLBACK_EPILOGUE
return (handled);
}
//-----------------------------------------------------------------------------
static Boolean ApplicationHandleEvent(EventPtr event) {
FormPtr formPtr;
#if DEBUG >= 4
EmulatorLog("ApplicationHandleEvent");
#endif
if (event->eType == frmLoadEvent) {
// we have only one form!
// load the form resource and set the event handler
formPtr = FrmInitForm(event->data.frmLoad.formID);
FrmSetActiveForm(formPtr);
FrmSetEventHandler(formPtr, MainFormHandleEvent);
return (true);
}
return (false);
}
//-----------------------------------------------------------------------------
UInt32 PilotMain(UInt16 cmd, void * cmdPBP, UInt16 launchFlags) {
UInt32 updateInterval;
static EventType event; // declared static to conserve stack space
Err error;
#if DEBUG >= 4
EmulatorLog("PilotMain: %u", cmd);
#endif
// see if we're on in minimum required version of the ROM or later
if (GetRomVersion() < MinRequiredVersion) {
if ((launchFlags & (sysAppLaunchFlagNewGlobals | sysAppLaunchFlagUIApp))
== (sysAppLaunchFlagNewGlobals | sysAppLaunchFlagUIApp)) {
FrmAlert(idErrOldRomAlert);
}
return (sysErrRomIncompatible);
}
// handle launch codes
switch (cmd) {
case sysAppLaunchCmdPanelCalledFromApp:
// we are called directly from an App (not by Prefs)
calledFromApp = true;
case sysAppLaunchCmdNormalLaunch:
// normal call from Prefs (or other panel)
case sysAppLaunchCmdReturnFromPanel:
// does this happen if we switch a lot between panels?
// get the decimal delimiter
switch (PrefGetPreference(prefNumberFormat)) {
case nfPeriodComma:
case nfSpaceComma:
case nfApostropheComma:
StrCopy(decimalPoint, ",");
break;
default:
StrCopy(decimalPoint, ".");
}
// go to the initial form
FrmGotoForm(idMainForm);
// go into the event loop
updateInterval = RefreshDelay;
do {
EvtGetEvent(&event, updateInterval);
if (! SysHandleEvent(&event)) {
if (! MenuHandleEvent(NULL, &event, &error)) {
if (! ApplicationHandleEvent(&event)) {
FrmDispatchEvent(&event);
}
}
}
} while (event.eType != appStopEvent);
// send a frmCloseEvent to active form handlers
FrmCloseAllForms();
break;
case sysAppLaunchCmdSyncNotify:
// we are freshly installed by a HotSync
case sysAppLaunchCmdSystemReset:
// the unit has been reset
// so we need to tell the system about our prefs
// no globals!
{
PrefsType prefs;
UInt16 warn, critical;
SysBatteryKind kind, prefkind;
// PalmOS 2.0 and 3.0 do not restore the battery kind
// from the system prefs.
// To be save we also retore them on newer OSes, if needed
prefkind = (SysBatteryKind) PrefGetPreference(prefSysBatteryKind);
GlueSysBatteryInfo(false, NULL, NULL, NULL, &kind, NULL, NULL);
if (prefkind != kind)
GlueSysBatteryInfo(true, NULL, NULL, NULL, &prefkind, NULL, NULL);
if (LoadPrefs(&prefs)) {
// prefs have been set
// GlueSysBatteryInfo(false, NULL, NULL, NULL, &kind, NULL, NULL);
// if (prefs.kind != kind) {
// // set the battery kind
// GlueSysBatteryInfo(true, NULL, NULL, NULL, &prefs.kind,
// NULL, NULL);
// }
if (prefs.acceptRisk) {
// set the warn and critical level
GlueSysBatteryInfo(false, &warn, &critical,
NULL, NULL, NULL, NULL);
if ((warn != prefs.warn) || (critical != prefs.critical)) {
GlueSysBatteryInfo(true, &prefs.warn, &prefs.critical,
NULL, NULL, NULL, NULL);
}
}
}
}
break;
}
return (0);
}
//=============================================================================