Sync with the latest one
[platform/framework/web/livebox-viewer.git] / src / fb.c
index b1d3bba..69c5dde 100644 (file)
--- a/src/fb.c
+++ b/src/fb.c
@@ -37,7 +37,6 @@
 #include "debug.h"
 #include "util.h"
 #include "fb.h"
-#include "critical_log.h"
 
 int errno;
 
@@ -54,7 +53,7 @@ struct fb_info {
 struct buffer { /*!< Must has to be sync with slave & provider */
        enum {
                CREATED = 0x00beef00,
-               DESTROYED = 0x00dead00,
+               DESTROYED = 0x00dead00
        } state;
        enum buffer_type type;
        int refcnt;
@@ -106,6 +105,11 @@ int fb_fini(void)
        return 0;
 }
 
+static inline void update_fb_size(struct fb_info *info)
+{
+       info->bufsz = info->w * info->h * s_info.depth;
+}
+
 static inline int sync_for_file(struct fb_info *info)
 {
        int fd;
@@ -113,8 +117,9 @@ static inline int sync_for_file(struct fb_info *info)
 
        buffer = info->buffer;
 
-       if (!buffer) /* Ignore this sync request */
+       if (!buffer) /* Ignore this sync request */
                return LB_STATUS_SUCCESS;
+       }
 
        if (buffer->state != CREATED) {
                ErrPrint("Invalid state of a FB\n");
@@ -122,7 +127,7 @@ static inline int sync_for_file(struct fb_info *info)
        }
 
        if (buffer->type != BUFFER_TYPE_FILE) {
-               DbgPrint("Invalid buffer\n");
+               ErrPrint("Invalid buffer\n");
                return LB_STATUS_SUCCESS;
        }
 
@@ -143,7 +148,9 @@ static inline int sync_for_file(struct fb_info *info)
 
        if (read(fd, buffer->data, info->bufsz) != info->bufsz) {
                ErrPrint("read: %s\n", strerror(errno));
-               close(fd);
+               if (close(fd) < 0) {
+                       ErrPrint("close: %s\n", strerror(errno));
+               }
 
                /*!
                 * \note
@@ -155,7 +162,9 @@ static inline int sync_for_file(struct fb_info *info)
                return LB_STATUS_SUCCESS;
        }
 
-       close(fd);
+       if (close(fd) < 0) {
+               ErrPrint("close: %s\n", strerror(errno));
+       }
        return LB_STATUS_SUCCESS;
 }
 
@@ -166,8 +175,9 @@ static inline __attribute__((always_inline)) int sync_for_pixmap(struct fb_info
        XImage *xim;
 
        buffer = info->buffer;
-       if (!buffer) /*!< Ignore this sync request */
+       if (!buffer) /*!< Ignore this sync request */
                return LB_STATUS_SUCCESS;
+       }
 
        if (buffer->state != CREATED) {
                ErrPrint("Invalid state of a FB\n");
@@ -175,7 +185,7 @@ static inline __attribute__((always_inline)) int sync_for_pixmap(struct fb_info
        }
 
        if (buffer->type != BUFFER_TYPE_PIXMAP) {
-               DbgPrint("Invalid buffer\n");
+               ErrPrint("Invalid buffer\n");
                return LB_STATUS_SUCCESS;
        }
 
@@ -197,11 +207,17 @@ static inline __attribute__((always_inline)) int sync_for_pixmap(struct fb_info
        }
 
        if (info->handle == 0) {
-               DbgPrint("Pixmap ID is not valid\n");
+               ErrPrint("Pixmap ID is not valid\n");
                return LB_STATUS_ERROR_INVALID;
        }
 
        if (info->bufsz == 0) {
+               /*!
+                * If the client does not acquire the buffer,
+                * This function will do nothing.
+                * It will work only if the buffer is acquired.
+                * To sync its contents.
+                */
                DbgPrint("Nothing can be sync\n");
                return LB_STATUS_SUCCESS;
        }
@@ -215,9 +231,9 @@ static inline __attribute__((always_inline)) int sync_for_pixmap(struct fb_info
        si.readOnly = False;
        si.shmaddr = shmat(si.shmid, NULL, 0);
        if (si.shmaddr == (void *)-1) {
-
-               if (shmctl(si.shmid, IPC_RMID, 0) < 0)
+               if (shmctl(si.shmid, IPC_RMID, 0) < 0) {
                        ErrPrint("shmctl: %s\n", strerror(errno));
+               }
 
                return LB_STATUS_ERROR_FAULT;
        }
@@ -231,11 +247,13 @@ static inline __attribute__((always_inline)) int sync_for_pixmap(struct fb_info
                                &si,
                                info->w, info->h);
        if (xim == NULL) {
-               if (shmdt(si.shmaddr) < 0)
+               if (shmdt(si.shmaddr) < 0) {
                        ErrPrint("shmdt: %s\n", strerror(errno));
+               }
 
-               if (shmctl(si.shmid, IPC_RMID, 0) < 0)
+               if (shmctl(si.shmid, IPC_RMID, 0) < 0) {
                        ErrPrint("shmctl: %s\n", strerror(errno));
+               }
 
                return LB_STATUS_ERROR_FAULT;
        }
@@ -251,11 +269,13 @@ static inline __attribute__((always_inline)) int sync_for_pixmap(struct fb_info
        XShmDetach(s_info.disp, &si);
        XDestroyImage(xim);
 
-       if (shmdt(si.shmaddr) < 0)
+       if (shmdt(si.shmaddr) < 0) {
                ErrPrint("shmdt: %s\n", strerror(errno));
+       }
 
-       if (shmctl(si.shmid, IPC_RMID, 0) < 0)
+       if (shmctl(si.shmid, IPC_RMID, 0) < 0) {
                ErrPrint("shmctl: %s\n", strerror(errno));
+       }
 
        return LB_STATUS_SUCCESS;
 }
@@ -295,13 +315,13 @@ struct fb_info *fb_create(const char *id, int w, int h)
 
        info = calloc(1, sizeof(*info));
        if (!info) {
-               CRITICAL_LOG("Heap: %s\n", strerror(errno));
+               ErrPrint("Heap: %s\n", strerror(errno));
                return NULL;
        }
 
        info->id = strdup(id);
        if (!info->id) {
-               CRITICAL_LOG("Heap: %s\n", strerror(errno));
+               ErrPrint("Heap: %s\n", strerror(errno));
                free(info);
                return NULL;
        }
@@ -376,10 +396,11 @@ void *fb_acquire_buffer(struct fb_info *info)
 
        if (!info->buffer) {
                if (!strncasecmp(info->id, SCHEMA_PIXMAP, strlen(SCHEMA_PIXMAP))) {
-                       info->bufsz = info->w * info->h * s_info.depth;
+                       update_fb_size(info);
+
                        buffer = calloc(1, sizeof(*buffer) + info->bufsz);
                        if (!buffer) {
-                               CRITICAL_LOG("Heap: %s\n", strerror(errno));
+                               ErrPrint("Heap: %s\n", strerror(errno));
                                info->bufsz = 0;
                                return NULL;
                        }
@@ -396,10 +417,11 @@ void *fb_acquire_buffer(struct fb_info *info)
                         */
                        sync_for_pixmap(info);
                } else if (!strncasecmp(info->id, SCHEMA_FILE, strlen(SCHEMA_FILE))) {
-                       info->bufsz = info->w * info->h * s_info.depth;
+                       update_fb_size(info);
+
                        buffer = calloc(1, sizeof(*buffer) + info->bufsz);
                        if (!buffer) {
-                               CRITICAL_LOG("Heap: %s\n", strerror(errno));
+                               ErrPrint("Heap: %s\n", strerror(errno));
                                info->bufsz = 0;
                                return NULL;
                        }
@@ -447,8 +469,8 @@ int fb_release_buffer(void *data)
        struct buffer *buffer;
 
        if (!data) {
-               DbgPrint("buffer data == NIL\n");
-               return 0;
+               ErrPrint("buffer data == NIL\n");
+               return LB_STATUS_ERROR_INVALID;
        }
 
        buffer = container_of(data, struct buffer, data);
@@ -460,8 +482,9 @@ int fb_release_buffer(void *data)
 
        switch (buffer->type) {
        case BUFFER_TYPE_SHM:
-               if (shmdt(buffer) < 0)
+               if (shmdt(buffer) < 0) {
                        ErrPrint("shmdt: %s\n", strerror(errno));
+               }
                break;
        case BUFFER_TYPE_PIXMAP:
                buffer->refcnt--;
@@ -472,8 +495,9 @@ int fb_release_buffer(void *data)
                        buffer->state = DESTROYED;
                        free(buffer);
                
-                       if (info && info->buffer == buffer)
+                       if (info && info->buffer == buffer) {
                                info->buffer = NULL;
+                       }
                }
                break;
        case BUFFER_TYPE_FILE:
@@ -485,8 +509,9 @@ int fb_release_buffer(void *data)
                        buffer->state = DESTROYED;
                        free(buffer);
 
-                       if (info && info->buffer == buffer)
+                       if (info && info->buffer == buffer) {
                                info->buffer = NULL;
+                       }
                }
                break;
        default:
@@ -494,7 +519,7 @@ int fb_release_buffer(void *data)
                break;
        }
 
-       return 0;
+       return LB_STATUS_SUCCESS;
 }
 
 int fb_refcnt(void *data)
@@ -503,8 +528,9 @@ int fb_refcnt(void *data)
        struct shmid_ds buf;
        int ret;
 
-       if (!data)
+       if (!data) {
                return LB_STATUS_ERROR_INVALID;
+       }
 
        buffer = container_of(data, struct buffer, data);
 
@@ -555,11 +581,43 @@ int fb_get_size(struct fb_info *info, int *w, int *h)
 
 int fb_size(struct fb_info *info)
 {
-       if (!info)
+       if (!info) {
                return 0;
+       }
+
+       update_fb_size(info);
 
-       info->bufsz = info->w * info->h * s_info.depth;
        return info->bufsz;
 }
 
+int fb_type(struct fb_info *info)
+{
+       struct buffer *buffer;
+
+       if (!info) {
+               return BUFFER_TYPE_ERROR;
+       }
+
+       buffer = info->buffer;
+       if (!buffer) {
+               int type = BUFFER_TYPE_ERROR;
+               /*!
+                * \note
+                * Try to get this from SCHEMA
+                */
+               if (info->id) {
+                       if (!strncasecmp(info->id, SCHEMA_FILE, strlen(SCHEMA_FILE))) {
+                               type = BUFFER_TYPE_FILE;
+                       } else if (!strncasecmp(info->id, SCHEMA_PIXMAP, strlen(SCHEMA_PIXMAP))) {
+                               type = BUFFER_TYPE_PIXMAP;
+                       } else if (!strncasecmp(info->id, SCHEMA_SHM, strlen(SCHEMA_SHM))) {
+                               type = BUFFER_TYPE_SHM;
+                       }
+               }
+
+               return type;
+       }
+
+       return buffer->type;
+}
 /* End of a file */