Implements WatchBase
[platform/core/appfw/widget-viewer.git] / watch-holder / api / watch_mirror.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 "watch_mirror.h"
18 #include "../src/watch_mirror.hh"
19
20 #include <cstring>
21 #include <list>
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_mirror_s : public WatchMirror {
30  public:
31   watch_mirror_s(Evas_Object* win, watch_mirror_lifecycle_st cb, void* cb_data)
32     : WatchMirror(win), cb_(cb), cb_data_(cb_data) {}
33   void OnAdded(const ISharableWatchBase& watch) override {
34     ISharableWatchBase& w = const_cast<ISharableWatchBase&>(watch);
35     cb_.watch_mirror_lifecycle_added_cb(reinterpret_cast<sharable_watch_h>(&w), cb_data_);
36   }
37
38   void OnUpdated(const ISharableWatchBase& watch) override {
39     ISharableWatchBase& w = const_cast<ISharableWatchBase&>(watch);
40     const Watch& wa = dynamic_cast<const Watch&>(watch);
41     cb_.watch_mirror_lifecycle_updated_cb(reinterpret_cast<sharable_watch_h>(&w), wa.GetCurrentImageEvas(), cb_data_);
42   }
43
44   void OnRemoved(const ISharableWatchBase& watch) override {
45     ISharableWatchBase& w = const_cast<ISharableWatchBase&>(watch);
46     cb_.watch_mirror_lifecycle_removed_cb(reinterpret_cast<sharable_watch_h>(&w), cb_data_);
47   }
48
49   void OnChanged(const ISharableWatchBase& watch) override {
50     ISharableWatchBase& w = const_cast<ISharableWatchBase&>(watch);
51     cb_.watch_mirror_lifecycle_changed_cb(reinterpret_cast<sharable_watch_h>(&w), cb_data_);
52   }
53
54   void OnAmbientChanged(bool enter, const tizen_base::Bundle& extra) override {
55     cb_.watch_mirror_lifecycle_ambient_changed_cb(enter, extra.GetHandle(), cb_data_);
56   }
57
58   void OnAmbientEvent(EventType ev, std::string sender,
59       tizen_base::Bundle extra) override {
60     cb_.watch_mirror_lifecycle_ambient_event_cb(
61         (watch_mirror_ambient_event_e)ev, sender.c_str(), extra.GetHandle(), cb_data_);
62   }
63
64   watch_mirror_lifecycle_st cb_;
65   void* cb_data_;
66 };
67
68 C_EXPORT int watch_mirror_create(Evas_Object *viewer_win,
69     watch_mirror_lifecycle_st lifecycle, void *user_data, watch_mirror_h *handle) {
70
71   if (viewer_win == nullptr)
72     return WATCH_HOLDER_ERROR_INVALID_PARAMETER;
73
74   struct watch_mirror_s *h = new (std::nothrow) struct watch_mirror_s(
75                                             viewer_win, lifecycle, user_data);
76
77   if (h == nullptr)
78     return WATCH_HOLDER_ERROR_OUT_OF_MEMORY;
79
80   *handle = h;
81   return WATCH_HOLDER_ERROR_NONE;
82 }
83
84 C_EXPORT int watch_mirror_destroy(watch_mirror_h handle) {
85   if (handle == nullptr)
86     return WATCH_HOLDER_ERROR_INVALID_PARAMETER;
87
88   delete handle;
89   return WATCH_HOLDER_ERROR_NONE;
90 }
91
92 C_EXPORT int watch_mirror_listen(watch_mirror_h handle) {
93   if (handle == nullptr)
94     return WATCH_HOLDER_ERROR_INVALID_PARAMETER;
95
96   if (handle->Listen() < 0)
97     return WATCH_HOLDER_ERROR_INVALID_OPERATION;
98
99   return WATCH_HOLDER_ERROR_NONE;
100 }
101
102 C_EXPORT int watch_mirror_get_current(watch_mirror_h handle, sharable_watch_h *watch) {
103   if (handle == nullptr)
104     return WATCH_HOLDER_ERROR_INVALID_PARAMETER;
105
106   if (handle->GetCurrent() == nullptr)
107     return WATCH_HOLDER_ERROR_INVALID_OPERATION;
108
109   *watch = reinterpret_cast<sharable_watch_h>(handle->GetCurrent().get());
110
111   return WATCH_HOLDER_ERROR_NONE;
112 }
113