display: plugin: Add display_plugin_get_device_flags()
[platform/core/system/deviced.git] / plugins / wearable / display / core.c
index f0e71dd..4365feb 100644 (file)
@@ -71,6 +71,7 @@
 #include "display-misc.h"
 #include "display-panel.h"
 #include "display-config.h"
+#include "display-state-transition.h"
 #include "shared/plugin.h"
 
 #define POWERLOCK_CONF_FILE  "/etc/deviced/powerlock.conf"
@@ -111,18 +112,14 @@ static int (*fp_get_charging_status) (int *val);
 static void (*power_saving_func) (int onoff);
 static enum device_ops_status status = DEVICE_OPS_STATUS_UNINIT;
 
-static guint timeout_src_id;
 static int system_wakeup_flag = false;
 static unsigned int custom_normal_timeout = 0;
 static unsigned int custom_dim_timeout = 0;
-int custom_holdkey_block = false;
 static char *custom_change_name;
 static guint lock_timeout_id;
 static guint transit_timer;
 static int lock_screen_timeout = LOCK_SCREEN_INPUT_TIMEOUT;
 static struct timeval lcdon_tv;
-static int lcd_paneloff_mode = false;
-static int stay_touchscreen_off = false;
 /*
  * The two variables(lcdon_broadcast, pmstate_suspend) must be set initial
  * state because it should be sent from previous state at booting time.
@@ -153,17 +150,6 @@ static struct state states[S_END] = {
        { S_POWEROFF, "S_POWEROFF", NULL,          NULL,           NULL,          NULL            },
 };
 
-static int trans_table[S_END][EVENT_END] = {
-       /* Timeout,   Input */
-       { S_START,    S_START    }, /* S_START */
-       { S_LCDDIM,   S_NORMAL   }, /* S_NORMAL */
-       { S_LCDOFF,   S_NORMAL   }, /* S_LCDDIM */
-       { S_SLEEP,    S_NORMAL   }, /* S_LCDOFF */
-       { S_SLEEP,    S_STANDBY  }, /* S_STANDBY */
-       { S_LCDOFF,   S_NORMAL   }, /* S_SLEEP */
-       { S_POWEROFF, S_POWEROFF }, /* S_POWEROFF */
-};
-
 #define SHIFT_UNLOCK           4
 #define SHIFT_CHANGE_STATE     7
 #define CHANGE_STATE_BIT       0xF00   /* 1111 0000 0000 */
@@ -248,11 +234,6 @@ inline struct state *state_st(enum state_t state)
        return &states[state];
 }
 
-guint get_transition_timer(void)
-{
-       return timeout_src_id;
-}
-
 static int display_auto_brightness_sensing(void *data)
 {
        if (!transit_timer)
@@ -406,7 +387,7 @@ static gboolean pmlock_terminate_daemon_to_release_lock(gpointer data)
                del_node(state, node);
                set_unlock_time(pid, state);
 
-               if (!timeout_src_id)
+               if (!display_state_transition_is_there_state_transition_timer())
                        states[get_pm_cur_state()].trans(EVENT_TIMEOUT);
        }
 
@@ -465,19 +446,31 @@ static void free_killable_daemon_list(void)
        initialized_killable_daemon_list = false;
 }
 
-static unsigned long get_lcd_on_flags(void)
+static int get_device_flags(unsigned long *device_flags)
 {
-       unsigned long flags = NORMAL_MODE;
+       if (!device_flags)
+               return -EINVAL;
 
-       if (lcd_paneloff_mode)
-               flags |= LCD_PANEL_OFF_MODE;
+       *device_flags = NORMAL_MODE;
+       bool lcd_paneloff_mode = false;
+       bool stay_touchscreen_off = false;
+       display_panel_get_lcd_paneloff_mode(&lcd_paneloff_mode);
+       display_misc_get_stay_touchscreen_off(&stay_touchscreen_off);
+
+       if (lcd_paneloff_mode) {
+               *device_flags |= LCD_PANEL_OFF_MODE;
+       }
+
+       if (stay_touchscreen_off) {
+               *device_flags |= TOUCH_SCREEN_OFF_MODE;
+       }
 
        if (ambient_get_state()) {
-               flags |= AMBIENT_MODE;
-               flags |= LCD_PHASED_TRANSIT_MODE;
+               *device_flags |= AMBIENT_MODE;
+               *device_flags |= LCD_PHASED_TRANSIT_MODE;
        }
 
-       return flags;
+       return 0;
 }
 
 bool touch_event_blocked(void)
@@ -499,7 +492,8 @@ static gboolean late_transit_on(void *data)
 
 void lcd_on_procedure(int state, enum device_flags flag)
 {
-       unsigned long flags = get_lcd_on_flags();
+       unsigned long flags = NORMAL_MODE;
+       display_plugin_get_device_flags(&flags);
        flags |= flag;
        int ret;
 
@@ -562,24 +556,10 @@ void lcd_on_procedure(int state, enum device_flags flag)
        touch_blocked = false;
 }
 
-static unsigned long get_lcd_off_flags(void)
-{
-       unsigned long flags = NORMAL_MODE;
-
-       if (ambient_get_condition() == true) {
-               flags |= AMBIENT_MODE;
-               flags |= LCD_PHASED_TRANSIT_MODE;
-       }
-
-       if (stay_touchscreen_off)
-               flags |= TOUCH_SCREEN_OFF_MODE;
-
-       return flags;
-}
-
 inline void lcd_off_procedure(enum device_flags flag)
 {
-       unsigned long flags = get_lcd_off_flags();
+       unsigned long flags = NORMAL_MODE;
+       display_plugin_get_device_flags(&flags);
        flags |= flag;
 
        /*
@@ -636,50 +616,6 @@ inline void lcd_off_procedure(enum device_flags flag)
        enter_doze();
 }
 
-void set_stay_touchscreen_off(int val)
-{
-       _I("Stay touch screen off: %d", val);
-       stay_touchscreen_off = val;
-
-       if (disp_plgn->pm_change_internal)
-               disp_plgn->pm_change_internal(INTERNAL_LOCK_PM, LCD_NORMAL);
-}
-
-void set_lcd_paneloff_mode(int val)
-{
-       _I("Lcd paneloff mode: %d", val);
-       lcd_paneloff_mode = val;
-
-       if (disp_plgn->pm_change_internal)
-               disp_plgn->pm_change_internal(INTERNAL_LOCK_PM, LCD_NORMAL);
-}
-
-void get_pname(pid_t pid, char *pname)
-{
-       char buf[PATH_MAX];
-       int cmdline, r;
-
-       if (pid >= INTERNAL_LOCK_BASE)
-               snprintf(buf, PATH_MAX, "/proc/%d/cmdline", getpid());
-       else
-               snprintf(buf, PATH_MAX, "/proc/%d/cmdline", pid);
-
-       cmdline = open(buf, O_RDONLY);
-       if (cmdline < 0) {
-               pname[0] = '\0';
-               _E("Process(%d) does not exist now(may be dead without unlock).", pid);
-               return;
-       }
-
-       r = read(cmdline, pname, PATH_MAX);
-       if ((r >= 0) && (r < PATH_MAX))
-               pname[r] = '\0';
-       else
-               pname[0] = '\0';
-
-       close(cmdline);
-}
-
 static void del_state_cond(void *data, enum state_t state)
 {
        PmLockNode *tmp = NULL;
@@ -703,7 +639,7 @@ static void del_state_cond(void *data, enum state_t state)
         *   1. There should be no running state-transition timer
         *   2. Released lock is one of the pm_cur_state's lock
         * This emulates already expired transition timer */
-       if (!timeout_src_id && get_pm_cur_state() == state)
+       if (!display_state_transition_is_there_state_transition_timer() && (get_pm_cur_state() == state))
                states[get_pm_cur_state()].trans(EVENT_TIMEOUT);
 
        if (state == S_LCDOFF)
@@ -728,45 +664,6 @@ static gboolean del_off_cond(void *data)
        return G_SOURCE_REMOVE;
 }
 
