c2547d00e42435fc74b5129a32f59b6d24d716a6
[framework/web/wrt-plugins-common.git] / src / modules / tizen / Power / Backlight.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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  * @file        Backlight.cpp
18  * @author      Shi Hezhang (hezhang.shi@samsung.com)
19  * @version     0.1
20  */
21
22 #include <vconf.h>
23 #include <pmapi.h>
24 #include <dpl/scoped_ptr.h>
25 #include <dpl/log/log.h>
26 #include <Commons/Exception.h>
27 #include <Commons/ThreadPool.h>
28 #include "Backlight.h"
29
30 using namespace WrtDeviceApis;
31 using namespace WrtDeviceApis::Commons;
32
33 namespace WrtDeviceApis {
34 namespace Power {
35
36 Backlight::Backlight() :
37     EventLightOffTimerReqReceiver(ThreadEnum::POWER_THREAD),
38     EventLightReqReceiver(ThreadEnum::POWER_THREAD)
39 {
40     // empty
41 }
42
43 Backlight::~Backlight()
44 {
45     // empty
46 }
47
48 int Backlight::changeState(Power::Api::BacklightStatus state,
49         unsigned int timeout)
50 {
51     LogInfo("Entered!!");
52     int result = -1;
53
54     LogDebug("state: " << state << ", timeout: " << timeout);
55     result = pm_change_state(toLCDState(state));
56     if (state == Power::Api::BACKLIGHT_NORMAL) {
57         EventLightOffTimerPtr lightOffTimerEvent =
58                                         EventLightOffTimerPtr(
59                                                 new EventLightOffTimer());
60         lightOffTimerEvent->setForAsynchronousCall(NULL);
61         EventLightOffTimerReqReceiver::PostRequest(
62                                                     lightOffTimerEvent,
63                                                     timeout / 1000.0);
64     }
65
66     if (result < 0) {
67         LogDebug("[ERROR] return value result =" << result);
68     } else {
69         LogDebug("[SUCCESS]return value result =" << result);
70     }
71
72     return result;
73 }
74
75 void Backlight::changeState(const Power::Api::EventLightPtr& event)
76 {
77     EventLightReqReceiver::PostRequest(event);
78 }
79
80 int Backlight::toLCDState(Power::Api::BacklightStatus state) const
81 {
82     LogDebug("enter");
83     switch (state) {
84     case Power::Api::BACKLIGHT_DIM:
85         return LCD_DIM;
86         break;
87     case Power::Api::BACKLIGHT_NORMAL:
88         return LCD_NORMAL;
89         break;
90     case Power::Api::BACKLIGHT_OFF:
91         return LCD_OFF;
92         break;
93     default:
94         LogDebug("error status");
95         break;
96     }
97
98     return 0;
99 }
100
101 void Backlight::OnRequestReceived(const EventLightOffTimerPtr& /*event*/)
102 {
103     LogInfo("LightOffTimer event is received!!");
104     pm_change_state(LCD_DIM);
105 }
106
107 void Backlight::OnRequestReceived(const Power::Api::EventLightPtr& event)
108 {
109     LogInfo("Backlight event is received!!");
110     try
111     {
112         if (event == NULL) {
113             LogError("event is NULL!");
114             ThrowMsg(Commons::PlatformException, "Event is NULL!!");
115         }
116         LogDebug(
117             "Status: " << event->getStatus() << ", Duratin: " <<
118             event->getDuration());
119         if (changeState(event->getStatus(), event->getDuration()) < 0) {
120             LogError("changeState returned error!!");
121             ThrowMsg(Commons::PlatformException, "Play Beep is Failed!");
122         }
123     }
124     catch (Commons::PlatformException) {
125         LogError("platform exception");
126         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
127     }
128 }
129 } // Power
130 } // WrtDeviceApis
131