6f06309eb3803f97cc96d7a25230a1c8ecac2894
[profile/ivi/ico-uxf-homescreen.git] / lib / system-controller / CicoSCInputController.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   CicoSCInputController.cpp
13  *
14  *  @brief  This file is implementation of CicoSCInputController class
15  */
16 //==========================================================================
17
18 #include <string>
19 #include <vector>
20 using namespace std;
21
22 #include "CicoSCInputController.h"
23 #include "CicoSCWayland.h"
24 #include "CicoLog.h"
25 #include "CicoSCSwitch.h"
26 #include "CicoSCInputDev.h"
27 #include "ico_syc_error.h"
28 #include "ico_syc_msg_cmd_def.h"
29 #include "ico_syc_private.h"
30 #include "CicoSystemConfig.h"
31 #include "CicoConf.h"
32 #include "CicoSCCommand.h"
33 #include "CicoSCServer.h"
34 #include "CicoSCMessage.h"
35
36 //--------------------------------------------------------------------------
37 /**
38  *  @brief  default constructor
39  */
40 //--------------------------------------------------------------------------
41 CicoSCInputController::CicoSCInputController()
42 {
43     CicoSCWayland* wayland = CicoSCWayland::getInstance();
44     wayland->addWaylandIF(ICO_WL_INPUT_MGR_CTRL_IF, this);
45     wayland->addWaylandIF(ICO_WL_EXINPUT_IF, this);
46     wayland->addWaylandIF(ICO_WL_INPUT_MGR_DEV_IF, this);
47
48 }
49
50 //--------------------------------------------------------------------------
51 /**
52  *  @brief  destructor
53  */
54 //--------------------------------------------------------------------------
55 CicoSCInputController::~CicoSCInputController()
56 {
57 }
58
59 #if 0 //TODO
60 int
61 initDB(void)
62 {
63     const vector<CicoSCInputDevConf*>& inputDevConfList =
64         CicoSystemConfig::getInstance()->getInputDevConfList();
65     vector<CicoSCInputDevConf*>::const_iterator itr;
66     itr = inputDevConfList.begin();
67     for (; itr != inputDevConfList.end(); ++itr) {
68         const CicoSCInputDevConf *iconf = const_cast<CicoSCInputDevConf*>(*itr);
69         CicoSCInputDev *device = new CicoSCInputDev();
70
71         device->devcie = iconf->name;
72
73         vector<CicoSCSwitchConf*>::const_iterator itr2;
74         itr2 = iconf->switchConfList.begin();
75         for (; itr2 != dconf->switchConfList.end(); ++itr2) {
76             const CicoSCSwitchConf *sconf = const_cast<CicoSCSwitchConf*>(*itr2);
77
78             CicoSCSwitch *sw = new CicoSCSwitch();
79             sw->swname = sconf->name;
80             sw->input  = sconf->id;
81             devcie->switchList.push_back(sw);
82         }
83
84
85
86
87     }
88
89 }
90 #endif
91
92 //--------------------------------------------------------------------------
93 /**
94  *  @brief executes an input control process corresponding to the command
95  *
96  *  @param  cmd     command
97  */
98 //--------------------------------------------------------------------------
99 void
100 CicoSCInputController::handleCommand(const CicoSCCommand *cmd)
101 {
102     ICO_TRA("CicoSCInputController::handleCommand Enter"
103             "(cmdid=0x%08x)", cmd->cmdid);
104
105     CicoSCCmdInputDevCtrlOpt *opt;
106     CicoSCCmdInputDevSettingOpt *set_opt; 
107
108     switch (cmd->cmdid) {
109     case MSG_CMD_ADD_INPUT:
110         opt = static_cast<CicoSCCmdInputDevCtrlOpt*>(cmd->opt);
111         addInputApp(cmd->appid, opt->device, opt->input, opt->fix,
112                     opt->keycode);
113         break;
114     case MSG_CMD_DEL_INPUT:
115         opt = static_cast<CicoSCCmdInputDevCtrlOpt*>(cmd->opt);
116         delInputApp(cmd->appid, opt->device, opt->input);
117         break;
118     case MSG_CMD_SET_REGION:
119         set_opt = static_cast<CicoSCCmdInputDevSettingOpt*>(cmd->opt);
120         setInputRegion(cmd->appid, set_opt->winname, set_opt->x, set_opt->y,
121                        set_opt->width, set_opt->height,
122                        set_opt->hotspot_x, set_opt->hotspot_y,
123                        set_opt->cursor_x, set_opt->cursor_y,
124                        set_opt->cursor_width, set_opt->cursor_height, set_opt->attr);
125         break;
126     case MSG_CMD_UNSET_REGION:
127         set_opt = static_cast<CicoSCCmdInputDevSettingOpt*>(cmd->opt);
128         unsetInputRegion(cmd->appid, set_opt->winname, set_opt->x, set_opt->y,
129                          set_opt->width, set_opt->height);
130         break;
131     default:
132         ICO_WRN("Unknown Command(0x%08x)", cmd->cmdid);
133         break;
134     }
135
136     ICO_TRA("CicoSCInputController::handleCommand Leave");
137 }
138
139 //--------------------------------------------------------------------------
140 /**
141  *  @brief  register input device control application
142  *
143  *  @param [in] appid   application id
144  *  @param [in] device  input device name
145  *  @param [in] input   input number
146  *  @parma [in] fix     fixed assign flag
147  *  @parma [in] keycode assigned keycode value
148  */
149 //--------------------------------------------------------------------------
150 int
151 CicoSCInputController::addInputApp(const string &appid,
152                                    const string &device,
153                                    int          input,
154                                    int          fix,
155                                    int          keycode)
156 {
157     ICO_TRA("CicoSCInputController::addInputApp Enter"
158             "(appid=%s device=%s input=%d fix=%d keycode=%d)",
159             appid.c_str(), device.c_str(), input, fix, keycode);
160
161     CicoSCWlInputMgrIF::addInputApp(appid.c_str(), device.c_str(),
162                                     input, fix, keycode);
163
164     ICO_TRA("CicoSCInputController::addInputApp Leave(EOK)");
165     return ICO_SYC_EOK;
166 }
167
168 //--------------------------------------------------------------------------
169 /**
170  *  @brief  unregister input device control application
171  *
172  *  @param [in] appid   application id
173  *  @param [in] device  input device name
174  *  @param [in] input   input number
175  */
176 //--------------------------------------------------------------------------
177 int
178 CicoSCInputController::delInputApp(const string &appid,
179                                    const string &device,
180                                    int          input)
181 {
182     ICO_TRA("CicoSCInputController::delInputApp Enter"
183             "(appid=%s device=%s input=%d)",
184             appid.c_str(), device.c_str(), input);
185
186     CicoSCWlInputMgrIF::delInputApp(appid.c_str(), device.c_str(), input);
187
188     ICO_TRA("CicoSCInputController::delInputApp Leave(EOK)");
189     return ICO_SYC_EOK;
190 }
191
192 //--------------------------------------------------------------------------
193 /**
194  *  @brief  set input region information
195  *
196  *  @param [in] appid     application id
197  *  @param [in] winname   window name
198  *  @param [in] x         region x positon
199  *  @param [in] y         region y positon
200  *  @param [in] width     region width
201  *  @param [in] height    region height
202  *  @param [in] hotspot_x hotspot x position
203  *  @param [in] hotspot_y hotspot y position
204  *  @param [in] cursor_x  cursor x position
205  *  @param [in] cursor_y  cursor y position
206  *  @param [in] cursor_width  cursor width
207  *  @param [in] cursor_height cursor height
208  *  @param [in] attr      region attribute
209  */
210 //--------------------------------------------------------------------------
211 int
212 CicoSCInputController::setInputRegion(const string &appid,
213                                       const string &winname,
214                                       int x,
215                                       int y,
216                                       int width,
217                                       int height,
218                                       int hotspot_x,
219                                       int hotspot_y,
220                                       int cursor_x,
221                                       int cursor_y,
222                                       int cursor_width,
223                                       int cursor_height,
224                                       int attr)
225 {
226     char    target[ICO_SYC_MAX_LEN];
227
228     snprintf(target, ICO_SYC_MAX_LEN-1, "%s@%s", winname.c_str(), appid.c_str());
229
230     ICO_TRA("CicoSCInputController::setInputRegion Enter"
231             "(target=%s x=%d y=%d width=%d height=%d "
232             "hotspot=%d/%d cursor=%d/%d-%d/%d attr=%d)",
233             target, x, y, width, height, hotspot_x, hotspot_y,
234             cursor_x, cursor_y, cursor_width, cursor_height, attr);
235
236     CicoSCWlInputMgrIF::setInputRegion(target, x, y, width, height,
237                                        hotspot_x, hotspot_y, cursor_x, cursor_y,
238                                        cursor_width, cursor_height, attr);
239
240     ICO_TRA("CicoSCInputController::setInputRegion Leave(EOK)");
241     return ICO_SYC_EOK;
242 }
243
244 //--------------------------------------------------------------------------
245 /**
246  *  @brief  unset input region information
247  *
248  *  @param [in] appid     application id
249  *  @param [in] winname   window name
250  *  @param [in] x         region x positon
251  *  @param [in] y         region y positon
252  *  @param [in] width     region width
253  *  @param [in] height    region height
254  */
255 //--------------------------------------------------------------------------
256 int
257 CicoSCInputController::unsetInputRegion(const string &appid,
258                                         const string &winname,
259                                         int x,
260                                         int y,
261                                         int width,
262                                         int height)
263 {
264     char    target[ICO_SYC_MAX_LEN];
265
266     snprintf(target, ICO_SYC_MAX_LEN-1, "%s@%s", winname.c_str(), appid.c_str());
267
268     ICO_TRA("CicoSCInputController::unsetInputRegion Enter"
269             "(target=%s x=%d y=%d width=%d height=%d", target, x, y, width, height);
270
271     CicoSCWlInputMgrIF::unsetInputRegion(target, x, y, width, height);
272
273     ICO_TRA("CicoSCInputController::unsetInputRegion Leave(EOK)");
274     return ICO_SYC_EOK;
275 }
276
277 //--------------------------------------------------------------------------
278 /**
279  *  @brief  callback to application for input switch information
280  *
281  *  @param [in] data        user data
282  *  @param [in] ico_exinput wayland ico_exinput interface
283  *  @param [in] device      input device name
284  *  @param [in] type        input device type (as enum type)
285  *  @param [in] swname      input switch name
286  *  @param [in] input       input switch number
287  *  @param [in] codename    input code name
288  *  @param [in] code        input code number
289  */
290 //--------------------------------------------------------------------------
291 void
292 CicoSCInputController::capabilitiesCB(void               *data,
293                                       struct ico_exinput *ico_exinput,
294                                       const char         *device,
295                                       int32_t            type,
296                                       const char         *swname,
297                                       int32_t            input,
298                                       const char         *codename,
299                                       int32_t            code)
300 {
301     ICO_TRA("CicoSCInputController::capabilitiesCB Enter",
302             "device=%s type=%s swname=%s input=%s codename=%s code=%d",
303             device, type, swname, input, codename, code);
304
305     // create CicoSCInputDev object
306     CicoSCInputDev *inputdev = findInputDev(device);
307     if (NULL == inputdev) {
308         inputdev = new CicoSCInputDev();
309         inputdev->device = device;
310         inputdev->type   = type;
311     }
312
313     CicoSCSwitch *sw = findInputSwitch(device, input);
314     if (NULL == sw) {
315         sw = new CicoSCSwitch();
316         sw->input   = input;
317         sw->code.push_back(code);
318         sw->codename.push_back(codename);
319     }
320     else {
321         sw->input   = input;
322     }
323
324     CicoSystemConfig *sysConf = CicoSystemConfig::getInstance();
325     const CicoSCSwitchConf *swconf = NULL;
326     swconf = sysConf->findSwitchConfbyName(device, swname);
327     if (NULL != swconf) {
328         if (false == swconf->appid.empty()) {
329             addInputApp(swconf->appid, inputdev->device,
330                         input,CicoSCSwitch::SWITCH_FIX, swconf->keycode);
331             sw->fix = true;
332         }
333     }
334
335     ICO_TRA("CicoSCInputController::capabilitiesCB Leave");
336 }
337
338 //--------------------------------------------------------------------------
339 /**
340  *  @brief  callback to application for input code information
341  *  
342  *  @param [in] data        user data
343  *  @param [in] ico_exinput wayland ico_exinput interface
344  *  @param [in] device      input device name
345  *  @param [in] input       input switch number
346  *  @param [in] codename    input code name
347  *  @param [in] code        input code number
348  */
349 //--------------------------------------------------------------------------
350 void
351 CicoSCInputController::codeCB(void               *data,
352                               struct ico_exinput *ico_exinput,
353                               const char         *device,
354                               int32_t            input,
355                               const char         *codename,
356                               int32_t            code)
357 {
358     ICO_TRA("CicoSCInputController::codeCB Enter"
359             "(device=%s input=%d codename=%s code=%d)",
360             device, input, codename, code);
361
362     CicoSCSwitch *sw = findInputSwitch(device, input);
363     if (NULL != sw) {
364         ICO_ERR("codeCB: device=%s input=%d not found.");
365         return;
366     }
367
368     if (sw->code.size() >= CicoSCSwitch::SWITCH_CODE_MAX) {
369         ICO_ERR("codeCB: device=%s input=%d number of codes overflow.");
370         return;
371     }
372
373     for (unsigned int i = 0; i < sw->code.size(); ++i) {
374         if (sw->code[i] == code) {
375             ICO_WRN("codeCB: device=%s input=%d code=%d already exist.");
376            return;
377         }
378     }
379
380     sw->code.push_back(code);
381     sw->codename.push_back(codename);
382
383     ICO_TRA("CicoSCInputController::codeCB Leave");
384 }
385
386 //--------------------------------------------------------------------------
387 /**
388  *  @brief  callback to application for switch input 
389  *
390  *  @param [in] data        user data
391  *  @param [in] ico_exinput wayland ico_exinput interface
392  *  @param [in] time        input time of millisecond
393  *  @param [in] device      input device name
394  *  @param [in] input       input switch number
395  *  @param [in] code        input switch code
396  *  @param [in] state       Of/Off status
397  */
398 //--------------------------------------------------------------------------
399 void
400 CicoSCInputController::inputCB(void               *data,
401                                struct ico_exinput *ico_exinput,
402                                uint32_t           time,
403                                const char         *device,
404                                int32_t            input,
405                                int32_t            code,
406                                int32_t            state)
407 {
408     ICO_TRA("CicoSCInputController::inputCB Enter"
409             "time=%d device=%s input=%d code=%d state=%d",
410             time, device, input, code, state);
411
412     // send message
413     CicoSCMessage *message = new CicoSCMessage();
414     message->addRootObject("command", ICO_SYC_EV_WIN_CREATE);
415     message->addArgObject("device", device);
416     message->addArgObject("time", time);
417     message->addArgObject("input", input);
418     message->addArgObject("code", code);
419     message->addArgObject("state", state);
420     CicoSCServer::getInstance()->sendMessageToHomeScreen(message);
421
422     ICO_TRA("CicoSCInputController::inputCB Leave");
423 }
424
425 //--------------------------------------------------------------------------
426 /**
427  *  @brief  callback to application for change input region
428  *
429  *  @param [in] data                user data
430  *  @param [in] ico_input_mgr_dev   wayland ico_exinput interface
431  *  @param [in] region              input regions
432  */
433 //--------------------------------------------------------------------------
434 void
435 CicoSCInputController::regionCB(void                        *data,
436                                 struct ico_input_mgr_device *ico_input_mgr_dev,
437                                 struct wl_array             *region)
438 {
439     ICO_TRA("CicoSCInputController::regionCB Enter");
440     ICO_TRA("CicoSCInputController::regionCB Leave");
441 }
442
443 //--------------------------------------------------------------------------
444 /**
445  *  @brief  find input device information
446  *
447  *  @param [in] device  input device name
448  *
449  *  @return CicoSCInputDev instance on found, NULL on not found
450  */
451 //--------------------------------------------------------------------------
452 CicoSCInputDev*
453 CicoSCInputController::findInputDev(const char *device)
454 {
455     vector<CicoSCInputDev*>::iterator itr;
456     itr = m_inputDevList.begin();
457     for (; itr != m_inputDevList.end(); ++itr) {
458         CicoSCInputDev* inputdev = const_cast<CicoSCInputDev*>(*itr);
459         if (inputdev->device == device) {
460             return inputdev;
461         }
462     }
463     return NULL;
464 }
465
466 //--------------------------------------------------------------------------
467 /**
468  *  @brief  find input device switch information
469  *
470  *  @param [in] device  input device name
471  *  @param [in] input   input number
472  *
473  *  @return CicoSCSwitch instance on found, NULL on not found
474  */
475 //--------------------------------------------------------------------------
476 CicoSCSwitch*
477 CicoSCInputController::findInputSwitch(const char *device, int input)
478 {
479     const CicoSCInputDev *inputdev = findInputDev(device);
480     if (NULL == inputdev) {
481         return NULL;
482     }
483
484     vector<CicoSCSwitch*>::const_iterator itr;
485     itr = inputdev->switchList.begin();
486     for (; itr != inputdev->switchList.end(); ++itr) {
487         CicoSCSwitch* sw = const_cast<CicoSCSwitch*>(*itr);
488         if (sw->input == input) {
489             return sw;
490         }
491     }
492     return NULL;
493 }
494 // vim:set expandtab ts=4 sw=4: