Back to index

gcompris  8.2.2
board.c
Go to the documentation of this file.
00001 /*  GCompris -- This files comes from XMMS
00002  *
00003  *  XMMS - Cross-platform multimedia player
00004  *  Copyright (C) 1998-2000  Peter Alm, Mikael Alm, Olle Hallnas, Thomas Nilsson and 4Front Technologies
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00019  */
00020 
00021 #include <string.h>
00022 
00023 #include "gcompris.h"
00024 #include "gc_core.h"
00025 
00026 
00027 static struct BoardPluginData *bp_data;
00028 
00029 static gboolean       get_board_playing(void);
00030 
00031 #ifdef WIN32
00032 int gc_activation_check(char *code);
00033 extern BoardPlugin * get_advanced_colors_bplugin_info();
00034 extern BoardPlugin * get_algebra_bplugin_info();
00035 extern BoardPlugin * get_algebra_guesscount_bplugin_info();
00036 extern BoardPlugin * get_awele_bplugin_info();
00037 extern BoardPlugin * get_canal_lock_bplugin_info();
00038 extern BoardPlugin * get_chess_bplugin_info();
00039 extern BoardPlugin * get_clickgame_bplugin_info();
00040 extern BoardPlugin * get_click_on_letter_bplugin_info();
00041 extern BoardPlugin * get_clockgame_bplugin_info();
00042 extern BoardPlugin * get_colors_bplugin_info();
00043 extern BoardPlugin * get_crane_bplugin_info();
00044 extern BoardPlugin * get_draw_bplugin_info();
00045 extern BoardPlugin * get_enumerate_bplugin_info();
00046 extern BoardPlugin * get_erase_bplugin_info();
00047 extern BoardPlugin * get_fifteen_bplugin_info();
00048 extern BoardPlugin * get_gletters_bplugin_info();
00049 extern BoardPlugin * get_gtans_bplugin_info();
00050 extern BoardPlugin * get_hanoi_bplugin_info();
00051 extern BoardPlugin * get_hanoi_real_bplugin_info();
00052 extern BoardPlugin * get_imageid_bplugin_info();
00053 extern BoardPlugin * get_leftright_bplugin_info();
00054 extern BoardPlugin * get_machpuzzle_bplugin_info();
00055 extern BoardPlugin * get_magic_hat_bplugin_info();
00056 extern BoardPlugin * get_maze_bplugin_info();
00057 extern BoardPlugin * get_memory_bplugin_info();
00058 extern BoardPlugin * get_menu_bplugin_info();
00059 extern BoardPlugin * get_missingletter_bplugin_info();
00060 extern BoardPlugin * get_money_bplugin_info();
00061 extern BoardPlugin * get_paratrooper_bplugin_info();
00062 extern BoardPlugin * get_planegame_bplugin_info();
00063 extern BoardPlugin * get_railroad_bplugin_info();
00064 extern BoardPlugin * get_read_colors_bplugin_info();
00065 extern BoardPlugin * get_reading_bplugin_info();
00066 extern BoardPlugin * get_reversecount_bplugin_info();
00067 extern BoardPlugin * get_shapegame_bplugin_info();
00068 extern BoardPlugin * get_smallnumbers_bplugin_info();
00069 extern BoardPlugin * get_submarine_bplugin_info();
00070 extern BoardPlugin * get_superbrain_bplugin_info();
00071 extern BoardPlugin * get_target_bplugin_info();
00072 extern BoardPlugin * get_traffic_bplugin_info();
00073 extern BoardPlugin * get_wordsgame_bplugin_info();
00074 extern BoardPlugin * get_python_bplugin_info();
00075 
00076 #define MAX_NUMBER_OF_BOARDS 200
00077 static BoardPlugin *static_boards_demo[MAX_NUMBER_OF_BOARDS];
00078 static BoardPlugin *static_boards[MAX_NUMBER_OF_BOARDS];
00079 
00080 #endif
00081 
00082 #ifdef WIN32
00083 int gc_board_number_in_demo;
00084 void gc_board_init(void)
00085 {
00086   guint i=0;
00087 
00088   bp_data = g_malloc0(sizeof (struct BoardPluginData));
00089 
00090   static_boards_demo[i++] = get_awele_bplugin_info();
00091   static_boards_demo[i++] = get_canal_lock_bplugin_info();
00092   static_boards_demo[i++] = get_click_on_letter_bplugin_info();
00093   static_boards_demo[i++] = get_clickgame_bplugin_info();
00094   static_boards_demo[i++] = get_clockgame_bplugin_info();
00095   static_boards_demo[i++] = get_colors_bplugin_info();
00096   static_boards_demo[i++] = get_crane_bplugin_info();
00097   static_boards_demo[i++] = get_draw_bplugin_info();
00098   static_boards_demo[i++] = get_enumerate_bplugin_info();
00099   static_boards_demo[i++] = get_erase_bplugin_info();
00100   static_boards_demo[i++] = get_gletters_bplugin_info();
00101   static_boards_demo[i++] = get_gtans_bplugin_info();
00102   static_boards_demo[i++] = get_hanoi_bplugin_info();
00103   static_boards_demo[i++] = get_leftright_bplugin_info();
00104   static_boards_demo[i++] = get_magic_hat_bplugin_info();
00105   static_boards_demo[i++] = get_menu_bplugin_info();
00106   static_boards_demo[i++] = get_money_bplugin_info();
00107   static_boards_demo[i++] = get_reading_bplugin_info();
00108   static_boards_demo[i++] = get_submarine_bplugin_info();
00109   static_boards_demo[i++] = get_superbrain_bplugin_info();
00110   static_boards_demo[i++] = get_target_bplugin_info();
00111   static_boards_demo[i++] = NULL;
00112 
00113   gc_board_number_in_demo = i - 2;
00114 
00115   i=0;
00116 
00117   static_boards[i++] = get_awele_bplugin_info();
00118   static_boards[i++] = get_advanced_colors_bplugin_info();
00119   static_boards[i++] = get_algebra_bplugin_info();
00120   static_boards[i++] = get_algebra_bplugin_info();
00121   static_boards[i++] = get_algebra_guesscount_bplugin_info();
00122   static_boards[i++] = get_canal_lock_bplugin_info();
00123   static_boards[i++] = get_chess_bplugin_info();
00124   static_boards[i++] = get_click_on_letter_bplugin_info();
00125   static_boards[i++] = get_clickgame_bplugin_info();
00126   static_boards[i++] = get_clockgame_bplugin_info();
00127   static_boards[i++] = get_colors_bplugin_info();
00128   static_boards[i++] = get_crane_bplugin_info();
00129   static_boards[i++] = get_draw_bplugin_info();
00130   static_boards[i++] = get_enumerate_bplugin_info();
00131   static_boards[i++] = get_erase_bplugin_info();
00132   static_boards[i++] = get_fifteen_bplugin_info();
00133   static_boards[i++] = get_gletters_bplugin_info();
00134   static_boards[i++] = get_gtans_bplugin_info();
00135   static_boards[i++] = get_hanoi_bplugin_info();
00136   static_boards[i++] = get_hanoi_real_bplugin_info();
00137   static_boards[i++] = get_imageid_bplugin_info();
00138   static_boards[i++] = get_leftright_bplugin_info();
00139   static_boards[i++] = get_machpuzzle_bplugin_info();
00140   static_boards[i++] = get_magic_hat_bplugin_info();
00141   static_boards[i++] = get_maze_bplugin_info();
00142   static_boards[i++] = get_memory_bplugin_info();
00143   static_boards[i++] = get_menu_bplugin_info();
00144   static_boards[i++] = get_missingletter_bplugin_info();
00145   static_boards[i++] = get_money_bplugin_info();
00146   static_boards[i++] = get_paratrooper_bplugin_info();
00147   static_boards[i++] = get_planegame_bplugin_info();
00148   static_boards[i++] = get_railroad_bplugin_info();
00149   static_boards[i++] = get_read_colors_bplugin_info();
00150   static_boards[i++] = get_reading_bplugin_info();
00151   static_boards[i++] = get_reversecount_bplugin_info();
00152   static_boards[i++] = get_shapegame_bplugin_info();
00153   static_boards[i++] = get_smallnumbers_bplugin_info();
00154   static_boards[i++] = get_submarine_bplugin_info();
00155   static_boards[i++] = get_superbrain_bplugin_info();
00156   static_boards[i++] = get_target_bplugin_info();
00157   static_boards[i++] = get_traffic_bplugin_info();
00158   static_boards[i++] = get_wordsgame_bplugin_info();
00159   static_boards[i++] = get_python_bplugin_info();
00160   static_boards[i++] = NULL;
00161 
00162   i=0;
00163   while(static_boards[i] != NULL) {
00164     /* If this plugin defines an initialisation entry point, call it */
00165     BoardPlugin *bp;
00166 
00167     /* Get the BoardPlugin Info */
00168     bp = (BoardPlugin *) static_boards[i++];
00169 
00170     if(bp->init != NULL) {
00171       bp->init(NULL);
00172     }
00173   }
00174 }
00175 #else
00176 void gc_board_init(void)
00177 {
00178 
00179   /* First make sure the module loading is supported on this platform */
00180   if (!g_module_supported())
00181     g_error( _("Dynamic module loading is not supported. gcompris cannot load.\n") );
00182 
00183   bp_data = g_malloc0(sizeof (struct BoardPluginData));
00184 
00185 }
00186 #endif
00187 
00188 BoardPlugin *gc_board_get_current_board_plugin(void)
00189 {
00190   if(bp_data->current_gcompris_board)
00191     return bp_data->current_gcompris_board->plugin;
00192 
00193   return NULL;
00194 }
00195 
00196 GcomprisBoard *gc_board_get_current(void)
00197 {
00198   return bp_data->current_gcompris_board;
00199 }
00200 
00201 void gc_board_set_current(GcomprisBoard * gcomprisBoard)
00202 {
00203   bp_data->current_gcompris_board = gcomprisBoard;
00204 }
00205 
00206 #ifdef WIN32
00207 gboolean
00208 gc_board_check_file(GcomprisBoard *gcomprisBoard)
00209 {
00210   GcomprisProperties *properties = gc_prop_get();
00211   BoardPlugin *bp;
00212   guint        i=0;
00213   guint        key_is_valid = 0;
00214 
00215   g_assert(gcomprisBoard!=NULL);
00216   g_assert(properties->key!=NULL);
00217 
00218   /* Check Already loaded */
00219   if(gcomprisBoard->plugin!=NULL) {
00220     return TRUE;
00221   }
00222 
00223   key_is_valid = gc_activation_check(properties->key);
00224 
00225   i = 0;
00226   if(key_is_valid == 1)
00227     {
00228       while(static_boards[i++] != NULL) {
00229 
00230        /* Get the BoardPlugin Info */
00231        bp = (BoardPlugin *) static_boards[i-1];
00232 
00233        if(bp->is_our_board(gcomprisBoard)) {
00234          /* Great, we found our plugin */
00235          g_warning("We found the correct plugin for board %s (type=%s)\n",
00236                   gcomprisBoard->name, gcomprisBoard->type);
00237 
00238          gcomprisBoard->plugin = bp;
00239 
00240          return TRUE;
00241        }
00242       }
00243     }
00244   else
00245     {
00246       while(static_boards_demo[i++] != NULL) {
00247 
00248        /* Get the BoardPlugin Info */
00249        bp = (BoardPlugin *) static_boards_demo[i-1];
00250 
00251        if(bp->is_our_board(gcomprisBoard)) {
00252          /* Great, we found our plugin */
00253          g_warning("We found the correct plugin for board %s (type=%s)\n",
00254                 gcomprisBoard->name, gcomprisBoard->type);
00255 
00256          gcomprisBoard->plugin = bp;
00257 
00258          return TRUE;
00259        }
00260       }
00261     }
00262 
00263   g_warning("No plugin library found for board type '%s', requested by '%s'",
00264            gcomprisBoard->type,  gcomprisBoard->filename);
00265 
00266   return FALSE;
00267 }
00268 #else
00269 gboolean
00270 gc_board_check_file(GcomprisBoard *gcomprisBoard)
00271 {
00272   GModule     *gmodule = NULL;
00273   gchar       *gmodule_file = NULL;
00274   BoardPlugin *(*plugin_get_bplugin_info) (void) = NULL;
00275   GcomprisProperties *properties = gc_prop_get();
00276   gchar *sep;
00277   gchar *type;
00278 
00279   g_assert(gcomprisBoard!=NULL);
00280 
00281   /* Check Already loaded */
00282   if(gcomprisBoard->plugin!=NULL) {
00283     return TRUE;
00284   }
00285 
00286   type = g_strdup(gcomprisBoard->type);
00287 
00288   /* Manage the python case where : is use to separate python plugin and boards */
00289   if((sep = strchr(type, ':')))
00290     *sep ='\0';
00291 
00292   gmodule_file = g_module_build_path (properties->package_plugin_dir, type);
00293 
00294   gmodule = g_module_open (gmodule_file, 0);
00295   if(gmodule) {
00296     g_warning("opened module %s with name %s\n", gmodule_file, type);
00297   } else {
00298     g_warning("Failed to open module %s with name %s (error=%s)\n", gmodule_file,
00299              type, g_module_error());
00300   }
00301   g_free(type);
00302 
00303   if(gmodule != NULL) {
00304 
00305     g_module_symbol (gmodule, "get_bplugin_info", (gpointer) &plugin_get_bplugin_info);
00306 
00307     if(plugin_get_bplugin_info != NULL) {
00308 
00309       BoardPlugin *bp;
00310 
00311       /* Get the BoardPlugin Info */
00312       bp = (BoardPlugin *) plugin_get_bplugin_info();
00313 
00314       /* If this plugin defines an initialisation entry point, call it */
00315       if(bp->init != NULL) {
00316        bp->init(gcomprisBoard);
00317       }
00318 
00319       if(bp->is_our_board(gcomprisBoard)) {
00320        /* Great, we found our plugin */
00321        g_warning("We found the correct plugin for board %s (type=%s)\n", gcomprisBoard->name, gcomprisBoard->type);
00322 
00323        /* done in is_our_board gcomprisBoard->plugin = bp; */
00324 
00325        /* Save this for caching and cleanup */
00326        gcomprisBoard->gmodule_file = gmodule_file;
00327        gcomprisBoard->gmodule      = gmodule;
00328 
00329        return TRUE;
00330       } else {
00331        g_warning("We found a plugin with the name %s but is_our_board() returned FALSE (type=%s)\n",
00332                 gcomprisBoard->name,
00333                 gcomprisBoard->type);
00334       }
00335     } else {
00336       g_warning("plugin_get_bplugin_info entry point not found for %s\n",
00337               gcomprisBoard->filename);
00338     }
00339   }
00340   g_warning("No plugin library found for board type '%s', requested by '%s'",
00341            gcomprisBoard->type,  gcomprisBoard->filename);
00342 
00343   return FALSE;
00344 }
00345 #endif
00346 
00347 void
00348 gc_board_play(GcomprisBoard *gcomprisBoard)
00349 {
00350   BoardPlugin *bp;
00351 
00352   g_assert(gcomprisBoard!=NULL);
00353 
00354   gc_board_check_file(gcomprisBoard);
00355 
00356   if(gcomprisBoard->plugin!=NULL)
00357     {
00358       /* Log the board start */
00359       gc_log_start(gcomprisBoard);
00360 
00361       bp = gcomprisBoard->plugin;
00362       gc_board_set_current(gcomprisBoard);
00363 
00364       bp->start_board(gcomprisBoard);
00365       bp_data->playing = TRUE;
00366 
00367       return;
00368     }
00369 
00370   /* We set the playing flag even if no boardplugin
00371      recognizes the board. This way we are sure it will be skipped. */
00372   bp_data->playing = TRUE;
00373 }
00374 
00375 void
00376 gc_board_pause(int pause)
00377 {
00378   if (get_board_playing() && gc_board_get_current_board_plugin())
00379     {
00380       if(gc_board_get_current_board_plugin()->pause_board)
00381        gc_board_get_current_board_plugin()->pause_board(pause);
00382     }
00383 }
00384 
00385 void
00386 gc_board_stop(void)
00387 {
00388   if (bp_data->playing && gc_board_get_current_board_plugin())
00389     {
00390       bp_data->playing = FALSE;
00391 
00392       if (gc_board_get_current_board_plugin()->end_board)
00393        gc_board_get_current_board_plugin()->end_board();
00394 
00395       gc_board_end();
00396 
00397       return;
00398     }
00399   bp_data->playing = FALSE;
00400 }
00401 
00402 static gboolean
00403 get_board_playing(void)
00404 {
00405   return bp_data->playing;
00406 }
00407 
00408 static GcomprisBoard *next_board = NULL;
00409 static gint next_board_callback_id = 0;
00410 #define NEXT_TIME_DELAY 10
00411 
00412 void board_run_next_end()
00413 {
00414   gtk_timeout_remove(next_board_callback_id);
00415   next_board_callback_id = 0;
00416 
00417   if (next_board &&
00418       next_board->previous_board &&
00419       next_board->previous_board->plugin->end_board)
00420     next_board->previous_board->plugin->end_board();
00421 
00422   /* reset the im context */
00423   gc_im_reset();
00424 
00425   /*run the board */
00426   gc_board_play(next_board);
00427 }
00428 
00429 void gc_board_run_next(GcomprisBoard *board)
00430 {
00431   board->previous_board = gc_board_get_current();
00432 
00433   next_board = board;
00434 
00435   next_board_callback_id = gtk_timeout_add (NEXT_TIME_DELAY,
00436                                        (GtkFunction) board_run_next_end,
00437                                        NULL);
00438 
00439 }