00001 #include "common.h"
00002 #include <string.h>
00003 #include <sys/stat.h>
00004 #ifdef HAVE_LIBGEN_H
00005 #include <libgen.h>
00006 #endif
00007
00008
00009 #ifndef HAVE_LIBGEN_H
00010 static char *basename(char *in) {
00011 char *p;
00012 if (in == NULL)
00013 return NULL;
00014 p = in + strlen(in) - 1;
00015 while (*p != '\\' && *p != '/' && *p != ':')
00016 { p--; }
00017 return ++p;
00018 }
00019 #endif
00020
00021 static int progress (u_int64_t sent, u_int64_t total, const char* buf, unsigned len, void *data)
00022 {
00023 int percent = (sent*100)/total;
00024 #ifdef __WIN32__
00025 printf("Progress: %I64u of %I64u (%d%%)\r", sent, total, percent);
00026 #else
00027 printf("Progress: %llu of %llu (%d%%)\r", sent, total, percent);
00028 #endif
00029 fflush(stdout);
00030 return 0;
00031 }
00032
00033 static char *prompt (const char *prompt, char *buffer, size_t bufsz, int required)
00034 {
00035 char *cp, *bp;
00036
00037 while (1) {
00038 fprintf(stdout, "%s> ", prompt);
00039 if ( fgets(buffer, bufsz, stdin) == NULL ) {
00040 if (ferror(stdin)) {
00041 perror("fgets");
00042 } else {
00043 fprintf(stderr, "EOF on stdin\n");
00044 }
00045 return NULL;
00046 }
00047
00048 cp= strrchr(buffer, '\n');
00049 if ( cp != NULL ) *cp= '\0';
00050
00051 bp= buffer;
00052 while ( bp != cp ) {
00053 if ( *bp != ' ' && *bp != '\t' ) return bp;
00054 bp++;
00055 }
00056
00057 if (! required) return bp;
00058 }
00059 }
00060
00061 static void usage (void)
00062 {
00063 fprintf(stderr, "usage: sendtr [ -D debuglvl ] <path>\n");
00064 exit(1);
00065 }
00066
00067
00068 int main(int argc, char **argv)
00069 {
00070 njb_t njbs[NJB_MAX_DEVICES], *njb;
00071 int n, opt, debug;
00072 extern int optind;
00073 extern char *optarg;
00074 char *path, *filename;
00075 char artist[80], title[80], genre[80], codec[80], album[80];
00076 char num[80];
00077 char *partist, *ptitle, *pgenre, *pcodec, *palbum, *pnum;
00078 u_int16_t tracknum, length, year;
00079 u_int32_t filesize;
00080 struct stat sb;
00081 u_int32_t trackid;
00082 char *lang;
00083 njb_songid_t *songid;
00084 njb_songid_frame_t *frame;
00085
00086 debug= 0;
00087 while ( (opt= getopt(argc, argv, "D:")) != -1 ) {
00088 switch (opt) {
00089 case 'D':
00090 debug= atoi(optarg);
00091 break;
00092 default:
00093 usage();
00094 }
00095 }
00096 argc-= optind;
00097 argv+= optind;
00098
00099 if ( argc != 1 ) usage();
00100 if ( debug ) NJB_Set_Debug(debug);
00101
00102
00103
00104
00105
00106
00107
00108 lang = getenv("LANG");
00109 if (lang != NULL) {
00110 if (strlen(lang) > 5) {
00111 if (!strcmp(&lang[strlen(lang)-5], "UTF-8")) {
00112 NJB_Set_Unicode(NJB_UC_UTF8);
00113 }
00114 }
00115 }
00116
00117 path = argv[0];
00118
00119 filename = basename(path);
00120 if ( stat(path, &sb) == -1 ) {
00121 fprintf(stderr, "%s: ", path);
00122 perror("stat");
00123 return 1;
00124 }
00125 filesize = (u_int32_t) sb.st_size;
00126
00127 if ( (pcodec = prompt("Codec", codec, 80, 1)) == NULL ) return 1;
00128
00129 if ( (ptitle = prompt("Title", title, 80, 0)) == NULL ) return 1;
00130 if ( ! strlen(ptitle) ) ptitle= NULL;
00131
00132 if ( (palbum = prompt("Album", album, 80, 0)) == NULL ) return 1;
00133 if ( ! strlen(palbum) ) palbum= NULL;
00134
00135 if ( (partist = prompt("Artist", artist, 80, 0)) == NULL ) return 1;
00136 if ( ! strlen(partist) ) partist= NULL;
00137
00138 if ( (pgenre = prompt("Genre", genre, 80, 0)) == NULL ) return 1;
00139 if ( ! strlen(pgenre) ) pgenre= NULL;
00140
00141 if ( (pnum = prompt("Track number", num, 80, 0)) == NULL ) return 1;
00142 if ( strlen(pnum) ) {
00143 tracknum = strtoul(pnum, 0, 10);
00144 } else {
00145 tracknum = 0;
00146 }
00147
00148 if ( (pnum = prompt("Year", num, 80, 0)) == NULL ) return 1;
00149 if ( strlen(pnum) ) {
00150 year = strtoul(pnum, 0, 10);
00151 } else {
00152 year = 0;
00153 }
00154
00155 if ( (pnum = prompt("Length", num, 80, 0)) == NULL ) return 1;
00156 if ( strlen(pnum) ) {
00157 length = strtoul(pnum, 0, 10);
00158 } else {
00159 length = 0;
00160 }
00161
00162 printf("Sending track:\n");
00163 printf("Codec: %s\n", pcodec);
00164 if (ptitle) printf("Title: %s\n", ptitle);
00165 if (palbum) printf("Album: %s\n", palbum);
00166 if (partist) printf("Artist: %s\n", partist);
00167 if (pgenre) printf("Genre: %s\n", pgenre);
00168 if (year > 0) printf("Year: %d\n", year);
00169 if (tracknum) printf("Track no: %d\n", tracknum);
00170 if (length) printf("Length: %d\n", length);
00171
00172 if (NJB_Discover(njbs, 0, &n) == -1) {
00173 fprintf(stderr, "could not locate any jukeboxes\n");
00174 return 1;
00175 }
00176
00177 if ( n == 0 ) {
00178 fprintf(stderr, "no NJB devices found\n");
00179 return 0;
00180 }
00181
00182 njb= njbs;
00183
00184 if ( NJB_Open(njb) == -1 ) {
00185 NJB_Error_Dump(njb,stderr);
00186 return 1;
00187 }
00188
00189 NJB_Capture(njb);
00190
00191 songid = NJB_Songid_New();
00192 frame = NJB_Songid_Frame_New_Codec(pcodec);
00193 NJB_Songid_Addframe(songid, frame);
00194 frame = NJB_Songid_Frame_New_Filesize(filesize);
00195 NJB_Songid_Addframe(songid, frame);
00196 frame = NJB_Songid_Frame_New_Title(ptitle);
00197 NJB_Songid_Addframe(songid, frame);
00198 frame = NJB_Songid_Frame_New_Album(palbum);
00199 NJB_Songid_Addframe(songid, frame);
00200 frame = NJB_Songid_Frame_New_Artist(partist);
00201 NJB_Songid_Addframe(songid, frame);
00202 frame = NJB_Songid_Frame_New_Genre(pgenre);
00203 NJB_Songid_Addframe(songid, frame);
00204 frame = NJB_Songid_Frame_New_Year(year);
00205 NJB_Songid_Addframe(songid, frame);
00206 frame = NJB_Songid_Frame_New_Tracknum(tracknum);
00207 NJB_Songid_Addframe(songid, frame);
00208 frame = NJB_Songid_Frame_New_Length(length);
00209 NJB_Songid_Addframe(songid, frame);
00210 frame = NJB_Songid_Frame_New_Filename(filename);
00211 NJB_Songid_Addframe(songid, frame);
00212
00213 if ( NJB_Send_Track(njb, path, songid, progress, NULL, &trackid) == -1 ) {
00214 printf("\n");
00215 NJB_Error_Dump(njb,stderr);
00216 } else {
00217 printf("\nNJB track ID: %u\n", trackid);
00218 }
00219 printf("\n");
00220
00221 NJB_Songid_Destroy(songid);
00222
00223 NJB_Release(njb);
00224 NJB_Close(njb);
00225
00226 return 0;
00227 }
00228