-/* timeout handler  */
-gboolean timeout_handler(void *data)
-{
-       _I("Time out state %s", states[get_pm_cur_state()].name);
-
-       if (timeout_src_id) {
-               g_source_remove(timeout_src_id);
-               timeout_src_id = 0;
-       }
-
-       states[get_pm_cur_state()].trans(EVENT_TIMEOUT);
-       return G_SOURCE_REMOVE;
-}
-
-void reset_timeout(int timeout)
-{
-       if (!display_conf.timeout_enable)
-               return;
-
-       if ((get_pm_cur_state() == S_LCDOFF)
-       && (is_emulator() == true || timeout_sleep_support == false))
-               return;
-
-       _I("Reset timeout(%d ms).", timeout);
-       if (timeout_src_id != 0) {
-               g_source_remove(timeout_src_id);
-               timeout_src_id = 0;
-       }
-
-       if (trans_table[get_pm_cur_state()][EVENT_TIMEOUT] == get_pm_cur_state())
-               return;
-
-       if (timeout > 0)
-               timeout_src_id = g_timeout_add(timeout,
-                   timeout_handler, NULL);
-       else if (timeout == 0)
-               states[get_pm_cur_state()].trans(EVENT_TIMEOUT);
-}
-
 /* get configurations from setting */
 static int get_lcd_timeout_from_settings(void)
 {
@@ -1168,7 +1065,7 @@ static void proc_condition_lock(PMMsg *data)
                return;
 
        flags = GET_COND_FLAG(data->cond);
-       get_pname(pid, pname);
+       display_misc_get_process_name(pid, pname);
 
        if ((state == S_LCDOFF) && (get_pm_cur_state() == S_SLEEP) &&
            (pm_get_power_lock() == POWER_UNLOCK))
@@ -1249,7 +1146,7 @@ static void proc_condition_unlock(PMMsg *data)
                return;
 
        flags = GET_COND_FLAG(data->cond);
-       get_pname(pid, pname);
+       display_misc_get_process_name(pid, pname);
 
        tmp = find_node(state, pid);
        del_node(state, tmp);
@@ -1303,13 +1200,13 @@ static int proc_condition(PMMsg *data)
        if (flags == 0) {
                /* guard time for suspend */
                if (get_pm_cur_state() == S_LCDOFF)
-                       reset_timeout(states[S_LCDOFF].timeout);
+                       display_state_transition_reset_state_transition_timeout(states[S_LCDOFF].timeout);
        } else {
                if (flags & PM_FLAG_RESET_TIMER)
-                       reset_timeout(states[get_pm_cur_state()].timeout);
+                       display_state_transition_reset_state_transition_timeout(states[get_pm_cur_state()].timeout);
        }
 
-       if (!timeout_src_id)
+       if (!display_state_transition_is_there_state_transition_timer())
                states[get_pm_cur_state()].trans(EVENT_TIMEOUT);
 
        return 0;
@@ -1335,118 +1232,6 @@ void update_lcdoff_source(int source)
                _E("Failed to set vconf value for lcd off source: %d", vconf_get_ext_errno());
 }
 
