display: plugin: Add display_plugin_get_device_flags()
[platform/core/system/deviced.git] / plugins / wearable / display / core.c
index 38949a5..4365feb 100644 (file)
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <dlfcn.h>
 #include <fnmatch.h>
 #include <vconf-keys.h>
 #include <sys/time.h>
+#include <libsyscommon/list.h>
+#include <hal/device/hal-device-power.h>
 
+#include "auto-brightness-sensorhub.h"
 #include "ambient-mode.h"
 #include "util.h"
 #include "core.h"
-#include "device-node.h"
+#include "poll.h"
 #include "lock-detector.h"
 #include "display-ops.h"
-#include "core/devices.h"
-#include "core/device-notifier.h"
+#include "shared/devices.h"
+#include "shared/device-notifier.h"
 #include "core/udev.h"
-#include "core/list.h"
-#include "core/common.h"
-#include "core/config-parser.h"
-#include "core/launch.h"
-#include "apps/apps.h"
+#include "shared/common.h"
+#include "shared/apps.h"
 #include "extcon/extcon.h"
 #include "battery/power-supply.h"
-#include "power/power-handler.h"
-#include "power/boot.h"
-#include "dd-display.h"
-#include "display/display-dpms.h"
+#include "power/power.h"
+#include "power/power-off.h"
+#include "power/power-suspend.h"
+#include "power/power-boot.h"
+#include "power/power-doze.h"
+#include "device-interface.h"
+#include "display-plugin.h"
 #include "display-info.h"
 #include "battery-monitor.h"
-#include "display/display-signal.h"
+#include "display-signal.h"
 #include "display-lock.h"
+#include "display-backlight.h"
+#include "display-misc.h"
+#include "display-panel.h"
+#include "display-config.h"
+#include "display-state-transition.h"
+#include "shared/plugin.h"
 
-#define DISPLAY_CONF_FILE    "/etc/deviced/display.conf"
+#define POWERLOCK_CONF_FILE  "/etc/deviced/powerlock.conf"
 
 #ifndef VCONFKEY_HOMESCREEN_TUTORIAL_OOBE_ENABLED
 #define VCONFKEY_HOMESCREEN_TUTORIAL_OOBE_ENABLED      "db/private/com.samsung.w-home/tutorial_oobe_enabled"
 #define DISPLAY_DETACH_STR  "display_detach"
 #define UNKNOWN_STR         "unknown"
 
-extern void init_pm_internal();
-extern int get_charging_status(int *val);
 extern void init_save_userlock(void);
 
-unsigned int pm_status_flag;
+static struct display_plugin *disp_plgn;
+static struct display_backlight_ops *backlight_ops;
+static struct battery_plugin *battery_plgn;
+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;
 
-int pm_cur_state;
-int pm_old_state;
-guint timeout_src_id;
-int system_wakeup_flag = false;
+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 bool hallic_open = true;
 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;
-dd_list *lcdon_ops;
 /*
  * The two variables(lcdon_broadcast, pmstate_suspend) must be set initial
  * state because it should be sent from previous state at booting time.
@@ -137,7 +140,7 @@ static gboolean del_off_cond(void *data);
 static int default_proc_change_state(unsigned int cond, pid_t pid);
 static int (*proc_change_state)(unsigned int cond, pid_t pid) = default_proc_change_state;
 
-struct state states[S_END] = {
+static struct state states[S_END] = {
        { S_START,    "S_START",    NULL,          NULL,           NULL,          NULL            },
        { S_NORMAL,   "S_NORMAL",   default_trans, default_action, default_check, del_normal_cond },
        { S_LCDDIM,   "S_LCDDIM",   default_trans, default_action, default_check, del_dim_cond    },
@@ -147,17 +150,6 @@ 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 */
@@ -170,7 +162,7 @@ static int trans_table[S_END][EVENT_END] = {
 
 #define S_COVER_TIMEOUT                        8000
 #define GET_HOLDKEY_BLOCK_STATE(x) ((x >> SHIFT_LOCK_FLAG) & HOLD_KEY_BLOCK_BIT)
-#define BOOTING_DONE_WATING_TIME       60000   /* 1 minute */
+#define DELAYED_INIT_WATING_TIME       60000   /* 1 minute */
 
 #define LOCK_SCREEN_WATING_TIME                300     /* 0.3 second */
 #define LONG_PRESS_INTERVAL             500       /* 0.5 seconds */
@@ -186,7 +178,20 @@ static int trans_table[S_END][EVENT_END] = {
        (b.tv_sec * 1000000 + b.tv_usec)) \
        / 1000)
 
