2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <Ecore_Input.h>
19 #include <Ecore_Wl2.h>
27 #include "media_key.h"
28 #include "log_private.hh"
33 #define EXPORT_API __attribute__((visibility("default")))
46 static MediaKey& GetInst() {
49 std::lock_guard<std::recursive_mutex> lock(inst.GetMutex());
59 int Reserve(media_key_event_cb cb, void* data) {
61 std::lock_guard<std::recursive_mutex> lock(GetMutex());
65 int ret = MEDIA_KEY_ERROR_NONE;
74 std::lock_guard<std::recursive_mutex> lock(GetMutex());
85 { "XF86AudioPlay", MEDIA_KEY_PLAY },
86 { "XF86AudioStop", MEDIA_KEY_STOP },
87 { "XF86AudioPause", MEDIA_KEY_PAUSE },
88 { "XF86AudioNext", MEDIA_KEY_NEXT },
89 { "XF86AudioPrev", MEDIA_KEY_PREVIOUS },
90 { "XF86AudioRewind", MEDIA_KEY_REWIND },
91 { "XF86AudioForward", MEDIA_KEY_FASTFORWARD },
92 { "XF86AudioPlayPause", MEDIA_KEY_PLAYPAUSE },
93 { "XF86AudioMedia", MEDIA_KEY_MEDIA }
98 if (!ecore_wl2_display_connect(NULL)) {
99 _E("Failed to connect to wl2 display");
100 ecore_wl2_shutdown();
108 Ecore_Wl2_Display* dpy = ecore_wl2_connected_display_get(NULL);
110 ecore_wl2_display_disconnect(dpy);
112 ecore_wl2_shutdown();
117 for (auto i = key_map_.begin(); i != key_map_.end(); i++) {
118 _D("key(%s)", i->first.c_str());
119 auto ret = ecore_wl2_window_keygrab_set(NULL, i->first.c_str(),
120 0, 0, 0, ECORE_WL2_WINDOW_KEYGRAB_OVERRIDE_EXCLUSIVE);
121 if (ret != EINA_TRUE) {
122 _E("Failed to grab key(%s)", i->first.c_str());
124 return MEDIA_KEY_ERROR_OPERATION_FAILED;
128 key_down_handler_ = ecore_event_handler_add(ECORE_EVENT_KEY_DOWN,
129 MediaKeyPressCb, this);
130 if (!key_down_handler_)
131 _E("Failed to register key down event handler");
133 key_up_handler_ = ecore_event_handler_add(ECORE_EVENT_KEY_UP,
134 MediaKeyReleaseCb, this);
135 if (!key_up_handler_)
136 _E("Failed to register key up event handler");
143 if (key_up_handler_) {
144 ecore_event_handler_del(key_up_handler_);
145 key_up_handler_ = nullptr;
148 if (key_down_handler_) {
149 ecore_event_handler_del(key_down_handler_);
150 key_down_handler_ = nullptr;
153 for (auto i = key_map_.rbegin(); i != key_map_.rend(); i++) {
154 _D("key(%s)", i->first.c_str());
155 auto ret = ecore_wl2_window_keygrab_unset(NULL, i->first.c_str(), 0, 0);
156 if (ret != EINA_TRUE)
157 _W("Failed to ungrab key(%s)", i->first.c_str());
163 std::recursive_mutex& GetMutex() const {
167 static Eina_Bool MediaKeyPressCb(void* data, int type, void* event) {
168 Evas_Event_Key_Down* ev = static_cast<Evas_Event_Key_Down*>(event);
170 _E("Invalid parameter");
171 return ECORE_CALLBACK_RENEW;
174 auto* handle = static_cast<MediaKey*>(data);
175 if (handle->cb_ == nullptr)
176 return ECORE_CALLBACK_RENEW;
178 if (handle->key_map_.find(ev->keyname) == handle->key_map_.end())
179 return ECORE_CALLBACK_RENEW;
181 media_key_e media_key = handle->key_map_[ev->keyname];
182 handle->cb_(media_key, MEDIA_KEY_STATUS_PRESSED, handle->data_);
183 return ECORE_CALLBACK_RENEW;
186 static Eina_Bool MediaKeyReleaseCb(void* data, int type, void* event) {
187 Evas_Event_Key_Up* ev = static_cast<Evas_Event_Key_Up*>(event);
189 _E("Invalid parameter");
190 return ECORE_CALLBACK_RENEW;
193 auto* handle = static_cast<MediaKey*>(data);
194 if (handle->cb_ == nullptr)
195 return ECORE_CALLBACK_RENEW;
197 if (handle->key_map_.find(ev->keyname) == handle->key_map_.end())
198 return ECORE_CALLBACK_RENEW;
200 media_key_e media_key = handle->key_map_[ev->keyname];
201 handle->cb_(media_key, MEDIA_KEY_STATUS_RELEASED, handle->data_);
202 return ECORE_CALLBACK_RENEW;
206 bool disposed_ = true;
207 bool grabbed_ = false;
208 media_key_event_cb cb_ = nullptr;
209 void* data_ = nullptr;
210 Ecore_Event_Handler* key_up_handler_ = nullptr;
211 Ecore_Event_Handler* key_down_handler_ = nullptr;
212 std::map<std::string, media_key_e> key_map_;
213 mutable std::recursive_mutex mutex_;
218 extern "C" EXPORT_API int media_key_reserve(media_key_event_cb callback,
220 if (callback == nullptr) {
221 _E("Invalid parameter");
222 return MEDIA_KEY_ERROR_INVALID_PARAMETER;
225 return ::MediaKey::GetInst().Reserve(callback, user_data);
228 extern "C" EXPORT_API int media_key_release(void) {
229 ::MediaKey::GetInst().Release();
230 return MEDIA_KEY_ERROR_NONE;