2007-06-08 05:15:52 +02:00
|
|
|
/* -*-mode: C; fill-column: 77; c-basic-offset: 4; compile-command: "make ARCH=ARM_ONLY MEMDEBUG=TRUE"; -*- */
|
2003-11-16 20:23:48 +01:00
|
|
|
/*
|
2006-01-08 02:25:02 +01:00
|
|
|
* Copyright 2003 by Eric House (xwords@eehouse.org). All rights reserved.
|
2003-11-16 20:23:48 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2006-10-10 03:34:37 +02:00
|
|
|
#if defined XWFEATURE_BLUETOOTH || defined XWFEATURE_RELAY
|
2003-11-16 20:23:48 +01:00
|
|
|
|
|
|
|
#include <NetMgr.h>
|
|
|
|
|
|
|
|
#include "callback.h"
|
|
|
|
|
|
|
|
#include "connsdlg.h"
|
2006-08-26 23:12:10 +02:00
|
|
|
#include "strutils.h"
|
2003-11-16 20:23:48 +01:00
|
|
|
#include "palmmain.h"
|
|
|
|
#include "palmutil.h"
|
|
|
|
#include "palmir.h"
|
2006-08-26 23:12:10 +02:00
|
|
|
#include "palmbt.h"
|
2003-11-16 20:23:48 +01:00
|
|
|
|
|
|
|
/* When user pops up via Host gadget, we want to get the port to listen on.
|
|
|
|
* When pops up via the Guest gadget, we want to get the port and IP address
|
|
|
|
* of the host AS WELL AS the local port that we'll tell it we're listening
|
|
|
|
* on. We need local state for both, since user can switch between them and
|
|
|
|
* expect state to live as long as the parent dialog isn't exited.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct ConnsDlgState {
|
|
|
|
ListPtr connTypesList;
|
|
|
|
XP_U16 serverRole;
|
|
|
|
XP_Bool isNewGame;
|
2006-10-28 16:48:08 +02:00
|
|
|
CommsConnType conType;
|
2005-04-03 03:58:02 +02:00
|
|
|
CommsAddrRec* addr;
|
2006-08-26 23:12:10 +02:00
|
|
|
XP_BtAddr btAddr; /* since there's no field, save it here */
|
2006-10-15 18:14:42 +02:00
|
|
|
XP_BtAddrStr tmp;
|
2007-06-08 05:15:52 +02:00
|
|
|
char btName[PALM_BT_NAME_LEN];
|
2003-11-16 20:23:48 +01:00
|
|
|
} ConnsDlgState;
|
|
|
|
|
2007-06-08 05:15:52 +02:00
|
|
|
static void
|
|
|
|
strFromField( XP_U16 id, XP_UCHAR* buf, XP_U16 max )
|
|
|
|
{
|
|
|
|
FieldPtr field = getActiveObjectPtr( id );
|
|
|
|
XP_UCHAR* str = FldGetTextPtr( field );
|
|
|
|
XP_U16 len = FldGetTextLength( field );
|
|
|
|
if ( len > max-1 ) {
|
|
|
|
len = max - 1;
|
|
|
|
}
|
|
|
|
XP_MEMCPY( buf, str, len );
|
|
|
|
buf[len] = '\0';
|
|
|
|
} /* strFromField */
|
|
|
|
|
2003-11-16 20:23:48 +01:00
|
|
|
static void
|
2006-09-18 06:40:27 +02:00
|
|
|
ctlsFromState( PalmAppGlobals* XP_UNUSED_BT(globals), ConnsDlgState* state )
|
2003-11-16 20:23:48 +01:00
|
|
|
{
|
2005-04-03 03:58:02 +02:00
|
|
|
CommsAddrRec* addr = state->addr;
|
2006-10-28 16:48:08 +02:00
|
|
|
XP_Bool isNewGame = state->isNewGame;
|
|
|
|
state->conType = addr->conType;
|
2003-11-16 20:23:48 +01:00
|
|
|
|
2006-10-10 03:34:37 +02:00
|
|
|
if ( 0 ) {
|
|
|
|
#ifdef XWFEATURE_RELAY
|
|
|
|
} else if ( addr->conType == COMMS_CONN_RELAY ) {
|
|
|
|
XP_UCHAR buf[16];
|
2006-08-26 23:12:10 +02:00
|
|
|
setFieldStr( XW_CONNS_RELAY_FIELD_ID, addr->u.ip_relay.hostName );
|
|
|
|
setFieldEditable( XW_CONNS_RELAY_FIELD_ID, isNewGame );
|
2003-11-16 20:23:48 +01:00
|
|
|
|
2006-08-26 23:12:10 +02:00
|
|
|
StrPrintF( buf, "%d", addr->u.ip_relay.port );
|
|
|
|
setFieldStr( XW_CONNS_PORT_FIELD_ID, buf );
|
|
|
|
setFieldEditable( XW_CONNS_PORT_FIELD_ID, isNewGame );
|
2003-11-16 20:23:48 +01:00
|
|
|
|
2006-08-26 23:12:10 +02:00
|
|
|
setFieldStr( XW_CONNS_COOKIE_FIELD_ID, addr->u.ip_relay.cookie );
|
|
|
|
setFieldEditable( XW_CONNS_COOKIE_FIELD_ID, isNewGame );
|
2006-10-10 03:34:37 +02:00
|
|
|
#endif
|
2006-08-26 23:12:10 +02:00
|
|
|
#ifdef XWFEATURE_BLUETOOTH
|
|
|
|
} else if ( addr->conType == COMMS_CONN_BT
|
|
|
|
&& state->serverRole == SERVER_ISCLIENT ) {
|
2006-10-15 18:14:42 +02:00
|
|
|
ControlPtr ctrl = getActiveObjectPtr( XW_CONNS_BT_HOSTTRIGGER_ID );
|
2006-09-08 09:02:16 +02:00
|
|
|
if ( '\0' == addr->u.bt.hostName[0] ) {
|
2006-10-15 18:14:42 +02:00
|
|
|
palm_bt_addrString( globals, &addr->u.bt.btAddr, &state->tmp );
|
|
|
|
CtlSetLabel( ctrl, state->tmp.chars );
|
2006-09-08 09:02:16 +02:00
|
|
|
} else {
|
2006-10-15 18:14:42 +02:00
|
|
|
CtlSetLabel( ctrl, addr->u.bt.hostName );
|
2006-09-08 09:02:16 +02:00
|
|
|
}
|
2006-10-28 16:48:08 +02:00
|
|
|
CtlSetEnabled( ctrl, isNewGame );
|
2006-08-26 23:12:10 +02:00
|
|
|
#endif
|
|
|
|
}
|
2003-11-16 20:23:48 +01:00
|
|
|
} /* ctlsFromState */
|
|
|
|
|
2006-10-28 16:48:08 +02:00
|
|
|
static void
|
2006-08-26 23:12:10 +02:00
|
|
|
stateFromCtls( ConnsDlgState* state )
|
2003-11-16 20:23:48 +01:00
|
|
|
{
|
2005-04-03 03:58:02 +02:00
|
|
|
CommsAddrRec* addr = state->addr;
|
2003-11-16 20:23:48 +01:00
|
|
|
|
2006-10-28 16:48:08 +02:00
|
|
|
addr->conType = state->conType;
|
|
|
|
|
2006-10-10 03:34:37 +02:00
|
|
|
if ( 0 ) {
|
|
|
|
#ifdef XWFEATURE_RELAY
|
|
|
|
} else if ( addr->conType == COMMS_CONN_RELAY ) {
|
|
|
|
XP_UCHAR buf[16];
|
2006-08-26 23:12:10 +02:00
|
|
|
strFromField( XW_CONNS_RELAY_FIELD_ID, addr->u.ip_relay.hostName,
|
|
|
|
sizeof(addr->u.ip_relay.hostName) );
|
|
|
|
|
|
|
|
strFromField( XW_CONNS_PORT_FIELD_ID, buf, sizeof(buf) );
|
|
|
|
addr->u.ip_relay.port = StrAToI( buf );
|
|
|
|
|
|
|
|
strFromField( XW_CONNS_COOKIE_FIELD_ID, addr->u.ip_relay.cookie,
|
|
|
|
sizeof(addr->u.ip_relay.cookie) );
|
2006-10-10 03:34:37 +02:00
|
|
|
#endif
|
2006-08-26 23:12:10 +02:00
|
|
|
#ifdef XWFEATURE_BLUETOOTH
|
|
|
|
} else if ( addr->conType == COMMS_CONN_BT
|
|
|
|
&& state->serverRole == SERVER_ISCLIENT ) {
|
|
|
|
/* Not exactly from a control... */
|
2006-10-15 18:14:42 +02:00
|
|
|
XP_MEMCPY( addr->u.bt.hostName, state->btName,
|
|
|
|
sizeof(addr->u.bt.hostName) );
|
2006-09-14 03:31:13 +02:00
|
|
|
XP_MEMCPY( &addr->u.bt.btAddr, &state->btAddr,
|
2006-08-26 23:12:10 +02:00
|
|
|
sizeof(addr->u.bt.btAddr) );
|
2006-09-14 03:31:13 +02:00
|
|
|
LOG_HEX( &addr->u.bt.btAddr, sizeof(addr->u.bt.btAddr), __FUNCTION__ );
|
2006-08-26 23:12:10 +02:00
|
|
|
#endif
|
|
|
|
}
|
2003-11-16 20:23:48 +01:00
|
|
|
} /* stateFromCtls */
|
|
|
|
|
|
|
|
static void
|
|
|
|
updateFormCtls( FormPtr form, ConnsDlgState* state )
|
|
|
|
{
|
2005-04-03 03:58:02 +02:00
|
|
|
const XP_U16 relayCtls[] = {
|
|
|
|
XW_CONNS_RELAY_LABEL_ID ,
|
2006-10-10 03:34:37 +02:00
|
|
|
#ifdef XWFEATURE_RELAY
|
2005-04-03 03:58:02 +02:00
|
|
|
XW_CONNS_RELAY_FIELD_ID,
|
|
|
|
XW_CONNS_PORT_LABEL_ID,
|
|
|
|
XW_CONNS_PORT_FIELD_ID,
|
|
|
|
XW_CONNS_COOKIE_LABEL_ID,
|
|
|
|
XW_CONNS_COOKIE_FIELD_ID,
|
2006-10-10 03:34:37 +02:00
|
|
|
#endif
|
2003-11-16 20:23:48 +01:00
|
|
|
0
|
|
|
|
};
|
2006-08-26 23:12:10 +02:00
|
|
|
const XP_U16 btGuestCtls[] = {
|
|
|
|
#ifdef XWFEATURE_BLUETOOTH
|
|
|
|
XW_CONNS_BT_HOSTNAME_LABEL_ID,
|
2006-10-15 18:14:42 +02:00
|
|
|
XW_CONNS_BT_HOSTTRIGGER_ID,
|
2006-09-18 06:40:27 +02:00
|
|
|
#else
|
|
|
|
XW_CONNS_BT_NOTSUPPORT_LABEL_ID,
|
2006-08-26 23:12:10 +02:00
|
|
|
#endif
|
|
|
|
0
|
|
|
|
};
|
|
|
|
const XP_U16* allCtls[] = {
|
|
|
|
relayCtls, btGuestCtls
|
|
|
|
};
|
|
|
|
const XP_U16* on;
|
|
|
|
XP_U16 i;
|
|
|
|
|
2006-10-28 16:48:08 +02:00
|
|
|
if ( state->conType == COMMS_CONN_RELAY ) {
|
2006-08-26 23:12:10 +02:00
|
|
|
on = relayCtls;
|
2006-10-28 16:48:08 +02:00
|
|
|
} else if ( state->conType == COMMS_CONN_BT
|
2006-09-18 06:40:27 +02:00
|
|
|
#ifdef XWFEATURE_BLUETOOTH
|
|
|
|
&& state->serverRole == SERVER_ISCLIENT
|
2006-08-26 23:12:10 +02:00
|
|
|
#endif
|
2006-09-18 06:40:27 +02:00
|
|
|
) {
|
|
|
|
on = btGuestCtls;
|
2006-08-26 23:12:10 +02:00
|
|
|
} else {
|
|
|
|
on = NULL;
|
|
|
|
}
|
|
|
|
|
2007-05-26 16:03:07 +02:00
|
|
|
for ( i = 0; i < VSIZE(allCtls); ++i ) {
|
2006-08-26 23:12:10 +02:00
|
|
|
const XP_U16* cur = allCtls[i];
|
|
|
|
if ( cur != on ) {
|
|
|
|
disOrEnableSet( form, cur, XP_FALSE );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( on != NULL ) {
|
|
|
|
disOrEnableSet( form, on, XP_TRUE );
|
|
|
|
}
|
|
|
|
|
2003-11-16 20:23:48 +01:00
|
|
|
} /* updateFormCtls */
|
|
|
|
|
|
|
|
static void
|
|
|
|
cleanupExit( PalmAppGlobals* globals )
|
|
|
|
{
|
|
|
|
XP_FREE( globals->mpool, globals->connState );
|
|
|
|
globals->connState = NULL;
|
|
|
|
FrmReturnToForm( 0 );
|
|
|
|
} /* cleanupExit */
|
|
|
|
|
2006-08-23 06:44:55 +02:00
|
|
|
static XP_U16
|
|
|
|
conTypeToSel( CommsConnType conType )
|
|
|
|
{
|
2006-08-26 23:12:10 +02:00
|
|
|
XP_U16 result = 0;
|
2006-08-23 06:44:55 +02:00
|
|
|
switch ( conType ) {
|
2006-08-26 23:12:10 +02:00
|
|
|
case COMMS_CONN_BT: /* result = 0; */break;
|
2006-08-23 06:44:55 +02:00
|
|
|
case COMMS_CONN_IR: result = 1; break;
|
|
|
|
case COMMS_CONN_RELAY: result = 2; break;
|
|
|
|
default:
|
|
|
|
XP_ASSERT(0);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
} /* conTypeToSel */
|
|
|
|
|
|
|
|
static CommsConnType
|
|
|
|
selToConType( XP_U16 sel )
|
|
|
|
{
|
2006-09-18 06:40:27 +02:00
|
|
|
CommsConnType conType = COMMS_CONN_BT;
|
2006-08-23 06:44:55 +02:00
|
|
|
switch( sel ) {
|
2006-09-18 06:40:27 +02:00
|
|
|
case 0: /* conType = COMMS_CONN_BT; */break;
|
2006-08-23 06:44:55 +02:00
|
|
|
case 1: conType = COMMS_CONN_IR; break;
|
|
|
|
case 2: conType = COMMS_CONN_RELAY; break;
|
|
|
|
default: XP_ASSERT(0);
|
|
|
|
}
|
|
|
|
return conType;
|
2006-09-18 06:40:27 +02:00
|
|
|
} /* selToConType */
|
2006-08-23 06:44:55 +02:00
|
|
|
|
2006-08-26 23:12:10 +02:00
|
|
|
#ifdef XWFEATURE_BLUETOOTH
|
|
|
|
static void
|
|
|
|
browseForDeviceName( PalmAppGlobals* globals, ConnsDlgState* state )
|
|
|
|
{
|
|
|
|
XP_BtAddr btAddr;
|
2006-10-15 18:14:42 +02:00
|
|
|
if ( palm_bt_browse_device( globals, &btAddr,
|
|
|
|
state->btName, sizeof( state->btName ) ) ) {
|
|
|
|
CtlSetLabel( getActiveObjectPtr( XW_CONNS_BT_HOSTTRIGGER_ID ),
|
|
|
|
state->btName );
|
2006-09-14 03:31:13 +02:00
|
|
|
XP_MEMCPY( &state->btAddr, &btAddr, sizeof(state->btAddr) );
|
|
|
|
LOG_HEX( &state->btAddr, sizeof(state->btAddr), __FUNCTION__ );
|
2006-08-26 23:12:10 +02:00
|
|
|
}
|
|
|
|
} /* browseForDeviceName */
|
|
|
|
#endif
|
|
|
|
|
2003-11-16 20:23:48 +01:00
|
|
|
Boolean
|
|
|
|
ConnsFormHandleEvent( EventPtr event )
|
|
|
|
{
|
|
|
|
Boolean result;
|
|
|
|
PalmAppGlobals* globals;
|
|
|
|
ConnsDlgState* state;
|
|
|
|
FormPtr form;
|
|
|
|
XP_S16 chosen;
|
|
|
|
|
|
|
|
CALLBACK_PROLOGUE();
|
|
|
|
|
|
|
|
globals = getFormRefcon();
|
|
|
|
state = globals->connState;
|
|
|
|
if ( !state ) {
|
|
|
|
state = globals->connState = XP_MALLOC( globals->mpool,
|
|
|
|
sizeof(*state) );
|
|
|
|
XP_MEMSET( state, 0, sizeof(*state) );
|
|
|
|
}
|
|
|
|
|
|
|
|
form = FrmGetActiveForm();
|
|
|
|
|
|
|
|
switch ( event->eType ) {
|
|
|
|
case frmOpenEvent:
|
|
|
|
state->serverRole =
|
2007-02-02 09:34:37 +01:00
|
|
|
(DeviceRole)globals->dlgParams[CONNS_PARAM_ROLE_INDEX];
|
2005-04-03 03:58:02 +02:00
|
|
|
state->addr =
|
|
|
|
(CommsAddrRec*)globals->dlgParams[CONNS_PARAM_ADDR_INDEX];
|
2003-11-16 20:23:48 +01:00
|
|
|
state->isNewGame = globals->isNewGame;
|
2006-09-14 03:31:13 +02:00
|
|
|
XP_MEMCPY( &state->btAddr, &state->addr->u.bt.btAddr,
|
2006-08-26 23:12:10 +02:00
|
|
|
sizeof(state->btAddr) );
|
2003-11-16 20:23:48 +01:00
|
|
|
|
2006-10-28 16:48:08 +02:00
|
|
|
ctlsFromState( globals, state );
|
|
|
|
|
2003-11-16 20:23:48 +01:00
|
|
|
/* setup connection popup */
|
|
|
|
state->connTypesList = getActiveObjectPtr( XW_CONNS_TYPE_LIST_ID );
|
2006-10-28 16:48:08 +02:00
|
|
|
XP_ASSERT( state->conType == COMMS_CONN_IR
|
|
|
|
|| state->conType == COMMS_CONN_RELAY
|
|
|
|
|| state->conType == COMMS_CONN_BT );
|
2003-11-16 20:23:48 +01:00
|
|
|
setSelectorFromList( XW_CONNS_TYPE_TRIGGER_ID, state->connTypesList,
|
2006-10-28 16:48:08 +02:00
|
|
|
conTypeToSel(state->conType) );
|
2003-11-16 20:23:48 +01:00
|
|
|
|
|
|
|
updateFormCtls( form, state );
|
|
|
|
|
|
|
|
case frmUpdateEvent:
|
|
|
|
FrmDrawForm( form );
|
|
|
|
result = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ctlSelectEvent:
|
|
|
|
result = true;
|
|
|
|
switch ( event->data.ctlSelect.controlID ) {
|
|
|
|
|
2006-08-26 23:12:10 +02:00
|
|
|
#ifdef XWFEATURE_BLUETOOTH
|
2006-10-15 18:14:42 +02:00
|
|
|
case XW_CONNS_BT_HOSTTRIGGER_ID:
|
2006-10-28 16:48:08 +02:00
|
|
|
if ( state->isNewGame ) {
|
|
|
|
browseForDeviceName( globals, state );
|
|
|
|
}
|
2006-08-26 23:12:10 +02:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2003-11-16 20:23:48 +01:00
|
|
|
case XW_CONNS_TYPE_TRIGGER_ID:
|
|
|
|
if ( state->isNewGame ) {
|
|
|
|
chosen = LstPopupList( state->connTypesList );
|
|
|
|
if ( chosen >= 0 ) {
|
|
|
|
setSelectorFromList( XW_CONNS_TYPE_TRIGGER_ID,
|
|
|
|
state->connTypesList, chosen );
|
2006-10-28 16:48:08 +02:00
|
|
|
state->conType = selToConType( chosen );
|
2003-11-16 20:23:48 +01:00
|
|
|
updateFormCtls( form, state );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XW_CONNS_OK_BUTTON_ID:
|
|
|
|
if ( !state->isNewGame ) {
|
|
|
|
/* do nothing; same as cancel */
|
|
|
|
} else {
|
2006-10-28 16:48:08 +02:00
|
|
|
EventType eventToPost = { .eType = connsSettingChgEvent };
|
|
|
|
stateFromCtls( state );
|
2003-11-16 20:23:48 +01:00
|
|
|
EvtAddEventToQueue( &eventToPost );
|
|
|
|
}
|
2006-10-28 16:48:08 +02:00
|
|
|
/* FALLTHRU */
|
2003-11-16 20:23:48 +01:00
|
|
|
case XW_CONNS_CANCEL_BUTTON_ID:
|
|
|
|
cleanupExit( globals );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CALLBACK_EPILOGUE();
|
|
|
|
return result;
|
|
|
|
} /* ConnsFormHandleEvent */
|
|
|
|
|
2006-10-10 03:34:37 +02:00
|
|
|
#endif /* #if defined XWFEATURE_BLUETOOTH || defined XWFEATURE_RELAY */
|