Add Display state publisher 60/141960/5
authorSomin Kim <somin926.kim@samsung.com>
Wed, 2 Aug 2017 06:13:49 +0000 (15:13 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Thu, 3 Aug 2017 02:06:49 +0000 (02:06 +0000)
Change-Id: I5de790fa0101c2ff48a56d209449be7629c5256b
Signed-off-by: Somin Kim <somin926.kim@samsung.com>
src/server/publisher/DisplayState.cpp [new file with mode: 0644]

diff --git a/src/server/publisher/DisplayState.cpp b/src/server/publisher/DisplayState.cpp
new file mode 100644 (file)
index 0000000..47388f3
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <device/callback.h>
+#include <device/display.h>
+
+#include <ContextTypes.h>
+#include <JobSchedulerTypesPrivate.h>
+#include <job_scheduler_types_internal.h>
+#include "../ContextPublisher.h"
+#include "../IContextObserver.h"
+
+using namespace ctx;
+
+namespace {
+       class DisplayState : public ContextPublisher {
+       public:
+               DisplayState(uid_t uid);
+               ~DisplayState();
+
+               const char* getUri() const;
+
+       protected:
+               void read();
+               void subscribe();
+               void unsubscribe();
+
+       private:
+               static void __deviceCb(device_callback_e deviceType, void* value, void* userData);
+       };
+}
+
+REGISTER_PUBLISHER(URI(DISPLAY), DisplayState)
+
+DisplayState::DisplayState(uid_t uid)
+{
+       _D("Created");
+}
+
+DisplayState::~DisplayState()
+{
+       unsubscribe();
+
+       _D("Destroyed");
+}
+
+const char* DisplayState::getUri() const
+{
+       return URI(DISPLAY);
+}
+
+void DisplayState::read()
+{
+       display_state_e state;
+       int err = device_display_get_state(&state);
+
+       if (IS_FAILED(err))
+               _E("Getting display state failed (%d)", err);
+
+       __data[NAME(STATE)] = [&state]()->const char* {
+               if (state == DISPLAY_STATE_NORMAL)
+                       return VALUE(ON);
+               if (state == DISPLAY_STATE_SCREEN_DIM)
+                       return VALUE(DIM);
+               if (state == DISPLAY_STATE_SCREEN_OFF)
+                       return VALUE(OFF);
+               return VALUE(UNDEFINED);
+       }();
+}
+
+void DisplayState::subscribe()
+{
+       int err = device_add_callback(DEVICE_CALLBACK_DISPLAY_STATE, __deviceCb, this);
+
+       if (IS_FAILED(err))
+               _E("Add device callback failed (%d)", err);
+}
+
+void DisplayState::unsubscribe()
+{
+       device_remove_callback(DEVICE_CALLBACK_DISPLAY_STATE, __deviceCb);
+}
+
+void DisplayState::__deviceCb(device_callback_e deviceType, void* value, void* userData)
+{
+       DisplayState* pubs = static_cast<DisplayState*>(userData);
+       pubs->read();
+       pubs->notifyObservers();
+}