Implements WatchBase
[platform/core/appfw/widget-viewer.git] / watch-holder / api / watch_holder.cc
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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 <cstring>
18 #include <list>
19
20 #include "watch_holder.h"
21 #include "../src/watch_holder.hh"
22
23 #ifndef C_EXPORT
24 #define C_EXPORT extern "C" __attribute__((visibility("default")))
25 #endif
26
27 using namespace watch_holder;
28
29 struct watch_holder_s : public WatchHolder {
30  public:
31   watch_holder_s(Evas_Object* win, watch_holder_lifecycle_st cb, void* cb_data)
32     : WatchHolder(win), cb_(cb), cb_data_(cb_data) {}
33
34   void OnLaunched(const WatchBase& watch) override {
35     WatchBase& w = const_cast<WatchBase&>(watch);
36     cb_.watch_holder_lifecycle_launched_cb(reinterpret_cast<watch_h>(&w), cb_data_);
37   }
38
39   void OnDead(const WatchBase& watch) override {
40     WatchBase& w = const_cast<WatchBase&>(watch);
41     cb_.watch_holder_lifecycle_dead_cb(reinterpret_cast<watch_h>(&w), false, cb_data_);
42   }
43
44   void OnBound(const WatchBase& watch) override {
45     WatchBase& w = const_cast<WatchBase&>(watch);
46     cb_.watch_holder_lifecycle_bound_cb(reinterpret_cast<watch_h>(&w), cb_data_);
47   }
48
49   void OnAdded(const WatchBase& watch) override {
50     WatchBase& w = const_cast<WatchBase&>(watch);
51     cb_.watch_holder_lifecycle_added_cb(reinterpret_cast<watch_h>(&w), cb_data_);
52   }
53
54   void OnUpdated(const WatchBase& watch) override {
55     WatchBase& w = const_cast<WatchBase&>(watch);
56     cb_.watch_holder_lifecycle_updated_cb(reinterpret_cast<watch_h>(&w),
57         ((const Watch&)watch).GetCurrentImageEvas(), cb_data_);
58   }
59
60   void OnRemoved(const WatchBase& watch) override {
61     WatchBase& w = const_cast<WatchBase&>(watch);
62     cb_.watch_holder_lifecycle_removed_cb(reinterpret_cast<watch_h>(&w), cb_data_);
63   }
64
65   void OnAmbientChanged(bool enter, const tizen_base::Bundle& extra) override {
66     cb_.watch_holder_lifecycle_ambient_changed_cb(enter, extra.GetHandle(), cb_data_);
67   }
68
69   void OnAmbientEvent(EventType ev, std::string sender, tizen_base::Bundle extra) override {
70     cb_.watch_holder_lifecycle_ambient_event_cb(
71         (watch_holder_ambient_event_e)ev, sender.c_str(), extra.GetHandle(), cb_data_);
72   }
73
74   watch_holder_lifecycle_st cb_;
75   void* cb_data_;
76 };
77
78 C_EXPORT int watch_holder_create(Evas_Object *viewer_win,
79     watch_holder_lifecycle_st lifecycle, void *user_data, watch_holder_h *handle) {
80
81   if (viewer_win == nullptr)
82     return WATCH_HOLDER_ERROR_INVALID_PARAMETER;
83
84   struct watch_holder_s *h = new struct watch_holder_s(viewer_win, lifecycle,
85                                                                 user_data);
86
87   if (h == nullptr)
88     return WATCH_HOLDER_ERROR_OUT_OF_MEMORY;
89
90   *handle = h;
91   return WATCH_HOLDER_ERROR_NONE;
92 }
93
94 C_EXPORT int watch_holder_destroy(watch_holder_h handle) {
95   if (handle == nullptr)
96     return WATCH_HOLDER_ERROR_INVALID_PARAMETER;
97
98   delete handle;
99   return WATCH_HOLDER_ERROR_NONE;
100 }
101
102 C_EXPORT int watch_holder_launch(watch_holder_h handle, bool background, const char *appid, bundle *extra) {
103   if (handle == nullptr || appid == nullptr)
104     return WATCH_HOLDER_ERROR_INVALID_PARAMETER;
105
106   return handle->Launch(std::string(appid), background, extra);
107 }
108
109 C_EXPORT int watch_holder_get_current(watch_holder_h handle, watch_h *watch) {
110   if (handle == nullptr)
111     return WATCH_HOLDER_ERROR_INVALID_PARAMETER;
112
113   if (handle->GetCurrent() == nullptr)
114     return WATCH_HOLDER_ERROR_INVALID_OPERATION;
115
116   *watch = reinterpret_cast<watch_h>(handle->GetCurrent().get());
117
118   return WATCH_HOLDER_ERROR_NONE;
119 }
120
121 C_EXPORT int watch_holder_foreach_watch(watch_holder_h handle, watch_holder_foreach_cb callback, void *user_data) {
122   if (handle == nullptr || callback == nullptr)
123     return WATCH_HOLDER_ERROR_INVALID_PARAMETER;
124
125   std::list<std::shared_ptr<WatchBase>> list = handle->GetStack();
126   for (std::shared_ptr<WatchBase> wptr : list) {
127     callback(reinterpret_cast<watch_h>(wptr.get()), user_data);
128   }
129
130   return WATCH_HOLDER_ERROR_NONE;
131 }