check rotation lock and set rotation info when ug launch
[framework/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 static int file_exist(const char *filename)
42 {
43         FILE *file;
44
45         file = fopen(filename, "r");
46         if (file)
47         {
48                 fclose(file);
49                 return 0;
50         }
51         return -1;
52 }
53
54 struct ug_engine *ug_engine_load()
55 {
56         void *handle;
57         struct ug_engine *engine;
58         char engine_file[PATH_MAX];
59         enum ug_engine_type type = UG_ENGINE_EFL;
60         int (*engine_init)(struct ug_engine_ops *ops);
61
62         engine = calloc(1, sizeof(struct ug_engine));
63
64         if (!engine) {
65                 errno = ENOMEM;
66                 return NULL;
67         }
68
69         if (type == UG_ENGINE_EFL) { /* UG_ENGINE_EFL is default*/
70                 if (snprintf(engine_file, PATH_MAX, "/usr/lib/libui-gadget-1-efl-engine.so") < 0){
71                         goto engine_free;
72                 }
73                 else if (file_exist(engine_file) < 0) {
74                         goto engine_free;
75                 }
76         }
77         else
78                 goto engine_free;
79
80         handle = dlopen(engine_file, RTLD_LAZY);
81         if (!handle) {
82                 _ERR("dlopen failed: %s\n", dlerror());
83                 goto engine_free;
84         }
85
86         engine_init = dlsym(handle, UG_ENGINE_INIT_SYM);
87         if (!engine_init) {
88                 _ERR("dlsym failed: %s\n", dlerror());
89                 goto engine_dlclose;
90         }
91
92         if (engine_init(&engine->ops))
93                 goto engine_dlclose;
94
95         engine->handle = handle;
96         return engine;
97
98 engine_dlclose:
99         dlclose(handle);
100
101 engine_free:
102         free(engine);
103         return NULL;
104 }
105
106 int ug_engine_unload(struct ug_engine *engine)
107 {
108         void (*engine_exit)(struct ug_engine_ops *ops);
109
110         if (!engine) {
111                 errno = EINVAL;
112                 return -1;
113         }
114
115         if (engine->handle) {
116                 engine_exit = dlsym(engine->handle, UG_ENGINE_EXIT_SYM);
117                 if (engine_exit)
118                         engine_exit(&engine->ops);
119                 else
120                         _ERR("dlsym failed: %s\n", dlerror());
121
122                 dlclose(engine->handle);
123         }
124
125         free(engine);
126         return 0;
127 }