/* $Id: misc.c,v 1.1.1.1 2000/01/30 16:39:13 benediktroth Exp $ */ #include #include #include #include #include #include #include #include #include "yamt.h" #include "dirbrowser.h" void destroy( GtkWidget *widget, gpointer *data ) { gtk_widget_destroy( GTK_WIDGET (data) ); } void yamtlog( gchar *fmt, ...) { va_list ap; gchar *s; gchar c; /* gint d; */ /* gchar buffer[BUFFERSIZE]=""; */ gchar message[BUFFERSIZE*2]=""; gchar *message2[2][1]; gchar *error=_("[...]"); va_start(ap, fmt); while(*fmt) { switch(*fmt++) { case 's': s = va_arg( ap, char * ); /* if there's not enough space */ if( BUFFERSIZE*2-strlen(message) < strlen(s) ) { if( BUFFERSIZE*2-strlen(message) > strlen(error) ) strcat( message, error ); break; } strcat( message, s ); break; case 'c': c = va_arg( ap, char ); if( BUFFERSIZE*2-strlen(message) < 2 ) { if( BUFFERSIZE*2-strlen(message) > strlen(error) ) strcat( message, error ); break; } strcat( message, c ); break; /* Does not work!!!! ???? */ /* case 'd': */ /* d = va_arg( ap, int ); */ /* g_print("d: %i\n", d); */ /* sprintf( buffer, "%i", d); */ /* strcat( message, buffer ); */ /* break; */ } } va_end(ap); g_print("%s\n", message); message2[0][0]=message; gtk_clist_append( GTK_CLIST(logwidget), message2[0] ); /* correct the scrolling */ gtk_clist_moveto( GTK_CLIST(logwidget), GTK_CLIST(logwidget)->rows-1, 0, 1.0, 0.0 ); } int play_file( gchar *file ) { pid_t pid; gchar buffer[2024]; gchar command[2024]; /* If file is NULL just start the player with no arguments */ if( file != NULL ) { /* If the file is not between '"'s make it so */ if( file[0] == '\"' ) strcpy( buffer, file ); else { strcpy( buffer, "\"" ); strcat( buffer, file ); strcat( buffer, "\"" ); } strcpy( command, prop_mp3_player); strcat( command, " "); strcat( command, buffer); } else strcpy( command, prop_mp3_player); yamtlog("%s %s", "Executing mp3 player with ", command); pid = fork(); switch (pid) { case -1: printf("Fork failed!\n"); break; case 0: system(command); /* Important to use _exit instead of exit ! */ _exit(1); break; default: break; } return(0); } /* Update the value of the progress bar */ /* so that we get some movement */ gint update_progress_bar( void ) { GtkProgress *progress; float val; /* from gtkfaq 5.12 */ while(g_main_iteration(FALSE)); progress = gnome_appbar_get_progress(GNOME_APPBAR(appbar) ); val = gtk_progress_get_value(GTK_PROGRESS(progress)); val += 0.5; if (val > 100) val = 0; /* g_print("var: %f\n", val); */ gtk_progress_set_value(GTK_PROGRESS(progress), val); return(TRUE); } gint menu_popup( GtkWidget *widget, GdkEvent *event) { GdkEventButton *event_button; if (event->type == GDK_BUTTON_PRESS) { event_button = (GdkEventButton *) event; if(event_button->button == 3) { gtk_menu_popup (GTK_MENU (widget), NULL, NULL, NULL, NULL, event_button->button, event_button->time ); return TRUE; } } return FALSE; } void expand_variables_in_string(gchar *s, guint size, gchar *dir, gchar *fname) { if(!strncmp(s,"%dir",size)) strncpy(s,dir,size); else if(!strncmp(s,"%file",size)) strncpy(s,fname,size); } struct id3tag expand_variables(struct id3tag tag, gchar *dir, gchar *fname) { gchar *index; /* find the base part of the name, without the extension */ fname = strdup(g_basename(fname)); index = strrchr(fname,'.'); if( index != NULL ) *index=0; /* find the base part of the directory */ dir = g_basename(dir); expand_variables_in_string(tag.title,30,dir,fname); expand_variables_in_string(tag.artist,30,dir,fname); expand_variables_in_string(tag.album,30,dir,fname); expand_variables_in_string(tag.comment,30,dir,fname); expand_variables_in_string(tag.year,4,dir,fname); free(fname); /* fname has been strduped */ return tag; } int mp3_is_valid( gchar *filename ) { gchar *tmp; struct mpegformat_t header; /* First check for a valid mpeg header, if there is none */ /* check for ".mp3" as the extension */ if( get_header_data(filename, &header) ) return(TRUE); else { tmp = strstr( filename, ".mp3" ); if( !tmp ) return(FALSE); /* If ".mp3" are the last four chars */ if( tmp-filename == strlen(filename)-4 ) return(TRUE); } return(FALSE); } int get_header_data( char *filename, struct mpegformat_t *header ) { char header_buffer[MPEG_HEADER_LENGTH]; struct stat stbuf; int mp3_fd; int filesize; /* check if file exists and get the file size */ if (stat(filename, &stbuf) == -1) { printf("ERROR File not found: %s \n", filename); return(0); } filesize = (long)stbuf.st_size; mp3_fd = open(filename, O_RDONLY); /* Read the header into the buffer */ read(mp3_fd, &header_buffer, MPEG_HEADER_LENGTH); close(mp3_fd); /* Process header */ mpegGetFormat(&header_buffer, header); /* if it seems to be a valid file output its format */ if (header->version != -1) { /* calculate duration of the MP3-song */ header->totalframes=(long)((double)filesize/header->framesize); header->duration=header->totalframes*((float)1152/(float)header->version)/(float)header->samplerate; return(1); } else return(0); }