From 5777b351e944b60dff592b271bce4612b02834e9 Mon Sep 17 00:00:00 2001 From: Krzysztof Sasiak Date: Fri, 9 Aug 2013 12:44:09 +0200 Subject: [PATCH] Add patch to reset configuration by NameOwnerChanged dbus signal. Process can change deviced daemon state by dbus. If the process is terminated, deviced can receive NameOwnerChanged signal. And It can reset the configurations from the process terminated. Change-Id: I1e7b12e62c981f261cace54b8381dc93e9769c3d Signed-off-by: Krzysztof Sasiak --- src/core/device-notifier.h | 1 + src/core/edbus-handler.c | 111 +++++++++++++++++++++++++++++++++++++++++++++ src/display/core.c | 19 ++++++-- src/display/display-dbus.c | 19 ++++++-- src/display/lsensor.c | 35 +++++++++++++- 5 files changed, 176 insertions(+), 9 deletions(-) diff --git a/src/core/device-notifier.h b/src/core/device-notifier.h index dd2bdce..7737ff9 100644 --- a/src/core/device-notifier.h +++ b/src/core/device-notifier.h @@ -24,6 +24,7 @@ enum device_notifier_type { DEVICE_NOTIFIER_LCD, DEVICE_NOTIFIER_INPUT_ADD, DEVICE_NOTIFIER_INPUT_REMOVE, + DEVICE_NOTIFIER_PROCESS_TERMINATED, DEVICE_NOTIFIER_MAX, }; diff --git a/src/core/edbus-handler.c b/src/core/edbus-handler.c index 1e3171f..bd8bfaf 100644 --- a/src/core/edbus-handler.c +++ b/src/core/edbus-handler.c @@ -22,8 +22,15 @@ #include "core/edbus-handler.h" #include "core/common.h" #include "core/devices.h" +#include "core/device-notifier.h" +#include "core/list.h" #define EDBUS_INIT_RETRY_COUNT 5 +#define NAME_OWNER_CHANGED "NameOwnerChanged" +#define NAME_OWNER_MATCH "type='signal',sender='org.freedesktop.DBus',\ + path='/org/freedesktop/DBus',interface='org.freedesktop.DBus',\ + member='NameOwnerChanged',arg0='%s'" + struct edbus_list{ char *signal_name; @@ -42,7 +49,9 @@ static struct edbus_object { }; static Eina_List *edbus_handler_list; +static Eina_List *edbus_watch_list; static int edbus_init_val; +static DBusConnection *conn; static E_DBus_Connection *edbus_conn; static DBusPendingCall *edbus_request_name; @@ -232,6 +241,106 @@ int broadcast_edbus_signal(const char *path, const char *interface, return 0; } +int register_edbus_watch(DBusMessage *msg) +{ + char match[256]; + const char *sender, *watch; + Eina_List *l; + + if (!msg) { + _E("invalid argument!"); + return -EINVAL; + } + + sender = dbus_message_get_sender(msg); + if (!sender) { + _E("invalid sender!"); + return -EINVAL; + } + + /* check the sender is already registered */ + EINA_LIST_FOREACH(edbus_watch_list, l, watch) { + if (strcmp(sender, watch)) continue; + + _I("%s is already watched!", watch); + return 0; + } + + watch = strndup(sender, strlen(sender)); + if (!watch) { + _E("Malloc failed"); + return -ENOMEM; + } + + /* Add sender to watch list */ + EINA_LIST_APPEND(edbus_watch_list, watch); + + snprintf(match, sizeof(match), NAME_OWNER_MATCH, watch); + dbus_bus_add_match(conn, match, NULL); + + _I("%s is watched by dbus!", watch); +} + +static DBusHandlerResult message_filter(DBusConnection *connection, + DBusMessage *message, void *data) +{ + char match[256]; + int ret; + const char *iface, *member, *watch, *arg = NULL; + Eina_List *l; + + if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_SIGNAL) + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + + iface = dbus_message_get_interface(message); + member = dbus_message_get_member(message); + + if (strcmp(iface, DBUS_INTERFACE_DBUS)) + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + + if (strcmp(member, NAME_OWNER_CHANGED)) + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + + ret = dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg, + DBUS_TYPE_INVALID); + if (!ret) { + _E("no message"); + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + } + + _D("Argument : %s", arg); + + EINA_LIST_FOREACH(edbus_watch_list, l, watch) { + if (strcmp(arg, watch)) continue; + + /* notify 'process terminated' to device notifiers */ + device_notify(DEVICE_NOTIFIER_PROCESS_TERMINATED, watch); + + /* remove registered sender */ + snprintf(match, sizeof(match), NAME_OWNER_MATCH, watch); + dbus_bus_remove_match(conn, match, NULL); + EINA_LIST_REMOVE(edbus_watch_list, watch); + free(watch); + break; + } + + return DBUS_HANDLER_RESULT_HANDLED; +} + +static unregister_edbus_watch_all(void) +{ + char match[256]; + Eina_List *n, *next; + struct edbus_list *watch; + + EINA_LIST_FOREACH_SAFE(edbus_watch_list, n, next, watch) { + snprintf(match, sizeof(match), NAME_OWNER_MATCH, watch); + dbus_bus_remove_match(conn, match, NULL); + EINA_LIST_REMOVE(edbus_watch_list, watch); + free(watch); + } +} + static void edbus_init(void *data) { int retry = EDBUS_INIT_RETRY_COUNT; @@ -291,6 +400,8 @@ err_dbus_shutdown: static void edbus_exit(void *data) { unregister_edbus_signal_handle(); + dbus_connection_remove_filter(conn, message_filter, NULL); + unregister_edbus_watch_all(); e_dbus_connection_close(edbus_conn); e_dbus_shutdown(); } diff --git a/src/display/core.c b/src/display/core.c index 4347c69..82f9738 100644 --- a/src/display/core.c +++ b/src/display/core.c @@ -1550,6 +1550,16 @@ static int input_device_remove(void *data) return 0; } +static int process_terminated(void *data) +{ + const char *name = data; + + _I("%s is terminated!", name); + reset_autobrightness_min(name); + + return 0; +} + /** * Power manager Main * @@ -1571,6 +1581,7 @@ static void display_init(void *data) register_notifier(DEVICE_NOTIFIER_INPUT_ADD, input_device_add); register_notifier(DEVICE_NOTIFIER_INPUT_REMOVE, input_device_remove); + register_notifier(DEVICE_NOTIFIER_PROCESS_TERMINATED, process_terminated); for (i = INIT_SETTING; i < INIT_END; i++) { switch (i) { @@ -1639,9 +1650,11 @@ static void display_exit(void *data) break; case INIT_POLL: unregister_notifier(DEVICE_NOTIFIER_INPUT_ADD, - input_device_add); - unregister_notifier(DEVICE_NOTIFIER_INPUT_REMOVE, - input_device_remove); + input_device_add); + unregister_notifier(DEVICE_NOTIFIER_INPUT_REMOVE, + input_device_remove); + unregister_notifier(DEVICE_NOTIFIER_PROCESS_TERMINATED, + process_terminated); unset_noti(noti_fd); exit_pm_poll(); break; diff --git a/src/display/display-dbus.c b/src/display/display-dbus.c index e25c154..06f1a29 100644 --- a/src/display/display-dbus.c +++ b/src/display/display-dbus.c @@ -344,17 +344,26 @@ static DBusMessage *e_dbus_setautobrightnessmin(E_DBus_Object *obj, DBusMessage DBusMessage *reply; int val, ret; pid_t pid; + char *sender; + sender = dbus_message_get_sender(msg); + if (!sender) { + _E("invalid sender name!"); + ret = -EINVAL; + goto error; + } dbus_message_iter_init(msg, &iter); dbus_message_iter_get_basic(&iter, &val); pid = get_edbus_sender_pid(msg); - ret = set_autobrightness_min(val); - if (ret) - _E("fail to set autobrightness min %d, %d by %d", val, ret, pid); - else + ret = set_autobrightness_min(val, sender); + if (ret) { + _I("fail to set autobrightness min %d, %d by %d", val, ret, pid); + } else { + register_edbus_watch(msg); _I("set autobrightness min %d by %d", val, pid); - + } +error: reply = dbus_message_new_method_return(msg); dbus_message_iter_init_append(reply, &iter); dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret); diff --git a/src/display/lsensor.c b/src/display/lsensor.c index 38fc13f..bc2a981 100644 --- a/src/display/lsensor.c +++ b/src/display/lsensor.c @@ -44,6 +44,7 @@ static int fault_count = 0; static int power_saving_display_stat = 0; static int sampling_interval = SAMPLING_INTERVAL; static int min_brightness = PM_MIN_BRIGHTNESS; +static char *min_brightness_name = 0; static bool alc_handler(void* data) { @@ -290,16 +291,48 @@ int set_autobrightness_interval(int val) return 0; } -int set_autobrightness_min(int val) +int set_autobrightness_min(int val, char *name) { + if (!name) + return -EINVAL; + if (val < PM_MIN_BRIGHTNESS || val > PM_MAX_BRIGHTNESS) return -EINVAL; min_brightness = val; + if (min_brightness_name) { + free(min_brightness_name); + min_brightness_name = 0; + } + min_brightness_name = strndup(name, strlen(name)); + + _I("auto brightness min value changed! (%d, %s)", + min_brightness, min_brightness_name); + return 0; } +void reset_autobrightness_min(char *name) +{ + if (!name) + return; + + if (!min_brightness_name) + return; + + if (strcmp(name, min_brightness_name)) + return; + + _I("change to default %d -> %d, %s", min_brightness, + PM_MIN_BRIGHTNESS, min_brightness_name); + min_brightness = PM_MIN_BRIGHTNESS; + if (min_brightness_name) { + free(min_brightness_name); + min_brightness_name = 0; + } +} + static void __attribute__ ((constructor)) pm_lsensor_init(void) { _default_action = NULL; -- 2.7.4