comparison src/sprite.c @ 228:25bc0e753b88

Add dynamic loading function
author wycc
date Wed, 17 Dec 2008 21:18:07 +0800
parents
children 31b6633e3538
comparison
equal deleted inserted replaced
227:a6e559fb76d7 228:25bc0e753b88
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <cairo.h>
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
14 static char *Sprite_Search_Path=NULL;
15 static int sprite_search_so(char *path, int len, const char *name)
16 {
17 struct stat st;
18
19 if (Sprite_Search_Path == NULL) {
20 Sprite_Search_Path = strdup("/usr/share/madbutterffly");
21 }
22
23 snprintf(path, len, "./%s.so", name);
24 if (stat(path, &st)==0) {
25 return 0;
26 }
27 snprintf(path, len, "%s/%s.so", Sprite_Search_Path, name);
28 if (stat(path, &st)==0) {
29 return 0;
30 } else {
31 return -1;
32 }
33 }
34
35 void sprite_set_search_path(char *path)
36 {
37 if (Sprite_Search_Path)
38 free(Sprite_Search_Path);
39 Sprite_Search_Path = strdup(path);
40 }
41
42 mb_sprite_t *sprite_load(const char *name, redraw_man_t *rdman, coord_t *root)
43 {
44 char path[1024];
45 const char *s;
46 void *handle;
47 mb_sprite_t *(*new)(redraw_man_t *, coord_t *);
48 mb_sprite_t *obj;
49
50 if (sprite_search_so(path, sizeof(path),name)) {
51 fprintf(stderr," can not find %s in search path\n", name);
52 return NULL;
53 }
54 handle = dlopen(path,RTLD_LAZY);
55 if (handle == NULL) {
56 fprintf(stderr, "can not load object %s\n", path);
57 return NULL;
58 }
59 s = name + strlen(name)-1;
60 while((s != name) && *s != '/') s--;
61 snprintf(path,sizeof(path), "%s_new", s);
62 new = dlsym(handle,path);
63 if (new == NULL) {
64 fprintf(stderr," Can not find symbol %s at module\n", path);
65 return NULL;
66 }
67 obj = new(rdman, root);
68 return obj;
69 }
70
71 /* vim: set ts=4 */