Code

replaced get_screen_X() with static screen_functions variable
authorMax Kellermann <max@duempel.org>
Wed, 17 Sep 2008 10:05:02 +0000 (12:05 +0200)
committerMax Kellermann <max@duempel.org>
Wed, 17 Sep 2008 10:05:02 +0000 (12:05 +0200)
Compile time initialization saves resources, compared to run-time
initialization.  Declare all screen_function structs as global
variables, and remove all get functions.

src/screen.c
src/screen.h
src/screen_artist.c
src/screen_clock.c
src/screen_file.c
src/screen_help.c
src/screen_keydef.c
src/screen_lyrics.c
src/screen_play.c
src/screen_search.c

index 0b335efa903f27bab16bdb1531ead40bddef391d..ea21a1af8fae1a5679b9815126df2b60cad72e1c 100644 (file)
 
 
 /* screens */
-extern struct screen_functions *get_screen_playlist(void);
-extern struct screen_functions *get_screen_browse(void);
-extern struct screen_functions *get_screen_help(void);
-extern struct screen_functions *get_screen_search(void);
-extern struct screen_functions *get_screen_artist(void);
-extern struct screen_functions *get_screen_keydef(void);
-extern struct screen_functions *get_screen_clock(void);
-extern struct screen_functions *get_screen_lyrics(void);
+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);
 
