Add loading HAL module before runnig API
[platform/hal/api/device.git] / src / display.c
index 75c465f..52ba9f9 100644 (file)
  * limitations under the License.
  */
 
-
 #include <hal/hal-common.h>
 
 #include "hal-display-interface.h"
 #include "common.h"
 
-static hal_backend_display_funcs *g_display_funcs = NULL;
+static hal_backend_display_funcs *hal_display_funcs = NULL;
+/*
+-1 : failed to initialize
+0  : not initialized
+1  : succeeded to initialize
+*/
+static int hal_initialized = 0;
 
 int hal_device_display_get_backend(void)
 {
        int ret;
 
-       if (g_display_funcs)
+       if (hal_display_funcs)
                return 0;
 
-       ret = hal_common_get_backend(HAL_MODULE_DEVICE_DISPLAY, (void **)&g_display_funcs);
+       ret = hal_common_get_backend(HAL_MODULE_DEVICE_DISPLAY, (void **)&hal_display_funcs);
        if (ret < 0) {
                 _E("Failed to get display backend");
+               hal_initialized = -1;
                return -EINVAL;
        }
 
+       hal_initialized = 1;
        return 0;
 }
 
 int hal_device_display_put_backend(void)
 {
-       if (!g_display_funcs)
+       if (!hal_display_funcs)
                return 0;
 
-       hal_common_put_backend(HAL_MODULE_DEVICE_DISPLAY, (void *)g_display_funcs);
-       g_display_funcs = NULL;
+       hal_common_put_backend(HAL_MODULE_DEVICE_DISPLAY, (void *)hal_display_funcs);
+       hal_display_funcs = NULL;
+       hal_initialized = 0;
 
        return 0;
 }
 
 int hal_device_display_get_max_brightness(int *brightness)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->get_max_brightness)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->get_max_brightness)
                return -ENODEV;
 
-       return g_display_funcs->get_max_brightness(brightness);
+       return hal_display_funcs->get_max_brightness(brightness);
 }
 
 int hal_device_display_get_brightness(int *brightness)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->get_brightness)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->get_brightness)
                return -ENODEV;
 
-       return g_display_funcs->get_brightness(brightness);
+       return hal_display_funcs->get_brightness(brightness);
 }
 
 int hal_device_display_set_brightness(int brightness)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->set_brightness)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->set_brightness)
                return -ENODEV;
 
-       return g_display_funcs->set_brightness(brightness);
+       return hal_display_funcs->set_brightness(brightness);
 }
 
 int hal_device_display_set_multi_brightness(int brightness, int step, int delay)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->set_multi_brightness)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->set_multi_brightness)
                return -ENODEV;
 
-       return g_display_funcs->set_multi_brightness(brightness, step, delay);
+       return hal_display_funcs->set_multi_brightness(brightness, step, delay);
 }
 
 int hal_device_display_get_auto_brightness(float lmax, float lmin, float light, int *brightness)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->get_auto_brightness)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->get_auto_brightness)
                return -ENODEV;
 
-       return g_display_funcs->get_auto_brightness(lmax, lmin, light, brightness);
+       return hal_display_funcs->get_auto_brightness(lmax, lmin, light, brightness);
 }
 
 int hal_device_display_get_state(enum display_state *state)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->get_state)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->get_state)
                return -ENODEV;
 
-       return g_display_funcs->get_state(state);
+       return hal_display_funcs->get_state(state);
 }
 
 int hal_device_display_set_state(enum display_state state)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->set_state)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->set_state)
                return -ENODEV;
 
-       return g_display_funcs->set_state(state);
+       return hal_display_funcs->set_state(state);
 }
 
 int hal_device_display_get_image_effect(enum display_image_effect *effect)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->get_image_effect)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->get_image_effect)
                return -ENODEV;
 
-       return g_display_funcs->get_image_effect(effect);
+       return hal_display_funcs->get_image_effect(effect);
 }
 
 int hal_device_display_set_image_effect(enum display_image_effect effect)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->set_image_effect)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->set_image_effect)
                return -ENODEV;
 
-       return g_display_funcs->set_image_effect(effect);
+       return hal_display_funcs->set_image_effect(effect);
 }
 
 int hal_device_display_get_panel_mode(enum display_panel_mode *mode)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->get_panel_mode)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->get_panel_mode)
                return -ENODEV;
 
-       return g_display_funcs->get_panel_mode(mode);
+       return hal_display_funcs->get_panel_mode(mode);
 }
 
 int hal_device_display_set_panel_mode(enum display_panel_mode mode)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->set_panel_mode)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->set_panel_mode)
                return -ENODEV;
 
-       return g_display_funcs->set_panel_mode(mode);
+       return hal_display_funcs->set_panel_mode(mode);
 }
 
 int hal_device_display_get_aod_mode(enum display_aod_mode *mode)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->get_aod_mode)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->get_aod_mode)
                return -ENODEV;
 
-       return g_display_funcs->get_aod_mode(mode);
+       return hal_display_funcs->get_aod_mode(mode);
 }
 
 int hal_device_display_get_aod_brightness(int *max, int *normal, int *min, int *charging)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->get_aod_brightness)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->get_aod_brightness)
                return -ENODEV;
 
-       return g_display_funcs->get_aod_brightness(max, normal, min, charging);
+       return hal_display_funcs->get_aod_brightness(max, normal, min, charging);
 }
 
 int hal_device_display_get_max_frame_rate(int *rate)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->get_max_frame_rate)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->get_max_frame_rate)
                return -ENODEV;
 
-       return g_display_funcs->get_max_frame_rate(rate);
+       return hal_display_funcs->get_max_frame_rate(rate);
 }
 
 int hal_device_display_get_min_frame_rate(int *rate)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->get_min_frame_rate)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->get_min_frame_rate)
                return -ENODEV;
 
-       return g_display_funcs->get_min_frame_rate(rate);
+       return hal_display_funcs->get_min_frame_rate(rate);
 }
 
 int hal_device_display_get_frame_rate(int *rate)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->get_frame_rate)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->get_frame_rate)
                return -ENODEV;
 
-       return g_display_funcs->get_frame_rate(rate);
+       return hal_display_funcs->get_frame_rate(rate);
 }
 
 int hal_device_display_set_frame_rate(int rate)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->set_frame_rate)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->set_frame_rate)
                return -ENODEV;
 
-       return g_display_funcs->set_frame_rate(rate);
+       return hal_display_funcs->set_frame_rate(rate);
 }