Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/gcompris/board.c
blob: af722d92a00c978af3e444556cc8769402e8e390 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*  GCompris -- This files comes from XMMS
 *
 *  XMMS - Cross-platform multimedia player
 *  Copyright (C) 1998-2000  Peter Alm, Mikael Alm, Olle Hallnas, Thomas Nilsson and 4Front Technologies
 *
 *  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 <string.h>

#include "gcompris.h"
#include "gc_core.h"


static struct BoardPluginData *bp_data;

static gboolean	 get_board_playing(void);

#if defined _WIN32 || defined __WIN32__
# undef WIN32   /* avoid warning on mingw32 */
# define WIN32
#endif


#ifdef WIN32
extern BoardPlugin * get_advanced_colors_bplugin_info();
extern BoardPlugin * get_algebra_bplugin_info();
extern BoardPlugin * get_algebra_guesscount_bplugin_info();
extern BoardPlugin * get_awele_bplugin_info();
extern BoardPlugin * get_canal_lock_bplugin_info();
extern BoardPlugin * get_chess_bplugin_info();
extern BoardPlugin * get_clickgame_bplugin_info();
extern BoardPlugin * get_click_on_letter_bplugin_info();
extern BoardPlugin * get_clockgame_bplugin_info();
extern BoardPlugin * get_colors_bplugin_info();
extern BoardPlugin * get_draw_bplugin_info();
extern BoardPlugin * get_enumerate_bplugin_info();
extern BoardPlugin * get_erase_bplugin_info();
extern BoardPlugin * get_fifteen_bplugin_info();
extern BoardPlugin * get_gletters_bplugin_info();
extern BoardPlugin * get_gtans_bplugin_info();
extern BoardPlugin * get_hanoi_bplugin_info();
extern BoardPlugin * get_hanoi_real_bplugin_info();
extern BoardPlugin * get_imageid_bplugin_info();
extern BoardPlugin * get_leftright_bplugin_info();
extern BoardPlugin * get_machpuzzle_bplugin_info();
extern BoardPlugin * get_magic_hat_bplugin_info();
extern BoardPlugin * get_maze_bplugin_info();
extern BoardPlugin * get_memory_bplugin_info();
extern BoardPlugin * get_menu_bplugin_info();
extern BoardPlugin * get_missingletter_bplugin_info();
extern BoardPlugin * get_money_bplugin_info();
extern BoardPlugin * get_paratrooper_bplugin_info();
extern BoardPlugin * get_planegame_bplugin_info();
extern BoardPlugin * get_railroad_bplugin_info();
extern BoardPlugin * get_read_colors_bplugin_info();
extern BoardPlugin * get_reading_bplugin_info();
extern BoardPlugin * get_reversecount_bplugin_info();
extern BoardPlugin * get_shapegame_bplugin_info();
extern BoardPlugin * get_smallnumbers_bplugin_info();
extern BoardPlugin * get_submarine_bplugin_info();
extern BoardPlugin * get_superbrain_bplugin_info();
extern BoardPlugin * get_target_bplugin_info();
extern BoardPlugin * get_traffic_bplugin_info();
extern BoardPlugin * get_wordsgame_bplugin_info();
extern BoardPlugin * get_python_bplugin_info();

#define MAX_NUMBER_OF_BOARDS 200
static BoardPlugin *static_boards_demo[MAX_NUMBER_OF_BOARDS];
static BoardPlugin *static_boards[MAX_NUMBER_OF_BOARDS];

#endif

#ifdef WIN32
int gc_board_number_in_demo;
void gc_board_init(void)
{
  guint i=0;

  bp_data = g_malloc0(sizeof (struct BoardPluginData));

  static_boards_demo[i++] = get_awele_bplugin_info();
  static_boards_demo[i++] = get_click_on_letter_bplugin_info();
  static_boards_demo[i++] = get_clickgame_bplugin_info();
  static_boards_demo[i++] = get_colors_bplugin_info();
  static_boards_demo[i++] = get_crane_bplugin_info();
  static_boards_demo[i++] = get_draw_bplugin_info();
  static_boards_demo[i++] = get_enumerate_bplugin_info();
  static_boards_demo[i++] = get_erase_bplugin_info();
  static_boards_demo[i++] = get_gletters_bplugin_info();
  static_boards_demo[i++] = get_gtans_bplugin_info();
  static_boards_demo[i++] = get_hanoi_bplugin_info();
  static_boards_demo[i++] = get_magic_hat_bplugin_info();
  static_boards_demo[i++] = get_menu_bplugin_info();
  static_boards_demo[i++] = get_reading_bplugin_info();
  static_boards_demo[i++] = get_submarine_bplugin_info();
  static_boards_demo[i++] = get_superbrain_bplugin_info();
  static_boards_demo[i++] = get_target_bplugin_info();
  static_boards_demo[i++] = get_python_bplugin_info();
  static_boards_demo[i++] = NULL;

  gc_board_number_in_demo = i - 2;

  i=0;

  static_boards[i++] = get_awele_bplugin_info();
  static_boards[i++] = get_advanced_colors_bplugin_info();
  static_boards[i++] = get_algebra_bplugin_info();
  static_boards[i++] = get_algebra_bplugin_info();
  static_boards[i++] = get_algebra_guesscount_bplugin_info();
  static_boards[i++] = get_canal_lock_bplugin_info();
  static_boards[i++] = get_chess_bplugin_info();
  static_boards[i++] = get_click_on_letter_bplugin_info();
  static_boards[i++] = get_clickgame_bplugin_info();
  static_boards[i++] = get_clockgame_bplugin_info();
  static_boards[i++] = get_colors_bplugin_info();
  static_boards[i++] = get_crane_bplugin_info();
  static_boards[i++] = get_draw_bplugin_info();
  static_boards[i++] = get_enumerate_bplugin_info();
  static_boards[i++] = get_erase_bplugin_info();
  static_boards[i++] = get_fifteen_bplugin_info();
  static_boards[i++] = get_gletters_bplugin_info();
  static_boards[i++] = get_gtans_bplugin_info();
  static_boards[i++] = get_hanoi_bplugin_info();
  static_boards[i++] = get_hanoi_real_bplugin_info();
  static_boards[i++] = get_imageid_bplugin_info();
  static_boards[i++] = get_leftright_bplugin_info();
  static_boards[i++] = get_machpuzzle_bplugin_info();
  static_boards[i++] = get_magic_hat_bplugin_info();
  static_boards[i++] = get_maze_bplugin_info();
  static_boards[i++] = get_memory_bplugin_info();
  static_boards[i++] = get_menu_bplugin_info();
  static_boards[i++] = get_missingletter_bplugin_info();
  static_boards[i++] = get_money_bplugin_info();
  static_boards[i++] = get_paratrooper_bplugin_info();
  static_boards[i++] = get_planegame_bplugin_info();
  static_boards[i++] = get_railroad_bplugin_info();
  static_boards[i++] = get_read_colors_bplugin_info();
  static_boards[i++] = get_reading_bplugin_info();
  static_boards[i++] = get_reversecount_bplugin_info();
  static_boards[i++] = get_shapegame_bplugin_info();
  static_boards[i++] = get_smallnumbers_bplugin_info();
  static_boards[i++] = get_submarine_bplugin_info();
  static_boards[i++] = get_superbrain_bplugin_info();
  static_boards[i++] = get_target_bplugin_info();
  static_boards[i++] = get_traffic_bplugin_info();
  static_boards[i++] = get_wordsgame_bplugin_info();
  static_boards[i++] = get_python_bplugin_info();
  static_boards[i++] = NULL;

  i=0;
  while(static_boards[i] != NULL) {
    /* If this plugin defines an initialisation entry point, call it */
    BoardPlugin *bp;

    /* Get the BoardPlugin Info */
    bp = (BoardPlugin *) static_boards[i++];

    if(bp->init != NULL) {
      bp->init(NULL);
    }
  }
}
#else
void gc_board_init(void)
{

  /* Fist make sure the module loading is supported on this platform */
  if (!g_module_supported())
    g_error( _("Dynamic module loading is not supported. gcompris cannot load.\n") );

  bp_data = g_malloc0(sizeof (struct BoardPluginData));

}
#endif

BoardPlugin *gc_board_get_current_board_plugin(void)
{
  if(bp_data->current_gcompris_board)
    return bp_data->current_gcompris_board->plugin;

  return NULL;
}

GcomprisBoard *gc_board_get_current(void)
{
  return bp_data->current_gcompris_board;
}

void gc_board_set_current(GcomprisBoard * gcomprisBoard)
{
  bp_data->current_gcompris_board = gcomprisBoard;
}

