Code

replaced get_screen_X() with static screen_functions variable
[ncmpc.git] / src / screen.c
index f4ced308c567dc80cf6d713729982c00e786ee9f..ea21a1af8fae1a5679b9815126df2b60cad72e1c 100644 (file)
  *
  */
 
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdarg.h>
-#include <string.h>
-#include <time.h>
-#include <locale.h>
-#include <glib.h>
-#include <ncurses.h>
-
+#include "screen.h"
+#include "screen_utils.h"
 #include "config.h"
 #include "ncmpc.h"
 #include "support.h"
 #include "colors.h"
 #include "strfsong.h"
 #include "wreadln.h"
-#include "screen.h"
-#include "screen_utils.h"
 
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdarg.h>
+#include <string.h>
+#include <time.h>
+#include <locale.h>
 
 #define SCREEN_PLAYLIST_ID     0
 #define SCREEN_BROWSE_ID       1
 
 
 /* screens */
-extern screen_functions_t *get_screen_playlist(void);
-extern screen_functions_t *get_screen_browse(void);
-extern screen_functions_t *get_screen_help(void);
-extern screen_functions_t *get_screen_search(void);
-extern screen_functions_t *get_screen_artist(void);
-extern screen_functions_t *get_screen_keydef(void);
-extern screen_functions_t *get_screen_clock(void);
-extern screen_functions_t *get_screen_lyrics(void);
-
-typedef screen_functions_t * (*screen_get_mode_functions_fn_t) (void);
-
-typedef struct
-{
-       gint id;
-       gchar *name;
-       screen_get_mode_functions_fn_t get_mode_functions;
-} screen_mode_info_t;
+extern const struct screen_functions screen_playlist;
+extern const struct screen_functions screen_browse;
+#ifdef ENABLE_ARTIST_SCREEN
+extern const struct screen_functions screen_artist;
+#endif
+extern const struct screen_functions screen_help;
+#ifdef ENABLE_SEARCH_SCREEN
+extern const struct screen_functions screen_search;
+#endif
+#ifdef ENABLE_KEYDEF_SCREEN
+extern const struct screen_functions screen_keydef;
+#endif
+#ifdef ENABLE_CLOCK_SCREEN
+extern const struct screen_functions screen_clock;
+#endif
+extern const struct screen_functions screen_lyrics;
 
+typedef struct screen_functions * (*screen_get_mode_functions_fn_t) (void);
 
