Python wrapper for Gcompris

Ir. Olivier Samyn

Revision History
Revision 0.7July 2010
Updated documentation of the sound API.

Table of Contents

Notes about this wrapper
Python board structure
GcomprisBoard structure mapping
gcompris.h functions and constants Mapping
Functions mapping
GComprisBarFlags enum mapping
Misc constants mapping
Colors constants mapping
Fonts constants mapping
Board font constants mapping
Cursor constants mapping
gameutil.h functions and constants Mapping
Functions mapping
Constants mapping
bonus.h functions and constants Mapping
Functions mapping
Constants mapping (BonusStatusList)
Constants mapping (BonusList)
Constants mapping (BoardFinishedList)
score.h functions and constants Mapping
Functions mapping
Constants mapping
soundutil.h functions and constants Mapping
Functions mapping
Constants mapping
timer.h functions and constants Mapping
Functions mapping
Constants mapping
anim.h structures Mapping
Animation
CanvasItem

Notes about this wrapper

Generaly speaking, all C gc_xxx functions are wrapped to a corresponding gcompris.xxx function; parameters and return types remains the same. Some structures have also been wrapped. See below particular notes about this.

The wrapped functions are divided into packages. Every package correponds to a gcompris header file (gameutil.h, score.h, ...)

For each package/header not all functions have been wrapped. Only functions that are used/usefull for plugins are wrapped.

Notice also that not all functions have been tested (with time and new python plugins implemented they will be, but...). The wrappers have been written, they compiles, but there is no warranty on their behaviour... I you find a bug report it!

Python board structure

Each board written in python must contains a class which name is "Gcompris_xxx" where xxx is the name of the board. Let's take an example: I made a gcompris board written in python and called "pythontest".

For this board there is a gcompris pythontest.xml file containing "pythontest" for the board name and "pythonboard" for the board type. There's also a pythontest.py file containing the board code. And to finish, this file contains a "Gcompris_pythontest" class definition which in turns contains the needed gcompris board functions.

The class defined in the python board must contains the following methods:

  • __init__(self, gcomprisBoard)
  • start(self)
  • end(self)
  • ok(self)
  • repeat(self)
  • config(self)
  • key_press(self, keyval)
  • pause(self, pause)
  • set_level(self, level)

For the method role and use, please consult the sample pythontest.py board and other C boards.

GcomprisBoard structure mapping

An instance of this structure in Python will act like an instance of the C one. Here a some Python call samples:

...
  gcomprisBoard.level = 1
  gcomprisBoard.maxlevel = 1
...
  gcompris.bar_start(gcomprisBoard.canvas)
...

Here are the Python structure member name and type, and a note indicating if the attribute is readeable and/or writable.

Structure memberTypeReadableWritable
typestringYN
board_readybooleanYN
modestringYN
namestringYN
titlestringYN
descriptionstringYN
icon_namestringYN
authorstringYN
boarddirstringYN
filenamestringYN
difficultystringYN
mandatory_sound_filestringYN
sectionstringYN
menuposotionstringYN
prerequisitestringYN
goalstringYN
manualstringYN
creditstringYN
widthintYN
heightintYN
canvasgnome.canvas.CanvasYN
previous_boardGcomprisBoardYN
levelintYY
maxlevelintYY
sublevelintYY
number_of_sublevelintYY

gcompris.h functions and constants Mapping

Functions mapping

