From f47e00e18d82e82aade75664ed2d30d0532ff0f5 Mon Sep 17 00:00:00 2001 From: Halton Huo Date: Wed, 31 Jul 2013 10:38:29 +0800 Subject: [PATCH] Implement DISPLAY listener --- examples/system_info.html | 22 ++++ system_info/system_info_context.cc | 57 +---------- system_info/system_info_context.h | 9 +- system_info/system_info_display.h | 36 ++++--- system_info/system_info_display_x11.cc | 179 +++++++++++++++++++-------------- 5 files changed, 155 insertions(+), 148 deletions(-) diff --git a/examples/system_info.html b/examples/system_info.html index ce2f408..70c52f3 100644 --- a/examples/system_info.html +++ b/examples/system_info.html @@ -91,6 +91,28 @@ handle("storage_btn", function() { }); handle("display_btn", function() { + var count = 0; + var id = tizen.systeminfo.addPropertyValueChangeListener( + "DISPLAY", + function(display) { + output.value += '\n Property DISPLAY changed.'; + output.value += '\n\t resolutionWidth: ' + display.resolutionWidth; + output.value += '\n\t resolutionHeight: ' + display.resolutionHeight; + output.value += '\n\t dotsPerInchWidth: ' + display.dotsPerInchWidth; + output.value += '\n\t dotsPerInchHeight: ' + display.dotsPerInchHeight; + output.value += '\n\t physicalWidth: ' + display.physicalWidth; + output.value += '\n\t physicalHeight: ' + display.physicalHeight; + output.value += '\n\t brightness: ' + display.brightness; + output.scrollTop = output.scrollHeight; + count += 1; + if (count == 3) { + output.value += '\n Maximum listen times(3) reached. Remove listener with id = ' + id; + output.scrollTop = output.scrollHeight; + count = 0; + tizen.systeminfo.removePropertyValueChangeListener(id); + } + }); + tizen.systeminfo.getPropertyValue( "DISPLAY", function(display) { diff --git a/system_info/system_info_context.cc b/system_info/system_info_context.cc index 4ee642d..d63ea82 100644 --- a/system_info/system_info_context.cc +++ b/system_info/system_info_context.cc @@ -9,7 +9,6 @@ #include "common/picojson.h" #include "system_info/system_info_battery.h" #include "system_info/system_info_build.h" -#include "system_info/system_info_display.h" #include "system_info/system_info_locale.h" #include "system_info/system_info_storage.h" #include "system_info/system_info_utils.h" @@ -22,7 +21,8 @@ CXWalkExtension* xwalk_extension_init(int32_t api_version) { SystemInfoContext::SystemInfoContext(ContextAPI* api) : api_(api), - cpu_(SysInfoCpu::GetSysInfoCpu(api)) { + cpu_(SysInfoCpu::GetSysInfoCpu(api)), + display_(SysInfoDisplay::GetSysInfoDisplay(api)) { } SystemInfoContext::~SystemInfoContext() { @@ -53,53 +53,6 @@ void SystemInfoContext::GetStorage(picojson::value& error, s.Update(error, data); } -void SystemInfoContext::GetDisplay(picojson::value& error, - picojson::value& data) { - double ret; - SysInfoDisplay& disp = SysInfoDisplay::GetSysInfoDisplay(); - - SetPicoJsonObjectValue(error, "message", picojson::value("")); - ret = static_cast(disp.GetResolutionWidth(error)); - if (DidFail(error)) - return; - SetPicoJsonObjectValue(data, "resolutionWidth", picojson::value(ret)); - - ret = static_cast(disp.GetResolutionHeight(error)); - if (DidFail(error)) - return; - SetPicoJsonObjectValue(data, "resolutionHeight", picojson::value(ret)); - - ret = static_cast(disp.GetDotsPerInchWidth(error)); - if (DidFail(error)) - return; - SetPicoJsonObjectValue(data, "dotsPerInchWidth", picojson::value(ret)); - - ret = static_cast(disp.GetDotsPerInchWidth(error)); - if (DidFail(error)) - return; - SetPicoJsonObjectValue(data, "dotsPerInchWidth", picojson::value(ret)); - - ret = static_cast(disp.GetDotsPerInchHeight(error)); - if (DidFail(error)) - return; - SetPicoJsonObjectValue(data, "dotsPerInchHeight", picojson::value(ret)); - - ret = static_cast(disp.GetPhysicalWidth(error)); - if (DidFail(error)) - return; - SetPicoJsonObjectValue(data, "physicalWidth", picojson::value(ret)); - - ret = static_cast(disp.GetPhysicalHeight(error)); - if (DidFail(error)) - return; - SetPicoJsonObjectValue(data, "physicalHeight", picojson::value(ret)); - - ret = static_cast(disp.GetBrightness(error)); - if (DidFail(error)) - return; - SetPicoJsonObjectValue(data, "brightness", picojson::value(ret)); -} - void SystemInfoContext::GetBuild(picojson::value& error, picojson::value& data) { SysInfoBuild& build = SysInfoBuild::GetSysInfoBuild(error); @@ -153,7 +106,7 @@ void SystemInfoContext::HandleGetPropertyValue(const picojson::value& input, } else if (prop == "STORAGE") { GetStorage(error, data); } else if (prop == "DISPLAY") { - GetDisplay(error, data); + display_.Get(error, data); } else if (prop == "DEVICE_ORIENTATION ") { GetDeviceOrientation(error, data); } else if (prop == "BUILD") { @@ -195,7 +148,7 @@ void SystemInfoContext::HandleStartListen(const picojson::value& input) { } else if (prop == "STORAGE") { // FIXME(halton): Add STORAGE listener } else if (prop == "DISPLAY") { - // FIXME(halton): Add DISPLAY listener + display_.StartListen(); } else if (prop == "DEVICE_ORIENTATION ") { // FIXME(halton): Add DEVICE_ORIENTATION listener } else if (prop == "BUILD") { @@ -225,7 +178,7 @@ void SystemInfoContext::HandleStopListen(const picojson::value& input) { } else if (prop == "STORAGE") { // FIXME(halton): Remove STORAGE listener } else if (prop == "DISPLAY") { - // FIXME(halton): Remove DISPLAY listener + display_.StopListen(); } else if (prop == "DEVICE_ORIENTATION ") { // FIXME(halton): Remove DEVICE_ORIENTATION listener } else if (prop == "BUILD") { diff --git a/system_info/system_info_context.h b/system_info/system_info_context.h index 1d1b206..bce103a 100644 --- a/system_info/system_info_context.h +++ b/system_info/system_info_context.h @@ -8,6 +8,7 @@ #include "common/extension_adapter.h" #include "common/picojson.h" #include "system_info/system_info_cpu.h" +#include "system_info/system_info_display.h" namespace picojson { class value; @@ -33,8 +34,6 @@ class SystemInfoContext { picojson::value& data); void GetStorage(picojson::value& error, picojson::value& data); - void GetDisplay(picojson::value& error, - picojson::value& data); void GetDeviceOrientation(picojson::value& error, picojson::value& data); void GetBuild(picojson::value& error, @@ -52,13 +51,9 @@ class SystemInfoContext { void GetPeripheral(picojson::value& error, picojson::value& data); - inline bool DidFail(picojson::value& error) { - picojson::object& error_map = error.get(); - return !error_map["message"].to_str().empty(); - } - ContextAPI* api_; SysInfoCpu& cpu_; + SysInfoDisplay& display_; }; #endif // SYSTEM_INFO_SYSTEM_INFO_CONTEXT_H_ diff --git a/system_info/system_info_display.h b/system_info/system_info_display.h index 0d539de..733b1e8 100644 --- a/system_info/system_info_display.h +++ b/system_info/system_info_display.h @@ -5,33 +5,45 @@ #ifndef SYSTEM_INFO_SYSTEM_INFO_DISPLAY_H_ #define SYSTEM_INFO_SYSTEM_INFO_DISPLAY_H_ +#include + +#include "common/extension_adapter.h" #include "common/picojson.h" #include "common/utils.h" class SysInfoDisplay { public: - static SysInfoDisplay& GetSysInfoDisplay() { - static SysInfoDisplay d; + static SysInfoDisplay& GetSysInfoDisplay(ContextAPI* api) { + static SysInfoDisplay d(api); return d; } ~SysInfoDisplay() { } - - unsigned long GetResolutionWidth(picojson::value& error); - unsigned long GetResolutionHeight(picojson::value& error); - unsigned long GetDotsPerInchWidth(picojson::value& error); - unsigned long GetDotsPerInchHeight(picojson::value& error); - double GetPhysicalWidth(picojson::value& error); - double GetPhysicalHeight(picojson::value& error); - double GetBrightness(picojson::value& error); + // Get support + void Get(picojson::value& error, picojson::value& data); + // Listerner support + inline void StartListen() { + stopping_ = false; + g_timeout_add_seconds(3, + SysInfoDisplay::TimedOutUpdate, + static_cast(this)); + } + void StopListen() { stopping_ = true; } private: - SysInfoDisplay(); - void Update(picojson::value& error); + SysInfoDisplay(ContextAPI* api); + + static gboolean TimedOutUpdate(gpointer user_data); + bool UpdateSize(); + bool UpdateBrightness(); + void SetData(picojson::value& data); + ContextAPI* api_; unsigned long resolution_width_; unsigned long resolution_height_; double physical_width_; double physical_height_; + double brightness_; + double stopping_; DISALLOW_COPY_AND_ASSIGN(SysInfoDisplay); }; diff --git a/system_info/system_info_display_x11.cc b/system_info/system_info_display_x11.cc index c2320d3..02250b5 100644 --- a/system_info/system_info_display_x11.cc +++ b/system_info/system_info_display_x11.cc @@ -22,109 +22,134 @@ using namespace system_info; -void SysInfoDisplay::Update(picojson::value& error) { - Display *dpy = XOpenDisplay(NULL); +SysInfoDisplay::SysInfoDisplay(ContextAPI* api) + : resolution_width_(0), + resolution_height_(0), + physical_width_(0.0), + physical_height_(0.0), + brightness_(0.0), + stopping_(false) { + api_ = api; +} - if (NULL == dpy) { +void SysInfoDisplay::Get(picojson::value& error, + picojson::value& data) { + if (!UpdateSize()) { SetPicoJsonObjectValue(error, "message", - picojson::value("XOpenDisplay Failed")); + picojson::value("Get display size failed.")); return; } - resolution_width_ = DisplayWidth(dpy, DefaultScreen(dpy)); - if (!resolution_width_) { + if (!UpdateBrightness()) { SetPicoJsonObjectValue(error, "message", - picojson::value("SCREEN WIDTH is 0px.")); - XCloseDisplay(dpy); + picojson::value("Get display brightness failed.")); return; } - resolution_height_ = DisplayHeight(dpy, DefaultScreen(dpy)); - if (!resolution_height_) { - SetPicoJsonObjectValue(error, "message", - picojson::value("SCREEN HEIGHT is 0px.")); - XCloseDisplay(dpy); - return; - } + SetData(data); + SetPicoJsonObjectValue(error, "message", picojson::value("")); +} - physical_width_ = DisplayWidthMM(dpy, DefaultScreen(dpy)); - if (!physical_width_) { - SetPicoJsonObjectValue(error, "message", - picojson::value("SCREEN WIDTH is 0mm.")); - XCloseDisplay(dpy); - return; +bool SysInfoDisplay::UpdateSize() { + Display *dpy = XOpenDisplay(NULL); + + if (NULL == dpy) { + return false; } + resolution_width_ = DisplayWidth(dpy, DefaultScreen(dpy)); + resolution_height_ = DisplayHeight(dpy, DefaultScreen(dpy)); + physical_width_ = DisplayWidthMM(dpy, DefaultScreen(dpy)); physical_height_ = DisplayHeightMM(dpy, DefaultScreen(dpy)); - if (!physical_width_) { - SetPicoJsonObjectValue(error, "message", - picojson::value("SCREEN HEIGHT is 0mm.")); - XCloseDisplay(dpy); - return; - } XCloseDisplay(dpy); + return true; } -SysInfoDisplay::SysInfoDisplay() - : resolution_width_(0), - resolution_height_(0), - physical_width_(0.0), - physical_height_(0.0) { -} +bool SysInfoDisplay::UpdateBrightness() { + char* str_val; -unsigned long SysInfoDisplay::GetResolutionWidth(picojson::value& error) { - Update(error); - return resolution_width_; -} + str_val = ReadOneLine(ACPI_BACKLIGHT_DIR"/max_brightness"); + if(!str_val) { + // FIXME(halton): ACPI is not enabled, fallback to maximum. + brightness_ = 1.0; + return true; + } + int max_val = atoi(str_val); + free(str_val); -unsigned long SysInfoDisplay::GetResolutionHeight(picojson::value& error) { - Update(error); - return resolution_height_; -} + str_val = ReadOneLine(ACPI_BACKLIGHT_DIR"/max_brightness"); + if(!str_val) { + // FIXME(halton): ACPI is not enabled, fallback to maximum. + brightness_ = 1.0; + return true; + } + int val = atoi(str_val); + free(str_val); -unsigned long SysInfoDisplay::GetDotsPerInchWidth(picojson::value& error) { - Update(error); - // dpi = N * 25.4 pixels / M inch - return (resolution_width_ * 25.4) / physical_width_; + brightness_ = val / max_val; + return true; } -unsigned long SysInfoDisplay::GetDotsPerInchHeight(picojson::value& error) { - Update(error); - return (resolution_height_ * 25.4) / physical_height_; -} +gboolean SysInfoDisplay::TimedOutUpdate(gpointer user_data) { + SysInfoDisplay* instance = static_cast(user_data); + + if (instance->stopping_) { + instance->stopping_ = false; + return FALSE; + } + + double old_brightness = instance->brightness_; + if (!instance->UpdateBrightness()) { + // Fail to update brightness, wait for next round + return TRUE; + } -double SysInfoDisplay::GetPhysicalWidth(picojson::value& error) { - Update(error); - return physical_width_; -} + unsigned long old_resolution_width = instance->resolution_width_; + unsigned long old_resolution_height = instance->resolution_width_; + double old_physical_width = instance->physical_width_; + double old_physical_height = instance->physical_height_; + if (!instance->UpdateSize()) { + // Fail to update size, wait for next round + return TRUE; + } -double SysInfoDisplay::GetPhysicalHeight(picojson::value& error) { - Update(error); - return physical_height_; + if ((old_brightness != instance->brightness_) || + (old_resolution_width != instance->resolution_width_) || + (old_resolution_height != instance->resolution_width_) || + (old_physical_width != instance->physical_width_) || + (old_physical_height != instance->physical_height_)) { + picojson::value output = picojson::value(picojson::object());; + picojson::value data = picojson::value(picojson::object()); + + instance->SetData(data); + SetPicoJsonObjectValue(output, "cmd", + picojson::value("SystemInfoPropertyValueChanged")); + SetPicoJsonObjectValue(output, "prop", picojson::value("DISPLAY")); + SetPicoJsonObjectValue(output, "data", data); + + std::string result = output.serialize(); + instance->api_->PostMessage(result.c_str()); + } + + return TRUE; } -double SysInfoDisplay::GetBrightness(picojson::value& error) { - char* str_val = NULL; - char max_path[] = ACPI_BACKLIGHT_DIR"/max_brightness"; - char brightness_path[] = ACPI_BACKLIGHT_DIR"/brightness"; - int max_val, val; +void SysInfoDisplay::SetData(picojson::value& data) { + SetPicoJsonObjectValue(data, "brightness", picojson::value(brightness_)); - str_val = ReadOneLine(max_path); - if(NULL == str_val) { - // FIXME(halton): ACPI is not enabled, fallback to maximum. - return 1.0; - } - max_val = atoi(str_val); - free(str_val); + SetPicoJsonObjectValue(data, "resolutionWidth", + picojson::value(static_cast(resolution_width_))); + SetPicoJsonObjectValue(data, "resolutionHeight", + picojson::value(static_cast(resolution_height_))); + SetPicoJsonObjectValue(data, "physicalWidth", + picojson::value(physical_width_)); + SetPicoJsonObjectValue(data, "physicalHeight", + picojson::value(physical_height_)); - str_val = ReadOneLine(brightness_path); - if(!str_val) { - // FIXME(halton): ACPI is not enabled, fallback to maximum. - return 1.0; - } - val = atoi(str_val); - free(str_val); - - return val / max_val; + // dpi = N * 25.4 pixels / M inch + SetPicoJsonObjectValue(data, "dotsPerInchWidth", + picojson::value((resolution_width_ * 25.4) / physical_width_)); + SetPicoJsonObjectValue(data, "dotsPerInchHeight", + picojson::value((resolution_width_ * 25.4) / physical_width_)); } -- 2.7.4