2 * Copyright (c) 2011 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.
20 #include <commons/Exception.h>
21 #include <API/Display/EventChangeState.h>
23 // TODO: attach to platform event informing about changed size!
26 const char* COMMAND_XDPYINFO = "xdpyinfo";
27 const char* PATTERN_COLOR_DEPTH = "depth of root window:\\s+(\\d+)\\s+planes";
28 const char* PATTERN_SIZE = "dimensions:\\s+(\\d+)x(\\d+)";
31 namespace WrtPlugins {
34 unsigned short Screen::getColorDepth() const
36 unsigned short colorDepth = 0;
37 if (!getXDpyProperty(PATTERN_COLOR_DEPTH, &colorDepth)) {
38 ThrowMsg(Commons::PlatformException, "Could not read color depth.");
43 Dimension Screen::getSize() const
45 unsigned int width = 0, height = 0;
46 if (!getXDpyProperty(PATTERN_SIZE, &width, &height)) {
47 ThrowMsg(Commons::PlatformException, "Could not read size.");
49 return Dimension(width, height);
52 Dimension Screen::getActualSize() const
54 unsigned int width = 0, height = 0;
55 if (!getXDpyProperty(PATTERN_SIZE, &width, &height)) {
56 ThrowMsg(Commons::PlatformException, "Could not read size.");
58 if (getOrientation() == Api::Display::O_LANDSCAPE) {
59 std::swap(width, height);
61 return Dimension(width, height);
64 Api::Display::Orientation Screen::getOrientation() const
66 appcore_rm rot = APPCORE_RM_UNKNOWN;
67 if (appcore_get_rotation_state(&rot) != 0) {
68 ThrowMsg(Commons::PlatformException, "Could not get orientation.");
71 case APPCORE_RM_LANDSCAPE_NORMAL:
72 case APPCORE_RM_LANDSCAPE_REVERSE:
73 return Api::Display::O_LANDSCAPE;
75 return Api::Display::O_PORTRAIT;
79 Api::Display::State Screen::getState() const
82 int state = m_powerState.getInt();
84 case VCONFKEY_PM_STATE_NORMAL:
85 return Api::Display::ST_ON;
86 case VCONFKEY_PM_STATE_LCDDIM:
87 return Api::Display::ST_DIM;
89 return Api::Display::ST_OFF;
92 Catch(Commons::ConversionException) {
93 ReThrowMsg(Commons::PlatformException,
94 "Could not get device's power state.");
98 void Screen::addOnChangeOrientation(
99 const Api::Display::EventChangeOrientationEmitterPtr& emitter)
101 m_onChangeOrientation.attach(emitter);
102 ChangeOrientationEmitters::LockType lock = m_onChangeOrientation.getLock();
103 if (m_onChangeOrientation.size() == 1) {
104 if (appcore_set_rotation_cb(onChangeOrientation, this) != 0) {
105 ThrowMsg(Commons::PlatformException,
106 "Could not attach to platform event.");
111 void Screen::removeOnChangeOrientation(
112 Api::Display::EventChangeOrientationEmitter::IdType id)
114 m_onChangeOrientation.detach(id);
115 ChangeOrientationEmitters::LockType lock = m_onChangeOrientation.getLock();
116 if (m_onChangeOrientation.size() == 0) {
117 if (appcore_unset_rotation_cb() != 0) {
118 ThrowMsg(Commons::PlatformException,
119 "Could not detach from platform event.");
124 void Screen::addOnChangeState(
125 const Api::Display::EventChangeStateEmitterPtr& emitter)
127 m_onChangeState.attach(emitter);
128 ChangeStateEmitters::LockType lock = m_onChangeState.getLock();
129 if (m_onChangeState.size() == 1) {
130 m_powerState.attachCallback(onChangeState, this);
134 void Screen::removeOnChangeState(
135 Api::Display::EventChangeStateEmitter::IdType id)
137 m_onChangeState.detach(id);
138 ChangeStateEmitters::LockType lock = m_onChangeState.getLock();
139 if (m_onChangeState.size() == 0) {
140 m_powerState.detachCallback();
144 Screen::Screen(std::size_t index) :
146 m_powerState(VCONFKEY_PM_STATE)
150 bool Screen::getXDpyProperty(const char* pattern,
151 const pcrecpp::Arg& arg1,
152 const pcrecpp::Arg& arg2)
154 FILE* pipe = popen(COMMAND_XDPYINFO, "r");
156 ThrowMsg(Commons::PlatformException,
157 "Could not initialize pipe to xdpyinfo.");
160 bool matched = false;
161 pcrecpp::RE re(pattern);
162 char line[LINE_MAX] = { 0 };
163 while (fgets(line, LINE_MAX, pipe)) {
164 if (re.PartialMatch(line, arg1, arg2)) {
174 void Screen::onChangeState(const VConf::Node* /*node*/,
177 Screen* this_ = static_cast<Screen*>(data);
178 Api::Display::EventChangeStatePtr event(new Api::Display::EventChangeState());
180 event->setState(this_->getState());
182 Catch(Commons::PlatformException) {
183 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
185 this_->m_onChangeState.emit(event);
188 int Screen::onChangeOrientation(appcore_rm rot,
191 Screen* this_ = static_cast<Screen*>(data);
192 Api::Display::EventChangeOrientationPtr event(
193 new Api::Display::EventChangeOrientation());
195 event->setOrientation(this_->getOrientation());
197 Catch(Commons::PlatformException) {
198 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
200 this_->m_onChangeOrientation.emit(event);
201 //TODO check returned value