Python functionC equivalentNotes
gcompris.end_board()void gc_board_end(void) 
gcompris.bar_start(gnomeCanvas)void gc_bar_start(GnomeCanvas *theCanvas) 
gcompris.set_background(gnomeCanvasGroup, file)GnomeCanvasItem *gc_set_background(GnomeCanvasGroup *parent, gchar *file)return a gnome.canvas.CanvasItem object
gcompris.bar_set_level(gcomprisBoard)void gc_bar_set_level(GcomprisBoard *gcomprisBoard) 
gcompris.bar_set_repeat_icon(pixmap)void gc_bar_set_repeat_icon(GdkPixbuf *pixmap) 
gcompris.bar_set(flags)void gc_bar_set(const GComprisBarFlags flags) 
gcompris.bar_hide(hide)void gc_bar_hide(gboolean hide) 
gcompris.get_canvas()GnomeCanvas* gc_get_canvas()return a gnome.canvas.Canvas
gcompris.get_window()GtkWidget* gc_get_window(void)return a gtk.Widget
gcompris.get_locale()gchar* gc_locale_get(void)return a string
gcompris.set_locale(locale)void gc_locale_set(gchar *locale) 
gcompris.set_cursor(cursor_type)void gc_cursor_set(guint gdk_cursor_type) 
gcompris.images_selector_start(gcomprisBoard, dataset, callback)void gc_selector_images_start (GcomprisBoard *gcomprisBoard, gchar *dataset, ImageSelectorCallBack imscb)The callback must be a callable object
gcompris.images_selector_stop()void gc_selector_images_stop(void) 
gcompris.exit()void gc_exit(void) 
gcompris.log_set_reason (gcomprisBoard, expected, got)gc_log_set_reason (GcomprisBoard *gcomprisBoard, gchar *expected, gchar *got) 
gcompris.log_end (gcomprisBoard, status)gc_log_end (GcomprisBoard *gcomprisBoard, gchar *status) 
gcompris.file_selector_load(gcomprisBoard, rootdir, file_types, callback)void gc_selector_file_load (GcomprisBoard *gcomprisBoard, gchar *rootdir, gchar *file_types, FileSelectorCallBack fscb)The callback must be a callable object
gcompris.file_selector_save(gcomprisBoard, rootdir, file_types, callback)void gc_selector_file_save(GcomprisBoard *gcomprisBoard, gchar *rootdir,gchar *file_types, FileSelectorCallBack fscb)The callback must be a callable object
gcompris.file_selector_stop()void gc_selector_file_stop(void) 
gcompris.get_database()gchar* gc_db_get_filename(void)return the filename (string) of the database (fullpath)
gcompris.get_wordlist()GcomprisWordlist* gc_wordlist_get_from_file(const gchar *fileformat, ...)Return a WordList object loaded from the given file. It can be loaded with "wordsgame/default-$LOCALE.xml"
gcompris.get_random_word(wordlist, level)gchar* gc_wordlist_random_word_get(GcomprisWordlist *wordlist, guint level)Return a random word from the previously loaded wordlist object and the specified level.

GComprisBarFlags enum mapping

Python constantC equivalent
gcompris.BAR_LEVELGCOMPRIS_BAR_LEVEL
gcompris.BAR_REPEATGCOMPRIS_BAR_REPEAT
gcompris.BAR_REPEAT_ICONGCOMPRIS_BAR_REPEAT_ICON
gcompris.BAR_CONFIGGCOMPRIS_BAR_CONFIG
gcompris.BAR_ABOUTGCOMPRIS_BAR_ABOUT

Misc constants mapping

Python constantC equivalent
gcompris.BOARD_HEIGHTBOARDHEIGHT
gcompris.BOARD_WIDTHBOARDWIDTH
gcompris.BAR_HEIGHTBAR_HEIGHT
gcompris.DEFAULT_SKINDEFAULT_SKIN

Colors constants mapping

Python constantC equivalent
gcompris.COLOR_TITLECOLOR_TITLE
gcompris.COLOR_TEXT_BUTTONCOLOR_TEXT_BUTTON
gcompris.COLOR_CONTENTCOLOR_CONTENT
gcompris.COLOR_SUBTITLECOLOR_SUBTITLE

Fonts constants mapping

Python constantC equivalent
gcompris.FONT_TITLEFONT_TITLE
gcompris.FONT_TITLE_FALLBACKFONT_TITLE_FALLBACK
gcompris.FONT_SUBTITLEFONT_SUBTITLE
gcompris.FONT_SUBTITLE_FALLBACKFONT_SUBTITLE_FALLBACK
gcompris.FONT_CONTENTFONT_CONTENT
gcompris.FONT_CONTENT_FALLBACKFONT_CONTENT_FALLBACK

Board font constants mapping

Python constantC equivalent
gcompris.FONT_BOARD_TINYFONT_BOARD_TINY
gcompris.FONT_BOARD_SMALLFONT_BOARD_SMALL
gcompris.FONT_BOARD_MEDIUMFONT_BOARD_MEDIUM
gcompris.FONT_BOARD_BIGFONT_BOARD_BIG
gcompris.FONT_BOARD_BIG_BOLDFONT_BOARD_BIG_BOLD
gcompris.FONT_BOARD_FIXEDFONT_BOARD_FIXED
gcompris.FONT_BOARD_TITLEFONT_BOARD_TITLE
gcompris.FONT_BOARD_TITLE_BOLDFONT_BOARD_TITLE_BOLD
gcompris.FONT_BOARD_HUGEFONT_BOARD_HUGE
gcompris.FONT_BOARD_HUGE_BOLDFONT_BOARD_HUGE_BOLD

Cursor constants mapping

Python constantC equivalent
gcompris.CURSOR_FIRST_CUSTOMGCOMPRIS_FIRST_CUSTOM_CURSOR
gcompris.CURSOR_LINEGCOMPRIS_LINE_CURSOR
gcompris.CURSOR_FILLRECTGCOMPRIS_FILLRECT_CURSOR
gcompris.CURSOR_RECTGCOMPRIS_RECT_CURSOR
gcompris.CURSOR_FILLCIRCLEGCOMPRIS_FILLCIRCLE_CURSOR
gcompris.CURSOR_CIRCLEGCOMPRIS_CIRCLE_CURSOR
gcompris.CURSOR_DELGCOMPRIS_DEL_CURSOR
gcompris.CURSOR_FILLGCOMPRIS_FILL_CURSOR
gcompris.CURSOR_SELECTGCOMPRIS_SELECT_CURSOR
gcompris.CURSOR_DEFAULTGCOMPRIS_DEFAULT_CURSOR

gameutil.h functions and constants Mapping

Functions mapping

Python functionC equivalentNotes
gcompris.utils.image_to_skin(imagename)gchar *gc_skin_image_get(gchar *imagename)return a string
gcompris.skin.load_pixmap(pixmapfile)GdkPixbuf *gc_skin_pixmap_load(char *pixmapfile)return a gtk.gdk.Pixbuf
gcompris.utils.load_pixmap(pixmapfile)GdkPixbuf *gc_pixmap_load(char *pixmapfile)return a gtk.gdk.Pixbuf
gcompris.utils.find_file_absolute(file)char *gc_file_find_absolute(char *file)return a string
gcompris.utils.set_image_focus(item, focus)void gc_item_focus_set(GnomeCanvasItem *item, gboolean focus) 
gcompris.utils.item_event_focus(item, event, dest_item)gint gc_item_focus_event(GnomeCanvasItem *item, GdkEvent *event, GnomeCanvasItem *dest_item)return an integer
gcompris.utils.item_absolute_move(item, x, y)void gc_item_absolute_move(GnomeCanvasItem *item, int x, int y) 
gcompris.utils.item_rotate(item, angle)void gc_item_rotate(GnomeCanvasItem *item, double angle) 
gcompris.utils.item_rotate_relative(item, angle)void gc_item_rotate_relative(GnomeCanvasItem *item, double angle) 
gcompris.utils.item_rotate_with_center(item, angle, x, y)void gc_item_rotate_with_center(GnomeCanvasItem *item, double angle, int x, int y) 
gcompris.utils.item_rotate_relative_with_center(item, angle, x, y)void gc_item_rotate_relative_with_center(GnomeCanvasItem *item, double angle, int x, int y) 
gcompris.utils.dialog(str, callback)void gc_dialog(gchar *str, DialogBoxCallBack callback)The callback must be a callable object
gcompris.utils.dialog_close()void gc_dialog_close() 

Constants mapping

There is no constants in this package.

bonus.h functions and constants Mapping

Functions mapping

Python functionC equivalentNotes
gcompris.bonus.display(BonusStatusList, BonusList)void gc_bonus_display(BonusStatusList, BonusList) 
gcompris.bonus.board_finished(BoardFinishedList)void gc_bonus_end_display(BoardFinishedList) 

Constants mapping (BonusStatusList)

Python constantC equivalent
gcompris.bonus.LOOSEBOARD_LOOSE
gcompris.bonus.WINBOARD_WIN
gcompris.bonus.DRAWBOARD_DRAW

Constants mapping (BonusList)

Python constantC equivalent
gcompris.bonus.TIME_CLICK_TOTIME_CLICK_TO_BONUS
gcompris.bonus.RANDOMBONUS_RANDOM
gcompris.bonus.SMILEYBONUS_SMILEY
gcompris.bonus.FLOWERBONUS_FLOWER
gcompris.bonus.TUXBONUS_TUX
gcompris.bonus.GNUBONUS_TUX
gcompris.bonus.LIONBONUS_LION

Constants mapping (BoardFinishedList)

Python constantC equivalent
gcompris.bonus.FINISHED_RANDOMBOARD_FINISHED_RANDOM
gcompris.bonus.FINISHED_TUXPLANEBOARD_FINISHED_TUXPLANE
gcompris.bonus.FINISHED_TUXLOCOBOARD_FINISHED_TUXLOCO
gcompris.bonus.FINISHED_TOOMANYERRORSBOARD_FINISHED_TOOMANYERRORS

score.h functions and constants Mapping

Functions mapping

Python functionC equivalentNotes
gcompris.score.start(style, x, y, max)void gc_score_start(ScoreStyleList style, guint x, guint y, guint max) 
gcompris.score.end()void gc_score_end() 
gcompris.score.set(value)void gc_score_set(guint value) 