-struct display_config display_conf = {
+#define KILLABLE_DAEMON_LOCK_LIMIT  1800  /* seconds, 30min */
+#define FORCE_RELEASE_LOCK_INTERVAL 5     /* seconds */
+
+static void get_comm(pid_t pid, char *comm);
+static bool is_killable_daemon(pid_t pid);
+static gboolean pmlock_terminate_daemon_to_release_lock(gpointer data);
+static int pmlock_check(void *data);
+static int powerlock_load_config(struct parse_result *result, void *user_data);
+static void free_killable_daemon_list(void);
+
+static GList *display_lock_killable_daemon;
+static bool initialized_killable_daemon_list;
+
+static struct display_config display_conf = {
        .lock_wait_time         = LOCK_SCREEN_WATING_TIME,
        .longpress_interval     = LONG_PRESS_INTERVAL,
        .lightsensor_interval   = SAMPLING_INTERVAL,
@@ -203,10 +208,13 @@ struct display_config display_conf = {
        .timeout_enable         = true,
        .input_support          = true,
        .lockcheck_timeout      = 600,
+       .display_init_direction = DISPLAY_INIT_DIRECTION_HORIZONTAL,
+       .pmlock_check = pmlock_check,
        .aod_enter_level        = 40,
        .aod_tsp                = true,
        .touch_wakeup           = false,
-       .sleep_support          = true,
+       .display_on_usb_conn_changed = true,
+       .display_dpms_type      = DISPLAY_DPMS_TYPE_WINDOW_MANAGER,
 };
 
 struct display_function_info display_info = {
@@ -216,57 +224,14 @@ struct display_function_info display_info = {
        .face_detection                 = NULL,
 };
 
-void change_state_action(enum state_t state, int (*func)(int timeout))
-{
-       _I("[%s] 'action' is changed.", states[state].name);
-       states[state].action = func;
-}
-
-void change_state_trans(enum state_t state, int (*func)(int evt))
-{
-       _I("[%s] 'trans' is changed.", states[state].name);
-       states[state].trans = func;
-}
-
-void change_state_check(enum state_t state, int (*func)(int curr, int next))
-{
-       _I("[%s] 'check' is changed.", states[state].name);
-       states[state].check = func;
-}
-
-void change_state_name(enum state_t state, const char *name)
-{
-       _I("[%s] 'name' is changed.", states[state].name);
-       states[state].name = name;
-}
-
-void change_trans_table(enum state_t state, enum state_t next)
+inline const struct display_config *get_var_display_config()
 {
-       _I("[%s] 'timeout trans table' is changed.", states[state].name);
-       trans_table[state][EVENT_TIMEOUT] = next;
+       return &display_conf;
 }
 
-void change_proc_change_state(int (*func)(unsigned int cond, pid_t pid))
+inline struct state *state_st(enum state_t state)
 {
-       _I("'proc change state' is changed.");
-       proc_change_state = func;
-}
-
-static int display_brightness_changed(void *data)
-{
-       int brt, ret;
-
-       brt = DATA_VALUE_INT(data);
-
-       ret = dbus_handle_emit_dbus_signal(NULL,
-                                                       DEVICED_PATH_DISPLAY,
-                                                       DEVICED_INTERFACE_DISPLAY,
-                                                       "Brightness",
-                                                       g_variant_new("(i)", brt));
-       if (ret < 0)
-               _E("Failed to send dbus signal Brightness.");
-
-       return 0;
+       return &states[state];
 }
 
 static int display_auto_brightness_sensing(void *data)
@@ -306,19 +271,206 @@ static const char* __device_flags_to_string(enum device_flags flags)
                return UNKNOWN_STR;
 }
 
-static unsigned long get_lcd_on_flags(void)
+static void get_comm(pid_t pid, char *comm)
 {
-       unsigned long flags = NORMAL_MODE;
+       char buf[PATH_MAX];
+       int fd, r;
+
+       if (pid >= INTERNAL_LOCK_BASE)
+               snprintf(buf, PATH_MAX, "/proc/%d/comm", getpid());
+       else
+               snprintf(buf, PATH_MAX, "/proc/%d/comm", pid);
+
+       fd = open(buf, O_RDONLY);
+       if (fd < 0) {
+               comm[0] = '\0';
+               _E("Process(%d) does not exist now(may be dead without unlock).", pid);
+               return;
+       }
+
+       r = read(fd, comm, PATH_MAX);
+       if ((r > 0) && (r < PATH_MAX)) {
+               if (comm[r - 1] == '\n')
+                       comm[r - 1] = '\0';
+               else
+                       comm[r] = '\0';
+       } else
+               comm[0] = '\0';
+
+       close(fd);
+}
+
+static bool is_killable_daemon(pid_t pid)
+{
+       char pname[PATH_MAX] = {0, };
+       const char *blacklist;
+       GList *l;
+
+       get_comm(pid, pname);
+       if (!(*pname))
+               return false;
+
+       SYS_G_LIST_FOREACH(display_lock_killable_daemon, l, blacklist) {
+               if (MATCH(pname, blacklist))
+                       return true;
+       }
+
+       return false;
+}
+
+static gboolean pmlock_terminate_daemon_to_release_lock(gpointer data)
+{
+       pid_t pid;
+       PmLockNode *node;
+       enum state_t state;
+       int ret;
+       bool pid_exist;
+       char pname[PATH_MAX] = {0, };
+
+       if (!data) {
+               _E("Invalid parameter.");
+               return G_SOURCE_REMOVE;
+       }
+
+       node = (PmLockNode *) data;
+       state = node->state;
+       pid = node->pid;
+
+       if (pid <= 0) {
+               _E("Invalid lock pid.");
+               del_node(state, node);
+               return G_SOURCE_REMOVE;
+       }
+
+       if (!node->killable_daemon) {
+               _E("Incorrect checker, this is not a killable daemon. Stop checking lock.");
+               return G_SOURCE_REMOVE;
+       }
+
+       pid_exist = (kill(pid, 0) == 0);
+       if (pid_exist)
+               get_comm(pid, pname);
+
+       if (node->force_release == false) {
+               /* Stop checking lock if process had been terminated */
+               if (!pid_exist) {
+                       del_node(state, node);
+                       _I("Process %d not found. Stop checking lock.", pid);
+                       return G_SOURCE_REMOVE;
+               }
+
+               /* KILLABLE_DAEMON_LOCK_LIMIT is expired. Kill the daemon */
+               CRITICAL_LOG("%s(%d) holds %s lock for %ds. kill SIGTERM.",
+                       *pname ? pname : "Unknown", pid, states[state].name, KILLABLE_DAEMON_LOCK_LIMIT);
+               ret = kill(pid, SIGTERM);
+               if (ret < 0)
+                       CRITICAL_LOG_E("Failed to send SIGTERM to process %s(%d), %d.",
+                               *pname ? pname : "Unknown", pid, errno);
+
+               node->force_release = true;
+               node->warning_id = g_timeout_add_seconds(FORCE_RELEASE_LOCK_INTERVAL,
+                               pmlock_terminate_daemon_to_release_lock, (gpointer)node);
+       } else if (node->force_release == true) {
+               /* kill confirmation */
+               if (pid_exist) {
+                       CRITICAL_LOG("%s(%d) is still alive, kill SIGKILL.",
+                               *pname ? pname : "Unknown", pid);
+
+                       ret = kill(pid, SIGKILL);
+                       if (ret < 0)
+                               CRITICAL_LOG_E("Failed to kill process %s(%d), %d.",
+                                       *pname ? pname : "Unknown", pid, errno);
+               }
+
+               /* release lock */
+               CRITICAL_LOG("Release %s lock occupied by PID %d.", states[state].name, pid);
+               del_node(state, node);
+               set_unlock_time(pid, state);
+
+               if (!display_state_transition_is_there_state_transition_timer())
+                       states[get_pm_cur_state()].trans(EVENT_TIMEOUT);
+       }
+
+       return G_SOURCE_REMOVE;
+}
+
+static int pmlock_check(void *data)
+{
+       PmLockNode *node;
+       int ret;
+
+       assert(data);
+       node = (PmLockNode *) data;
+
+       if (!initialized_killable_daemon_list) {
+               ret = config_parse(POWERLOCK_CONF_FILE, powerlock_load_config, NULL);
+               /* config file may not exist */
+               if (ret < 0 && ret != -ENOENT)
+                       _W("Failed to load %s, %d.", POWERLOCK_CONF_FILE, ret);
+
+               if (status == DEVICE_OPS_STATUS_UNINIT) {
+                       _W("Lock request before display init. Preloaded killable list.");
+                       initialized_killable_daemon_list = true;
+               } else if (status == DEVICE_OPS_STATUS_STOP) {
+                       _W("Lock request after display stop. Loaded list will be freed immediately.");
+                       node->killable_daemon = is_killable_daemon(node->pid);
+                       free_killable_daemon_list();
+                       goto killable_marked;
+               }
+       }
+
+       node->killable_daemon = is_killable_daemon(node->pid);
+
+killable_marked:
+       /* use default lock checker */
+       if (!node->killable_daemon)
+               return 0;
+
+       return g_timeout_add_seconds(KILLABLE_DAEMON_LOCK_LIMIT,
+               pmlock_terminate_daemon_to_release_lock, (gpointer)node);
+}
+
+static void free_killable_daemon_list(void)
+{
+       GList *l, *l_next;
+       char *blacklist;
+
+       if (!display_lock_killable_daemon)
+               return;
+
+       SYS_G_LIST_FOREACH_SAFE(display_lock_killable_daemon, l, l_next, blacklist) {
+               SYS_G_LIST_REMOVE(display_lock_killable_daemon, blacklist);
+               free(blacklist);
+       }
+       display_lock_killable_daemon = NULL;
+       initialized_killable_daemon_list = false;
+}
+
+static int get_device_flags(unsigned long *device_flags)
+{
+       if (!device_flags)
+               return -EINVAL;
+
+       *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 (lcd_paneloff_mode)
-               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)
@@ -334,30 +486,31 @@ static gboolean late_transit_on(void *data)
        g_source_remove(transit_timer);
        transit_timer = 0;
 
-       backlight_ops.transit_state(DPMS_ON);
+       display_backlight_change_brightness_by_dpms_state(DPMS_ON);
        return G_SOURCE_REMOVE;
 }
 
 void lcd_on_procedure(int state, enum device_flags flag)
 {
-       dd_list *l = NULL;
-       const struct device_ops *ops = NULL;
-       unsigned long flags = get_lcd_on_flags();
+       unsigned long flags = NORMAL_MODE;
+       display_plugin_get_device_flags(&flags);
        flags |= flag;
        int ret;
 
        /*
         * Display on procedure
-        * step 0. check if display is detached (only for factory mode)
-        * step 1. broadcast lcd on signal with cause
-        * step 2. set brightness
-        * step 3. set pmstate of vconf
-        * step 4. display on operate
+        * step 1. leave doze
+        * step 2. check if display is detached (only for factory mode)
+        * step 3. broadcast lcd on signal with cause
+        * step 4. set brightness
+        * step 5. set pmstate of vconf
+        * step 6. display on operate
         *  - a. display on
-        *  - b. TSP(touch screen) and touchkey enable
-        * step 5. broadcast lcd on complete signal
-        * step 6. key backlight enable
+        *  - b. TSP(touch screen) enable
+        * step 7. broadcast lcd on complete signal
+        * step 8. key backlight enable
         */
+       leave_doze();
 
        ret = is_lcdon_blocked();
        if (ret != LCDON_BLOCK_NONE) {
@@ -368,7 +521,7 @@ void lcd_on_procedure(int state, enum device_flags flag)
        _I("[lcdstep] 0x%lx", flags);
 
        if (flags & AMBIENT_MODE) {
-               if (ambient_get_state() == false && backlight_ops.get_lcd_power() == DPMS_ON)
+               if (ambient_get_state() == false && display_panel_get_dpms_cached_state() == DPMS_ON)
                        return;
                ambient_set_state(false);
        }
@@ -380,9 +533,9 @@ void lcd_on_procedure(int state, enum device_flags flag)
        if (!(flags & LCD_PHASED_TRANSIT_MODE)) {
                /* Update brightness level */
                if (state == LCD_DIM)
-                       backlight_ops.dim();
+                       display_backlight_set_brightness_by_dim_brightness();
                else if (state == LCD_NORMAL)
-                       backlight_ops.update();
+                       display_backlight_update_by_default_brightness();
        }
 
        if (state == LCD_NORMAL)
@@ -390,8 +543,7 @@ void lcd_on_procedure(int state, enum device_flags flag)
        else if (state == LCD_DIM)
                set_setting_pmstate(S_LCDDIM);
 
-       DD_LIST_FOREACH(lcdon_ops, l, ops)
-               ops->start(flags);
+       display_start_dependent_device(flags);
 
        if (!lcdon_broadcast) {
                broadcast_lcd_on(SIGNAL_POST, flags);
@@ -401,32 +553,13 @@ void lcd_on_procedure(int state, enum device_flags flag)
                lcdon_broadcast = true;
        }
 
-       if (CHECK_OPS(keyfilter_ops, backlight_enable))
-               keyfilter_ops->backlight_enable(true);
-
        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)
 {
-       dd_list *l = NULL;
-       const struct device_ops *ops = NULL;
-       unsigned long flags = get_lcd_off_flags();
+       unsigned long flags = NORMAL_MODE;
+       display_plugin_get_device_flags(&flags);
        flags |= flag;
 
        /*
@@ -436,13 +569,23 @@ inline void lcd_off_procedure(enum device_flags flag)
         * step 2. set pmstate of vconf
         * step 3. display off operate
         *  - a. display off
-        *  - b. TSP(touch screen) and touchkey disable
+        *  - b. TSP(touch screen) disable
         * step 4. broadcast lcd off complete siganl
+        * step 5. enter doze mode if it is enabled
         */
        _I("[lcdstep] 0x%lx", flags);
 
+       /* notification */
+
        device_notify(DEVICE_NOTIFIER_LCD_OFF, NULL);
 
+       if (lcdon_broadcast) {
+               broadcast_lcd_off(SIGNAL_PRE, flags);
+               lcdon_broadcast = false;
+       }
+
+       /* operation */
+
        touch_blocked = true;
 
        if (flags & AMBIENT_MODE) {
@@ -451,25 +594,17 @@ inline void lcd_off_procedure(enum device_flags flag)
                ambient_set_state(true);
        }
 
-       if (lcdon_broadcast) {
-               broadcast_lcd_off(SIGNAL_PRE, flags);
-               lcdon_broadcast = false;
-       }
        set_setting_pmstate(S_LCDOFF);
 
-       if (CHECK_OPS(keyfilter_ops, backlight_enable))
-               keyfilter_ops->backlight_enable(false);
-
        if (transit_timer) {
                g_source_remove(transit_timer);
                transit_timer = 0;
        }
 
        if (flags & LCD_PHASED_TRANSIT_MODE)
-               backlight_ops.transit_state(DPMS_OFF);
+               display_backlight_change_brightness_by_dpms_state(DPMS_OFF);
 
-       DD_LIST_FOREACH(lcdon_ops, l, ops)
-               ops->stop(flags);
+       display_stop_dependent_device(flags);
 
        if (flags & AMBIENT_MODE) {
                broadcast_lcd_off_late(flags);
@@ -477,66 +612,8 @@ inline void lcd_off_procedure(enum device_flags flag)
                broadcast_lcd_off(SIGNAL_POST, flags);
                device_notify(DEVICE_NOTIFIER_LCD_OFF_COMPLETE, NULL);
        }
-}
-
-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);
-}
-
-int low_battery_state(int val)
-{
-       switch (val) {
-       case VCONFKEY_SYSMAN_BAT_POWER_OFF:
-       case VCONFKEY_SYSMAN_BAT_CRITICAL_LOW:
-       case VCONFKEY_SYSMAN_BAT_REAL_POWER_OFF:
-               return true;
-       }
-       return false;
-}
-
-int get_hallic_open(void)
-{
-       return hallic_open;
-}
-
-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);
+       enter_doze();
 }
 
 static void del_state_cond(void *data, enum state_t state)
@@ -549,7 +626,7 @@ static void del_state_cond(void *data, enum state_t state)
 
        /* A passed data is a pid_t type data, not a 64bit data. */
        pid = (pid_t)((intptr_t)data);
-       _I("delete prohibit dim condition by timeout (%d)", pid);
+       _I("delete prohibit %s condition by timeout (%d)", states[state].name, pid);
 
        if (pid == INTERNAL_LOCK_AMBIENT)
                ambient_check_invalid_state(pid);
@@ -558,8 +635,12 @@ static void del_state_cond(void *data, enum state_t state)
        del_node(state, tmp);
        set_unlock_time(pid, state);
 
-       if (!timeout_src_id)
-               states[pm_cur_state].trans(EVENT_TIMEOUT);
+       /* Change state only when the two conditions below are satisfied.
+        *   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 (!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)
                set_process_active(false, pid);
@@ -583,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[pm_cur_state].name);
-
-       if (timeout_src_id) {
-               g_source_remove(timeout_src_id);
-               timeout_src_id = 0;
-       }
-
-       states[pm_cur_state].trans(EVENT_TIMEOUT);
-       return G_SOURCE_REMOVE;
-}
-
-void reset_timeout(int timeout)
-{
-       if (!display_conf.timeout_enable)
-               return;
-
-       if (pm_cur_state == S_LCDOFF
-       && (is_emulator() == true || display_conf.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[pm_cur_state][EVENT_TIMEOUT] == pm_cur_state)
-               return;
-
-       if (timeout > 0)
-               timeout_src_id = g_timeout_add(timeout,
-                   timeout_handler, NULL);
-       else if (timeout == 0)
-               states[pm_cur_state].trans(EVENT_TIMEOUT);
-}
-
 /* get configurations from setting */
 static int get_lcd_timeout_from_settings(void)
 {
@@ -658,15 +700,7 @@ static void update_display_time(void)
 {
        int run_timeout, val;
 
-       /* first priority : s cover */
-       if (!hallic_open) {
-               states[S_NORMAL].timeout = S_COVER_TIMEOUT;
-               _I("S cover closed: Timeout(%d ms) is set by normal.",
-                   S_COVER_TIMEOUT);
-               return;
-       }
-
-       /* second priority : custom timeout */
+       /* first priority : custom timeout */
        if (custom_normal_timeout > 0) {
                states[S_NORMAL].timeout = custom_normal_timeout;
                states[S_LCDDIM].timeout = custom_dim_timeout;
@@ -675,7 +709,7 @@ static void update_display_time(void)
                return;
        }
 
-       /* third priority : lock state */
+       /* second priority : lock state */
        if ((__get_lock_screen_state() == VCONFKEY_IDLE_LOCK) &&
            !get_lock_screen_bg_state()) {
                /* timeout is different according to key or event. */
@@ -715,27 +749,22 @@ void set_dim_state(bool on)
 {
        _I("Dim state is %d.", on);
        update_display_time();
-       states[pm_cur_state].trans(EVENT_INPUT);
+       states[get_pm_cur_state()].trans(EVENT_INPUT);
 }
 
 void lcd_on_direct(enum device_flags flags)
 {
-       int ret;
+       enum hal_device_power_transition_reason reason;
 
-       if (power_ops.get_power_lock_support()
-           && pm_cur_state == S_SLEEP) {
-               broadcast_pm_wakeup();
-               power_ops.power_lock();
-       }
-
-       ret = is_lcdon_blocked();
-       if (ret != LCDON_BLOCK_NONE) {
-               _W("LCDON is blocked, %d", ret);
-               return;
-       }
+       if (flags & LCD_ON_BY_POWER_KEY)
+               reason = HAL_DEVICE_POWER_TRANSITION_REASON_POWER_KEY;
+       else if (flags & LCD_ON_BY_TOUCH)
+               reason = HAL_DEVICE_POWER_TRANSITION_REASON_TOUCH_SCREEN;
+       else
+               reason = HAL_DEVICE_POWER_TRANSITION_REASON_UNKNOWN;
 
-       pm_old_state = pm_cur_state;
-       pm_cur_state = S_NORMAL;
+       power_request_change_state_strict(DEVICED_POWER_STATE_SLEEP, DEVICED_POWER_STATE_NORMAL, reason, NULL);
+       set_pm_cur_state(S_NORMAL);
 
        _D("lcd is on directly");
        gettimeofday(&lcdon_tv, NULL);
@@ -746,7 +775,7 @@ void lcd_on_direct(enum device_flags flags)
 
 static inline bool check_lcd_is_on(void)
 {
-       if (backlight_ops.get_lcd_power() != DPMS_ON)
+       if (display_panel_get_dpms_cached_state() != DPMS_ON)
                return false;
 
        return true;
@@ -772,9 +801,9 @@ static gboolean timer_refresh_cb(gpointer data)
                update_display_time();
 
        /* state transition */
-       pm_old_state = pm_cur_state;
-       pm_cur_state = S_NORMAL;
-       st = &states[pm_cur_state];
+       set_pm_old_state(get_pm_cur_state());
+       set_pm_cur_state(S_NORMAL);
+       st = &states[get_pm_cur_state()];
 
        if (st->action)
                st->action(st->timeout);
@@ -809,9 +838,9 @@ int custom_lcdon(int timeout)
                update_display_time();
 
        /* state transition */
-       pm_old_state = pm_cur_state;
-       pm_cur_state = S_NORMAL;
-       st = &states[pm_cur_state];
+       set_pm_old_state(get_pm_cur_state());
+       set_pm_cur_state(S_NORMAL);
+       st = &states[get_pm_cur_state()];
 
        /* enter action */
        if (st->action)
@@ -847,16 +876,16 @@ int custom_lcdoff(enum device_flags flag)
        }
 
        _I("custom lcd off by flag(%d)", flag);
-       if (backlight_ops.get_lcd_power() == DPMS_ON)
+       if (display_panel_get_dpms_cached_state() == DPMS_ON)
                lcd_off_procedure(flag);
 
        if (set_custom_lcdon_timeout(0) == true)
                update_display_time();
 
        /* state transition */
-       pm_old_state = pm_cur_state;
-       pm_cur_state = S_LCDOFF;
-       st = &states[pm_cur_state];
+       set_pm_old_state(get_pm_cur_state());
+       set_pm_cur_state(S_LCDOFF);
+       st = &states[get_pm_cur_state()];
 
        /* enter action */
        if (st->action)
@@ -878,7 +907,7 @@ int display_on_by_reason(const char *reason, int timeout)
        else if (!strncmp(reason, EVENT_STR, str_len))
                flag = LCD_ON_BY_EVENT;
        else {
-               _E("Reason is unkown(%s)", reason);
+               _E("Reason is unknown(%s)", reason);
                return -EINVAL;
        }
 
@@ -895,9 +924,9 @@ int display_on_by_reason(const char *reason, int timeout)
                update_display_time();
 
        /* state transition */
-       pm_old_state = pm_cur_state;
-       pm_cur_state = S_NORMAL;
-       st = &states[pm_cur_state];
+       set_pm_old_state(get_pm_cur_state());
+       set_pm_cur_state(S_NORMAL);
+       st = &states[get_pm_cur_state()];
 
        /* enter action */
        if (st->action)
@@ -940,13 +969,13 @@ int display_off_by_reason(const char *reason)
        }
 
        _I("platform lcd off by %s", reason);
-       if (backlight_ops.get_lcd_power() == DPMS_ON)
+       if (display_panel_get_dpms_cached_state() == DPMS_ON)
                lcd_off_procedure(flag);
 
        /* state transition */
-       pm_old_state = pm_cur_state;
-       pm_cur_state = S_LCDOFF;
-       st = &states[pm_cur_state];
+       set_pm_old_state(get_pm_cur_state());
+       set_pm_cur_state(S_LCDOFF);
+       st = &states[get_pm_cur_state()];
 
        /* enter action */
        if (st->action)
@@ -959,10 +988,10 @@ static void default_proc_change_state_action(enum state_t next, int timeout)
 {
        struct state *st;
 
-       pm_old_state = pm_cur_state;
-       pm_cur_state = next;
+       set_pm_old_state(get_pm_cur_state());
+       set_pm_cur_state(next);
 
-       st = &states[pm_cur_state];
+       st = &states[get_pm_cur_state()];
 
        if (st && st->action) {
                if (timeout < 0)
@@ -990,7 +1019,7 @@ static int default_proc_change_state(unsigned int cond, pid_t pid)
                default_proc_change_state_action(next, -1);
                break;
        case S_LCDOFF:
-               if (backlight_ops.get_lcd_power() == DPMS_ON)
+               if (display_panel_get_dpms_cached_state() == DPMS_ON)
                        lcd_off_procedure(LCD_OFF_BY_EVENT);
                if (set_custom_lcdon_timeout(0))
                        update_display_time();
@@ -1019,22 +1048,6 @@ static int default_proc_change_state(unsigned int cond, pid_t pid)
        return 0;
 }
 
-/* update transition condition for application requrements */
-static void update_lock_timer(PMMsg *data,
-               PmLockNode *node, guint timeout_id)
-{
-       time_t now;
-
-       if (data->timeout > 0) {
-               time(&now);
-               node->time = now;
-       }
-
-       if (node->timeout_id)
-               g_source_remove(node->timeout_id);
-       node->timeout_id = timeout_id;
-}
-
 static void proc_condition_lock(PMMsg *data)
 {
        PmLockNode *tmp;
@@ -1045,17 +1058,17 @@ static void proc_condition_lock(PMMsg *data)
        int holdkey_block, ret;
        bool value = true;
        unsigned int flags;
-       char *lock_type;
+       const char *lock_type = NULL;
 
        state = GET_COND_STATE(data->cond);
        if (state == S_START)
                return;
 
        flags = GET_COND_FLAG(data->cond);
-       get_pname(pid, pname);
+       display_misc_get_process_name(pid, pname);
 
-       if (state == S_LCDOFF && pm_cur_state == S_SLEEP &&
-           power_ops.get_power_lock() == POWER_UNLOCK)
+       if ((state == S_LCDOFF) && (get_pm_cur_state() == S_SLEEP) &&
+           (pm_get_power_lock() == POWER_UNLOCK))
                proc_change_state(data->cond, INTERNAL_LOCK_PM);
 
        if (data->timeout > 0) {
@@ -1073,9 +1086,13 @@ static void proc_condition_lock(PMMsg *data)
        holdkey_block = GET_COND_FLAG(data->cond) & PM_FLAG_BLOCK_HOLDKEY;
 
        tmp = find_node(state, pid);
-       if (!tmp)
-               add_node(state, pid, cond_timeout_id, holdkey_block);
-       else {
+       if (!tmp) {
+               tmp = add_node(state, pid, cond_timeout_id, holdkey_block);
+               if (!tmp) {
+                       _E("Failed to acquire lock, state: %d, pid: %d.", state, pid);
+                       return;
+               }
+       } else {
                update_lock_timer(data, tmp, cond_timeout_id);
                tmp->holdkey_block = holdkey_block;
        }
@@ -1084,28 +1101,30 @@ static void proc_condition_lock(PMMsg *data)
                set_process_active(true, pid);
 
        _I("[%s] locked by %5d with %u ms", states[state].name, pid, data->timeout);
-       /* for debug */
-       if (state == S_LCDOFF)
-               lock_type = PM_LCDOFF_STR;
-       else if (state == S_LCDDIM)
-               lock_type = PM_LCDDIM_STR;
-       else if (state == S_NORMAL)
-               lock_type = PM_LCDON_STR;
-       else
-               goto skip_broadcast_signal;
-
-       /* power lock signal */
-       ret = dbus_handle_emit_dbus_signal(NULL,
-                       DEVICED_PATH_DISPLAY,
-                       DEVICED_INTERFACE_DISPLAY,
-                       "PowerLock",
-                       g_variant_new("(sii)", lock_type, pid, (int)data->timeout));
-       if (ret < 0)
-               _E("Failed to send dbus signal PowerLock.");
 
-skip_broadcast_signal:
-       _SD("be requested LOCK info pname(%s), holdkeyblock(%d) flags(%d)",
-           pname, holdkey_block, flags);
+       if (pid < INTERNAL_LOCK_BASE) {
+               /* for debug */
+               if (state == S_LCDOFF)
+                       lock_type = PM_LCDOFF_STR;
+               else if (state == S_LCDDIM)
+                       lock_type = PM_LCDDIM_STR;
+               else if (state == S_NORMAL)
+                       lock_type = PM_LCDON_STR;
+
+               if (lock_type) {
+                       /* power lock signal */
+                       ret = gdbus_signal_emit(NULL,
+                                       DEVICED_PATH_DISPLAY,
+                                       DEVICED_INTERFACE_DISPLAY,
+                                       "PowerLock",
+                                       g_variant_new("(sii)", lock_type, pid, (int)data->timeout));
+                       if (ret < 0)
+                               _E("Failed to send dbus signal PowerLock.");
+               }
+       }
+
+       _SD("be requested LOCK info pname(%s), holdkeyblock(%d) flags(%d) killable_daemon(%d)",
+           pname, holdkey_block, flags, tmp->killable_daemon);
        set_lock_time(pid, pname, state);
 
        device_notify(DEVICE_NOTIFIER_DISPLAY_LOCK, (void *)&value);
@@ -1120,14 +1139,14 @@ static void proc_condition_unlock(PMMsg *data)
        bool value = false;
        unsigned int flags;
        int ret;
-       char *lock_type;
+       const char *lock_type = NULL;
 
        state = GET_COND_STATE(data->cond);
        if (!state)
                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);
@@ -1136,26 +1155,28 @@ static void proc_condition_unlock(PMMsg *data)
                set_process_active(false, pid);
 
        _I("[%s] unlocked by %5d", states[state].name, pid);
-       /* for debug */
-       if (state == S_LCDOFF)
-               lock_type = PM_LCDOFF_STR;
-       else if (state == S_LCDDIM)
-               lock_type =  PM_LCDDIM_STR;
-       else if (state == S_NORMAL)
-               lock_type = PM_LCDON_STR;
-       else
-               goto skip_broadcast_signal;
-
-       /* power unlock signal */
-       ret = dbus_handle_emit_dbus_signal(NULL,
-                       DEVICED_PATH_DISPLAY,
-                       DEVICED_INTERFACE_DISPLAY,
-                       "PowerUnlock",
-                       g_variant_new("(sii)", lock_type, pid, (int)data->timeout));
-       if (ret < 0)
-               _E("Failed to send dbus signal PowerUnlock.");
 
-skip_broadcast_signal:
+       if (pid < INTERNAL_LOCK_BASE) {
+               /* for debug */
+               if (state == S_LCDOFF)
+                       lock_type = PM_LCDOFF_STR;
+               else if (state == S_LCDDIM)
+                       lock_type =  PM_LCDDIM_STR;
+               else if (state == S_NORMAL)
+                       lock_type = PM_LCDON_STR;
+
+               if (lock_type) {
+                       /* power unlock signal */
+                       ret = gdbus_signal_emit(NULL,
+                                       DEVICED_PATH_DISPLAY,
+                                       DEVICED_INTERFACE_DISPLAY,
+                                       "PowerUnlock",
+                                       g_variant_new("(sii)", lock_type, pid, (int)data->timeout));
+                       if (ret < 0)
+                               _E("Failed to send dbus signal PowerUnlock.");
+               }
+       }
+
        _SD("be requested UNLOCK info pname(%s) flag(%d)", pname, flags);
        set_unlock_time(pid, state);
 
@@ -1178,15 +1199,15 @@ static int proc_condition(PMMsg *data)
        flags = GET_COND_FLAG(data->cond);
        if (flags == 0) {
                /* guard time for suspend */
-               if (pm_cur_state == S_LCDOFF)
-                       reset_timeout(states[S_LCDOFF].timeout);
+               if (get_pm_cur_state() == S_LCDOFF)
+                       display_state_transition_reset_state_transition_timeout(states[S_LCDOFF].timeout);
        } else {
                if (flags & PM_FLAG_RESET_TIMER)
-                       reset_timeout(states[pm_cur_state].timeout);
+                       display_state_transition_reset_state_transition_timeout(states[get_pm_cur_state()].timeout);
        }
 
-       if (!timeout_src_id)
-               states[pm_cur_state].trans(EVENT_TIMEOUT);
+       if (!display_state_transition_is_there_state_transition_timer())
+               states[get_pm_cur_state()].trans(EVENT_TIMEOUT);
 
        return 0;
 }
@@ -1211,192 +1232,6 @@ void update_lcdoff_source(int source)
                _E("Failed to set vconf value for lcd off source: %d", vconf_get_ext_errno());
 }
 
-#ifdef ENABLE_PM_LOG
-
-typedef struct _pm_history {
-       time_t time;
-       enum pm_log_type log_type;
-       int keycode;
-} pm_history;
-
-static int max_history_count = MAX_LOG_COUNT;
-static pm_history pm_history_log[MAX_LOG_COUNT] = {{0, }, };
-static int history_count = 0;
-
-static const char history_string[PM_LOG_MAX][15] = {
-       "PRESS", "LONG PRESS", "RELEASE", "LCD ON", "LCD ON COMPL", "LCD ON FAIL",
-       "LCD DIM", "LCD DIM FAIL", "LCD OFF", "LCD OFF COMPL", "LCD OFF FAIL",
-       "LCD FAIL", "SLEEP"};
-
-void pm_history_init()
-{
-       memset(pm_history_log, 0x0, sizeof(pm_history_log));
-       history_count = 0;
-       max_history_count = MAX_LOG_COUNT;
-}
-
-void pm_history_save(enum pm_log_type log_type, int code)
-{
-       time_t now;
-
-       time(&now);
-       pm_history_log[history_count].time = now;
-       pm_history_log[history_count].log_type = log_type;
-       pm_history_log[history_count].keycode = code;
-       history_count++;
-
-       if (history_count >= max_history_count)
-               history_count = 0;
-}
-
-void pm_history_print(int fd, int count)
-{
-       int start_index, index, i;
-       int ret;
-       char buf[255];
-       char time_buf[30];
-
-       if (count <= 0 || count > max_history_count)
-               return;
-
-       start_index = (history_count - count + max_history_count)
-                   % max_history_count;
-
-       for (i = 0; i < count; i++) {
-               index = (start_index + i) % max_history_count;
-
-               if (pm_history_log[index].time == 0)
-                       continue;
-
-               if (pm_history_log[index].log_type < PM_LOG_MIN ||
-                   pm_history_log[index].log_type >= PM_LOG_MAX)
-                       continue;
-               ctime_r(&pm_history_log[index].time, time_buf);
-               time_buf[strlen(time_buf) - 1] = 0;
-               snprintf(buf, sizeof(buf), "[%3d] %15s %3d %s\n",
-                       index,
-                       history_string[pm_history_log[index].log_type],
-                       pm_history_log[index].keycode,
-                       time_buf);
-               ret = write(fd, buf, strlen(buf));
-               if (ret < 0)
-                       _E("Write() failed: %d", errno);
-       }
-}
-#endif
-
-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;
-       dd_list *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[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++) {
-               DD_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", 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
  */
@@ -1412,17 +1247,17 @@ int check_lcdoff_direct(void)
        int ret, lock, cradle;
        int hdmi_state;
 
-       if (pm_old_state != S_NORMAL)
+       if (get_pm_old_state() != S_NORMAL)
                return false;
 
-       if (pm_cur_state != S_LCDDIM)
+       if (get_pm_cur_state() != S_LCDDIM)
                return false;
 
        if (!display_conf.dimming)
                return true;
 
        lock = __get_lock_screen_state();
-       if (lock != VCONFKEY_IDLE_LOCK && hallic_open)
+       if (lock != VCONFKEY_IDLE_LOCK)
                return false;
 
        hdmi_state = extcon_get_status(EXTCON_CABLE_HDMI);
@@ -1448,21 +1283,18 @@ int check_lcdoff_direct(void)
  */
 static int default_trans(int evt)
 {
-       struct state *st = &states[pm_cur_state];
-       int next_state, ret;
+       struct state *st = &states[get_pm_cur_state()];
+       int ret;
+       enum state_t next_state;
 
-       next_state = (enum state_t)trans_table[pm_cur_state][evt];
+       display_state_transition_get_next_transition_display_state(get_pm_cur_state(), &next_state, evt);
 
        /* check conditions */
-       while (st->check && !st->check(pm_cur_state, next_state)) {
+       if (st->check && !st->check(get_pm_cur_state(), next_state)) {
                /* There is a condition. */
-               _I("%s locked. Trans to %s failed.", states[pm_cur_state].name,
+               _I("%s locked. Trans to %s failed.", states[get_pm_cur_state()].name,
                       states[next_state].name);
-               if (!check_processes(pm_cur_state)) {
-                       /* This is valid condition
-                        * The application that sent the condition is running now. */
-                       return -1;
-               }
+               return -1;
        }
 
        ret = is_lcdon_blocked();
@@ -1474,25 +1306,25 @@ static int default_trans(int evt)
        }
 
        /* state transition */
-       pm_old_state = pm_cur_state;
-       pm_cur_state = next_state;
-       st = &states[pm_cur_state];
+       set_pm_old_state(get_pm_cur_state());
+       set_pm_cur_state(next_state);
+       st = &states[get_pm_cur_state()];
 
        /* enter action */
        if (st->action) {
-               if (pm_cur_state == S_LCDOFF)
+               if (get_pm_cur_state() == S_LCDOFF)
                        update_lcdoff_source(VCONFKEY_PM_LCDOFF_BY_TIMEOUT);
 
-               if (pm_cur_state == S_NORMAL || pm_cur_state == S_LCDOFF)
+               if ((get_pm_cur_state() == S_NORMAL) || (get_pm_cur_state() == S_LCDOFF))
                        if (set_custom_lcdon_timeout(0) == true)
                                update_display_time();
 
                if (check_lcdoff_direct() == true) {
                        /* enter next state directly */
-                       states[pm_cur_state].trans(EVENT_TIMEOUT);
+                       states[get_pm_cur_state()].trans(EVENT_TIMEOUT);
                } else {
-                       if (pm_cur_state == S_SLEEP
-                       && (is_emulator() == true || display_conf.sleep_support == false))
+                       if ((get_pm_cur_state() == S_SLEEP)
+                       && (is_emulator() == true || timeout_sleep_support == false))
                                return 0;
 
                        st->action(st->timeout);
@@ -1550,13 +1382,16 @@ static void check_lock_screen(void)
 /* default enter action function */
 static int default_action(int timeout)
 {
-       int wakeup_count = -1;
+       int wakeup_count = -1, pm_cur_state;
        int ret;
        time_t now;
        double diff;
        static time_t last_update_time = 0;
        static int last_timeout = 0;
        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.");
@@ -1565,99 +1400,101 @@ static int default_action(int timeout)
 
        ret = is_lcdon_blocked();
        if (ret != LCDON_BLOCK_NONE) {
-               if (pm_cur_state == S_NORMAL || pm_cur_state == S_LCDDIM) {
+               if ((get_pm_cur_state() == S_NORMAL) || (get_pm_cur_state() == S_LCDDIM)) {
                        _W("LCDON is blocked, %d.", ret);
                        return -ENOTSUP;
                }
        }
 
-       if (pm_cur_state != S_SLEEP) {
-               if (pm_cur_state == S_NORMAL &&
+       if (get_pm_cur_state() != S_SLEEP) {
+               if ((get_pm_cur_state() == S_NORMAL) &&
                    lcdon_tv.tv_sec != 0) {
                        gettimeofday(&now_tv, NULL);
                        timeout -= DIFF_TIMEVAL_MS(now_tv, lcdon_tv);
                        lcdon_tv.tv_sec = 0;
                }
                /* set timer with current state timeout */
-               reset_timeout(timeout);
+               display_state_transition_reset_state_transition_timeout(timeout);
 
-               if (pm_cur_state == S_NORMAL) {
+               if (get_pm_cur_state() == S_NORMAL) {
                        time(&last_update_time);
                        last_timeout = timeout;
                } else {
                        _I("Timout set: %s state %d ms",
-                           states[pm_cur_state].name, timeout);
+                           states[get_pm_cur_state()].name, timeout);
                }
        }
 
-       if (pm_cur_state != pm_old_state && pm_cur_state != S_SLEEP) {
-               if (power_ops.get_power_lock_support()) {
-                       broadcast_pm_wakeup();
-                       power_ops.power_lock();
-               }
-               set_setting_pmstate(pm_cur_state);
+       if ((get_pm_cur_state() != get_pm_old_state()) && (get_pm_cur_state() != S_SLEEP)) {
+               power_request_change_state_strict(DEVICED_POWER_STATE_SLEEP, DEVICED_POWER_STATE_NORMAL,
+                       power_get_wakeup_reason(), NULL);
+               set_setting_pmstate(get_pm_cur_state());
+               pm_cur_state = get_pm_cur_state();
                device_notify(DEVICE_NOTIFIER_LCD, (void *)&pm_cur_state);
        }
 
-       if (pm_old_state == S_NORMAL && pm_cur_state != S_NORMAL) {
+       if ((get_pm_old_state() == S_NORMAL) && (get_pm_cur_state() != S_NORMAL)) {
                time(&now);
                diff = difftime(now, last_update_time);
                _I("S_NORMAL is changed to %s (timeout=%d ms diff=%.0f s).",
-                   states[pm_cur_state].name, last_timeout, diff);
+                   states[get_pm_cur_state()].name, last_timeout, diff);
        }
 
-       /* update status for batter monitor */
-       update_bds_record(pm_cur_state);
+       /* update status for battery monitor */
+       update_bds_record(get_pm_cur_state());
 
-       switch (pm_cur_state) {
+       switch (get_pm_cur_state()) {
        case S_NORMAL:
                /*
                 * normal state : backlight on and restore
                 * the previous brightness
                 */
-               if (pm_old_state == S_LCDOFF || pm_old_state == S_SLEEP)
+               if ((get_pm_old_state() == S_LCDOFF) || (get_pm_old_state() == S_SLEEP))
                        check_lock_screen();
-               else if (pm_old_state == S_LCDDIM)
-                       backlight_ops.update();
+               else if (get_pm_old_state() == S_LCDDIM)
+                       display_backlight_update_by_default_brightness();
 
                if (check_lcd_is_on() == false)
                        lcd_on_procedure(LCD_NORMAL, NORMAL_MODE);
                break;
 
        case S_LCDDIM:
-               if (pm_old_state == S_NORMAL &&
-                   backlight_ops.get_custom_status())
-                       backlight_ops.save_custom_brightness();
+               display_backlight_get_custom_status(&custom_status);
+               if ((get_pm_old_state() == S_NORMAL) && custom_status) {
+                       display_backlight_get_brightness(&brightness);
+                       display_backlight_set_custom_brightness(brightness);
+               }
                /* lcd dim state : dim the brightness */
-               backlight_ops.dim();
+               display_backlight_set_brightness_by_dim_brightness();
 
-               if (pm_old_state == S_LCDOFF || pm_old_state == S_SLEEP)
+               if ((get_pm_old_state() == S_LCDOFF) || (get_pm_old_state() == S_SLEEP))
                        lcd_on_procedure(LCD_DIM, NORMAL_MODE);
                break;
 
        case S_LCDOFF:
-               if (pm_old_state != S_SLEEP && pm_old_state != S_LCDOFF) {
+               if ((get_pm_old_state() != S_SLEEP) && (get_pm_old_state() != S_LCDOFF)) {
                        stop_lock_timer();
                        /* lcd off state : turn off the backlight */
-                       if (backlight_ops.get_lcd_power() == DPMS_ON)
+                       if (display_panel_get_dpms_cached_state() == DPMS_ON)
                                lcd_off_procedure(LCD_OFF_BY_TIMEOUT);
                }
 
-               if (backlight_ops.get_lcd_power() == DPMS_ON
+               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);
                break;
 
        case S_SLEEP:
-               if (pm_old_state != S_SLEEP && pm_old_state != S_LCDOFF)
+               if ((get_pm_old_state() != S_SLEEP) && (get_pm_old_state() != S_LCDOFF))
                        stop_lock_timer();
 
-               if (backlight_ops.get_lcd_power() == DPMS_ON)
+               if (display_panel_get_dpms_cached_state() == DPMS_ON)
                        lcd_off_procedure(LCD_OFF_BY_TIMEOUT);
 
-               if (!power_ops.get_power_lock_support()) {
+               if (!pm_get_power_lock_support()) {
                        /* sleep state : set system mode to SUSPEND */
-                       if (power_ops.get_wakeup_count(&wakeup_count) < 0)
+                       if (get_wakeup_count(&wakeup_count) < 0)
                                _E("Wakeup count read error.");
 
                        if (wakeup_count < 0) {
@@ -1665,7 +1502,7 @@ static int default_action(int timeout)
                                goto go_lcd_off;
                        }
 
-                       if (power_ops.set_wakeup_count(wakeup_count) < 0) {
+                       if (set_wakeup_count(wakeup_count) < 0) {
                                _E("Wakeup count write error.");
                                goto go_lcd_off;
                        }
@@ -1677,33 +1514,15 @@ static int default_action(int timeout)
 
 go_suspend:
 #ifdef ENABLE_PM_LOG
-       pm_history_save(PM_LOG_SLEEP, pm_cur_state);
+       pm_history_save(PM_LOG_SLEEP, get_pm_cur_state());
 #endif
-       broadcast_pm_suspend();
-       if (power_ops.get_power_lock_support()) {
-               if (power_ops.enable_autosleep)
-                       power_ops.enable_autosleep();
-
-               if (power_ops.power_unlock() < 0)
-                       _E("Power unlock state error.");
-       } else {
-               power_ops.suspend();
-               _I("system wakeup!!");
-               system_wakeup_flag = true;
-               /* Resume !! */
-               if (power_ops.check_wakeup_src() == EVENT_DEVICE)
-                       /* system waked up by devices */
-                       states[pm_cur_state].trans(EVENT_DEVICE);
-               else
-                       /* system waked up by user input */
-                       states[pm_cur_state].trans(EVENT_INPUT);
-       }
+       power_request_change_state(DEVICED_POWER_STATE_SLEEP, HAL_DEVICE_POWER_TRANSITION_REASON_DISPLAY_OFF_TIMEOUT);
        return 0;
 
 go_lcd_off:
-       if (!power_ops.get_power_lock_support()) {
+       if (!pm_get_power_lock_support()) {
                /* Resume !! */
-               states[pm_cur_state].trans(EVENT_DEVICE);
+               states[get_pm_cur_state()].trans(EVENT_DEVICE);
        }
        return 0;
 }
@@ -1749,16 +1568,16 @@ static int default_check(int curr, int next)
 
 static void default_saving_mode(int onoff)
 {
-       if (onoff)
-               pm_status_flag |= PWRSV_FLAG;
-       else
-               pm_status_flag &= ~PWRSV_FLAG;
-
-       if (pm_cur_state == S_NORMAL)
-               backlight_ops.update();
+       if (onoff) {
+               set_pm_status_flag(PWRSV_FLAG);
+               auto_brightness_control(BR_LOWDIM_ON, BR_IMPLICIT);
+       } else {
+               clear_pm_status_flag(PWRSV_FLAG);
+               auto_brightness_control(BR_LOWDIM_OFF, BR_IMPLICIT);
+       }
 }
 
-static int poll_callback(int condition, PMMsg *data)
+int poll_callback(int condition, PMMsg *data)
 {
        static time_t last_t;
        time_t now;
@@ -1769,13 +1588,13 @@ static int poll_callback(int condition, PMMsg *data)
        }
 
        if (condition == INPUT_POLL_EVENT) {
-               if (pm_cur_state == S_LCDOFF || pm_cur_state == S_SLEEP)
+               if ((get_pm_cur_state() == S_LCDOFF) || (get_pm_cur_state() == S_SLEEP))
                        _I("Input event signal at Display Off");
                time(&now);
-               if (last_t != now ||
-                   pm_cur_state == S_LCDOFF ||
-                   pm_cur_state == S_SLEEP) {
-                       states[pm_cur_state].trans(EVENT_INPUT);
+               if ((last_t != now) ||
+                   (get_pm_cur_state() == S_LCDOFF) ||
+                   (get_pm_cur_state() == S_SLEEP)) {
+                       states[get_pm_cur_state()].trans(EVENT_INPUT);
                        last_t = now;
                }
        }
@@ -1797,26 +1616,18 @@ static int update_setting(int key_idx, int val)
        switch (key_idx) {
        case SETTING_TO_NORMAL:
                update_display_time();
-               states[pm_cur_state].trans(EVENT_INPUT);
-               break;
-       case SETTING_HALLIC_OPEN:
-               hallic_open = val;
-               update_display_time();
-               if (pm_cur_state == S_NORMAL || pm_cur_state == S_LCDDIM)
-                       states[pm_cur_state].trans(EVENT_INPUT);
-               else if (pm_cur_state == S_SLEEP && hallic_open)
-                       proc_change_state(S_LCDOFF, INTERNAL_LOCK_HALLIC);
+               states[get_pm_cur_state()].trans(EVENT_INPUT);
                break;
        case SETTING_LOW_BATT:
-               if (low_battery_state(val)) {
-                       if (!(pm_status_flag & CHRGR_FLAG))
+               if (display_misc_is_low_battery_state(val)) {
+                       if (!(get_pm_status_flag() & CHRGR_FLAG))
                                power_saving_func(true);
-                       pm_status_flag |= LOWBT_FLAG;
+                       set_pm_status_flag(LOWBT_FLAG);
                } else {
-                       if (pm_status_flag & PWRSV_FLAG)
+                       if (get_pm_status_flag() & PWRSV_FLAG)
                                power_saving_func(false);
-                       pm_status_flag &= ~LOWBT_FLAG;
-                       pm_status_flag &= ~BRTCH_FLAG;
+                       clear_pm_status_flag(LOWBT_FLAG);
+                       clear_pm_status_flag(BRTCH_FLAG);
                        ret = vconf_set_bool(VCONFKEY_PM_BRIGHTNESS_CHANGED_IN_LPM, false);
                        if (ret < 0)
                                _E("Failed to set vconf value for brightness changed in lpm: %d", vconf_get_ext_errno());
@@ -1824,11 +1635,11 @@ static int update_setting(int key_idx, int val)
                break;
        case SETTING_CHARGING:
                if (val) {
-                       if (pm_status_flag & LOWBT_FLAG) {
+                       if (get_pm_status_flag() & LOWBT_FLAG) {
                                power_saving_func(false);
-                               pm_status_flag &= ~LOWBT_FLAG;
+                               clear_pm_status_flag(LOWBT_FLAG);
                        }
-                       pm_status_flag |= CHRGR_FLAG;
+                       set_pm_status_flag(CHRGR_FLAG);
                } else {
                        int bat_state;
                        ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW,
@@ -1837,64 +1648,49 @@ static int update_setting(int key_idx, int val)
                                bat_state = VCONFKEY_SYSMAN_BAT_NORMAL;
                                _E("Failed to get vconf value for battery status low: %d", vconf_get_ext_errno());
                        }
-                       if (low_battery_state(bat_state)) {
+                       if (display_misc_is_low_battery_state(bat_state)) {
                                power_saving_func(true);
-                               pm_status_flag |= LOWBT_FLAG;
+                               set_pm_status_flag(LOWBT_FLAG);
                        }
-                       pm_status_flag &= ~CHRGR_FLAG;
+                       clear_pm_status_flag(CHRGR_FLAG);
                }
                break;
        case SETTING_BRT_LEVEL:
-               if (pm_status_flag & PWRSV_FLAG) {
-                       pm_status_flag |= BRTCH_FLAG;
+               if (get_pm_status_flag() & PWRSV_FLAG) {
+                       set_pm_status_flag(BRTCH_FLAG);
                        ret = vconf_set_bool(VCONFKEY_PM_BRIGHTNESS_CHANGED_IN_LPM, true);
                        if (ret < 0)
                                _E("Failed to set vconf value for brightness changed in lpm: %d", vconf_get_ext_errno());
                        _I("Brightness changed in low battery,"
                                "escape dim state.");
                }
-               backlight_ops.set_default_brt(val);
+               display_backlight_set_default_brightness(val);
                break;
        case SETTING_LOCK_SCREEN:
                set_lock_screen_state(val);
-               if (val == VCONFKEY_IDLE_UNLOCK) {
-                       if (CHECK_OPS(keyfilter_ops, backlight_enable))
-                               keyfilter_ops->backlight_enable(false);
-               }
 
                /* LCD on if lock screen show before waiting time */
-               if (pm_cur_state == S_NORMAL &&
+               if ((get_pm_cur_state() == S_NORMAL) &&
                    val == VCONFKEY_IDLE_LOCK &&
-                   backlight_ops.get_lcd_power() != DPMS_ON &&
+                   display_panel_get_dpms_cached_state() != DPMS_ON &&
                        is_lcdon_blocked() == LCDON_BLOCK_NONE)
                        lcd_on_procedure(LCD_NORMAL, LCD_ON_BY_EVENT);
                stop_lock_timer();
                update_display_time();
-               if (pm_cur_state == S_NORMAL)
-                       states[pm_cur_state].trans(EVENT_INPUT);
+               if (get_pm_cur_state() == S_NORMAL)
+                       states[get_pm_cur_state()].trans(EVENT_INPUT);
                break;
        case SETTING_LOCK_SCREEN_BG:
                set_lock_screen_bg_state(val);
                update_display_time();
-               if (pm_cur_state == S_NORMAL)
-                       states[pm_cur_state].trans(EVENT_INPUT);
-               break;
-       case SETTING_POWEROFF:
-               switch (val) {
-               case POWEROFF_TYPE_NONE:
-                       pm_status_flag &= ~PWROFF_FLAG;
-                       break;
-               case POWEROFF_TYPE_DIRECT:
-               case POWEROFF_TYPE_RESTART:
-                       pm_status_flag |= PWROFF_FLAG;
-                       break;
-               }
+               if (get_pm_cur_state() == S_NORMAL)
+                       states[get_pm_cur_state()].trans(EVENT_INPUT);
                break;
        case SETTING_POWER_CUSTOM_BRIGHTNESS:
                if (val == VCONFKEY_PM_CUSTOM_BRIGHTNESS_ON)
-                       backlight_ops.set_custom_status(true);
+                       display_backlight_set_custom_status(true);
                else
-                       backlight_ops.set_custom_status(false);
+                       display_backlight_set_custom_status(false);
                break;
 
        default:
@@ -1912,8 +1708,8 @@ static void check_seed_status(void)
        int lock_state;
 
        /* Charging check */
-       if ((get_charging_status(&tmp) == 0) && (tmp > 0))
-               pm_status_flag |= CHRGR_FLAG;
+       if (fp_get_charging_status && (fp_get_charging_status(&tmp) == 0) && (tmp > 0))
+               set_pm_status_flag(CHRGR_FLAG);
 
        ret = get_setting_brightness(&tmp);
        if (ret != 0 || (tmp < PM_MIN_BRIGHTNESS || tmp > PM_MAX_BRIGHTNESS)) {
@@ -1927,18 +1723,18 @@ static void check_seed_status(void)
                tmp = brt;
        }
        _I("Set brightness(%d) from setting app.", tmp);
-       backlight_ops.set_default_brt(tmp);
-       backlight_ops.set_brightness(tmp);
+       display_backlight_set_default_brightness(tmp);
+       display_backlight_set_brightness(tmp);
 
        ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, &bat_state);
        if (ret < 0) {
                bat_state = VCONFKEY_SYSMAN_BAT_NORMAL;
                _E("Failed to get vconf value for battery status low: %d", vconf_get_ext_errno());
        }
-       if (low_battery_state(bat_state)) {
-               if (!(pm_status_flag & CHRGR_FLAG)) {
+       if (display_misc_is_low_battery_state(bat_state)) {
+               if (!(get_pm_status_flag() & CHRGR_FLAG)) {
                        power_saving_func(true);
-                       pm_status_flag |= LOWBT_FLAG;
+                       set_pm_status_flag(LOWBT_FLAG);
                }
        }
 
@@ -1964,29 +1760,20 @@ static void init_lcd_operation(void)
 
        ops = find_device("display");
        if (!check_default(ops))
-               DD_LIST_APPEND(lcdon_ops, ops);
-
-       ops = find_device("touchkey");
-       if (!check_default(ops))
-               DD_LIST_APPEND(lcdon_ops, ops);
+               display_register_dependent_device(ops);
 
        ops = find_device("touchscreen");
        if (!check_default(ops))
-               DD_LIST_APPEND(lcdon_ops, ops);
+               display_register_dependent_device(ops);
 
        ops = find_device("bezel");
        if (!check_default(ops))
-               DD_LIST_APPEND(lcdon_ops, ops);
+               display_register_dependent_device(ops);
 }
 
 static void exit_lcd_operation(void)
 {
-       dd_list *l = NULL;
-       dd_list *l_next = NULL;
-       const struct device_ops *ops = NULL;
-
-       DD_LIST_FOREACH_SAFE(lcdon_ops, l, l_next, ops)
-               DD_LIST_REMOVE_LIST(lcdon_ops, l);
+       display_unregister_dependent_device();
 }
 
 enum {
@@ -2021,14 +1808,14 @@ int set_lcd_timeout(int on, int dim, int holdkey_block, const char *name)
        }
        /* Apply new backlight time */
        update_display_time();
-       if (pm_cur_state == S_NORMAL)
-               states[pm_cur_state].trans(EVENT_INPUT);
+       if (get_pm_cur_state() == S_NORMAL)
+               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.");
        }
 
@@ -2046,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;
        }
 
@@ -2072,14 +1859,14 @@ 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 (pm_cur_state == S_NORMAL)
-               states[pm_cur_state].trans(EVENT_INPUT);
+       if (get_pm_cur_state() == S_NORMAL)
+               states[get_pm_cur_state()].trans(EVENT_INPUT);
 }
 
-static int booting_done(void *data)
+static int delayed_init_done(void *data)
 {
        static bool done = false;
 
@@ -2091,148 +1878,42 @@ static int booting_done(void *data)
                return done;
 
        _I("Booting done, release booting lock.");
-       if (disp_plgn.pm_unlock_internal) {
-               disp_plgn.pm_unlock_internal(INTERNAL_LOCK_BOOTING, LCD_NORMAL, PM_SLEEP_MARGIN);
-               disp_plgn.pm_unlock_internal(INTERNAL_LOCK_BOOTING, LCD_OFF, PM_SLEEP_MARGIN);
+       if (disp_plgn->pm_unlock_internal) {
+               disp_plgn->pm_unlock_internal(INTERNAL_LOCK_BOOTING, LCD_NORMAL, PM_SLEEP_MARGIN);
+               disp_plgn->pm_unlock_internal(INTERNAL_LOCK_BOOTING, LCD_OFF, PM_SLEEP_MARGIN);
        }
 
        return done;
 }
 
-static int process_background(void *data)
+static int powerlock_load_config(struct parse_result *result, void *user_data)
 {
-       pid_t pid;
-       PmLockNode *node;
-
-       pid = *(pid_t *)data;
-
-       node = find_node(S_NORMAL, pid);
-       if (node) {
-               node->background = true;
-               _I("Process(%d) is background, then PM will be unlocked LCD_NORMAL.", pid);
-       }
-
-       return 0;
-}
+       char *name = NULL;
 
-static int process_foreground(void *data)
-{
-       pid_t pid;
-       PmLockNode *node;
-
-       pid = *(pid_t *)data;
-
-       node = find_node(S_NORMAL, pid);
-       if (node) {
-               node->background = false;
-               _I("Process(%d) is foreground, then PM will be locked LCD_NORMAL.", pid);
-       }
-
-       return 0;
-}
-
-static int battery_health_changed(void *data)
-{
-       int health = DATA_VALUE_INT(data);
+       _D("powerlock_load_config: section=%s name=%s value=%s", result->section, result->name, result->value);
 
-       _I("battery health change %d", health);
-
-       if (health == HEALTH_GOOD) {
-               pm_status_flag &= ~BATTERY_FLAG;
-               pm_status_flag &= ~DIMSTAY_FLAG;
-       } else if (health == HEALTH_LOW || health == HEALTH_HIGH || health == HEALTH_OVP) {
-               pm_status_flag |= BATTERY_FLAG;
-               pm_status_flag |= DIMSTAY_FLAG;
-       }
-
-       if (backlight_ops.get_lcd_power() == DPMS_ON)
-               backlight_ops.update();
-
-       return 0;
-}
-
-static int display_load_config(struct parse_result *result, void *user_data)
-{
-       struct display_config *c = user_data;
-
-       _D("display_load_config: section=%s name=%s value=%s", result->section, result->name, result->value);
-
-       if (!c)
-               return -EINVAL;
-
-       if (!MATCH(result->section, "Display"))
-               return 0;
-
-       if (MATCH(result->name, "LockScreenWaitingTime")) {
-               SET_CONF(c->lock_wait_time, atof(result->value));
-               _D("lock wait time is %.3f", c->lock_wait_time);
-       } else if (MATCH(result->name, "LongPressInterval")) {
-               SET_CONF(c->longpress_interval, atof(result->value));
-               _D("long press interval is %.3f", c->longpress_interval);
-       } else if (MATCH(result->name, "LightSensorSamplingInterval")) {
-               SET_CONF(c->lightsensor_interval, atof(result->value));
-               _D("lightsensor interval is %.3f", c->lightsensor_interval);
-       } else if (MATCH(result->name, "LCDOffTimeout")) {
-               SET_CONF(c->lcdoff_timeout, atoi(result->value));
-               _D("lcdoff timeout is %d ms", c->lcdoff_timeout);
-       } else if (MATCH(result->name, "BrightnessChangeStep")) {
-               SET_CONF(c->brightness_change_step, atoi(result->value));
-               _D("brightness change step is %d", c->brightness_change_step);
-       } else if (MATCH(result->name, "LCDAlwaysOn")) {
-               c->lcd_always_on = (MATCH(result->value, "yes") ? 1 : 0);
-               _D("LCD always on is %d", c->lcd_always_on);
-       } else if (MATCH(result->name, "Dimming")) {
-               c->dimming = (MATCH(result->value, "yes") ? 1 : 0);
-                _D("Dimming is %d", c->dimming);
-       } else if (MATCH(result->name, "ChangedFrameRateAllowed")) {
-               if (strstr(result->value, "setting")) {
-                       c->framerate_app[REFRESH_SETTING] = 1;
-                       _D("framerate app is Setting");
-               }
-               if (strstr(result->value, "all")) {
-                       memset(c->framerate_app, 1, sizeof(c->framerate_app));
-                       _D("framerate app is All");
+       if (MATCH(result->section, "KillableDaemon") && MATCH(result->name, "KillableList")) {
+               name = strndup(result->value, PATH_MAX - 1);
+               if (!name) {
+                       _E("Not enough memory.");
+                       return -ENOMEM;
                }
-       } else if (MATCH(result->name, "ControlDisplay")) {
-               c->control_display = (MATCH(result->value, "yes") ? 1 : 0);
-               _D("ControlDisplay is %d", c->control_display);
-       } else if (MATCH(result->name, "PowerKeyDoublePressSupport")) {
-               c->powerkey_doublepress = (MATCH(result->value, "yes") ? 1 : 0);
-               _D("PowerKeyDoublePressSupport is %d", c->powerkey_doublepress);
-       } else if (MATCH(result->name, "AccelSensorOn")) {
-               c->accel_sensor_on = (MATCH(result->value, "yes") ? 1 : 0);
-               _D("AccelSensorOn is %d", c->accel_sensor_on);
-       } else if (MATCH(result->name, "ContinuousSampling")) {
-               c->continuous_sampling = (MATCH(result->value, "yes") ? 1 : 0);
-               _D("ContinuousSampling is %d", c->continuous_sampling);
-       } else if (MATCH(result->name, "TimeoutEnable")) {
-               c->timeout_enable = (MATCH(result->value, "yes") ? true : false);
-               _D("Timeout is %s", c->timeout_enable ? "enalbed" : "disabled");
-       } else if (MATCH(result->name, "InputSupport")) {
-               c->input_support = (MATCH(result->value, "yes") ? true : false);
-               _D("Input is %s", c->input_support ? "supported" : "NOT supported");
-       } else if (MATCH(result->name, "LockCheckTimeout")) {
-               SET_CONF(c->lockcheck_timeout, atoi(result->value));
-               _D("LockCheckTimeout is %d", c->lockcheck_timeout);
-       } else if (MATCH(result->name, "AODTSP")) {
-               c->aod_tsp = (MATCH(result->value, "yes") ? true : false);
-               _D("TSP control at is %d at aod", c->aod_tsp);
-       } else if (MATCH(result->name, "SleepSupport")) {
-               c->sleep_support = (MATCH(result->value, "yes") ? true : false);
-               _D("SleepSupport is %d", c->sleep_support);
+
+               CRITICAL_LOG("Add %s to killable daemon list.", name);
+               SYS_G_LIST_APPEND(display_lock_killable_daemon, name);
        }
 
        return 0;
 }
 
-static gboolean delayed_init_dpms(gpointer data)
+static gboolean delayed_dpms_init_done(gpointer data)
 {
        int timeout;
 
-       if (!init_dpms())
+       if (!display_panel_init_dpms())
                return G_SOURCE_CONTINUE;
 
-       switch (pm_cur_state) {
+       switch (get_pm_cur_state()) {
        case S_NORMAL:
        case S_LCDDIM:
                lcd_on_procedure(LCD_NORMAL, LCD_ON_BY_EVENT);
@@ -2241,13 +1922,13 @@ static gboolean delayed_init_dpms(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;
@@ -2256,13 +1937,19 @@ static gboolean delayed_init_dpms(gpointer data)
        return G_SOURCE_REMOVE;
 }
 
-static void add_timer_for_init_dpms(void)
+static void add_timer_for_dpms_init(void)
 {
-       guint id = g_timeout_add(500/* milliseconds */, delayed_init_dpms, NULL);
+       guint id = g_timeout_add(500/* milliseconds */, delayed_dpms_init_done, NULL);
        if (id == 0)
-               _E("Failed to add init_dpms timeout.");
+               _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
  *
@@ -2270,6 +1957,16 @@ static void add_timer_for_init_dpms(void)
 static int display_probe(void *data)
 {
        int ret;
+       struct display_plugin *dp = (struct display_plugin *) 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);
 
        /**
         * load display service
@@ -2281,8 +1978,15 @@ static int display_probe(void *data)
                return ret;
 
        /* display_plugin instance initialization */
-       init_pm_internal();
-       disp_plgn.device_flags_to_string = __device_flags_to_string;
+       init_pm_internal(data);
+       disp_plgn->device_flags_to_string = __device_flags_to_string;
+
+       if (battery_plgn->handle) {
+               fp_get_charging_status = dlsym(battery_plgn->handle, "get_charging_status");
+               if (!fp_get_charging_status)
+                        _E("Failed to obtain address of get_charging_status");
+       } else
+               _I("There is no battery module.");
 
        return 0;
 }
@@ -2290,9 +1994,37 @@ static int display_probe(void *data)
 static int input_init_handler(void)
 {
        if (!display_conf.input_support)
-               return 0;
+               remove_device_by_devname("input");
+
+       return 0;
+}
 
-       g_idle_add(init_input, NULL);
+static int power_resume_from_echomem_callback(void *data)
+{
+       system_wakeup_flag = true;
+       if (check_wakeup_src() == EVENT_DEVICE)
+               /* system waked up by devices */
+               states[get_pm_cur_state()].trans(EVENT_DEVICE);
+       else
+               /* system waked up by user input */
+               states[get_pm_cur_state()].trans(EVENT_INPUT);
+
+       return 0;
+}
+
+static int poweroff_triggered_callback(void *udata)
+{
+       int val = (int)(intptr_t) udata;
+
+       switch (val) {
+       case VCONFKEY_SYSMAN_POWER_OFF_NONE:
+               clear_pm_status_flag(PWROFF_FLAG);
+               break;
+       case VCONFKEY_SYSMAN_POWER_OFF_DIRECT:
+       case VCONFKEY_SYSMAN_POWER_OFF_RESTART:
+               set_pm_status_flag(PWROFF_FLAG);
+               break;
+       }
 
        return 0;
 }
@@ -2307,8 +2039,8 @@ static void esd_action(void)
 
        if (!check_default(touchscreen_ops))
                touchscreen_ops->stop(NORMAL_MODE);
-       backlight_ops.off(NORMAL_MODE);
-       backlight_ops.on(NORMAL_MODE);
+       display_panel_set_panel_state_by_off_state(NORMAL_MODE);
+       display_panel_set_panel_state_by_on_state(NORMAL_MODE);
        if (!check_default(touchscreen_ops))
                touchscreen_ops->start(NORMAL_MODE);
 }
@@ -2342,7 +2074,7 @@ static void sec_dsim_uevent_changed(struct udev_device *dev)
        if (!fnmatch(SEC_DSIM_PATH, devpath, 0)) {
                action = udev_device_get_action(dev);
                if (!strcmp(action, UDEV_CHANGE)) {
-                       ret = dbus_handle_emit_dbus_signal(NULL,
+                       ret = gdbus_signal_emit(NULL,
                                        DEVICED_PATH_DISPLAY,
                                        DEVICED_INTERFACE_DISPLAY,
                                        DISPLAY_DETACH_STR,
@@ -2379,20 +2111,27 @@ static void display_init(void *data)
        power_saving_func = default_saving_mode;
 
        /* load configutation */
-       ret = config_parse(DISPLAY_CONF_FILE, display_load_config, &display_conf);
+       ret = display_load_config(&display_conf);
        if (ret < 0)
                _W("Failed to load '%s', use default value: %d",
                    DISPLAY_CONF_FILE, ret);
 
+       ret = config_parse(POWERLOCK_CONF_FILE, powerlock_load_config, NULL);
+       /* config file may not exist */
+       if (ret < 0 && ret != -ENOENT)
+               _W("Failed to load %s, %d.", POWERLOCK_CONF_FILE, ret);
+       initialized_killable_daemon_list = true;
+
        register_kernel_uevent_control(&lcd_uevent_ops);
        register_kernel_uevent_control(&sec_dsim_uevent_ops);
 
-       register_notifier(DEVICE_NOTIFIER_BOOTING_DONE, booting_done);
-       register_notifier(DEVICE_NOTIFIER_PROCESS_BACKGROUND, process_background);
-       register_notifier(DEVICE_NOTIFIER_PROCESS_FOREGROUND, process_foreground);
-       register_notifier(DEVICE_NOTIFIER_BATTERY_HEALTH, battery_health_changed);
-       register_notifier(DEVICE_NOTIFIER_DISPLAY_BRIGHTNESS, display_brightness_changed);
+       register_notifier(DEVICE_NOTIFIER_DELAYED_INIT, delayed_init_done);
+       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_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);
 
        init_save_userlock();
 
@@ -2408,7 +2147,6 @@ static void display_init(void *data)
                        break;
                case INIT_POLL:
                        _I("input init");
-                       pm_callback = poll_callback;
                        ret = input_init_handler();
 
                        pm_lock_detector_init();
@@ -2433,7 +2171,7 @@ static void display_init(void *data)
                check_seed_status();
 
                /* In smd test, TSP should be turned off if display panel is not existed. */
-               if (backlight_ops.get_lcd_power() == -ENOENT) {
+               if (display_panel_get_dpms_cached_state() == -ENOENT) {
                        _I("Display panel is not existed.");
                        lcd_direct_control(DPMS_OFF, NORMAL_MODE);
                        exit_lcd_operation();
@@ -2443,40 +2181,39 @@ static void display_init(void *data)
                 * since display manager can be launched later than deviced.
                 * In the case, display cannot be turned on at the first booting */
                // wm_ready = check_wm_ready();
-               if (init_dpms()) {
-                       lcd_on_procedure(LCD_NORMAL, LCD_ON_BY_EVENT);
+               if (display_panel_init_dpms()) {
                        if (is_lcdon_blocked() != LCDON_BLOCK_NONE)
                                lcd_off_procedure(LCD_OFF_BY_EVENT);
+                       else
+                               lcd_on_procedure(LCD_NORMAL, LCD_ON_BY_EVENT);
                } else {
-                       add_timer_for_init_dpms();
+                       add_timer_for_dpms_init();
                }
 
                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 */
                        _I("Start Power managing without noti");
-                       if (power_ops.get_power_lock_support()) {
-                               broadcast_pm_wakeup();
-                               power_ops.power_lock();
-                       }
+                       power_request_change_state_strict(DEVICED_POWER_STATE_SLEEP, DEVICED_POWER_STATE_NORMAL,
+                               HAL_DEVICE_POWER_TRANSITION_REASON_UNKNOWN, NULL);
                        /*
                         * Lock lcd off until booting is done.
                         * deviced guarantees all booting script is executing.
                         * Last script of booting unlocks this suspend blocking state.
                         */
-                       if (disp_plgn.pm_lock_internal)
-                               disp_plgn.pm_lock_internal(INTERNAL_LOCK_BOOTING, LCD_OFF,
-                                               STAY_CUR_STATE, BOOTING_DONE_WATING_TIME);
+                       if (disp_plgn->pm_lock_internal)
+                               disp_plgn->pm_lock_internal(INTERNAL_LOCK_BOOTING, LCD_OFF,
+                                               STAY_CUR_STATE, DELAYED_INIT_WATING_TIME);
 
-                       /* initial display state right after the booting done */
+                       /* Initial display state right after the booting done */
                        if (is_lcdon_blocked())
-                               pm_cur_state = S_LCDOFF;
+                               set_pm_cur_state(S_LCDOFF);
                        else
-                               pm_cur_state = S_NORMAL;
-                       ret = vconf_set_int(VCONFKEY_PM_STATE, pm_cur_state);
+                               set_pm_cur_state(S_NORMAL);
+                       ret = vconf_set_int(VCONFKEY_PM_STATE, get_pm_cur_state());
                        if (ret < 0)
                                _E("Failed to set vconf value for pm cur state: %d", vconf_get_ext_errno());
 
@@ -2487,8 +2224,8 @@ static void display_init(void *data)
                                if (timeout < SEC_TO_MSEC(DEFAULT_NORMAL_TIMEOUT))
                                        timeout = SEC_TO_MSEC(DEFAULT_NORMAL_TIMEOUT);
 
-                               if (disp_plgn.pm_lock_internal)
-                                       disp_plgn.pm_lock_internal(INTERNAL_LOCK_BOOTING, LCD_NORMAL,
+                               if (disp_plgn->pm_lock_internal)
+                                       disp_plgn->pm_lock_internal(INTERNAL_LOCK_BOOTING, LCD_NORMAL,
                                                        STAY_CUR_STATE, timeout);
                        }
 
@@ -2497,6 +2234,8 @@ static void display_init(void *data)
                if (display_conf.input_support)
                        if (CHECK_OPS(keyfilter_ops, init))
                                keyfilter_ops->init();
+
+               set_display_init_direction(display_conf.display_init_direction);
        }
 }
 
@@ -2507,10 +2246,10 @@ static void display_exit(void *data)
        status = DEVICE_OPS_STATUS_STOP;
 
        /* Set current state to S_NORMAL */
-       pm_cur_state = S_NORMAL;
-       set_setting_pmstate(pm_cur_state);
+       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();
@@ -2529,19 +2268,19 @@ static void display_exit(void *data)
                        exit_sysfs();
                        break;
                case INIT_POLL:
-                       unregister_notifier(DEVICE_NOTIFIER_BOOTING_DONE, booting_done);
-                       unregister_notifier(DEVICE_NOTIFIER_PROCESS_BACKGROUND, process_background);
-                       unregister_notifier(DEVICE_NOTIFIER_PROCESS_FOREGROUND, process_foreground);
-                       unregister_notifier(DEVICE_NOTIFIER_BATTERY_HEALTH, battery_health_changed);
-                       unregister_notifier(DEVICE_NOTIFIER_DISPLAY_BRIGHTNESS, display_brightness_changed);
+                       unregister_notifier(DEVICE_NOTIFIER_DELAYED_INIT, delayed_init_done);
+                       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_POWER_RESUME_FROM_ECHO_MEM, power_resume_from_echomem_callback);
 
-                       exit_input();
                        break;
                }
        }
 
        exit_lcd_operation();
        free_lock_info_list();
+       free_killable_daemon_list();
 
        /* free display service */
        display_service_free();
@@ -2557,10 +2296,10 @@ static int display_start(enum device_flags flags)
        if (flags & NORMAL_MODE) {
                if (flags & LCD_PANEL_OFF_MODE)
                        /* standby on */
-                       backlight_ops.standby(true);
+                       display_panel_set_panel_state_by_standby_state(true);
                else
                        /* normal lcd on */
-                       backlight_ops.on(flags);
+                       display_panel_set_panel_state_by_on_state(flags);
                FIND_DISPLAY(enhance_ops, "enhance");
                if (enhance_ops && enhance_ops->func)
                        enhance_ops->func(RESTORE_ENHANCE_OUTDOOR, &on);
@@ -2586,7 +2325,7 @@ static int display_stop(enum device_flags flags)
 {
        /* NORMAL MODE */
        if (flags & NORMAL_MODE || flags & FORCE_OFF_MODE) {
-               backlight_ops.off(flags);
+               display_panel_set_panel_state_by_off_state(flags);
                return 0;
        }
 
@@ -2607,9 +2346,9 @@ static int display_status(void)
        return status;
 }
 
-static const struct device_ops display_device_ops = {
-       .priority = DEVICE_PRIORITY_HIGH,
-       DECLARE_NAME_LEN("display"),
+static const struct device_ops display_plugin_device_ops = {
+       .disable_auto_init = true,
+       DECLARE_NAME_LEN("display-plugin"),
        .probe    = display_probe,
        .init     = display_init,
        .exit     = display_exit,
@@ -2618,7 +2357,22 @@ static const struct device_ops display_device_ops = {
        .status   = display_status,
 };
 
-DEVICE_OPS_REGISTER(&display_device_ops)
+DEVICE_OPS_REGISTER(&display_plugin_device_ops)
+
+static void __CONSTRUCTOR__ initialize(void)
+{
+       disp_plgn = get_var_display_plugin();
+       if (!disp_plgn)
+               _E("Failed to get display plugin variable.");
+
+       backlight_ops = get_var_backlight_ops();
+       if (!backlight_ops)
+               _E("Failed to get backlight operator variable.");
+
+       battery_plgn = get_var_battery_plugin();
+       if (!battery_plgn)
+                _E("Failed to get battery plugin variable.");
+}
 
 /**
  * @}