-static screen_mode_info_t screens[] = {
-       { SCREEN_PLAYLIST_ID, "playlist", get_screen_playlist },
-       { SCREEN_BROWSE_ID,   "browse",   get_screen_browse },
+static const struct
+{
+       gint id;
+       const gchar *name;
+       const struct screen_functions *functions;
+} screens[] = {
+       { SCREEN_PLAYLIST_ID, "playlist", &screen_playlist },
+       { SCREEN_BROWSE_ID, "browse", &screen_browse },
 #ifdef ENABLE_ARTIST_SCREEN
-       { SCREEN_ARTIST_ID,   "artist",   get_screen_artist },
+       { SCREEN_ARTIST_ID, "artist", &screen_artist },
 #endif
-       { SCREEN_HELP_ID,     "help",     get_screen_help },
+       { SCREEN_HELP_ID, "help", &screen_help },
 #ifdef ENABLE_SEARCH_SCREEN
-       { SCREEN_SEARCH_ID,   "search",   get_screen_search },
+       { SCREEN_SEARCH_ID, "search", &screen_search },
 #endif
 #ifdef ENABLE_KEYDEF_SCREEN
-       { SCREEN_KEYDEF_ID,   "keydef",   get_screen_keydef },
+       { SCREEN_KEYDEF_ID, "keydef", &screen_keydef },
 #endif
 #ifdef ENABLE_CLOCK_SCREEN
-       { SCREEN_CLOCK_ID,    "clock",    get_screen_clock },
+       { SCREEN_CLOCK_ID, "clock", &screen_clock },
 #endif
 #ifdef ENABLE_LYRICS_SCREEN
-       { SCREEN_LYRICS_ID,    "lyrics",    get_screen_lyrics },
+       { SCREEN_LYRICS_ID, "lyrics", &screen_lyrics },
 #endif
-       { G_MAXINT, NULL,      NULL }
+       { G_MAXINT, NULL, NULL }
 };
 
 static gboolean welcome = TRUE;
 static screen_t *screen = NULL;
-static screen_functions_t *mode_fn = NULL;
+static const struct screen_functions *mode_fn = NULL;
 static int seek_id = -1;
 static int seek_target_time = 0;
 
 gint
-screen_get_id(char *name)
+screen_get_id(const char *name)
 {
        gint i=0;
 
@@ -126,7 +128,7 @@ lookup_mode(gint id)
        return -1;
 }
 
-gint get_cur_mode_id()
+gint get_cur_mode_id(void)
 {
        return screens[screen->mode].id;
 }
@@ -145,9 +147,9 @@ switch_screen_mode(gint id, mpdclient_t *c)
 
        /* get functions for the new mode */
        new_mode = lookup_mode(id);
-       if (new_mode>=0 && screens[new_mode].get_mode_functions) {
+       if (new_mode >= 0 && screens[new_mode].functions) {
                D("switch_screen(%s)\n", screens[new_mode].name );
-               mode_fn = screens[new_mode].get_mode_functions();
+               mode_fn = screens[new_mode].functions;
                screen->mode = new_mode;
        }
 
@@ -186,7 +188,7 @@ screen_next_mode(mpdclient_t *c, int offset)
 }
 
 static void
-paint_top_window2(char *header, mpdclient_t *c)
+paint_top_window2(const char *header, mpdclient_t *c)
 {
        char flags[5];
        WINDOW *w = screen->top_window.w;
@@ -258,23 +260,23 @@ paint_top_window2(char *header, mpdclient_t *c)
 }
 
 static void
-paint_top_window(char *header, mpdclient_t *c, int clear)
+paint_top_window(const char *header, mpdclient_t *c, int full_repaint)
 {
        static int prev_volume = -1;
-       static int prev_header_len = -1;
+       static size_t prev_header_len = -1;
        WINDOW *w = screen->top_window.w;
 
        if (prev_header_len!=my_strlen(header)) {
                prev_header_len = my_strlen(header);
-               clear = 1;
+               full_repaint = 1;
        }
 
-       if (clear) {
+       if (full_repaint) {
                wmove(w, 0, 0);
                wclrtoeol(w);
        }
 
-       if (prev_volume!=c->status->volume || clear)
+       if (prev_volume!=c->status->volume || full_repaint)
                paint_top_window2(header, c);
 }
 
@@ -314,7 +316,7 @@ paint_status_window(mpdclient_t *c)
        mpd_Status *status = c->status;
        mpd_Song *song = c->song;
        int elapsedTime = 0;
-       char *str = NULL;
+       const char *str = NULL;
        int x = 0;
 
        if( time(NULL) - screen->status_timestamp <= SCREEN_STATUS_MESSAGE_TIME )
@@ -388,7 +390,7 @@ paint_status_window(mpdclient_t *c)
 
                colors_use(w, COLOR_STATUS);
                /* scroll if the song name is to long */
-               if (options.scroll && my_strlen(songname) > width) {
+               if (options.scroll && my_strlen(songname) > (size_t)width) {
                        static  scroll_state_t st = { 0, 0 };
                        char *tmp = strscroll(songname, options.scroll_sep, width, &st);
 
@@ -418,13 +420,13 @@ screen_exit(void)
 
                /* close and exit all screens (playlist,browse,help...) */
                i=0;
-               while (screens[i].get_mode_functions) {
-                       screen_functions_t *mode_fn = screens[i].get_mode_functions();
+               while (screens[i].functions) {
+                       const struct screen_functions *sf = screens[i].functions;
 
-                       if (mode_fn && mode_fn->close)
-                               mode_fn->close();
-                       if (mode_fn && mode_fn->exit)
-                               mode_fn->exit();
+                       if (sf->close)
+                               sf->close();
+                       if (sf->exit)
+                               sf->exit();
 
                        i++;
                }
@@ -482,11 +484,11 @@ screen_resize(void)
 
        /* close and exit all screens (playlist,browse,help...) */
        i=0;
-       while (screens[i].get_mode_functions) {
-               screen_functions_t *mode_fn = screens[i].get_mode_functions();
+       while (screens[i].functions) {
+               const struct screen_functions *sf = screens[i].functions;
 
-               if (mode_fn && mode_fn->resize)
-                       mode_fn->resize(screen->main_window.cols, screen->main_window.rows);
+               if (sf->resize)
+                       sf->resize(screen->main_window.cols, screen->main_window.rows);
 
                i++;
        }
@@ -499,7 +501,7 @@ screen_resize(void)
 }
 
 void
-screen_status_message(char *msg)
+screen_status_message(const char *msg)
 {
        WINDOW *w = screen->status_window.w;
 
@@ -512,7 +514,7 @@ screen_status_message(char *msg)
 }
 
 void
-screen_status_printf(char *format, ...)
+screen_status_printf(const char *format, ...)
 {
        char *msg;
        va_list ap;
@@ -525,7 +527,7 @@ screen_status_printf(char *format, ...)
 }
 
 void
-ncurses_init()
+ncurses_init(void)
 {
 
        /* initialize the curses library */
@@ -621,32 +623,34 @@ ncurses_init()
                        wbkgd(screen->status_window.w,   COLOR_PAIR(COLOR_STATUS));
                        colors_use(screen->progress_window.w, COLOR_PROGRESSBAR);
                }
+
+       refresh();
 }
 
+int
 screen_init(mpdclient_t *c)
 {
        gint i;
 
        /* initialize screens */
        i=0;
-       while( screens[i].get_mode_functions )
-               {
-                       screen_functions_t *fn = screens[i].get_mode_functions();
+       while (screens[i].functions) {
+               const struct screen_functions *fn = screens[i].functions;
 
-                       if( fn && fn->init )
-                               fn->init(screen->main_window.w,
-                                        screen->main_window.cols,
-                                        screen->main_window.rows);
+               if (fn->init)
+                       fn->init(screen->main_window.w,
+                                screen->main_window.cols,
+                                screen->main_window.rows);
 
-                       i++;
-               }
+               i++;
+       }
 
 #if 0
        /* broken */
        mode_fn = NULL;
        switch_screen_mode(screen_get_id(options.screen_list[0]), c);
 #else
-       mode_fn = get_screen_playlist();
+       mode_fn = &screen_playlist;
 #endif
 
        if( mode_fn && mode_fn->open )
@@ -662,7 +666,7 @@ screen_init(mpdclient_t *c)
 void
 screen_paint(mpdclient_t *c)
 {
-       char *title = NULL;
+       const char *title = NULL;
 
        if (mode_fn && mode_fn->get_title)
                title = mode_fn->get_title(screen->buf, screen->buf_size);
@@ -693,7 +697,7 @@ void
 screen_update(mpdclient_t *c)
 {
        static int repeat = -1;
-       static int random = -1;
+       static int random_enabled = -1;
        static int crossfade = -1;
        static int dbupdate = -1;
        list_window_t *lw = NULL;
@@ -704,7 +708,7 @@ screen_update(mpdclient_t *c)
        /* print a message if mpd status has changed */
        if (repeat < 0) {
                repeat = c->status->repeat;
-               random = c->status->random;
+               random_enabled = c->status->random;
                crossfade = c->status->crossfade;
                dbupdate = c->status->updatingDb;
        }
@@ -714,7 +718,7 @@ screen_update(mpdclient_t *c)
                                     _("Repeat is on") :
                                     _("Repeat is off"));
 
-       if (random != c->status->random)
+       if (random_enabled != c->status->random)
                screen_status_printf(c->status->random ?
                                     _("Random is on") :
                                     _("Random is off"));
@@ -728,7 +732,7 @@ screen_update(mpdclient_t *c)
        }
 
        repeat = c->status->repeat;
-       random = c->status->random;
+       random_enabled = c->status->random;
        crossfade = c->status->crossfade;
        dbupdate = c->status->updatingDb;
 
@@ -769,12 +773,10 @@ screen_update(mpdclient_t *c)
 void
 screen_idle(mpdclient_t *c)
 {
-       if( c->song && seek_id ==  c->song->id &&
+       if (c->song && seek_id == c->song->id &&
            (screen->last_cmd == CMD_SEEK_FORWARD ||
-            screen->last_cmd == CMD_SEEK_BACKWARD) )
-               {
-                       mpdclient_cmd_seek(c, seek_id, seek_target_time);
-               }
+            screen->last_cmd == CMD_SEEK_BACKWARD))
+               mpdclient_cmd_seek(c, seek_id, seek_target_time);
 
        screen->last_cmd = CMD_NONE;
        seek_id = -1;
@@ -811,7 +813,7 @@ screen_get_mouse_event(mpdclient_t *c,
        }
 
        /* if the even occured below the list window move down */
-       if (*row >= lw->rows && lw) {
+       if ((unsigned)*row >= lw->rows && lw) {
                if (event.bstate & BUTTON3_CLICKED)
                        list_window_last(lw, lw_length);
                else