@@ -65,32 +73,32 @@ static const struct
 {
        gint id;
        const gchar *name;
-       screen_get_mode_functions_fn_t get_mode_functions;
+       const struct screen_functions *functions;
 } screens[] = {
-       { SCREEN_PLAYLIST_ID, "playlist", get_screen_playlist },
-       { SCREEN_BROWSE_ID,   "browse",   get_screen_browse },
+       { 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 struct screen_functions *mode_fn = NULL;
+static const struct screen_functions *mode_fn = NULL;
 static int seek_id = -1;
 static int seek_target_time = 0;
 
@@ -139,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;
        }
 
@@ -412,12 +420,12 @@ screen_exit(void)
 
                /* close and exit all screens (playlist,browse,help...) */
                i=0;
-               while (screens[i].get_mode_functions) {
-                       struct screen_functions *sf = screens[i].get_mode_functions();
+               while (screens[i].functions) {
+                       const struct screen_functions *sf = screens[i].functions;
 
-                       if (sf && sf->close)
+                       if (sf->close)
                                sf->close();
-                       if (sf && sf->exit)
+                       if (sf->exit)
                                sf->exit();
 
                        i++;
@@ -476,10 +484,10 @@ screen_resize(void)
 
        /* close and exit all screens (playlist,browse,help...) */
        i=0;
-       while (screens[i].get_mode_functions) {
-               struct screen_functions *sf = screens[i].get_mode_functions();
+       while (screens[i].functions) {
+               const struct screen_functions *sf = screens[i].functions;
 
-               if (sf && sf->resize)
+               if (sf->resize)
                        sf->resize(screen->main_window.cols, screen->main_window.rows);
 
                i++;
@@ -626,10 +634,10 @@ screen_init(mpdclient_t *c)
 
        /* initialize screens */
        i=0;
-       while (screens[i].get_mode_functions) {
-               struct screen_functions *fn = screens[i].get_mode_functions();
+       while (screens[i].functions) {
+               const struct screen_functions *fn = screens[i].functions;
 
-               if (fn && fn->init)
+               if (fn->init)
                        fn->init(screen->main_window.w,
                                 screen->main_window.cols,
                                 screen->main_window.rows);
@@ -642,7 +650,7 @@ screen_init(mpdclient_t *c)
        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 )
index 06ce71da36b0297ee4e6dfcdd76265a0d28fcb34..7aa72d7d296188e39f73a15f0b26460c1cc6ce34 100644 (file)
@@ -92,25 +92,4 @@ int screen_get_mouse_event(mpdclient_t *c,
                           struct list_window *lw, int lw_length, 
                           unsigned long *bstate, int *row);
 
-struct screen_functions *
-get_screen_search(void);
-
-struct screen_functions *
-get_screen_browse(void);
-
-struct screen_functions *
-get_screen_playlist(void);
-
-struct screen_functions *
-get_screen_help(void);
-
-struct screen_functions *
-get_screen_lyrics(void);
-
-struct screen_functions *
-get_screen_keydef(void);
-
-struct screen_functions *
-get_screen_clock(void);
-
 #endif
index fa42f1f7794aa34f22fddae02e0ecfb28ed34f28..f010a089dcaeea11ef1582c5dcadf01ad40c249a 100644 (file)
@@ -473,25 +473,17 @@ artist_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
   return 0;
 }
 
-screen_functions_t *
-get_screen_artist(void)
-{
-  static screen_functions_t functions;
-
-  memset(&functions, 0, sizeof(screen_functions_t));
-  functions.init   = init;
-  functions.exit   = quit;
-  functions.open   = open;
-  functions.close  = close;
-  functions.resize = resize;
-  functions.paint  = paint;
-  functions.update = update;
-  functions.cmd    = artist_cmd;
-  functions.get_lw = get_filelist_window;
-  functions.get_title = get_title;
-
-  return &functions;
-}
-
+const struct screen_functions screen_artist = {
+       .init = init,
+       .exit = quit,
+       .open = open,
+       .close = close,
+       .resize = resize,
+       .paint = paint,
+       .update = update,
+       .cmd = artist_cmd,
+       .get_lw = get_filelist_window,
+       .get_title = get_title,
+};
 
 #endif /* ENABLE_ARTIST_SCREEN */
index 493150bb056fffd185049476ae4e5b516a8b3ecd..11d2da1b2dbd15f168ea2a936a3604c50c633105 100644 (file)
@@ -241,24 +241,16 @@ clock_cmd(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c,
        return 0;
 }
 
-screen_functions_t *
-get_screen_clock(void)
-{
-       static screen_functions_t functions;
-
-       memset(&functions, 0, sizeof(screen_functions_t));
-       functions.init = clock_init;
-       functions.exit = clock_exit;
-       functions.open = clock_open;
-       functions.close = clock_close;
-       functions.resize = clock_resize;
-       functions.paint = clock_paint;
-       functions.update = clock_update;
-       functions.cmd = clock_cmd;
-       functions.get_lw = NULL;
-       functions.get_title = clock_title;
-
-       return &functions;
-}
+const struct screen_functions screen_clock = {
+       .init = clock_init,
+       .exit = clock_exit,
+       .open = clock_open,
+       .close = clock_close,
+       .resize = clock_resize,
+       .paint = clock_paint,
+       .update = clock_update,
+       .cmd = clock_cmd,
+       .get_title = clock_title,
+};
 
 #endif
index c480961c4b9ebab4cde59bce4b315d9302a98687..e996aba0903d3ebbc211348cd3afc6881ddde93b 100644 (file)
@@ -751,23 +751,16 @@ get_filelist_window(void)
        return lw;
 }
 
-screen_functions_t *
-get_screen_browse(void)
-{
-       static screen_functions_t functions;
-
-       memset(&functions, 0, sizeof(screen_functions_t));
-       functions.init = browse_init;
-       functions.exit = browse_exit;
-       functions.open = browse_open;
-       functions.close = browse_close;
-       functions.resize = browse_resize;
-       functions.paint = browse_paint;
-       functions.update = browse_update;
-       functions.cmd = browse_cmd;
-       functions.get_lw = get_filelist_window;
-       functions.get_title = browse_title;
-
-       return &functions;
-}
+const struct screen_functions screen_browse = {
+       .init = browse_init,
+       .exit = browse_exit,
+       .open = browse_open,
+       .close = browse_close,
+       .resize = browse_resize,
+       .paint = browse_paint,
+       .update = browse_update,
+       .cmd = browse_cmd,
+       .get_lw = get_filelist_window,
+       .get_title = browse_title,
+};
 
index d225b3d6d396b08b6afab88ea450191c127a7b22..bfefc1e7abb3040fb06c106519f87e59ea215203 100644 (file)
@@ -306,22 +306,13 @@ help_lw(void)
        return lw;
 }
 
-screen_functions_t *
-get_screen_help(void)
-{
-       static screen_functions_t functions;
-
-       memset(&functions, 0, sizeof(screen_functions_t));
-       functions.init   = help_init;
-       functions.exit   = help_exit;
-       functions.open   = NULL;
-       functions.close  = NULL;
-       functions.resize = help_resize;
-       functions.paint  = help_paint;
-       functions.update = help_update;
-       functions.cmd    = help_cmd;
-       functions.get_lw = help_lw;
-       functions.get_title = help_title;
-
-       return &functions;
-}
+const struct screen_functions screen_help = {
+       .init = help_init,
+       .exit = help_exit,
+       .resize = help_resize,
+       .paint = help_paint,
+       .update = help_update,
+       .cmd = help_cmd,
+       .get_lw = help_lw,
+       .get_title = help_title,
+};
index 44c84ca42f0f8747f66e889acc92868c68e1fe9c..1fde75d4540eba24d7b3071ca9507ee8e432e455 100644 (file)
@@ -368,25 +368,17 @@ keydef_lw(void)
   return lw;
 }
 
-screen_functions_t *
-get_screen_keydef(void)
-{
-  static screen_functions_t functions;
-
-  memset(&functions, 0, sizeof(screen_functions_t));
-  functions.init   = keydef_init;
-  functions.exit   = keydef_exit;
-  functions.open   = keydef_open;
-  functions.close  = keydef_close;
-  functions.resize = keydef_resize;
-  functions.paint  = keydef_paint;
-  functions.update = keydef_update;
-  functions.cmd    = keydef_cmd;
-  functions.get_lw = keydef_lw;
-  functions.get_title = keydef_title;
-
-  return &functions;
-}
-
+const struct screen_functions screen_keydef = {
+       .init = keydef_init,
+       .exit = keydef_exit,
+       .open = keydef_open,
+       .close = keydef_close,
+       .resize = keydef_resize,
+       .paint = keydef_paint,
+       .update = keydef_update,
+       .cmd = keydef_cmd,
+       .get_lw = keydef_lw,
+       .get_title = keydef_title,
+};
 
 #endif
index 2f63fb0ae62be5d5cd72cbffa117c8b5d07b9ffd..885a35a766c843eac8aa3878e19f41b5b2288b49 100644 (file)
@@ -376,23 +376,17 @@ lyrics_lw(void)
   return lw;
 }
 
-screen_functions_t *
-get_screen_lyrics(void)
-{
-  static screen_functions_t functions;
-
-  memset(&functions, 0, sizeof(screen_functions_t));
-  functions.init   = lyrics_screen_init;
-  functions.exit   = lyrics_exit;
-  functions.open = lyrics_open;
-  functions.close  = NULL;
-  functions.resize = lyrics_resize;
-  functions.paint  = lyrics_paint;
-  functions.update = lyrics_update;
-  functions.cmd    = lyrics_cmd;
-  functions.get_lw = lyrics_lw;
-  functions.get_title = lyrics_title;
-
-  return &functions;
-}
+const struct screen_functions screen_lyrics = {
+       .init = lyrics_screen_init,
+       .exit = lyrics_exit,
+       .open = lyrics_open,
+       .close = NULL,
+       .resize = lyrics_resize,
+       .paint = lyrics_paint,
+       .update = lyrics_update,
+       .cmd = lyrics_cmd,
+       .get_lw = lyrics_lw,
+       .get_title = lyrics_title,
+};
+
 #endif /* ENABLE_LYRICS_SCREEN */
index 32c08ea73a21ede201215cf8fd2e8635bc7bdf68..22d7198aa7e685f9e7831f3f9fbd3a92f8a3d91e 100644 (file)
@@ -503,22 +503,15 @@ play_lw(void)
   return lw;
 }
 
