diff options
-rw-r--r-- | src/colors.c | 41 | ||||
-rw-r--r-- | src/command.c | 24 | ||||
-rw-r--r-- | src/command.h | 13 | ||||
-rw-r--r-- | src/conf.c | 471 | ||||
-rw-r--r-- | src/libmpdclient.c | 15 | ||||
-rw-r--r-- | src/list_window.c | 375 | ||||
-rw-r--r-- | src/list_window.h | 39 | ||||
-rw-r--r-- | src/main.c | 379 | ||||
-rw-r--r-- | src/mpdclient.c | 545 | ||||
-rw-r--r-- | src/options.c | 251 | ||||
-rw-r--r-- | src/options.h | 64 | ||||
-rw-r--r-- | src/screen.c | 1545 | ||||
-rw-r--r-- | src/screen.h | 92 | ||||
-rw-r--r-- | src/screen_clock.c | 313 | ||||
-rw-r--r-- | src/screen_file.c | 1156 | ||||
-rw-r--r-- | src/screen_help.c | 249 | ||||
-rw-r--r-- | src/screen_keydef.c | 177 | ||||
-rw-r--r-- | src/screen_lyrics.c | 475 | ||||
-rw-r--r-- | src/screen_play.c | 392 | ||||
-rw-r--r-- | src/screen_search.c | 244 | ||||
-rw-r--r-- | src/screen_utils.c | 27 | ||||
-rw-r--r-- | src/screen_utils.h | 4 | ||||
-rw-r--r-- | src/src_lyrics.c | 238 | ||||
-rw-r--r-- | src/strfsong.c | 360 | ||||
-rw-r--r-- | src/support.c | 82 | ||||
-rw-r--r-- | src/support.h | 8 | ||||
-rw-r--r-- | src/utils.c | 35 | ||||
-rw-r--r-- | src/utils.h | 4 | ||||
-rw-r--r-- | src/wreadln.c | 563 |
29 files changed, 3931 insertions, 4250 deletions
diff --git a/src/colors.c b/src/colors.c index 692f01ecd..c504e0085 100644 --- a/src/colors.c +++ b/src/colors.c @@ -58,33 +58,32 @@ #define NAME_BGCOLOR "background" typedef struct { - short color; - short r,g,b; + short color; + short r,g,b; } color_definition_entry_t; typedef struct { - int id; - char *name; - short fg; - attr_t attrs; + int id; + char *name; + short fg; + attr_t attrs; } color_entry_t; static color_entry_t colors[] = { - - /* color pair, field name, color, mono attribute */ - /*-------------------------------------------------------------------------*/ - { COLOR_TITLE, NAME_TITLE, COLOR_YELLOW, A_NORMAL }, - { COLOR_TITLE_BOLD, NAME_TITLE_BOLD, COLOR_BRIGHT_YELLOW, A_BOLD }, - { COLOR_LINE, NAME_LINE, COLOR_WHITE, A_NORMAL }, - { COLOR_LINE_BOLD, NAME_LINE_BOLD, COLOR_BRIGHT_WHITE, A_BOLD }, - { COLOR_LIST, NAME_LIST, COLOR_GREEN, A_NORMAL }, - { COLOR_LIST_BOLD, NAME_LIST_BOLD, COLOR_BRIGHT_GREEN, A_BOLD }, - { COLOR_PROGRESSBAR, NAME_PROGRESS, COLOR_WHITE, A_NORMAL }, - { COLOR_STATUS, NAME_STATUS, COLOR_YELLOW, A_NORMAL }, - { COLOR_STATUS_BOLD, NAME_STATUS_BOLD, COLOR_BRIGHT_YELLOW, A_BOLD }, - { COLOR_STATUS_TIME, NAME_STATUS_TIME, COLOR_RED, A_NORMAL }, - { COLOR_STATUS_ALERT, NAME_ALERT, COLOR_BRIGHT_RED, A_BOLD }, - { 0, NULL, 0, 0 } + /* color pair, field name, color, mono attribute */ + /*-------------------------------------------------------------------------*/ + { COLOR_TITLE, NAME_TITLE, COLOR_YELLOW, A_NORMAL }, + { COLOR_TITLE_BOLD, NAME_TITLE_BOLD, COLOR_BRIGHT_YELLOW, A_BOLD }, + { COLOR_LINE, NAME_LINE, COLOR_WHITE, A_NORMAL }, + { COLOR_LINE_BOLD, NAME_LINE_BOLD, COLOR_BRIGHT_WHITE, A_BOLD }, + { COLOR_LIST, NAME_LIST, COLOR_GREEN, A_NORMAL }, + { COLOR_LIST_BOLD, NAME_LIST_BOLD, COLOR_BRIGHT_GREEN, A_BOLD }, + { COLOR_PROGRESSBAR, NAME_PROGRESS, COLOR_WHITE, A_NORMAL }, + { COLOR_STATUS, NAME_STATUS, COLOR_YELLOW, A_NORMAL }, + { COLOR_STATUS_BOLD, NAME_STATUS_BOLD, COLOR_BRIGHT_YELLOW, A_BOLD }, + { COLOR_STATUS_TIME, NAME_STATUS_TIME, COLOR_RED, A_NORMAL }, + { COLOR_STATUS_ALERT, NAME_ALERT, COLOR_BRIGHT_RED, A_BOLD }, + { 0, NULL, 0, 0 } }; /* background color */ diff --git a/src/command.c b/src/command.c index d0d141904..a565c25ec 100644 --- a/src/command.c +++ b/src/command.c @@ -383,21 +383,21 @@ get_key_command_from_name(char *name) } -command_t +command_t find_key_command(int key, command_definition_t *cmds) { - int i; + int i; + + i=0; + while (key && cmds && cmds[i].name) { + if (cmds[i].keys[0] == key || + cmds[i].keys[1] == key || + cmds[i].keys[2] == key) + return cmds[i].command; + i++; + } - i=0; - while( key && cmds && cmds[i].name ) - { - if( cmds[i].keys[0] == key || - cmds[i].keys[1] == key || - cmds[i].keys[2] == key ) - return cmds[i].command; - i++; - } - return CMD_NONE; + return CMD_NONE; } command_t diff --git a/src/command.h b/src/command.h index 1d5b9500a..568f05098 100644 --- a/src/command.h +++ b/src/command.h @@ -71,13 +71,12 @@ typedef enum #define KEYDEF_WRITE_ALL 0x02 #define KEYDEF_COMMENT_ALL 0x04 -typedef struct -{ - int keys[MAX_COMMAND_KEYS]; - char flags; - command_t command; - char *name; - char *description; +typedef struct { + int keys[MAX_COMMAND_KEYS]; + char flags; + command_t command; + char *name; + char *description; } command_definition_t; command_definition_t *get_command_definitions(void); diff --git a/src/conf.c b/src/conf.c index 049671c24..dda13f56b 100644 --- a/src/conf.c +++ b/src/conf.c @@ -88,285 +88,270 @@ extern gint screen_get_id(char *name); static gboolean str2bool(char *str) { - if( !strcasecmp(str,"yes") || !strcasecmp(str,"true") || - !strcasecmp(str,"on") || !strcasecmp(str,"1") ) - return TRUE; - return FALSE; + if (!strcasecmp(str, "yes") || !strcasecmp(str, "true") || + !strcasecmp(str, "on") || !strcasecmp(str, "1")) + return TRUE; + return FALSE; } static int parse_key_value(char *str, size_t len, char **end) { - int i, value; - key_parser_state_t state; - - i=0; - value=0; - state=KEY_PARSER_UNKNOWN; - *end = str; - - while( i<len && state!=KEY_PARSER_DONE ) - { - int next = 0; - int c = str[i]; - - if( i+1<len ) - next = str[i+1]; - - switch(state) - { - case KEY_PARSER_UNKNOWN: - if( c=='\'' ) - state = KEY_PARSER_CHAR; - else if( c=='0' && next=='x' ) - state = KEY_PARSER_HEX; - else if( isdigit(c) ) - state = KEY_PARSER_DEC; - else { - fprintf(stderr, - _("Error: Unsupported key definition - %s\n"), - str); - return -1; - } - break; - case KEY_PARSER_CHAR: - if( next!='\'' ) - { - fprintf(stderr, - _("Error: Unsupported key definition - %s\n"), - str); - return -1; - } - value = c; - *end = str+i+2; - state = KEY_PARSER_DONE; - break; - case KEY_PARSER_DEC: - value = (int) strtol(str+(i-1), end, 10); - state = KEY_PARSER_DONE; - break; - case KEY_PARSER_HEX: - if( !isdigit(next) ) - { - fprintf(stderr,_("Error: Digit expected after 0x - %s\n"), str); - return -1; - } - value = (int) strtol(str+(i+1), end, 16); - state = KEY_PARSER_DONE; - break; - case KEY_PARSER_DONE: - break; + int i, value; + key_parser_state_t state; + + i=0; + value=0; + state=KEY_PARSER_UNKNOWN; + *end = str; + + while (i < len && state != KEY_PARSER_DONE) { + int next = 0; + int c = str[i]; + + if( i+1<len ) + next = str[i+1]; + + switch(state) { + case KEY_PARSER_UNKNOWN: + if( c=='\'' ) + state = KEY_PARSER_CHAR; + else if( c=='0' && next=='x' ) + state = KEY_PARSER_HEX; + else if( isdigit(c) ) + state = KEY_PARSER_DEC; + else { + fprintf(stderr, + _("Error: Unsupported key definition - %s\n"), + str); + return -1; + } + break; + case KEY_PARSER_CHAR: + if( next!='\'' ) { + fprintf(stderr, + _("Error: Unsupported key definition - %s\n"), + str); + return -1; + } + value = c; + *end = str+i+2; + state = KEY_PARSER_DONE; + break; + case KEY_PARSER_DEC: + value = (int) strtol(str+(i-1), end, 10); + state = KEY_PARSER_DONE; + break; + case KEY_PARSER_HEX: + if( !isdigit(next) ) { + fprintf(stderr,_("Error: Digit expected after 0x - %s\n"), str); + return -1; + } + value = (int) strtol(str+(i+1), end, 16); + state = KEY_PARSER_DONE; + break; + case KEY_PARSER_DONE: + break; + } + i++; } - i++; - } - if( *end> str+len ) - *end = str+len; + if( *end> str+len ) + *end = str+len; - return value; + return value; } static int parse_key_definition(char *str) { - char buf[MAX_LINE_LENGTH]; - char *p, *end; - size_t len = strlen(str); - int i,j,key; - int keys[MAX_COMMAND_KEYS]; - command_t cmd; - - /* get the command name */ - i=0; - j=0; - memset(buf, 0, MAX_LINE_LENGTH); - while( i<len && str[i]!='=' && !IS_WHITESPACE(str[i]) ) - buf[j++] = str[i++]; - if( (cmd=get_key_command_from_name(buf)) == CMD_NONE ) - { - fprintf(stderr, _("Error: Unknown key command %s\n"), buf); - return -1; - } + char buf[MAX_LINE_LENGTH]; + char *p, *end; + size_t len = strlen(str); + int i,j,key; + int keys[MAX_COMMAND_KEYS]; + command_t cmd; + + /* get the command name */ + i=0; + j=0; + memset(buf, 0, MAX_LINE_LENGTH); + while( i<len && str[i]!='=' && !IS_WHITESPACE(str[i]) ) + buf[j++] = str[i++]; + if( (cmd=get_key_command_from_name(buf)) == CMD_NONE ) { + fprintf(stderr, _("Error: Unknown key command %s\n"), buf); + return -1; + } - /* skip whitespace */ - while( i<len && (str[i]=='=' || IS_WHITESPACE(str[i])) ) - i++; + /* skip whitespace */ + while( i<len && (str[i]=='=' || IS_WHITESPACE(str[i])) ) + i++; - /* get the value part */ - memset(buf, 0, MAX_LINE_LENGTH); - g_strlcpy(buf, str+i, MAX_LINE_LENGTH); - len = strlen(buf); - if( len==0 ) - { - fprintf(stderr,_("Error: Incomplete key definition - %s\n"), str); - return -1; - } + /* get the value part */ + memset(buf, 0, MAX_LINE_LENGTH); + g_strlcpy(buf, str+i, MAX_LINE_LENGTH); + len = strlen(buf); + if( len==0 ) { + fprintf(stderr,_("Error: Incomplete key definition - %s\n"), str); + return -1; + } - /* parse key values */ - i = 0; - key = 0; - len = strlen(buf); - p = buf; - end = buf+len; - memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS); - while( i<MAX_COMMAND_KEYS && p<end && (key=parse_key_value(p,len+1,&p))>=0 ) - { - keys[i++] = key; - while( p<end && (*p==',' || *p==' ' || *p=='\t') ) - p++; - len = strlen(p); - } - if( key<0 ) - { - fprintf(stderr,_("Error: Bad key definition - %s\n"), str); - return -1; - } + /* parse key values */ + i = 0; + key = 0; + len = strlen(buf); + p = buf; + end = buf+len; + memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS); + while( i<MAX_COMMAND_KEYS && p<end && + (key=parse_key_value(p,len+1,&p))>=0 ) { + keys[i++] = key; + while( p<end && (*p==',' || *p==' ' || *p=='\t') ) + p++; + len = strlen(p); + } + if( key<0 ) { + fprintf(stderr,_("Error: Bad key definition - %s\n"), str); + return -1; + } - return assign_keys(cmd, keys); + return assign_keys(cmd, keys); } static char * parse_timedisplay_type(char *str) { - if((!strcmp(str,"elapsed")) || (!strcmp(str,"remaining"))){ - return str; - } else { - fprintf(stderr,_("Error: Bad time display type - %s\n"), str); - return DEFAULT_TIMEDISPLAY_TYPE; - } + if((!strcmp(str,"elapsed")) || (!strcmp(str,"remaining"))){ + return str; + } else { + fprintf(stderr,_("Error: Bad time display type - %s\n"), str); + return DEFAULT_TIMEDISPLAY_TYPE; + } } static int parse_color(char *str) { - char *name = str; - char *value = NULL; - int len,i; - - i=0; - len=strlen(str); - /* get the color name */ - while( i<len && str[i]!='=' && !IS_WHITESPACE(str[i]) ) - i++; - - /* skip whitespace */ - while( i<len && (str[i]=='=' || IS_WHITESPACE(str[i])) ) - { - str[i]='\0'; - i++; - } - - if( i<len ) - value = str+i; - - return colors_assign(name, value); + char *name = str; + char *value = NULL; + int len,i; + + i=0; + len=strlen(str); + /* get the color name */ + while( i<len && str[i]!='=' && !IS_WHITESPACE(str[i]) ) + i++; + + /* skip whitespace */ + while( i<len && (str[i]=='=' || IS_WHITESPACE(str[i])) ) { + str[i]='\0'; + i++; + } + + if( i<len ) + value = str+i; + + return colors_assign(name, value); } static int parse_color_definition(char *str) { - char buf[MAX_LINE_LENGTH]; - char *p, *end, *name; - size_t len = strlen(str); - int i,j,value; - short color, rgb[3]; - - /* get the command name */ - i=0; - j=0; - memset(buf, 0, MAX_LINE_LENGTH); - while( i<len && str[i]!='=' && !IS_WHITESPACE(str[i]) ) - buf[j++] = str[i++]; - color=colors_str2color(buf); - if( color<0 ) - { - fprintf(stderr,_("Error: Bad color %s [%d]\n"), buf, color); - return -1; - } - name = g_strdup(buf); + char buf[MAX_LINE_LENGTH]; + char *p, *end, *name; + size_t len = strlen(str); + int i,j,value; + short color, rgb[3]; + + /* get the command name */ + i=0; + j=0; + memset(buf, 0, MAX_LINE_LENGTH); + while( i<len && str[i]!='=' && !IS_WHITESPACE(str[i]) ) + buf[j++] = str[i++]; + color=colors_str2color(buf); + if( color<0 ) { + fprintf(stderr,_("Error: Bad color %s [%d]\n"), buf, color); + return -1; + } + name = g_strdup(buf); - /* skip whitespace */ - while( i<len && (str[i]=='=' || IS_WHITESPACE(str[i])) ) - i++; + /* skip whitespace */ + while( i<len && (str[i]=='=' || IS_WHITESPACE(str[i])) ) + i++; - /* get the value part */ - memset(buf, 0, MAX_LINE_LENGTH); - g_strlcpy(buf, str+i, MAX_LINE_LENGTH); - len = strlen(buf); - if( len==0 ) - { - fprintf(stderr,_("Error: Incomplete color definition - %s\n"), str); - g_free(name); - return -1; - } + /* get the value part */ + memset(buf, 0, MAX_LINE_LENGTH); + g_strlcpy(buf, str+i, MAX_LINE_LENGTH); + len = strlen(buf); + if( len==0 ) { + fprintf(stderr,_("Error: Incomplete color definition - %s\n"), str); + g_free(name); + return -1; + } - /* parse r,g.b values with the key definition parser */ - i = 0; - value = 0; - len = strlen(buf); - p = buf; - end = buf+len; - memset(rgb, 0, sizeof(short)*3); - while( i<3 && p<end && (value=parse_key_value(p,len+1,&p))>=0 ) - { - rgb[i++] = value; - while( p<end && (*p==',' || *p==' ' || *p=='\t') ) - p++; - len = strlen(p); - } - if( value<0 || i!=3) - { - fprintf(stderr,_("Error: Bad color definition - %s\n"), str); - g_free(name); - return -1; - } - value = colors_define(name, rgb[0], rgb[1], rgb[2]); - g_free(name); - return value; + /* parse r,g.b values with the key definition parser */ + i = 0; + value = 0; + len = strlen(buf); + p = buf; + end = buf+len; + memset(rgb, 0, sizeof(short)*3); + while( i<3 && p<end && (value=parse_key_value(p,len+1,&p))>=0 ) { + rgb[i++] = value; + while( p<end && (*p==',' || *p==' ' || *p=='\t') ) + p++; + len = strlen(p); + } + if( value<0 || i!=3) { + fprintf(stderr,_("Error: Bad color definition - %s\n"), str); + g_free(name); + return -1; + } + value = colors_define(name, rgb[0], rgb[1], rgb[2]); + g_free(name); + return value; } static char * get_format(char *str) { - gsize len = strlen(str); + gsize len = strlen(str); - if( str && str[0]=='\"' && str[len-1] == '\"' ) - { - str[len-1] = '\0'; - str++; - } - return g_strdup(str); + if( str && str[0]=='\"' && str[len-1] == '\"' ) { + str[len-1] = '\0'; + str++; + } + return g_strdup(str); } static char ** check_screen_list(char *value) { - char **tmp = g_strsplit_set(value, " \t,", 100); - char **screen = NULL; - int i,j; - - i=0; - j=0; - while( tmp && tmp[i] ) - { - tmp[i] = lowerstr(tmp[i]); - if( screen_get_id(tmp[i]) == -1 ) - fprintf(stderr, - _("Error: Unsupported screen \"%s\"\n"), - tmp[i]); - else - { - screen = g_realloc(screen, (j+2)*sizeof(char *)); - screen[j++] = g_strdup(tmp[i]); - screen[j] = NULL; + char **tmp = g_strsplit_set(value, " \t,", 100); + char **screen = NULL; + int i,j; + + i=0; + j=0; + while( tmp && tmp[i] ) { + tmp[i] = lowerstr(tmp[i]); + if( screen_get_id(tmp[i]) == -1 ) + fprintf(stderr, + _("Error: Unsupported screen \"%s\"\n"), + tmp[i]); + else { + screen = g_realloc(screen, (j+2)*sizeof(char *)); + screen[j++] = g_strdup(tmp[i]); + screen[j] = NULL; + } + i++; } - i++; - } - g_strfreev(tmp); - if( screen == NULL ) - return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0); + g_strfreev(tmp); + if( screen == NULL ) + return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0); - return screen; + return screen; } static int @@ -613,23 +598,23 @@ read_rc_file(char *filename, options_t *options) int check_user_conf_dir(void) { - int retval; - char *dirname = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL); - - if( g_file_test(dirname, G_FILE_TEST_IS_DIR) ) - { - g_free(dirname); - return 0; - } - retval = mkdir(dirname, 0755); - g_free(dirname); - return retval; + int retval; + char *dirname = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL); + + if (g_file_test(dirname, G_FILE_TEST_IS_DIR)) { + g_free(dirname); + return 0; + } + + retval = mkdir(dirname, 0755); + g_free(dirname); + return retval; } char * get_user_key_binding_filename(void) { - return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL); + return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL); } diff --git a/src/libmpdclient.c b/src/libmpdclient.c index 65c2e447e..c9641e9e4 100644 --- a/src/libmpdclient.c +++ b/src/libmpdclient.c @@ -924,7 +924,8 @@ static void mpd_initDirectory(mpd_Directory * directory) { } static void mpd_finishDirectory(mpd_Directory * directory) { - if(directory->path) free(directory->path); + if (directory->path) + free(directory->path); } mpd_Directory * mpd_newDirectory(void) { @@ -944,7 +945,8 @@ void mpd_freeDirectory(mpd_Directory * directory) { mpd_Directory * mpd_directoryDup(mpd_Directory * directory) { mpd_Directory * ret = mpd_newDirectory(); - if(directory->path) ret->path = strdup(directory->path); + if (directory->path) + ret->path = strdup(directory->path); return ret; } @@ -954,7 +956,8 @@ static void mpd_initPlaylistFile(mpd_PlaylistFile * playlist) { } static void mpd_finishPlaylistFile(mpd_PlaylistFile * playlist) { - if(playlist->path) free(playlist->path); + if (playlist->path) + free(playlist->path); } mpd_PlaylistFile * mpd_newPlaylistFile(void) { @@ -973,7 +976,8 @@ void mpd_freePlaylistFile(mpd_PlaylistFile * playlist) { mpd_PlaylistFile * mpd_playlistFileDup(mpd_PlaylistFile * playlist) { mpd_PlaylistFile * ret = mpd_newPlaylistFile(); - if(playlist->path) ret->path = strdup(playlist->path); + if (playlist->path) + ret->path = strdup(playlist->path); return ret; } @@ -1017,8 +1021,7 @@ mpd_InfoEntity * mpd_getNextInfoEntity(mpd_Connection * connection) { mpd_InfoEntity * entity = NULL; if(connection->doneProcessing || (connection->listOks && - connection->doneListOk)) - { + connection->doneListOk)) { return NULL; } diff --git a/src/list_window.c b/src/list_window.c index 2b5fe8c60..96ea7c110 100644 --- a/src/list_window.c +++ b/src/list_window.c @@ -36,292 +36,275 @@ extern void screen_bell(void); list_window_t * list_window_init(WINDOW *w, int width, int height) { - list_window_t *lw; - - lw = g_malloc0(sizeof(list_window_t)); - lw->w = w; - lw->cols = width; - lw->rows = height; - lw->clear = 1; - return lw; + list_window_t *lw; + + lw = g_malloc0(sizeof(list_window_t)); + lw->w = w; + lw->cols = width; + lw->rows = height; + lw->clear = 1; + return lw; } list_window_t * list_window_free(list_window_t *lw) { - if( lw ) - { - memset(lw, 0, sizeof(list_window_t)); - g_free(lw); - } - return NULL; + if (lw) { + memset(lw, 0, sizeof(list_window_t)); + g_free(lw); + } + + return NULL; } void list_window_reset(list_window_t *lw) { - lw->selected = 0; - lw->xoffset = 0; - lw->start = 0; - lw->clear = 1; + lw->selected = 0; + lw->xoffset = 0; + lw->start = 0; + lw->clear = 1; } void list_window_check_selected(list_window_t *lw, int length) { - while( lw->start && lw->start+lw->rows>length) - lw->start--; + while (lw->start && lw->start + lw->rows > length) + lw->start--; - if( lw->selected<0 ) - lw->selected=0; + if (lw->selected < 0) + lw->selected = 0; - while( lw->selected<lw->start ) - lw->selected++; + while (lw->selected < lw->start) + lw->selected++; - while( lw->selected>0 && length>0 && lw->selected>=length ) - lw->selected--; + while (lw->selected > 0 && length > 0 && lw->selected >= length) + lw->selected--; } -void +void list_window_set_selected(list_window_t *lw, int n) { - lw->selected=n; + lw->selected = n; } void list_window_next(list_window_t *lw, int length) { - if( lw->selected < length-1 ) - lw->selected++; - else if ( options.list_wrap ) - lw->selected = 0; + if (lw->selected < length - 1) + lw->selected++; + else if (options.list_wrap) + lw->selected = 0; } void list_window_previous(list_window_t *lw, int length) { - if( lw->selected > 0 ) - lw->selected--; - else if( options.list_wrap ) - lw->selected = length-1; + if (lw->selected > 0) + lw->selected--; + else if (options.list_wrap) + lw->selected = length - 1; } void list_window_first(list_window_t *lw) { - lw->xoffset = 0; - lw->selected = 0; + lw->xoffset = 0; + lw->selected = 0; } void list_window_last(list_window_t *lw, int length) { - lw->xoffset = 0; - lw->selected = length-1; + lw->xoffset = 0; + lw->selected = length - 1; } void list_window_next_page(list_window_t *lw, int length) { - int step = lw->rows-1; - if( step<= 0 ) - return; - if( lw->selected+step < length-1 ) - lw->selected+=step; - else - return list_window_last(lw,length); + int step = lw->rows - 1; + if (step <= 0) + return; + if (lw->selected + step < length - 1) + lw->selected += step; + else + return list_window_last(lw, length); } void list_window_previous_page(list_window_t *lw) { - int step = lw->rows-1; - if( step<= 0 ) - return; - if( lw->selected-step > 0 ) - lw->selected-=step; - else - list_window_first(lw); + int step = lw->rows - 1; + if (step <= 0) + return; + if (lw->selected-step > 0) + lw->selected -= step; + else + list_window_first(lw); } -void +void list_window_paint(list_window_t *lw, list_window_callback_fn_t callback, void *callback_data) { - int i; - int fill = options.wide_cursor; - int show_cursor = !(lw->flags & LW_HIDE_CURSOR); - - if( show_cursor ) - { - while( lw->selected < lw->start ) - { - lw->start--; - lw->clear=1; - } - while( lw->selected >= lw->start+lw->rows ) - { - lw->start++; - lw->clear=1; + int i; + int fill = options.wide_cursor; + int show_cursor = !(lw->flags & LW_HIDE_CURSOR); + + if (show_cursor) { + while (lw->selected < lw->start) { + lw->start--; + lw->clear=1; + } + + while (lw->selected >= lw->start+lw->rows) { + lw->start++; + lw->clear=1; + } } - } - - for(i=0; i<lw->rows; i++) - { - int highlight = 0; - char *label; - - label = (callback) (lw->start+i, &highlight, callback_data); - wmove(lw->w, i, 0); - if( lw->clear && (!fill || !label) ) - wclrtoeol(lw->w); - if( label ) - { - int selected = lw->start+i == lw->selected; - size_t len = my_strlen(label); - - if( highlight ) - colors_use(lw->w, COLOR_LIST_BOLD); - else - colors_use(lw->w, COLOR_LIST); - - if( show_cursor && selected ) - wattron(lw->w, A_REVERSE); - - //waddnstr(lw->w, label, lw->cols); - waddstr(lw->w, label); - if( fill && len<lw->cols ) - whline(lw->w, ' ', lw->cols-len); - - if( selected ) - wattroff(lw->w, A_REVERSE); + + for (i = 0; i < lw->rows; i++) { + int highlight = 0; + char *label; + + label = callback(lw->start + i, &highlight, callback_data); + wmove(lw->w, i, 0); + if( lw->clear && (!fill || !label) ) + wclrtoeol(lw->w); + + if (label) { + int selected = lw->start + i == lw->selected; + size_t len = my_strlen(label); + + if( highlight ) + colors_use(lw->w, COLOR_LIST_BOLD); + else + colors_use(lw->w, COLOR_LIST); + + if( show_cursor && selected ) + wattron(lw->w, A_REVERSE); + + //waddnstr(lw->w, label, lw->cols); + waddstr(lw->w, label); + if( fill && len<lw->cols ) + whline(lw->w, ' ', lw->cols-len); + + if( selected ) + wattroff(lw->w, A_REVERSE); + } } - - } - lw->clear=0; -} + lw->clear=0; +} int -list_window_find(list_window_t *lw, +list_window_find(list_window_t *lw, list_window_callback_fn_t callback, void *callback_data, char *str, int wrap) { - int h; - int i = lw->selected+1; - char *label; - - while( wrap || i==lw->selected+1 ) - { - while( (label=(callback) (i,&h,callback_data)) ) - { - if( str && label && strcasestr(label, str) ) - { - lw->selected = i; - return 0; - } - if( wrap && i==lw->selected ) - return 1; - i++; + int h; + int i = lw->selected + 1; + char *label; + + while (wrap || i == lw->selected + 1) { + while ((label = callback(i,&h,callback_data))) { + if (str && label && strcasestr(label, str)) { + lw->selected = i; + return 0; + } + if (wrap && i == lw->selected) + return 1; + i++; + } + if (wrap) { + if (i == 0) /* empty list */ + return 1; + i=0; /* first item */ + screen_bell(); + } } - if( wrap ) - { - if ( i==0 ) /* empty list */ - return 1; - i=0; /* first item */ - screen_bell(); - } - } - return 1; -} + return 1; +} int -list_window_rfind(list_window_t *lw, +list_window_rfind(list_window_t *lw, list_window_callback_fn_t callback, void *callback_data, char *str, int wrap, int rows) { - int h; - int i = lw->selected-1; - char *label; - - if ( rows == 0 ) - return 1; - - while( wrap || i==lw->selected-1 ) - { - while( i>=0 && (label=(callback) (i,&h,callback_data)) ) - { - if( str && label && strcasestr(label, str) ) - { - lw->selected = i; - return 0; - } - if( wrap && i==lw->selected ) - return 1; - i--; + int h; + int i = lw->selected-1; + char *label; + + if (rows == 0) + return 1; + + while (wrap || i == lw->selected - 1) { + while (i >= 0 && (label = callback(i,&h,callback_data))) { + if( str && label && strcasestr(label, str) ) { + lw->selected = i; + return 0; + } + if (wrap && i == lw->selected) + return 1; + i--; + } + if (wrap) { + i = rows - 1; /* last item */ + screen_bell(); + } } - if( wrap ) - { - i=rows-1; /* last item */ - screen_bell(); - } - } - return 1; + return 1; } - /* perform basic list window commands (movement) */ -int +int list_window_cmd(list_window_t *lw, int rows, command_t cmd) { - switch(cmd) - { - case CMD_LIST_PREVIOUS: - list_window_previous(lw, rows); - lw->repaint=1; - break; - case CMD_LIST_NEXT: - list_window_next(lw, rows); - lw->repaint=1; - break; - case CMD_LIST_FIRST: - list_window_first(lw); - lw->repaint = 1; - break; - case CMD_LIST_LAST: - list_window_last(lw, rows); - lw->repaint = 1; - break; - case CMD_LIST_NEXT_PAGE: - list_window_next_page(lw, rows); - lw->repaint = 1; - break; - case CMD_LIST_PREVIOUS_PAGE: - list_window_previous_page(lw); - lw->repaint = 1; - break; - default: - return 0; - } - return 1; -} - - - + switch (cmd) { + case CMD_LIST_PREVIOUS: + list_window_previous(lw, rows); + lw->repaint=1; + break; + case CMD_LIST_NEXT: + list_window_next(lw, rows); + lw->repaint=1; + break; + case CMD_LIST_FIRST: + list_window_first(lw); + lw->repaint = 1; + break; + case CMD_LIST_LAST: + list_window_last(lw, rows); + lw->repaint = 1; + break; + case CMD_LIST_NEXT_PAGE: + list_window_next_page(lw, rows); + lw->repaint = 1; + break; + case CMD_LIST_PREVIOUS_PAGE: + list_window_previous_page(lw); + lw->repaint = 1; + break; + default: + return 0; + } + return 1; +} list_window_state_t * list_window_init_state(void) { - return g_malloc0(sizeof(list_window_state_t)); + return g_malloc0(sizeof(list_window_state_t)); } list_window_state_t * diff --git a/src/list_window.h b/src/list_window.h index fe465bede..9ef67214b 100644 --- a/src/list_window.h +++ b/src/list_window.h @@ -7,27 +7,24 @@ #define LW_HIDE_CURSOR 0x01 -typedef char * (*list_window_callback_fn_t) (int index, - int *highlight, - void *data); - -typedef struct -{ - WINDOW *w; - int rows, cols; - - int start; - int selected; - int xoffset; - int clear; - int repaint; - int flags; - +typedef char *(*list_window_callback_fn_t)(int index, + int *highlight, + void *data); + +typedef struct { + WINDOW *w; + int rows, cols; + + int start; + int selected; + int xoffset; + int clear; + int repaint; + int flags; } list_window_t; -typedef struct -{ - GList *list; +typedef struct { + GList *list; } list_window_state_t; @@ -60,7 +57,7 @@ void list_window_next_page(list_window_t *lw, int length); void list_window_check_selected(list_window_t *lw, int length); /* find a string in a list window */ -int list_window_find(list_window_t *lw, +int list_window_find(list_window_t *lw, list_window_callback_fn_t callback, void *callback_data, char *str, @@ -68,7 +65,7 @@ int list_window_find(list_window_t *lw, /* find a string in a list window (reversed) */ int -list_window_rfind(list_window_t *lw, +list_window_rfind(list_window_t *lw, list_window_callback_fn_t callback, void *callback_data, char *str, diff --git a/src/main.c b/src/main.c index f3cc155ed..c2c7170eb 100644 --- a/src/main.c +++ b/src/main.c @@ -60,25 +60,24 @@ error_msg(gchar *msg) static void error_callback(mpdclient_t *c, gint error, gchar *msg) { - gint code = GET_ACK_ERROR_CODE(error); - - error = error & 0xFF; - D("Error [%d:%d]> \"%s\"\n", error, code, msg); - switch(error) - { - case MPD_ERROR_CONNPORT: - case MPD_ERROR_NORESPONSE: - break; - case MPD_ERROR_ACK: - screen_status_printf("%s", error_msg(msg)); - screen_bell(); - break; - default: - screen_status_printf("%s", msg); - screen_bell(); - doupdate(); - connected = FALSE; - } + gint code = GET_ACK_ERROR_CODE(error); + + error = error & 0xFF; + D("Error [%d:%d]> \"%s\"\n", error, code, msg); + switch (error) { + case MPD_ERROR_CONNPORT: + case MPD_ERROR_NORESPONSE: + break; + case MPD_ERROR_ACK: + screen_status_printf("%s", error_msg(msg)); + screen_bell(); + break; + default: + screen_status_printf("%s", msg); + screen_bell(); + doupdate(); + connected = FALSE; + } } static void @@ -132,19 +131,19 @@ exit_and_cleanup(void) void catch_sigint( int sig ) { - printf("\n%s\n", _("Exiting...")); - exit(EXIT_SUCCESS); + printf("\n%s\n", _("Exiting...")); + exit(EXIT_SUCCESS); } void catch_sigcont( int sig ) { - D("catch_sigcont()\n"); + D("catch_sigcont()\n"); #ifdef ENABLE_RAW_MODE - reset_prog_mode(); /* restore tty modes */ - refresh(); + reset_prog_mode(); /* restore tty modes */ + refresh(); #endif - screen_resize(); + screen_resize(); } void @@ -173,197 +172,187 @@ D(char *format, ...) } #endif -int +int main(int argc, const char *argv[]) { - options_t *options; - struct sigaction act; - const char *charset = NULL; - gboolean key_error; + options_t *options; + struct sigaction act; + const char *charset = NULL; + gboolean key_error; #ifdef HAVE_LOCALE_H - /* time and date formatting */ - setlocale(LC_TIME,""); - /* care about sorting order etc */ - setlocale(LC_COLLATE,""); - /* charset */ - setlocale(LC_CTYPE,""); - /* initialize charset conversions */ - charset_init(g_get_charset(&charset)); - D("charset: %s\n", charset); + /* time and date formatting */ + setlocale(LC_TIME,""); + /* care about sorting order etc */ + setlocale(LC_COLLATE,""); + /* charset */ + setlocale(LC_CTYPE,""); + /* initialize charset conversions */ + charset_init(g_get_charset(&charset)); + D("charset: %s\n", charset); #endif - /* initialize i18n support */ + /* initialize i18n support */ #ifdef ENABLE_NLS - setlocale(LC_MESSAGES, ""); - bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR); - bind_textdomain_codeset(GETTEXT_PACKAGE, charset); - textdomain(GETTEXT_PACKAGE); + setlocale(LC_MESSAGES, ""); + bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR); + bind_textdomain_codeset(GETTEXT_PACKAGE, charset); + textdomain(GETTEXT_PACKAGE); #endif - /* initialize options */ - options = options_init(); + /* initialize options */ + options = options_init(); - /* parse command line options - 1 pass get configuration files */ - options_parse(argc, argv); - - /* read configuration */ - read_configuration(options); - - /* check key bindings */ - key_error = check_key_bindings(NULL, NULL, 0); - - /* parse command line options - 2 pass */ - options_parse(argc, argv); - - /* setup signal behavior - SIGINT */ - sigemptyset( &act.sa_mask ); - act.sa_flags = 0; - act.sa_handler = catch_sigint; - if( sigaction(SIGINT, &act, NULL)<0 ) - { - perror("signal"); - exit(EXIT_FAILURE); - } - /* setup signal behavior - SIGTERM */ - sigemptyset( &act.sa_mask ); - act.sa_flags = 0; - act.sa_handler = catch_sigint; - if( sigaction(SIGTERM, &act, NULL)<0 ) - { - perror("sigaction()"); - exit(EXIT_FAILURE); - } - /* setup signal behavior - SIGCONT */ - sigemptyset( &act.sa_mask ); - act.sa_flags = 0; - act.sa_handler = catch_sigcont; - if( sigaction(SIGCONT, &act, NULL)<0 ) - { - perror("sigaction(SIGCONT)"); - exit(EXIT_FAILURE); - } - - /* setup signal behaviour - SIGHUP*/ - sigemptyset( &act.sa_mask ); - act.sa_flags = 0; - act.sa_handler = catch_sigint; - if( sigaction(SIGHUP, &act, NULL)<0 ) - { - perror("sigaction(SIGHUP)"); - exit(EXIT_FAILURE); - } - - /* install exit function */ - atexit(exit_and_cleanup); - - ncurses_init(); - - src_lyr_init (); - - /* connect to our music player daemon */ - mpd = mpdclient_new(); - - if( mpdclient_connect(mpd, - options->host, - options->port, - 10.0, - options->password) ) - { - exit(EXIT_FAILURE); - } + /* parse command line options - 1 pass get configuration files */ + options_parse(argc, argv); - /* if no password is used, but the mpd wants one, the connection - might be established but no status information is avaiable */ - mpdclient_update(mpd); - if(!mpd->status) - { - screen_auth(mpd); - } - if(!mpd->status) exit(EXIT_FAILURE); - - connected = TRUE; - D("Connected to MPD version %d.%d.%d\n", - mpd->connection->version[0], - mpd->connection->version[1], - mpd->connection->version[2]); - - /* quit if mpd is pre 0.11.0 - song id not supported by mpd */ - if( MPD_VERSION_LT(mpd, 0,11,0) ) - { - fprintf(stderr, - _("Error: MPD version %d.%d.%d is to old (0.11.0 needed).\n"), - mpd->connection->version[0], - mpd->connection->version[1], - mpd->connection->version[2]); - exit(EXIT_FAILURE); - } + /* read configuration */ + read_configuration(options); - /* initialize curses */ - screen_init(mpd); - /* install error callback function */ - mpdclient_install_error_callback(mpd, error_callback); + /* check key bindings */ + key_error = check_key_bindings(NULL, NULL, 0); - /* initialize timer */ - timer = g_timer_new(); + /* parse command line options - 2 pass */ + options_parse(argc, argv); - connected = TRUE; - while( connected || options->reconnect ) - { - static gdouble t = G_MAXDOUBLE; + /* setup signal behavior - SIGINT */ + sigemptyset( &act.sa_mask ); + act.sa_flags = 0; + act.sa_handler = catch_sigint; + if( sigaction(SIGINT, &act, NULL)<0 ) { + perror("signal"); + exit(EXIT_FAILURE); + } - if( key_error ) - { - char buf[BUFSIZE]; + /* setup signal behavior - SIGTERM */ + sigemptyset( &act.sa_mask ); + act.sa_flags = 0; + act.sa_handler = catch_sigint; + if( sigaction(SIGTERM, &act, NULL)<0 ) { + perror("sigaction()"); + exit(EXIT_FAILURE); + } - key_error=check_key_bindings(NULL, buf, BUFSIZE); - screen_status_printf("%s", buf); + /* setup signal behavior - SIGCONT */ + sigemptyset( &act.sa_mask ); + act.sa_flags = 0; + act.sa_handler = catch_sigcont; + if( sigaction(SIGCONT, &act, NULL)<0 ) { + perror("sigaction(SIGCONT)"); + exit(EXIT_FAILURE); } - if( connected && (t>=MPD_UPDATE_TIME || mpd->need_update) ) - { - mpdclient_update(mpd); - g_timer_start(timer); + /* setup signal behaviour - SIGHUP*/ + sigemptyset( &act.sa_mask ); + act.sa_flags = 0; + act.sa_handler = catch_sigint; + if( sigaction(SIGHUP, &act, NULL)<0 ) { + perror("sigaction(SIGHUP)"); + exit(EXIT_FAILURE); } - if( connected ) - { - command_t cmd; - - screen_update(mpd); - if( (cmd=get_keyboard_command()) != CMD_NONE ) - { - screen_cmd(mpd, cmd); - if( cmd==CMD_VOLUME_UP || cmd==CMD_VOLUME_DOWN) - /* make shure we dont update the volume yet */ - g_timer_start(timer); - } - else - screen_idle(mpd); + /* install exit function */ + atexit(exit_and_cleanup); + + ncurses_init(); + + src_lyr_init (); + + /* connect to our music player daemon */ + mpd = mpdclient_new(); + + if (mpdclient_connect(mpd, + options->host, + options->port, + 10.0, + options->password)) + exit(EXIT_FAILURE); + + /* if no password is used, but the mpd wants one, the connection + might be established but no status information is avaiable */ + mpdclient_update(mpd); + if (!mpd->status) + screen_auth(mpd); + + if (!mpd->status) + exit(EXIT_FAILURE); + + connected = TRUE; + D("Connected to MPD version %d.%d.%d\n", + mpd->connection->version[0], + mpd->connection->version[1], + mpd->connection->version[2]); + + /* quit if mpd is pre 0.11.0 - song id not supported by mpd */ + if( MPD_VERSION_LT(mpd, 0,11,0) ) { + fprintf(stderr, + _("Error: MPD version %d.%d.%d is to old (0.11.0 needed).\n"), + mpd->connection->version[0], + mpd->connection->version[1], + mpd->connection->version[2]); + exit(EXIT_FAILURE); } - else if( options->reconnect ) - { - screen_status_printf(_("Connecting to %s... [Press %s to abort]"), - options->host, get_key_names(CMD_QUIT,0) ); - - if( get_keyboard_command_with_timeout(MPD_RECONNECT_TIME)==CMD_QUIT) - exit(EXIT_SUCCESS); - - if( mpdclient_connect(mpd, - options->host, - options->port, - 1.5, - options->password) == 0 ) - { - screen_status_printf(_("Connected to %s!"), options->host); - connected = TRUE; - } - doupdate(); + + /* initialize curses */ + screen_init(mpd); + /* install error callback function */ + mpdclient_install_error_callback(mpd, error_callback); + + /* initialize timer */ + timer = g_timer_new(); + + connected = TRUE; + while (connected || options->reconnect) { + static gdouble t = G_MAXDOUBLE; + + if( key_error ) { + char buf[BUFSIZE]; + + key_error=check_key_bindings(NULL, buf, BUFSIZE); + screen_status_printf("%s", buf); + } + + if( connected && (t>=MPD_UPDATE_TIME || mpd->need_update) ) { + mpdclient_update(mpd); + g_timer_start(timer); + } + + if( connected ) { + command_t cmd; + + screen_update(mpd); + if( (cmd=get_keyboard_command()) != CMD_NONE ) + { + screen_cmd(mpd, cmd); + if( cmd==CMD_VOLUME_UP || cmd==CMD_VOLUME_DOWN) + /* make shure we dont update the volume yet */ + g_timer_start(timer); + } + else + screen_idle(mpd); + } else if (options->reconnect) { + screen_status_printf(_("Connecting to %s... [Press %s to abort]"), + options->host, get_key_names(CMD_QUIT,0) ); + + if( get_keyboard_command_with_timeout(MPD_RECONNECT_TIME)==CMD_QUIT) + exit(EXIT_SUCCESS); + + if (mpdclient_connect(mpd, + options->host, options->port, + 1.5, + options->password) == 0) { + screen_status_printf(_("Connected to %s!"), options->host); + connected = TRUE; + } + + doupdate(); + } + + if (options->enable_xterm_title) + update_xterm_title(); + + t = g_timer_elapsed(timer, NULL); } - if( options->enable_xterm_title ) - update_xterm_title(); - t = g_timer_elapsed(timer, NULL); - } - exit(EXIT_FAILURE); + exit(EXIT_FAILURE); } diff --git a/src/mpdclient.c b/src/mpdclient.c index b496a2d86..cc31f9ca5 100644 --- a/src/mpdclient.c +++ b/src/mpdclient.c @@ -48,46 +48,50 @@ extern GList *string_list_free(GList *string_list); /* filelist sorting functions */ static gint -compare_filelistentry_dir(gconstpointer filelist_entry1, gconstpointer filelist_entry2) +compare_filelistentry_dir(gconstpointer filelist_entry1, + gconstpointer filelist_entry2) { - mpd_InfoEntity *e1, *e2; - char *key1, *key2; - int n = 0; - - e1 = ((filelist_entry_t *)filelist_entry1)->entity; - e2 = ((filelist_entry_t *)filelist_entry2)->entity; - if (e1 && e2 && - e1->type == MPD_INFO_ENTITY_TYPE_DIRECTORY && - e2->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) - { - key1 = g_utf8_collate_key(e1->info.directory->path,-1); - key2 = g_utf8_collate_key(e2->info.directory->path,-1); - n = strcmp(key1,key2); - g_free(key1); - g_free(key2); - } - return n; + mpd_InfoEntity *e1, *e2; + char *key1, *key2; + int n = 0; + + e1 = ((filelist_entry_t *)filelist_entry1)->entity; + e2 = ((filelist_entry_t *)filelist_entry2)->entity; + + if (e1 && e2 && + e1->type == MPD_INFO_ENTITY_TYPE_DIRECTORY && + e2->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) { + key1 = g_utf8_collate_key(e1->info.directory->path,-1); + key2 = g_utf8_collate_key(e2->info.directory->path,-1); + n = strcmp(key1,key2); + g_free(key1); + g_free(key2); + } + + return n; } /* sort by list-format */ gint -compare_filelistentry_format(gconstpointer filelist_entry1, gconstpointer filelist_entry2) +compare_filelistentry_format(gconstpointer filelist_entry1, + gconstpointer filelist_entry2) { - mpd_InfoEntity *e1, *e2; - char key1[BUFSIZE], key2[BUFSIZE]; - int n = 0; - - e1 = ((filelist_entry_t *)filelist_entry1)->entity; - e2 = ((filelist_entry_t *)filelist_entry2)->entity; - if (e1 && e2 && - e1->type == MPD_INFO_ENTITY_TYPE_SONG && - e2->type == MPD_INFO_ENTITY_TYPE_SONG) - { - strfsong(key1, BUFSIZE, LIST_FORMAT, e1->info.song); - strfsong(key2, BUFSIZE, LIST_FORMAT, e2->info.song); - n = strcmp(key1,key2); - } - return n; + mpd_InfoEntity *e1, *e2; + char key1[BUFSIZE], key2[BUFSIZE]; + int n = 0; + + e1 = ((filelist_entry_t *)filelist_entry1)->entity; + e2 = ((filelist_entry_t *)filelist_entry2)->entity; + + if (e1 && e2 && + e1->type == MPD_INFO_ENTITY_TYPE_SONG && + e2->type == MPD_INFO_ENTITY_TYPE_SONG) { + strfsong(key1, BUFSIZE, LIST_FORMAT, e1->info.song); + strfsong(key2, BUFSIZE, LIST_FORMAT, e2->info.song); + n = strcmp(key1,key2); + } + + return n; } @@ -197,32 +201,32 @@ mpdclient_disconnect(mpdclient_t *c) } gint -mpdclient_connect(mpdclient_t *c, - gchar *host, - gint port, +mpdclient_connect(mpdclient_t *c, + gchar *host, + gint port, gfloat timeout, gchar *password) { - gint retval = 0; - - /* close any open connection */ - if( c->connection ) - mpdclient_disconnect(c); - - /* connect to MPD */ - c->connection = mpd_newConnection(host, port, timeout); - if( c->connection->error ) - return error_cb(c, c->connection->error, c->connection->errorStr); - - /* send password */ - if( password ) - { - mpd_sendPasswordCommand(c->connection, password); - retval = mpdclient_finish_command(c); - } - c->need_update = TRUE; + gint retval = 0; + + /* close any open connection */ + if( c->connection ) + mpdclient_disconnect(c); + + /* connect to MPD */ + c->connection = mpd_newConnection(host, port, timeout); + if( c->connection->error ) + return error_cb(c, c->connection->error, + c->connection->errorStr); + + /* send password */ + if( password ) { + mpd_sendPasswordCommand(c->connection, password); + retval = mpdclient_finish_command(c); + } + c->need_update = TRUE; - return retval; + return retval; } gint @@ -272,52 +276,52 @@ mpdclient_update(mpdclient_t *c) /*** MPD Commands **********************************************************/ /****************************************************************************/ -gint +gint mpdclient_cmd_play(mpdclient_t *c, gint index) { #ifdef ENABLE_SONG_ID - mpd_Song *song = playlist_get_song(c, index); + mpd_Song *song = playlist_get_song(c, index); - D("Play id:%d\n", song ? song->id : -1); - if( song ) - mpd_sendPlayIdCommand(c->connection, song->id); - else - mpd_sendPlayIdCommand(c->connection, MPD_PLAY_AT_BEGINNING); + D("Play id:%d\n", song ? song->id : -1); + if (song) + mpd_sendPlayIdCommand(c->connection, song->id); + else + mpd_sendPlayIdCommand(c->connection, MPD_PLAY_AT_BEGINNING); #else - mpd_sendPlayCommand(c->connection, index); + mpd_sendPlayCommand(c->connection, index); #endif - c->need_update = TRUE; - return mpdclient_finish_command(c); + c->need_update = TRUE; + return mpdclient_finish_command(c); } -gint +gint mpdclient_cmd_pause(mpdclient_t *c, gint value) { - mpd_sendPauseCommand(c->connection, value); - return mpdclient_finish_command(c); + mpd_sendPauseCommand(c->connection, value); + return mpdclient_finish_command(c); } -gint +gint mpdclient_cmd_stop(mpdclient_t *c) { - mpd_sendStopCommand(c->connection); - return mpdclient_finish_command(c); + mpd_sendStopCommand(c->connection); + return mpdclient_finish_command(c); } -gint +gint mpdclient_cmd_next(mpdclient_t *c) { - mpd_sendNextCommand(c->connection); - c->need_update = TRUE; - return mpdclient_finish_command(c); + mpd_sendNextCommand(c->connection); + c->need_update = TRUE; + return mpdclient_finish_command(c); } -gint +gint mpdclient_cmd_prev(mpdclient_t *c) { - mpd_sendPrevCommand(c->connection); - c->need_update = TRUE; - return mpdclient_finish_command(c); + mpd_sendPrevCommand(c->connection); + c->need_update = TRUE; + return mpdclient_finish_command(c); } gint @@ -402,146 +406,146 @@ mpdclient_cmd_add_path(mpdclient_t *c, gchar *path) return retval; } -gint +gint mpdclient_cmd_add(mpdclient_t *c, mpd_Song *song) -{ - gint retval = 0; +{ + gint retval = 0; - if( !song || !song->file ) - return -1; + if( !song || !song->file ) + return -1; - /* send the add command to mpd */ - mpd_sendAddCommand(c->connection, song->file); - if( (retval=mpdclient_finish_command(c)) ) - return retval; + /* send the add command to mpd */ + mpd_sendAddCommand(c->connection, song->file); + if( (retval=mpdclient_finish_command(c)) ) + return retval; #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD - /* add the song to playlist */ - c->playlist.list = g_list_append(c->playlist.list, mpd_songDup(song)); - c->playlist.length++; + /* add the song to playlist */ + c->playlist.list = g_list_append(c->playlist.list, mpd_songDup(song)); + c->playlist.length++; - /* increment the playlist id, so we dont retrives a new playlist */ - c->playlist.id++; + /* increment the playlist id, so we dont retrives a new playlist */ + c->playlist.id++; - /* call playlist updated callback */ - mpdclient_playlist_callback(c, PLAYLIST_EVENT_ADD, (gpointer) song); + /* call playlist updated callback */ + mpdclient_playlist_callback(c, PLAYLIST_EVENT_ADD, (gpointer) song); #else - c->need_update = TRUE; + c->need_update = TRUE; #endif - return 0; + return 0; } gint mpdclient_cmd_delete(mpdclient_t *c, gint index) { - gint retval = 0; - mpd_Song *song = playlist_get_song(c, index); + gint retval = 0; + mpd_Song *song = playlist_get_song(c, index); - if( !song ) - return -1; + if( !song ) + return -1; - /* send the delete command to mpd */ + /* send the delete command to mpd */ #ifdef ENABLE_SONG_ID - D("Delete id:%d\n", song->id); - mpd_sendDeleteIdCommand(c->connection, song->id); + D("Delete id:%d\n", song->id); + mpd_sendDeleteIdCommand(c->connection, song->id); #else - mpd_sendDeleteCommand(c->connection, index); + mpd_sendDeleteCommand(c->connection, index); #endif - if( (retval=mpdclient_finish_command(c)) ) - return retval; + if( (retval=mpdclient_finish_command(c)) ) + return retval; #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE - /* increment the playlist id, so we dont retrive a new playlist */ - c->playlist.id++; + /* increment the playlist id, so we dont retrive a new playlist */ + c->playlist.id++; - /* remove the song from the playlist */ - c->playlist.list = g_list_remove(c->playlist.list, (gpointer) song); - c->playlist.length = g_list_length(c->playlist.list); + /* remove the song from the playlist */ + c->playlist.list = g_list_remove(c->playlist.list, (gpointer) song); + c->playlist.length = g_list_length(c->playlist.list); - /* call playlist updated callback */ - mpdclient_playlist_callback(c, PLAYLIST_EVENT_DELETE, (gpointer) song); + /* call playlist updated callback */ + mpdclient_playlist_callback(c, PLAYLIST_EVENT_DELETE, (gpointer) song); - /* remove references to the song */ - if( c->song == song ) - { - c->song = NULL; - c->need_update = TRUE; - } + /* remove references to the song */ + if (c->song == song) { + c->song = NULL; + c->need_update = TRUE; + } - /* free song */ - mpd_freeSong(song); + /* free song */ + mpd_freeSong(song); #else - c->need_update = TRUE; + c->need_update = TRUE; #endif - return 0; + return 0; } gint mpdclient_cmd_move(mpdclient_t *c, gint old_index, gint new_index) { - gint n, index1, index2; - GList *item1, *item2; - gpointer data1, data2; - mpd_Song *song1, *song2; + gint n, index1, index2; + GList *item1, *item2; + gpointer data1, data2; + mpd_Song *song1, *song2; - if( old_index==new_index || new_index<0 || new_index>=c->playlist.length ) - return -1; + if (old_index == new_index || new_index < 0 || + new_index >= c->playlist.length) + return -1; - song1 = playlist_get_song(c, old_index); - song2 = playlist_get_song(c, new_index); + song1 = playlist_get_song(c, old_index); + song2 = playlist_get_song(c, new_index); - /* send the move command to mpd */ + /* send the move command to mpd */ #ifdef ENABLE_SONG_ID - D("Swapping id:%d with id:%d\n", song1->id, song2->id); - mpd_sendSwapIdCommand(c->connection, song1->id, song2->id); + D("Swapping id:%d with id:%d\n", song1->id, song2->id); + mpd_sendSwapIdCommand(c->connection, song1->id, song2->id); #else - D("Moving index %d to id:%d\n", old_index, new_index); - mpd_sendMoveCommand(c->connection, old_index, new_index); + D("Moving index %d to id:%d\n", old_index, new_index); + mpd_sendMoveCommand(c->connection, old_index, new_index); #endif - if( (n=mpdclient_finish_command(c)) ) - return n; + if( (n=mpdclient_finish_command(c)) ) + return n; #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE - /* update the songs position field */ - n = song1->pos; - song1->pos = song2->pos; - song2->pos = n; - index1 = MIN(old_index, new_index); - index2 = MAX(old_index, new_index); - /* retreive the list items */ - item1 = g_list_nth(c->playlist.list, index1); - item2 = g_list_nth(c->playlist.list, index2); - /* retrieve the songs */ - data1 = item1->data; - data2 = item2->data; - - /* move the second item */ - c->playlist.list = g_list_remove(c->playlist.list, data2); - c->playlist.list = g_list_insert_before(c->playlist.list, item1, data2); - - /* move the first item */ - if( index2-index1 >1 ) - { - item2 = g_list_nth(c->playlist.list, index2); - c->playlist.list = g_list_remove(c->playlist.list, data1); - c->playlist.list = g_list_insert_before(c->playlist.list, item2, data1); - } + /* update the songs position field */ + n = song1->pos; + song1->pos = song2->pos; + song2->pos = n; + index1 = MIN(old_index, new_index); + index2 = MAX(old_index, new_index); + /* retreive the list items */ + item1 = g_list_nth(c->playlist.list, index1); + item2 = g_list_nth(c->playlist.list, index2); + /* retrieve the songs */ + data1 = item1->data; + data2 = item2->data; + + /* move the second item */ + c->playlist.list = g_list_remove(c->playlist.list, data2); + c->playlist.list = g_list_insert_before(c->playlist.list, item1, data2); + + /* move the first item */ + if (index2-index1 > 1) { + item2 = g_list_nth(c->playlist.list, index2); + c->playlist.list = g_list_remove(c->playlist.list, data1); + c->playlist.list = g_list_insert_before(c->playlist.list, + item2, data1); + } - /* increment the playlist id, so we dont retrives a new playlist */ - c->playlist.id++; + /* increment the playlist id, so we dont retrives a new playlist */ + c->playlist.id++; #else - c->need_update = TRUE; -#endif + c->need_update = TRUE; +#endif - /* call playlist updated callback */ - D("move> new_index=%d, old_index=%d\n", new_index, old_index); - mpdclient_playlist_callback(c, PLAYLIST_EVENT_MOVE, (gpointer) &new_index); + /* call playlist updated callback */ + D("move> new_index=%d, old_index=%d\n", new_index, old_index); + mpdclient_playlist_callback(c, PLAYLIST_EVENT_MOVE, (gpointer) &new_index); - return 0; + return 0; } gint @@ -677,162 +681,156 @@ mpdclient_playlist_free(mpdclient_playlist_t *playlist) list=list->next; } g_list_free(playlist->list); - memset(playlist, 0, sizeof(mpdclient_playlist_t)); - return 0; + memset(playlist, 0, sizeof(mpdclient_playlist_t)); + return 0; } /* update playlist */ -gint +gint mpdclient_playlist_update(mpdclient_t *c) { - mpd_InfoEntity *entity; + mpd_InfoEntity *entity; - D("mpdclient_playlist_update() [%lld]\n", c->status->playlist); + D("mpdclient_playlist_update() [%lld]\n", c->status->playlist); - if( MPD_ERROR(c) ) - return -1; + if (MPD_ERROR(c)) + return -1; - if( c->playlist.list ) - mpdclient_playlist_free(&c->playlist); + if (c->playlist.list) + mpdclient_playlist_free(&c->playlist); - mpd_sendPlaylistInfoCommand(c->connection,-1); - while( (entity=mpd_getNextInfoEntity(c->connection)) ) - { - if(entity->type==MPD_INFO_ENTITY_TYPE_SONG) - { - mpd_Song *song = mpd_songDup(entity->info.song); + mpd_sendPlaylistInfoCommand(c->connection,-1); + while ((entity = mpd_getNextInfoEntity(c->connection))) { + if (entity->type == MPD_INFO_ENTITY_TYPE_SONG) { + mpd_Song *song = mpd_songDup(entity->info.song); - c->playlist.list = g_list_append(c->playlist.list, (gpointer) song); - c->playlist.length++; + c->playlist.list = g_list_append(c->playlist.list, + (gpointer)song); + c->playlist.length++; + } + mpd_freeInfoEntity(entity); } - mpd_freeInfoEntity(entity); - } - c->playlist.id = c->status->playlist; - c->song = NULL; - c->playlist.updated = TRUE; - /* call playlist updated callbacks */ - mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL); + c->playlist.id = c->status->playlist; + c->song = NULL; + c->playlist.updated = TRUE; - return mpdclient_finish_command(c); + /* call playlist updated callbacks */ + mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL); + + return mpdclient_finish_command(c); } #ifdef ENABLE_PLCHANGES /* update playlist (plchanges) */ -gint +gint mpdclient_playlist_update_changes(mpdclient_t *c) { - mpd_InfoEntity *entity; + mpd_InfoEntity *entity; - D("mpdclient_playlist_update_changes() [%lld -> %lld]\n", - c->status->playlist, c->playlist.id); + D("mpdclient_playlist_update_changes() [%lld -> %lld]\n", + c->status->playlist, c->playlist.id); - if( MPD_ERROR(c) ) - return -1; + if (MPD_ERROR(c)) + return -1; - mpd_sendPlChangesCommand(c->connection, c->playlist.id); + mpd_sendPlChangesCommand(c->connection, c->playlist.id); - while( (entity=mpd_getNextInfoEntity(c->connection)) != NULL ) - { - mpd_Song *song = entity->info.song; + while ((entity = mpd_getNextInfoEntity(c->connection)) != NULL) { + mpd_Song *song = entity->info.song; - if( song->pos < c->playlist.length ) - { - GList *item = g_list_nth(c->playlist.list, song->pos); + if (song->pos < c->playlist.length) { + GList *item = g_list_nth(c->playlist.list, song->pos); - /* update song */ - D("updating pos:%d, id=%d [%p] - %s\n", - song->pos, song->id, item, song->file); - mpd_freeSong((mpd_Song *) item->data); - item->data = mpd_songDup(song); + /* update song */ + D("updating pos:%d, id=%d [%p] - %s\n", + song->pos, song->id, item, song->file); + mpd_freeSong((mpd_Song *) item->data); + item->data = mpd_songDup(song); + } else { + /* add a new song */ + D("adding song at pos %d\n", song->pos); + c->playlist.list = g_list_append(c->playlist.list, + (gpointer)mpd_songDup(song)); + } } - else - { - /* add a new song */ - D("adding song at pos %d\n", song->pos); - c->playlist.list = g_list_append(c->playlist.list, - (gpointer) mpd_songDup(song)); - } - - } - /* remove trailing songs */ - while( c->status->playlistLength < c->playlist.length ) - { - GList *item = g_list_last(c->playlist.list); + /* remove trailing songs */ + while (c->status->playlistLength < c->playlist.length) { + GList *item = g_list_last(c->playlist.list); - /* Remove the last playlist entry */ - D("removing song at pos %d\n", ((mpd_Song *) item->data)->pos); - mpd_freeSong((mpd_Song *) item->data); - c->playlist.list = g_list_delete_link(c->playlist.list, item); - c->playlist.length = g_list_length(c->playlist.list); - } + /* Remove the last playlist entry */ + D("removing song at pos %d\n", ((mpd_Song *) item->data)->pos); + mpd_freeSong((mpd_Song *) item->data); + c->playlist.list = g_list_delete_link(c->playlist.list, item); + c->playlist.length = g_list_length(c->playlist.list); + } - c->song = NULL; - c->playlist.id = c->status->playlist; - c->playlist.updated = TRUE; - c->playlist.length = g_list_length(c->playlist.list); + c->song = NULL; + c->playlist.id = c->status->playlist; + c->playlist.updated = TRUE; + c->playlist.length = g_list_length(c->playlist.list); - mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL); + mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL); - return 0; + return 0; } #else -gint +gint mpdclient_playlist_update_changes(mpdclient_t *c) { - return mpdclient_playlist_update(c); + return mpdclient_playlist_update(c); } #endif mpd_Song * playlist_get_song(mpdclient_t *c, gint index) { - return (mpd_Song *) g_list_nth_data(c->playlist.list, index); + return (mpd_Song *) g_list_nth_data(c->playlist.list, index); } GList * playlist_lookup(mpdclient_t *c, int id) { - GList *list = g_list_first(c->playlist.list); + GList *list = g_list_first(c->playlist.list); - while( list ) - { - mpd_Song *song = (mpd_Song *) list->data; - if( song->id == id ) - return list; - list=list->next; - } - return NULL; + while (list) { + mpd_Song *song = (mpd_Song *) list->data; + if( song->id == id ) + return list; + list=list->next; + } + + return NULL; } mpd_Song * playlist_lookup_song(mpdclient_t *c, gint id) { - GList *list = c->playlist.list; + GList *list = c->playlist.list; - while( list ) - { - mpd_Song *song = (mpd_Song *) list->data; - if( song->id == id ) - return song; - list=list->next; - } - return NULL; + while (list) { + mpd_Song *song = (mpd_Song *) list->data; + if (song->id == id) + return song; + list=list->next; + } + + return NULL; } -gint +gint playlist_get_index(mpdclient_t *c, mpd_Song *song) { - return g_list_index(c->playlist.list, song); + return g_list_index(c->playlist.list, song); } -gint +gint playlist_get_index_from_id(mpdclient_t *c, gint id) { - return g_list_index(c->playlist.list, playlist_lookup_song(c, id)); + return g_list_index(c->playlist.list, playlist_lookup_song(c, id)); } gint @@ -972,18 +970,19 @@ mpdclient_filelist_search_utf8(mpdclient_t *c, mpdclient_filelist_t * mpdclient_filelist_search(mpdclient_t *c, - int exact_match, - int table, + int exact_match, + int table, gchar *filter) { - mpdclient_filelist_t *filelist; - gchar *filter_utf8 = locale_to_utf8(filter); + mpdclient_filelist_t *filelist; + gchar *filter_utf8 = locale_to_utf8(filter); - D("mpdclient_filelist_search(%s)\n", filter); - filelist = mpdclient_filelist_search_utf8(c,exact_match,table,filter_utf8); - g_free(filter_utf8); + D("mpdclient_filelist_search(%s)\n", filter); + filelist = mpdclient_filelist_search_utf8(c, exact_match, table, + filter_utf8); + g_free(filter_utf8); - return filelist; + return filelist; } mpdclient_filelist_t * diff --git a/src/options.c b/src/options.c index ab383a38f..60101c478 100644 --- a/src/options.c +++ b/src/options.c @@ -38,22 +38,19 @@ #define ERROR_GOT_ARGUMENT 0x03 #define ERROR_MISSING_ARGUMENT 0x04 - - -typedef struct -{ - int shortopt; - char *longopt; - char *argument; - char *descrition; +typedef struct { + int shortopt; + char *longopt; + char *argument; + char *descrition; } arg_opt_t; -typedef void (*option_callback_fn_t) (int c, char *arg); +typedef void (*option_callback_fn_t)(int c, char *arg); options_t options; - + static arg_opt_t option_table[] = { { '?', "help", NULL, "Show this help message" }, { 'V', "version", NULL, "Display version information" }, @@ -238,138 +235,126 @@ handle_option(int c, char *arg) options_t * options_parse(int argc, const char *argv[]) { - int i; - arg_opt_t *opt = NULL; - option_callback_fn_t option_cb = handle_option; + int i; + arg_opt_t *opt = NULL; + option_callback_fn_t option_cb = handle_option; - i=1; - while( i<argc ) - { - char *arg = (char *) argv[i]; - size_t len=strlen(arg); - - /* check for a long option */ - if( g_str_has_prefix(arg, "--") ) - { - char *name, *value; - - /* make shure we got an argument for the previous option */ - if( opt && opt->argument ) - option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument); - - /* retreive a option argument */ - if( (value=g_strrstr(arg+2, "=")) ) - { - *value = '\0'; - name = g_strdup(arg); - *value = '='; - value++; - } - else - name = g_strdup(arg); - - /* check if the option exists */ - if( (opt=lookup_option(0, name+2)) == NULL ) - option_error(ERROR_UNKNOWN_OPTION, name, NULL); - g_free(name); - - /* abort if we got an argument to the option and dont want one */ - if( value && opt->argument==NULL ) - option_error(ERROR_GOT_ARGUMENT, arg, value); - - /* execute option callback */ - if( value || opt->argument==NULL ) - { - option_cb (opt->shortopt, value); - opt = NULL; - } - } - /* check for short options */ - else if( len>=2 && g_str_has_prefix(arg, "-") ) - { - int j; - - for(j=1; j<len; j++) - { - /* make shure we got an argument for the previous option */ - if( opt && opt->argument ) - option_error(ERROR_MISSING_ARGUMENT, - opt->longopt, opt->argument); - - /* check if the option exists */ - if( (opt=lookup_option(arg[j], NULL))==NULL ) - option_error(ERROR_UNKNOWN_OPTION, arg, NULL); - - /* if no option argument is needed execute callback */ - if( opt->argument==NULL ) - { - option_cb (opt->shortopt, NULL); - opt = NULL; + i=1; + while (i < argc) { + char *arg = (char *) argv[i]; + size_t len = strlen(arg); + + /* check for a long option */ + if (g_str_has_prefix(arg, "--")) { + char *name, *value; + + /* make shure we got an argument for the previous option */ + if( opt && opt->argument ) + option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument); + + /* retreive a option argument */ + if ((value=g_strrstr(arg+2, "="))) { + *value = '\0'; + name = g_strdup(arg); + *value = '='; + value++; + } else + name = g_strdup(arg); + + /* check if the option exists */ + if( (opt=lookup_option(0, name+2)) == NULL ) + option_error(ERROR_UNKNOWN_OPTION, name, NULL); + g_free(name); + + /* abort if we got an argument to the option and dont want one */ + if( value && opt->argument==NULL ) + option_error(ERROR_GOT_ARGUMENT, arg, value); + + /* execute option callback */ + if (value || opt->argument==NULL) { + option_cb (opt->shortopt, value); + opt = NULL; + } } - } - } - else - { - /* is this a option argument? */ - if( opt && opt->argument) - { - option_cb (opt->shortopt, arg); - opt = NULL; - } - else - option_error(ERROR_BAD_ARGUMENT, arg, NULL); + /* check for short options */ + else if (len>=2 && g_str_has_prefix(arg, "-")) { + int j; + + for(j=1; j<len; j++) { + /* make shure we got an argument for the previous option */ + if( opt && opt->argument ) + option_error(ERROR_MISSING_ARGUMENT, + opt->longopt, opt->argument); + + /* check if the option exists */ + if( (opt=lookup_option(arg[j], NULL))==NULL ) + option_error(ERROR_UNKNOWN_OPTION, arg, NULL); + + /* if no option argument is needed execute callback */ + if( opt->argument==NULL ) { + option_cb (opt->shortopt, NULL); + opt = NULL; + } + } + } else { + /* is this a option argument? */ + if( opt && opt->argument) { + option_cb (opt->shortopt, arg); + opt = NULL; + } else + option_error(ERROR_BAD_ARGUMENT, arg, NULL); + } + i++; } - i++; - } - - if( opt && opt->argument==NULL) - option_cb (opt->shortopt, NULL); - else if( opt && opt->argument ) - option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument); - return &options; -} + if( opt && opt->argument==NULL) + option_cb (opt->shortopt, NULL); + else if( opt && opt->argument ) + option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument); + return &options; +} options_t * options_init( void ) { - const char *value; - char *tmp; + const char *value; + char *tmp; - memset(&options, 0, sizeof(options_t)); + memset(&options, 0, sizeof(options_t)); - /* get initial values for host and password from MPD_HOST (enviroment) */ - if( (value=g_getenv(MPD_HOST_ENV)) ) - options.host = g_strdup(value); - else - options.host = g_strdup(DEFAULT_HOST); - if( (tmp=g_strstr_len(options.host, strlen(options.host), "@")) ) - { - char *oldhost = options.host; - *tmp = '\0'; - options.password = locale_to_utf8(oldhost); - options.host = g_strdup(tmp+1); - g_free(oldhost); - } - /* get initial values for port from MPD_PORT (enviroment) */ - if( (value=g_getenv(MPD_PORT_ENV)) ) - options.port = atoi(value); - else - options.port = DEFAULT_PORT; - - /* default option values */ - options.reconnect = TRUE; - options.find_wrap = TRUE; - options.wide_cursor = TRUE; - options.audible_bell = TRUE; - options.crossfade_time = DEFAULT_CROSSFADE_TIME; - options.seek_time = 1; - options.screen_list = g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0); - options.timedisplay_type = DEFAULT_TIMEDISPLAY_TYPE; - options.lyrics_timeout = DEFAULT_LYRICS_TIMEOUT; - options.scroll = DEFAULT_SCROLL; - options.scroll_sep = g_strdup(DEFAULT_SCROLL_SEP); - - return &options; + /* get initial values for host and password from MPD_HOST (enviroment) */ + if ((value=g_getenv(MPD_HOST_ENV))) + options.host = g_strdup(value); + else + options.host = g_strdup(DEFAULT_HOST); + + if ((tmp=g_strstr_len(options.host, strlen(options.host), "@"))) { + char *oldhost = options.host; + *tmp = '\0'; + options.password = locale_to_utf8(oldhost); + options.host = g_strdup(tmp+1); + g_free(oldhost); + } + + /* get initial values for port from MPD_PORT (enviroment) */ + if ((value=g_getenv(MPD_PORT_ENV))) + options.port = atoi(value); + else + options.port = DEFAULT_PORT; + + /* default option values */ + options.reconnect = TRUE; + options.find_wrap = TRUE; + options.wide_cursor = TRUE; + options.audible_bell = TRUE; + options.crossfade_time = DEFAULT_CROSSFADE_TIME; + options.seek_time = 1; + options.screen_list = g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0); + options.timedisplay_type = DEFAULT_TIMEDISPLAY_TYPE; + options.lyrics_timeout = DEFAULT_LYRICS_TIMEOUT; + options.scroll = DEFAULT_SCROLL; + options.scroll_sep = g_strdup(DEFAULT_SCROLL_SEP); + + return &options; } diff --git a/src/options.h b/src/options.h index 661d87440..e2b3b9b48 100644 --- a/src/options.h +++ b/src/options.h @@ -2,39 +2,37 @@ #define MPD_HOST_ENV "MPD_HOST" #define MPD_PORT_ENV "MPD_PORT" -typedef struct -{ - char *host; - char *username; - char *password; - char *config_file; - char *key_file; - char *list_format; - char *status_format; - char *xterm_title_format; - char *scroll_sep; - char **screen_list; - char *timedisplay_type; - int port; - int crossfade_time; - int search_mode; - int hide_cursor; - int seek_time; - int lyrics_timeout; - gboolean reconnect; - gboolean debug; - gboolean find_wrap; - gboolean find_show_last_pattern; - gboolean list_wrap; - gboolean auto_center; - gboolean wide_cursor; - gboolean enable_colors; - gboolean audible_bell; - gboolean visible_bell; - gboolean enable_xterm_title; - gboolean enable_mouse; - gboolean scroll; - +typedef struct { + char *host; + char *username; + char *password; + char *config_file; + char *key_file; + char *list_format; + char *status_format; + char *xterm_title_format; + char *scroll_sep; + char **screen_list; + char *timedisplay_type; + int port; + int crossfade_time; + int search_mode; + int hide_cursor; + int seek_time; + int lyrics_timeout; + gboolean reconnect; + gboolean debug; + gboolean find_wrap; + gboolean find_show_last_pattern; + gboolean list_wrap; + gboolean auto_center; + gboolean wide_cursor; + gboolean enable_colors; + gboolean audible_bell; + gboolean visible_bell; + gboolean enable_xterm_title; + gboolean enable_mouse; + gboolean scroll; } options_t; extern options_t options; diff --git a/src/screen.c b/src/screen.c index 4949e283f..911c94c28 100644 --- a/src/screen.c +++ b/src/screen.c @@ -1,4 +1,4 @@ -/* +/* * $Id$ * * (c) 2004 by Kalle Wallin <kaw@linux.se> @@ -48,7 +48,7 @@ #define SCREEN_KEYDEF_ID 101 #define SCREEN_CLOCK_ID 102 #define SCREEN_SEARCH_ID 103 -#define SCREEN_LYRICS_ID 104 +#define SCREEN_LYRICS_ID 104 @@ -66,32 +66,32 @@ 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; + gint id; + gchar *name; + screen_get_mode_functions_fn_t get_mode_functions; } screen_mode_info_t; static screen_mode_info_t screens[] = { - { SCREEN_PLAYLIST_ID, "playlist", get_screen_playlist }, - { SCREEN_BROWSE_ID, "browse", get_screen_browse }, + { SCREEN_PLAYLIST_ID, "playlist", get_screen_playlist }, + { SCREEN_BROWSE_ID, "browse", get_screen_browse }, #ifdef ENABLE_ARTIST_SCREEN - { SCREEN_ARTIST_ID, "artist", get_screen_artist }, + { SCREEN_ARTIST_ID, "artist", get_screen_artist }, #endif - { SCREEN_HELP_ID, "help", get_screen_help }, + { SCREEN_HELP_ID, "help", get_screen_help }, #ifdef ENABLE_SEARCH_SCREEN - { SCREEN_SEARCH_ID, "search", get_screen_search }, + { SCREEN_SEARCH_ID, "search", get_screen_search }, #endif #ifdef ENABLE_KEYDEF_SCREEN - { SCREEN_KEYDEF_ID, "keydef", get_screen_keydef }, + { SCREEN_KEYDEF_ID, "keydef", get_screen_keydef }, #endif #ifdef ENABLE_CLOCK_SCREEN - { SCREEN_CLOCK_ID, "clock", get_screen_clock }, + { SCREEN_CLOCK_ID, "clock", get_screen_clock }, #endif - #ifdef ENABLE_LYRICS_SCREEN - { SCREEN_LYRICS_ID, "lyrics", get_screen_lyrics }, +#ifdef ENABLE_LYRICS_SCREEN + { SCREEN_LYRICS_ID, "lyrics", get_screen_lyrics }, #endif - { G_MAXINT, NULL, NULL } + { G_MAXINT, NULL, NULL } }; static gboolean welcome = TRUE; @@ -103,895 +103,856 @@ static int seek_target_time = 0; gint screen_get_id(char *name) { - gint i=0; - - while( screens[i].name ) - { - if( strcmp(name, screens[i].name) == 0 ) - return screens[i].id; - i++; - } - return -1; + gint i=0; + + while (screens[i].name) { + if (strcmp(name, screens[i].name) == 0) + return screens[i].id; + i++; + } + return -1; } -static gint +static gint lookup_mode(gint id) { - gint i=0; - - while( screens[i].name ) - { - if( screens[i].id == id ) - return i; - i++; - } - return -1; + gint i=0; + + while (screens[i].name) { + if (screens[i].id == id) + return i; + i++; + } + return -1; } gint get_cur_mode_id() { return screens[screen->mode].id; } + static void switch_screen_mode(gint id, mpdclient_t *c) { - gint new_mode; - - if( id == screens[screen->mode].id ) - return; - - /* close the old mode */ - if( mode_fn && mode_fn->close ) - mode_fn->close(); - - /* get functions for the new mode */ - new_mode = lookup_mode(id); - if( new_mode>=0 && screens[new_mode].get_mode_functions ) - { - D("switch_screen(%s)\n", screens[new_mode].name ); - mode_fn = screens[new_mode].get_mode_functions(); - screen->mode = new_mode; - } - - screen->painted = 0; - - /* open the new mode */ - if( mode_fn && mode_fn->open ) - mode_fn->open(screen, c); + gint new_mode; + + if( id == screens[screen->mode].id ) + return; + + /* close the old mode */ + if( mode_fn && mode_fn->close ) + mode_fn->close(); + + /* get functions for the new mode */ + new_mode = lookup_mode(id); + if (new_mode>=0 && screens[new_mode].get_mode_functions) { + D("switch_screen(%s)\n", screens[new_mode].name ); + mode_fn = screens[new_mode].get_mode_functions(); + screen->mode = new_mode; + } + screen->painted = 0; + + /* open the new mode */ + if (mode_fn && mode_fn->open) + mode_fn->open(screen, c); } static void screen_next_mode(mpdclient_t *c, int offset) { - int max = g_strv_length(options.screen_list); - int current, next; - int i; - - /* find current screen */ - current = -1; - i = 0; - while( options.screen_list[i] ) - { - if( strcmp(options.screen_list[i], screens[screen->mode].name) == 0 ) - current = i; - i++; - } - next = current + offset; - if( next<0 ) - next = max-1; - else if( next>=max ) - next = 0; - - D("current mode: %d:%d next:%d\n", current, max, next); - switch_screen_mode(screen_get_id(options.screen_list[next]), c); + int max = g_strv_length(options.screen_list); + int current, next; + int i; + + /* find current screen */ + current = -1; + i = 0; + while (options.screen_list[i]) { + if (strcmp(options.screen_list[i], + screens[screen->mode].name) == 0) + current = i; + i++; + } + + next = current + offset; + if (next<0) + next = max-1; + else if (next>=max) + next = 0; + + D("current mode: %d:%d next:%d\n", current, max, next); + switch_screen_mode(screen_get_id(options.screen_list[next]), c); } static void paint_top_window(char *header, mpdclient_t *c, int clear) { - char flags[5]; - static int prev_volume = -1; - static int 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; - } - - if(clear) - { - wmove(w, 0, 0); - wclrtoeol(w); - } - - if(prev_volume!=c->status->volume || clear) - { - char buf[32]; - - if( header[0] ) - { - colors_use(w, COLOR_TITLE_BOLD); - mvwaddstr(w, 0, 0, header); + char flags[5]; + static int prev_volume = -1; + static int 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; } - else - { - colors_use(w, COLOR_TITLE_BOLD); - waddstr(w, get_key_names(CMD_SCREEN_HELP, FALSE)); - colors_use(w, COLOR_TITLE); - waddstr(w, _(":Help ")); - colors_use(w, COLOR_TITLE_BOLD); - waddstr(w, get_key_names(CMD_SCREEN_PLAY, FALSE)); - colors_use(w, COLOR_TITLE); - waddstr(w, _(":Playlist ")); - colors_use(w, COLOR_TITLE_BOLD); - waddstr(w, get_key_names(CMD_SCREEN_FILE, FALSE)); - colors_use(w, COLOR_TITLE); - waddstr(w, _(":Browse ")); + + if (clear) { + wmove(w, 0, 0); + wclrtoeol(w); + } + + if (prev_volume!=c->status->volume || clear) { + char buf[32]; + + if (header[0]) { + colors_use(w, COLOR_TITLE_BOLD); + mvwaddstr(w, 0, 0, header); + } else { + colors_use(w, COLOR_TITLE_BOLD); + waddstr(w, get_key_names(CMD_SCREEN_HELP, FALSE)); + colors_use(w, COLOR_TITLE); + waddstr(w, _(":Help ")); + colors_use(w, COLOR_TITLE_BOLD); + waddstr(w, get_key_names(CMD_SCREEN_PLAY, FALSE)); + colors_use(w, COLOR_TITLE); + waddstr(w, _(":Playlist ")); + colors_use(w, COLOR_TITLE_BOLD); + waddstr(w, get_key_names(CMD_SCREEN_FILE, FALSE)); + colors_use(w, COLOR_TITLE); + waddstr(w, _(":Browse ")); #ifdef ENABLE_ARTIST_SCREEN - colors_use(w, COLOR_TITLE_BOLD); - waddstr(w, get_key_names(CMD_SCREEN_ARTIST, FALSE)); - colors_use(w, COLOR_TITLE); - waddstr(w, _(":Artist ")); + colors_use(w, COLOR_TITLE_BOLD); + waddstr(w, get_key_names(CMD_SCREEN_ARTIST, FALSE)); + colors_use(w, COLOR_TITLE); + waddstr(w, _(":Artist ")); #endif #ifdef ENABLE_SEARCH_SCREEN - colors_use(w, COLOR_TITLE_BOLD); - waddstr(w, get_key_names(CMD_SCREEN_SEARCH, FALSE)); - colors_use(w, COLOR_TITLE); - waddstr(w, _(":Search ")); + colors_use(w, COLOR_TITLE_BOLD); + waddstr(w, get_key_names(CMD_SCREEN_SEARCH, FALSE)); + colors_use(w, COLOR_TITLE); + waddstr(w, _(":Search ")); #endif #ifdef ENABLE_LYRICS_SCREEN - colors_use(w, COLOR_TITLE_BOLD); - waddstr(w, get_key_names(CMD_SCREEN_LYRICS, FALSE)); - colors_use(w, COLOR_TITLE); - waddstr(w, _(":Lyrics ")); + colors_use(w, COLOR_TITLE_BOLD); + waddstr(w, get_key_names(CMD_SCREEN_LYRICS, FALSE)); + colors_use(w, COLOR_TITLE); + waddstr(w, _(":Lyrics ")); #endif + } + if (c->status->volume==MPD_STATUS_NO_VOLUME) { + g_snprintf(buf, 32, _("Volume n/a ")); + } else { + g_snprintf(buf, 32, _(" Volume %d%%"), c->status->volume); + } + colors_use(w, COLOR_TITLE); + mvwaddstr(w, 0, screen->top_window.cols-my_strlen(buf), buf); + + flags[0] = 0; + if( c->status->repeat ) + g_strlcat(flags, "r", sizeof(flags)); + if( c->status->random ) + g_strlcat(flags, "z", sizeof(flags));; + if( c->status->crossfade ) + g_strlcat(flags, "x", sizeof(flags)); + if( c->status->updatingDb ) + g_strlcat(flags, "U", sizeof(flags)); + colors_use(w, COLOR_LINE); + mvwhline(w, 1, 0, ACS_HLINE, screen->top_window.cols); + if (flags[0]) { + wmove(w,1,screen->top_window.cols-strlen(flags)-3); + waddch(w, '['); + colors_use(w, COLOR_LINE_BOLD); + waddstr(w, flags); + colors_use(w, COLOR_LINE); + waddch(w, ']'); + } + wnoutrefresh(w); } - if( c->status->volume==MPD_STATUS_NO_VOLUME ) - { - g_snprintf(buf, 32, _("Volume n/a ")); - } - else - { - g_snprintf(buf, 32, _(" Volume %d%%"), c->status->volume); - } - colors_use(w, COLOR_TITLE); - mvwaddstr(w, 0, screen->top_window.cols-my_strlen(buf), buf); - - flags[0] = 0; - if( c->status->repeat ) - g_strlcat(flags, "r", sizeof(flags)); - if( c->status->random ) - g_strlcat(flags, "z", sizeof(flags));; - if( c->status->crossfade ) - g_strlcat(flags, "x", sizeof(flags)); - if( c->status->updatingDb ) - g_strlcat(flags, "U", sizeof(flags)); - colors_use(w, COLOR_LINE); - mvwhline(w, 1, 0, ACS_HLINE, screen->top_window.cols); - if( flags[0] ) - { - wmove(w,1,screen->top_window.cols-strlen(flags)-3); - waddch(w, '['); - colors_use(w, COLOR_LINE_BOLD); - waddstr(w, flags); - colors_use(w, COLOR_LINE); - waddch(w, ']'); - } - wnoutrefresh(w); - } } static void paint_progress_window(mpdclient_t *c) { - double p; - int width; - int elapsedTime = c->status->elapsedTime; - - if( c->status==NULL || IS_STOPPED(c->status->state) ) - { - mvwhline(screen->progress_window.w, 0, 0, ACS_HLINE, - screen->progress_window.cols); - wnoutrefresh(screen->progress_window.w); - return; - } - - if( c->song && seek_id == c->song->id ) - elapsedTime = seek_target_time; - - p = ((double) elapsedTime) / ((double) c->status->totalTime); - - width = (int) (p * (double) screen->progress_window.cols); - mvwhline(screen->progress_window.w, - 0, 0, - ACS_HLINE, - screen->progress_window.cols); - whline(screen->progress_window.w, '=', width-1); - mvwaddch(screen->progress_window.w, 0, width-1, 'O'); - wnoutrefresh(screen->progress_window.w); + double p; + int width; + int elapsedTime = c->status->elapsedTime; + + if (c->status==NULL || IS_STOPPED(c->status->state)) { + mvwhline(screen->progress_window.w, 0, 0, ACS_HLINE, + screen->progress_window.cols); + wnoutrefresh(screen->progress_window.w); + return; + } + + if (c->song && seek_id == c->song->id) + elapsedTime = seek_target_time; + + p = ((double) elapsedTime) / ((double) c->status->totalTime); + + width = (int) (p * (double) screen->progress_window.cols); + mvwhline(screen->progress_window.w, + 0, 0, + ACS_HLINE, + screen->progress_window.cols); + whline(screen->progress_window.w, '=', width-1); + mvwaddch(screen->progress_window.w, 0, width-1, 'O'); + wnoutrefresh(screen->progress_window.w); } -static void +static void paint_status_window(mpdclient_t *c) { - WINDOW *w = screen->status_window.w; - mpd_Status *status = c->status; - mpd_Song *song = c->song; - int elapsedTime = 0; - char *str = NULL; - int x = 0; - - if( time(NULL) - screen->status_timestamp <= SCREEN_STATUS_MESSAGE_TIME ) - return; - - wmove(w, 0, 0); - wclrtoeol(w); - colors_use(w, COLOR_STATUS_BOLD); - - switch(status->state) - { - case MPD_STATUS_STATE_PLAY: - str = _("Playing:"); - break; - case MPD_STATUS_STATE_PAUSE: - str = _("[Paused]"); - break; - case MPD_STATUS_STATE_STOP: - default: - break; - } - - if( str ) - { - waddstr(w, str); - x += my_strlen(str)+1; - } - - /* create time string */ - memset(screen->buf, 0, screen->buf_size); - if( IS_PLAYING(status->state) || IS_PAUSED(status->state) ) - { - if( status->totalTime > 0 ) - { - /*checks the conf to see whether to display elapsed or remaining time */ - if(!strcmp(options.timedisplay_type,"elapsed")) - elapsedTime = c->status->elapsedTime; - else if(!strcmp(options.timedisplay_type,"remaining")) - elapsedTime = (c->status->totalTime - c->status->elapsedTime); - - if( c->song && seek_id == c->song->id ) - elapsedTime = seek_target_time; - /*write out the time, using hours if time over 60 minutes*/ - if (c->status->totalTime > 3600) - { - g_snprintf(screen->buf, screen->buf_size, - " [%i:%02i:%02i/%i:%02i:%02i]", - elapsedTime/3600, (elapsedTime%3600)/60, elapsedTime%60, - status->totalTime/3600, (status->totalTime%3600)/60, status->totalTime%60); - } - else - { - g_snprintf(screen->buf, screen->buf_size, - " [%i:%02i/%i:%02i]", - elapsedTime/60, elapsedTime%60, - status->totalTime/60, status->totalTime%60 ); - } + WINDOW *w = screen->status_window.w; + mpd_Status *status = c->status; + mpd_Song *song = c->song; + int elapsedTime = 0; + char *str = NULL; + int x = 0; + + if( time(NULL) - screen->status_timestamp <= SCREEN_STATUS_MESSAGE_TIME ) + return; + + wmove(w, 0, 0); + wclrtoeol(w); + colors_use(w, COLOR_STATUS_BOLD); + + switch(status->state) { + case MPD_STATUS_STATE_PLAY: + str = _("Playing:"); + break; + case MPD_STATUS_STATE_PAUSE: + str = _("[Paused]"); + break; + case MPD_STATUS_STATE_STOP: + default: + break; + } + + if (str) { + waddstr(w, str); + x += my_strlen(str)+1; + } + + /* create time string */ + memset(screen->buf, 0, screen->buf_size); + if (IS_PLAYING(status->state) || IS_PAUSED(status->state)) { + if (status->totalTime > 0) { + /*checks the conf to see whether to display elapsed or remaining time */ + if(!strcmp(options.timedisplay_type,"elapsed")) + elapsedTime = c->status->elapsedTime; + else if(!strcmp(options.timedisplay_type,"remaining")) + elapsedTime = (c->status->totalTime - c->status->elapsedTime); + + if( c->song && seek_id == c->song->id ) + elapsedTime = seek_target_time; + /*write out the time, using hours if time over 60 minutes*/ + if (c->status->totalTime > 3600) { + g_snprintf(screen->buf, screen->buf_size, + " [%i:%02i:%02i/%i:%02i:%02i]", + elapsedTime/3600, (elapsedTime%3600)/60, elapsedTime%60, + status->totalTime/3600, (status->totalTime%3600)/60, status->totalTime%60); + } else { + g_snprintf(screen->buf, screen->buf_size, + " [%i:%02i/%i:%02i]", + elapsedTime/60, elapsedTime%60, + status->totalTime/60, status->totalTime%60 ); + } + } else { + g_snprintf(screen->buf, screen->buf_size, + " [%d kbps]", status->bitRate ); + } + } else { + time_t timep; + + time(&timep); + strftime(screen->buf, screen->buf_size, "%X ",localtime(&timep)); } - else - { - g_snprintf(screen->buf, screen->buf_size, - " [%d kbps]", status->bitRate ); + + /* display song */ + if (IS_PLAYING(status->state) || IS_PAUSED(status->state)) { + char songname[MAX_SONGNAME_LENGTH]; + int width = COLS-x-my_strlen(screen->buf); + + if (song) + strfsong(songname, MAX_SONGNAME_LENGTH, STATUS_FORMAT, song); + else + songname[0] = '\0'; + + colors_use(w, COLOR_STATUS); + /* scroll if the song name is to long */ + if (options.scroll && my_strlen(songname) > width) { + static scroll_state_t st = { 0, 0 }; + char *tmp = strscroll(songname, options.scroll_sep, width, &st); + + g_strlcpy(songname, tmp, MAX_SONGNAME_LENGTH); + g_free(tmp); + } + //mvwaddnstr(w, 0, x, songname, width); + mvwaddstr(w, 0, x, songname); } - } - else - { - time_t timep; - - time(&timep); - strftime(screen->buf, screen->buf_size, "%X ",localtime(&timep)); - } - - /* display song */ - if( (IS_PLAYING(status->state) || IS_PAUSED(status->state)) ) - { - char songname[MAX_SONGNAME_LENGTH]; - int width = COLS-x-my_strlen(screen->buf); - - if( song ) - strfsong(songname, MAX_SONGNAME_LENGTH, STATUS_FORMAT, song); - else - songname[0] = '\0'; - - colors_use(w, COLOR_STATUS); - /* scroll if the song name is to long */ - if( options.scroll && my_strlen(songname) > width ) - { - static scroll_state_t st = { 0, 0 }; - char *tmp = strscroll(songname, options.scroll_sep, width, &st); - - g_strlcpy(songname, tmp, MAX_SONGNAME_LENGTH); - g_free(tmp); + + /* display time string */ + if (screen->buf[0]) { + x = screen->status_window.cols - strlen(screen->buf); + colors_use(w, COLOR_STATUS_TIME); + mvwaddstr(w, 0, x, screen->buf); } - //mvwaddnstr(w, 0, x, songname, width); - mvwaddstr(w, 0, x, songname); - } - - /* display time string */ - if( screen->buf[0] ) - { - x = screen->status_window.cols - strlen(screen->buf); - colors_use(w, COLOR_STATUS_TIME); - mvwaddstr(w, 0, x, screen->buf); - } - - wnoutrefresh(w); + + wnoutrefresh(w); } int screen_exit(void) { - endwin(); - if( screen ) - { - gint i; - - /* 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(); - - if( mode_fn && mode_fn->close ) - mode_fn->close(); - if( mode_fn && mode_fn->exit ) - mode_fn->exit(); - - i++; + endwin(); + if (screen) { + gint i; + + /* 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(); + + if (mode_fn && mode_fn->close) + mode_fn->close(); + if (mode_fn && mode_fn->exit) + mode_fn->exit(); + + i++; + } + + string_list_free(screen->find_history); + g_free(screen->buf); + g_free(screen->findbuf); + + g_free(screen); + screen = NULL; } - - string_list_free(screen->find_history); - g_free(screen->buf); - g_free(screen->findbuf); - - g_free(screen); - screen = NULL; - } - return 0; + return 0; } void screen_resize(void) { - gint i; - - D("Resize rows %d->%d, cols %d->%d\n",screen->rows,LINES,screen->cols,COLS); - if( COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS ) - { - screen_exit(); - fprintf(stderr, _("Error: Screen to small!\n")); - exit(EXIT_FAILURE); - } - - resizeterm(LINES, COLS); - - screen->cols = COLS; - screen->rows = LINES; - - /* top window */ - screen->top_window.cols = screen->cols; - wresize(screen->top_window.w, 2, screen->cols); - - /* main window */ - screen->main_window.cols = screen->cols; - screen->main_window.rows = screen->rows-4; - wresize(screen->main_window.w, screen->main_window.rows, screen->cols); - wclear(screen->main_window.w); - - /* progress window */ - screen->progress_window.cols = screen->cols; - wresize(screen->progress_window.w, 1, screen->cols); - mvwin(screen->progress_window.w, screen->rows-2, 0); - - /* status window */ - screen->status_window.cols = screen->cols; - wresize(screen->status_window.w, 1, screen->cols); - mvwin(screen->status_window.w, screen->rows-1, 0); - - screen->buf_size = screen->cols; - g_free(screen->buf); - screen->buf = g_malloc(screen->cols); - - /* 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(); - - if( mode_fn && mode_fn->resize ) - mode_fn->resize(screen->main_window.cols, screen->main_window.rows); - - i++; - } - - /* ? - without this the cursor becomes visible with aterm & Eterm */ - curs_set(1); - curs_set(0); - - screen->painted = 0; + gint i; + + D("Resize rows %d->%d, cols %d->%d\n",screen->rows,LINES,screen->cols,COLS); + if (COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS) { + screen_exit(); + fprintf(stderr, _("Error: Screen to small!\n")); + exit(EXIT_FAILURE); + } + + resizeterm(LINES, COLS); + + screen->cols = COLS; + screen->rows = LINES; + + /* top window */ + screen->top_window.cols = screen->cols; + wresize(screen->top_window.w, 2, screen->cols); + + /* main window */ + screen->main_window.cols = screen->cols; + screen->main_window.rows = screen->rows-4; + wresize(screen->main_window.w, screen->main_window.rows, screen->cols); + wclear(screen->main_window.w); + + /* progress window */ + screen->progress_window.cols = screen->cols; + wresize(screen->progress_window.w, 1, screen->cols); + mvwin(screen->progress_window.w, screen->rows-2, 0); + + /* status window */ + screen->status_window.cols = screen->cols; + wresize(screen->status_window.w, 1, screen->cols); + mvwin(screen->status_window.w, screen->rows-1, 0); + + screen->buf_size = screen->cols; + g_free(screen->buf); + screen->buf = g_malloc(screen->cols); + + /* 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(); + + if (mode_fn && mode_fn->resize) + mode_fn->resize(screen->main_window.cols, screen->main_window.rows); + + i++; + } + + /* ? - without this the cursor becomes visible with aterm & Eterm */ + curs_set(1); + curs_set(0); + + screen->painted = 0; } -void +void screen_status_message(char *msg) { - WINDOW *w = screen->status_window.w; - - wmove(w, 0, 0); - wclrtoeol(w); - colors_use(w, COLOR_STATUS_ALERT); - waddstr(w, msg); - wnoutrefresh(w); - screen->status_timestamp = time(NULL); + WINDOW *w = screen->status_window.w; + + wmove(w, 0, 0); + wclrtoeol(w); + colors_use(w, COLOR_STATUS_ALERT); + waddstr(w, msg); + wnoutrefresh(w); + screen->status_timestamp = time(NULL); } -void +void screen_status_printf(char *format, ...) { - char *msg; - va_list ap; - - va_start(ap,format); - msg = g_strdup_vprintf(format,ap); - va_end(ap); - screen_status_message(msg); - g_free(msg); + char *msg; + va_list ap; + + va_start(ap,format); + msg = g_strdup_vprintf(format,ap); + va_end(ap); + screen_status_message(msg); + g_free(msg); } void ncurses_init() { - /* initialize the curses library */ - initscr(); - /* initialize color support */ - colors_start(); - /* tell curses not to do NL->CR/NL on output */ - nonl(); - /* use raw mode (ignore interrupt,quit,suspend, and flow control ) */ + /* initialize the curses library */ + initscr(); + /* initialize color support */ + colors_start(); + /* tell curses not to do NL->CR/NL on output */ + nonl(); + /* use raw mode (ignore interrupt,quit,suspend, and flow control ) */ #ifdef ENABLE_RAW_MODE - // raw(); + // raw(); #endif - /* don't echo input */ - noecho(); - /* set cursor invisible */ - curs_set(0); - /* enable extra keys */ - keypad(stdscr, TRUE); - /* return from getch() without blocking */ - timeout(SCREEN_TIMEOUT); - /* initialize mouse support */ + /* don't echo input */ + noecho(); + /* set cursor invisible */ + curs_set(0); + /* enable extra keys */ + keypad(stdscr, TRUE); + /* return from getch() without blocking */ + timeout(SCREEN_TIMEOUT); + /* initialize mouse support */ #ifdef HAVE_GETMOUSE - if( options.enable_mouse ) - mousemask(ALL_MOUSE_EVENTS, NULL); + if( options.enable_mouse ) + mousemask(ALL_MOUSE_EVENTS, NULL); #endif - if( COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS ) - { - fprintf(stderr, _("Error: Screen to small!\n")); - exit(EXIT_FAILURE); - } - screen = g_malloc(sizeof(screen_t)); - memset(screen, 0, sizeof(screen_t)); - screen->mode = 0; - screen->cols = COLS; - screen->rows = LINES; - - screen->buf = g_malloc(screen->cols); - screen->buf_size = screen->cols; - screen->findbuf = NULL; - screen->painted = 0; - screen->start_timestamp = time(NULL); - screen->input_timestamp = time(NULL); - screen->last_cmd = CMD_NONE; - - /* create top window */ - screen->top_window.rows = 2; - screen->top_window.cols = screen->cols; - screen->top_window.w = newwin(screen->top_window.rows, - screen->top_window.cols, - 0, 0); - leaveok(screen->top_window.w, TRUE); - keypad(screen->top_window.w, TRUE); - - /* create main window */ - screen->main_window.rows = screen->rows-4; - screen->main_window.cols = screen->cols; - screen->main_window.w = newwin(screen->main_window.rows, - screen->main_window.cols, - 2, - 0); - - // leaveok(screen->main_window.w, TRUE); temporary disabled - keypad(screen->main_window.w, TRUE); - - /* create progress window */ - screen->progress_window.rows = 1; - screen->progress_window.cols = screen->cols; - screen->progress_window.w = newwin(screen->progress_window.rows, - screen->progress_window.cols, - screen->rows-2, - 0); - leaveok(screen->progress_window.w, TRUE); - - /* create status window */ - screen->status_window.rows = 1; - screen->status_window.cols = screen->cols; - screen->status_window.w = newwin(screen->status_window.rows, - screen->status_window.cols, - screen->rows-1, - 0); - - leaveok(screen->status_window.w, FALSE); - keypad(screen->status_window.w, TRUE); - - if( options.enable_colors ) - { - /* set background attributes */ - wbkgd(stdscr, COLOR_PAIR(COLOR_LIST)); - wbkgd(screen->main_window.w, COLOR_PAIR(COLOR_LIST)); - wbkgd(screen->top_window.w, COLOR_PAIR(COLOR_TITLE)); - wbkgd(screen->progress_window.w, COLOR_PAIR(COLOR_PROGRESSBAR)); - wbkgd(screen->status_window.w, COLOR_PAIR(COLOR_STATUS)); - colors_use(screen->progress_window.w, COLOR_PROGRESSBAR); - } + if( COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS ) + { + fprintf(stderr, _("Error: Screen to small!\n")); + exit(EXIT_FAILURE); + } + screen = g_malloc(sizeof(screen_t)); + memset(screen, 0, sizeof(screen_t)); + screen->mode = 0; + screen->cols = COLS; + screen->rows = LINES; + + screen->buf = g_malloc(screen->cols); + screen->buf_size = screen->cols; + screen->findbuf = NULL; + screen->painted = 0; + screen->start_timestamp = time(NULL); + screen->input_timestamp = time(NULL); + screen->last_cmd = CMD_NONE; + + /* create top window */ + screen->top_window.rows = 2; + screen->top_window.cols = screen->cols; + screen->top_window.w = newwin(screen->top_window.rows, + screen->top_window.cols, + 0, 0); + leaveok(screen->top_window.w, TRUE); + keypad(screen->top_window.w, TRUE); + + /* create main window */ + screen->main_window.rows = screen->rows-4; + screen->main_window.cols = screen->cols; + screen->main_window.w = newwin(screen->main_window.rows, + screen->main_window.cols, + 2, + 0); + + // leaveok(screen->main_window.w, TRUE); temporary disabled + keypad(screen->main_window.w, TRUE); + + /* create progress window */ + screen->progress_window.rows = 1; + screen->progress_window.cols = screen->cols; + screen->progress_window.w = newwin(screen->progress_window.rows, + screen->progress_window.cols, + screen->rows-2, + 0); + leaveok(screen->progress_window.w, TRUE); + + /* create status window */ + screen->status_window.rows = 1; + screen->status_window.cols = screen->cols; + screen->status_window.w = newwin(screen->status_window.rows, + screen->status_window.cols, + screen->rows-1, + 0); + + leaveok(screen->status_window.w, FALSE); + keypad(screen->status_window.w, TRUE); + + if( options.enable_colors ) + { + /* set background attributes */ + wbkgd(stdscr, COLOR_PAIR(COLOR_LIST)); + wbkgd(screen->main_window.w, COLOR_PAIR(COLOR_LIST)); + wbkgd(screen->top_window.w, COLOR_PAIR(COLOR_TITLE)); + wbkgd(screen->progress_window.w, COLOR_PAIR(COLOR_PROGRESSBAR)); + wbkgd(screen->status_window.w, COLOR_PAIR(COLOR_STATUS)); + colors_use(screen->progress_window.w, COLOR_PROGRESSBAR); + } } screen_init(mpdclient_t *c) { - gint i; + gint i; - /* initialize screens */ - i=0; - while( screens[i].get_mode_functions ) - { - screen_functions_t *fn = screens[i].get_mode_functions(); + /* initialize screens */ + i=0; + while( screens[i].get_mode_functions ) + { + screen_functions_t *fn = screens[i].get_mode_functions(); - if( fn && fn->init ) - fn->init(screen->main_window.w, - screen->main_window.cols, - screen->main_window.rows); + if( fn && 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); + /* broken */ + mode_fn = NULL; + switch_screen_mode(screen_get_id(options.screen_list[0]), c); #else - mode_fn = get_screen_playlist(); + mode_fn = get_screen_playlist(); #endif - if( mode_fn && mode_fn->open ) - mode_fn->open(screen, c); + if( mode_fn && mode_fn->open ) + mode_fn->open(screen, c); - /* initialize wreadln */ - wrln_wgetch = my_wgetch; - wrln_max_history_length = 16; + /* initialize wreadln */ + wrln_wgetch = my_wgetch; + wrln_max_history_length = 16; - return 0; + return 0; } -void +void screen_paint(mpdclient_t *c) { - char *title = NULL; - - if( mode_fn && mode_fn->get_title ) - title = mode_fn->get_title(screen->buf,screen->buf_size); - - D("screen_paint(%s)\n", title); - /* paint the title/header window */ - if( title ) - paint_top_window(title, c, 1); - else - paint_top_window("", c, 1); - - /* paint the main window */ - wclear(screen->main_window.w); - if( mode_fn && mode_fn->paint ) - mode_fn->paint(screen, c); - - paint_progress_window(c); - paint_status_window(c); - screen->painted = 1; - wmove(screen->main_window.w, 0, 0); - wnoutrefresh(screen->main_window.w); - - /* tell curses to update */ - doupdate(); + char *title = NULL; + + if (mode_fn && mode_fn->get_title) + title = mode_fn->get_title(screen->buf, screen->buf_size); + + D("screen_paint(%s)\n", title); + /* paint the title/header window */ + if( title ) + paint_top_window(title, c, 1); + else + paint_top_window("", c, 1); + + /* paint the main window */ + wclear(screen->main_window.w); + if( mode_fn && mode_fn->paint ) + mode_fn->paint(screen, c); + + paint_progress_window(c); + paint_status_window(c); + screen->painted = 1; + wmove(screen->main_window.w, 0, 0); + wnoutrefresh(screen->main_window.w); + + /* tell curses to update */ + doupdate(); } -void +void screen_update(mpdclient_t *c) { - static int repeat = -1; - static int random = -1; - static int crossfade = -1; - static int dbupdate = -1; - list_window_t *lw = NULL; - - if( !screen->painted ) - return screen_paint(c); - - /* print a message if mpd status has changed */ - if( repeat<0 ) - { - repeat = c->status->repeat; - random = c->status->random; - crossfade = c->status->crossfade; - dbupdate = c->status->updatingDb; - } - if( repeat != c->status->repeat ) - screen_status_printf(c->status->repeat ? - _("Repeat is on") : - _("Repeat is off")); - if( random != c->status->random ) - screen_status_printf(c->status->random ? - _("Random is on") : - _("Random is off")); - - if( crossfade != c->status->crossfade ) - screen_status_printf(_("Crossfade %d seconds"), c->status->crossfade); - if( dbupdate && dbupdate != c->status->updatingDb ) - { - screen_status_printf(_("Database updated!")); - mpdclient_browse_callback(c, BROWSE_DB_UPDATED, NULL); - } - - repeat = c->status->repeat; - random = c->status->random; - crossfade = c->status->crossfade; - dbupdate = c->status->updatingDb; - - /* update title/header window */ - if( welcome && screen->last_cmd==CMD_NONE && - time(NULL)-screen->start_timestamp <= SCREEN_WELCOME_TIME) - paint_top_window("", c, 0); - else if( mode_fn && mode_fn->get_title ) - { - paint_top_window(mode_fn->get_title(screen->buf,screen->buf_size), c, 0); - welcome = FALSE; - } - else - paint_top_window("", c, 0); - - /* update the main window */ - if( mode_fn && mode_fn->paint ) - mode_fn->update(screen, c); - - if( mode_fn && mode_fn->get_lw ) - lw = mode_fn->get_lw(); - - /* update progress window */ - paint_progress_window(c); - - /* update status window */ - paint_status_window(c); - - /* move the cursor to the selected row in the main window */ - if( lw ) - wmove(screen->main_window.w, LW_ROW(lw), 0); - else - wmove(screen->main_window.w, 0, 0); - wnoutrefresh(screen->main_window.w); - - /* tell curses to update */ - doupdate(); + static int repeat = -1; + static int random = -1; + static int crossfade = -1; + static int dbupdate = -1; + list_window_t *lw = NULL; + + if( !screen->painted ) + return screen_paint(c); + + /* print a message if mpd status has changed */ + if (repeat < 0) { + repeat = c->status->repeat; + random = c->status->random; + crossfade = c->status->crossfade; + dbupdate = c->status->updatingDb; + } + + if (repeat != c->status->repeat) + screen_status_printf(c->status->repeat ? + _("Repeat is on") : + _("Repeat is off")); + + if (random != c->status->random) + screen_status_printf(c->status->random ? + _("Random is on") : + _("Random is off")); + + if (crossfade != c->status->crossfade) + screen_status_printf(_("Crossfade %d seconds"), c->status->crossfade); + + if (dbupdate && dbupdate != c->status->updatingDb) { + screen_status_printf(_("Database updated!")); + mpdclient_browse_callback(c, BROWSE_DB_UPDATED, NULL); + } + + repeat = c->status->repeat; + random = c->status->random; + crossfade = c->status->crossfade; + dbupdate = c->status->updatingDb; + + /* update title/header window */ + if (welcome && screen->last_cmd==CMD_NONE && + time(NULL)-screen->start_timestamp <= SCREEN_WELCOME_TIME) + paint_top_window("", c, 0); + else if (mode_fn && mode_fn->get_title) { + paint_top_window(mode_fn->get_title(screen->buf,screen->buf_size), c, 0); + welcome = FALSE; + } else + paint_top_window("", c, 0); + + /* update the main window */ + if (mode_fn && mode_fn->paint) + mode_fn->update(screen, c); + + if (mode_fn && mode_fn->get_lw) + lw = mode_fn->get_lw(); + + /* update progress window */ + paint_progress_window(c); + + /* update status window */ + paint_status_window(c); + + /* move the cursor to the selected row in the main window */ + if (lw) + wmove(screen->main_window.w, LW_ROW(lw), 0); + else + wmove(screen->main_window.w, 0, 0); + wnoutrefresh(screen->main_window.w); + + /* tell curses to update */ + doupdate(); } void screen_idle(mpdclient_t *c) { - 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_NONE; - seek_id = -1; + 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_NONE; + seek_id = -1; } #ifdef HAVE_GETMOUSE int screen_get_mouse_event(mpdclient_t *c, - list_window_t *lw, int lw_length, + list_window_t *lw, int lw_length, unsigned long *bstate, int *row) { - MEVENT event; - - /* retreive the mouse event from ncurses */ - getmouse(&event); - D("mouse: id=%d y=%d,x=%d,z=%d\n",event.id,event.y,event.x,event.z); - /* calculate the selected row in the list window */ - *row = event.y - screen->top_window.rows; - /* copy button state bits */ - *bstate = event.bstate; - /* if button 2 was pressed switch screen */ - if( event.bstate & BUTTON2_CLICKED ) - { - screen_cmd(c, CMD_SCREEN_NEXT); - return 1; - } - /* if the even occured above the list window move up */ - if( *row<0 && lw ) - { - if( event.bstate & BUTTON3_CLICKED ) - list_window_first(lw); - else - list_window_previous_page(lw); - return 1; - } - /* if the even occured below the list window move down */ - if( *row>=lw->rows && lw ) - { - if( event.bstate & BUTTON3_CLICKED ) - list_window_last(lw, lw_length); - else - list_window_next_page(lw, lw_length); - return 1; - } - return 0; + MEVENT event; + + /* retreive the mouse event from ncurses */ + getmouse(&event); + D("mouse: id=%d y=%d,x=%d,z=%d\n",event.id,event.y,event.x,event.z); + /* calculate the selected row in the list window */ + *row = event.y - screen->top_window.rows; + /* copy button state bits */ + *bstate = event.bstate; + /* if button 2 was pressed switch screen */ + if (event.bstate & BUTTON2_CLICKED) { + screen_cmd(c, CMD_SCREEN_NEXT); + return 1; + } + + /* if the even occured above the list window move up */ + if (*row < 0 && lw) { + if (event.bstate & BUTTON3_CLICKED) + list_window_first(lw); + else + list_window_previous_page(lw); + return 1; + } + + /* if the even occured below the list window move down */ + if (*row >= lw->rows && lw) { + if (event.bstate & BUTTON3_CLICKED) + list_window_last(lw, lw_length); + else + list_window_next_page(lw, lw_length); + return 1; + } + + return 0; } #endif -void +void screen_cmd(mpdclient_t *c, command_t cmd) { - screen->input_timestamp = time(NULL); - screen->last_cmd = cmd; - welcome = FALSE; - - if( mode_fn && mode_fn->cmd && mode_fn->cmd(screen, c, cmd) ) - return; - - switch(cmd) - { - case CMD_PLAY: - mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING); - break; - case CMD_PAUSE: - mpdclient_cmd_pause(c, !IS_PAUSED(c->status->state)); - break; - case CMD_STOP: - mpdclient_cmd_stop(c); - break; - case CMD_SEEK_FORWARD: - if( !IS_STOPPED(c->status->state) ) - { - if( c->song && seek_id != c->song->id ) - { - seek_id = c->song->id; - seek_target_time = c->status->elapsedTime; - } - seek_target_time+=options.seek_time; - if( seek_target_time < c->status->totalTime ) - break; - seek_target_time = c->status->totalTime; - /* seek_target_time=0; */ - } - break; - /* fall through... */ - case CMD_TRACK_NEXT: - if( !IS_STOPPED(c->status->state) ) - mpdclient_cmd_next(c); - break; - case CMD_SEEK_BACKWARD: - if( !IS_STOPPED(c->status->state) ) - { - if( seek_id != c->song->id ) - { - seek_id = c->song->id; - seek_target_time = c->status->elapsedTime; - } - seek_target_time-=options.seek_time; - if( seek_target_time < 0 ) - seek_target_time=0; - } - break; - case CMD_TRACK_PREVIOUS: - if( !IS_STOPPED(c->status->state) ) - mpdclient_cmd_prev(c); - break; - case CMD_SHUFFLE: - if( mpdclient_cmd_shuffle(c) == 0 ) - screen_status_message(_("Shuffled playlist!")); - break; - case CMD_CLEAR: - if( mpdclient_cmd_clear(c) == 0 ) - screen_status_message(_("Cleared playlist!")); - break; - case CMD_REPEAT: - mpdclient_cmd_repeat(c, !c->status->repeat); - break; - case CMD_RANDOM: - mpdclient_cmd_random(c, !c->status->random); - break; - case CMD_CROSSFADE: - if( c->status->crossfade ) - mpdclient_cmd_crossfade(c, 0); - else - mpdclient_cmd_crossfade(c, options.crossfade_time); - break; - case CMD_DB_UPDATE: - if( !c->status->updatingDb ) - { - if( mpdclient_cmd_db_update_utf8(c,NULL)==0 ) - screen_status_printf(_("Database update started!")); - } - else - screen_status_printf(_("Database update running...")); - break; - case CMD_VOLUME_UP: - if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume<100 ) - mpdclient_cmd_volume(c, ++c->status->volume); - break; - case CMD_VOLUME_DOWN: - if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume>0 ) - mpdclient_cmd_volume(c, --c->status->volume); - break; - case CMD_TOGGLE_FIND_WRAP: - options.find_wrap = !options.find_wrap; - screen_status_printf(options.find_wrap ? - _("Find mode: Wrapped") : - _("Find mode: Normal")); - break; - case CMD_TOGGLE_AUTOCENTER: - options.auto_center = !options.auto_center; - screen_status_printf(options.auto_center ? - _("Auto center mode: On") : - _("Auto center mode: Off")); - break; - case CMD_SCREEN_UPDATE: - screen->painted = 0; - break; - case CMD_SCREEN_PREVIOUS: - screen_next_mode(c, -1); - break; - case CMD_SCREEN_NEXT: - screen_next_mode(c, 1); - break; - case CMD_SCREEN_PLAY: - switch_screen_mode(SCREEN_PLAYLIST_ID, c); - break; - case CMD_SCREEN_FILE: - switch_screen_mode(SCREEN_BROWSE_ID, c); - break; - case CMD_SCREEN_HELP: - switch_screen_mode(SCREEN_HELP_ID, c); - break; - case CMD_SCREEN_SEARCH: - switch_screen_mode(SCREEN_SEARCH_ID, c); - break; - case CMD_SCREEN_ARTIST: - switch_screen_mode(SCREEN_ARTIST_ID, c); - break; - case CMD_SCREEN_KEYDEF: - switch_screen_mode(SCREEN_KEYDEF_ID, c); - break; - case CMD_SCREEN_CLOCK: - switch_screen_mode(SCREEN_CLOCK_ID, c); - break; + screen->input_timestamp = time(NULL); + screen->last_cmd = cmd; + welcome = FALSE; + + if( mode_fn && mode_fn->cmd && mode_fn->cmd(screen, c, cmd) ) + return; + + switch(cmd) { + case CMD_PLAY: + mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING); + break; + case CMD_PAUSE: + mpdclient_cmd_pause(c, !IS_PAUSED(c->status->state)); + break; + case CMD_STOP: + mpdclient_cmd_stop(c); + break; + case CMD_SEEK_FORWARD: + if (!IS_STOPPED(c->status->state)) { + if (c->song && seek_id != c->song->id) { + seek_id = c->song->id; + seek_target_time = c->status->elapsedTime; + } + seek_target_time+=options.seek_time; + if (seek_target_time < c->status->totalTime) + break; + seek_target_time = c->status->totalTime; + /* seek_target_time=0; */ + } + break; + /* fall through... */ + case CMD_TRACK_NEXT: + if (!IS_STOPPED(c->status->state)) + mpdclient_cmd_next(c); + break; + case CMD_SEEK_BACKWARD: + if (!IS_STOPPED(c->status->state)) { + if (seek_id != c->song->id) { + seek_id = c->song->id; + seek_target_time = c->status->elapsedTime; + } + seek_target_time-=options.seek_time; + if (seek_target_time < 0) + seek_target_time=0; + } + break; + case CMD_TRACK_PREVIOUS: + if (!IS_STOPPED(c->status->state)) + mpdclient_cmd_prev(c); + break; + case CMD_SHUFFLE: + if (mpdclient_cmd_shuffle(c) == 0) + screen_status_message(_("Shuffled playlist!")); + break; + case CMD_CLEAR: + if (mpdclient_cmd_clear(c) == 0) + screen_status_message(_("Cleared playlist!")); + break; + case CMD_REPEAT: + mpdclient_cmd_repeat(c, !c->status->repeat); + break; + case CMD_RANDOM: + mpdclient_cmd_random(c, !c->status->random); + break; + case CMD_CROSSFADE: + if (c->status->crossfade) + mpdclient_cmd_crossfade(c, 0); + else + mpdclient_cmd_crossfade(c, options.crossfade_time); + break; + case CMD_DB_UPDATE: + if (!c->status->updatingDb) { + if( mpdclient_cmd_db_update_utf8(c,NULL)==0 ) + screen_status_printf(_("Database update started!")); + } else + screen_status_printf(_("Database update running...")); + break; + case CMD_VOLUME_UP: + if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume<100 ) + mpdclient_cmd_volume(c, ++c->status->volume); + break; + case CMD_VOLUME_DOWN: + if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume>0 ) + mpdclient_cmd_volume(c, --c->status->volume); + break; + case CMD_TOGGLE_FIND_WRAP: + options.find_wrap = !options.find_wrap; + screen_status_printf(options.find_wrap ? + _("Find mode: Wrapped") : + _("Find mode: Normal")); + break; + case CMD_TOGGLE_AUTOCENTER: + options.auto_center = !options.auto_center; + screen_status_printf(options.auto_center ? + _("Auto center mode: On") : + _("Auto center mode: Off")); + break; + case CMD_SCREEN_UPDATE: + screen->painted = 0; + break; + case CMD_SCREEN_PREVIOUS: + screen_next_mode(c, -1); + break; + case CMD_SCREEN_NEXT: + screen_next_mode(c, 1); + break; + case CMD_SCREEN_PLAY: + switch_screen_mode(SCREEN_PLAYLIST_ID, c); + break; + case CMD_SCREEN_FILE: + switch_screen_mode(SCREEN_BROWSE_ID, c); + break; + case CMD_SCREEN_HELP: + switch_screen_mode(SCREEN_HELP_ID, c); + break; + case CMD_SCREEN_SEARCH: + switch_screen_mode(SCREEN_SEARCH_ID, c); + break; + case CMD_SCREEN_ARTIST: + switch_screen_mode(SCREEN_ARTIST_ID, c); + break; + case CMD_SCREEN_KEYDEF: + switch_screen_mode(SCREEN_KEYDEF_ID, c); + break; + case CMD_SCREEN_CLOCK: + switch_screen_mode(SCREEN_CLOCK_ID, c); + break; case CMD_SCREEN_LYRICS: - switch_screen_mode(SCREEN_LYRICS_ID, c); - break; - case CMD_QUIT: - exit(EXIT_SUCCESS); - default: - break; - } + switch_screen_mode(SCREEN_LYRICS_ID, c); + break; + case CMD_QUIT: + exit(EXIT_SUCCESS); + default: + break; + } } diff --git a/src/screen.h b/src/screen.h index d15be3bb9..f2c54d3da 100644 --- a/src/screen.h +++ b/src/screen.h @@ -9,66 +9,60 @@ #define MAX_SONGNAME_LENGTH 512 -typedef struct -{ - WINDOW *w; - int rows, cols; - int cur_action_id; +typedef struct { + WINDOW *w; + int rows, cols; + int cur_action_id; } window_t; +typedef struct { + window_t top_window; + window_t main_window; + window_t progress_window; + window_t status_window; -typedef struct -{ - window_t top_window; - window_t main_window; - window_t progress_window; - window_t status_window; + /* GTime is equivalent to time_t */ + GTime start_timestamp; + GTime status_timestamp; + GTime input_timestamp; + GTime last_cmd; - /* GTime is equivalent to time_t */ - GTime start_timestamp; - GTime status_timestamp; - GTime input_timestamp; - GTime last_cmd; + int cols, rows; - int cols, rows; + int mode; - int mode; + char *buf; + size_t buf_size; - char *buf; - size_t buf_size; - - char *findbuf; - GList *find_history; - - int painted; + char *findbuf; + GList *find_history; + int painted; } screen_t; -typedef void (*screen_init_fn_t) (WINDOW *w, int cols, int rows); -typedef void (*screen_exit_fn_t) (void); -typedef void (*screen_open_fn_t) (screen_t *screen, mpdclient_t *c); -typedef void (*screen_close_fn_t) (void); -typedef void (*screen_resize_fn_t) (int cols, int rows); -typedef void (*screen_paint_fn_t) (screen_t *screen, mpdclient_t *c); -typedef void (*screen_update_fn_t) (screen_t *screen, mpdclient_t *c); -typedef int (*screen_cmd_fn_t) (screen_t *scr, mpdclient_t *c, command_t cmd); -typedef char * (*screen_title_fn_t) (char *s, size_t size); -typedef list_window_t * (*screen_get_lw_fn_t) (void); - -typedef struct -{ - screen_init_fn_t init; - screen_exit_fn_t exit; - screen_open_fn_t open; - screen_close_fn_t close; - screen_resize_fn_t resize; - screen_paint_fn_t paint; - screen_update_fn_t update; - screen_cmd_fn_t cmd; - screen_title_fn_t get_title; - screen_get_lw_fn_t get_lw; - +typedef void (*screen_init_fn_t)(WINDOW *w, int cols, int rows); +typedef void (*screen_exit_fn_t)(void); +typedef void (*screen_open_fn_t)(screen_t *screen, mpdclient_t *c); +typedef void (*screen_close_fn_t)(void); +typedef void (*screen_resize_fn_t)(int cols, int rows); +typedef void (*screen_paint_fn_t)(screen_t *screen, mpdclient_t *c); +typedef void (*screen_update_fn_t)(screen_t *screen, mpdclient_t *c); +typedef int (*screen_cmd_fn_t)(screen_t *scr, mpdclient_t *c, command_t cmd); +typedef char *(*screen_title_fn_t)(char *s, size_t size); +typedef list_window_t *(*screen_get_lw_fn_t) (void); + +typedef struct { + screen_init_fn_t init; + screen_exit_fn_t exit; + screen_open_fn_t open; + screen_close_fn_t close; + screen_resize_fn_t resize; + screen_paint_fn_t paint; + screen_update_fn_t update; + screen_cmd_fn_t cmd; + screen_title_fn_t get_title; + screen_get_lw_fn_t get_lw; } screen_functions_t; diff --git a/src/screen_clock.c b/src/screen_clock.c index c996c1924..dee3e1e3e 100644 --- a/src/screen_clock.c +++ b/src/screen_clock.c @@ -31,10 +31,10 @@ static gboolean enable_seconds = ENABLE_SECONDS; /* orginal variables from gdc.c */ static short disp[11] = -{ - 075557, 011111, 071747, 071717, 055711, - 074717, 074757, 071111, 075757, 075717, 002020 -}; + { + 075557, 011111, 071747, 071717, 055711, + 074717, 074757, 071111, 075757, 075717, 002020 + }; static long older[6], next[6], newer[6], mask; @@ -46,104 +46,102 @@ static int XLENGTH = 54; static void set(int t, int n) { - int i, m; - - m = 7 << n; - for (i = 0; i < 5; i++) { - next[i] |= ((disp[t] >> ((4 - i) * 3)) & 07) << n; - mask |= (next[i] ^ older[i]) & m; - } - if (mask & m) - mask |= m; + int i, m; + + m = 7 << n; + for (i = 0; i < 5; i++) { + next[i] |= ((disp[t] >> ((4 - i) * 3)) & 07) << n; + mask |= (next[i] ^ older[i]) & m; + } + if (mask & m) + mask |= m; } static void drawbox(void) { - chtype bottom[XLENGTH + 1]; - int n; - - mvwaddch(win.w, YBASE - 1, XBASE - 1, ACS_ULCORNER); - whline(win.w, ACS_HLINE, XLENGTH); - mvwaddch(win.w, YBASE - 1, XBASE + XLENGTH, ACS_URCORNER); - - mvwaddch(win.w, YBASE + YDEPTH, XBASE - 1, ACS_LLCORNER); - mvwinchnstr(win.w, YBASE + YDEPTH, XBASE, bottom, XLENGTH); - for (n = 0; n < XLENGTH; n++) - bottom[n] = ACS_HLINE | (bottom[n] & (A_ATTRIBUTES | A_COLOR)); - mvwaddchnstr(win.w, YBASE + YDEPTH, XBASE, bottom, XLENGTH); - mvwaddch(win.w, YBASE + YDEPTH, XBASE + XLENGTH, ACS_LRCORNER); - - wmove(win.w, YBASE, XBASE - 1); - wvline(win.w, ACS_VLINE, YDEPTH); - - wmove(win.w, YBASE, XBASE + XLENGTH); - wvline(win.w, ACS_VLINE, YDEPTH); + chtype bottom[XLENGTH + 1]; + int n; + + mvwaddch(win.w, YBASE - 1, XBASE - 1, ACS_ULCORNER); + whline(win.w, ACS_HLINE, XLENGTH); + mvwaddch(win.w, YBASE - 1, XBASE + XLENGTH, ACS_URCORNER); + + mvwaddch(win.w, YBASE + YDEPTH, XBASE - 1, ACS_LLCORNER); + mvwinchnstr(win.w, YBASE + YDEPTH, XBASE, bottom, XLENGTH); + for (n = 0; n < XLENGTH; n++) + bottom[n] = ACS_HLINE | (bottom[n] & (A_ATTRIBUTES | A_COLOR)); + mvwaddchnstr(win.w, YBASE + YDEPTH, XBASE, bottom, XLENGTH); + mvwaddch(win.w, YBASE + YDEPTH, XBASE + XLENGTH, ACS_LRCORNER); + + wmove(win.w, YBASE, XBASE - 1); + wvline(win.w, ACS_VLINE, YDEPTH); + + wmove(win.w, YBASE, XBASE + XLENGTH); + wvline(win.w, ACS_VLINE, YDEPTH); } static void standt(int on) { - if(on) - wattron(win.w, A_REVERSE); - else - wattroff(win.w, A_REVERSE); + if (on) + wattron(win.w, A_REVERSE); + else + wattroff(win.w, A_REVERSE); } - - /* ncmpc screen functions */ static void clock_resize(int cols, int rows) { - int j; - - for (j = 0; j < 5; j++) - older[j] = newer[j] = next[j] = 0; - - win.cols = cols; - win.rows = rows; - - if( cols<60 ) - enable_seconds = FALSE; - else - enable_seconds = ENABLE_SECONDS; - - if( enable_seconds ) - XLENGTH = 54; - else - XLENGTH = 54-18; - - - XBASE = (cols-XLENGTH)/2; - YBASE = (rows-YDEPTH)/2-(YDEPTH/2)+2; + int j; + + for (j = 0; j < 5; j++) + older[j] = newer[j] = next[j] = 0; + + win.cols = cols; + win.rows = rows; + + if (cols < 60) + enable_seconds = FALSE; + else + enable_seconds = ENABLE_SECONDS; + + if (enable_seconds) + XLENGTH = 54; + else + XLENGTH = 54-18; + + + XBASE = (cols-XLENGTH)/2; + YBASE = (rows-YDEPTH)/2-(YDEPTH/2)+2; } -static void +static void clock_init(WINDOW *w, int cols, int rows) { - win.w = w; - clock_resize(cols, rows); + win.w = w; + clock_resize(cols, rows); } -static void +static void clock_exit(void) { } -static void +static void clock_open(screen_t *screen, mpdclient_t *c) { - int j; + int j; - for (j = 0; j < 5; j++) - older[j] = newer[j] = next[j] = 0; + for (j = 0; j < 5; j++) + older[j] = newer[j] = next[j] = 0; } -static void +static void clock_close(void) { } @@ -151,120 +149,113 @@ clock_close(void) static char * clock_title(char *str, size_t size) { - return _("Clock"); + return _("Clock"); } -static void +static void clock_update(screen_t *screen, mpdclient_t *c) -{ - time_t now; - struct tm *tm; - long t, a; - int i, j, s, k; - char buf[BUFSIZE]; - - time(&now); - tm = localtime(&now); - - if( win.rows<=YDEPTH+1 || win.cols<=XLENGTH+1 ) - { - strftime(buf, BUFSIZE, "%X ",tm); - mvwaddstr(win.w, win.rows ? win.rows/2:0, (win.cols-strlen(buf))/2, buf); - wrefresh(win.w); - return; - } - - - - mask = 0; - set(tm->tm_sec % 10, 0); - set(tm->tm_sec / 10, 4); - set(tm->tm_min % 10, 10); - set(tm->tm_min / 10, 14); - set(tm->tm_hour % 10, 20); - set(tm->tm_hour / 10, 24); - set(10, 7); - set(10, 17); - - for (k = 0; k < 6; k++) { - newer[k] = (newer[k] & ~mask) | (next[k] & mask); - next[k] = 0; - for (s = 1; s >= 0; s--) { - standt(s); - for (i = 0; i < 6; i++) { - if ((a = (newer[i] ^ older[i]) & (s ? newer : older)[i]) - != 0) { - for (j = 0, t = 1 << 26; t; t >>= 1, j++) { - if (a & t) { - if (!(a & (t << 1))) { - wmove(win.w, YBASE + i, XBASE + 2 * j); - } - if( enable_seconds || j<18 ) - waddstr(win.w, " "); - } - } +{ + time_t now; + struct tm *tm; + long t, a; + int i, j, s, k; + char buf[BUFSIZE]; + + time(&now); + tm = localtime(&now); + + if (win.rows<=YDEPTH+1 || win.cols<=XLENGTH+1) { + strftime(buf, BUFSIZE, "%X ",tm); + mvwaddstr(win.w, win.rows ? win.rows/2:0, (win.cols-strlen(buf))/2, buf); + wrefresh(win.w); + return; } - if (!s) { - older[i] = newer[i]; + + mask = 0; + set(tm->tm_sec % 10, 0); + set(tm->tm_sec / 10, 4); + set(tm->tm_min % 10, 10); + set(tm->tm_min / 10, 14); + set(tm->tm_hour % 10, 20); + set(tm->tm_hour / 10, 24); + set(10, 7); + set(10, 17); + + for (k = 0; k < 6; k++) { + newer[k] = (newer[k] & ~mask) | (next[k] & mask); + next[k] = 0; + for (s = 1; s >= 0; s--) { + standt(s); + for (i = 0; i < 6; i++) { + if ((a = (newer[i] ^ older[i]) & (s ? newer : older)[i]) + != 0) { + for (j = 0, t = 1 << 26; t; t >>= 1, j++) { + if (a & t) { + if (!(a & (t << 1))) { + wmove(win.w, YBASE + i, XBASE + 2 * j); + } + if( enable_seconds || j<18 ) + waddstr(win.w, " "); + } + } + } + if (!s) { + older[i] = newer[i]; + } + } + if (!s) { + wrefresh(win.w); + } + } } - } - if (!s) { - wrefresh(win.w); - } - } - } - + #ifdef HAVE_LOCALE_H - strftime(buf, BUFSIZE, "%x", tm); + strftime(buf, BUFSIZE, "%x", tm); # else - /* this depends on the detailed format of ctime(3) */ - strcpy(buf, ctime(&now)); - strcpy(buf + 10, buf + 19); + /* this depends on the detailed format of ctime(3) */ + strcpy(buf, ctime(&now)); + strcpy(buf + 10, buf + 19); #endif - mvwaddstr(win.w, YBASE+YDEPTH+1, (win.cols-strlen(buf))/2, buf); - - wmove(win.w, 6, 0); - drawbox(); - wrefresh(win.w); + mvwaddstr(win.w, YBASE+YDEPTH+1, (win.cols-strlen(buf))/2, buf); + + wmove(win.w, 6, 0); + drawbox(); + wrefresh(win.w); } -static void +static void clock_paint(screen_t *screen, mpdclient_t *c) { - /* this seems to be a better way to clear the window than wclear() ?! */ - wmove(win.w, 0, 0); - wclrtobot(win.w); - clock_update(screen, c); + /* this seems to be a better way to clear the window than wclear() ?! */ + wmove(win.w, 0, 0); + wclrtobot(win.w); + clock_update(screen, c); } - - -static int +static int clock_cmd(screen_t *screen, mpdclient_t *c, command_t cmd) { - return 0; + 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; + 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; } - #endif diff --git a/src/screen_file.c b/src/screen_file.c index 31b34608a..c8468cc81 100644 --- a/src/screen_file.c +++ b/src/screen_file.c @@ -49,345 +49,316 @@ static list_window_state_t *lw_state = NULL; static mpdclient_filelist_t *filelist = NULL; - /* clear the highlight flag for all items in the filelist */ void clear_highlights(mpdclient_filelist_t *filelist) { - GList *list = g_list_first(filelist->list); - - while( list ) - { - filelist_entry_t *entry = list->data; - - entry->flags &= ~HIGHLIGHT; - list = list->next; - } + GList *list = g_list_first(filelist->list); + + while( list ) { + filelist_entry_t *entry = list->data; + + entry->flags &= ~HIGHLIGHT; + list = list->next; + } } /* change the highlight flag for a song */ void set_highlight(mpdclient_filelist_t *filelist, mpd_Song *song, int highlight) { - GList *list = g_list_first(filelist->list); - - if( !song ) - return; - - while( list ) - { - filelist_entry_t *entry = list->data; - mpd_InfoEntity *entity = entry->entity; - - if( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG ) - { - mpd_Song *song2 = entity->info.song; - - if( strcmp(song->file, song2->file) == 0 ) - { - if(highlight) - entry->flags |= HIGHLIGHT; - else - entry->flags &= ~HIGHLIGHT; - } + GList *list = g_list_first(filelist->list); + + if( !song ) + return; + + while( list ) { + filelist_entry_t *entry = list->data; + mpd_InfoEntity *entity = entry->entity; + + if( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG ) { + mpd_Song *song2 = entity->info.song; + + if( strcmp(song->file, song2->file) == 0 ) { + if(highlight) + entry->flags |= HIGHLIGHT; + else + entry->flags &= ~HIGHLIGHT; + } + } + list = list->next; } - list = list->next; - } } /* sync highlight flags with playlist */ void sync_highlights(mpdclient_t *c, mpdclient_filelist_t *filelist) { - GList *list = g_list_first(filelist->list); - - while(list) - { - filelist_entry_t *entry = list->data; - mpd_InfoEntity *entity = entry->entity; - - if( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG ) - { - mpd_Song *song = entity->info.song; - - if( playlist_get_index_from_file(c, song->file) >= 0 ) - entry->flags |= HIGHLIGHT; - else - entry->flags &= ~HIGHLIGHT; + GList *list = g_list_first(filelist->list); + + while(list) { + filelist_entry_t *entry = list->data; + mpd_InfoEntity *entity = entry->entity; + + if( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG ) { + mpd_Song *song = entity->info.song; + + if( playlist_get_index_from_file(c, song->file) >= 0 ) + entry->flags |= HIGHLIGHT; + else + entry->flags &= ~HIGHLIGHT; + } + list=list->next; } - list=list->next; - } } /* the db have changed -> update the filelist */ -static void +static void file_changed_callback(mpdclient_t *c, int event, gpointer data) { - D("screen_file.c> filelist_callback() [%d]\n", event); - filelist = mpdclient_filelist_update(c, filelist); - sync_highlights(c, filelist); - list_window_check_selected(lw, filelist->length); + D("screen_file.c> filelist_callback() [%d]\n", event); + filelist = mpdclient_filelist_update(c, filelist); + sync_highlights(c, filelist); + list_window_check_selected(lw, filelist->length); } /* the playlist have been updated -> fix highlights */ -static void +static void playlist_changed_callback(mpdclient_t *c, int event, gpointer data) { - D("screen_file.c> playlist_callback() [%d]\n", event); - switch(event) - { - case PLAYLIST_EVENT_CLEAR: - clear_highlights(filelist); - break; - case PLAYLIST_EVENT_ADD: - set_highlight(filelist, (mpd_Song *) data, 1); - break; - case PLAYLIST_EVENT_DELETE: - set_highlight(filelist, (mpd_Song *) data, 0); - break; - case PLAYLIST_EVENT_MOVE: - break; - default: - sync_highlights(c, filelist); - break; - } + D("screen_file.c> playlist_callback() [%d]\n", event); + switch(event) { + case PLAYLIST_EVENT_CLEAR: + clear_highlights(filelist); + break; + case PLAYLIST_EVENT_ADD: + set_highlight(filelist, (mpd_Song *) data, 1); + break; + case PLAYLIST_EVENT_DELETE: + set_highlight(filelist, (mpd_Song *) data, 0); + break; + case PLAYLIST_EVENT_MOVE: + break; + default: + sync_highlights(c, filelist); + break; + } } /* list_window callback */ char * browse_lw_callback(int index, int *highlight, void *data) { - static char buf[BUFSIZE]; - mpdclient_filelist_t *filelist = (mpdclient_filelist_t *) data; - filelist_entry_t *entry; - mpd_InfoEntity *entity; - - *highlight = 0; - if( (entry=(filelist_entry_t *)g_list_nth_data(filelist->list,index))==NULL ) - return NULL; - - entity = entry->entity; - *highlight = (entry->flags & HIGHLIGHT); - - if( entity == NULL ) - { - return "[..]"; - } - if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY ) - { - mpd_Directory *dir = entity->info.directory; - char *dirname = utf8_to_locale(basename(dir->path)); - - g_snprintf(buf, BUFSIZE, "[%s]", dirname); - g_free(dirname); - return buf; - } - else if( entity->type==MPD_INFO_ENTITY_TYPE_SONG ) - { - mpd_Song *song = entity->info.song; - - strfsong(buf, BUFSIZE, LIST_FORMAT, song); - return buf; - } - else if( entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) - { - mpd_PlaylistFile *plf = entity->info.playlistFile; - char *filename = utf8_to_locale(basename(plf->path)); - -#ifdef USE_OLD_LAYOUT - g_snprintf(buf, BUFSIZE, "*%s*", filename); -#else - g_snprintf(buf, BUFSIZE, "<Playlist> %s", filename); + static char buf[BUFSIZE]; + mpdclient_filelist_t *filelist = (mpdclient_filelist_t *) data; + filelist_entry_t *entry; + mpd_InfoEntity *entity; + + *highlight = 0; + if( (entry=(filelist_entry_t *)g_list_nth_data(filelist->list,index))==NULL ) + return NULL; + + entity = entry->entity; + *highlight = (entry->flags & HIGHLIGHT); + + if( entity == NULL ) + return "[..]"; + + if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY ) { + mpd_Directory *dir = entity->info.directory; + char *dirname = utf8_to_locale(basename(dir->path)); + + g_snprintf(buf, BUFSIZE, "[%s]", dirname); + g_free(dirname); + return buf; + } else if( entity->type==MPD_INFO_ENTITY_TYPE_SONG ) { + mpd_Song *song = entity->info.song; + + strfsong(buf, BUFSIZE, LIST_FORMAT, song); + return buf; + } else if( entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) { + mpd_PlaylistFile *plf = entity->info.playlistFile; + char *filename = utf8_to_locale(basename(plf->path)); + +#ifdef USE_OLD_LAYOUT + g_snprintf(buf, BUFSIZE, "*%s*", filename); +#else + g_snprintf(buf, BUFSIZE, "<Playlist> %s", filename); #endif - g_free(filename); - return buf; - } - return "Error: Unknown entry!"; + g_free(filename); + return buf; + } + + return "Error: Unknown entry!"; } /* chdir */ static int -change_directory(screen_t *screen, mpdclient_t *c, filelist_entry_t *entry, char *new_path) +change_directory(screen_t *screen, mpdclient_t *c, filelist_entry_t *entry, + char *new_path) { - mpd_InfoEntity *entity = NULL; - gchar *path = NULL; - - if( entry!=NULL ) - entity = entry->entity; - else if( new_path==NULL ) - return -1; - - if( entity==NULL ) - { - if( entry || 0==strcmp(new_path, "..") ) - { - /* return to parent */ - char *parent = g_path_get_dirname(filelist->path); - if( strcmp(parent, ".") == 0 ) - { - parent[0] = '\0'; - } - path = g_strdup(parent); - list_window_reset(lw); - /* restore previous list window state */ - list_window_pop_state(lw_state,lw); - } - else - { - /* entry==NULL, then new_path ("" is root) */ - path = g_strdup(new_path); - list_window_reset(lw); - /* restore first list window state (pop while returning true) */ - while(list_window_pop_state(lw_state,lw)); - } - } - else - if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY) - { - /* enter sub */ - mpd_Directory *dir = entity->info.directory; - path = utf8_to_locale(dir->path); - /* save current list window state */ - list_window_push_state(lw_state,lw); - } - else - return -1; - - filelist = mpdclient_filelist_free(filelist); - filelist = mpdclient_filelist_get(c, path); - sync_highlights(c, filelist); - list_window_check_selected(lw, filelist->length); - g_free(path); - return 0; + mpd_InfoEntity *entity = NULL; + gchar *path = NULL; + + if( entry!=NULL ) + entity = entry->entity; + else if( new_path==NULL ) + return -1; + + if( entity==NULL ) { + if( entry || 0==strcmp(new_path, "..") ) { + /* return to parent */ + char *parent = g_path_get_dirname(filelist->path); + if( strcmp(parent, ".") == 0 ) + parent[0] = '\0'; + path = g_strdup(parent); + list_window_reset(lw); + /* restore previous list window state */ + list_window_pop_state(lw_state,lw); + } else { + /* entry==NULL, then new_path ("" is root) */ + path = g_strdup(new_path); + list_window_reset(lw); + /* restore first list window state (pop while returning true) */ + while(list_window_pop_state(lw_state,lw)); + } + } else if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY) { + /* enter sub */ + mpd_Directory *dir = entity->info.directory; + path = utf8_to_locale(dir->path); + /* save current list window state */ + list_window_push_state(lw_state,lw); + } else + return -1; + + filelist = mpdclient_filelist_free(filelist); + filelist = mpdclient_filelist_get(c, path); + sync_highlights(c, filelist); + list_window_check_selected(lw, filelist->length); + g_free(path); + return 0; } static int load_playlist(screen_t *screen, mpdclient_t *c, filelist_entry_t *entry) { - mpd_InfoEntity *entity = entry->entity; - mpd_PlaylistFile *plf = entity->info.playlistFile; - char *filename = utf8_to_locale(plf->path); - - if( mpdclient_cmd_load_playlist_utf8(c, plf->path) == 0 ) - screen_status_printf(_("Loading playlist %s..."), basename(filename)); - g_free(filename); - return 0; + mpd_InfoEntity *entity = entry->entity; + mpd_PlaylistFile *plf = entity->info.playlistFile; + char *filename = utf8_to_locale(plf->path); + + if( mpdclient_cmd_load_playlist_utf8(c, plf->path) == 0 ) + screen_status_printf(_("Loading playlist %s..."), basename(filename)); + g_free(filename); + return 0; } static int handle_save(screen_t *screen, mpdclient_t *c) { - filelist_entry_t *entry; - char *defaultname = NULL; - - - entry=( filelist_entry_t *) g_list_nth_data(filelist->list,lw->selected); - if( entry && entry->entity ) - { - mpd_InfoEntity *entity = entry->entity; - if( entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) - { - mpd_PlaylistFile *plf = entity->info.playlistFile; - defaultname = plf->path; + filelist_entry_t *entry; + char *defaultname = NULL; + + + entry=( filelist_entry_t *) g_list_nth_data(filelist->list,lw->selected); + if( entry && entry->entity ) { + mpd_InfoEntity *entity = entry->entity; + if( entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) { + mpd_PlaylistFile *plf = entity->info.playlistFile; + defaultname = plf->path; + } } - } - return playlist_save(screen, c, NULL, defaultname); + + return playlist_save(screen, c, NULL, defaultname); } -static int +static int handle_delete(screen_t *screen, mpdclient_t *c) { - filelist_entry_t *entry; - mpd_InfoEntity *entity; - mpd_PlaylistFile *plf; - char *str, *buf; - int key; - - entry=( filelist_entry_t *) g_list_nth_data(filelist->list,lw->selected); - if( entry==NULL || entry->entity==NULL ) - return -1; - - entity = entry->entity; - - if( entity->type!=MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) - { - screen_status_printf(_("You can only delete playlists!")); - screen_bell(); - return -1; - } - - plf = entity->info.playlistFile; - str = utf8_to_locale(basename(plf->path)); - buf = g_strdup_printf(_("Delete playlist %s [%s/%s] ? "), str, YES, NO); - g_free(str); - key = tolower(screen_getch(screen->status_window.w, buf)); - g_free(buf); - if( key==KEY_RESIZE ) - screen_resize(); - if( key != YES[0] ) - { - screen_status_printf(_("Aborted!")); - return 0; - } - - if( mpdclient_cmd_delete_playlist_utf8(c, plf->path) ) - { - return -1; - } - screen_status_printf(_("Playlist deleted!")); - return 0; + filelist_entry_t *entry; + mpd_InfoEntity *entity; + mpd_PlaylistFile *plf; + char *str, *buf; + int key; + + entry=( filelist_entry_t *) g_list_nth_data(filelist->list,lw->selected); + if( entry==NULL || entry->entity==NULL ) + return -1; + + entity = entry->entity; + + if( entity->type!=MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) { + screen_status_printf(_("You can only delete playlists!")); + screen_bell(); + return -1; + } + + plf = entity->info.playlistFile; + str = utf8_to_locale(basename(plf->path)); + buf = g_strdup_printf(_("Delete playlist %s [%s/%s] ? "), str, YES, NO); + g_free(str); + key = tolower(screen_getch(screen->status_window.w, buf)); + g_free(buf); + if( key==KEY_RESIZE ) + screen_resize(); + if( key != YES[0] ) { + screen_status_printf(_("Aborted!")); + return 0; + } + + if( mpdclient_cmd_delete_playlist_utf8(c, plf->path) ) + return -1; + + screen_status_printf(_("Playlist deleted!")); + return 0; } static int enqueue_and_play(screen_t *screen, mpdclient_t *c, filelist_entry_t *entry) { - int index; - mpd_InfoEntity *entity = entry->entity; - mpd_Song *song = entity->info.song; - - if(!( entry->flags & HIGHLIGHT )) - { - if( mpdclient_cmd_add(c, song) == 0 ) - { - char buf[BUFSIZE]; - - entry->flags |= HIGHLIGHT; - strfsong(buf, BUFSIZE, LIST_FORMAT, song); - screen_status_printf(_("Adding \'%s\' to playlist\n"), buf); - mpdclient_update(c); /* get song id */ - } - else - return -1; - } - - index = playlist_get_index_from_file(c, song->file); - mpdclient_cmd_play(c, index); - return 0; + int index; + mpd_InfoEntity *entity = entry->entity; + mpd_Song *song = entity->info.song; + + if(!( entry->flags & HIGHLIGHT )) { + if( mpdclient_cmd_add(c, song) == 0 ) { + char buf[BUFSIZE]; + + entry->flags |= HIGHLIGHT; + strfsong(buf, BUFSIZE, LIST_FORMAT, song); + screen_status_printf(_("Adding \'%s\' to playlist\n"), buf); + mpdclient_update(c); /* get song id */ + } else + return -1; + } + + index = playlist_get_index_from_file(c, song->file); + mpdclient_cmd_play(c, index); + return 0; } int -browse_handle_enter(screen_t *screen, +browse_handle_enter(screen_t *screen, mpdclient_t *c, list_window_t *lw, mpdclient_filelist_t *filelist) { - filelist_entry_t *entry; - mpd_InfoEntity *entity; - - if ( filelist==NULL ) - return -1; - entry = ( filelist_entry_t *) g_list_nth_data(filelist->list, lw->selected); - if( entry==NULL ) - return -1; - - entity = entry->entity; - if( entity==NULL || entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY ) - return change_directory(screen, c, entry, NULL); - else if( entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) - return load_playlist(screen, c, entry); - else if( entity->type==MPD_INFO_ENTITY_TYPE_SONG ) - return enqueue_and_play(screen, c, entry); - return -1; + filelist_entry_t *entry; + mpd_InfoEntity *entity; + + if ( filelist==NULL ) + return -1; + entry = ( filelist_entry_t *) g_list_nth_data(filelist->list, lw->selected); + if( entry==NULL ) + return -1; + + entity = entry->entity; + if( entity==NULL || entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY ) + return change_directory(screen, c, entry, NULL); + else if( entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) + return load_playlist(screen, c, entry); + else if( entity->type==MPD_INFO_ENTITY_TYPE_SONG ) + return enqueue_and_play(screen, c, entry); + return -1; } @@ -398,243 +369,221 @@ extern gint mpdclient_finish_command(mpdclient_t *c); static int add_directory(mpdclient_t *c, char *dir) { - mpd_InfoEntity *entity; - GList *subdir_list = NULL; - GList *list = NULL; - char *dirname; - - dirname = utf8_to_locale(dir); - screen_status_printf(_("Adding directory %s...\n"), dirname); - doupdate(); - g_free(dirname); - dirname = NULL; - - mpd_sendLsInfoCommand(c->connection, dir); - mpd_sendCommandListBegin(c->connection); - while( (entity=mpd_getNextInfoEntity(c->connection)) ) - { - if( entity->type==MPD_INFO_ENTITY_TYPE_SONG ) - { - mpd_Song *song = entity->info.song; - mpd_sendAddCommand(c->connection, song->file); - mpd_freeInfoEntity(entity); + mpd_InfoEntity *entity; + GList *subdir_list = NULL; + GList *list = NULL; + char *dirname; + + dirname = utf8_to_locale(dir); + screen_status_printf(_("Adding directory %s...\n"), dirname); + doupdate(); + g_free(dirname); + dirname = NULL; + + mpd_sendLsInfoCommand(c->connection, dir); + mpd_sendCommandListBegin(c->connection); + while( (entity=mpd_getNextInfoEntity(c->connection)) ) { + if( entity->type==MPD_INFO_ENTITY_TYPE_SONG ) { + mpd_Song *song = entity->info.song; + mpd_sendAddCommand(c->connection, song->file); + mpd_freeInfoEntity(entity); + } else if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY ) { + subdir_list = g_list_append(subdir_list, (gpointer) entity); + } else + mpd_freeInfoEntity(entity); } - else if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY ) - { - subdir_list = g_list_append(subdir_list, (gpointer) entity); + mpd_sendCommandListEnd(c->connection); + mpdclient_finish_command(c); + c->need_update = TRUE; + + list = g_list_first(subdir_list); + while( list!=NULL ) { + mpd_Directory *dir; + + entity = list->data; + dir = entity->info.directory; + add_directory(c, dir->path); + mpd_freeInfoEntity(entity); + list->data=NULL; + list=list->next; } - else - mpd_freeInfoEntity(entity); - } - mpd_sendCommandListEnd(c->connection); - mpdclient_finish_command(c); - c->need_update = TRUE; - - list = g_list_first(subdir_list); - while( list!=NULL ) - { - mpd_Directory *dir; - - entity = list->data; - dir = entity->info.directory; - add_directory(c, dir->path); - mpd_freeInfoEntity(entity); - list->data=NULL; - list=list->next; - } - g_list_free(subdir_list); - return 0; + g_list_free(subdir_list); + return 0; } #endif int -browse_handle_select(screen_t *screen, +browse_handle_select(screen_t *screen, mpdclient_t *c, list_window_t *lw, mpdclient_filelist_t *filelist) { - filelist_entry_t *entry; + filelist_entry_t *entry; - if ( filelist==NULL ) - return -1; - entry=( filelist_entry_t *) g_list_nth_data(filelist->list, lw->selected); - if( entry==NULL || entry->entity==NULL) - return -1; + if ( filelist==NULL ) + return -1; + entry=( filelist_entry_t *) g_list_nth_data(filelist->list, lw->selected); + if( entry==NULL || entry->entity==NULL) + return -1; - if( entry->entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) - return load_playlist(screen, c, entry); + if( entry->entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) + return load_playlist(screen, c, entry); - if( entry->entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY ) - { - mpd_Directory *dir = entry->entity->info.directory; + if( entry->entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY ) { + mpd_Directory *dir = entry->entity->info.directory; #ifdef USE_OLD_ADD - add_directory(c, tmp); + add_directory(c, tmp); #else - if( mpdclient_cmd_add_path_utf8(c, dir->path) == 0 ) - { - char *tmp = utf8_to_locale(dir->path); + if( mpdclient_cmd_add_path_utf8(c, dir->path) == 0 ) { + char *tmp = utf8_to_locale(dir->path); - screen_status_printf(_("Adding \'%s\' to playlist\n"), tmp); - g_free(tmp); - } + screen_status_printf(_("Adding \'%s\' to playlist\n"), tmp); + g_free(tmp); + } #endif - return 0; - } - - if( entry->entity->type!=MPD_INFO_ENTITY_TYPE_SONG ) - return -1; - - if( entry->flags & HIGHLIGHT ) - entry->flags &= ~HIGHLIGHT; - else - entry->flags |= HIGHLIGHT; - - if( entry->flags & HIGHLIGHT ) - { - if( entry->entity->type==MPD_INFO_ENTITY_TYPE_SONG ) - { - mpd_Song *song = entry->entity->info.song; - - if( mpdclient_cmd_add(c, song) == 0 ) - { - char buf[BUFSIZE]; - - strfsong(buf, BUFSIZE, LIST_FORMAT, song); - screen_status_printf(_("Adding \'%s\' to playlist\n"), buf); - } + return 0; } - } - else - { - /* remove song from playlist */ - if( entry->entity->type==MPD_INFO_ENTITY_TYPE_SONG ) - { - mpd_Song *song = entry->entity->info.song; - - if( song ) - { - int index = playlist_get_index_from_file(c, song->file); - - while( (index=playlist_get_index_from_file(c, song->file))>=0 ) - mpdclient_cmd_delete(c, index); - } + + if( entry->entity->type!=MPD_INFO_ENTITY_TYPE_SONG ) + return -1; + + if( entry->flags & HIGHLIGHT ) + entry->flags &= ~HIGHLIGHT; + else + entry->flags |= HIGHLIGHT; + + if( entry->flags & HIGHLIGHT ) { + if( entry->entity->type==MPD_INFO_ENTITY_TYPE_SONG ) { + mpd_Song *song = entry->entity->info.song; + + if( mpdclient_cmd_add(c, song) == 0 ) { + char buf[BUFSIZE]; + + strfsong(buf, BUFSIZE, LIST_FORMAT, song); + screen_status_printf(_("Adding \'%s\' to playlist\n"), buf); + } + } + } else { + /* remove song from playlist */ + if( entry->entity->type==MPD_INFO_ENTITY_TYPE_SONG ) { + mpd_Song *song = entry->entity->info.song; + + if( song ) { + int index = playlist_get_index_from_file(c, song->file); + + while( (index=playlist_get_index_from_file(c, song->file))>=0 ) + mpdclient_cmd_delete(c, index); + } + } } - } - return 0; + return 0; } int -browse_handle_select_all (screen_t *screen, - mpdclient_t *c, - list_window_t *lw, - mpdclient_filelist_t *filelist) +browse_handle_select_all (screen_t *screen, + mpdclient_t *c, + list_window_t *lw, + mpdclient_filelist_t *filelist) { - filelist_entry_t *entry; - GList *temp = filelist->list; - - if ( filelist==NULL ) - return -1; - for (filelist->list = g_list_first(filelist->list); - filelist->list; - filelist->list = g_list_next(filelist->list)) - { - entry=( filelist_entry_t *) filelist->list->data; - if( entry==NULL || entry->entity==NULL) - return -1; - - if( entry->entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) - load_playlist(screen, c, entry); - - if( entry->entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY ) - { - mpd_Directory *dir = entry->entity->info.directory; + filelist_entry_t *entry; + GList *temp = filelist->list; + + if ( filelist==NULL ) + return -1; + for (filelist->list = g_list_first(filelist->list); + filelist->list; + filelist->list = g_list_next(filelist->list)) { + entry=( filelist_entry_t *) filelist->list->data; + if( entry==NULL || entry->entity==NULL) + return -1; + + if( entry->entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) + load_playlist(screen, c, entry); + + if( entry->entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY ) { + mpd_Directory *dir = entry->entity->info.directory; #ifdef USE_OLD_ADD - add_directory(c, tmp); + add_directory(c, tmp); #else - if( mpdclient_cmd_add_path_utf8(c, dir->path) == 0 ) - { - char *tmp = utf8_to_locale(dir->path); + if (mpdclient_cmd_add_path_utf8(c, dir->path) == 0) { + char *tmp = utf8_to_locale(dir->path); - screen_status_printf(_("Adding \'%s\' to playlist\n"), tmp); - g_free(tmp); - } + screen_status_printf(_("Adding \'%s\' to playlist\n"), tmp); + g_free(tmp); + } #endif - } - - if( entry->entity->type!=MPD_INFO_ENTITY_TYPE_SONG ) - continue; - - entry->flags |= HIGHLIGHT; - - if( entry->flags & HIGHLIGHT ) - { - if( entry->entity->type==MPD_INFO_ENTITY_TYPE_SONG ) - { - mpd_Song *song = entry->entity->info.song; - - if( mpdclient_cmd_add(c, song) == 0 ) - { - char buf[BUFSIZE]; - - strfsong(buf, BUFSIZE, LIST_FORMAT, song); - screen_status_printf(_("Adding \'%s\' to playlist\n"), buf); - } - } - } - /*else - { - //remove song from playlist - if( entry->entity->type==MPD_INFO_ENTITY_TYPE_SONG ) - { - mpd_Song *song = entry->entity->info.song; - - if( song ) - { - int index = playlist_get_index_from_file(c, song->file); - - while( (index=playlist_get_index_from_file(c, song->file))>=0 ) - mpdclient_cmd_delete(c, index); - } + } + + if( entry->entity->type!=MPD_INFO_ENTITY_TYPE_SONG ) + continue; + + entry->flags |= HIGHLIGHT; + + if( entry->flags & HIGHLIGHT ) { + if( entry->entity->type==MPD_INFO_ENTITY_TYPE_SONG ) { + mpd_Song *song = entry->entity->info.song; + + if( mpdclient_cmd_add(c, song) == 0 ) { + char buf[BUFSIZE]; + + strfsong(buf, BUFSIZE, LIST_FORMAT, song); + screen_status_printf(_("Adding \'%s\' to playlist\n"), buf); + } + } + } + /* + else { + //remove song from playlist + if( entry->entity->type==MPD_INFO_ENTITY_TYPE_SONG ) { + mpd_Song *song = entry->entity->info.song; + + if( song ) { + int index = playlist_get_index_from_file(c, song->file); + + while( (index=playlist_get_index_from_file(c, song->file))>=0 ) + mpdclient_cmd_delete(c, index); + } + } + } + */ + return 0; } - } - return 0;*/ - } - filelist->list = temp; - return 0; + + filelist->list = temp; + return 0; } static void browse_init(WINDOW *w, int cols, int rows) { - lw = list_window_init(w, cols, rows); - lw_state = list_window_init_state(); + lw = list_window_init(w, cols, rows); + lw_state = list_window_init_state(); } static void browse_resize(int cols, int rows) { - lw->cols = cols; - lw->rows = rows; + lw->cols = cols; + lw->rows = rows; } static void browse_exit(void) { - if( filelist ) - filelist = mpdclient_filelist_free(filelist); - lw = list_window_free(lw); - lw_state = list_window_free_state(lw_state); + if( filelist ) + filelist = mpdclient_filelist_free(filelist); + lw = list_window_free(lw); + lw_state = list_window_free_state(lw_state); } -static void +static void browse_open(screen_t *screen, mpdclient_t *c) { - if( filelist == NULL ) - { - filelist = mpdclient_filelist_get(c, ""); - mpdclient_install_playlist_callback(c, playlist_changed_callback); - mpdclient_install_browse_callback(c, file_changed_callback); - } + if( filelist == NULL ) { + filelist = mpdclient_filelist_get(c, ""); + mpdclient_install_playlist_callback(c, playlist_changed_callback); + mpdclient_install_browse_callback(c, file_changed_callback); + } } static void @@ -645,180 +594,175 @@ browse_close(void) static char * browse_title(char *str, size_t size) { - char *pathcopy; - char *parentdir; - pathcopy = strdup(filelist->path); - parentdir = dirname(pathcopy); - parentdir = basename(parentdir); - if( parentdir[0] == '.' && strlen(parentdir) == 1 ) - { - parentdir = NULL; - } - g_snprintf(str, size, _("Browse: %s%s%s"), - parentdir ? parentdir : "", - parentdir ? "/" : "", - basename(filelist->path)); - free(pathcopy); - return str; + char *pathcopy; + char *parentdir; + + pathcopy = strdup(filelist->path); + parentdir = dirname(pathcopy); + parentdir = basename(parentdir); + + if( parentdir[0] == '.' && strlen(parentdir) == 1 ) { + parentdir = NULL; + } + + g_snprintf(str, size, _("Browse: %s%s%s"), + parentdir ? parentdir : "", + parentdir ? "/" : "", + basename(filelist->path)); + free(pathcopy); + return str; } -static void +static void browse_paint(screen_t *screen, mpdclient_t *c) { - lw->clear = 1; - - list_window_paint(lw, browse_lw_callback, (void *) filelist); - wnoutrefresh(lw->w); + lw->clear = 1; + + list_window_paint(lw, browse_lw_callback, (void *) filelist); + wnoutrefresh(lw->w); } -static void +static void browse_update(screen_t *screen, mpdclient_t *c) { - if( filelist->updated ) - { - browse_paint(screen, c); - filelist->updated = FALSE; - return; - } - list_window_paint(lw, browse_lw_callback, (void *) filelist); - wnoutrefresh(lw->w); + if( filelist->updated ) { + browse_paint(screen, c); + filelist->updated = FALSE; + return; + } + + list_window_paint(lw, browse_lw_callback, (void *) filelist); + wnoutrefresh(lw->w); } #ifdef HAVE_GETMOUSE int -browse_handle_mouse_event(screen_t *screen, +browse_handle_mouse_event(screen_t *screen, mpdclient_t *c, list_window_t *lw, mpdclient_filelist_t *filelist) { - int row; - int prev_selected = lw->selected; - unsigned long bstate; - int length; - - if ( filelist ) - length = filelist->length; - else - length = 0; - - if( screen_get_mouse_event(c, lw, length, &bstate, &row) ) - return 1; - - lw->selected = lw->start+row; - list_window_check_selected(lw, length); - - if( bstate & BUTTON1_CLICKED ) - { - if( prev_selected == lw->selected ) - browse_handle_enter(screen, c, lw, filelist); - } - else if( bstate & BUTTON3_CLICKED ) - { - if( prev_selected == lw->selected ) - browse_handle_select(screen, c, lw, filelist); - } - - return 1; + int row; + int prev_selected = lw->selected; + unsigned long bstate; + int length; + + if ( filelist ) + length = filelist->length; + else + length = 0; + + if( screen_get_mouse_event(c, lw, length, &bstate, &row) ) + return 1; + + lw->selected = lw->start+row; + list_window_check_selected(lw, length); + + if( bstate & BUTTON1_CLICKED ) { + if( prev_selected == lw->selected ) + browse_handle_enter(screen, c, lw, filelist); + } else if( bstate & BUTTON3_CLICKED ) { + if( prev_selected == lw->selected ) + browse_handle_select(screen, c, lw, filelist); + } + + return 1; } -#endif +#endif -static int +static int browse_cmd(screen_t *screen, mpdclient_t *c, command_t cmd) { - switch(cmd) - { - case CMD_PLAY: - browse_handle_enter(screen, c, lw, filelist); - return 1; - case CMD_GO_ROOT_DIRECTORY: - return change_directory(screen, c, NULL, ""); - break; - case CMD_GO_PARENT_DIRECTORY: - return change_directory(screen, c, NULL, ".."); - break; - case CMD_SELECT: - if( browse_handle_select(screen, c, lw, filelist) == 0 ) - { - /* continue and select next item... */ - cmd = CMD_LIST_NEXT; - } - break; - case CMD_DELETE: - handle_delete(screen, c); - break; - case CMD_SAVE_PLAYLIST: - handle_save(screen, c); - break; - case CMD_SCREEN_UPDATE: - screen->painted = 0; - lw->clear = 1; - lw->repaint = 1; - filelist = mpdclient_filelist_update(c, filelist); - list_window_check_selected(lw, filelist->length); - screen_status_printf(_("Screen updated!")); - return 1; - case CMD_DB_UPDATE: - if( !c->status->updatingDb ) - { - if( mpdclient_cmd_db_update_utf8(c,filelist->path)==0 ) - { - if(strcmp(filelist->path,"")) { - screen_status_printf(_("Database update of %s started!"), - filelist->path); - } else { - screen_status_printf(_("Database update started!")); - } - /* set updatingDb to make shure the browse callback gets called - * even if the updated has finished before status is updated */ - c->status->updatingDb = 1; - } - } - else - screen_status_printf(_("Database update running...")); - return 1; - case CMD_LIST_FIND: - case CMD_LIST_RFIND: - case CMD_LIST_FIND_NEXT: - case CMD_LIST_RFIND_NEXT: - return screen_find(screen, - lw, filelist->length, - cmd, browse_lw_callback, (void *) filelist); - case CMD_MOUSE_EVENT: - return browse_handle_mouse_event(screen,c,lw,filelist); - default: - break; - } - return list_window_cmd(lw, filelist->length, cmd); + switch(cmd) + { + case CMD_PLAY: + browse_handle_enter(screen, c, lw, filelist); + return 1; + case CMD_GO_ROOT_DIRECTORY: + return change_directory(screen, c, NULL, ""); + break; + case CMD_GO_PARENT_DIRECTORY: + return change_directory(screen, c, NULL, ".."); + break; + case CMD_SELECT: + if( browse_handle_select(screen, c, lw, filelist) == 0 ) + { + /* continue and select next item... */ + cmd = CMD_LIST_NEXT; + } + break; + case CMD_DELETE: + handle_delete(screen, c); + break; + case CMD_SAVE_PLAYLIST: + handle_save(screen, c); + break; + case CMD_SCREEN_UPDATE: + screen->painted = 0; + lw->clear = 1; + lw->repaint = 1; + filelist = mpdclient_filelist_update(c, filelist); + list_window_check_selected(lw, filelist->length); + screen_status_printf(_("Screen updated!")); + return 1; + case CMD_DB_UPDATE: + if( !c->status->updatingDb ) + { + if( mpdclient_cmd_db_update_utf8(c,filelist->path)==0 ) + { + if(strcmp(filelist->path,"")) { + screen_status_printf(_("Database update of %s started!"), + filelist->path); + } else { + screen_status_printf(_("Database update started!")); + } + /* set updatingDb to make shure the browse callback gets called + * even if the updated has finished before status is updated */ + c->status->updatingDb = 1; + } + } + else + screen_status_printf(_("Database update running...")); + return 1; + case CMD_LIST_FIND: + case CMD_LIST_RFIND: + case CMD_LIST_FIND_NEXT: + case CMD_LIST_RFIND_NEXT: + return screen_find(screen, + lw, filelist->length, + cmd, browse_lw_callback, (void *) filelist); + case CMD_MOUSE_EVENT: + return browse_handle_mouse_event(screen,c,lw,filelist); + default: + break; + } + return list_window_cmd(lw, filelist->length, cmd); } - list_window_t * get_filelist_window() { - return lw; + 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; + 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; } diff --git a/src/screen_help.c b/src/screen_help.c index 4a7961427..46fefd9d6 100644 --- a/src/screen_help.c +++ b/src/screen_help.c @@ -31,11 +31,10 @@ #include "screen_utils.h" -typedef struct -{ - signed char highlight; - command_t command; - char *text; +typedef struct { + signed char highlight; + command_t command; + char *text; } help_text_row_t; static help_text_row_t help_text[] = @@ -150,50 +149,45 @@ static list_window_t *lw = NULL; static char * list_callback(int index, int *highlight, void *data) { - static char buf[512]; - - if( help_text_rows<0 ) - { - help_text_rows = 0; - while( help_text[help_text_rows].highlight != -1 ) - help_text_rows++; - } - - *highlight = 0; - if( index<help_text_rows ) - { - *highlight = help_text[index].highlight > 0; - if( help_text[index].command == CMD_NONE ) - { - if( help_text[index].text ) - g_snprintf(buf, sizeof(buf), " %s", _(help_text[index].text)); - else - if( help_text[index].highlight == 2 ) - { - int i; - - for(i=3; i<COLS-3 && i<sizeof(buf); i++) - buf[i]='-'; - buf[i] = '\0'; - } - else - g_strlcpy(buf, " ", sizeof(buf)); - return buf; + static char buf[512]; + + if (help_text_rows < 0) { + help_text_rows = 0; + while (help_text[help_text_rows].highlight != -1) + help_text_rows++; } - if( help_text[index].text ) - g_snprintf(buf, sizeof(buf), - "%20s : %s ", - get_key_names(help_text[index].command, TRUE), - _(help_text[index].text)); - else - g_snprintf(buf, sizeof(buf), - "%20s : %s ", - get_key_names(help_text[index].command, TRUE), - get_key_description(help_text[index].command)); - return buf; - } - - return NULL; + + *highlight = 0; + if (index < help_text_rows) { + *highlight = help_text[index].highlight > 0; + if (help_text[index].command == CMD_NONE) { + if (help_text[index].text) + g_snprintf(buf, sizeof(buf), " %s", _(help_text[index].text)); + else if (help_text[index].highlight == 2) { + int i; + + for (i = 3; i < COLS - 3 && i < sizeof(buf); i++) + buf[i] = '-'; + buf[i] = '\0'; + } else + g_strlcpy(buf, " ", sizeof(buf)); + return buf; + } + + if (help_text[index].text) + g_snprintf(buf, sizeof(buf), + "%20s : %s ", + get_key_names(help_text[index].command, TRUE), + _(help_text[index].text)); + else + g_snprintf(buf, sizeof(buf), + "%20s : %s ", + get_key_names(help_text[index].command, TRUE), + get_key_description(help_text[index].command)); + return buf; + } + + return NULL; } static void @@ -220,107 +214,104 @@ help_exit(void) static char * help_title(char *str, size_t size) { - return _("Help"); + return _("Help"); } -static void +static void help_paint(screen_t *screen, mpdclient_t *c) { - lw->clear = 1; - list_window_paint(lw, list_callback, NULL); - wrefresh(lw->w); + lw->clear = 1; + list_window_paint(lw, list_callback, NULL); + wrefresh(lw->w); } -static void +static void help_update(screen_t *screen, mpdclient_t *c) -{ - if( lw->repaint ) - { - list_window_paint(lw, list_callback, NULL); - wrefresh(lw->w); - lw->repaint = 0; - } +{ + if (lw->repaint) { + list_window_paint(lw, list_callback, NULL); + wrefresh(lw->w); + lw->repaint = 0; + } } -static int +static int help_cmd(screen_t *screen, mpdclient_t *c, command_t cmd) { - lw->repaint=1; - lw->clear=1; - switch(cmd) - { - case CMD_LIST_NEXT: - if( lw->start+lw->rows < help_text_rows ) - lw->start++; - return 1; - case CMD_LIST_PREVIOUS: - if( lw->start >0 ) - lw->start--; - return 1; - case CMD_LIST_FIRST: - lw->start = 0; - return 1; - case CMD_LIST_LAST: - lw->start = help_text_rows-lw->rows; - if( lw->start<0 ) - lw->start = 0; - return 1; - case CMD_LIST_NEXT_PAGE: - lw->start = lw->start + lw->rows; - if( lw->start+lw->rows >= help_text_rows ) - lw->start = help_text_rows-lw->rows; - if( lw->start<0 ) - lw->start = 0; - return 1; - case CMD_LIST_PREVIOUS_PAGE: - lw->start = lw->start - lw->rows; - if( lw->start<0 ) - lw->start = 0; - return 1; - default: - break; - } - - lw->selected = lw->start+lw->rows; - if( screen_find(screen, - lw, help_text_rows, - cmd, list_callback, NULL) ) - { - /* center the row */ - lw->start = lw->selected-(lw->rows/2); - if( lw->start+lw->rows > help_text_rows ) - lw->start = help_text_rows-lw->rows; - if( lw->start<0 ) - lw->start=0; - return 1; - } - - return 0; + lw->repaint=1; + lw->clear=1; + switch(cmd) { + case CMD_LIST_NEXT: + if (lw->start + lw->rows < help_text_rows) + lw->start++; + return 1; + case CMD_LIST_PREVIOUS: + if (lw->start > 0) + lw->start--; + return 1; + case CMD_LIST_FIRST: + lw->start = 0; + return 1; + case CMD_LIST_LAST: + lw->start = help_text_rows - lw->rows; + if (lw->start < 0) + lw->start = 0; + return 1; + case CMD_LIST_NEXT_PAGE: + lw->start = lw->start + lw->rows; + if (lw->start + lw->rows >= help_text_rows) + lw->start = help_text_rows - lw->rows; + if (lw->start < 0) + lw->start = 0; + return 1; + case CMD_LIST_PREVIOUS_PAGE: + lw->start = lw->start - lw->rows; + if (lw->start < 0) + lw->start = 0; + return 1; + default: + break; + } + + lw->selected = lw->start+lw->rows; + if (screen_find(screen, + lw, help_text_rows, + cmd, list_callback, NULL)) { + /* center the row */ + lw->start = lw->selected - (lw->rows / 2); + if (lw->start + lw->rows > help_text_rows) + lw->start = help_text_rows - lw->rows; + if (lw->start < 0) + lw->start = 0; + return 1; + } + + return 0; } static list_window_t * help_lw(void) { - return lw; + 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; + 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; } diff --git a/src/screen_keydef.c b/src/screen_keydef.c index 5014b294f..a8f32d3da 100644 --- a/src/screen_keydef.c +++ b/src/screen_keydef.c @@ -185,44 +185,36 @@ assign_new_key(WINDOW *w, int cmd_index, int key_index) static char * list_callback(int index, int *highlight, void *data) { - static char buf[BUFSIZE]; - - *highlight = 0; - if( subcmd <0 ) - { - if( index<command_list_length ) - { - if( cmds[index].flags & COMMAND_KEY_CONFLICT ) - *highlight = 1; - return cmds[index].name; + static char buf[BUFSIZE]; + + *highlight = 0; + if (subcmd < 0) { + if (index < command_list_length) { + if (cmds[index].flags & COMMAND_KEY_CONFLICT) + *highlight = 1; + return cmds[index].name; + } else if (index == LIST_ITEM_APPLY()) + return LIST_ITEM_APPLY_LABEL; + else if (index == LIST_ITEM_SAVE()) + return LIST_ITEM_SAVE_LABEL; + } else { + if (index == 0) + return "[..]"; + index--; + if (index < MAX_COMMAND_KEYS && cmds[subcmd].keys[index] > 0) { + g_snprintf(buf, + BUFSIZE, "%d. %-20s (%d) ", + index + 1, + key2str(cmds[subcmd].keys[index]), + cmds[subcmd].keys[index]); + return buf; + } else if (index == subcmd_addpos) { + g_snprintf(buf, BUFSIZE, _("%d. Add new key "), index + 1); + return buf; + } } - else if( index==LIST_ITEM_APPLY() ) - return LIST_ITEM_APPLY_LABEL; - else if( index==LIST_ITEM_SAVE() ) - return LIST_ITEM_SAVE_LABEL; - } - else - { - if( index== 0 ) - return "[..]"; - index--; - if( index<MAX_COMMAND_KEYS && cmds[subcmd].keys[index]>0 ) - { - g_snprintf(buf, - BUFSIZE, "%d. %-20s (%d) ", - index+1, - key2str(cmds[subcmd].keys[index]), - cmds[subcmd].keys[index]); - return buf; - } - else if ( index==subcmd_addpos ) - { - g_snprintf(buf, BUFSIZE, _("%d. Add new key "), index+1 ); - return buf; - } - } - - return NULL; + + return NULL; } static void @@ -312,70 +304,61 @@ keydef_update(screen_t *screen, mpdclient_t *c) } } -static int +static int keydef_cmd(screen_t *screen, mpdclient_t *c, command_t cmd) { - int length = LIST_LENGTH(); - - if( subcmd>=0 ) - length = subcmd_length; - - switch(cmd) - { - case CMD_PLAY: - if( subcmd<0 ) - { - if( lw->selected == LIST_ITEM_APPLY() ) - apply_keys(); - else if( lw->selected == LIST_ITEM_SAVE() ) - { - apply_keys(); - save_keys(); - } - else - { - subcmd = lw->selected; - lw->selected=0; - check_subcmd_length(); - } - } - else - { - if( lw->selected == 0 ) /* up */ - { - lw->selected = subcmd; - subcmd = -1; - } - else - assign_new_key(screen->status_window.w, - subcmd, - lw->selected-STATIC_SUB_ITEMS); + int length = LIST_LENGTH(); + + if (subcmd >= 0) + length = subcmd_length; + + switch(cmd) { + case CMD_PLAY: + if( subcmd<0 ) { + if( lw->selected == LIST_ITEM_APPLY() ) + apply_keys(); + else if( lw->selected == LIST_ITEM_SAVE() ) { + apply_keys(); + save_keys(); + } else { + subcmd = lw->selected; + lw->selected=0; + check_subcmd_length(); + } + } else { + if (lw->selected == 0) { /* up */ + lw->selected = subcmd; + subcmd = -1; + } else + assign_new_key(screen->status_window.w, + subcmd, + lw->selected-STATIC_SUB_ITEMS); + } + lw->repaint = 1; + lw->clear = 1; + return 1; + case CMD_DELETE: + if (subcmd >= 0 && lw->selected - STATIC_SUB_ITEMS >= 0) + delete_key(subcmd, lw->selected - STATIC_SUB_ITEMS); + return 1; + break; + case CMD_SAVE_PLAYLIST: + apply_keys(); + save_keys(); + break; + case CMD_LIST_FIND: + case CMD_LIST_RFIND: + case CMD_LIST_FIND_NEXT: + case CMD_LIST_RFIND_NEXT: + return screen_find(screen, + lw, length, + cmd, list_callback, NULL); + + default: + break; } - lw->repaint = 1; - lw->clear = 1; - return 1; - case CMD_DELETE: - if( subcmd>=0 && lw->selected-STATIC_SUB_ITEMS>=0 ) - delete_key(subcmd, lw->selected-STATIC_SUB_ITEMS); - return 1; - break; - case CMD_SAVE_PLAYLIST: - apply_keys(); - save_keys(); - break; - case CMD_LIST_FIND: - case CMD_LIST_RFIND: - case CMD_LIST_FIND_NEXT: - case CMD_LIST_RFIND_NEXT: - return screen_find(screen, - lw, length, - cmd, list_callback, NULL); - - default: - break; - } - return list_window_cmd(lw, length, cmd); + return list_window_cmd(lw, length, cmd); } static list_window_t * diff --git a/src/screen_lyrics.c b/src/screen_lyrics.c index 4cdc8f03a..ec028a392 100644 --- a/src/screen_lyrics.c +++ b/src/screen_lyrics.c @@ -48,52 +48,54 @@ static void lyrics_paint(screen_t *screen, mpdclient_t *c); FILE *create_lyr_file(char *artist, char *title) { - char path[1024]; - - snprintf(path, 1024, "%s/.lyrics", - getenv("HOME")); - if(g_access(path, W_OK) != 0) if(mkdir(path, S_IRWXU) != 0) return NULL; - - snprintf(path, 1024, "%s/.lyrics/%s", - getenv("HOME"), artist); - if(g_access(path, W_OK) != 0) if(mkdir(path, S_IRWXU) != 0) return NULL; - - snprintf(path, 1024, "%s/.lyrics/%s/%s.lyric", - getenv("HOME"), artist, title); - - return fopen(path, "w"); -} - - + char path[1024]; + + snprintf(path, 1024, "%s/.lyrics", + getenv("HOME")); + if(g_access(path, W_OK) != 0) if(mkdir(path, S_IRWXU) != 0) return NULL; + + snprintf(path, 1024, "%s/.lyrics/%s", + getenv("HOME"), artist); + if(g_access(path, W_OK) != 0) if(mkdir(path, S_IRWXU) != 0) return NULL; + + snprintf(path, 1024, "%s/.lyrics/%s/%s.lyric", + getenv("HOME"), artist, title); + + return fopen(path, "w"); +} + + int store_lyr_hd() { - char artist[512]; - char title[512]; - static char path[1024]; - FILE *lyr_file; - - get_text_line(&lyr_text, 0, artist, 512); - get_text_line(&lyr_text, 1, title, 512); - artist[strlen(artist)-1] = '\0'; - title[strlen(title)-1] = '\0'; - - snprintf(path, 1024, "%s/.lyrics/%s/%s.lyric", - getenv("HOME"), artist, title); - lyr_file = create_lyr_file(artist, title); - if(lyr_file == NULL) return -1; - int i; - char line_buf[1024]; - - for(i = 3; i <= lyr_text.text->len; i++) - { - if(get_text_line(&lyr_text, i, line_buf, 1024) == -1); - fputs(line_buf, lyr_file); - } - fclose(lyr_file); - return 0; + char artist[512]; + char title[512]; + static char path[1024]; + FILE *lyr_file; + + get_text_line(&lyr_text, 0, artist, 512); + get_text_line(&lyr_text, 1, title, 512); + artist[strlen(artist)-1] = '\0'; + title[strlen(title)-1] = '\0'; + + snprintf(path, 1024, "%s/.lyrics/%s/%s.lyric", + getenv("HOME"), artist, title); + lyr_file = create_lyr_file(artist, title); + if (lyr_file == NULL) + return -1; + + int i; + char line_buf[1024]; + + for (i = 3; i <= lyr_text.text->len; i++) { + if(get_text_line(&lyr_text, i, line_buf, 1024) == -1); + fputs(line_buf, lyr_file); + } + + fclose(lyr_file); + return 0; } - - + + void check_repaint() { if(screen_get_id("lyrics") == get_cur_mode_id())lyrics_paint(NULL, NULL); @@ -102,250 +104,243 @@ void check_repaint() gpointer get_lyr(void *c) { - mpd_Status *status = ((retrieval_spec*)c)->client->status; - mpd_Song *cur = ((retrieval_spec*)c)->client->song; - //mpdclient_update((mpdclient_t*)c); - - if(!(IS_PAUSED(status->state)||IS_PLAYING(status->state))) - { - formed_text_init(&lyr_text); - return NULL; - } - - - char artist[MAX_SONGNAME_LENGTH]; - char title[MAX_SONGNAME_LENGTH]; + mpd_Status *status = ((retrieval_spec*)c)->client->status; + mpd_Song *cur = ((retrieval_spec*)c)->client->song; + //mpdclient_update((mpdclient_t*)c); + + if(!(IS_PAUSED(status->state)||IS_PLAYING(status->state))) { + formed_text_init(&lyr_text); + return NULL; + } + + + char artist[MAX_SONGNAME_LENGTH]; + char title[MAX_SONGNAME_LENGTH]; lock=2; - result = 0; - - formed_text_init(&lyr_text); - - strfsong(artist, MAX_SONGNAME_LENGTH, "%artist%", cur); - strfsong(title, MAX_SONGNAME_LENGTH, "%title%", cur); - - //write header.. - formed_text_init(&lyr_text); - add_text_line(&lyr_text, artist, 0); - add_text_line(&lyr_text, title, 0); - add_text_line(&lyr_text, "", 0); - add_text_line(&lyr_text, "", 0); - - if (((retrieval_spec*)c)->way != -1) /*till it'S of use*/ - { - if(get_lyr_by_src (src_selection, artist, title) != 0) - { - lock=0; - return NULL; - } - } - /*else{ - if(get_lyr_hd(artist, title) != 0) - { - if(get_lyr_hd(artist, title) != 0) return NULL; - } - else result |= 1; - }*/ + result = 0; + + formed_text_init(&lyr_text); + + strfsong(artist, MAX_SONGNAME_LENGTH, "%artist%", cur); + strfsong(title, MAX_SONGNAME_LENGTH, "%title%", cur); + + //write header.. + formed_text_init(&lyr_text); + add_text_line(&lyr_text, artist, 0); + add_text_line(&lyr_text, title, 0); + add_text_line(&lyr_text, "", 0); + add_text_line(&lyr_text, "", 0); + + if (((retrieval_spec*)c)->way != -1) /*till it'S of use*/ { + if(get_lyr_by_src (src_selection, artist, title) != 0) { + lock=0; + return NULL; + } + } + /*else{ + if(get_lyr_hd(artist, title) != 0) + { + if(get_lyr_hd(artist, title) != 0) return NULL; + } + else result |= 1; + }*/ //return NULL; - lw->start = 0; - check_repaint(); - lock = 1; - return &lyr_text; -} + lw->start = 0; + check_repaint(); + lock = 1; + return &lyr_text; +} static char * list_callback(int index, int *highlight, void *data) { - static char buf[512]; - - //i think i'ts fine to write it into the 1st line... - if((index == lyr_text.lines->len && lyr_text.lines->len > 4)|| - ((lyr_text.lines->len == 0 - ||lyr_text.lines->len == 4) && index == 0)) - { - *highlight=3; - src_lyr* selected = g_array_index (src_lyr_stack, src_lyr*, src_selection); - if (selected != NULL) return selected->description; - return ""; - } - - if(index < 2 && lyr_text.lines->len > 4) *highlight=3; - else if(index >= lyr_text.lines->len || - ( index < 4 && index != 0 && lyr_text.lines->len < 5)) - { - return ""; - } - - get_text_line(&lyr_text, index, buf, 512); - return buf; -} + static char buf[512]; + + //i think i'ts fine to write it into the 1st line... + if ((index == lyr_text.lines->len && lyr_text.lines->len > 4) || + ((lyr_text.lines->len == 0 || lyr_text.lines->len == 4) && + index == 0)) { + *highlight=3; + src_lyr* selected = g_array_index(src_lyr_stack, src_lyr*, src_selection); + if (selected != NULL) + return selected->description; + return ""; + } + + if (index < 2 && lyr_text.lines->len > 4) + *highlight=3; + else if(index >= lyr_text.lines->len || + (index < 4 && index != 0 && lyr_text.lines->len < 5)) { + return ""; + } + + get_text_line(&lyr_text, index, buf, 512); + return buf; +} static void lyrics_init(WINDOW *w, int cols, int rows) { - lw = list_window_init(w, cols, rows); - lw->flags = LW_HIDE_CURSOR; - //lyr_text.lines = g_array_new(FALSE, TRUE, 4); - formed_text_init(&lyr_text); - if (!g_thread_supported()) g_thread_init(NULL); - + lw = list_window_init(w, cols, rows); + lw->flags = LW_HIDE_CURSOR; + //lyr_text.lines = g_array_new(FALSE, TRUE, 4); + formed_text_init(&lyr_text); + if (!g_thread_supported()) + g_thread_init(NULL); } static void lyrics_resize(int cols, int rows) { - lw->cols = cols; - lw->rows = rows; + lw->cols = cols; + lw->rows = rows; } static void lyrics_exit(void) { - list_window_free(lw); + list_window_free(lw); } static char * lyrics_title(char *str, size_t size) { - static GString *msg; - if (msg == NULL) - msg = g_string_new (""); - else g_string_erase (msg, 0, -1); - - g_string_append (msg, "Lyrics ["); - - if (src_selection > src_lyr_stack->len-1) - g_string_append (msg, "No plugin available"); - else - { - src_lyr* selected = g_array_index (src_lyr_stack, src_lyr*, src_selection); - if (selected != NULL) - g_string_append (msg, selected->name); - else g_string_append (msg, "NONE"); - } - if(lyr_text.lines->len == 4) - { - if(lock == 1) - { - if(!(result & 1)) - { + static GString *msg; + if (msg == NULL) + msg = g_string_new (""); + else g_string_erase (msg, 0, -1); + + g_string_append (msg, "Lyrics ["); + + if (src_selection > src_lyr_stack->len - 1) + g_string_append (msg, "No plugin available"); + else { + src_lyr* selected = g_array_index (src_lyr_stack, src_lyr*, src_selection); + if (selected != NULL) + g_string_append (msg, selected->name); + else + g_string_append (msg, "NONE"); + } + + if(lyr_text.lines->len == 4) { + if(lock == 1) { + if(!(result & 1)) { g_string_append (msg, " - "); - if(!(result & 2)) g_string_append (msg, _("No access")); - else if(!(result & 4)||!(result & 16)) g_string_append (msg, _("Not found")); - } - } - if(lock == 2) - { - g_string_append (msg, " - "); - g_string_append (msg, _("retrieving")); - } - } + if(!(result & 2)) g_string_append (msg, _("No access")); + else if(!(result & 4)||!(result & 16)) g_string_append (msg, _("Not found")); + } + } + if(lock == 2) { + g_string_append (msg, " - "); + g_string_append (msg, _("retrieving")); + } + } g_string_append_c (msg, ']'); return msg->str; } -static void +static void lyrics_paint(screen_t *screen, mpdclient_t *c) { - lw->clear = 1; - list_window_paint(lw, list_callback, NULL); - wrefresh(lw->w); + lw->clear = 1; + list_window_paint(lw, list_callback, NULL); + wrefresh(lw->w); } -static void +static void lyrics_update(screen_t *screen, mpdclient_t *c) -{ - if( lw->repaint ) - { - list_window_paint(lw, list_callback, NULL); - wrefresh(lw->w); - lw->repaint = 0; - } +{ + if( lw->repaint ) { + list_window_paint(lw, list_callback, NULL); + wrefresh(lw->w); + lw->repaint = 0; + } } -static int +static int lyrics_cmd(screen_t *screen, mpdclient_t *c, command_t cmd) { - lw->repaint=1; - static retrieval_spec spec; - switch(cmd) - { - case CMD_LIST_NEXT: - if( lw->start+lw->rows < lyr_text.lines->len+1 ) - lw->start++; - return 1; - case CMD_LIST_PREVIOUS: - if( lw->start >0 ) - lw->start--; - return 1; - case CMD_LIST_FIRST: - lw->start = 0; - return 1; - case CMD_LIST_LAST: - lw->start = lyrics_text_rows-lw->rows; - if( lw->start<0 ) - lw->start = 0; - return 1; - case CMD_LIST_NEXT_PAGE: - lw->start = lw->start + lw->rows-1; - if( lw->start+lw->rows >= lyr_text.lines->len+1 ) - lw->start = lyr_text.lines->len-lw->rows+1; - if( lw->start<0 ) - lw->start = 0; - return 1; - case CMD_LIST_PREVIOUS_PAGE: - lw->start = lw->start - lw->rows; - if( lw->start<0 ) - lw->start = 0; - return 1; - case CMD_SELECT: - spec.client = c; - spec.way = 0; - g_thread_create(get_lyr, &spec, FALSE, NULL); - return 1; - case CMD_INTERRUPT: - if(lock > 1) lock = 4; - return 1; - case CMD_ADD: - if(lock > 0 && lock != 4) - { - if(store_lyr_hd() == 0) screen_status_message (_("Lyrics saved!")); - } - return 1; - case CMD_LYRICS_UPDATE: - spec.client = c; - spec.way = 1; - g_thread_create(get_lyr, &spec, FALSE, NULL); - return 1; + static retrieval_spec spec; + + lw->repaint=1; + switch(cmd) { + case CMD_LIST_NEXT: + if( lw->start+lw->rows < lyr_text.lines->len+1 ) + lw->start++; + return 1; + case CMD_LIST_PREVIOUS: + if( lw->start >0 ) + lw->start--; + return 1; + case CMD_LIST_FIRST: + lw->start = 0; + return 1; + case CMD_LIST_LAST: + lw->start = lyrics_text_rows-lw->rows; + if( lw->start<0 ) + lw->start = 0; + return 1; + case CMD_LIST_NEXT_PAGE: + lw->start = lw->start + lw->rows-1; + if( lw->start+lw->rows >= lyr_text.lines->len+1 ) + lw->start = lyr_text.lines->len-lw->rows+1; + if( lw->start<0 ) + lw->start = 0; + return 1; + case CMD_LIST_PREVIOUS_PAGE: + lw->start = lw->start - lw->rows; + if( lw->start<0 ) + lw->start = 0; + return 1; + case CMD_SELECT: + spec.client = c; + spec.way = 0; + g_thread_create(get_lyr, &spec, FALSE, NULL); + return 1; + case CMD_INTERRUPT: + if(lock > 1) lock = 4; + return 1; + case CMD_ADD: + if(lock > 0 && lock != 4) { + if(store_lyr_hd() == 0) + screen_status_message (_("Lyrics saved!")); + } + return 1; + case CMD_LYRICS_UPDATE: + spec.client = c; + spec.way = 1; + g_thread_create(get_lyr, &spec, FALSE, NULL); + return 1; case CMD_SEARCH_MODE: - //while (0==0) fprintf (stderr, "%i", src_lyr_stack->len); - if (src_selection == src_lyr_stack->len-1) src_selection = -1; - src_selection++; - return 1; + //while (0==0) fprintf (stderr, "%i", src_lyr_stack->len); + if (src_selection == src_lyr_stack->len - 1) + src_selection = -1; + src_selection++; + return 1; default: - break; - } - - lw->selected = lw->start+lw->rows; - if( screen_find(screen, - lw, lyrics_text_rows, - cmd, list_callback, NULL) ) - { - /* center the row */ - lw->start = lw->selected-(lw->rows/2); - if( lw->start+lw->rows > lyrics_text_rows ) - lw->start = lyrics_text_rows-lw->rows; - if( lw->start<0 ) - lw->start=0; - return 1; - } - - return 0; + break; + } + + lw->selected = lw->start+lw->rows; + if (screen_find(screen, + lw, lyrics_text_rows, + cmd, list_callback, NULL)) { + /* center the row */ + lw->start = lw->selected - (lw->rows / 2); + if (lw->start + lw->rows > lyrics_text_rows) + lw->start = lyrics_text_rows - lw->rows; + if (lw->start < 0) + lw->start = 0; + return 1; + } + + return 0; } static list_window_t * diff --git a/src/screen_play.c b/src/screen_play.c index c9bc84e19..f94b5fbfe 100644 --- a/src/screen_play.c +++ b/src/screen_play.c @@ -50,109 +50,102 @@ typedef struct static list_window_t *lw = NULL; -static void +static void playlist_changed_callback(mpdclient_t *c, int event, gpointer data) { - D("screen_play.c> playlist_callback() [%d]\n", event); - switch(event) - { - case PLAYLIST_EVENT_DELETE: - break; - case PLAYLIST_EVENT_MOVE: - lw->selected = *((int *) data); - if( lw->selected<lw->start ) - lw->start--; - break; - default: - break; - } - /* make shure the playlist is repainted */ - lw->clear = 1; - lw->repaint = 1; - list_window_check_selected(lw, c->playlist.length); + D("screen_play.c> playlist_callback() [%d]\n", event); + switch(event) { + case PLAYLIST_EVENT_DELETE: + break; + case PLAYLIST_EVENT_MOVE: + lw->selected = *((int *) data); + if( lw->selected<lw->start ) + lw->start--; + break; + default: + break; + } + /* make shure the playlist is repainted */ + lw->clear = 1; + lw->repaint = 1; + list_window_check_selected(lw, c->playlist.length); } static char * list_callback(int index, int *highlight, void *data) { - static char songname[MAX_SONG_LENGTH]; - mpdclient_t *c = (mpdclient_t *) data; - mpd_Song *song; + static char songname[MAX_SONG_LENGTH]; + mpdclient_t *c = (mpdclient_t *) data; + mpd_Song *song; - *highlight = 0; - if( (song=playlist_get_song(c, index)) == NULL ) - { - return NULL; - } + *highlight = 0; + if( (song=playlist_get_song(c, index)) == NULL ) { + return NULL; + } - if( c->song && song->id==c->song->id && !IS_STOPPED(c->status->state) ) - { - *highlight = 1; - } - strfsong(songname, MAX_SONG_LENGTH, LIST_FORMAT, song); - return songname; + if( c->song && song->id==c->song->id && !IS_STOPPED(c->status->state) ) { + *highlight = 1; + } + strfsong(songname, MAX_SONG_LENGTH, LIST_FORMAT, song); + return songname; } static int center_playing_item(screen_t *screen, mpdclient_t *c) { - int length = c->playlist.length; - int offset = lw->selected-lw->start; - int index; - - if( !lw || !c->song || length<lw->rows || IS_STOPPED(c->status->state) ) - return 0; - - /* try to center the song that are playing */ - index = playlist_get_index(c, c->song); - D("Autocenter song id:%d pos:%d index:%d\n", c->song->id,c->song->pos,index); - lw->start = index-(lw->rows/2); - if( lw->start+lw->rows > length ) - lw->start = length-lw->rows; - if( lw->start<0 ) - lw->start=0; - - /* make sure the cursor is in the window */ - lw->selected = lw->start+offset; - list_window_check_selected(lw, length); - - lw->clear = 1; - lw->repaint = 1; - - return 0; + int length = c->playlist.length; + int offset = lw->selected - lw->start; + int index; + + if (!lw || !c->song || length<lw->rows || + IS_STOPPED(c->status->state)) + return 0; + + /* try to center the song that are playing */ + index = playlist_get_index(c, c->song); + D("Autocenter song id:%d pos:%d index:%d\n", c->song->id,c->song->pos,index); + lw->start = index - (lw->rows / 2); + if (lw->start + lw->rows > length) + lw->start = length - lw->rows; + if (lw->start < 0) + lw->start = 0; + + /* make sure the cursor is in the window */ + lw->selected = lw->start+offset; + list_window_check_selected(lw, length); + + lw->clear = 1; + lw->repaint = 1; + + return 0; } +void save_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data) +{ + completion_callback_data_t *tmp = (completion_callback_data_t *)data; + GList **list = tmp->list; + mpdclient_t *c = tmp->c; + + if( *list == NULL ) { + /* create completion list */ + *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST); + g_completion_add_items(gcmp, *list); + } +} +void save_post_completion_cb(GCompletion *gcmp, gchar *line, GList *items, + void *data) +{ + completion_callback_data_t *tmp = (completion_callback_data_t *)data; + screen_t *screen = tmp->screen; + if( g_list_length(items)>=1 ) { + screen_display_completion_list(screen, items); + lw->clear = 1; + lw->repaint = 1; + } +} - -void save_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data) - { - completion_callback_data_t *tmp = (completion_callback_data_t *)data; - GList **list = tmp->list; - mpdclient_t *c = tmp->c; - - if( *list == NULL ) - { - /* create completion list */ - *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST); - g_completion_add_items(gcmp, *list); - } - } - - void save_post_completion_cb(GCompletion *gcmp, gchar *line, GList *items, - void *data) - { - completion_callback_data_t *tmp = (completion_callback_data_t *)data; - screen_t *screen = tmp->screen; - - if( g_list_length(items)>=1 ) - { - screen_display_completion_list(screen, items); - lw->clear = 1; - lw->repaint = 1; - } - } int playlist_save(screen_t *screen, mpdclient_t *c, char *name, char *defaultname) { @@ -335,135 +328,122 @@ handle_add_to_playlist(screen_t *screen, mpdclient_t *c) static void play_init(WINDOW *w, int cols, int rows) { - lw = list_window_init(w, cols, rows); + lw = list_window_init(w, cols, rows); } static void play_open(screen_t *screen, mpdclient_t *c) { - static gboolean install_cb = TRUE; + static gboolean install_cb = TRUE; - if( install_cb ) - { - mpdclient_install_playlist_callback(c, playlist_changed_callback); - install_cb = FALSE; - } + if (install_cb) { + mpdclient_install_playlist_callback(c, playlist_changed_callback); + install_cb = FALSE; + } } static void play_resize(int cols, int rows) { - lw->cols = cols; - lw->rows = rows; + lw->cols = cols; + lw->rows = rows; } static void play_exit(void) { - list_window_free(lw); + list_window_free(lw); } static char * play_title(char *str, size_t size) { - if( strcmp(options.host, "localhost") == 0 ) - return _("Playlist"); - - g_snprintf(str, size, _("Playlist on %s"), options.host); + if( strcmp(options.host, "localhost") == 0 ) + return _("Playlist"); - return str; + g_snprintf(str, size, _("Playlist on %s"), options.host); + return str; } static void play_paint(screen_t *screen, mpdclient_t *c) -{ - lw->clear = 1; +{ + lw->clear = 1; - list_window_paint(lw, list_callback, (void *) c); - wnoutrefresh(lw->w); + list_window_paint(lw, list_callback, (void *) c); + wnoutrefresh(lw->w); } static void play_update(screen_t *screen, mpdclient_t *c) { - /* hide the cursor when mpd are playing and the user are inactive */ - if( options.hide_cursor>0 && c->status->state == MPD_STATUS_STATE_PLAY && - time(NULL)-screen->input_timestamp >= options.hide_cursor ) - { - lw->flags |= LW_HIDE_CURSOR; - } - else - { - lw->flags &= ~LW_HIDE_CURSOR; - } - - /* center the cursor */ - if( options.auto_center ) - { - static int prev_song_id = 0; - - if( c->song && prev_song_id != c->song->id ) - { - center_playing_item(screen, c); - prev_song_id = c->song->id; + /* hide the cursor when mpd are playing and the user are inactive */ + if( options.hide_cursor>0 && c->status->state == MPD_STATUS_STATE_PLAY && + time(NULL)-screen->input_timestamp >= options.hide_cursor ) { + lw->flags |= LW_HIDE_CURSOR; + } else { + lw->flags &= ~LW_HIDE_CURSOR; } - } - if( c->playlist.updated ) - { - if( lw->selected >= c->playlist.length ) - lw->selected = c->playlist.length-1; - if( lw->start >= c->playlist.length ) - list_window_reset(lw); + /* center the cursor */ + if( options.auto_center ) { + static int prev_song_id = 0; - play_paint(screen, c); - c->playlist.updated = FALSE; - } - else if( lw->repaint || 1) - { - list_window_paint(lw, list_callback, (void *) c); - wnoutrefresh(lw->w); - lw->repaint = 0; - } + if( c->song && prev_song_id != c->song->id ) { + center_playing_item(screen, c); + prev_song_id = c->song->id; + } + } + + if( c->playlist.updated ) { + if( lw->selected >= c->playlist.length ) + lw->selected = c->playlist.length-1; + if( lw->start >= c->playlist.length ) + list_window_reset(lw); + + play_paint(screen, c); + c->playlist.updated = FALSE; + } else if( lw->repaint || 1) { + list_window_paint(lw, list_callback, (void *) c); + wnoutrefresh(lw->w); + lw->repaint = 0; + } } #ifdef HAVE_GETMOUSE static int handle_mouse_event(screen_t *screen, mpdclient_t *c) { - int row; - int selected; - unsigned long bstate; + int row; + int selected; + unsigned long bstate; - if( screen_get_mouse_event(c, lw, c->playlist.length, &bstate, &row) ) - return 1; + if (screen_get_mouse_event(c, lw, c->playlist.length, &bstate, &row)) + return 1; - if( bstate & BUTTON1_DOUBLE_CLICKED ) - { - /* stop */ - screen_cmd(c, CMD_STOP); - return 1; - } + if (bstate & BUTTON1_DOUBLE_CLICKED) { + /* stop */ + screen_cmd(c, CMD_STOP); + return 1; + } - selected = lw->start+row; + selected = lw->start + row; - if( bstate & BUTTON1_CLICKED ) - { - /* play */ - if( lw->start+row < c->playlist.length ) - mpdclient_cmd_play(c, lw->start+row); - } - else if( bstate & BUTTON3_CLICKED ) - { - /* delete */ - if( selected == lw->selected ) - mpdclient_cmd_delete(c, lw->selected); - } - lw->selected = selected; - list_window_check_selected(lw, c->playlist.length); + if (bstate & BUTTON1_CLICKED) { + /* play */ + if (lw->start + row < c->playlist.length) + mpdclient_cmd_play(c, lw->start + row); + } else if (bstate & BUTTON3_CLICKED) { + /* delete */ + if (selected == lw->selected) + mpdclient_cmd_delete(c, lw->selected); + } + + lw->selected = selected; + list_window_check_selected(lw, c->playlist.length); - return 1; + return 1; } #else #define handle_mouse_event(s,c) (0) @@ -472,56 +452,52 @@ handle_mouse_event(screen_t *screen, mpdclient_t *c) static int play_cmd(screen_t *screen, mpdclient_t *c, command_t cmd) { - switch(cmd) - { - case CMD_PLAY: - mpdclient_cmd_play(c, lw->selected); - return 1; - case CMD_DELETE: - mpdclient_cmd_delete(c, lw->selected); - return 1; - case CMD_SAVE_PLAYLIST: - playlist_save(screen, c, NULL, NULL); - return 1; - case CMD_ADD: - handle_add_to_playlist(screen, c); - return 1; - case CMD_SCREEN_UPDATE: - screen->painted = 0; - lw->clear = 1; - lw->repaint = 1; - center_playing_item(screen, c); - return 1; - case CMD_LIST_MOVE_UP: - mpdclient_cmd_move(c, lw->selected, lw->selected-1); - return 1; - case CMD_LIST_MOVE_DOWN: - mpdclient_cmd_move(c, lw->selected, lw->selected+1); - return 1; - case CMD_LIST_FIND: - case CMD_LIST_RFIND: - case CMD_LIST_FIND_NEXT: - case CMD_LIST_RFIND_NEXT: - return screen_find(screen, - lw, c->playlist.length, - cmd, list_callback, (void *) c); - case CMD_MOUSE_EVENT: - return handle_mouse_event(screen,c); - default: - break; - } - return list_window_cmd(lw, c->playlist.length, cmd) ; + switch(cmd) { + case CMD_PLAY: + mpdclient_cmd_play(c, lw->selected); + return 1; + case CMD_DELETE: + mpdclient_cmd_delete(c, lw->selected); + return 1; + case CMD_SAVE_PLAYLIST: + playlist_save(screen, c, NULL, NULL); + return 1; + case CMD_ADD: + handle_add_to_playlist(screen, c); + return 1; + case CMD_SCREEN_UPDATE: + screen->painted = 0; + lw->clear = 1; + lw->repaint = 1; + center_playing_item(screen, c); + return 1; + case CMD_LIST_MOVE_UP: + mpdclient_cmd_move(c, lw->selected, lw->selected-1); + return 1; + case CMD_LIST_MOVE_DOWN: + mpdclient_cmd_move(c, lw->selected, lw->selected+1); + return 1; + case CMD_LIST_FIND: + case CMD_LIST_RFIND: + case CMD_LIST_FIND_NEXT: + case CMD_LIST_RFIND_NEXT: + return screen_find(screen, + lw, c->playlist.length, + cmd, list_callback, (void *) c); + case CMD_MOUSE_EVENT: + return handle_mouse_event(screen,c); + default: + break; + } + return list_window_cmd(lw, c->playlist.length, cmd) ; } - - static list_window_t * play_lw(void) { return lw; } - screen_functions_t * get_screen_playlist(void) { diff --git a/src/screen_search.c b/src/screen_search.c index f163ef7a2..234c91a52 100644 --- a/src/screen_search.c +++ b/src/screen_search.c @@ -45,11 +45,10 @@ extern gint mpdclient_finish_command(mpdclient_t *c); -typedef struct -{ - int id; - char *name; - char *localname; +typedef struct { + int id; + char *name; + char *localname; } search_tag_t; static search_tag_t search_tag[] = { @@ -94,8 +93,8 @@ search_get_tag_id(char *name) #define SEARCH_ARTIST_TITLE 999 typedef struct { - int table; - char *label; + int table; + char *label; } search_type_t; static search_type_t mode[] = { @@ -118,27 +117,27 @@ static gboolean advanced_search_mode = FALSE; static char * lw_search_help_callback(int index, int *highlight, void *data) { - int text_rows; - static char *text[] = { - "Quick - just enter a string and ncmpc will search according", - " to the current search mode (displayed above).", - "", - "Advanced - <tag>:<search term> [<tag>:<search term>...]", - " Example: artist:radiohead album:pablo honey", - "", - " avalible tags: artist, album, title, track,", - " name, genre, date composer, performer, comment, file", - "", - NULL - }; - - text_rows=0; - while( text[text_rows] ) - text_rows++; - - if( index < text_rows ) - return text[index]; - return NULL; + int text_rows; + static char *text[] = { + "Quick - just enter a string and ncmpc will search according", + " to the current search mode (displayed above).", + "", + "Advanced - <tag>:<search term> [<tag>:<search term>...]", + " Example: artist:radiohead album:pablo honey", + "", + " avalible tags: artist, album, title, track,", + " name, genre, date composer, performer, comment, file", + "", + NULL + }; + + text_rows=0; + while (text[text_rows]) + text_rows++; + + if (index < text_rows) + return text[index]; + return NULL; } /* the playlist have been updated -> fix highlights */ @@ -220,116 +219,103 @@ filelist_search(mpdclient_t *c, int exact_match, int table, gchar *pattern) static mpdclient_filelist_t * search_advanced_query(char *query, mpdclient_t *c) { - int i,j; - char **strv; - int table[10]; - char *arg[10]; - mpdclient_filelist_t *filelist = NULL; - - advanced_search_mode = FALSE; - if( g_strrstr(query, ":") == NULL ) - return NULL; - - strv = g_strsplit_set(query, ": ", 0); + int i,j; + char **strv; + int table[10]; + char *arg[10]; + mpdclient_filelist_t *filelist = NULL; + + advanced_search_mode = FALSE; + if( g_strrstr(query, ":") == NULL ) + return NULL; + + strv = g_strsplit_set(query, ": ", 0); + + i=0; + while (strv[i]) { + D("strv[%d] = \"%s\"\n", i, strv[i]); + i++; + } - i=0; - while( strv[i] ) - { - D("strv[%d] = \"%s\"\n", i, strv[i]); - i++; - } + memset(table, 0, 10*sizeof(int)); + memset(arg, 0, 10*sizeof(char *)); + + i=0; + j=0; + while (strv[i] && strlen(strv[i]) > 0 && i < 9) { + D("strv[%d] = \"%s\"\n", i, strv[i]); + + int id = search_get_tag_id(strv[i]); + if (id == -1) { + if (table[j]) { + char *tmp = arg[j]; + arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]); + g_free(tmp); + } else { + D("Bad search tag %s\n", strv[i]); + screen_status_printf(_("Bad search tag %s"), strv[i]); + } + i++; + } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) { + D("No argument for search tag %s\n", strv[i]); + screen_status_printf(_("No argument for search tag %s"), strv[i]); + i++; + // j--; + //table[j] = -1; + } else { + table[j] = id; + arg[j] = locale_to_utf8(strv[i+1]); // FREE ME + j++; + table[j] = -1; + arg[j] = NULL; + i = i + 2; + advanced_search_mode = TRUE; + } + } - memset(table, 0, 10*sizeof(int)); - memset(arg, 0, 10*sizeof(char *)); + g_strfreev(strv); - i=0; - j=0; - while( strv[i] && strlen(strv[i])>0 && i<9 ) - { - D("strv[%d] = \"%s\"\n", i, strv[i]); - int id = search_get_tag_id(strv[i]); - if( id==-1 ) - { - if( table[j] ) - { - char *tmp = arg[j]; - arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]); - g_free(tmp); - } - else - { - D("Bad search tag %s\n", strv[i]); - screen_status_printf(_("Bad search tag %s"), strv[i]); - } - i++; - } - else if( strv[i+1] == NULL || strlen(strv[i+1])==0 ) - { - D("No argument for search tag %s\n", strv[i]); - screen_status_printf(_("No argument for search tag %s"), strv[i]); - i++; - // j--; - //table[j] = -1; - } - else - { - table[j] = id; - arg[j] = locale_to_utf8(strv[i+1]); // FREE ME - j++; - table[j] = -1; - arg[j] = NULL; - i = i + 2; - advanced_search_mode = TRUE; - } - } + if (advanced_search_mode && j > 0) { + /*----------------------------------------------------------------------- + * NOTE (again): This code exists to test a new search ui, + * Its ugly and MUST be redesigned before the next release! + * + the code below should live in mpdclient.c + *----------------------------------------------------------------------- + */ + /** stupid - but this is just a test...... (fulhack) */ + mpd_startSearch(c->connection, FALSE); - g_strfreev(strv); + int iter; + for(iter = 0; iter < 10; iter++) { + mpd_addConstraintSearch(c->connection, table[iter], arg[iter]); + } + mpd_commitSearch(c->connection); - if( advanced_search_mode && j>0 ) - { - /*----------------------------------------------------------------------- - * NOTE (again): This code exists to test a new search ui, - * Its ugly and MUST be redesigned before the next release! - * + the code below should live in mpdclient.c - *----------------------------------------------------------------------- - */ - /** stupid - but this is just a test...... (fulhack) */ - mpd_startSearch(c->connection, FALSE); - - int iter; - for(iter = 0; iter < 10; iter++) - { - mpd_addConstraintSearch(c->connection, table[iter], arg[iter]); - } - - mpd_commitSearch(c->connection); - - filelist = g_malloc0(sizeof(mpdclient_filelist_t)); - - mpd_InfoEntity *entity; - - while( (entity=mpd_getNextInfoEntity(c->connection)) ) - { - filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t)); - - entry->entity = entity; - filelist->list = g_list_append(filelist->list, (gpointer) entry); - filelist->length++; + filelist = g_malloc0(sizeof(mpdclient_filelist_t)); + + mpd_InfoEntity *entity; + + while ((entity=mpd_getNextInfoEntity(c->connection))) { + filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t)); + + entry->entity = entity; + filelist->list = g_list_append(filelist->list, (gpointer) entry); + filelist->length++; + } + + if (mpdclient_finish_command(c) && filelist) + filelist = mpdclient_filelist_free(filelist); + + filelist->updated = TRUE; } - - if( mpdclient_finish_command(c) && filelist ) - filelist = mpdclient_filelist_free(filelist); - filelist->updated = TRUE; - } - - i=0; - while( arg[i] ) - g_free(arg[i++]); + i=0; + while( arg[i] ) + g_free(arg[i++]); - return filelist; + return filelist; } #else #define search_advanced_query(pattern,c) (NULL) @@ -470,7 +456,7 @@ get_title(char *str, size_t size) static list_window_t * get_filelist_window() { - return lw; + return lw; } static int diff --git a/src/screen_utils.c b/src/screen_utils.c index 40f73e63b..a5ccd58f4 100644 --- a/src/screen_utils.c +++ b/src/screen_utils.c @@ -80,8 +80,8 @@ screen_getch(WINDOW *w, char *prompt) } char * -screen_readln(WINDOW *w, - char *prompt, +screen_readln(WINDOW *w, + char *prompt, char *value, GList **history, GCompletion *gcmp) @@ -139,9 +139,9 @@ _screen_auth(mpdclient_t *c, gint recursion) gint screen_auth(mpdclient_t *c) { - _screen_auth(c, 0); - mpdclient_update(c); - curs_set(0); + _screen_auth(c, 0); + mpdclient_update(c); + curs_set(0); } /* query user for a string and find it in a list window */ @@ -153,16 +153,15 @@ screen_find(screen_t *screen, list_window_callback_fn_t callback_fn, void *callback_data) { - int reversed = 0; - int retval = 0; - char *prompt = FIND_PROMPT; - char *value = options.find_show_last_pattern ? (char *) -1 : NULL; + int reversed = 0; + int retval = 0; + char *prompt = FIND_PROMPT; + char *value = options.find_show_last_pattern ? (char *) -1 : NULL; - if( findcmd==CMD_LIST_RFIND ||findcmd==CMD_LIST_RFIND_NEXT ) - { - prompt = RFIND_PROMPT; - reversed = 1; - } + if (findcmd == CMD_LIST_RFIND || findcmd == CMD_LIST_RFIND_NEXT) { + prompt = RFIND_PROMPT; + reversed = 1; + } switch(findcmd) { diff --git a/src/screen_utils.h b/src/screen_utils.h index 8040b105d..a5adf79f4 100644 --- a/src/screen_utils.h +++ b/src/screen_utils.h @@ -12,10 +12,10 @@ char *screen_getstr(WINDOW *w, char *prompt); char *screen_readln(WINDOW *w, char *prompt, char *value, GList **history, GCompletion *gcmp); char *screen_readln_masked(WINDOW *w, char *prompt); -char *screen_read_pasword(WINDOW *w, char *prompt); +char *screen_read_pasword(WINDOW *w, char *prompt); /* query user for a string and find it in a list window */ int screen_find(screen_t *screen, - list_window_t *lw, + list_window_t *lw, int rows, command_t findcmd, list_window_callback_fn_t callback_fn, diff --git a/src/src_lyrics.c b/src/src_lyrics.c index 935fded16..ecacef250 100644 --- a/src/src_lyrics.c +++ b/src/src_lyrics.c @@ -28,72 +28,69 @@ int get_text_line(formed_text *text, int num, char *dest, int len) { - memset(dest, '\0', len*sizeof(char)); - if(num >= text->lines->len-1) return -1; - int linelen; - if(num == 0) - { - linelen = g_array_index(text->lines, int, num); - memcpy(dest, text->text->str, linelen*sizeof(char)); - } - else if(num == 1) - { //dont ask me why, but this is needed.... - linelen = g_array_index(text->lines, int, num) - - g_array_index(text->lines, int, num-1); - memcpy(dest, &text->text->str[g_array_index(text->lines, int, num-1)], - linelen*sizeof(char)); - } - else - { - linelen = g_array_index(text->lines, int, num+1) - - g_array_index(text->lines, int, num); - memcpy(dest, &text->text->str[g_array_index(text->lines, int, num)], - linelen*sizeof(char)); - } + memset(dest, '\0', len*sizeof(char)); + if (num >= text->lines->len - 1) + return -1; + int linelen; + if (num == 0) { + linelen = g_array_index(text->lines, int, num); + memcpy(dest, text->text->str, linelen*sizeof(char)); + } else if (num == 1) { //dont ask me why, but this is needed.... + linelen = g_array_index(text->lines, int, num) + - g_array_index(text->lines, int, num-1); + memcpy(dest, &text->text->str[g_array_index(text->lines, int, num-1)], + linelen*sizeof(char)); + } else { + linelen = g_array_index(text->lines, int, num+1) + - g_array_index(text->lines, int, num); + memcpy(dest, &text->text->str[g_array_index(text->lines, int, num)], + linelen*sizeof(char)); + } + dest[linelen] = '\n'; - dest[linelen+1] = '\0'; - - return 0; + dest[linelen + 1] = '\0'; + + return 0; } - + void add_text_line(formed_text *dest, const char *src, int len) { - // need this because g_array_append_val doesnt work with literals - // and expat sends "\n" as an extra line everytime - if(len == 0) - { - dest->val = strlen(src); - if(dest->lines->len > 0) dest->val += g_array_index(dest->lines, int, - dest->lines->len-1); - g_string_append(dest->text, src); - g_array_append_val(dest->lines, dest->val); - return; - } - if(len > 1 || dest->val == 0) - { - dest->val = len; - if(dest->lines->len > 0) dest->val += g_array_index(dest->lines, int, - dest->lines->len-1); - } - else if (len < 6 && dest->val != 0) dest->val = 0; - - if(dest->val > 0) - { - g_string_append_len(dest->text, src, len); - g_array_append_val(dest->lines, dest->val); - } + // need this because g_array_append_val doesnt work with literals + // and expat sends "\n" as an extra line everytime + if(len == 0) { + dest->val = strlen(src); + if(dest->lines->len > 0) dest->val += g_array_index(dest->lines, int, + dest->lines->len-1); + g_string_append(dest->text, src); + g_array_append_val(dest->lines, dest->val); + return; + } + + if(len > 1 || dest->val == 0) { + dest->val = len; + if(dest->lines->len > 0) dest->val += g_array_index(dest->lines, int, + dest->lines->len-1); + } else if (len < 6 && dest->val != 0) + dest->val = 0; + + if (dest->val > 0) { + g_string_append_len(dest->text, src, len); + g_array_append_val(dest->lines, dest->val); + } } void formed_text_init(formed_text *text) { - if(text->text != NULL) g_string_free(text->text, TRUE); - text->text = g_string_new(""); - - if(text->lines != NULL) g_array_free(text->lines, TRUE); - text->lines = g_array_new(FALSE, TRUE, 4); - - text->val = 0; + if (text->text != NULL) + g_string_free(text->text, TRUE); + text->text = g_string_new(""); + + if (text->lines != NULL) + g_array_free(text->lines, TRUE); + text->lines = g_array_new(FALSE, TRUE, 4); + + text->val = 0; } #ifdef ENABLE_LYRSRC_LEOSLYRICS @@ -108,98 +105,91 @@ int register_lyr_hd (src_lyr *source_descriptor); int src_lyr_stack_init () { - src_lyr_stack = g_array_new (TRUE, FALSE, sizeof (src_lyr*)); + src_lyr_stack = g_array_new (TRUE, FALSE, sizeof (src_lyr*)); #ifdef ENABLE_LYRSRC_HD - src_lyr *src_lyr_hd = malloc (sizeof (src_lyr)); - src_lyr_hd->register_src_lyr = register_lyr_hd; - g_array_append_val (src_lyr_stack, src_lyr_hd); + src_lyr *src_lyr_hd = malloc (sizeof (src_lyr)); + src_lyr_hd->register_src_lyr = register_lyr_hd; + g_array_append_val (src_lyr_stack, src_lyr_hd); #endif -#ifdef ENABLE_LYRSRC_LEOSLYRICS - src_lyr *src_lyr_leoslyrics = malloc (sizeof (src_lyr)); - src_lyr_leoslyrics->register_src_lyr = register_lyr_leoslyrics; - g_array_append_val (src_lyr_stack, src_lyr_leoslyrics); +#ifdef ENABLE_LYRSRC_LEOSLYRICS + src_lyr *src_lyr_leoslyrics = malloc (sizeof (src_lyr)); + src_lyr_leoslyrics->register_src_lyr = register_lyr_leoslyrics; + g_array_append_val (src_lyr_stack, src_lyr_leoslyrics); #endif #ifndef DISABLE_PLUGIN_SYSTEM - src_lyr_plugins_load (); + src_lyr_plugins_load (); #endif - } int src_lyr_init () { - src_lyr_stack_init (); - - int i = 0; - while (g_array_index (src_lyr_stack, src_lyr*, i) != NULL) - { - src_lyr *i_stack; - i_stack = g_array_index (src_lyr_stack, src_lyr*, i); - i_stack->register_src_lyr (i_stack); - i++; - } - return 0; + src_lyr_stack_init (); + + int i = 0; + while (g_array_index (src_lyr_stack, src_lyr*, i) != NULL) { + src_lyr *i_stack; + i_stack = g_array_index (src_lyr_stack, src_lyr*, i); + i_stack->register_src_lyr (i_stack); + i++; + } + return 0; } int get_lyr_by_src (int priority, char *artist, char *title) { - if(src_lyr_stack->len == 0) return -1; - g_array_index (src_lyr_stack, src_lyr*, priority)->get_lyr (artist, title); - return 0; + if(src_lyr_stack->len == 0) return -1; + g_array_index (src_lyr_stack, src_lyr*, priority)->get_lyr (artist, title); + return 0; } int src_lyr_load_plugin_file (const char *file) { - GString *path; - path = g_string_new (PLUGIN_DIR_SYSTEM); - g_string_append (path, "/"); - g_string_append (path, file); - - src_lyr_plugin_register register_func; - src_lyr *new_src = malloc (sizeof (src_lyr)); - new_src->module = g_module_open (path->str, G_MODULE_BIND_LAZY); - if (!g_module_symbol (new_src->module, "register_me", (gpointer*) ®ister_func)) - return -1; - new_src->register_src_lyr = register_func; - g_array_append_val (src_lyr_stack, new_src); - return 0; + GString *path; + path = g_string_new (PLUGIN_DIR_SYSTEM); + g_string_append (path, "/"); + g_string_append (path, file); + + src_lyr_plugin_register register_func; + src_lyr *new_src = malloc (sizeof (src_lyr)); + new_src->module = g_module_open (path->str, G_MODULE_BIND_LAZY); + if (!g_module_symbol (new_src->module, "register_me", (gpointer*) ®ister_func)) + return -1; + new_src->register_src_lyr = register_func; + g_array_append_val (src_lyr_stack, new_src); + return 0; } - + void src_lyr_plugins_load_from_dir (GDir *plugin_dir) { - const gchar *cur_file; - - for (;;) - { - cur_file = g_dir_read_name (plugin_dir); - if (cur_file == NULL) break; - src_lyr_load_plugin_file (cur_file); - } -} - + const gchar *cur_file; + for (;;) { + cur_file = g_dir_read_name (plugin_dir); + if (cur_file == NULL) break; + src_lyr_load_plugin_file (cur_file); + } +} int src_lyr_plugins_load () { - GDir *plugin_dir; - - plugin_dir = g_dir_open (PLUGIN_DIR_SYSTEM, 0, NULL); - if (plugin_dir == NULL) - return -1; - src_lyr_plugins_load_from_dir (plugin_dir); - - GString *user_dir_path; - user_dir_path = g_string_new (g_get_home_dir()); - g_string_append (user_dir_path, PLUGIN_DIR_USER); - - plugin_dir = g_dir_open (user_dir_path->str, 0, NULL); - if (plugin_dir == NULL) - return -1; - src_lyr_plugins_load_from_dir (plugin_dir); - - return 0; -} + GDir *plugin_dir; - + plugin_dir = g_dir_open (PLUGIN_DIR_SYSTEM, 0, NULL); + if (plugin_dir == NULL) + return -1; + src_lyr_plugins_load_from_dir (plugin_dir); + + GString *user_dir_path; + user_dir_path = g_string_new (g_get_home_dir()); + g_string_append (user_dir_path, PLUGIN_DIR_USER); + + plugin_dir = g_dir_open (user_dir_path->str, 0, NULL); + if (plugin_dir == NULL) + return -1; + src_lyr_plugins_load_from_dir (plugin_dir); + + return 0; +} diff --git a/src/strfsong.c b/src/strfsong.c index df622a2bb..27eadb236 100644 --- a/src/strfsong.c +++ b/src/strfsong.c @@ -34,222 +34,192 @@ #include "support.h" #include "strfsong.h" -static gchar * -skip(gchar * p) +static gchar * +skip(gchar * p) { - gint stack = 0; - - while (*p != '\0') { - if(*p == '[') stack++; - if(*p == '#' && p[1] != '\0') { - /* skip escaped stuff */ - ++p; - } - else if(stack) { - if(*p == ']') stack--; - } - else { - if(*p == '&' || *p == '|' || *p == ']') { - break; - } - } - ++p; - } + gint stack = 0; + + while (*p != '\0') { + if(*p == '[') stack++; + if(*p == '#' && p[1] != '\0') { + /* skip escaped stuff */ + ++p; + } else if(stack) { + if(*p == ']') stack--; + } else { + if(*p == '&' || *p == '|' || *p == ']') { + break; + } + } + ++p; + } - return p; + return p; } static gsize -_strfsong(gchar *s, - gsize max, - const gchar *format, - mpd_Song *song, +_strfsong(gchar *s, + gsize max, + const gchar *format, + mpd_Song *song, gchar **last) { - gchar *p, *end; - gchar *temp; - gsize n, length = 0; - gboolean found = FALSE; - - memset(s, 0, max); - if( song==NULL ) - return 0; - - for( p=(gchar *) format; *p != '\0' && length<max; ) - { - /* OR */ - if (p[0] == '|') - { - ++p; - if(!found) - { - memset(s, 0, max); - length = 0; - } - else - { - p = skip(p); - } - continue; - } + gchar *p, *end; + gchar *temp; + gsize n, length = 0; + gboolean found = FALSE; + + memset(s, 0, max); + if( song==NULL ) + return 0; + + for (p = (gchar *)format; *p != '\0' && length<max;) { + /* OR */ + if (p[0] == '|') { + ++p; + if(!found) { + memset(s, 0, max); + length = 0; + } else { + p = skip(p); + } + continue; + } - /* AND */ - if (p[0] == '&') - { - ++p; - if(!found) - { - p = skip(p); - } - else - { - found = FALSE; - } - continue; - } + /* AND */ + if (p[0] == '&') { + ++p; + if(!found) { + p = skip(p); + } else { + found = FALSE; + } + continue; + } - /* EXPRESSION START */ - if (p[0] == '[') - { - temp = g_malloc0(max); - if( _strfsong(temp, max, p+1, song, &p) >0 ) - { - g_strlcat(s, temp, max); - length = strlen(s); - found = TRUE; - } - g_free(temp); - continue; - } + /* EXPRESSION START */ + if (p[0] == '[') { + temp = g_malloc0(max); + if( _strfsong(temp, max, p+1, song, &p) >0 ) { + g_strlcat(s, temp, max); + length = strlen(s); + found = TRUE; + } + g_free(temp); + continue; + } - /* EXPRESSION END */ - if (p[0] == ']') - { - if(last) *last = p+1; - if(!found && length) - { - memset(s, 0, max); - length = 0; - } - return length; - } + /* EXPRESSION END */ + if (p[0] == ']') { + if(last) *last = p+1; + if(!found && length) { + memset(s, 0, max); + length = 0; + } + return length; + } - /* pass-through non-escaped portions of the format string */ - if (p[0] != '#' && p[0] != '%' && length<max) - { - s[length++] = *p; - p++; - continue; - } + /* pass-through non-escaped portions of the format string */ + if (p[0] != '#' && p[0] != '%' && length<max) { + s[length++] = *p; + p++; + continue; + } - /* let the escape character escape itself */ - if (p[0] == '#' && p[1] != '\0' && length<max) - { - s[length++] = *(p+1); - p+=2; - continue; - } + /* let the escape character escape itself */ + if (p[0] == '#' && p[1] != '\0' && length<max) { + s[length++] = *(p+1); + p+=2; + continue; + } - /* advance past the esc character */ + /* advance past the esc character */ - /* find the extent of this format specifier (stop at \0, ' ', or esc) */ - temp = NULL; - end = p+1; - while(*end >= 'a' && *end <= 'z') - { - end++; - } - n = end - p + 1; - if(*end != '%') - n--; - else if (strncmp("%file%", p, n) == 0) - temp = utf8_to_locale(song->file); - else if (strncmp("%artist%", p, n) == 0) - temp = song->artist ? utf8_to_locale(song->artist) : NULL; - else if (strncmp("%title%", p, n) == 0) - temp = song->title ? utf8_to_locale(song->title) : NULL; - else if (strncmp("%album%", p, n) == 0) - temp = song->album ? utf8_to_locale(song->album) : NULL; - else if (strncmp("%shortalbum%", p, n) == 0) - { - temp = song->album ? utf8_to_locale(song->album) : NULL; - if (temp) - { - gchar *temp2 = g_strndup(temp, 25); - if (strlen(temp) > 25) - { - temp2[24] = '.'; - temp2[23] = '.'; - temp2[22] = '.'; + /* find the extent of this format specifier (stop at \0, ' ', or esc) */ + temp = NULL; + end = p+1; + while(*end >= 'a' && *end <= 'z') { + end++; } - g_free(temp); - temp = temp2; - } - } - else if (strncmp("%track%", p, n) == 0) - temp = song->track ? utf8_to_locale(song->track) : NULL; - else if (strncmp("%name%", p, n) == 0) - temp = song->name ? utf8_to_locale(song->name) : NULL; - else if (strncmp("%date%", p, n) == 0) - temp = song->date ? utf8_to_locale(song->date) : NULL; - else if (strncmp("%genre%", p, n) == 0) - temp = song->genre ? utf8_to_locale(song->genre) : NULL; - else if (strncmp("%shortfile%", p, n) == 0) - { - if( strstr(song->file, "://") ) - temp = utf8_to_locale(song->file); - else - temp = utf8_to_locale(basename(song->file)); - } - else if (strncmp("%time%", p, n) == 0) - { - if (song->time != MPD_SONG_NO_TIME) - { - if (song->time > 3600) - { - temp = g_strdup_printf("%d:%02d:%02d", - song->time / 3600, - (song->time % 3600) / 60, - song->time % 60); + n = end - p + 1; + if(*end != '%') + n--; + else if (strncmp("%file%", p, n) == 0) + temp = utf8_to_locale(song->file); + else if (strncmp("%artist%", p, n) == 0) + temp = song->artist ? utf8_to_locale(song->artist) : NULL; + else if (strncmp("%title%", p, n) == 0) + temp = song->title ? utf8_to_locale(song->title) : NULL; + else if (strncmp("%album%", p, n) == 0) + temp = song->album ? utf8_to_locale(song->album) : NULL; + else if (strncmp("%shortalbum%", p, n) == 0) { + temp = song->album ? utf8_to_locale(song->album) : NULL; + if (temp) { + gchar *temp2 = g_strndup(temp, 25); + if (strlen(temp) > 25) { + temp2[24] = '.'; + temp2[23] = '.'; + temp2[22] = '.'; + } + g_free(temp); + temp = temp2; + } } - else - { - temp = g_strdup_printf("%d:%02d", - song->time / 60, - song->time % 60); + else if (strncmp("%track%", p, n) == 0) + temp = song->track ? utf8_to_locale(song->track) : NULL; + else if (strncmp("%name%", p, n) == 0) + temp = song->name ? utf8_to_locale(song->name) : NULL; + else if (strncmp("%date%", p, n) == 0) + temp = song->date ? utf8_to_locale(song->date) : NULL; + else if (strncmp("%genre%", p, n) == 0) + temp = song->genre ? utf8_to_locale(song->genre) : NULL; + else if (strncmp("%shortfile%", p, n) == 0) { + if( strstr(song->file, "://") ) + temp = utf8_to_locale(song->file); + else + temp = utf8_to_locale(basename(song->file)); + } else if (strncmp("%time%", p, n) == 0) { + if (song->time != MPD_SONG_NO_TIME) { + if (song->time > 3600) { + temp = g_strdup_printf("%d:%02d:%02d", + song->time / 3600, + (song->time % 3600) / 60, + song->time % 60); + } else { + temp = g_strdup_printf("%d:%02d", + song->time / 60, + song->time % 60); + } + } } - } - } - - if( temp == NULL) - { - gsize templen=n; - /* just pass-through any unknown specifiers (including esc) */ - /* drop a null char in so printf stops at the end of this specifier, - but put the real character back in (pseudo-const) */ - if( length+templen > max ) - templen = max-length; - g_strlcat(s, p,max); - length+=templen; - } - else { - gsize templen = strlen(temp); - found = TRUE; - if( length+templen > max ) - templen = max-length; - g_strlcat(s, temp, max); - length+=templen; - g_free(temp); - } + if( temp == NULL) { + gsize templen=n; + /* just pass-through any unknown specifiers (including esc) */ + /* drop a null char in so printf stops at the end of this specifier, + but put the real character back in (pseudo-const) */ + if( length+templen > max ) + templen = max-length; + g_strlcat(s, p,max); + length+=templen; + } else { + gsize templen = strlen(temp); + + found = TRUE; + if( length+templen > max ) + templen = max-length; + g_strlcat(s, temp, max); + length+=templen; + g_free(temp); + } - /* advance past the specifier */ - p += n; - } + /* advance past the specifier */ + p += n; + } - if(last) *last = p; + if(last) *last = p; - return length; + return length; } gsize diff --git a/src/support.c b/src/support.c index 847178d9c..2f3e4fd5d 100644 --- a/src/support.c +++ b/src/support.c @@ -110,50 +110,44 @@ strcasestr(const char *haystack, const char *needle) char * strscroll(char *str, char *separator, int width, scroll_state_t *st) { - gchar *tmp, *buf; - gsize len, size; - - if( st->offset==0 ) - { - st->offset++; - return g_strdup(str); - } - - /* create a buffer containing the string and the separator */ - size = strlen(str)+strlen(separator)+1; - tmp = g_malloc(size); - g_strlcpy(tmp, str, size); - g_strlcat(tmp, separator, size); - len = my_strlen(tmp); - - if( st->offset >= len ) - st->offset = 0; - - /* create the new scrolled string */ - size = width+1; - if (g_utf8_validate(tmp, -1, NULL) ) - { - int ulen; - buf = g_malloc(size*6);// max length of utf8 char is 6 - g_utf8_strncpy(buf, g_utf8_offset_to_pointer(tmp,st->offset), size); - if( (ulen = g_utf8_strlen(buf, -1)) < width ) - g_utf8_strncpy(buf+strlen(buf), tmp, size - ulen - 1); - } - else - { - buf = g_malloc(size); - g_strlcpy(buf, tmp+st->offset, size); - if( strlen(buf) < width ) - g_strlcat(buf, tmp, size); - } - if( time(NULL)-st->t >= 1 ) - { - st->t = time(NULL); - st->offset++; - } - g_free(tmp); - return buf; - + gchar *tmp, *buf; + gsize len, size; + + if( st->offset==0 ) { + st->offset++; + return g_strdup(str); + } + + /* create a buffer containing the string and the separator */ + size = strlen(str)+strlen(separator)+1; + tmp = g_malloc(size); + g_strlcpy(tmp, str, size); + g_strlcat(tmp, separator, size); + len = my_strlen(tmp); + + if( st->offset >= len ) + st->offset = 0; + + /* create the new scrolled string */ + size = width+1; + if (g_utf8_validate(tmp, -1, NULL) ) { + int ulen; + buf = g_malloc(size*6);// max length of utf8 char is 6 + g_utf8_strncpy(buf, g_utf8_offset_to_pointer(tmp,st->offset), size); + if( (ulen = g_utf8_strlen(buf, -1)) < width ) + g_utf8_strncpy(buf+strlen(buf), tmp, size - ulen - 1); + } else { + buf = g_malloc(size); + g_strlcpy(buf, tmp+st->offset, size); + if (strlen(buf) < width) + g_strlcat(buf, tmp, size); + } + if( time(NULL)-st->t >= 1 ) { + st->t = time(NULL); + st->offset++; + } + g_free(tmp); + return buf; } void diff --git a/src/support.h b/src/support.h index 8b4a28c18..089a6863a 100644 --- a/src/support.h +++ b/src/support.h @@ -15,11 +15,9 @@ char *remove_trailing_slash(char *path); char *lowerstr(char *str); char *strcasestr(const char *haystack, const char *needle); -typedef struct -{ - int offset; - GTime t; /* GTime is equivalent to time_t */ - +typedef struct { + int offset; + GTime t; /* GTime is equivalent to time_t */ } scroll_state_t; char *strscroll(char *str, char *separator, int width, scroll_state_t *st); diff --git a/src/utils.c b/src/utils.c index 404484ef4..014136480 100644 --- a/src/utils.c +++ b/src/utils.c @@ -50,33 +50,30 @@ string_list_free(GList *string_list) GList * string_list_find(GList *string_list, gchar *str) { - GList *list = g_list_first(string_list); + GList *list = g_list_first(string_list); - while(list) - { - if( strcmp(str, (gchar *) list->data) == 0 ) - return list; - list = list->next; - } - return NULL; + while(list) { + if( strcmp(str, (gchar *) list->data) == 0 ) + return list; + list = list->next; + } + return NULL; } GList * string_list_remove(GList *string_list, gchar *str) { - GList *list = g_list_first(string_list); + GList *list = g_list_first(string_list); - while(list) - { - if( strcmp(str, (gchar *) list->data) == 0 ) - { - g_free(list->data); - list->data = NULL; - return g_list_delete_link(string_list, list); + while(list) { + if( strcmp(str, (gchar *) list->data) == 0 ) { + g_free(list->data); + list->data = NULL; + return g_list_delete_link(string_list, list); + } + list = list->next; } - list = list->next; - } - return list; + return list; } /* create a list suiteble for GCompletion from path */ diff --git a/src/utils.h b/src/utils.h index 4446d1cd7..a7f81bf1a 100644 --- a/src/utils.h +++ b/src/utils.h @@ -14,8 +14,8 @@ GList *string_list_remove(GList *string_list, gchar *str); #define GCMP_TYPE_RFILE (GCMP_TYPE_DIR | GCMP_TYPE_FILE) #define GCMP_TYPE_RPLAYLIST (GCMP_TYPE_DIR | GCMP_TYPE_PLAYLIST) -GList *gcmp_list_from_path(mpdclient_t *c, - gchar *path, +GList *gcmp_list_from_path(mpdclient_t *c, + gchar *path, GList *list, gint types); diff --git a/src/wreadln.c b/src/wreadln.c index f4a7ec3e2..2ae03f83f 100644 --- a/src/wreadln.c +++ b/src/wreadln.c @@ -66,12 +66,11 @@ static inline void cursor_move_right(gint *cursor, gint x1, gchar *line) { - if( *cursor < strlen(line) && *cursor<wrln_max_line_size-1 ) - { - (*cursor)++; - if( *cursor+x0 >= x1 && *start<*cursor-width+1) - (*start)++; - } + if (*cursor < strlen(line) && *cursor < wrln_max_line_size - 1) { + (*cursor)++; + if (*cursor + x0 >= x1 && *start < *cursor - width + 1) + (*start)++; + } } /* move the cursor one step to the left */ @@ -125,276 +124,253 @@ static inline void drawline(gint cursor, /* libcurses version */ gchar * -_wreadln(WINDOW *w, - gchar *prompt, - gchar *initial_value, - gint x1, - GList **history, - GCompletion *gcmp, - gboolean masked) +_wreadln(WINDOW *w, + gchar *prompt, + gchar *initial_value, + gint x1, + GList **history, + GCompletion *gcmp, + gboolean masked) { - GList *hlist = NULL, *hcurrent = NULL; - gchar *line; - gint x0, y, width; - gint cursor = 0, start = 0; - gint key = 0, i; - - /* allocate a line buffer */ - line = g_malloc0(wrln_max_line_size); - /* turn off echo */ - noecho(); - /* make shure the cursor is visible */ - curs_set(1); - /* print prompt string */ - if( prompt ) - waddstr(w, prompt); - /* retrive y and x0 position */ - getyx(w, y, x0); - /* check the x1 value */ - if( x1<=x0 || x1>COLS ) - x1 = COLS; - width = x1-x0; - /* clear input area */ - mvwhline(w, y, x0, ' ', width); - - if( history ) - { - /* append the a new line to our history list */ - *history = g_list_append(*history, g_malloc0(wrln_max_line_size)); - /* hlist points to the current item in the history list */ - hlist = g_list_last(*history); - hcurrent = hlist; - } + GList *hlist = NULL, *hcurrent = NULL; + gchar *line; + gint x0, y, width; + gint cursor = 0, start = 0; + gint key = 0, i; + + /* allocate a line buffer */ + line = g_malloc0(wrln_max_line_size); + /* turn off echo */ + noecho(); + /* make shure the cursor is visible */ + curs_set(1); + /* print prompt string */ + if( prompt ) + waddstr(w, prompt); + /* retrive y and x0 position */ + getyx(w, y, x0); + /* check the x1 value */ + if( x1<=x0 || x1>COLS ) + x1 = COLS; + width = x1-x0; + /* clear input area */ + mvwhline(w, y, x0, ' ', width); + + if( history ) { + /* append the a new line to our history list */ + *history = g_list_append(*history, g_malloc0(wrln_max_line_size)); + /* hlist points to the current item in the history list */ + hlist = g_list_last(*history); + hcurrent = hlist; + } - if( initial_value == (char *) -1 ) - { - /* get previous history entry */ - if( history && hlist->prev ) - { - if( hlist==hcurrent ) - { - /* save the current line */ - g_strlcpy(hlist->data, line, wrln_max_line_size); - } - /* get previous line */ - hlist = hlist->prev; - g_strlcpy(line, hlist->data, wrln_max_line_size); + if( initial_value == (char *) -1 ) { + /* get previous history entry */ + if( history && hlist->prev ) + { + if( hlist==hcurrent ) + { + /* save the current line */ + g_strlcpy(hlist->data, line, wrln_max_line_size); + } + /* get previous line */ + hlist = hlist->prev; + g_strlcpy(line, hlist->data, wrln_max_line_size); + } + cursor_move_to_eol(&cursor, &start, width, x0, x1, line); + drawline(cursor, start, width, x0, y, masked, line, w); + } else if( initial_value ) { + /* copy the initial value to the line buffer */ + g_strlcpy(line, initial_value, wrln_max_line_size); + cursor_move_to_eol(&cursor, &start, width, x0, x1, line); + drawline(cursor, start, width, x0, y, masked, line, w); } - cursor_move_to_eol(&cursor, &start, width, x0, x1, line); - drawline(cursor, start, width, x0, y, masked, line, w); - } - else if( initial_value ) - { - /* copy the initial value to the line buffer */ - g_strlcpy(line, initial_value, wrln_max_line_size); - cursor_move_to_eol(&cursor, &start, width, x0, x1, line); - drawline(cursor, start, width, x0, y, masked, line, w); - } - while( key!=13 && key!='\n' ) - { - if( wrln_wgetch ) - key = wrln_wgetch(w); - else - key = wgetch(w); + while( key!=13 && key!='\n' ) { + if( wrln_wgetch ) + key = wrln_wgetch(w); + else + key = wgetch(w); - /* check if key is a function key */ - for(i=0; i<63; i++) - if( key==KEY_F(i) ) - { - key=KEY_F(1); - i=64; - } + /* check if key is a function key */ + for(i=0; i<63; i++) + if( key==KEY_F(i) ) { + key=KEY_F(1); + i=64; + } - switch (key) - { + switch (key) { #ifdef HAVE_GETMOUSE - case KEY_MOUSE: /* ignore mouse events */ + case KEY_MOUSE: /* ignore mouse events */ #endif - case ERR: /* ingnore errors */ - break; - - case KEY_RESIZE: - /* a resize event */ - if( x1>COLS ) - { - x1=COLS; - width = x1-x0; - cursor_move_to_eol(&cursor, &start, width, x0, x1, line); - } - /* make shure the cursor is visible */ - curs_set(1); - break; - - case TAB: - if( gcmp ) - { - char *prefix = NULL; - GList *list; - - if(wrln_pre_completion_callback) - wrln_pre_completion_callback(gcmp, line, - wrln_completion_callback_data); - list = g_completion_complete(gcmp, line, &prefix); - if( prefix ) - { - g_strlcpy(line, prefix, wrln_max_line_size); - cursor_move_to_eol(&cursor, &start, width, x0, x1, line); - g_free(prefix); + case ERR: /* ingnore errors */ + break; + + case KEY_RESIZE: + /* a resize event */ + if( x1>COLS ) { + x1=COLS; + width = x1-x0; + cursor_move_to_eol(&cursor, &start, width, x0, x1, line); + } + /* make shure the cursor is visible */ + curs_set(1); + break; + + case TAB: + if( gcmp ) { + char *prefix = NULL; + GList *list; + + if(wrln_pre_completion_callback) + wrln_pre_completion_callback(gcmp, line, + wrln_completion_callback_data); + list = g_completion_complete(gcmp, line, &prefix); + if( prefix ) { + g_strlcpy(line, prefix, wrln_max_line_size); + cursor_move_to_eol(&cursor, &start, width, x0, x1, line); + g_free(prefix); + } + else + screen_bell(); + if( wrln_post_completion_callback ) + wrln_post_completion_callback(gcmp, line, list, + wrln_completion_callback_data); + } + break; + + case KEY_CTRL_G: + screen_bell(); + g_free(line); + if( history ) { + g_free(hcurrent->data); + hcurrent->data = NULL; + *history = g_list_delete_link(*history, hcurrent); + } + return NULL; + + case KEY_LEFT: + cursor_move_left(&cursor, &start); + break; + case KEY_RIGHT: + cursor_move_right(&cursor, &start, width, x0, x1, line); + break; + case KEY_HOME: + case KEY_CTRL_A: + cursor = 0; + start = 0; + break; + case KEY_END: + case KEY_CTRL_E: + cursor_move_to_eol(&cursor, &start, width, x0, x1, line); + break; + case KEY_CTRL_K: + line[cursor] = 0; + break; + case KEY_CTRL_U: + cursor = my_strlen(line); + for (i = 0;i < cursor; i++) + line[i] = '\0'; + cursor = 0; + break; + case 127: + case KEY_BCKSPC: /* handle backspace: copy all */ + case KEY_BACKSPACE: /* chars starting from curpos */ + if( cursor > 0 ) {/* - 1 from buf[n+1] to buf */ + for (i = cursor - 1; line[i] != 0; i++) + line[i] = line[i + 1]; + cursor_move_left(&cursor, &start); + } + break; + case KEY_DC: /* handle delete key. As above */ + case KEY_CTRL_D: + if (cursor <= my_strlen(line) - 1) { + for (i = cursor; line[i] != 0; i++) + line[i] = line[i + 1]; + } + break; + case KEY_UP: + /* get previous history entry */ + if( history && hlist->prev ) { + if( hlist==hcurrent ) + { + /* save the current line */ + g_strlcpy(hlist->data, line, wrln_max_line_size); + } + /* get previous line */ + hlist = hlist->prev; + g_strlcpy(line, hlist->data, wrln_max_line_size); + } + cursor_move_to_eol(&cursor, &start, width, x0, x1, line); + break; + case KEY_DOWN: + /* get next history entry */ + if( history && hlist->next ) { + /* get next line */ + hlist = hlist->next; + g_strlcpy(line, hlist->data, wrln_max_line_size); + } + cursor_move_to_eol(&cursor, &start, width, x0, x1, line); + break; + + case '\n': + case 13: + case KEY_IC: + case KEY_PPAGE: + case KEY_NPAGE: + case KEY_F(1): + /* ignore char */ + break; + default: + if (key >= 32) { + if (strlen (line + cursor)) { /* if the cursor is */ + /* not at the last pos */ + gchar *tmp = 0; + gsize size = strlen(line + cursor) + 1; + + tmp = g_malloc0(size); + g_strlcpy (tmp, line + cursor, size); + line[cursor] = key; + line[cursor + 1] = 0; + g_strlcat (&line[cursor + 1], tmp, size); + g_free(tmp); + cursor_move_right(&cursor, &start, width, x0, x1, line); + } else { + line[cursor + 1] = 0; + line[cursor] = key; + cursor_move_right(&cursor, &start, width, x0, x1, line); + } + } } - else - screen_bell(); - if( wrln_post_completion_callback ) - wrln_post_completion_callback(gcmp, line, list, - wrln_completion_callback_data); - } - break; - case KEY_CTRL_G: - screen_bell(); - g_free(line); - if( history ) - { - g_free(hcurrent->data); - hcurrent->data = NULL; - *history = g_list_delete_link(*history, hcurrent); - } - return NULL; - - case KEY_LEFT: - cursor_move_left(&cursor, &start); - break; - case KEY_RIGHT: - cursor_move_right(&cursor, &start, width, x0, x1, line); - break; - case KEY_HOME: - case KEY_CTRL_A: - cursor = 0; - start = 0; - break; - case KEY_END: - case KEY_CTRL_E: - cursor_move_to_eol(&cursor, &start, width, x0, x1, line); - break; - case KEY_CTRL_K: - line[cursor] = 0; - break; - case KEY_CTRL_U: - cursor = my_strlen(line); - for (i = 0;i < cursor; i++) - line[i] = '\0'; - cursor = 0; - break; - case 127: - case KEY_BCKSPC: /* handle backspace: copy all */ - case KEY_BACKSPACE: /* chars starting from curpos */ - if( cursor > 0 ) /* - 1 from buf[n+1] to buf */ - { - for (i = cursor - 1; line[i] != 0; i++) - line[i] = line[i + 1]; - cursor_move_left(&cursor, &start); - } - break; - case KEY_DC: /* handle delete key. As above */ - case KEY_CTRL_D: - if( cursor <= my_strlen(line) - 1 ) - { - for (i = cursor; line[i] != 0; i++) - line[i] = line[i + 1]; - } - break; - case KEY_UP: - /* get previous history entry */ - if( history && hlist->prev ) - { - if( hlist==hcurrent ) - { - /* save the current line */ - g_strlcpy(hlist->data, line, wrln_max_line_size); - } - /* get previous line */ - hlist = hlist->prev; - g_strlcpy(line, hlist->data, wrln_max_line_size); - } - cursor_move_to_eol(&cursor, &start, width, x0, x1, line); - break; - case KEY_DOWN: - /* get next history entry */ - if( history && hlist->next ) - { - /* get next line */ - hlist = hlist->next; - g_strlcpy(line, hlist->data, wrln_max_line_size); - } - cursor_move_to_eol(&cursor, &start, width, x0, x1, line); - break; - - case '\n': - case 13: - case KEY_IC: - case KEY_PPAGE: - case KEY_NPAGE: - case KEY_F(1): - /* ignore char */ - break; - default: - if (key >= 32) - { - if (strlen (line + cursor)) /* if the cursor is */ - { /* not at the last pos */ - gchar *tmp = 0; - gsize size = strlen(line + cursor) + 1; - - tmp = g_malloc0(size); - g_strlcpy (tmp, line + cursor, size); - line[cursor] = key; - line[cursor + 1] = 0; - g_strlcat (&line[cursor + 1], tmp, size); - g_free(tmp); - cursor_move_right(&cursor, &start, width, x0, x1, line); - } - else - { - line[cursor + 1] = 0; - line[cursor] = key; - cursor_move_right(&cursor, &start, width, x0, x1, line); - } - } + drawline(cursor, start, width, x0, y, masked, line, w); } - drawline(cursor, start, width, x0, y, masked, line, w); - } - - /* update history */ - if( history ) - { - if( strlen(line) ) - { - /* update the current history entry */ - size_t size = strlen(line)+1; - hcurrent->data = g_realloc(hcurrent->data, size); - g_strlcpy(hcurrent->data, line, size); - } - else - { - /* the line was empty - remove the current history entry */ - g_free(hcurrent->data); - hcurrent->data = NULL; - *history = g_list_delete_link(*history, hcurrent); - } + /* update history */ + if( history ) { + if( strlen(line) ) { + /* update the current history entry */ + size_t size = strlen(line)+1; + hcurrent->data = g_realloc(hcurrent->data, size); + g_strlcpy(hcurrent->data, line, size); + } else { + /* the line was empty - remove the current history entry */ + g_free(hcurrent->data); + hcurrent->data = NULL; + *history = g_list_delete_link(*history, hcurrent); + } - while( g_list_length(*history) > wrln_max_history_length ) - { - GList *first = g_list_first(*history); + while( g_list_length(*history) > wrln_max_history_length ) { + GList *first = g_list_first(*history); - /* remove the oldest history entry */ - g_free(first->data); - first->data = NULL; - *history = g_list_delete_link(*history, first); + /* remove the oldest history entry */ + g_free(first->data); + first->data = NULL; + *history = g_list_delete_link(*history, first); + } } - } - - return g_realloc(line, strlen(line)+1); + + return g_realloc(line, strlen(line)+1); } #else @@ -500,24 +476,23 @@ static inline void drawline(gint cursor, /* libcursesw version */ gchar * -_wreadln(WINDOW *w, - gchar *prompt, - gchar *initial_value, - gint x1, - GList **history, - GCompletion *gcmp, - gboolean masked) +_wreadln(WINDOW *w, + gchar *prompt, + gchar *initial_value, + gint x1, + GList **history, + GCompletion *gcmp, + gboolean masked) { - GList *hlist = NULL, *hcurrent = NULL; - wchar_t *wline; - gchar *mbline; - gint x0, x, y, width, start; - gint cursor; - wint_t wch; - gint key; - gint i; + GList *hlist = NULL, *hcurrent = NULL; + wchar_t *wline; + gchar *mbline; + gint x0, x, y, width, start; + gint cursor; + wint_t wch; + gint key; + gint i; - /* initialize variables */ start = 0; x = 0; @@ -787,23 +762,23 @@ _wreadln(WINDOW *w, #endif gchar * -wreadln(WINDOW *w, - gchar *prompt, +wreadln(WINDOW *w, + gchar *prompt, gchar *initial_value, - gint x1, - GList **history, + gint x1, + GList **history, GCompletion *gcmp) - { - return _wreadln(w, prompt, initial_value, x1, history, gcmp, FALSE); - } - +{ + return _wreadln(w, prompt, initial_value, x1, history, gcmp, FALSE); +} + gchar * -wreadln_masked(WINDOW *w, - gchar *prompt, - gchar *initial_value, - gint x1, - GList **history, - GCompletion *gcmp) - { - return _wreadln(w, prompt, initial_value, x1, history, gcmp, TRUE); - } +wreadln_masked(WINDOW *w, + gchar *prompt, + gchar *initial_value, + gint x1, + GList **history, + GCompletion *gcmp) +{ + return _wreadln(w, prompt, initial_value, x1, history, gcmp, TRUE); +} |