#ifdef WIN32
gboolean
gc_board_check_file(GcomprisBoard *gcomprisBoard)
{
  GcomprisProperties	*properties = gc_prop_get();
  BoardPlugin *bp;
  guint        i=0;

  g_assert(gcomprisBoard!=NULL);
  g_assert(properties->key!=NULL);

  /* Check Already loaded */
  if(gcomprisBoard->plugin!=NULL) {
    return TRUE;
  }

  if(strncmp(properties->key, "your_welcome", 12)==0) {
    while(static_boards[i++] != NULL) {

      /* Get the BoardPlugin Info */
      bp = (BoardPlugin *) static_boards[i-1];

      if(bp->is_our_board(gcomprisBoard)) {
	/* Great, we found our plugin */
	g_warning("We found the correct plugin for board %s (type=%s)\n",
		  gcomprisBoard->name, gcomprisBoard->type);

	gcomprisBoard->plugin = bp;

	return TRUE;
      }
    }
  } else {
    while(static_boards_demo[i++] != NULL) {

      /* Get the BoardPlugin Info */
      bp = (BoardPlugin *) static_boards_demo[i-1];

      if(bp->is_our_board(gcomprisBoard)) {
	/* Great, we found our plugin */
	g_warning("We found the correct plugin for board %s (type=%s)\n",
		  gcomprisBoard->name, gcomprisBoard->type);

	gcomprisBoard->plugin = bp;

	return TRUE;
      }
    }
  }

  g_warning("No plugin library found for board type '%s', requested by '%s'",
	    gcomprisBoard->type,  gcomprisBoard->filename);

  return FALSE;
}
#else
gboolean
gc_board_check_file(GcomprisBoard *gcomprisBoard)
{
  GModule     *gmodule = NULL;
  gchar       *gmodule_file = NULL;
  BoardPlugin *(*plugin_get_bplugin_info) (void) = NULL;
  GcomprisProperties *properties = gc_prop_get();
  gchar *sep;
  gchar *type;

  g_assert(gcomprisBoard!=NULL);

  /* Check Already loaded */
  if(gcomprisBoard->plugin!=NULL) {
    return TRUE;
  }

  type = g_strdup(gcomprisBoard->type);

  /* Manage the python case where : is use to separate python plugin and boards */
  if((sep = strchr(type, ':')))
    *sep ='\0';

  gmodule_file = g_module_build_path (properties->package_plugin_dir, type);

  gmodule = g_module_open (gmodule_file, 0);
  if(gmodule) {
    g_warning("opened module %s with name %s\n", gmodule_file, type);
  } else {
    g_warning("Failed to open module %s with name %s (error=%s)\n", gmodule_file,
	      type, g_module_error());
  }
  g_free(type);

  if(gmodule != NULL) {

    g_module_symbol (gmodule, "get_bplugin_info", (gpointer) &plugin_get_bplugin_info);

    if(plugin_get_bplugin_info != NULL) {

      BoardPlugin *bp;

      /* Get the BoardPlugin Info */
      bp = (BoardPlugin *) plugin_get_bplugin_info();

      /* If this plugin defines an initialisation entry point, call it */
      if(bp->init != NULL) {
	bp->init(gcomprisBoard);
      }

      if(bp->is_our_board(gcomprisBoard)) {
	/* Great, we found our plugin */
	g_warning("We found the correct plugin for board %s (type=%s)\n", gcomprisBoard->name, gcomprisBoard->type);

	/* done in is_our_board gcomprisBoard->plugin = bp; */

	/* Save this for caching and cleanup */
	gcomprisBoard->gmodule_file = gmodule_file;
	gcomprisBoard->gmodule      = gmodule;

	return TRUE;
      } else {
	g_warning("We found a plugin with the name %s but is_our_board() returned FALSE (type=%s)\n",
		  gcomprisBoard->name,
		  gcomprisBoard->type);
      }
    } else {
      g_warning("plugin_get_bplugin_info entry point not found for %s\n",
		gcomprisBoard->filename);
    }
  }
  g_warning("No plugin library found for board type '%s', requested by '%s'",
	    gcomprisBoard->type,  gcomprisBoard->filename);

  return FALSE;
}
#endif

void
gc_board_play(GcomprisBoard *gcomprisBoard)
{
  BoardPlugin *bp;

  g_assert(gcomprisBoard!=NULL);

  gc_board_check_file(gcomprisBoard);

  if(gcomprisBoard->plugin!=NULL)
    {
      /* Log the board start */
      gc_log_start(gcomprisBoard);

      bp = gcomprisBoard->plugin;
      gc_board_set_current(gcomprisBoard);

      bp->start_board(gcomprisBoard);
      bp_data->playing = TRUE;

      return;
    }

  /* We set the playing flag even if no boardplugin
     recognizes the board. This way we are sure it will be skipped. */
  bp_data->playing = TRUE;
}

void
gc_board_pause(int pause)
{
  if (get_board_playing() && gc_board_get_current_board_plugin())
    {
      if(gc_board_get_current_board_plugin()->pause_board)
	gc_board_get_current_board_plugin()->pause_board(pause);
    }
}

void
gc_board_stop(void)
{
  if (bp_data->playing && gc_board_get_current_board_plugin())
    {
      bp_data->playing = FALSE;

      if (gc_board_get_current_board_plugin()->end_board)
	gc_board_get_current_board_plugin()->end_board();

      gc_board_end();

      return;
    }
  bp_data->playing = FALSE;
}

static gboolean
get_board_playing(void)
{
  return bp_data->playing;
}

static GcomprisBoard *next_board = NULL;
static gint next_board_callback_id = 0;
#define NEXT_TIME_DELAY 10

void board_run_next_end()
{
  gtk_timeout_remove(next_board_callback_id);
  next_board_callback_id = 0;

  if (next_board &&
      next_board->previous_board &&
      next_board->previous_board->plugin->end_board)
    next_board->previous_board->plugin->end_board();

  /* reset the im context */
  gc_im_reset();

  /*run the board */
  gc_board_play(next_board);
}

void gc_board_run_next(GcomprisBoard *board)
{
  board->previous_board = gc_board_get_current();

  next_board = board;

  next_board_callback_id = gtk_timeout_add (NEXT_TIME_DELAY,
					    (GtkFunction) board_run_next_end,
					    NULL);

}