-void print_info(int fd)
-{
-       int s_index = 0;
-       char buf[PATH_MAX + 255];
-       int i = 1;
-       int ret;
-       char pname[PATH_MAX];
-       PmLockNode *t;
-       GList *elem;
-       char time_buf[30];
-
-       if (fd < 0)
-               return;
-
-       snprintf(buf, sizeof(buf),
-               "\n==========================================="
-               "===========================\n");
-       ret = write(fd, buf, strlen(buf));
-       if (ret < 0)
-               _E("Write() failed: %d", errno);
-       snprintf(buf, sizeof(buf), "Timeout Info: Run[%dms] Dim[%dms] Off[%dms]\n",
-                states[S_NORMAL].timeout,
-                states[S_LCDDIM].timeout, states[S_LCDOFF].timeout);
-       ret = write(fd, buf, strlen(buf));
-       if (ret < 0)
-               _E("Write() failed: %d", errno);
-
-       snprintf(buf, sizeof(buf), "Tran. Locked : %s %s %s\n",
-                (get_trans_condition() & MASK_NORMAL) ? states[S_NORMAL].name : "-",
-                (get_trans_condition() & MASK_DIM) ? states[S_LCDDIM].name : "-",
-                (get_trans_condition() & MASK_OFF) ? states[S_LCDOFF].name : "-");
-       ret = write(fd, buf, strlen(buf));
-       if (ret < 0)
-               _E("Write() failed: %d", errno);
-
-       snprintf(buf, sizeof(buf), "Current State: %s\n",
-               states[get_pm_cur_state()].name);
-       ret = write(fd, buf, strlen(buf));
-       if (ret < 0)
-               _E("Write() failed: %d", errno);
-
-       snprintf(buf, sizeof(buf), "Current Lock Conditions: \n");
-       ret = write(fd, buf, strlen(buf));
-       if (ret < 0)
-               _E("Write() failed: %d", errno);
-
-       for (s_index = S_NORMAL; s_index < S_END; s_index++) {
-               SYS_G_LIST_FOREACH(get_cond_head(s_index), elem, t) {
-                       get_pname((pid_t)t->pid, pname);
-                       ctime_r(&t->time, time_buf);
-                       time_buf[strlen(time_buf) - 1] = 0;
-                       snprintf(buf, sizeof(buf),
-                                " %d: [%s] locked by pid %d %s %s\n",
-                                i++, states[s_index].name, t->pid, pname, time_buf);
-                       ret = write(fd, buf, strlen(buf));
-                       if (ret < 0)
-                               _E("Write() failed: %d", errno);
-               }
-       }
-
-       print_lock_info_list(fd);
-
-#ifdef ENABLE_PM_LOG
-       pm_history_print(fd, 250);
-#endif
-}
-
-void save_display_log(const char *path)
-{
-       int fd, ret;
-       char buf[255];
-       time_t now_time;
-       char time_buf[30];
-
-       _D("internal state is saved!");
-
-       time(&now_time);
-       ctime_r(&now_time, time_buf);
-       time_buf[strlen(time_buf) - 1] = 0;
-
-       fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0644);
-       if (fd != -1) {
-               snprintf(buf, sizeof(buf),
-                       "\npm_state_log now-time : %d(s) %s\n\n",
-                       (int)now_time, time_buf);
-               ret = write(fd, buf, strlen(buf));
-               if (ret < 0)
-                       _E("write() failed (%d)", errno);
-
-               snprintf(buf, sizeof(buf), "pm_status_flag: %#x\n", get_pm_status_flag());
-               ret = write(fd, buf, strlen(buf));
-               if (ret < 0)
-                       _E("write() failed (%d)", errno);
-
-               if (disp_plgn->get_lock_screen_state ) {
-                       snprintf(buf, sizeof(buf), "screen lock status : %d\n",
-                               disp_plgn->get_lock_screen_state());
-                       ret = write(fd, buf, strlen(buf));
-                       if (ret < 0)
-                               _E("write() failed (%d)", errno);
-               }
-               print_info(fd);
-               close(fd);
-       }
-
-       fd = open("/dev/console", O_WRONLY);
-       if (fd != -1) {
-               print_info(fd);
-               close(fd);
-       }
-}
-
 /* SIGHUP signal handler
  * For debug... print info to syslog
  */
