FS#11399 by me: fix r26998 for text_viewer
authorfunman <funman@a1c6a512-1295-4272-9138-f99709370657>
Wed, 23 Jun 2010 13:56:08 +0000 (23 13:56 +0000)
committerfunman <funman@a1c6a512-1295-4272-9138-f99709370657>
Wed, 23 Jun 2010 13:56:08 +0000 (23 13:56 +0000)
Restore the old behaviour:

- preferences must be read-write for tv_preferences.c , read-only for
  all other modules -> use pointer to const struct
- init functions must get the plugin buffer + its size as arguments for
  easily adding new functions -> use pointer to buffer pointer and size
  to make allocation easier
- preferences meaning is private to each file and must not be known by
  tv_preferences.c -> move tv_check_header_and_footer() back in
  tv_window.c; also avoid chaining 3 times the callbacks by calling
  tv_set_preferences() only once if more than one preference needs
  changing

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@27089 a1c6a512-1295-4272-9138-f99709370657

13 files changed:
apps/plugins/text_viewer/tv_action.c
apps/plugins/text_viewer/tv_pager.c
apps/plugins/text_viewer/tv_pager.h
apps/plugins/text_viewer/tv_preferences.c
apps/plugins/text_viewer/tv_preferences.h
apps/plugins/text_viewer/tv_reader.c
apps/plugins/text_viewer/tv_reader.h
apps/plugins/text_viewer/tv_text_processor.c
apps/plugins/text_viewer/tv_text_processor.h
apps/plugins/text_viewer/tv_text_reader.c
apps/plugins/text_viewer/tv_text_reader.h
apps/plugins/text_viewer/tv_window.c
apps/plugins/text_viewer/tv_window.h

index 546ee99..88338fe 100644 (file)
 
 bool tv_init(const unsigned char *file)
 {
+    size_t size;
+
+    /* get the plugin buffer */
+    unsigned char *buf = rb->plugin_get_buffer(&size);
+
     tv_init_bookmark();
 
     /* initialize modules */
-    if (!tv_init_window())
+    if (!tv_init_window(&buf, &size))
         return false;
 
     /* load the preferences and bookmark */
index 0c10953..e7016c7 100644 (file)
@@ -85,7 +85,7 @@ static void tv_change_preferences(const struct tv_preferences *oldp)
     tv_seek(0, SEEK_SET);
 }
 
-bool tv_init_pager(void)
+bool tv_init_pager(unsigned char **buf, size_t *size)
 {
     tv_set_screen_pos(&cur_pos);
     tv_add_preferences_change_listner(tv_change_preferences);
@@ -95,7 +95,7 @@ bool tv_init_pager(void)
 
     line_pos[0] = 0;
 
-    return tv_init_reader();
+    return tv_init_reader(buf, size);
 }
 
 void tv_finalize_pager(void)
index ec1de24..27795a1 100644 (file)
 /*
  * initialize the pager module
  *
+ * [In/Out] buf
+ *          the start pointer of the buffer
+ *
+ * [In/Out] size
+ *          buffer size
+ *
  * return
  *     true  initialize success
  *     false initialize failure
  */
-bool tv_init_pager(void);
+bool tv_init_pager(unsigned char **buf, size_t *bufsize);
 
 /* finalize the pager module */
 void tv_finalize_pager(void);
index b1045fb..ec59dd8 100644 (file)
 #include "plugin.h"
 #include "tv_preferences.h"
 
-/* global preferences */
+
 static struct tv_preferences prefs;
-struct tv_preferences *preferences = &prefs;
+/* read-only preferences pointer, for access by other files */
+const struct tv_preferences * const preferences = &prefs;
 
 static int listner_count = 0;
 
@@ -66,24 +67,6 @@ static void tv_notify_change_preferences(const struct tv_preferences *oldp)
     }
 }
 
-static void tv_check_header_and_footer(void)
-{
-    if (rb->global_settings->statusbar != STATUSBAR_TOP)
-    {
-        if (preferences->header_mode == HD_SBAR)
-            preferences->header_mode = HD_NONE;
-        else if (preferences->header_mode == HD_BOTH)
-            preferences->header_mode = HD_PATH;
-    }
-    if (rb->global_settings->statusbar != STATUSBAR_BOTTOM)
-    {
-        if (preferences->footer_mode == FT_SBAR)
-            preferences->footer_mode = FT_NONE;
-        else if (preferences->footer_mode == FT_BOTH)
-            preferences->footer_mode = FT_PAGE;
-    }
-}
-
 void tv_set_preferences(const struct tv_preferences *new_prefs)
 {
     static struct tv_preferences old_prefs;
@@ -94,8 +77,7 @@ void tv_set_preferences(const struct tv_preferences *new_prefs)
         tv_copy_preferences((oldp = &old_prefs));
     is_initialized = true;
 
-    rb->memcpy(preferences, new_prefs, sizeof(struct tv_preferences));
-    tv_check_header_and_footer();
+    rb->memcpy(&prefs, new_prefs, sizeof(struct tv_preferences));
     tv_notify_change_preferences(oldp);
 }
 
index 906f5c5..7705a16 100644 (file)
@@ -122,9 +122,9 @@ struct tv_preferences {
 };
 
 /*
- *     global pointer to the preferences
+ *     global pointer to the preferences (read-only)
  */
-extern struct tv_preferences *preferences;
+extern const struct tv_preferences * const preferences;
 
 /*
  * change the preferences
index 4e91af9..8403c30 100644 (file)
@@ -170,18 +170,18 @@ static void tv_change_preferences(const struct tv_preferences *oldp)
     tv_seek(cur_file_pos + cur_start_file_pos - start_file_pos, SEEK_SET);
 }
 
-bool tv_init_reader(void)
+bool tv_init_reader(unsigned char **buf, size_t *size)
 {
-    size_t size;
-
-    /* get the plugin buffer */
-    reader_buffer = rb->plugin_get_buffer(&size);
-
-    if (size < 2 * TV_MIN_BLOCK_SIZE)
+    if (*size < 2 * TV_MIN_BLOCK_SIZE)
         return false;
 
-    block_size    = size / 2;
+    block_size    = *size / 2;
     buffer_size   = 2 * block_size;
+    reader_buffer = *buf;
+
+    *buf += buffer_size;
+    *size -= buffer_size;
+
     tv_add_preferences_change_listner(tv_change_preferences);
 
     return true;
index 2ee0b82..0b079ed 100644 (file)
 /*
  * initialize the reader module
  *
+ * [In/Out] buf
+ *          the start pointer of the buffer
+ *
+ * [In/Out] size
+ *          enabled buffer size
+ *
  * return
  *     true  initialize success
  *     false initialize failure
  */
-bool tv_init_reader(void);
+bool tv_init_reader(unsigned char **buf, size_t *bufsize);
 
 /* finalize the reader module */
 void tv_finalize_reader(void);
index f9a2fad..3ad388e 100644 (file)
@@ -544,11 +544,16 @@ int tv_create_formed_text(const unsigned char *src, ssize_t bufsize,
     return size;
 }
 
-void tv_init_text_processor(void)
+bool tv_init_text_processor(unsigned char **buf, size_t *size)
 {
+    /* unused : no need for dynamic buffer yet */
+    (void)buf;
+    (void)size;
+
     text_type = TV_TEXT_UNKNOWN;
     expand_extra_line = false;
     is_break_line = false;
+    return true;
 }
 
 void tv_set_creation_conditions(int blocks, int width)
index 8a9de90..33e6a02 100644 (file)
 /*
  * initialize the text processor module
  *
+ * [In/Out] buf
+ *          the start pointer of the buffer
+ *
+ * [In/Out] size
+ *          enabled buffer size
+ *
+ * return
+ *     true  initialize success
+ *     false initialize failure
  */
-void tv_init_text_processor(void);
+bool tv_init_text_processor(unsigned char **buf, size_t *bufsize);
 
 /*
  * set the processing conditions
index 53c8f67..ec1c138 100644 (file)
 static int get_block;
 static bool get_double_blocks;
 
-bool tv_init_text_reader(void)
+bool tv_init_text_reader(unsigned char **buf, size_t *size)
 {
-    tv_init_text_processor();
-
-    return tv_init_pager();
+    return tv_init_text_processor(buf, size) && tv_init_pager(buf, size);
 }
 
 void tv_finalize_text_reader(void)
index 50059c8..5162892 100644 (file)
 /*
  * initialize the text reader module
  *
+ * [In/Out] buf
+ *          the start pointer of the buffer
+ *
+ * [In/Out] size
+ *          enabled buffer size
+ *
  * return
  *     true  initialize success
  *     false initialize failure
  */
-bool tv_init_text_reader(void);
+bool tv_init_text_reader(unsigned char **buf, size_t *bufsize);
 
 /* finalize the text reader module */
 void tv_finalize_text_reader(void);
index c733d5a..f1f2033 100644 (file)
@@ -67,6 +67,40 @@ static bool tv_set_font(const unsigned char *font)
     return true;
 }
 
+static bool tv_check_header_and_footer(struct tv_preferences *new_prefs)
+{
+    bool change_prefs = false;
+
+    if (rb->global_settings->statusbar != STATUSBAR_TOP)
+    {
+        if (new_prefs->header_mode == HD_SBAR)
+        {
+            new_prefs->header_mode = HD_NONE;
+            change_prefs = true;
+        }
+        else if (new_prefs->header_mode == HD_BOTH)
+        {
+            new_prefs->header_mode = HD_PATH;
+            change_prefs = true;
+        }
+    }
+    if (rb->global_settings->statusbar != STATUSBAR_BOTTOM)
+    {
+        if (new_prefs->footer_mode == FT_SBAR)
+        {
+            new_prefs->footer_mode = FT_NONE;
+            change_prefs = true;
+        }
+        else if (new_prefs->footer_mode == FT_BOTH)
+        {
+            new_prefs->footer_mode = FT_PAGE;
+            change_prefs = true;
+        }
+    }
+
+    return change_prefs;
+}
+
 static void tv_show_header(void)
 {
     unsigned header_mode = header_mode;
@@ -259,6 +293,9 @@ static void tv_change_preferences(const struct tv_preferences *oldp)
 #ifdef HAVE_LCD_BITMAP
     static bool font_changing = false;
     const unsigned char *font_str;
+    bool change_prefs = false;
+    struct tv_preferences new_prefs;
+    tv_copy_preferences(&new_prefs);
 
     font_str = (oldp && !font_changing)? oldp->font_name : rb->global_settings->font_file;
 
@@ -268,14 +305,17 @@ static void tv_change_preferences(const struct tv_preferences *oldp)
         font_changing = true;
         if (!tv_set_font(preferences->font_name))
         {
-            struct tv_preferences new_prefs;
-            tv_copy_preferences(&new_prefs);
-
             rb->strlcpy(new_prefs.font_name, font_str, MAX_PATH);
-            tv_set_preferences(&new_prefs);
-            return;
+            change_prefs = true;
         }
     }
+
+    if (tv_check_header_and_footer(&new_prefs) || change_prefs)
+    {
+        tv_set_preferences(&new_prefs);
+        return;
+    }
+
     font_changing = false;
 
     /* calculates display lines */
@@ -319,10 +359,10 @@ static void tv_change_preferences(const struct tv_preferences *oldp)
     tv_set_read_conditions(preferences->windows, window_width);
 }
 
-bool tv_init_window(void)
+bool tv_init_window(unsigned char **buf, size_t *size)
 {
     tv_add_preferences_change_listner(tv_change_preferences);
-    return tv_init_text_reader();
+    return tv_init_text_reader(buf, size);
 }
 
 void tv_finalize_window(void)
index abf475a..14eeb08 100644 (file)
 /*
  * initialize the window module
  *
+ * [In/Out] buf
+ *          the start pointer of the buffer
+ *
+ * [In/Out] size
+ *          enabled buffer size
+ *
  * return
  *     true  initialize success
  *     false initialize failure
  */
-bool tv_init_window(void);
+bool tv_init_window(unsigned char **buf, size_t *bufsize);
 
 /* finalize the window module */
 void tv_finalize_window(void);