change mem base from mmap
authorHyoyoung Chang <hyoyoung.chang@samsung.com>
Mon, 31 Jan 2011 07:31:47 +0000 (16:31 +0900)
committerHyoyoung Chang <hyoyoung.chang@samsung.com>
Mon, 31 Jan 2011 07:31:47 +0000 (16:31 +0900)
src/clipdrawer.c
src/clipdrawer.h
src/storage.c
src/storage.h
src/xcnphandler.c

index b6be718..6e054c7 100644 (file)
@@ -48,9 +48,9 @@ int clipdrawer_update_contents(void *data)
                if (pos < 0)
                        pos = pos+HISTORY_QUEUE_MAX_ITEMS;
 
-               if (get_item_contents_by_pos(pos) != NULL && strlen(get_item_contents_by_pos(pos)) > 0)
+               if (clipdrawer_get_item_data(ad, pos) != NULL && strlen(clipdrawer_get_item_data(ad, pos)) > 0)
                {
-                       unesc = clipdrawer_get_plain_string_from_escaped(get_item_contents_by_pos(pos));
+                       unesc = clipdrawer_get_plain_string_from_escaped(clipdrawer_get_item_data(ad, pos));
                        unesc = unesc ? unesc : "";
                        elm_list_item_append(ad->txtlist, unesc, NULL, NULL, NULL, ad);
                        free(unesc);
@@ -330,8 +330,6 @@ static void _grid_click_paste(void *data, Evas_Object *obj, void *event_info)
        sgobj = elm_gengrid_selected_item_get(ad->hig);
        griditem_t *ti = NULL;
        ti = elm_gengrid_item_data_get(sgobj);
-
-       fprintf(stderr, "## grid_click_paste = 0x%x\n", event_info);
 }
 
 void _grid_del(const void *data, Evas_Object *obj)
@@ -344,6 +342,36 @@ void _grid_del(const void *data, Evas_Object *obj)
        free(ti);
 }
 
+char* clipdrawer_get_item_data(void *data, int pos)
+{
+       struct appdata *ad = data;
+       griditem_t *ti = NULL;
+       griditem_t *newgi = NULL;
+       int count = 0;
+
+       if (pos < 0 || pos > ad->hicount)
+               return NULL;
+
+       Elm_Gengrid_Item *item = elm_gengrid_first_item_get(ad->hig);
+       while (item)    
+       {
+               ti = elm_gengrid_item_data_get(item);
+               if (count == pos)
+               {
+                       if (!ti)
+                               break;
+                       if (ti->itype == GI_TEXT)
+                               return (char*)eina_strbuf_string_get(ti->istrdata);
+                       else
+                               return ti->ipathdata;
+               }
+               count++;
+               item = elm_gengrid_item_next_get(item);      
+       }
+
+       return NULL;
+}
+
 // FIXME: how to remove calling g_get_main_appdata()? 
 //        it's mainly used at 'clipdrawer_image_item'
 int clipdrawer_add_item(char *idata, int type)
@@ -381,7 +409,6 @@ int clipdrawer_add_item(char *idata, int type)
                        }
                        item = elm_gengrid_item_next_get(item);      
                }
-
                newgi->ipathdata = eina_stringshare_add(idata);
        }
 
index 7f8ce7d..3a5399e 100644 (file)
@@ -24,6 +24,7 @@ void clipdrawer_lower_view(void *data);
 
 const char* clipdrawer_get_plain_string_from_escaped(char *escstr);
 
+char *clipdrawer_get_item_data(void *data, int pos);
 int clipdrawer_add_item(char *idata, int type);
 
 void clipdrawer_paste_textonly_set(void *data, Eina_Bool textonly);
index 1d15d90..e654522 100644 (file)
@@ -1,4 +1,5 @@
 #include "common.h"
+#include "cbhm_main.h"
 #include "storage.h"
 
 /*
 #define STORAGE_BODY_SIZE (STORAGE_MAX_ITEMS * BODY_ITEM_SIZE) 
 #define TOTAL_STORAGE_SIZE (STORAGE_HEADER_SIZE+STORAGE_BODY_SIZE)
 
-#define GET_ITEM_ADDR_BY_POSITION(map, pos) (map+STORAGE_HEADER_SIZE+pos*BODY_ITEM_SIZE)
+#define GET_HEADER_ADDR_BY_POSITION(pos) (STORAGE_HEADER_SIZE+pos*(HEADER_ITEM_SIZE+BODY_ITEM_SIZE))
+#define GET_BODY_ADDR_BY_POSITION(pos) (GET_HEADER_ADDR_BY_POSITION(pos)+HEADER_ITEM_SIZE)
 
-static int g_storage_fd = 0;
+static FILE *g_storage_file = NULL;
 static unsigned int g_storage_serial_number = 0;
-static char *g_map = NULL;
 
-int init_storage()
+int init_storage(void *data)
 {
+       struct appdata *ad = data;
+
        int i;
        int result = 0;
 
-       if (g_storage_fd != 0)
+       if (g_storage_file)
                return 1;
 
-       g_storage_fd = open(STORAGE_FILEPATH, O_RDWR | O_CREAT, (mode_t)0600);
-       if (g_storage_fd == -1)
-       {
-               g_storage_fd = 0;
-               close_storage();
-               DTRACE("Error : failed openning file for writing\n");
-               return -1;
-       }
-
-    result = lseek(g_storage_fd, TOTAL_STORAGE_SIZE-1, SEEK_SET);
-    if (result == -1) 
-       {
-               close_storage();
-               DTRACE("Error : failed moving file position to file's end\n");
-               return -1;
-    }
-    
-    result = write(g_storage_fd, "", 1);
-    if (result != 1) 
-       {
-               close_storage();
+       g_storage_file = fopen(STORAGE_FILEPATH, "r+");
+       if (!g_storage_file)
+       {  // making data savefile
+               g_storage_file = fopen(STORAGE_FILEPATH, "w+");
+
+               if (!g_storage_file)
+               {
+                       close_storage(ad);
+                       DTRACE("Error : failed openning file for writing\n");
+                       return -1;
+               }
+
+               result = fseek(g_storage_file, TOTAL_STORAGE_SIZE-1, SEEK_SET);
+               if (!result) 
+               {
+                       close_storage(ad);
+                       DTRACE("Error : failed moving file position to file's end\n");
+                       return -1;
+               }
+
+               result = fputc(0, g_storage_file);
+               if (result == EOF)
+               {
                DTRACE("Error : failed writing to file's end\n");
                return -1;
-    }
-
-       g_map = mmap(0, TOTAL_STORAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, g_storage_fd, 0);
-       if (g_map == MAP_FAILED)
-       {
-               close_storage();
-               DTRACE("Error : failed mmapping the file\n");
-               return -1;
+               }
        }
 
-       // FIXME : do not unconditionaly initialize, maybe old data can be
-       // at here
-       int *header = g_map;
-       for (i = 0; i < STORAGE_MAX_ITEMS; i++)
-       {
-               char d = 0;
-               header[i] = 0;
-               memcpy(GET_ITEM_ADDR_BY_POSITION(g_map, i), &d, 1);
-       }
        DTRACE("Success : storage init is done\n");
 
        g_storage_serial_number = 0;
@@ -81,82 +70,61 @@ int init_storage()
        return 0;
 }
 
-int sync_storage()
+int sync_storage(void *data)
 {
-       if (g_map == NULL)
+//     struct appdata *ad = data;
+
+       if (!g_storage_file)
        {
-               DTRACE("g_map is null\n");
+               DTRACE("g_storage_file is null\n");
                return -1;
        }
-       msync(g_map, TOTAL_STORAGE_SIZE, MS_ASYNC);
+       fsync(g_storage_file);
 
        return 0;
 }
 
-int get_total_storage_size()
+unsigned int get_storage_serial_code(void *data)
 {
-       return TOTAL_STORAGE_SIZE;
-}
+//     struct appdata *ad = data;
 
-unsigned int get_storage_serial_code()
-{
        return g_storage_serial_number;
 }
 
-int adding_item_to_storage(int pos, char *data)
+int adding_item_to_storage(void *data, int pos, char *idata)
 {
-       int *header = get_storage_start_addr();
+//     struct appdata *ad = data;
 
-       if (g_map == NULL)
+       if (!g_storage_file)
        {
-               DTRACE("g_map is null");
+               DTRACE("g_storage_file is null\n");
                return -1;
        }
-       // saving relative addr at header
-       header[pos] = STORAGE_HEADER_SIZE+pos*BODY_ITEM_SIZE; 
-       memcpy(GET_ITEM_ADDR_BY_POSITION(g_map, pos), data, BODY_ITEM_SIZE);
+
+       int result;
+       result = fseek(g_storage_file, GET_HEADER_ADDR_BY_POSITION(pos), SEEK_SET);
+       // FIXME : replace from fprintf to fwrite
+       fprintf(g_storage_file, "%d", strlen(idata));
+       fprintf(g_storage_file, "%s", idata);
+       
        g_storage_serial_number++;
        return 0;
 }
 
-char *get_storage_start_addr()
-{
-       return g_map;
-}
-
-int get_item_counts()
+int get_item_counts(void *data)
 {
-       int i, count;
-       int *header = get_storage_start_addr();
+       struct appdata *ad = data;
 
-       count = 0;
-       for (i = 0; i < STORAGE_MAX_ITEMS; i++)
-       {
-               if (header[i] != 0)
-                       count++;
-       }
-       return count;
-}
-
-char *get_item_contents_by_pos(int pos)
-{
-       if (g_map == NULL)
-       {
-               DTRACE("g_map is null");
-               return NULL;
-       }
-       return GET_ITEM_ADDR_BY_POSITION(g_map, pos);
+       return ad->hicount;
 }
 
-int close_storage()
+int close_storage(void *data)
 {
-       if (g_map)
-               munmap(g_map, TOTAL_STORAGE_SIZE);
-       g_map = NULL;
+       struct appdata *ad = data;
 
-       if (g_storage_fd)
-               close(g_storage_fd);
-       g_storage_fd = 0;
+       if (g_storage_file)
+               fclose(g_storage_file);
+       g_storage_file = NULL;
        g_storage_serial_number = 0;
 
        return 0;
index 2b25985..26bf145 100644 (file)
@@ -6,14 +6,12 @@
 #include <fcntl.h>
 #include <sys/mman.h>
 
-int init_storage();
-int sync_storage();
-unsigned int get_storage_serial_code();
-int adding_item_to_storage(int pos, char *data);
-char *get_storage_start_addr();
-int get_item_counts();
-char *get_item_contents_by_pos(int pos);
-int close_storage();
+int init_storage(void *data);
+int sync_storage(void *data);
+unsigned int get_storage_serial_code(void *data);
+int adding_item_to_storage(void *data, int pos, char *idata);
+int get_item_counts(void *data);
+int close_storage(void *data);
 
 int check_regular_file(char *path);
 
index c80a3dc..5094666 100755 (executable)
@@ -115,20 +115,21 @@ int add_to_storage_buffer(void *data, char *src, int len)
        if (len <= 0)
                return -1;
 
-       if (g_lastest_content == NULL)
-               g_lastest_content = malloc(sizeof(char)*(HISTORY_QUEUE_ITEM_SIZE));
+//     if (g_lastest_content == NULL)
+//             g_lastest_content = malloc(sizeof(char)*(HISTORY_QUEUE_ITEM_SIZE));
        if (g_history_pos >= HISTORY_QUEUE_MAX_ITEMS)
                g_history_pos = 0;
 
        // FIXME: remove g_lasteset_content
        //strcpy(g_lastest_content, src);
-       memcpy(g_lastest_content, src, len);
-       g_lastest_content[len] = '\0';
-       adding_item_to_storage(g_history_pos, g_lastest_content);
+//     memcpy(g_lastest_content, src, len);
+//     g_lastest_content[len] = '\0';
+       g_lastest_content = src;
+       adding_item_to_storage(ad, g_history_pos, g_lastest_content);
        increment_current_history_position();
 
        int nserial = 0;
-       nserial = get_storage_serial_code();
+       nserial = get_storage_serial_code(ad);
        Atom atomCbhmSerial = XInternAtom(g_disp, "CBHM_SERIAL_NUMBER", False);
        XChangeProperty(g_disp, g_evtwin, atomCbhmSerial, XA_INTEGER,
                                        32, PropModeReplace,
@@ -138,8 +139,10 @@ int add_to_storage_buffer(void *data, char *src, int len)
        return 0;
 }
 
-int print_storage_buffer()
+int print_storage_buffer(void *data)
 {
+       struct appdata *ad = data;
+
        int pos;
        int i = 0;
        for (i = 0; i < HISTORY_QUEUE_MAX_ITEMS; i++)
@@ -147,7 +150,8 @@ int print_storage_buffer()
                pos = get_current_history_position()+i;
                if (pos > HISTORY_QUEUE_MAX_ITEMS-1)
                        pos = pos-HISTORY_QUEUE_MAX_ITEMS;
-               DTRACE("%d: %s\n", i, get_item_contents_by_pos(pos) != NULL ? get_item_contents_by_pos(pos) : "NULL");
+               DTRACE("%d: %s\n", i, 
+                          clipdrawer_get_item_data(ad, pos) != NULL ? clipdrawer_get_item_data(ad, pos) : "NULL");
        }
 }
 
@@ -264,12 +268,12 @@ int get_selection_content(void *data)
                check_regular_file(cbbuf+7))
        {
                DTRACE("clipdrawer add path = %s\n", cbbuf+7);
-               clipdrawer_add_item(cbbuf+7, GI_IMAGE);
+               clipdrawer_add_item(ad, cbbuf+7, GI_IMAGE);
        }
        else
        {
                add_to_storage_buffer(ad, cbbuf, unesc_len);
-               clipdrawer_add_item(cbbuf, GI_TEXT);
+               clipdrawer_add_item(ad, cbbuf, GI_TEXT);
        }
        DTRACE("len = %ld, data = %s\n", unesc_len, cbbuf);
 #endif
@@ -295,7 +299,7 @@ int get_selection_content(void *data)
 #endif
 
        DTRACE("\n");
-       print_storage_buffer();
+       print_storage_buffer(ad);
        DTRACE("\n");
 
        XFree(cbbuf);
@@ -415,7 +419,7 @@ static int _cbhm_init(void *data)
 
        _set_cbhmwin_prop();
     _init_atoms();
-       init_storage();
+       init_storage(ad);
 
        DTRACE("_cbhm_init ok\n");
 
@@ -428,7 +432,9 @@ static int _cbhm_init(void *data)
 
 static void _cbhm_fini()
 {
-       close_storage();
+       struct appdata *ad = g_get_main_appdata();
+
+       close_storage(ad);
 
        return;
 }
@@ -523,7 +529,7 @@ static int _xclient_msg_cb(void *data, int ev_type, void *event)
 
        if (strcmp("get count", ev->data.b) == 0)
        {
-               int icount = get_item_counts();
+               int icount = get_item_counts(ad);
                char countbuf[10];
                DTRACE("## cbhm count : %d\n", icount);
                sprintf(countbuf, "%d", icount);
@@ -550,12 +556,12 @@ static int _xclient_msg_cb(void *data, int ev_type, void *event)
                        // FIXME : handle with correct atom
                        sprintf(atomname, "CBHM_c%d", num);
                        cbhm_atoms[0] = XInternAtom(g_disp, atomname, False);
-                       if (get_item_contents_by_pos(num) != NULL)
+                       if (clipdrawer_get_item_data(ad, num) != NULL)
                        {
                                XChangeProperty(g_disp, reqwin, cbhm_atoms[0], atomUTF8String, 
                                                                8, PropModeReplace, 
-                                                               (unsigned char *) get_item_contents_by_pos(num), 
-                                                               (int) strlen(get_item_contents_by_pos(num)));
+                                                               (unsigned char *) clipdrawer_get_item_data(ad, num), 
+                                                               (int) strlen(clipdrawer_get_item_data(ad, num)));
                        }
                }
        }
@@ -568,12 +574,12 @@ static int _xclient_msg_cb(void *data, int ev_type, void *event)
                        DTRACE("## %d -> %d\n", i, pos);
                        sprintf(atomname, "CBHM_c%d", i);
                        cbhm_atoms[i] = XInternAtom(g_disp, atomname, False);
-                       if (get_item_contents_by_pos(pos) != NULL)
+                       if (clipdrawer_get_item_data(ad, pos) != NULL)
                        {
                                XChangeProperty(g_disp, reqwin, cbhm_atoms[i], atomUTF8String, 
                                                                8, PropModeReplace, 
-                                                               (unsigned char *) get_item_contents_by_pos(pos) , 
-                                                               (int) strlen(get_item_contents_by_pos(pos)));
+                                                               (unsigned char *) clipdrawer_get_item_data(ad, pos),
+                                                               (int) strlen(clipdrawer_get_item_data(ad, pos)));
                        }
                        pos--;
                        if (pos < 0)
@@ -582,6 +588,7 @@ static int _xclient_msg_cb(void *data, int ev_type, void *event)
        }
        else if (strcmp("get raw", ev->data.b) == 0)
        {
+/*
                if (get_storage_start_addr != NULL)
                {
                        XChangeProperty(g_disp, reqwin, atomCBHM_cRAW, atomUTF8String, 
@@ -589,6 +596,7 @@ static int _xclient_msg_cb(void *data, int ev_type, void *event)
                                                        (unsigned char *) get_storage_start_addr(),
                                                        (int) get_total_storage_size());
                }
+*/
        }
        else if (strncmp("show", ev->data.b, 4) == 0)
        {