2013-01-07 15:10:44 +01:00
|
|
|
/* -*- compile-command: "make MEMDEBUG=TRUE -j3"; -*- */
|
2003-11-01 06:35:29 +01:00
|
|
|
/*
|
2013-01-06 01:08:19 +01:00
|
|
|
* Copyright 2000-2013 by Eric House (xwords@eehouse.org). All rights
|
|
|
|
* reserved.
|
2003-11-01 06:35:29 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef PLATFORM_GTK
|
|
|
|
|
2013-01-06 01:08:19 +01:00
|
|
|
#include "main.h"
|
2013-01-16 15:46:33 +01:00
|
|
|
#include "gtkmain.h"
|
2013-01-06 01:08:19 +01:00
|
|
|
#include "gamesdb.h"
|
|
|
|
#include "gtkboard.h"
|
|
|
|
#include "linuxmain.h"
|
2013-01-16 15:46:33 +01:00
|
|
|
#include "relaycon.h"
|
2013-01-20 00:18:36 +01:00
|
|
|
#include "gtkask.h"
|
2011-08-30 05:36:01 +02:00
|
|
|
|
2013-01-16 04:04:20 +01:00
|
|
|
static void onNewData( GtkAppGlobals* apg, sqlite3_int64 rowid,
|
2013-01-09 15:30:52 +01:00
|
|
|
XP_Bool isNew );
|
2013-01-17 06:16:07 +01:00
|
|
|
static void updateButtons( GtkAppGlobals* apg );
|
2013-01-09 15:30:52 +01:00
|
|
|
|
2013-01-07 15:36:34 +01:00
|
|
|
static void
|
2013-01-16 04:04:20 +01:00
|
|
|
recordOpened( GtkAppGlobals* apg, GtkGameGlobals* globals )
|
2013-01-07 15:36:34 +01:00
|
|
|
{
|
2013-01-16 04:04:20 +01:00
|
|
|
apg->globalsList = g_slist_prepend( apg->globalsList, globals );
|
|
|
|
globals->apg = apg;
|
2013-01-07 15:36:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-01-16 04:04:20 +01:00
|
|
|
recordClosed( GtkAppGlobals* apg, GtkGameGlobals* globals )
|
2013-01-07 15:36:34 +01:00
|
|
|
{
|
2013-01-16 04:04:20 +01:00
|
|
|
apg->globalsList = g_slist_remove( apg->globalsList, globals );
|
2013-01-07 15:36:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static XP_Bool
|
2013-01-16 04:04:20 +01:00
|
|
|
gameIsOpen( GtkAppGlobals* apg, sqlite3_int64 rowid )
|
2013-01-07 15:36:34 +01:00
|
|
|
{
|
|
|
|
XP_Bool found = XP_FALSE;
|
|
|
|
GSList* iter;
|
2013-01-16 04:04:20 +01:00
|
|
|
for ( iter = apg->globalsList; !!iter && !found; iter = iter->next ) {
|
|
|
|
GtkGameGlobals* globals = (GtkGameGlobals*)iter->data;
|
2013-01-07 15:36:34 +01:00
|
|
|
found = globals->cGlobals.selRow == rowid;
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2013-01-16 15:46:33 +01:00
|
|
|
static GtkGameGlobals*
|
2013-07-12 05:01:17 +02:00
|
|
|
findOpenGame( const GtkAppGlobals* apg, sqlite3_int64 rowid )
|
2013-01-16 15:46:33 +01:00
|
|
|
{
|
|
|
|
GtkGameGlobals* result = NULL;
|
|
|
|
GSList* iter;
|
|
|
|
for ( iter = apg->globalsList; !!iter; iter = iter->next ) {
|
|
|
|
GtkGameGlobals* globals = (GtkGameGlobals*)iter->data;
|
|
|
|
CommonGlobals* cGlobals = &globals->cGlobals;
|
2013-07-12 05:01:17 +02:00
|
|
|
if ( cGlobals->selRow == rowid ) {
|
2013-01-16 15:46:33 +01:00
|
|
|
result = globals;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-07-12 05:01:17 +02:00
|
|
|
enum { ROW_ITEM, NAME_ITEM, ROOM_ITEM, SEED_ITEM, OVER_ITEM, TURN_ITEM,
|
|
|
|
NMOVES_ITEM, MISSING_ITEM, N_ITEMS };
|
2013-01-17 06:16:07 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
foreachProc( GtkTreeModel* model, GtkTreePath* XP_UNUSED(path),
|
|
|
|
GtkTreeIter* iter, gpointer data )
|
|
|
|
{
|
|
|
|
GtkAppGlobals* apg = (GtkAppGlobals*)data;
|
|
|
|
sqlite3_int64 rowid;
|
|
|
|
gtk_tree_model_get( model, iter, ROW_ITEM, &rowid, -1 );
|
|
|
|
apg->selRows = g_array_append_val( apg->selRows, rowid );
|
|
|
|
}
|
2011-09-03 03:36:03 +02:00
|
|
|
|
2013-01-08 15:32:43 +01:00
|
|
|
static void
|
2013-01-06 06:01:26 +01:00
|
|
|
tree_selection_changed_cb( GtkTreeSelection* selection, gpointer data )
|
|
|
|
{
|
2013-01-16 04:04:20 +01:00
|
|
|
GtkAppGlobals* apg = (GtkAppGlobals*)data;
|
2013-01-06 06:01:26 +01:00
|
|
|
|
2013-01-17 06:16:07 +01:00
|
|
|
apg->selRows = g_array_set_size( apg->selRows, 0 );
|
|
|
|
gtk_tree_selection_selected_foreach( selection, foreachProc, apg );
|
|
|
|
|
|
|
|
updateButtons( apg );
|
|
|
|
}
|
2013-01-06 06:01:26 +01:00
|
|
|
|
2013-01-17 06:16:07 +01:00
|
|
|
static void
|
|
|
|
removeRow( GtkAppGlobals* apg, sqlite3_int64 rowid )
|
|
|
|
{
|
|
|
|
GtkTreeModel* model =
|
|
|
|
gtk_tree_view_get_model(GTK_TREE_VIEW(apg->listWidget));
|
|
|
|
GtkTreeIter iter;
|
|
|
|
gboolean valid;
|
|
|
|
for ( valid = gtk_tree_model_get_iter_first( model, &iter );
|
|
|
|
valid;
|
|
|
|
valid = gtk_tree_model_iter_next( model, &iter ) ) {
|
|
|
|
sqlite3_int64 tmpid;
|
|
|
|
gtk_tree_model_get( model, &iter, ROW_ITEM, &tmpid, -1 );
|
|
|
|
if ( tmpid == rowid ) {
|
|
|
|
gtk_list_store_remove( GTK_LIST_STORE(model), &iter );
|
|
|
|
break;
|
|
|
|
}
|
2013-01-06 06:01:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-09 15:30:52 +01:00
|
|
|
static void
|
|
|
|
addTextColumn( GtkWidget* list, const gchar* title, int item )
|
|
|
|
{
|
|
|
|
GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
|
|
|
|
GtkTreeViewColumn* column =
|
|
|
|
gtk_tree_view_column_new_with_attributes( title, renderer, "text",
|
|
|
|
item, NULL );
|
|
|
|
gtk_tree_view_append_column( GTK_TREE_VIEW(list), column );
|
|
|
|
}
|
|
|
|
|
2013-01-06 06:01:26 +01:00
|
|
|
static GtkWidget*
|
2013-01-16 04:04:20 +01:00
|
|
|
init_games_list( GtkAppGlobals* apg )
|
2011-10-14 04:14:08 +02:00
|
|
|
{
|
2013-01-06 06:01:26 +01:00
|
|
|
GtkWidget* list = gtk_tree_view_new();
|
2013-01-08 15:32:43 +01:00
|
|
|
|
2013-01-09 15:30:52 +01:00
|
|
|
addTextColumn( list, "Row", ROW_ITEM );
|
|
|
|
addTextColumn( list, "Name", NAME_ITEM );
|
|
|
|
addTextColumn( list, "Room", ROOM_ITEM );
|
2013-07-12 05:01:17 +02:00
|
|
|
addTextColumn( list, "Seed", SEED_ITEM );
|
2013-01-09 15:30:52 +01:00
|
|
|
addTextColumn( list, "Ended", OVER_ITEM );
|
|
|
|
addTextColumn( list, "Turn", TURN_ITEM );
|
|
|
|
addTextColumn( list, "NMoves", NMOVES_ITEM );
|
|
|
|
addTextColumn( list, "NMissing", MISSING_ITEM );
|
|
|
|
|
2013-01-17 06:16:07 +01:00
|
|
|
GtkListStore* store = gtk_list_store_new( N_ITEMS,
|
2013-07-12 05:01:17 +02:00
|
|
|
G_TYPE_INT64, /* ROW_ITEM */
|
|
|
|
G_TYPE_STRING, /* NAME_ITEM */
|
|
|
|
G_TYPE_STRING, /* ROOM_ITEM */
|
|
|
|
G_TYPE_INT, /* SEED_ITEM */
|
|
|
|
G_TYPE_BOOLEAN, /* OVER_ITEM */
|
|
|
|
G_TYPE_INT, /* TURN_ITEM */
|
|
|
|
G_TYPE_INT, /* NMOVES_ITEM */
|
|
|
|
G_TYPE_INT /* MISSING_ITEM */
|
|
|
|
);
|
2013-01-06 01:08:19 +01:00
|
|
|
gtk_tree_view_set_model( GTK_TREE_VIEW(list), GTK_TREE_MODEL(store) );
|
|
|
|
g_object_unref( store );
|
2013-01-06 06:01:26 +01:00
|
|
|
|
2013-01-08 15:32:43 +01:00
|
|
|
GtkTreeSelection* select =
|
2013-01-06 06:01:26 +01:00
|
|
|
gtk_tree_view_get_selection( GTK_TREE_VIEW (list) );
|
2013-01-17 06:16:07 +01:00
|
|
|
gtk_tree_selection_set_mode( select, GTK_SELECTION_MULTIPLE );
|
2013-01-06 06:01:26 +01:00
|
|
|
g_signal_connect( G_OBJECT(select), "changed",
|
2013-01-17 06:16:07 +01:00
|
|
|
G_CALLBACK(tree_selection_changed_cb), apg );
|
2013-01-06 06:01:26 +01:00
|
|
|
return list;
|
2011-08-30 05:36:01 +02:00
|
|
|
}
|
2003-11-01 06:35:29 +01:00
|
|
|
|
2013-01-06 04:40:29 +01:00
|
|
|
static void
|
2013-01-09 15:30:52 +01:00
|
|
|
add_to_list( GtkWidget* list, sqlite3_int64 rowid, XP_Bool isNew,
|
|
|
|
const GameInfo* gib )
|
2013-01-06 04:40:29 +01:00
|
|
|
{
|
2013-01-09 15:30:52 +01:00
|
|
|
GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list));
|
|
|
|
GtkListStore* store = GTK_LIST_STORE( model );
|
2013-01-06 04:40:29 +01:00
|
|
|
GtkTreeIter iter;
|
2013-01-09 15:30:52 +01:00
|
|
|
if ( isNew ) {
|
|
|
|
gtk_list_store_append( store, &iter );
|
|
|
|
} else {
|
|
|
|
gboolean valid;
|
|
|
|
for ( valid = gtk_tree_model_get_iter_first( model, &iter );
|
|
|
|
valid;
|
|
|
|
valid = gtk_tree_model_iter_next( model, &iter ) ) {
|
|
|
|
sqlite3_int64 tmpid;
|
|
|
|
gtk_tree_model_get( model, &iter, ROW_ITEM, &tmpid, -1 );
|
|
|
|
if ( tmpid == rowid ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-06 04:40:29 +01:00
|
|
|
gtk_list_store_set( store, &iter,
|
2013-01-09 15:30:52 +01:00
|
|
|
ROW_ITEM, rowid,
|
|
|
|
NAME_ITEM, gib->name,
|
|
|
|
ROOM_ITEM, gib->room,
|
2013-07-12 05:01:17 +02:00
|
|
|
SEED_ITEM, gib->seed,
|
2013-01-09 15:30:52 +01:00
|
|
|
OVER_ITEM, gib->gameOver,
|
|
|
|
TURN_ITEM, gib->turn,
|
|
|
|
NMOVES_ITEM, gib->nMoves,
|
|
|
|
MISSING_ITEM, gib->nMissing,
|
2013-01-06 04:40:29 +01:00
|
|
|
-1 );
|
|
|
|
XP_LOGF( "DONE adding" );
|
|
|
|
}
|
|
|
|
|
2013-01-17 06:16:07 +01:00
|
|
|
static void updateButtons( GtkAppGlobals* apg )
|
|
|
|
{
|
|
|
|
guint count = apg->selRows->len;
|
|
|
|
|
|
|
|
gtk_widget_set_sensitive( apg->openButton, 1 == count );
|
|
|
|
gtk_widget_set_sensitive( apg->deleteButton, 1 <= count );
|
|
|
|
}
|
|
|
|
|
2003-11-01 06:35:29 +01:00
|
|
|
static void
|
2013-01-06 01:08:19 +01:00
|
|
|
handle_newgame_button( GtkWidget* XP_UNUSED(widget), void* closure )
|
2003-11-01 06:35:29 +01:00
|
|
|
{
|
2013-01-16 04:04:20 +01:00
|
|
|
GtkAppGlobals* apg = (GtkAppGlobals*)closure;
|
2013-01-06 01:08:19 +01:00
|
|
|
XP_LOGF( "%s called", __func__ );
|
2013-01-16 04:04:20 +01:00
|
|
|
GtkGameGlobals* globals = malloc( sizeof(*globals) );
|
|
|
|
apg->params->needsNewGame = XP_FALSE;
|
|
|
|
initGlobals( globals, apg->params );
|
2013-01-06 01:08:19 +01:00
|
|
|
if ( !makeNewGame( globals ) ) {
|
|
|
|
freeGlobals( globals );
|
2005-09-14 07:11:29 +02:00
|
|
|
} else {
|
2013-01-06 01:08:19 +01:00
|
|
|
GtkWidget* gameWindow = globals->window;
|
2013-01-29 16:42:10 +01:00
|
|
|
globals->cGlobals.pDb = apg->params->pDb;
|
2013-01-06 06:01:26 +01:00
|
|
|
globals->cGlobals.selRow = -1;
|
2013-01-16 04:04:20 +01:00
|
|
|
recordOpened( apg, globals );
|
2013-01-06 01:08:19 +01:00
|
|
|
gtk_widget_show( gameWindow );
|
2003-11-01 06:35:29 +01:00
|
|
|
}
|
2011-10-20 03:34:26 +02:00
|
|
|
}
|
|
|
|
|
2013-01-06 06:01:26 +01:00
|
|
|
static void
|
|
|
|
handle_open_button( GtkWidget* XP_UNUSED(widget), void* closure )
|
|
|
|
{
|
2013-01-16 04:04:20 +01:00
|
|
|
GtkAppGlobals* apg = (GtkAppGlobals*)closure;
|
2013-01-17 06:16:07 +01:00
|
|
|
sqlite3_int64 selRow = getSelRow( apg );
|
|
|
|
if ( -1 != selRow && !gameIsOpen( apg, selRow ) ) {
|
2013-01-16 04:04:20 +01:00
|
|
|
apg->params->needsNewGame = XP_FALSE;
|
|
|
|
GtkGameGlobals* globals = malloc( sizeof(*globals) );
|
|
|
|
initGlobals( globals, apg->params );
|
2013-01-29 16:42:10 +01:00
|
|
|
globals->cGlobals.pDb = apg->params->pDb;
|
2013-01-17 06:16:07 +01:00
|
|
|
globals->cGlobals.selRow = selRow;
|
2013-01-16 04:04:20 +01:00
|
|
|
recordOpened( apg, globals );
|
2013-01-06 06:01:26 +01:00
|
|
|
gtk_widget_show( globals->window );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-06 01:08:47 +01:00
|
|
|
static void
|
2013-01-17 06:16:07 +01:00
|
|
|
handle_delete_button( GtkWidget* XP_UNUSED(widget), void* closure )
|
2013-01-06 01:08:47 +01:00
|
|
|
{
|
2013-01-17 06:16:07 +01:00
|
|
|
GtkAppGlobals* apg = (GtkAppGlobals*)closure;
|
2013-01-29 16:42:10 +01:00
|
|
|
LaunchParams* params = apg->params;
|
2013-01-17 06:16:07 +01:00
|
|
|
guint len = apg->selRows->len;
|
|
|
|
for ( guint ii = 0; ii < len; ++ii ) {
|
|
|
|
sqlite3_int64 rowid = g_array_index( apg->selRows, sqlite3_int64, ii );
|
2013-07-12 05:01:17 +02:00
|
|
|
|
|
|
|
GameInfo gib;
|
2013-07-17 15:52:35 +02:00
|
|
|
#ifdef DEBUG
|
|
|
|
XP_Bool success =
|
|
|
|
#endif
|
|
|
|
getGameInfo( params->pDb, rowid, &gib );
|
2013-07-12 05:01:17 +02:00
|
|
|
XP_ASSERT( success );
|
|
|
|
XP_U32 clientToken = makeClientToken( rowid, gib.seed );
|
2013-01-17 06:16:07 +01:00
|
|
|
removeRow( apg, rowid );
|
2013-01-29 16:42:10 +01:00
|
|
|
deleteGame( params->pDb, rowid );
|
|
|
|
|
|
|
|
XP_UCHAR devIDBuf[64] = {0};
|
|
|
|
db_fetch( params->pDb, KEY_RDEVID, devIDBuf, sizeof(devIDBuf) );
|
|
|
|
if ( '\0' != devIDBuf[0] ) {
|
2013-07-12 05:01:17 +02:00
|
|
|
relaycon_deleted( params, devIDBuf, clientToken );
|
2013-01-29 16:42:10 +01:00
|
|
|
} else {
|
|
|
|
XP_LOGF( "%s: not calling relaycon_deleted: no relayID", __func__ );
|
|
|
|
}
|
2013-01-17 06:16:07 +01:00
|
|
|
}
|
|
|
|
apg->selRows = g_array_set_size( apg->selRows, 0 );
|
|
|
|
updateButtons( apg );
|
|
|
|
/* Need now to update the selection and sync the buttons */
|
2013-01-06 01:08:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_destroy( GtkWidget* XP_UNUSED(widget), gpointer data )
|
|
|
|
{
|
|
|
|
LOG_FUNC();
|
2013-01-16 04:04:20 +01:00
|
|
|
GtkAppGlobals* apg = (GtkAppGlobals*)data;
|
2013-01-07 15:36:34 +01:00
|
|
|
GSList* iter;
|
2013-01-16 04:04:20 +01:00
|
|
|
for ( iter = apg->globalsList; !!iter; iter = iter->next ) {
|
|
|
|
GtkGameGlobals* globals = (GtkGameGlobals*)iter->data;
|
2013-01-18 16:49:00 +01:00
|
|
|
destroy_board_window( NULL, globals );
|
|
|
|
// freeGlobals( globals );
|
2013-01-07 15:36:34 +01:00
|
|
|
}
|
2013-01-16 04:04:20 +01:00
|
|
|
g_slist_free( apg->globalsList );
|
2013-01-06 01:08:47 +01:00
|
|
|
gtk_main_quit();
|
|
|
|
}
|
|
|
|
|
2013-01-07 15:10:44 +01:00
|
|
|
static void
|
2013-01-17 06:16:07 +01:00
|
|
|
handle_quit_button( GtkWidget* XP_UNUSED(widget), gpointer data )
|
|
|
|
{
|
|
|
|
GtkAppGlobals* apg = (GtkAppGlobals*)data;
|
|
|
|
handle_destroy( NULL, apg );
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkWidget*
|
2013-01-07 15:10:44 +01:00
|
|
|
addButton( gchar* label, GtkWidget* parent, GCallback proc, void* closure )
|
|
|
|
{
|
|
|
|
GtkWidget* button = gtk_button_new_with_label( label );
|
|
|
|
gtk_container_add( GTK_CONTAINER(parent), button );
|
|
|
|
g_signal_connect( GTK_OBJECT(button), "clicked",
|
|
|
|
G_CALLBACK(proc), closure );
|
|
|
|
gtk_widget_show( button );
|
2013-01-17 06:16:07 +01:00
|
|
|
return button;
|
2013-01-07 15:10:44 +01:00
|
|
|
}
|
2013-01-06 01:08:47 +01:00
|
|
|
|
2003-11-01 06:35:29 +01:00
|
|
|
static GtkWidget*
|
2013-01-16 04:04:20 +01:00
|
|
|
makeGamesWindow( GtkAppGlobals* apg )
|
2003-11-01 06:35:29 +01:00
|
|
|
{
|
2013-01-06 01:08:19 +01:00
|
|
|
GtkWidget* window;
|
2003-11-01 06:35:29 +01:00
|
|
|
|
2013-01-06 01:08:19 +01:00
|
|
|
window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
|
2013-01-06 01:08:47 +01:00
|
|
|
g_signal_connect( G_OBJECT(window), "destroy",
|
2013-01-16 04:04:20 +01:00
|
|
|
G_CALLBACK(handle_destroy), apg );
|
2013-01-17 06:16:07 +01:00
|
|
|
|
|
|
|
if ( !!apg->params->dbName ) {
|
|
|
|
gtk_window_set_title( GTK_WINDOW(window), apg->params->dbName );
|
|
|
|
}
|
2013-01-06 01:08:47 +01:00
|
|
|
|
2013-01-06 01:08:19 +01:00
|
|
|
GtkWidget* vbox = gtk_vbox_new( FALSE, 0 );
|
|
|
|
gtk_container_add( GTK_CONTAINER(window), vbox );
|
2003-11-01 06:35:29 +01:00
|
|
|
gtk_widget_show( vbox );
|
2013-01-16 04:04:20 +01:00
|
|
|
GtkWidget* list = init_games_list( apg );
|
|
|
|
apg->listWidget = list;
|
2013-01-06 01:08:19 +01:00
|
|
|
gtk_container_add( GTK_CONTAINER(vbox), list );
|
2013-01-06 06:01:26 +01:00
|
|
|
|
2013-01-06 01:08:47 +01:00
|
|
|
gtk_widget_show( list );
|
|
|
|
|
2013-01-29 16:42:10 +01:00
|
|
|
GSList* games = listGames( apg->params->pDb );
|
2013-01-06 04:40:29 +01:00
|
|
|
for ( GSList* iter = games; !!iter; iter = iter->next ) {
|
|
|
|
sqlite3_int64* rowid = (sqlite3_int64*)iter->data;
|
2013-01-16 04:04:20 +01:00
|
|
|
onNewData( apg, *rowid, XP_TRUE );
|
2013-01-06 04:40:29 +01:00
|
|
|
}
|
|
|
|
g_slist_free( games );
|
|
|
|
|
2013-01-06 01:08:47 +01:00
|
|
|
GtkWidget* hbox = gtk_hbox_new( FALSE, 0 );
|
|
|
|
gtk_widget_show( hbox );
|
|
|
|
gtk_container_add( GTK_CONTAINER(vbox), hbox );
|
2013-01-07 15:10:44 +01:00
|
|
|
|
2013-01-17 06:16:07 +01:00
|
|
|
(void)addButton( "New game", hbox, G_CALLBACK(handle_newgame_button), apg );
|
|
|
|
apg->openButton = addButton( "Open", hbox,
|
|
|
|
G_CALLBACK(handle_open_button), apg );
|
|
|
|
apg->deleteButton = addButton( "Delete", hbox,
|
|
|
|
G_CALLBACK(handle_delete_button), apg );
|
|
|
|
(void)addButton( "Quit", hbox, G_CALLBACK(handle_quit_button), apg );
|
|
|
|
updateButtons( apg );
|
2011-01-24 06:52:26 +01:00
|
|
|
|
2013-01-06 01:08:19 +01:00
|
|
|
gtk_widget_show( window );
|
|
|
|
return window;
|
2008-10-24 11:07:30 +02:00
|
|
|
}
|
|
|
|
|
2013-01-07 15:10:44 +01:00
|
|
|
static gint
|
|
|
|
freeGameGlobals( gpointer data )
|
|
|
|
{
|
|
|
|
LOG_FUNC();
|
2013-01-16 04:04:20 +01:00
|
|
|
GtkGameGlobals* globals = (GtkGameGlobals*)data;
|
|
|
|
GtkAppGlobals* apg = globals->apg;
|
|
|
|
recordClosed( apg, globals );
|
2013-01-07 15:10:44 +01:00
|
|
|
freeGlobals( globals );
|
|
|
|
return 0; /* don't run again */
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-16 04:04:20 +01:00
|
|
|
windowDestroyed( GtkGameGlobals* globals )
|
2013-01-07 15:10:44 +01:00
|
|
|
{
|
|
|
|
/* schedule to run after we're back to main loop */
|
|
|
|
(void)g_idle_add( freeGameGlobals, globals );
|
|
|
|
}
|
|
|
|
|
2013-01-09 15:30:52 +01:00
|
|
|
static void
|
2013-01-16 04:04:20 +01:00
|
|
|
onNewData( GtkAppGlobals* apg, sqlite3_int64 rowid, XP_Bool isNew )
|
2013-01-09 15:30:52 +01:00
|
|
|
{
|
|
|
|
GameInfo gib;
|
2013-01-29 16:42:10 +01:00
|
|
|
if ( getGameInfo( apg->params->pDb, rowid, &gib ) ) {
|
2013-01-16 04:04:20 +01:00
|
|
|
add_to_list( apg->listWidget, rowid, isNew, &gib );
|
2013-01-09 15:30:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-16 15:46:33 +01:00
|
|
|
static gboolean
|
2013-01-24 16:49:49 +01:00
|
|
|
gtk_app_socket_proc( GIOChannel* source, GIOCondition condition, gpointer data )
|
2013-01-16 15:46:33 +01:00
|
|
|
{
|
|
|
|
if ( 0 != (G_IO_IN & condition) ) {
|
|
|
|
GtkAppGlobals* apg = (GtkAppGlobals*)data;
|
|
|
|
int socket = g_io_channel_unix_get_fd( source );
|
|
|
|
GList* iter;
|
|
|
|
for ( iter = apg->sources; !!iter; iter = iter->next ) {
|
|
|
|
SourceData* sd = (SourceData*)iter->data;
|
|
|
|
if ( sd->channel == source ) {
|
|
|
|
(*sd->proc)( sd->procClosure, socket );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
XP_ASSERT( !!iter ); /* didn't fail to find it */
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-01-24 16:49:49 +01:00
|
|
|
gtkSocketChanged( void* closure, int newSock, int XP_UNUSED(oldSock),
|
|
|
|
SockReceiver proc, void* procClosure )
|
2013-01-16 15:46:33 +01:00
|
|
|
{
|
2013-07-09 16:25:23 +02:00
|
|
|
#if 1
|
|
|
|
GtkAppGlobals* apg = (GtkAppGlobals*)closure;
|
|
|
|
SourceData* sd = g_malloc( sizeof(*sd) );
|
|
|
|
sd->channel = g_io_channel_unix_new( newSock );
|
|
|
|
sd->watch = g_io_add_watch( sd->channel, G_IO_IN | G_IO_ERR,
|
|
|
|
gtk_app_socket_proc, apg );
|
|
|
|
sd->proc = proc;
|
|
|
|
sd->procClosure = procClosure;
|
|
|
|
apg->sources = g_list_append( apg->sources, sd );
|
|
|
|
#else
|
2005-03-19 23:07:53 +01:00
|
|
|
GtkAppGlobals* globals = (GtkAppGlobals*)closure;
|
2007-11-26 03:58:25 +01:00
|
|
|
SockInfo* info = (SockInfo*)*storage;
|
|
|
|
XP_LOGF( "%s(old:%d; new:%d)", __func__, oldSock, newSock );
|
2006-09-08 09:23:19 +02:00
|
|
|
|
2005-03-19 23:07:53 +01:00
|
|
|
if ( oldSock != -1 ) {
|
2007-11-26 03:58:25 +01:00
|
|
|
XP_ASSERT( info != NULL );
|
|
|
|
g_source_remove( info->watch );
|
|
|
|
g_io_channel_unref( info->channel );
|
|
|
|
XP_FREE( globals->cGlobals.params->util->mpool, info );
|
|
|
|
*storage = NULL;
|
2008-12-29 02:35:29 +01:00
|
|
|
XP_LOGF( "Removed socket %d from gtk's list of listened-to sockets",
|
|
|
|
oldSock );
|
2005-03-19 23:07:53 +01:00
|
|
|
}
|
|
|
|
if ( newSock != -1 ) {
|
2007-11-26 03:58:25 +01:00
|
|
|
info = (SockInfo*)XP_MALLOC( globals->cGlobals.params->util->mpool,
|
|
|
|
sizeof(*info) );
|
|
|
|
GIOChannel* channel = g_io_channel_unix_new( newSock );
|
|
|
|
g_io_channel_set_close_on_unref( channel, TRUE );
|
|
|
|
guint result = g_io_add_watch( channel,
|
|
|
|
G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
|
|
|
|
newConnectionInput,
|
|
|
|
globals );
|
|
|
|
info->channel = channel;
|
|
|
|
info->watch = result;
|
2013-06-26 09:41:16 +02:00
|
|
|
if ( !!*storage ) {
|
|
|
|
XP_FREE( globals->cGlobals.params->util->mpool, *storage );
|
|
|
|
}
|
2007-11-26 03:58:25 +01:00
|
|
|
*storage = info;
|
|
|
|
XP_LOGF( "g_io_add_watch(%d) => %d", newSock, result );
|
2005-03-19 23:07:53 +01:00
|
|
|
}
|
2007-11-26 03:58:25 +01:00
|
|
|
#ifdef XWFEATURE_RELAY
|
2006-08-26 23:15:20 +02:00
|
|
|
globals->cGlobals.socket = newSock;
|
2007-11-26 03:58:25 +01:00
|
|
|
#endif
|
2006-11-11 23:44:31 +01:00
|
|
|
/* A hack for the bluetooth case. */
|
|
|
|
CommsCtxt* comms = globals->cGlobals.game.comms;
|
2007-11-26 03:58:25 +01:00
|
|
|
if ( (comms != NULL) && (comms_getConType(comms) == COMMS_CONN_BT) ) {
|
2012-11-14 06:32:42 +01:00
|
|
|
comms_resendAll( comms, XP_FALSE );
|
2006-11-11 23:44:31 +01:00
|
|
|
}
|
2013-07-09 16:25:23 +02:00
|
|
|
#endif
|
2007-11-26 03:58:25 +01:00
|
|
|
LOG_RETURN_VOID();
|
2009-02-01 16:50:58 +01:00
|
|
|
} /* gtk_socket_changed */
|
2013-01-16 15:46:33 +01:00
|
|
|
|
|
|
|
static void
|
2013-01-23 16:43:58 +01:00
|
|
|
gtkGotBuf( void* closure, const XP_U8* buf, XP_U16 len )
|
2013-01-16 15:46:33 +01:00
|
|
|
{
|
|
|
|
LOG_FUNC();
|
|
|
|
GtkAppGlobals* apg = (GtkAppGlobals*)closure;
|
2013-07-12 05:01:17 +02:00
|
|
|
XP_U32 clientToken;
|
|
|
|
XP_ASSERT( sizeof(clientToken) < len );
|
|
|
|
XP_MEMCPY( &clientToken, &buf[0], sizeof(clientToken) );
|
|
|
|
buf += sizeof(clientToken);
|
|
|
|
len -= sizeof(clientToken);
|
|
|
|
|
|
|
|
sqlite3_int64 rowid;
|
|
|
|
XP_U16 gotSeed;
|
|
|
|
rowidFromToken( ntohl( clientToken ), &rowid, &gotSeed );
|
2013-01-16 15:46:33 +01:00
|
|
|
|
2013-07-12 05:01:17 +02:00
|
|
|
XP_U16 seed = 0;
|
|
|
|
GtkGameGlobals* globals = findOpenGame( apg, rowid );
|
2013-01-16 15:46:33 +01:00
|
|
|
if ( !!globals ) {
|
2013-01-24 16:49:49 +01:00
|
|
|
gameGotBuf( &globals->cGlobals, XP_TRUE, buf, len );
|
2013-07-12 05:01:17 +02:00
|
|
|
seed = comms_getChannelSeed( globals->cGlobals.game.comms );
|
2013-01-16 15:46:33 +01:00
|
|
|
} else {
|
2013-01-18 16:49:00 +01:00
|
|
|
GtkGameGlobals tmpGlobals;
|
2013-07-12 05:01:17 +02:00
|
|
|
if ( loadGameNoDraw( &tmpGlobals, apg->params, rowid ) ) {
|
2013-01-24 16:49:49 +01:00
|
|
|
gameGotBuf( &tmpGlobals.cGlobals, XP_FALSE, buf, len );
|
2013-07-12 05:01:17 +02:00
|
|
|
seed = comms_getChannelSeed( tmpGlobals.cGlobals.game.comms );
|
2013-01-18 16:49:00 +01:00
|
|
|
saveGame( &tmpGlobals.cGlobals );
|
|
|
|
}
|
|
|
|
freeGlobals( &tmpGlobals );
|
2013-01-16 15:46:33 +01:00
|
|
|
}
|
2013-07-12 05:01:17 +02:00
|
|
|
XP_ASSERT( seed == 0 || gotSeed == seed );
|
2013-07-17 15:52:35 +02:00
|
|
|
XP_USE( seed );
|
2013-01-16 15:46:33 +01:00
|
|
|
}
|
|
|
|
|
2013-01-19 23:37:49 +01:00
|
|
|
static gint
|
|
|
|
requestMsgs( gpointer data )
|
|
|
|
{
|
|
|
|
GtkAppGlobals* apg = (GtkAppGlobals*)data;
|
|
|
|
XP_UCHAR devIDBuf[64] = {0};
|
2013-01-29 16:42:10 +01:00
|
|
|
db_fetch( apg->params->pDb, KEY_RDEVID, devIDBuf, sizeof(devIDBuf) );
|
2013-01-19 23:37:49 +01:00
|
|
|
if ( '\0' != devIDBuf[0] ) {
|
|
|
|
relaycon_requestMsgs( apg->params, devIDBuf );
|
|
|
|
} else {
|
|
|
|
XP_LOGF( "%s: not requesting messages as don't have relay id", __func__ );
|
|
|
|
}
|
|
|
|
return 0; /* don't run again */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-01-25 04:20:35 +01:00
|
|
|
gtkNoticeRcvd( void* closure )
|
2013-01-19 23:37:49 +01:00
|
|
|
{
|
|
|
|
LOG_FUNC();
|
|
|
|
GtkAppGlobals* apg = (GtkAppGlobals*)closure;
|
|
|
|
(void)g_idle_add( requestMsgs, apg );
|
|
|
|
}
|
|
|
|
|
2013-08-02 17:01:16 +02:00
|
|
|
static gboolean
|
|
|
|
keepalive_timer( gpointer data )
|
|
|
|
{
|
|
|
|
LOG_FUNC();
|
|
|
|
requestMsgs( data );
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-01-16 15:46:33 +01:00
|
|
|
static void
|
2013-08-02 17:01:16 +02:00
|
|
|
gtkDevIDChanged( void* closure, const XP_UCHAR* devID, XP_U16 maxInterval )
|
2013-01-16 15:46:33 +01:00
|
|
|
{
|
|
|
|
GtkAppGlobals* apg = (GtkAppGlobals*)closure;
|
2013-01-29 16:42:10 +01:00
|
|
|
LaunchParams* params = apg->params;
|
2013-01-19 23:37:49 +01:00
|
|
|
if ( !!devID ) {
|
|
|
|
XP_LOGF( "%s(devID=%s)", __func__, devID );
|
2013-01-29 16:42:10 +01:00
|
|
|
db_store( params->pDb, KEY_RDEVID, devID );
|
2013-08-02 17:01:16 +02:00
|
|
|
(void)g_timeout_add_seconds( maxInterval, keepalive_timer, apg );
|
2013-01-19 23:37:49 +01:00
|
|
|
} else {
|
|
|
|
XP_LOGF( "%s: bad relayid", __func__ );
|
2013-01-29 16:42:10 +01:00
|
|
|
db_remove( params->pDb, KEY_RDEVID );
|
|
|
|
|
|
|
|
DevIDType typ;
|
|
|
|
const XP_UCHAR* devID = linux_getDevID( params, &typ );
|
|
|
|
relaycon_reg( params, devID, typ );
|
2013-01-19 23:37:49 +01:00
|
|
|
}
|
2013-01-16 15:46:33 +01:00
|
|
|
}
|
|
|
|
|
2013-01-20 00:18:36 +01:00
|
|
|
static void
|
|
|
|
gtkErrorMsgRcvd( void* closure, const XP_UCHAR* msg )
|
|
|
|
{
|
|
|
|
GtkAppGlobals* apg = (GtkAppGlobals*)closure;
|
|
|
|
(void)gtkask( apg->window, msg, GTK_BUTTONS_OK );
|
|
|
|
}
|
|
|
|
|
2013-01-07 17:00:47 +01:00
|
|
|
void
|
2013-01-09 15:30:52 +01:00
|
|
|
onGameSaved( void* closure, sqlite3_int64 rowid,
|
|
|
|
XP_Bool firstTime )
|
2013-01-07 17:00:47 +01:00
|
|
|
{
|
2013-01-16 04:04:20 +01:00
|
|
|
GtkGameGlobals* globals = (GtkGameGlobals*)closure;
|
|
|
|
GtkAppGlobals* apg = globals->apg;
|
2013-01-18 16:49:00 +01:00
|
|
|
/* May not be recorded */
|
|
|
|
if ( !!apg ) {
|
|
|
|
onNewData( apg, rowid, firstTime );
|
|
|
|
}
|
2013-01-07 17:00:47 +01:00
|
|
|
}
|
|
|
|
|
2013-01-17 06:16:07 +01:00
|
|
|
sqlite3_int64
|
|
|
|
getSelRow( const GtkAppGlobals* apg )
|
|
|
|
{
|
|
|
|
sqlite3_int64 result = -1;
|
|
|
|
guint len = apg->selRows->len;
|
|
|
|
if ( 1 == len ) {
|
|
|
|
result = g_array_index( apg->selRows, sqlite3_int64, 0 );
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-07-09 16:25:23 +02:00
|
|
|
static GtkAppGlobals* g_globals_for_signal = NULL;
|
|
|
|
|
2013-01-17 15:37:53 +01:00
|
|
|
static void
|
|
|
|
handle_sigintterm( int XP_UNUSED(sig) )
|
|
|
|
{
|
|
|
|
LOG_FUNC();
|
|
|
|
handle_destroy( NULL, g_globals_for_signal );
|
|
|
|
}
|
|
|
|
|
2003-11-01 06:35:29 +01:00
|
|
|
int
|
2013-01-06 01:08:19 +01:00
|
|
|
gtkmain( LaunchParams* params )
|
2003-11-01 06:35:29 +01:00
|
|
|
{
|
2013-01-16 04:04:20 +01:00
|
|
|
GtkAppGlobals apg = {0};
|
2013-01-17 15:37:53 +01:00
|
|
|
|
|
|
|
g_globals_for_signal = &apg;
|
2003-11-01 06:35:29 +01:00
|
|
|
|
2013-01-17 15:37:53 +01:00
|
|
|
struct sigaction act = { .sa_handler = handle_sigintterm };
|
|
|
|
sigaction( SIGINT, &act, NULL );
|
|
|
|
sigaction( SIGTERM, &act, NULL );
|
|
|
|
|
2013-01-17 06:16:07 +01:00
|
|
|
apg.selRows = g_array_new( FALSE, FALSE, sizeof( sqlite3_int64 ) );
|
2013-01-16 04:04:20 +01:00
|
|
|
apg.params = params;
|
2013-01-29 16:42:10 +01:00
|
|
|
params->pDb = openGamesDB( params->dbName );
|
2013-01-16 15:46:33 +01:00
|
|
|
|
|
|
|
RelayConnProcs procs = {
|
|
|
|
.msgReceived = gtkGotBuf,
|
2013-01-19 23:37:49 +01:00
|
|
|
.msgNoticeReceived = gtkNoticeRcvd,
|
2013-01-16 15:46:33 +01:00
|
|
|
.devIDChanged = gtkDevIDChanged,
|
2013-01-20 00:18:36 +01:00
|
|
|
.msgErrorMsg = gtkErrorMsgRcvd,
|
2013-01-24 16:49:49 +01:00
|
|
|
.socketChanged = gtkSocketChanged,
|
2013-01-16 15:46:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
relaycon_init( params, &procs, &apg,
|
|
|
|
params->connInfo.relay.relayName,
|
2013-01-19 23:37:49 +01:00
|
|
|
params->connInfo.relay.defaultSendPort );
|
2013-01-29 16:42:10 +01:00
|
|
|
|
|
|
|
DevIDType typ;
|
|
|
|
const XP_UCHAR* devID = linux_getDevID( params, &typ );
|
|
|
|
relaycon_reg( params, devID, typ );
|
2003-11-01 06:35:29 +01:00
|
|
|
|
2013-01-20 00:18:36 +01:00
|
|
|
apg.window = makeGamesWindow( &apg );
|
2013-01-06 01:08:19 +01:00
|
|
|
gtk_main();
|
2003-11-01 06:35:29 +01:00
|
|
|
|
2013-01-29 16:42:10 +01:00
|
|
|
closeGamesDB( params->pDb );
|
2013-01-16 15:46:33 +01:00
|
|
|
relaycon_cleanup( params );
|
2010-09-20 13:55:35 +02:00
|
|
|
|
2003-11-01 06:35:29 +01:00
|
|
|
return 0;
|
|
|
|
} /* gtkmain */
|
|
|
|
|
|
|
|
#endif /* PLATFORM_GTK */
|