change shortcut area
[apps/homescreen.git] / homescreen / src / homescreenhandler.cpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "homescreenhandler.h"
18 #include <functional>
19 #include "hmi-debug.h"
20
21 void* HomescreenHandler::myThis = 0;
22
23 HomescreenHandler::HomescreenHandler(QObject *parent) :
24     QObject(parent),
25     mp_hs(NULL),
26     previous_application("launcher"),
27     current_applciation("launcher")
28 {
29
30 }
31
32 HomescreenHandler::~HomescreenHandler()
33 {
34     if (mp_hs != NULL) {
35         delete mp_hs;
36     }
37 }
38
39 void HomescreenHandler::init(int port, const char *token)
40 {
41     mp_hs = new LibHomeScreen();
42     mp_hs->init(port, token);
43
44     myThis = this;
45
46     mp_hs->registerCallback(nullptr, HomescreenHandler::onRep_static);
47
48     mp_hs->set_event_handler(LibHomeScreen::Event_OnScreenMessage, [this](json_object *object){
49         const char *display_message = json_object_get_string(
50             json_object_object_get(object, "display_message"));
51         HMI_DEBUG("HomeScreen","set_event_handler Event_OnScreenMessage display_message = %s", display_message);
52     });
53 }
54
55 void HomescreenHandler::tapShortcut(QString application_name, bool is_full)
56 {
57     HMI_DEBUG("HomeScreen","tapShortcut %s", application_name.toStdString().c_str());
58     struct json_object* j_json = json_object_new_object();
59     struct json_object* value;
60     if(is_full) {
61         value = json_object_new_string("fullscreen");
62         HMI_DEBUG("HomeScreen","ullscreen");
63     } else {
64         value = json_object_new_string("normal");
65         HMI_DEBUG("HomeScreen","normal");
66     }
67     json_object_object_add(j_json, "area", value);
68     mp_hs->showWindow(application_name.toStdString().c_str(), j_json);
69 }
70
71 bool HomescreenHandler::isSplit()
72 {
73     if(current_applciation == "video") {
74         if(previous_application == "navigation") {
75             return true;
76         }
77     }
78     return false;
79 }
80
81 void HomescreenHandler::setCurrentApplication(QString application_name)
82 {
83     HMI_DEBUG("HomeScreen","setCurrentApplication %s", application_name.toStdString().c_str());
84     previous_application = current_applciation;
85     current_applciation = application_name;
86 }
87
88 QString HomescreenHandler::getCurrentApplication()
89 {
90     HMI_DEBUG("HomeScreen","getCurrentApplication %s", current_applciation.toStdString().c_str());
91     return current_applciation;
92 }
93
94 void HomescreenHandler::onRep_static(struct json_object* reply_contents)
95 {
96     static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onRep(reply_contents);
97 }
98
99 void HomescreenHandler::onEv_static(const string& event, struct json_object* event_contents)
100 {
101     static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onEv(event, event_contents);
102 }
103
104 void HomescreenHandler::onRep(struct json_object* reply_contents)
105 {
106     const char* str = json_object_to_json_string(reply_contents);
107     HMI_DEBUG("HomeScreen","HomeScreen onReply %s", str);
108 }
109
110 void HomescreenHandler::onEv(const string& event, struct json_object* event_contents)
111 {
112     const char* str = json_object_to_json_string(event_contents);
113     HMI_DEBUG("HomeScreen","HomeScreen onEv %s, contents: %s", event.c_str(), str);
114
115     if (event.compare("homescreen/on_screen_message") == 0) {
116         struct json_object *json_data = json_object_object_get(event_contents, "data");
117         struct json_object *json_display_message = json_object_object_get(json_data, "display_message");
118         const char* display_message = json_object_get_string(json_display_message);
119
120         HMI_DEBUG("HomeScreen","display_message = %s", display_message);
121     }
122 }