Fixed bug - glk_set_window(NULL) doesn't crash anymore
[rodin/chimara.git] / libchimara / dispatch.c
1 #include <libchimara/glk.h>
2 #include "chimara-glk-private.h"
3 #include "window.h"
4 #include "stream.h"
5 #include "fileref.h"
6
7 extern GPrivate *glk_data_key;
8
9 void 
10 gidispatch_set_object_registry(gidispatch_rock_t (*regi)(void *obj, glui32 objclass), void (*unregi)(void *obj, glui32 objclass, gidispatch_rock_t objrock))
11 {
12         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
13         winid_t win;
14     strid_t str;
15     frefid_t fref;
16     
17     glk_data->register_obj = regi;
18     glk_data->unregister_obj = unregi;
19     
20     if(glk_data->register_obj) 
21         {
22         /* It's now necessary to go through all existing objects, and register them. */
23         for(win = glk_window_iterate(NULL, NULL); win; win = glk_window_iterate(win, NULL))
24             win->disprock = (*glk_data->register_obj)(win, gidisp_Class_Window);
25         for(str = glk_stream_iterate(NULL, NULL); str; str = glk_stream_iterate(str, NULL))
26             str->disprock = (*glk_data->register_obj)(str, gidisp_Class_Stream);
27         for(fref = glk_fileref_iterate(NULL, NULL); fref; fref = glk_fileref_iterate(fref, NULL))
28             fref->disprock = (*glk_data->register_obj)(fref, gidisp_Class_Fileref);
29     }
30 }
31
32 gidispatch_rock_t 
33 gidispatch_get_objrock(void *obj, glui32 objclass)
34 {
35         switch(objclass) 
36         {
37                 case gidisp_Class_Window:
38                         return ((winid_t)obj)->disprock;
39                 case gidisp_Class_Stream:
40                         return ((strid_t)obj)->disprock;
41                 case gidisp_Class_Fileref:
42                         return ((frefid_t)obj)->disprock;
43                 default: 
44                 {
45                         gidispatch_rock_t dummy;
46                         dummy.num = 0;
47                         return dummy;
48                 }
49         }
50 }
51
52 void 
53 gidispatch_set_retained_registry(gidispatch_rock_t (*regi)(void *array, glui32 len, char *typecode), void (*unregi)(void *array, glui32 len, char *typecode, gidispatch_rock_t objrock))
54 {
55         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
56         glk_data->register_arr = regi;
57         glk_data->unregister_arr = unregi;
58 }