@@ -1499,9 +1284,10 @@ int check_lcdoff_direct(void)
 static int default_trans(int evt)
 {
        struct state *st = &states[get_pm_cur_state()];
-       int next_state, ret;
+       int ret;
+       enum state_t next_state;
 
-       next_state = (enum state_t)trans_table[get_pm_cur_state()][evt];
+       display_state_transition_get_next_transition_display_state(get_pm_cur_state(), &next_state, evt);
 
        /* check conditions */
        if (st->check && !st->check(get_pm_cur_state(), next_state)) {
@@ -1605,6 +1391,7 @@ static int default_action(int timeout)
        struct timeval now_tv;
        bool custom_status;
        int brightness;
+       bool lcd_paneloff_mode = false;
 
        if (status != DEVICE_OPS_STATUS_START) {
                _E("Display is not started.");
@@ -1627,7 +1414,7 @@ static int default_action(int timeout)
                        lcdon_tv.tv_sec = 0;
                }
                /* set timer with current state timeout */
-               reset_timeout(timeout);
+               display_state_transition_reset_state_transition_timeout(timeout);
 
                if (get_pm_cur_state() == S_NORMAL) {
                        time(&last_update_time);
@@ -1692,6 +1479,7 @@ static int default_action(int timeout)
                                lcd_off_procedure(LCD_OFF_BY_TIMEOUT);
                }
 
+               display_panel_get_lcd_paneloff_mode(&lcd_paneloff_mode);
                if (display_panel_get_dpms_cached_state() == DPMS_ON
                    || lcd_paneloff_mode)
                        lcd_off_procedure(LCD_OFF_BY_TIMEOUT);
@@ -2024,10 +1812,10 @@ int set_lcd_timeout(int on, int dim, int holdkey_block, const char *name)
                states[get_pm_cur_state()].trans(EVENT_INPUT);
 
        if (holdkey_block) {
-               custom_holdkey_block = true;
+               display_lock_set_custom_holdkey_block(true);
                _I("Hold key disabled.");
        } else {
-               custom_holdkey_block = false;
+               display_lock_set_custom_holdkey_block(false);
                _I("Hold key enabled.");
        }
 
@@ -2045,7 +1833,7 @@ int set_lcd_timeout(int on, int dim, int holdkey_block, const char *name)
        if (!custom_change_name) {
                _E("Failed to malloc.");
                custom_normal_timeout = custom_dim_timeout = 0;
-               custom_holdkey_block = false;
+               display_lock_set_custom_holdkey_block(false);
                return -ENOMEM;
        }
 
@@ -2071,7 +1859,7 @@ void reset_lcd_timeout(GDBusConnection *conn,
        free(custom_change_name);
        custom_change_name = 0;
        custom_normal_timeout = custom_dim_timeout = 0;
-       custom_holdkey_block = false;
+       display_lock_set_custom_holdkey_block(false);
 
        update_display_time();
        if (get_pm_cur_state() == S_NORMAL)
@@ -2098,27 +1886,6 @@ static int delayed_init_done(void *data)
        return done;
 }
 
-static int battery_health_changed(void *data)
-{
-       int health = DATA_VALUE_INT(data);
-
-       _I("battery health change %d", health);
-
-       if (health == HEALTH_GOOD) {
-               clear_pm_status_flag(BATTERY_FLAG);
-               clear_pm_status_flag(DIMSTAY_FLAG);
-               if (display_panel_get_dpms_cached_state() == DPMS_ON)
-                       auto_brightness_restore();
-       } else if (health == HEALTH_LOW || health == HEALTH_HIGH || health == HEALTH_OVP) {
-               set_pm_status_flag(BATTERY_FLAG);
-               set_pm_status_flag(DIMSTAY_FLAG);
-               if (display_panel_get_dpms_cached_state() == DPMS_ON)
-                       display_backlight_update_by_default_brightness();
-       }
-
-       return 0;
-}
-
 static int powerlock_load_config(struct parse_result *result, void *user_data)
 {
        char *name = NULL;
@@ -2155,13 +1922,13 @@ static gboolean delayed_dpms_init_done(gpointer data)
                        /* check minimun lcd on time */
                        if (timeout < SEC_TO_MSEC(DEFAULT_NORMAL_TIMEOUT))
                                timeout = SEC_TO_MSEC(DEFAULT_NORMAL_TIMEOUT);
-                       reset_timeout(timeout);
+                       display_state_transition_reset_state_transition_timeout(timeout);
                }
                break;
        case S_LCDOFF:
                lcd_off_procedure(LCD_OFF_BY_EVENT);
                timeout = display_conf.lcdoff_timeout;
-               reset_timeout(timeout);
+               display_state_transition_reset_state_transition_timeout(timeout);
                break;
        default:
                break;
@@ -2177,6 +1944,12 @@ static void add_timer_for_dpms_init(void)
                _E("Failed to add display_panel_init_dpms timeout.");
 }
 
+static void init_display_states(void *data)
+{
+       struct display_plugin *dp = (struct display_plugin *) data;
+       for(int i = 0; i < S_END; i++)
+               dp->display_states[i] = &states[i];
+}
 /**
  * Power manager Main
  *
@@ -2188,6 +1961,11 @@ static int display_probe(void *data)
        assert(dp);
 
        dp->config = &display_conf;
+       init_display_states(dp);
+       setup_display_plugin_backlight_ops(dp);
+       dp->delete_state_cond = del_state_cond;
+       dp->set_dim_state = set_dim_state;
+       dp->get_device_flags = get_device_flags;
        setup_display_plugin_backlight_ops(dp);
 
        /**
@@ -2351,7 +2129,6 @@ static void display_init(void *data)
        register_notifier(DEVICE_NOTIFIER_APPLICATION_BACKGROUND, display_app_background);
        register_notifier(DEVICE_NOTIFIER_APPLICATION_FOREGROUND, display_app_foreground);
        register_notifier(DEVICE_NOTIFIER_APPLICATION_TERMINATED, display_app_terminated);
-       register_notifier(DEVICE_NOTIFIER_BATTERY_HEALTH, battery_health_changed);
        register_notifier(DEVICE_NOTIFIER_LCD_AUTOBRT_SENSING, display_auto_brightness_sensing);
        register_notifier(DEVICE_NOTIFIER_POWER_RESUME_FROM_ECHO_MEM, power_resume_from_echomem_callback);
        register_notifier(DEVICE_NOTIFIER_POWEROFF_TRIGGERED, poweroff_triggered_callback);
@@ -2415,7 +2192,7 @@ static void display_init(void *data)
 
                if (display_conf.lcd_always_on) {
                        _I("LCD always on.");
-                       trans_table[S_NORMAL][EVENT_TIMEOUT] = S_NORMAL;
+                       display_state_transition_set_transition_table_display_state(S_NORMAL, S_NORMAL, EVENT_TIMEOUT);
                }
 
                if (flags & WITHOUT_STARTNOTI) {        /* start without noti */
@@ -2472,7 +2249,7 @@ static void display_exit(void *data)
        set_pm_cur_state(S_NORMAL);
        set_setting_pmstate(get_pm_cur_state());
        /* timeout is not needed */
-       reset_timeout(TIMEOUT_NONE);
+       display_state_transition_reset_state_transition_timeout(TIMEOUT_NONE);
 
        if (CHECK_OPS(keyfilter_ops, exit))
                keyfilter_ops->exit();
@@ -2495,7 +2272,6 @@ static void display_exit(void *data)
                        unregister_notifier(DEVICE_NOTIFIER_APPLICATION_BACKGROUND, display_app_background);
                        unregister_notifier(DEVICE_NOTIFIER_APPLICATION_FOREGROUND, display_app_foreground);
                        unregister_notifier(DEVICE_NOTIFIER_APPLICATION_TERMINATED, display_app_terminated);
-                       unregister_notifier(DEVICE_NOTIFIER_BATTERY_HEALTH, battery_health_changed);
                        unregister_notifier(DEVICE_NOTIFIER_POWER_RESUME_FROM_ECHO_MEM, power_resume_from_echomem_callback);
 
                        break;