-screen_functions_t *
-get_screen_playlist(void)
-{
-  static screen_functions_t functions;
-
-  memset(&functions, 0, sizeof(screen_functions_t));
-  functions.init   = play_init;
-  functions.exit   = play_exit;
-  functions.open   = play_open;
-  functions.close  = NULL;
-  functions.resize = play_resize;
-  functions.paint  = play_paint;
-  functions.update = play_update;
-  functions.cmd    = play_cmd;
-  functions.get_lw = play_lw;
-  functions.get_title = play_title;
-
-  return &functions;
-}
+const struct screen_functions screen_playlist = {
+       .init = play_init,
+       .exit = play_exit,
+       .open = play_open,
+       .close = NULL,
+       .resize = play_resize,
+       .paint = play_paint,
+       .update = play_update,
+       .cmd = play_cmd,
+       .get_lw = play_lw,
+       .get_title = play_title,
+};
index 6e2a0d68ccdd93a749ba1d5e4a7c72599fe48cdd..659070804a44cdb57f6e346588a84324693feab2 100644 (file)
@@ -537,25 +537,17 @@ search_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
   return 0;
 }
 
-screen_functions_t *
-get_screen_search(void)
-{
-  static screen_functions_t functions;
-
-  memset(&functions, 0, sizeof(screen_functions_t));
-  functions.init   = init;
-  functions.exit   = quit;
-  functions.open   = open;
-  functions.close  = close;
-  functions.resize = resize;
-  functions.paint  = paint;
-  functions.update = update;
-  functions.cmd    = search_cmd;
-  functions.get_lw = get_filelist_window;
-  functions.get_title = get_title;
-
-  return &functions;
-}
-
+const struct screen_functions screen_search = {
+       .init = init,
+       .exit = quit,
+       .open = open,
+       .close = close,
+       .resize = resize,
+       .paint = paint,
+       .update = update,
+       .cmd = search_cmd,
+       .get_lw = get_filelist_window,
+       .get_title = get_title,
+};
 
 #endif /* ENABLE_SEARCH_SCREEN */