apply FSL license
[pkgs/u/ui-gadget.git] / src / engine.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.tizenopensource.org/license
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18
19
20
21 #include <linux/limits.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <dlfcn.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29
30 #include "ug-engine.h"
31 #include "ug-dbg.h"
32
33 #define UG_ENGINE_INIT_SYM "UG_ENGINE_INIT"
34 #define UG_ENGINE_EXIT_SYM "UG_ENGINE_EXIT"
35
36 enum ug_engine_type {
37         UG_ENGINE_EFL = 0x00,
38 };
39
40 static int file_exist(const char *filename)
41 {
42         FILE *file;
43
44         file = fopen(filename, "r");
45         if (file)
46         {
47                 fclose(file);
48                 return 0;
49         }
50         return -1;
51 }
52
53 struct ug_engine *ug_engine_load()
54 {
55         void *handle;
56         struct ug_engine *engine;
57         char engine_file[PATH_MAX];
58         enum ug_engine_type type = UG_ENGINE_EFL;
59         int (*engine_init)(struct ug_engine_ops *ops);
60
61         engine = calloc(1, sizeof(struct ug_engine));
62
63         if (!engine) {
64                 errno = ENOMEM;
65                 return NULL;
66         }
67
68         if (type == UG_ENGINE_EFL) { /* UG_ENGINE_EFL is default*/
69                 if (snprintf(engine_file, PATH_MAX, "/usr/lib/libug-efl-engine.so") < 0){
70                         goto engine_free;
71                 }
72                 else if (file_exist(engine_file) < 0) {
73                         goto engine_free;
74                 }
75         }
76         else
77                 goto engine_free;
78
79         handle = dlopen(engine_file, RTLD_LAZY);
80         if (!handle) {
81                 _ERR("dlopen failed: %s\n", dlerror());
82                 goto engine_free;
83         }
84
85         engine_init = dlsym(handle, UG_ENGINE_INIT_SYM);
86         if (!engine_init) {
87                 _ERR("dlsym failed: %s\n", dlerror());
88                 goto engine_dlclose;
89         }
90
91         if (engine_init(&engine->ops))
92                 goto engine_dlclose;
93
94         engine->handle = handle;
95         return engine;
96
97 engine_dlclose:
98         dlclose(handle);
99
100 engine_free:
101         free(engine);
102         return NULL;
103 }
104
105 int ug_engine_unload(struct ug_engine *engine)
106 {
107         void (*engine_exit)(struct ug_engine_ops *ops);
108
109         if (!engine) {
110                 errno = EINVAL;
111                 return -1;
112         }
113
114         if (engine->handle) {
115                 engine_exit = dlsym(engine->handle, UG_ENGINE_EXIT_SYM);
116                 if (engine_exit)
117                         engine_exit(&engine->ops);
118                 else
119                         _ERR("dlsym failed: %s\n", dlerror());
120
121                 dlclose(engine->handle);
122         }
123
124         free(engine);
125         return 0;
126 }