Release version 0.2.12
[platform/core/appfw/ui-gadget-1.git] / src / engine.c
1 /*
2  *  UI Gadget
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <linux/limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <dlfcn.h>
28 #include <unistd.h>
29 #include <sys/types.h>
30
31 #include "ug-engine.h"
32 #include "ug-dbg.h"
33
34 #define UG_ENGINE_INIT_SYM "UG_ENGINE_INIT"
35 #define UG_ENGINE_EXIT_SYM "UG_ENGINE_EXIT"
36
37 enum ug_engine_type {
38         UG_ENGINE_EFL = 0x00,
39 };
40
41 struct ug_engine *ug_engine_load()
42 {
43         void *handle;
44         struct ug_engine *engine;
45         int (*engine_init)(struct ug_engine_ops *ops);
46
47         engine = calloc(1, sizeof(struct ug_engine));
48
49         if (!engine) {
50                 /* LCOV_EXCL_START */
51                 errno = ENOMEM;
52                 return NULL;
53                 /* LCOV_EXCL_STOP */
54         }
55
56         handle = dlopen(UG_LIB_DIR"/libui-gadget-1-efl-engine.so", RTLD_LAZY);
57         if (!handle) {
58                 /* LCOV_EXCL_START */
59                 _ERR("dlopen failed: %s", dlerror());
60                 goto engine_free;
61                 /* LCOV_EXCL_STOP */
62         }
63
64         engine_init = dlsym(handle, UG_ENGINE_INIT_SYM);
65         if (!engine_init) {
66                 _ERR("dlsym failed: %s", dlerror());
67                 goto engine_dlclose;
68         }
69
70         if (engine_init(&engine->ops))
71                 goto engine_dlclose;
72
73         engine->handle = handle;
74         return engine;
75
76 /* LCOV_EXCL_START */
77 engine_dlclose:
78         dlclose(handle);
79
80 engine_free:
81         free(engine);
82         return NULL;
83 /* LCOV_EXCL_STOP */
84 }
85
86 /* LCOV_EXCL_START */
87 int ug_engine_unload(struct ug_engine *engine)
88 {
89         void (*engine_exit)(struct ug_engine_ops *ops);
90
91         if (!engine) {
92                 errno = EINVAL;
93                 return -1;
94         }
95
96         if (engine->handle) {
97                 engine_exit = dlsym(engine->handle, UG_ENGINE_EXIT_SYM);
98                 if (engine_exit)
99                         engine_exit(&engine->ops);
100                 else
101                         _ERR("dlsym failed: %s", dlerror());
102
103                 dlclose(engine->handle);
104         }
105
106         free(engine);
107         engine = NULL;
108         return 0;
109 }
110 /* LCOV_EXCL_STOP */