Commit 500e9c71 authored by Yves Combe's avatar Yves Combe

add config_start and config_end entry point to all plugin.

modify python initialisation to detect config_start in python file and setup it correctly in plugin pointer
parent 4b90cb6a
2005-07-04 Yves Combe <yves@ycombe.net>
add config_start and config_end entry point to all plugin.
modify python initialisation to detect config_start in python file and setup it correctly in plugin pointer
* src/boards/advanced_colors.c:
* src/boards/algebra.c:
* src/boards/algebra_guesscount.c:
* src/boards/animtest.c:
* src/boards/canal_lock.c:
* src/boards/chess.c:
* src/boards/click_on_letter.c:
* src/boards/clickgame.c:
* src/boards/clockgame.c:
* src/boards/colors.c:
* src/boards/draw.c:
* src/boards/enumerate.c:
* src/boards/erase.c:
* src/boards/fifteen.c:
* src/boards/gletters.c:
* src/boards/hanoi.c:
* src/boards/imageid.c:
* src/boards/leftright.c:
* src/boards/machpuzzle.c:
* src/boards/maze.c:
* src/boards/memory.c:
* src/boards/menu.c:
* src/boards/missingletter.c:
* src/boards/money.c:
* src/boards/paratrooper.c:
* src/boards/planegame.c:
* src/boards/python.c: (get_pythonboards_list), (pythonboard_init),
(pythonboard_is_our_board), (pythonboard_repeat),
(pythonboard_config_start), (pythonboard_config_stop):
* src/boards/railroad.c:
* src/boards/read_colors.c:
* src/boards/reading.c:
* src/boards/reversecount.c:
* src/boards/shapegame.c:
* src/boards/smallnumbers.c:
* src/boards/submarine.c:
* src/boards/superbrain.c:
* src/boards/target.c:
* src/boards/traffic.c:
* src/boards/wordsgame.c:
* src/gcompris/Makefile.am:
* src/gcompris/board.c: (init_plugins), (board_check_file):
* src/gcompris/gameutil.c: (gcompris_get_boards_list):
* src/gcompris/gameutil.h:
* src/gcompris/plugin.h:
2005-07-01 Bruno coudoin <bruno.coudoin@free.fr>
Added missing files for the edit mode (editing class)
......
......@@ -98,6 +98,8 @@ static BoardPlugin menu_bp =
NULL,
set_level,
NULL,
NULL,
NULL,
NULL
};
......
/* gcompris - algebra.c
*
* Time-stamp: <2005/05/02 22:06:44 bruno>
* Time-stamp: <2005/07/01 23:09:38 yves>
*
* Copyright (C) 2000 Bruno Coudoin
*
......@@ -109,6 +109,8 @@ static BoardPlugin menu_bp =
process_ok,
set_level,
NULL,
NULL,
NULL,
NULL
};
......
......@@ -155,6 +155,8 @@ static BoardPlugin menu_bp =
process_ok,
set_level,
NULL,
NULL,
NULL,
NULL
};
......
......@@ -51,6 +51,8 @@ static BoardPlugin menu_bp =
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
};
......
......@@ -125,6 +125,8 @@ static BoardPlugin menu_bp =
NULL,
set_level,
NULL,
NULL,
NULL,
NULL
};
......
......@@ -148,6 +148,8 @@ static BoardPlugin menu_bp =
NULL,
set_level,
NULL,
NULL,
NULL,
NULL
};
......
......@@ -92,7 +92,9 @@ static BoardPlugin menu_bp =
process_ok,
set_level,
NULL,
repeat
repeat,
NULL,
NULL
};
/*
......
/* gcompris - clickgame.c
*
* Time-stamp: <2005/05/02 01:58:00 bruno>
* Time-stamp: <2005/07/01 23:42:40 yves>
*
* Copyright (C) 2000 Bruno Coudoin
*
......@@ -97,6 +97,8 @@ static BoardPlugin menu_bp =
NULL,
clickgame_set_level,
clickgame_config,
NULL,
NULL,
NULL
};
......
/* gcompris - clockgame.c
*
* Time-stamp: <2005/06/14 23:03:45 yves>
* Time-stamp: <2005/07/01 23:43:24 yves>
*
* Copyright (C) 2000 Bruno Coudoin
*
......@@ -89,6 +89,8 @@ static BoardPlugin menu_bp =
process_ok,
set_level,
NULL,
NULL,
NULL,
NULL
};
......
......@@ -86,7 +86,9 @@ static BoardPlugin menu_bp =
process_ok,
NULL,//set_level,
NULL,
repeat
repeat,
NULL,
NULL
};
/* =====================================================================
......
......@@ -227,6 +227,8 @@ static BoardPlugin menu_bp =
NULL,
NULL,
config,
NULL,
NULL,
NULL
};
......
......@@ -103,6 +103,8 @@ static BoardPlugin menu_bp =
process_ok,
set_level,
NULL,
NULL,
NULL,
NULL
};
......
......@@ -95,6 +95,8 @@ static BoardPlugin menu_bp =
NULL,
set_level,
NULL,
NULL,
NULL,
NULL
};
......
......@@ -68,6 +68,8 @@ static BoardPlugin menu_bp =
NULL,
set_level,
NULL,
NULL,
NULL,
NULL
};
......
/* gcompris - gletters.c
*
* Time-stamp: <2005/05/10 22:14:55 bruno>
* Time-stamp: <2005/07/01 23:44:34 yves>
*
* Copyright (C) 2000 Bruno Coudoin
*
......@@ -141,6 +141,8 @@ static BoardPlugin menu_bp =
NULL,
set_level,
NULL,
NULL,
NULL,
NULL
};
......
......@@ -116,6 +116,8 @@ static BoardPlugin menu_bp =
NULL,
set_level,
NULL,
NULL,
NULL,
NULL
};
......
......@@ -120,6 +120,8 @@ static BoardPlugin menu_bp =
process_ok,
set_level,
NULL,
NULL,
NULL,
NULL
};
......
......@@ -118,6 +118,8 @@ static BoardPlugin menu_bp =
process_ok,
set_level,
NULL,
NULL,
NULL,
NULL
};
......
......@@ -105,6 +105,8 @@ static BoardPlugin menu_bp =
NULL,
set_level,
NULL,
NULL,
NULL,
NULL
};
......
......@@ -144,7 +144,9 @@ static BoardPlugin menu_bp =
process_ok,
set_level,//set_level,
NULL,
repeat
repeat,
NULL,
NULL
};
/* =====================================================================
......
/* gcompris - memory.c
*
* Time-stamp: <2005/04/27 22:15:58 bruno>
* Time-stamp: <2005/07/01 23:45:39 yves>
*
* Copyright (C) 2000 Bruno Coudoin
*
......@@ -176,6 +176,8 @@ static BoardPlugin menu_bp =
NULL,
set_level,
NULL,
NULL,
NULL,
NULL
};
......
/* gcompris - menu.c
*
* Time-stamp: <2005/06/22 10:34:21 yves>
* Time-stamp: <2005/07/01 23:45:53 yves>
*
* Copyright (C) 2000 Bruno Coudoin
*
......@@ -84,6 +84,8 @@ static BoardPlugin menu_bp =
NULL,
NULL,
menu_config,
NULL,
NULL,
NULL
};
......
......@@ -113,6 +113,8 @@ static BoardPlugin menu_bp =
process_ok,
set_level,
NULL,
NULL,
NULL,
NULL
};
......
......@@ -99,6 +99,8 @@ static BoardPlugin menu_bp =
process_ok,
set_level,
NULL,
NULL,
NULL,
NULL
};
......
/* gcompris - paratrooper.c
*
* Time-stamp: <2005/04/27 22:32:14 bruno>
* Time-stamp: <2005/07/01 23:46:38 yves>
*
* Copyright (C) 2000 Bruno Coudoin
*
......@@ -114,6 +114,8 @@ static BoardPlugin menu_bp =
NULL,
set_level,
NULL,
NULL,
NULL,
NULL
};
......
/* gcompris - planegame.c
*
* Time-stamp: <2005/04/27 22:16:32 bruno>
* Time-stamp: <2005/07/01 23:48:30 yves>
*
* Copyright (C) 2000 Bruno Coudoin
*
......@@ -89,6 +89,8 @@ static BoardPlugin menu_bp =
NULL,
set_level,
NULL,
NULL,
NULL,
NULL
};
......
......@@ -29,7 +29,11 @@ static PyObject* python_gcomprisBoard = NULL;
static PyObject* python_board_module = NULL;
static PyObject* python_board_instance = NULL;
static void pythonboard_init ();
static PyObject* python_gcomprisBoard_config = NULL;;
static PyObject* python_board_config_module = NULL;
static PyObject* python_board_config_instance = NULL;
static void pythonboard_init (GcomprisBoard *agcomprisBoard);
static void pythonboard_start (GcomprisBoard *agcomprisBoard);
static void pythonboard_pause (gboolean pause);
static void pythonboard_end (void);
......@@ -41,6 +45,13 @@ static void pythonboard_ok (void);
static void pythonboard_set_level (guint level);
static void pythonboard_config(void);
static void pythonboard_repeat (void);
static void pythonboard_config_start (GcomprisBoard *agcomprisBoard,
GnomeCanvas *canvas,
int x,
int y,
int width,
int height);
static void pythonboard_config_stop (void);
static gboolean pythonboard_is_ready = FALSE;
......@@ -65,9 +76,12 @@ static BoardPlugin menu_bp =
pythonboard_ok,
pythonboard_set_level,
pythonboard_config,
pythonboard_repeat
pythonboard_repeat,
pythonboard_config_start,
pythonboard_config_stop
};
static BoardPlugin *bp_board = NULL;
/*
* Return the plugin structure (Common to all gcompris boards)
......@@ -80,13 +94,50 @@ GET_BPLUGIN_INFO(python)
* Tests if a python interpreter is available
* and that all required imports can be loaded.
*/
static GList *config_boards= NULL;
GList *get_pythonboards_list()
{
GList *pythonboards_list = NULL;
GList *boards_list = gcompris_get_boards_list();
GList *list;
GcomprisBoard *board;
for (list = boards_list; list != NULL; list = list->next){
board = (GcomprisBoard *) list->data;
if (g_ascii_strncasecmp(board->type, "python", 6)==0)
pythonboards_list = g_list_append(pythonboards_list, board);
}
return pythonboards_list;
}
static void
pythonboard_init (){
pythonboard_init (GcomprisBoard *agcomprisBoard){
PyObject* main_module;
PyObject* globals;
gchar* execstr;
gchar* userplugindir;
gboolean has_config = TRUE;
char* board_file_name;
char* boardclass;
PyObject* module_dict;
PyObject* py_boardclass;
PyObject* py_boardclass_args;
PyObject* py_config_start;
GcomprisBoard *board;
GList *list;
if (pythonboard_is_ready)
return ;
config_boards = get_pythonboards_list();
/* Initialize the python interpreter */
Py_Initialize();
......@@ -140,6 +191,42 @@ pythonboard_init (){
pythonboard_is_ready = FALSE;
g_warning("! Python disabled: Cannot import gcompris modules\n");
}
else {
/* Load the gcompris modules */
python_gcompris_module_init();
list = config_boards;
while (list != NULL){
board = (GcomprisBoard *) list->data;
/* Python is now initialized we create some usefull variables */
board_file_name = strchr(board->type, ':')+1;
boardclass = g_strdup_printf("Gcompris_%s", board_file_name);
/* Insert the board module into the python's interpreter */
python_board_module = PyImport_ImportModuleEx(board_file_name,
globals,
globals,
NULL);
if(python_board_module!=NULL){
//printf("init: python_board_module ok \n");
/* Get the module dictionnary */
module_dict = PyModule_GetDict(python_board_module);
/* Get the python board class */
py_boardclass = PyDict_GetItemString(module_dict, boardclass);
list = list->next;
if (!PyObject_HasAttrString( py_boardclass, "config_start")){
config_boards = g_list_remove(config_boards, board);
g_free(boardclass);
}
}
}
}
}
}
}
......@@ -149,6 +236,7 @@ pythonboard_init (){
/* Finalize the python interpreter */
Py_Finalize();
}
/*
......@@ -299,12 +387,48 @@ static void pythonboard_end (void){
*/
static gboolean pythonboard_is_our_board (GcomprisBoard *agcomprisBoard){
if (agcomprisBoard->plugin)
return TRUE;
if(pythonboard_is_ready) {
if (agcomprisBoard!=NULL) {
if (g_ascii_strncasecmp(agcomprisBoard->type, "python", 6)==0) {
bp_board = g_malloc0(sizeof(BoardPlugin));
bp_board->handle = menu_bp.handle;
bp_board->filename = menu_bp.filename;
bp_board->name = menu_bp.name;
bp_board->description = menu_bp.description;
bp_board->author = menu_bp.author;
bp_board->init = menu_bp.init;
bp_board->cleanup = menu_bp.cleanup;
bp_board->about = menu_bp.about;
bp_board->configure = menu_bp.configure;
bp_board->start_board = menu_bp.start_board;
bp_board->pause_board = menu_bp.pause_board;
bp_board->end_board = menu_bp.end_board;
bp_board->is_our_board = menu_bp.is_our_board;
bp_board->key_press = menu_bp.key_press;
bp_board->ok = menu_bp.ok;
bp_board->set_level = menu_bp.set_level;
bp_board->config = menu_bp.config;
bp_board->repeat = menu_bp.repeat;
if (g_list_find (config_boards, agcomprisBoard)){
bp_board->config_start = menu_bp.config_start;
bp_board->config_stop = menu_bp.config_stop;
} else {
bp_board->config_start = NULL;
bp_board->config_stop = NULL;
}
/* Set the plugin entry */
agcomprisBoard->plugin=&menu_bp;
agcomprisBoard->plugin = bp_board;
bp_board = NULL;
//g_print("pythonboard: is our board = TRUE\n");
......@@ -312,6 +436,7 @@ static gboolean pythonboard_is_our_board (GcomprisBoard *agcomprisBoard){
}
}
}
agcomprisBoard->plugin=NULL;
return FALSE;
}
......@@ -386,3 +511,97 @@ static void pythonboard_repeat (void){
PyErr_Print();
}
}
/*
* Start the board.
* In this case:
* - initialize python interpreter
* - import gcompris functions/objects
* - import gtk/gnome functions/objects
* - load the python written board
* - call the board start function
*/
static void
pythonboard_config_start (GcomprisBoard *agcomprisBoard,
GnomeCanvas *canvas,
int x,
int y,
int width,
int height){
PyObject* py_function_result;
PyObject* module_dict;
PyObject* py_boardclass;
PyObject* py_boardclass_args;
PyObject* globals;
static char *python_args[]={ "" };
static char* python_prog_name="gcompris";
char* boardclass;
char* board_file_name;
/* Python is now initialized we create some usefull variables */
board_file_name = strchr(agcomprisBoard->type, ':')+1;
boardclass = g_strdup_printf("Gcompris_%s", board_file_name);
/* Insert the board module into the python's interpreter */
python_board_config_module = PyImport_ImportModuleEx(board_file_name,
globals,
globals,
NULL);
if(python_board_config_module!=NULL){
/* Get the module dictionnary */
module_dict = PyModule_GetDict(python_board_config_module);
/* Get the python board class */
py_boardclass = PyDict_GetItemString(module_dict, boardclass);
/* Create a python gcompris board */
python_gcomprisBoard_config=gcompris_new_pyGcomprisBoardObject(agcomprisBoard);
/* Create an instance of the board class */
py_boardclass_args = PyTuple_New(1);
Py_INCREF(python_gcomprisBoard_config);
PyTuple_SetItem(py_boardclass_args, 0, python_gcomprisBoard_config);
PyTuple_SetItem(py_boardclass_args, 1, PyInt_FromLong((long) x));
PyTuple_SetItem(py_boardclass_args, 1, PyInt_FromLong((long) y));
PyTuple_SetItem(py_boardclass_args, 1, PyInt_FromLong((long) width));
PyTuple_SetItem(py_boardclass_args, 1, PyInt_FromLong((long) height));
python_board_config_instance = PyInstance_New(py_boardclass, py_boardclass_args, NULL);
Py_DECREF(py_boardclass_args);
/* Call the function */
py_function_result = PyObject_CallMethod(python_board_config_instance, "config_start", NULL);
if( py_function_result != NULL){
Py_DECREF(py_function_result);
} else {
PyErr_Print();
}
} else {
PyErr_Print();
}
g_free(boardclass);
}
/*
* End the board.
* In this case:
* - call the board end function
* - finalise python interpreter
*/
static void pythonboard_config_stop (void){
PyObject* result = NULL;
if(python_gcomprisBoard_config!=NULL){
result = PyObject_CallMethod(python_board_config_instance, "config_stop", NULL);
if( result == NULL){
PyErr_Print();
} else {
Py_DECREF(result);
}
Py_XDECREF(python_board_config_module);
Py_XDECREF(python_board_config_instance);
Py_XDECREF(python_gcomprisBoard_config);
Py_Finalize();
}
}
......@@ -108,7 +108,9 @@ static BoardPlugin menu_bp =
process_ok,
set_level,
NULL,
repeat
repeat,
NULL,
NULL
};
/*
......
......@@ -97,6 +97,8 @@ static BoardPlugin menu_bp =
process_ok,
NULL,//set_level,
NULL,
NULL,
NULL,
NULL
};
......
/* gcompris - reading.c
*
* Time-stamp: <2005/05/02 02:01:00 bruno>
* Time-stamp: <2005/07/01 23:50:00 yves>
*
* Copyright (C) 2000 Bruno Coudoin
*
......@@ -119,6 +119,8 @@ static BoardPlugin menu_bp =
NULL,
set_level,
NULL,
NULL,
NULL,
NULL
};
......
......@@ -149,6 +149,8 @@ static BoardPlugin menu_bp =
process_ok,
set_level,
NULL,
NULL,
NULL,
NULL
};
......
/* gcompris - shapegame.c
*
* Time-stamp: <2005/06/19 02:11:47 bruno>
* Time-stamp: <2005/07/01 23:50:26 yves>
*
* Copyright (C) 2000 Bruno Coudoin
*
......@@ -181,6 +181,8 @@ static BoardPlugin menu_bp =
process_ok,
set_level,
NULL,
NULL,
NULL,
NULL
};
......
/* gcompris - smallnumbers.c
*
* Time-stamp: <2005/05/02 02:01:35 bruno>
* Time-stamp: <2005/07/01 23:50:45 yves>
*
* Copyright (C) 2000 Bruno Coudoin
*
......@@ -75,6 +75,8 @@ static BoardPlugin menu_bp =
NULL,
set_level,
NULL,
NULL,
NULL,
NULL
};
......
......@@ -213,6 +213,8 @@ static BoardPlugin menu_bp =
NULL,
set_level,
NULL,
NULL,
NULL,
NULL
};
......
......@@ -122,6 +122,8 @@ static BoardPlugin menu_bp =
process_ok,