Fix casting bug
[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 ISharableWatch& watch) override {
34     ISharableWatch& w = const_cast<ISharableWatch&>(watch);
35     cb_.watch_mirror_lifecycle_added_cb(reinterpret_cast<sharable_watch_h>(&w), cb_data_);
36   }
37
38   void OnUpdated(const ISharableWatch& watch) override {
39     ISharableWatch& w = const_cast<ISharableWatch&>(watch);
40     cb_.watch_mirror_lifecycle_updated_cb(reinterpret_cast<sharable_watch_h>(&w), watch.GetCurrentImage(), cb_data_);
41   }
42
43   void OnRemoved(const ISharableWatch& watch) override {
44     ISharableWatch& w = const_cast<ISharableWatch&>(watch);
45     cb_.watch_mirror_lifecycle_removed_cb(reinterpret_cast<sharable_watch_h>(&w), cb_data_);
46   }
47
48   void OnChanged(const ISharableWatch& watch) override {
49     ISharableWatch& w = const_cast<ISharableWatch&>(watch);
50     cb_.watch_mirror_lifecycle_changed_cb(reinterpret_cast<sharable_watch_h>(&w), cb_data_);
51   }
52
53   void OnAmbientChanged(bool enter, tizen_base::Bundle& extra) override {
54     cb_.watch_mirror_lifecycle_ambient_changed_cb(enter, extra.GetHandle(), cb_data_);
55   }
56
57   void OnAmbientEvent(EventType ev, std::string sender,
58       tizen_base::Bundle extra) override {
59     cb_.watch_mirror_lifecycle_ambient_event_cb(
60         (watch_mirror_ambient_event_e)ev, sender.c_str(), extra.GetHandle(), cb_data_);
61   }
62
63   watch_mirror_lifecycle_st cb_;
64   void* cb_data_;
65 };
66
67 C_EXPORT int watch_mirror_create(Evas_Object *viewer_win,
68     watch_mirror_lifecycle_st lifecycle, void *user_data, watch_mirror_h *handle) {
69
70   if (viewer_win == nullptr)
71     return WATCH_HOLDER_ERROR_INVALID_PARAMETER;
72
73   struct watch_mirror_s *h = new (std::nothrow) struct watch_mirror_s(
74                                             viewer_win, lifecycle, user_data);
75
76   if (h == nullptr)
77     return WATCH_HOLDER_ERROR_OUT_OF_MEMORY;
78
79   *handle = h;
80   return WATCH_HOLDER_ERROR_NONE;
81 }
82
83 C_EXPORT int watch_mirror_destroy(watch_mirror_h handle) {
84   if (handle == nullptr)
85     return WATCH_HOLDER_ERROR_INVALID_PARAMETER;
86
87   delete handle;
88   return WATCH_HOLDER_ERROR_NONE;
89 }
90
91 C_EXPORT int watch_mirror_listen(watch_mirror_h handle) {
92   if (handle == nullptr)
93     return WATCH_HOLDER_ERROR_INVALID_PARAMETER;
94
95   if (handle->Listen() < 0)
96     return WATCH_HOLDER_ERROR_INVALID_OPERATION;
97
98   return WATCH_HOLDER_ERROR_NONE;
99 }
100
101 C_EXPORT int watch_mirror_get_current(watch_mirror_h handle, sharable_watch_h *watch) {
102   if (handle == nullptr)
103     return WATCH_HOLDER_ERROR_INVALID_PARAMETER;
104
105   if (handle->GetCurrent() == nullptr)
106     return WATCH_HOLDER_ERROR_INVALID_OPERATION;
107
108   *watch = reinterpret_cast<sharable_watch_h>(handle->GetCurrent().get());
109
110   return WATCH_HOLDER_ERROR_NONE;
111 }
112