Refactoring homescreen.
[profile/ivi/ico-uxf-homescreen.git] / lib / system-controller / CicoSCResourceManager.cpp
1 /*
2  * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9
10 //==========================================================================
11 /**
12  *  @file   CicoSCResourceManager.cpp
13  *
14  *  @brief 
15  */
16 //==========================================================================
17 #include "CicoSCResourceManager.h"
18 #include "CicoLog.h"
19 #include "ico_syc_error.h"
20 #include "ico_syc_mrp_resource_private.h"
21
22 #include "ico_syc_msg_cmd_def.h"
23 #include "CicoSCSystemConfig.h"
24 #include "CicoSCCommandParser.h"
25 #include "CicoSCPolicyManager.h"
26 #include "CicoSCLifeCycleController.h"
27 #include "CicoSCAilItems.h"
28 #include "CicoSCWindowController.h"
29 #include "CicoSCInputController.h"
30
31 CicoSCResourceManager::CicoSCResourceManager()
32     : m_winCtrl(NULL), m_inputCtrl(NULL)
33 {
34     m_policyMgr = new CicoSCPolicyManager(this);
35 }
36
37 CicoSCResourceManager::~CicoSCResourceManager()
38 {
39     delete m_policyMgr;
40 }
41
42 int
43 CicoSCResourceManager::initialize(void)
44 {
45     ICO_DBG("CicoSCResourceManager::initialize Enter");
46
47     int ret = ICO_SYC_EOK;
48
49     ret = ico_syc_mrp_init(enforceDisplay, enforceSound, enforceInput, this);
50     if (ICO_SYC_EOK != ret) {
51         return ret;
52     }
53
54     ret = m_policyMgr->initialize();
55     if (ICO_SYC_EOK != ret) {
56         return ret;
57     }
58
59     ICO_DBG("CicoSCResourceManager::initialize Leave");
60     return ret;
61 }
62   
63 void
64 CicoSCResourceManager::terminate(void)
65 {
66     ICO_DBG("CicoSCResourceManager::terminate Enter");
67     m_policyMgr->terminate();
68     ICO_DBG("CicoSCResourceManager::terminate Leave");
69 }
70
71 void
72 CicoSCResourceManager::handleCommand(const CicoSCCommand &cmd,
73                                      bool internal)
74 {
75     ICO_DBG("CicoSCResourceManager::handleCommand Enter"
76             "(cmdid=%08X)", cmd.cmdid);
77
78     CicoSCCmdResCtrlOpt *opt = (CicoSCCmdResCtrlOpt*)cmd.opt;
79
80     // request command from application or internal
81     int reqtype = internal ? REQTYPE_AUTO : REQTYPE_APP;
82
83     if (MSG_CMD_ACQUIRE_RES == cmd.cmdid) {
84         
85         if (true == opt->displayres) {
86             resource_request_t *req = newResourceRequest(RESID_KIND_DISPLAY,
87                                                          reqtype,
88                                                          cmd);
89             acquireDisplayResource(req, 0);
90         }
91         if (true == opt->soundres) {
92             resource_request_t *req = newResourceRequest(RESID_KIND_SOUND,
93                                                          reqtype,
94                                                          cmd);
95             acquireSoundResource(req, 0);
96         }
97         if (true == opt->inputres) {
98             resource_request_t *req = newResourceRequest(RESID_KIND_INPUT,
99                                                          reqtype,
100                                                          cmd);
101             acquireInputResource(req, 0);
102         }
103     }
104     else if (MSG_CMD_RELEASE_RES == cmd.cmdid) {
105
106         if (true == opt->displayres) {
107             resource_request_t *req = newResourceRequest(RESID_KIND_DISPLAY,
108                                                          reqtype,
109                                                          cmd);
110             releaseDisplayResource(req);
111         }
112         if (true == opt->soundres) {
113             resource_request_t *req = newResourceRequest(RESID_KIND_SOUND,
114                                                          reqtype,
115                                                          cmd);
116             releaseSoundResource(req);
117         }
118         if (true == opt->inputres) {
119             resource_request_t *req = newResourceRequest(RESID_KIND_INPUT,
120                                                          reqtype,
121                                                          cmd);
122             releaseInputResource(req);
123         }
124     }
125     else {
126         ICO_WRN("Unknown command");
127     }
128
129     ICO_DBG("CicoSCResourceManager::handleCommand Leave");
130 }
131
132 void
133 CicoSCResourceManager::setWindowController(CicoSCWindowController *winCtrl)
134 {
135     m_winCtrl = winCtrl;
136 }
137
138 void
139 CicoSCResourceManager::setInputController(CicoSCInputController *inputCtrl)
140 {
141     m_inputCtrl = inputCtrl;
142 }
143
144 bool
145 CicoSCResourceManager::acquireDisplayResource(resource_request_t *req,
146                                               int addprio)
147 {
148     bool state = m_policyMgr->acquireDisplayResource(req->dispzoneid,
149                                                      req->category);
150     if (true == state) {
151         if (NULL != m_winCtrl) {
152             //m_winCtrl->show(req->surfaceid, NULL, 0);
153         }
154     }
155     return true;
156 }
157
158 bool
159 CicoSCResourceManager::releaseDisplayResource(resource_request_t *req)
160                                                  
161 {
162     return ico_syc_mrp_release_display_resource(req);
163 }
164
165 bool
166 CicoSCResourceManager::acquireSoundResource(resource_request_t *req,
167                                             int addprio)
168 {
169     return ico_syc_mrp_acquire_sound_resource(req, addprio);
170 }
171
172 bool
173 CicoSCResourceManager::releaseSoundResource(resource_request_t *req)
174 {
175     return ico_syc_mrp_release_sound_resource(req);
176 }
177
178 bool
179 CicoSCResourceManager::acquireInputResource(resource_request_t *req,
180                                             int addprio)
181 {
182     return ico_syc_mrp_acquire_input_resource(req, addprio);
183 }
184
185 bool
186 CicoSCResourceManager::releaseInputResource(resource_request_t *req)
187 {
188     return ico_syc_mrp_release_input_resource(req);
189 }
190
191 resource_request_t *
192 CicoSCResourceManager::newResourceRequest(int resid,
193                                           int reqtype,
194                                           const CicoSCCommand &cmd)
195 {
196     resource_request_t *req = NULL;
197     req = (resource_request_t*)calloc(1, sizeof(resource_request_t));
198     CicoSCCmdResCtrlOpt *opt = (CicoSCCmdResCtrlOpt*)cmd.opt;
199     CicoSCSystemConfig *systemConfig = CicoSCSystemConfig::getInstance();
200
201     req->reqtype     = reqtype;
202
203     CicoSCLifeCycleController *lifeCycle =
204         CicoSCLifeCycleController::getInstance();
205     const CicoSCAilItems* ailItem = lifeCycle->findAIL(cmd.appid);
206     req->category = ailItem->m_categoryID;
207     ICO_DBG("req->category=%d", req->category);
208
209     // TODO
210     const CicoSCCategoryConf *categoryConf =
211         systemConfig->findCategoryConfbyId(req->category);
212     if (NULL == categoryConf) {
213         req->prio = 10;
214     }
215  
216     req->released = 0;
217
218     /* set resource id */
219     req->resid = RESID_TYPE_BASIC;
220     if (1 == opt->type) {
221         req->resid = RESID_TYPE_INTERRUPT;
222     }
223     req->resid |= resid;
224
225     req->resid |= RESID_CMD_RELEASE;
226     if (MSG_CMD_ACQUIRE_RES == cmd.cmdid) {
227         req->resid |= RESID_CMD_ACQUIRE;
228     }
229
230     /* set application infomartion */
231     req->appid = strdup(cmd.appid.c_str());
232     req->pid   = cmd.pid;
233     req->state = ICO_APF_RESOURCE_STATE_WAITTING;
234
235     if (resid == RESID_KIND_DISPLAY) {
236         req->dispzone   = strdup(opt->displayZone.c_str());
237         req->dispzoneid = systemConfig->getDizplayZoneIdbyFullName(req->dispzone);
238         req->winname    = strdup(opt->windowName.c_str());
239         req->surfaceid  = opt->surfaceid;
240         req->id         = opt->surfaceid;;
241     }
242     else if (resid == RESID_KIND_SOUND) {
243         req->soundzone   = strdup(opt->soundZone.c_str());
244         req->soundzoneid = systemConfig->getSoundZoneIdbyFullName(req->soundzone);
245         req->sooudname   = strdup(opt->soundName.c_str());
246         req->soundid     = opt->soundid;
247         req->soundadjust = opt->adjust;
248         req->id          = opt->soundid;
249     }
250     else if (resid == RESID_KIND_INPUT) {
251         req->device = strdup(opt->device.c_str());
252         req->input  = opt->input;
253         req->id     = opt->input;
254     }
255
256     return req;
257 }
258
259 void
260 CicoSCResourceManager::delResourceRequest(resource_request_t *req)
261 {
262     if (NULL == req) return;
263
264     if (NULL != req->appid)     free(req->appid);
265     if (NULL != req->dispzone)  free(req->dispzone);
266     if (NULL != req->winname)   free(req->winname);
267     if (NULL != req->soundzone) free(req->soundzone);
268     if (NULL != req->sooudname) free(req->sooudname);
269     if (NULL != req->device)    free(req->device);
270  
271     free(req);
272 }
273
274 void
275 CicoSCResourceManager::enforceDisplay(unsigned short state,
276                                       const char *appid,
277                                       unsigned int id,
278                                       void *user_data)
279 {
280     ICO_DBG("CicoSCResourceManager::enforceDisplay Enter");
281 //    ico_uxf_window_control(appid, id, ICO_UXF_APPSCTL_INVISIBLE,
282 //            state == ICO_APF_RESOURCE_STATE_ACQUIRED ? 0 : 1);
283     ICO_DBG("CicoSCResourceManager::enforceDisplay Leave");
284 }
285                        
286 void
287 CicoSCResourceManager::enforceSound(unsigned short state,
288                                     pid_t pid,
289                                     void *user_data)
290 {
291     ICO_DBG("CicoSCResourceManager::enforceSound Enter");
292     /* NOP */
293     ICO_DBG("CicoSCResourceManager::enforceSound Leave");
294 }
295             
296 void
297 CicoSCResourceManager::enforceInput(unsigned short state,
298                                     const char *appid,
299                                     const char *device,
300                                     void *user_data)
301 {
302     ICO_DBG("CicoSCResourceManager::enforceInput Enter");
303 #if 0
304     ico_apc_inputsw_t *czone;
305     int i;
306
307     for (i = 0; i < ninputsw; i++) {
308         if (strcasecmp(inputsw[i].inputsw->swname, device) == 0)  break;
309     }
310
311     if (i >= ninputsw) {
312         i = confsys->misc.default_inputswId;
313     }
314     czone = &inputsw[i];
315
316     if (ico_uxf_input_control(state == ICO_APF_RESOURCE_STATE_ACQUIRED ? 1 : 0,
317                               appid, czone->inputdev->device,
318                               czone->inputsw->input) != ICO_UXF_EOK) {
319         ICO_WRN("failed to enforce input control");
320     }
321 #endif
322     ICO_DBG("CicoSCResourceManager::enforceInput Leave");
323 }
324
325 // receive changed state
326 void
327 CicoSCResourceManager::receiveChangedState(int state)
328 {
329     ICO_DBG("CicoSCResourceManager::receiveChanagedState Enter");
330     ICO_DBG("CicoSCResourceManager::receiveChanagedState Leave");
331 }
332 // vim:set expandtab ts=4 sw=4: