Linus Walleij | 7fe4a39 | 2012-01-12 22:16:19 +0100 | [diff] [blame] | 1 | /** |
Linus Walleij | 3d78c4c | 2007-02-15 12:18:12 +0000 | [diff] [blame] | 2 | * \file tracks.c |
| 3 | * Example program to list the tracks on a device. |
| 4 | * |
Linus Walleij | 7fe4a39 | 2012-01-12 22:16:19 +0100 | [diff] [blame] | 5 | * Copyright (C) 2005-2012 Linus Walleij <triad@df.lth.se> |
tedbullock | 5164920 | 2007-02-23 03:04:33 +0000 | [diff] [blame] | 6 | * Copyright (C) 2007 Ted Bullock <tbullock@canada.com> |
Linus Walleij | 3d78c4c | 2007-02-15 12:18:12 +0000 | [diff] [blame] | 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation; either |
| 11 | * version 2 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This library is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
| 19 | * License along with this library; if not, write to the |
| 20 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 21 | * Boston, MA 02111-1307, USA. |
| 22 | */ |
Linus Walleij | 6d6d8f6 | 2006-02-13 13:21:57 +0000 | [diff] [blame] | 23 | #include "common.h" |
Linus Walleij | 2eaaff0 | 2009-01-15 21:30:36 +0000 | [diff] [blame] | 24 | #include <stdlib.h> |
Linus Walleij | 6d6d8f6 | 2006-02-13 13:21:57 +0000 | [diff] [blame] | 25 | |
| 26 | static void dump_trackinfo(LIBMTP_track_t *track) |
| 27 | { |
Linus Walleij | 64ad9ae | 2006-11-27 11:23:26 +0000 | [diff] [blame] | 28 | printf("Track ID: %u\n", track->item_id); |
Linus Walleij | 6d6d8f6 | 2006-02-13 13:21:57 +0000 | [diff] [blame] | 29 | if (track->title != NULL) |
| 30 | printf(" Title: %s\n", track->title); |
| 31 | if (track->artist != NULL) |
| 32 | printf(" Artist: %s\n", track->artist); |
| 33 | if (track->genre != NULL) |
| 34 | printf(" Genre: %s\n", track->genre); |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 35 | if (track->composer != NULL) |
| 36 | printf(" Composer: %s\n", track->composer); |
Linus Walleij | 6d6d8f6 | 2006-02-13 13:21:57 +0000 | [diff] [blame] | 37 | if (track->album != NULL) |
| 38 | printf(" Album: %s\n", track->album); |
| 39 | if (track->date != NULL) |
| 40 | printf(" Date: %s\n", track->date); |
| 41 | if (track->filename != NULL) |
| 42 | printf(" Origfilename: %s\n", track->filename); |
| 43 | printf(" Track number: %d\n", track->tracknumber); |
| 44 | printf(" Duration: %d milliseconds\n", track->duration); |
Linus Walleij | e73f74a | 2007-04-02 08:51:35 +0000 | [diff] [blame] | 45 | #ifdef __WIN32__ |
| 46 | printf(" File size %I64u bytes\n", track->filesize); |
| 47 | #else |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 48 | printf(" File size %llu bytes\n", (long long unsigned int) track->filesize); |
Linus Walleij | e73f74a | 2007-04-02 08:51:35 +0000 | [diff] [blame] | 49 | #endif |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 50 | printf(" Filetype: %s\n", LIBMTP_Get_Filetype_Description(track->filetype)); |
Linus Walleij | cf223e6 | 2006-06-19 09:31:53 +0000 | [diff] [blame] | 51 | if (track->samplerate != 0) { |
| 52 | printf(" Sample rate: %u Hz\n", track->samplerate); |
| 53 | } |
| 54 | if (track->nochannels != 0) { |
| 55 | printf(" Number of channels: %u\n", track->nochannels); |
| 56 | } |
| 57 | if (track->wavecodec != 0) { |
| 58 | printf(" WAVE fourCC code: 0x%08X\n", track->wavecodec); |
| 59 | } |
| 60 | if (track->bitrate != 0) { |
| 61 | printf(" Bitrate: %u bits/s\n", track->bitrate); |
| 62 | } |
| 63 | if (track->bitratetype != 0) { |
| 64 | if (track->bitratetype == 1) { |
| 65 | printf(" Bitrate type: Constant\n"); |
| 66 | } else if (track->bitratetype == 2) { |
| 67 | printf(" Bitrate type: Variable (VBR)\n"); |
| 68 | } else if (track->bitratetype == 3) { |
| 69 | printf(" Bitrate type: Free\n"); |
| 70 | } else { |
| 71 | printf(" Bitrate type: Unknown/Erroneous value\n"); |
| 72 | } |
| 73 | } |
| 74 | if (track->rating != 0) { |
| 75 | printf(" User rating: %u (out of 100)\n", track->rating); |
| 76 | } |
| 77 | if (track->usecount != 0) { |
| 78 | printf(" Use count: %u times\n", track->usecount); |
| 79 | } |
Linus Walleij | 6d6d8f6 | 2006-02-13 13:21:57 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Linus Walleij | 0c754e3 | 2015-08-21 22:19:10 +0200 | [diff] [blame] | 82 | static void |
| 83 | dump_tracks(LIBMTP_mtpdevice_t *device, uint32_t storageid, int leaf) |
| 84 | { |
| 85 | LIBMTP_file_t *files; |
| 86 | |
| 87 | /* Get track listing. */ |
| 88 | files = LIBMTP_Get_Files_And_Folders(device, |
| 89 | storageid, |
| 90 | leaf); |
| 91 | if (files == NULL) { |
| 92 | LIBMTP_Dump_Errorstack(device); |
| 93 | LIBMTP_Clear_Errorstack(device); |
| 94 | } else { |
| 95 | LIBMTP_file_t *file, *tmp; |
| 96 | |
| 97 | file = files; |
| 98 | while (file != NULL) { |
| 99 | /* Please don't print these */ |
| 100 | if (file->filetype == LIBMTP_FILETYPE_FOLDER) { |
| 101 | dump_tracks(device, storageid, file->item_id); |
| 102 | } else if (LIBMTP_FILETYPE_IS_TRACK(file->filetype)) { |
| 103 | LIBMTP_track_t *track; |
| 104 | |
| 105 | track = LIBMTP_Get_Trackmetadata(device, file->item_id); |
| 106 | dump_trackinfo(track); |
| 107 | LIBMTP_destroy_track_t(track); |
| 108 | } |
| 109 | tmp = file; |
| 110 | file = file->next; |
| 111 | LIBMTP_destroy_file_t(tmp); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
Linus Walleij | 6d6d8f6 | 2006-02-13 13:21:57 +0000 | [diff] [blame] | 116 | int main (int argc, char **argv) |
| 117 | { |
Linus Walleij | 0c754e3 | 2015-08-21 22:19:10 +0200 | [diff] [blame] | 118 | LIBMTP_raw_device_t *rawdevices; |
| 119 | int numrawdevices; |
| 120 | LIBMTP_error_number_t err; |
| 121 | int i; |
Linus Walleij | 6d6d8f6 | 2006-02-13 13:21:57 +0000 | [diff] [blame] | 122 | |
| 123 | LIBMTP_Init(); |
tedbullock | 5164920 | 2007-02-23 03:04:33 +0000 | [diff] [blame] | 124 | fprintf(stdout, "Attempting to connect device(s)\n"); |
| 125 | |
Linus Walleij | 0c754e3 | 2015-08-21 22:19:10 +0200 | [diff] [blame] | 126 | err = LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices); |
| 127 | switch(err) |
tedbullock | 5164920 | 2007-02-23 03:04:33 +0000 | [diff] [blame] | 128 | { |
tedbullock | 433d217 | 2007-02-23 22:39:12 +0000 | [diff] [blame] | 129 | case LIBMTP_ERROR_NO_DEVICE_ATTACHED: |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 130 | fprintf(stdout, "mtp-tracks: No Devices have been found\n"); |
tedbullock | 5164920 | 2007-02-23 03:04:33 +0000 | [diff] [blame] | 131 | return 0; |
| 132 | case LIBMTP_ERROR_CONNECTING: |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 133 | fprintf(stderr, "mtp-tracks: There has been an error connecting. Exit\n"); |
tedbullock | 5164920 | 2007-02-23 03:04:33 +0000 | [diff] [blame] | 134 | return 1; |
| 135 | case LIBMTP_ERROR_MEMORY_ALLOCATION: |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 136 | fprintf(stderr, "mtp-tracks: Memory Allocation Error. Exit\n"); |
tedbullock | 5164920 | 2007-02-23 03:04:33 +0000 | [diff] [blame] | 137 | return 1; |
Linus Walleij | 7fe4a39 | 2012-01-12 22:16:19 +0100 | [diff] [blame] | 138 | |
tedbullock | 5164920 | 2007-02-23 03:04:33 +0000 | [diff] [blame] | 139 | /* Unknown general errors - This should never execute */ |
| 140 | case LIBMTP_ERROR_GENERAL: |
| 141 | default: |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 142 | fprintf(stderr, "mtp-tracks: Unknown error, please report " |
tedbullock | 5164920 | 2007-02-23 03:04:33 +0000 | [diff] [blame] | 143 | "this to the libmtp developers\n"); |
| 144 | return 1; |
| 145 | |
| 146 | /* Successfully connected at least one device, so continue */ |
| 147 | case LIBMTP_ERROR_NONE: |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 148 | fprintf(stdout, "mtp-tracks: Successfully connected\n"); |
tedbullock | 5164920 | 2007-02-23 03:04:33 +0000 | [diff] [blame] | 149 | fflush(stdout); |
Linus Walleij | 0c754e3 | 2015-08-21 22:19:10 +0200 | [diff] [blame] | 150 | break; |
Linus Walleij | 6d6d8f6 | 2006-02-13 13:21:57 +0000 | [diff] [blame] | 151 | } |
Linus Walleij | 7fe4a39 | 2012-01-12 22:16:19 +0100 | [diff] [blame] | 152 | |
Linus Walleij | 0c754e3 | 2015-08-21 22:19:10 +0200 | [diff] [blame] | 153 | /* Iterate through connected MTP devices */ |
| 154 | for (i = 0; i < numrawdevices; i++) { |
| 155 | LIBMTP_mtpdevice_t *device; |
| 156 | LIBMTP_devicestorage_t *storage; |
Linus Walleij | 7fe4a39 | 2012-01-12 22:16:19 +0100 | [diff] [blame] | 157 | char *friendlyname; |
| 158 | |
Linus Walleij | 0c754e3 | 2015-08-21 22:19:10 +0200 | [diff] [blame] | 159 | device = LIBMTP_Open_Raw_Device_Uncached(&rawdevices[i]); |
| 160 | if (device == NULL) { |
| 161 | fprintf(stderr, "Unable to open raw device %d\n", i); |
| 162 | continue; |
| 163 | } |
| 164 | |
tedbullock | 5164920 | 2007-02-23 03:04:33 +0000 | [diff] [blame] | 165 | /* Echo the friendly name so we know which device we are working with */ |
Linus Walleij | 7fe4a39 | 2012-01-12 22:16:19 +0100 | [diff] [blame] | 166 | friendlyname = LIBMTP_Get_Friendlyname(device); |
tedbullock | 5164920 | 2007-02-23 03:04:33 +0000 | [diff] [blame] | 167 | if (friendlyname == NULL) { |
| 168 | printf("Friendly name: (NULL)\n"); |
| 169 | } else { |
| 170 | printf("Friendly name: %s\n", friendlyname); |
| 171 | free(friendlyname); |
Linus Walleij | 6d6d8f6 | 2006-02-13 13:21:57 +0000 | [diff] [blame] | 172 | } |
Linus Walleij | 0c754e3 | 2015-08-21 22:19:10 +0200 | [diff] [blame] | 173 | |
| 174 | LIBMTP_Dump_Errorstack(device); |
| 175 | LIBMTP_Clear_Errorstack(device); |
| 176 | |
| 177 | /* Loop over storages */ |
| 178 | for (storage = device->storage; storage != 0; storage = storage->next) { |
Stanisław Pitucha | 4c162fa | 2017-02-04 09:35:54 +1100 | [diff] [blame] | 179 | dump_tracks(device, storage->id, LIBMTP_FILES_AND_FOLDERS_ROOT); |
Linus Walleij | 7fe4a39 | 2012-01-12 22:16:19 +0100 | [diff] [blame] | 180 | } |
Linus Walleij | 0c754e3 | 2015-08-21 22:19:10 +0200 | [diff] [blame] | 181 | |
| 182 | LIBMTP_Release_Device(device); |
Linus Walleij | 6d6d8f6 | 2006-02-13 13:21:57 +0000 | [diff] [blame] | 183 | } |
Linus Walleij | 7fe4a39 | 2012-01-12 22:16:19 +0100 | [diff] [blame] | 184 | |
Linus Walleij | 6d6d8f6 | 2006-02-13 13:21:57 +0000 | [diff] [blame] | 185 | printf("OK.\n"); |
| 186 | exit (0); |
| 187 | } |
| 188 | |