tizen 2.3 release
[framework/web/wearable/wrt-plugins-tizen.git] / src / Mediakey / MediaKeyManager.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include "MediaKeyManager.h"
19 #include "MediaKeyUtil.h"
20
21 #include <JSStringRefWrapper.h>
22 #include <PlatformException.h>
23 #include <Logger.h>
24 #include <glib.h>
25
26 #include <media_key.h>
27
28 namespace DeviceAPI {
29 namespace MediaKey {
30
31 void onMediaKeyEventCb(media_key_e key, media_key_event_e status, void* user_data)
32 {
33     LOGD("Entered");
34
35     MediaKeyManager* mediaKeyManager = MediaKeyManager::getInstance();
36
37     MediaKeyType mediaKeyType = MediaKeyUtil::mediakeyTypeToString(key);
38     MediaKeyEventType mediaKeyEventType= MediaKeyUtil::mediakeyEventTypeToString(status);
39
40     mediaKeyManager->onMediaKey(mediaKeyType, mediaKeyEventType);
41 }
42
43
44 MediaKeyManager::MediaKeyManager() :
45     m_listener(MediaKeyEventListenerPtr(NULL)),
46     m_listeningPlatformEvent(false)
47 {
48 }
49
50 MediaKeyManager* MediaKeyManager::getInstance()
51 {
52     static MediaKeyManager instance;
53     return &instance;
54 }
55
56
57 MediaKeyManager::~MediaKeyManager()
58 {
59     m_listener = NULL;
60
61     if(m_listeningPlatformEvent)
62     {
63         LOGD("::media_key_release");
64         int ret = media_key_release();
65         if (ret != MEDIA_KEY_ERROR_NONE)
66         {
67             LOGE("ret : %d", ret);
68             MediaKeyUtil::throwMediaKeyException(ret, "media_key_release()");
69         }
70         m_listeningPlatformEvent = false;
71     }
72 }
73
74
75 void MediaKeyManager::setMediaKeyEventListener(MediaKeyEventListenerPtr listener)
76 {
77     LOGD("Enter MediaKeyManager setMediaKeyEventListener");
78
79     if(m_listener){
80         LOGD("delete m_listener");
81         m_listener = NULL;
82     }
83
84     if(listener){
85         m_listener = listener;
86     }
87
88     if(!m_listeningPlatformEvent)
89     {
90         LOGD("::media_key_reserve");
91         int ret = media_key_reserve(onMediaKeyEventCb, static_cast<void*>(this));
92         if (ret != MEDIA_KEY_ERROR_NONE)
93         {
94             LOGE("ret : %d", ret);
95             MediaKeyUtil::throwMediaKeyException(ret, "media_key_reserve()");
96         }
97         m_listeningPlatformEvent = true;
98     }
99 }
100 void MediaKeyManager::unsetMediaKeyEventListener()
101 {
102     LOGD("Enter MediaKeyManager unsetMediaKeyEventListener");
103     m_listener = NULL;
104
105     if(m_listeningPlatformEvent)
106     {
107         LOGD("::media_key_release");
108         int ret = media_key_release();
109         if (ret != MEDIA_KEY_ERROR_NONE)
110         {
111             LOGE("ret : %d", ret);
112             MediaKeyUtil::throwMediaKeyException(ret, "media_key_release()");
113         }
114         m_listeningPlatformEvent = false;
115     }
116 }
117
118 void MediaKeyManager::onMediaKey(MediaKeyType type, MediaKeyEventType event)
119 {
120         if(!m_listeningPlatformEvent)
121         {
122                 LOGE("event is not registered");
123                 return;
124         }
125
126         if(NULL == m_listener)
127         {
128                 LOGE("event listener is not registered");
129                 return;
130         }
131
132
133         MediaKeyEventListenerPtr cb = m_listener;
134         cb->setMediaKeyType(type);
135         MediaKeyEventListenerPtr* data = new MediaKeyEventListenerPtr(cb);
136
137         switch(event)
138         {
139         case MediaKeyEventType::MEDIA_KEY_EVENT_PRESSED:
140                 LOGD("event : PRESSED");
141                 if (!g_idle_add(MediaKeyEventCallback::onpressed, data)) {
142                     LOGE("g_idle addition failed");
143                     throw UnknownException("g_idle addition failed");
144                 }
145                 break;
146         case MEDIA_KEY_STATUS_RELEASED:
147                 LOGD("event : RELEASED");
148                 if (!g_idle_add(MediaKeyEventCallback::onreleased, data)) {
149                     LOGE("g_idle addition failed");
150                     throw UnknownException("g_idle addition failed");
151                 }
152                 break;
153         }
154 }
155
156 } // MediaKey
157 } // DeviceAPI