Mercurial > MadButterfly
annotate src/sprite.c @ 764:194e24d8ecab
Delete weak handle for shape objects before GC
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Sun, 29 Aug 2010 00:32:12 +0800 |
parents | 84ce2d4a8c3f |
children | 586e50f82c1f |
rev | line source |
---|---|
228 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 #include <string.h> | |
448
16116d84bc5e
Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents:
399
diff
changeset
|
4 #include "mb_graph_engine.h" |
228 | 5 #include <dlfcn.h> |
6 #include <sys/stat.h> | |
7 #include "mb_types.h" | |
8 #include "mb_shapes.h" | |
9 #include "mb_tools.h" | |
10 #include "mb_redraw_man.h" | |
11 #include "mb_observer.h" | |
12 #include "mb_prop.h" | |
13 | |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
14 #define ASSERT(x) |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
15 #define OK 0 |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
16 #define ERR 1 |
452 | 17 |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
18 static char *sprite_search_path = NULL; |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
19 |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
20 static char *sprite_search_so(const char *name) { |
452 | 21 struct stat st; |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
22 int fsz; |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
23 char *fullname; |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
24 int r; |
452 | 25 |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
26 if(sprite_search_path == NULL) |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
27 sprite_search_path = strdup("/usr/share/madbutterffly"); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
28 |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
29 fsz = strlen(sprite_search_path) + strlen(name) + 5; |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
30 fullname = (char *)malloc(fsz); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
31 |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
32 snprintf(fullname, fsz, "%s/%s.so", sprite_search_path, name); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
33 r = stat(fullname, &st); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
34 if(r != 0) { |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
35 free(fullname); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
36 return NULL; |
452 | 37 } |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
38 |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
39 return fullname; |
228 | 40 } |
41 | |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
42 void sprite_set_search_path(char *path) { |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
43 int sz; |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
44 |
452 | 45 if (sprite_search_path) |
46 free(sprite_search_path); | |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
47 |
452 | 48 sprite_search_path = strdup(path); |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
49 |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
50 sz = strlen(sprite_search_path); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
51 if(sprite_search_path[sz - 1] == '/') |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
52 sprite_search_path[sz - 1] = 0; |
228 | 53 } |
54 | |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
55 mb_sprite_t * |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
56 sprite_load(const char *name, redraw_man_t *rdman, coord_t *root) { |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
57 char cnstr_name[256]; |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
58 char *so_path; |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
59 const char *bname; |
452 | 60 void *handle; |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
61 mb_sprite_t *(*cnstr)(redraw_man_t *, coord_t *); |
452 | 62 mb_sprite_t *obj; |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
63 int r; |
452 | 64 |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
65 so_path = sprite_search_so(name); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
66 if(so_path == NULL) |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
67 return NULL; |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
68 |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
69 handle = dlopen(so_path, RTLD_LAZY); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
70 free(so_path); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
71 if (handle == NULL) |
452 | 72 return NULL; |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
73 |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
74 bname = strrchr(name, '/'); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
75 if(bname != NULL && strlen(bname) > 250) |
452 | 76 return NULL; |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
77 |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
78 if(bname == NULL) |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
79 bname = name; |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
80 else |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
81 bname++; |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
82 |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
83 snprintf(cnstr_name, sizeof(cnstr_name), "%s_new", bname); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
84 cnstr = dlsym(handle, cnstr_name); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
85 if (cnstr == NULL) |
452 | 86 return NULL; |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
87 |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
88 obj = cnstr(rdman, root); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
89 |
452 | 90 return obj; |
228 | 91 } |
92 | |
93 /* vim: set ts=4 */ |