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
00010
00011
00012
00013
00014
00015 #ifndef HAVE_LIBGEN_H
00016 static char *basename(char *in) {
00017 char *p;
00018 if (in == NULL)
00019 return NULL;
00020 p = in + strlen(in) - 1;
00021 while (*p != '\\' && *p != '/' && *p != ':')
00022 { p--; }
00023 return ++p;
00024 }
00025 #endif
00026
00027 static int progress (u_int64_t sent, u_int64_t total, const char* buf, unsigned len, void *data)
00028 {
00029 int percent = (sent*100)/total;
00030 #ifdef __WIN32__
00031 printf("Progress: %I64u of %I64u (%d%%)\r", sent, total, percent);
00032 #else
00033 printf("Progress: %llu of %llu (%d%%)\r", sent, total, percent);
00034 #endif
00035 fflush(stdout);
00036 return 0;
00037 }
00038
00039 static char *prompt (const char *prompt, char *buffer, size_t bufsz, int required)
00040 {
00041 char *cp, *bp;
00042
00043 while (1) {
00044 fprintf(stdout, "%s> ", prompt);
00045 if ( fgets(buffer, bufsz, stdin) == NULL ) {
00046 if (ferror(stdin)) {
00047 perror("fgets");
00048 } else {
00049 fprintf(stderr, "EOF on stdin\n");
00050 }
00051 return NULL;
00052 }
00053
00054 cp = strrchr(buffer, '\n');
00055 if ( cp != NULL ) *cp = '\0';
00056
00057 bp = buffer;
00058 while ( bp != cp ) {
00059 if ( *bp != ' ' && *bp != '\t' ) return bp;
00060 bp++;
00061 }
00062
00063 if (! required) return bp;
00064 }
00065 }
00066
00067 static void usage(void)
00068 {
00069 fprintf(stderr, "usage: sendtr [ -D debuglvl ] [ -q ] -t <title> -a <artist> -l <album> -c <codec>\n");
00070 fprintf(stderr, " -g <genre> -n <track number> -d <duration in seconds> <path>\n");
00071 fprintf(stderr, "(-q means the program will not ask for missing information.)\n");
00072 exit(1);
00073 }
00074
00075 int main(int argc, char **argv)
00076 {
00077 njb_t njbs[NJB_MAX_DEVICES], *njb;
00078 int n, opt, debug;
00079 extern int optind;
00080 extern char *optarg;
00081 char *path, *filename;
00082 char artist[80], title[80], genre[80], codec[80], album[80];
00083 char num[80];
00084 char *partist = NULL;
00085 char *ptitle = NULL;
00086 char *pgenre = NULL;
00087 char *pcodec = NULL;
00088 char *palbum = NULL;
00089 u_int16_t tracknum = 0;
00090 u_int16_t length = 0;
00091 u_int16_t year = 0;
00092 u_int16_t quiet = 0;
00093 u_int32_t filesize;
00094 struct stat sb;
00095 u_int32_t trackid;
00096 char *lang;
00097 njb_songid_t *songid;
00098 njb_songid_frame_t *frame;
00099
00100 debug = 0;
00101
00102
00103 while ( (opt = getopt(argc, argv, "qD:t:a:l:c:g:n:d:")) != -1 ) {
00104 switch (opt) {
00105 case 'D':
00106 debug = atoi(optarg);
00107 break;
00108 case 't':
00109 ptitle = strdup(optarg);
00110 break;
00111 case 'a':
00112 partist = strdup(optarg);
00113 break;
00114 case 'l':
00115 palbum = strdup(optarg);
00116 break;
00117 case 'c':
00118 pcodec = strdup(optarg);
00119 break;
00120 case 'g':
00121 pgenre = strdup(optarg);
00122 break;
00123 case 'n':
00124 tracknum = atoi(optarg);
00125 break;
00126 case 'd':
00127 length = atoi(optarg);
00128 break;
00129 case 'q':
00130 quiet = 1;
00131 break;
00132 default:
00133 usage();
00134 }
00135 }
00136 argc-= optind;
00137 argv+= optind;
00138
00139 if ( argc != 1 ) {
00140 printf("You need to pass a filename.\n");
00141 usage();
00142 }
00143 if ( debug ) {
00144 NJB_Set_Debug(debug);
00145 }
00146
00147
00148
00149
00150
00151
00152
00153
00154 lang = getenv("LANG");
00155 if (lang != NULL) {
00156 if (strlen(lang) > 5) {
00157 if (!strcmp(&lang[strlen(lang)-5], "UTF-8")) {
00158 NJB_Set_Unicode(NJB_UC_UTF8);
00159 }
00160 }
00161 }
00162
00163 path = argv[0];
00164
00165 filename = basename(path);
00166 if ( stat(path, &sb) == -1 ) {
00167 fprintf(stderr, "%s: ", path);
00168 perror("stat");
00169 return 1;
00170 }
00171 filesize = (u_int32_t) sb.st_size;
00172
00173
00174 if (!quiet) {
00175 if (pcodec == NULL) {
00176 if ( (pcodec = prompt("Codec", codec, 80, 1)) == NULL ) {
00177 printf("A codec name is required.\n");
00178 usage();
00179 }
00180 }
00181 if (!strlen(pcodec)) {
00182 printf("A codec name is required.\n");
00183 usage();
00184 }
00185
00186 if (ptitle == NULL) {
00187 if ( (ptitle = prompt("Title", title, 80, 0)) == NULL )
00188 usage();
00189 }
00190 if (!strlen(ptitle))
00191 ptitle = NULL;
00192
00193
00194 if (palbum == NULL) {
00195 if ( (palbum = prompt("Album", album, 80, 0)) == NULL )
00196 usage();
00197 }
00198 if (!strlen(palbum))
00199 palbum = NULL;
00200
00201 if (partist == NULL) {
00202 if ( (partist = prompt("Artist", artist, 80, 0)) == NULL )
00203 usage();
00204 }
00205 if (!strlen(partist))
00206 partist = NULL;
00207
00208 if (pgenre == NULL) {
00209 if ( (pgenre = prompt("Genre", genre, 80, 0)) == NULL )
00210 usage();
00211 }
00212 if (!strlen(pgenre))
00213 pgenre = NULL;
00214
00215 if (tracknum == 0) {
00216 char *pnum;
00217 if ( (pnum = prompt("Track number", num, 80, 0)) == NULL )
00218 tracknum = 0;
00219 if ( strlen(pnum) ) {
00220 tracknum = strtoul(pnum, 0, 10);
00221 } else {
00222 tracknum = 0;
00223 }
00224 }
00225
00226 if (year == 0) {
00227 char *pnum;
00228 if ( (pnum = prompt("Year", num, 80, 0)) == NULL )
00229 year = 0;
00230 if ( strlen(pnum) ) {
00231 year = strtoul(pnum, 0, 10);
00232 } else {
00233 year = 0;
00234 }
00235 }
00236
00237 if (length == 0) {
00238 char *pnum;
00239 if ( (pnum = prompt("Length", num, 80, 0)) == NULL )
00240 length = 0;
00241 if ( strlen(pnum) ) {
00242 length = strtoul(pnum, 0, 10);
00243 } else {
00244 length = 0;
00245 }
00246 }
00247 }
00248
00249 printf("Sending track:\n");
00250 printf("Codec: %s\n", pcodec);
00251 if (ptitle) printf("Title: %s\n", ptitle);
00252 if (palbum) printf("Album: %s\n", palbum);
00253 if (partist) printf("Artist: %s\n", partist);
00254 if (pgenre) printf("Genre: %s\n", pgenre);
00255 if (year > 0) printf("Year: %d\n", year);
00256 if (tracknum > 0) printf("Track no: %d\n", tracknum);
00257 if (length > 0) printf("Length: %d\n", length);
00258
00259 if (NJB_Discover(njbs, 0, &n) == -1) {
00260 fprintf(stderr, "could not locate any jukeboxes\n");
00261 return 1;
00262 }
00263
00264 if ( n == 0 ) {
00265 fprintf(stderr, "no NJB devices found\n");
00266 return 0;
00267 }
00268
00269 njb = njbs;
00270
00271 if ( NJB_Open(njb) == -1 ) {
00272 NJB_Error_Dump(njb,stderr);
00273 return 1;
00274 }
00275
00276 NJB_Capture(njb);
00277
00278 songid = NJB_Songid_New();
00279 frame = NJB_Songid_Frame_New_Codec(pcodec);
00280 NJB_Songid_Addframe(songid, frame);
00281 frame = NJB_Songid_Frame_New_Filesize(filesize);
00282 NJB_Songid_Addframe(songid, frame);
00283 frame = NJB_Songid_Frame_New_Title(ptitle);
00284 NJB_Songid_Addframe(songid, frame);
00285 frame = NJB_Songid_Frame_New_Album(palbum);
00286 NJB_Songid_Addframe(songid, frame);
00287 frame = NJB_Songid_Frame_New_Artist(partist);
00288 NJB_Songid_Addframe(songid, frame);
00289 frame = NJB_Songid_Frame_New_Genre(pgenre);
00290 NJB_Songid_Addframe(songid, frame);
00291 frame = NJB_Songid_Frame_New_Year(year);
00292 NJB_Songid_Addframe(songid, frame);
00293 frame = NJB_Songid_Frame_New_Tracknum(tracknum);
00294 NJB_Songid_Addframe(songid, frame);
00295 frame = NJB_Songid_Frame_New_Length(length);
00296 NJB_Songid_Addframe(songid, frame);
00297 frame = NJB_Songid_Frame_New_Filename(filename);
00298 NJB_Songid_Addframe(songid, frame);
00299
00300 if ( NJB_Send_Track(njb, path, songid, progress, NULL, &trackid) == -1 ) {
00301 printf("\n");
00302 NJB_Error_Dump(njb,stderr);
00303 } else {
00304 printf("\nNJB track ID: %u\n", trackid);
00305 }
00306 printf("\n");
00307
00308 NJB_Songid_Destroy(songid);
00309
00310 NJB_Release(njb);
00311 NJB_Close(njb);
00312
00313 return 0;
00314 }