tizen 2.4 release
[adaptation/xorg/driver/xserver-xorg-module-lazyload.git] / src / module_lazyload_main.c
1 /**************************************************************************
2
3 xserver-xorg-module-lazyload
4
5 Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
6
7 Contact: Sung-Jin Park <sj76.park@samsung.com>
8          Sangjin LEE <lsj119@samsung.com>
9
10 Permission is hereby granted, free of charge, to any person obtaining a
11 copy of this software and associated documentation files (the
12 "Software"), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sub license, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
17
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial portions
20 of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
25 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 **************************************************************************/
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 #include <signal.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include <dbus/dbus.h>
39 #include "module_lazyload_main.h"
40 #include "module_lazyload_dbus.h"
41
42 extern Bool noInputInit;
43 extern Bool xf86LoadModules(char **list, pointer *optlist);
44
45 static ModuleLazyloadDbusMethod method;
46
47 static void
48 moduleLazyloadCommand (void *data, int argc, char **argv, char *reply, int *len)
49 {
50     ModuleLazyload *pMod = (ModuleLazyload*)data;
51
52     XDBG_DEBUG (MLAZY, "%p\n", pMod);
53
54     //REPLY ("Hello\n");
55 }
56
57 void
58 moduleLazyloadMain (ModuleLazyload *pMod, pointer opts)
59 {
60         XF86OptionPtr pOpt;
61         char *token = NULL;
62         char *tmp = NULL;
63         char *reentrant_tmp = NULL;
64         char *reentrant_optVal = NULL;
65         int mod_count = 0;
66         int opt_count;
67         const char **opt_pointer = NULL;
68         XF86OptionPtr options = NULL;
69
70         int module_count = 0;
71         char **modulelist = NULL;
72         pointer *optarray = NULL;
73
74         OsSignal(SIGINT, SIG_IGN);
75
76         if(!(XF86OptionPtr)opts)
77         {
78                 xf86Msg(X_INFO, "[%s] No options for lazyload module.\n", __FUNCTION__);
79                 goto out;
80         }
81
82         for(pOpt = (XF86OptionPtr)opts ; pOpt ; pOpt = xf86NextOption(pOpt))
83                 module_count++;
84
85 /* dead_error_condition: The condition is cannot be true. */
86 #if 0
87         if(!module_count)
88         {
89                 xf86Msg(X_INFO, "[%s] Module count is zero.\n", __FUNCTION__);
90                 goto out;
91         }
92 #endif
93         modulelist = malloc((module_count + 1) * sizeof(char *));
94         optarray = malloc((module_count + 1) * sizeof(pointer));
95
96         if(!modulelist || !optarray)
97         {
98                 xf86Msg(X_INFO, "[%s] Failed to allocate memory for (modulelist/optarray)\n", __FUNCTION__);
99                 goto out;
100         }
101
102         for(pOpt = (XF86OptionPtr)opts ; pOpt ; pOpt = xf86NextOption(pOpt))
103         {
104                 int len;
105                 char *opt_name = xf86OptionName(pOpt);
106                 char *opt_val = xf86OptionValue(pOpt);
107
108                 if(!opt_name || !opt_val)
109                 {
110                         xf86Msg(X_INFO, "[%s] opt_name or opt_val is NULL\n", __FUNCTION__);
111                         break;
112                 }
113
114                 //Input intialization
115                 if(!strncmp("init_input", opt_name, sizeof("init_input")) && !strncmp("1", opt_val, 1))
116                 {
117                         if(noInputInit)
118                         {
119                                 xf86Msg(X_INFO, "[%s] Begin InitInput\n", __FUNCTION__);
120                                 InitInput((int)0, (char **)NULL);
121                                 xf86Msg(X_INFO, "[%s] End InitInput\n", __FUNCTION__);
122                         }
123                         continue;
124                 }
125
126                 //set module name
127                 modulelist[mod_count] = opt_name;
128                 xf86Msg(X_INFO, "[%s] module [%d] = %s\n", __FUNCTION__, mod_count, modulelist[mod_count]);
129
130                 //count number of options
131                 opt_count = 0;
132                 len = strlen(opt_val);
133                 tmp = malloc((len+1)*sizeof(char));
134
135                 if(!tmp)
136                 {
137                         xf86Msg(X_INFO, "[%s] Failed to allocate memory\n", __FUNCTION__);
138                         goto out;
139                 }
140
141                 strncpy(tmp, opt_val, len);
142                 tmp[len] = '\0';
143
144                 //Count number of options
145                 
146                 token = strtok_r (tmp, " ,", &reentrant_tmp);
147                 while (token != NULL)
148                 {
149                         opt_count++;            
150                         token = strtok_r (NULL, " ,", &reentrant_tmp);
151                 }
152
153                 if(opt_count)
154                 {
155                         opt_pointer = calloc(opt_count+1, sizeof(char*));
156
157                         if(!opt_pointer)
158                         {
159                                 xf86Msg(X_INFO, "[%s] Failed to allocate memory for option pointer\n", __FUNCTION__);
160                                 goto out;
161                         }
162                 }
163
164                 //set option value(s)
165                 opt_count = 0;  
166                 token = strtok_r (opt_val, " ,", &reentrant_optVal);
167                 while (token != NULL)
168                 {
169                         opt_pointer[opt_count] = token;
170                         xf86Msg(X_INFO, "[%s] option [%d]=%s\n", __FUNCTION__, opt_count, opt_pointer[opt_count]);
171
172                         opt_count++;    
173                         token = strtok_r (NULL, " ,", &reentrant_optVal);
174                 }
175                 /* Terminate with NULL */
176                 opt_pointer[opt_count] = NULL;
177
178                 options = xf86OptionListCreate(opt_pointer, -1, 0);
179                 optarray[mod_count] = options;
180
181                 //Increase number of modules
182                 mod_count++;
183         }
184
185         modulelist[mod_count] = NULL;
186         optarray[mod_count] = NULL;
187
188         if(mod_count)
189         {
190                 xf86Msg(X_INFO, "[%s] Begin xf86LoadModules\n", __FUNCTION__);
191                 xf86LoadModules(modulelist, optarray);
192                 xf86Msg(X_INFO, "[%s] End xf86LoadModules\n", __FUNCTION__);
193
194                 xf86Msg(X_INFO, "[%s] Lazy load of modules has been done.\n", __FUNCTION__);
195         }
196
197         if(opt_pointer) free(opt_pointer);
198         if(modulelist) free(modulelist);
199         if(optarray) free(optarray);
200         if(tmp) free(tmp);
201
202     moduleLazyloadDbusConnect ();
203     snprintf (method.name, sizeof (method.name), "%s", LAZYLOAD_DBUS_METHOD);
204     method.func = moduleLazyloadCommand;
205     method.data = pMod;
206     moduleLazyloadDbusAddMethod (&method);
207
208         return;
209
210 out:
211         if(opt_pointer) free(opt_pointer);
212         if(modulelist) free(modulelist);
213         if(optarray) free(optarray);
214         if(tmp) free(tmp);
215
216     moduleLazyloadDbusRemoveMethod (&method);
217     moduleLazyloadDbusDisconnect ();
218
219         xf86Msg(X_INFO, "[%s] Failed to do lazy load !\n", __FUNCTION__);
220         return;
221 }
222
223 void
224 moduleLazyloadMainExit (ModuleLazyload *pMod)
225 {
226     moduleLazyloadDbusRemoveMethod (&method);
227     moduleLazyloadDbusDisconnect ();
228 }