Constants mapping

Python constantC equivalent
gcompris.score.STYLE_NOTESCORESTYLE_NOTE
gcompris.score.STYLE_LIFESCORESTYLE_LIFE

soundutil.h functions and constants Mapping

Functions mapping

Python functionC equivalentNotes
gcompris.sound.play_ogg_list(list)void gc_sound_play_ogg_list( GList* files )The list argument is a python list
import gcompris.sound

sounds=["sounds/Harbor1.wav", "sounds/Harbor3.ogg"]
gcompris.sound.play_ogg(sounds)
		       
gcompris.sound.play_ogg(file1, ...)void gc_sound_play_ogg(char *, ...)Play all the given files.
import gcompris.sound

gcompris.sound.play_ogg("sounds/Harbor1.wav", "sounds/Harbor3.ogg")
		       
gcompris.sound.play_ogg_cb(list, callback)void gc_sound_play_ogg_cb((const gchar *file, GcomprisSoundCallback cb)The list argument is a python string and a function callback. It will be called once the sound has been played.
import gcompris.sound

gcompris.sound.play_ogg_cb("melody/into.ogg", self.intro_cb)

def intro_cb(self, file):
  print "intro passed. go play"
  self.pause(False)
		       
gcompris.sound.policy_get()int gc_sound_policy_get()Returns the current sound policy.
import gcompris.sound

self.saved_policy = gcompris.sound.policy_get()
		       
gcompris.sound.policy_set(policy)void gc_sound_policy_set(int)Set the sound policy.
import gcompris.sound

gcompris.sound.policy_set(gcompris.sound.PLAY_AND_INTERRUPT)
		       
gcompris.sound.pause()void gc_sound_bg_pause()Pause the background music.
import gcompris.sound

gcompris.sound.pause()
		       
gcompris.sound.resume()void gc_sound_bg_resume()Resume the background music.
import gcompris.sound

gcompris.sound.resume()
		       
gcompris.sound.close()void gc_sound_close()Close the audio channel. This is usefull to start an external programm which may then access the audio device on the system.
import gcompris.sound

gcompris.sound.close()
		       
gcompris.sound.reopen()void gc_sound_reopen()Reopen the audio channel to be used by GCompris.
import gcompris.sound

gcompris.sound.reopen()
		       

Constants mapping

Python constantC equivalent
gcompris.sound.PLAY_ONLY_IF_IDLEPLAY_ONLY_IF_IDLE (Discard play request if busy)
gcompris.sound.PLAY_AFTER_CURRENTPLAY_AFTER_CURRENT Default value, queue mode()
gcompris.sound.PLAY_AND_INTERRUPTPLAY_AND_INTERRUPT (Interrupt current play with the new one)

timer.h functions and constants Mapping

Functions mapping

Python functionC equivalentNotes
gcompris.timer.display(x, y, type, second, callback)void gcompris_timer_display (int x, int y, TimerList type, int second, GcomprisTimerEnd gcomprisTimerEnd);The callback must be a callable object
gcompris.timer.add(second)void gc_timer_add(int second) 
gcompris.timer.end()void gc_timer_end(void) 
gcompris.timer.get_remaining()guint gc_timer_get_remaining()Return an integer
gcompris.timer.pause(pause)void gc_timer_pause(gboolean pause) 

Constants mapping

Python constantC equivalent
gcompris.timer.TEXTGCOMPRIS_TIMER_TEXT
gcompris.timer.SANDGCOMPRIS_TIMER_SAND
gcompris.timer.BALLOONGCOMPRIS_TIMER_BALLOON
gcompris.timer.CLOCKGCOMPRIS_TIMER_CLOCK

anim.h structures Mapping

The gcompris.anim module supplies two objects, Animation and CanvasItems, corresponding to GcomprisAnimation and GcomprisAnimCanvasItem respectively.

Animation

The Animation object represents a set of animation files loaded into memory; it has no methods or members, only a constructor and a destructor. The constructor takes a single argument, the name of a text file containing a space-separated list of animation files. Each animation file in the text file corresponds to an animation state; the animations states are numbered from 0 to n-1.

CanvasItem

The CanvasItem object is a bit more interesting than the Animation object. It represents an active instance of an animation file. Its constructor takes two arguments, an Animation and a GnomeCanvasGroup (the parent of the desired active animation).

Members

Structure memberTypeReadableWritable
gnome_canvasgnome.canvas.CanvasPixbufYN
num_statesintYN

Functions

Python functionC equivalentNotes
gcompris.anim.CanvasItem.setState(int)gc_anim_set_state(GcomprisAnimCanvasItem*, int)