From 51cb2863dd7def8505ba37bdbcb8221416c26f55 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 17 Sep 2008 12:05:02 +0200 Subject: [PATCH] replaced get_screen_X() with static screen_functions variable 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 | 72 +++++++++++++++++++++++++-------------------- src/screen.h | 21 ------------- src/screen_artist.c | 32 ++++++++------------ src/screen_clock.c | 30 +++++++------------ src/screen_file.c | 31 ++++++++----------- src/screen_help.c | 29 +++++++----------- src/screen_keydef.c | 32 ++++++++------------ src/screen_lyrics.c | 32 ++++++++------------ src/screen_play.c | 31 ++++++++----------- src/screen_search.c | 32 ++++++++------------ 10 files changed, 134 insertions(+), 208 deletions(-) diff --git a/src/screen.c b/src/screen.c index 0b335ef..ea21a1a 100644 --- a/src/screen.c +++ b/src/screen.c @@ -50,14 +50,22 @@ /* 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 ) diff --git a/src/screen.h b/src/screen.h index 06ce71d..7aa72d7 100644 --- a/src/screen.h +++ b/src/screen.h @@ -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 diff --git a/src/screen_artist.c b/src/screen_artist.c index fa42f1f..f010a08 100644 --- a/src/screen_artist.c +++ b/src/screen_artist.c @@ -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 */ diff --git a/src/screen_clock.c b/src/screen_clock.c index 493150b..11d2da1 100644 --- a/src/screen_clock.c +++ b/src/screen_clock.c @@ -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 diff --git a/src/screen_file.c b/src/screen_file.c index c480961..e996aba 100644 --- a/src/screen_file.c +++ b/src/screen_file.c @@ -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, +}; diff --git a/src/screen_help.c b/src/screen_help.c index d225b3d..bfefc1e 100644 --- a/src/screen_help.c +++ b/src/screen_help.c @@ -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, +}; diff --git a/src/screen_keydef.c b/src/screen_keydef.c index 44c84ca..1fde75d 100644 --- a/src/screen_keydef.c +++ b/src/screen_keydef.c @@ -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 diff --git a/src/screen_lyrics.c b/src/screen_lyrics.c index 2f63fb0..885a35a 100644 --- a/src/screen_lyrics.c +++ b/src/screen_lyrics.c @@ -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 */ diff --git a/src/screen_play.c b/src/screen_play.c index 32c08ea..22d7198 100644 --- a/src/screen_play.c +++ b/src/screen_play.c @@ -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, +}; diff --git a/src/screen_search.c b/src/screen_search.c index 6e2a0d6..6590708 100644 --- a/src/screen_search.c +++ b/src/screen_search.c @@ -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 */ -- 2.30.2