Linus Walleij | 543badf | 2007-02-05 19:07:38 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file albumart.c |
| 3 | * Example program to send album art. |
| 4 | * |
| 5 | * Copyright (C) 2006 Andy Kelk <andy@mopoke.co.uk> |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the |
| 19 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 20 | * Boston, MA 02111-1307, USA. |
| 21 | */ |
Linus Walleij | 2f1b640 | 2009-06-15 19:49:33 +0000 | [diff] [blame] | 22 | #include "config.h" |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 23 | #include "common.h" |
| 24 | #include "string.h" |
Linus Walleij | 2eaaff0 | 2009-01-15 21:30:36 +0000 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <limits.h> |
| 27 | #include <unistd.h> |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 28 | #include <fcntl.h> |
| 29 | #include <errno.h> |
Linus Walleij | 2eaaff0 | 2009-01-15 21:30:36 +0000 | [diff] [blame] | 30 | #include <sys/stat.h> |
| 31 | #include <sys/types.h> |
Linus Walleij | 6f05002 | 2009-05-06 21:14:41 +0000 | [diff] [blame] | 32 | #ifdef HAVE_SYS_UIO_H |
Linus Walleij | 2eaaff0 | 2009-01-15 21:30:36 +0000 | [diff] [blame] | 33 | #include <sys/uio.h> |
Linus Walleij | 6f05002 | 2009-05-06 21:14:41 +0000 | [diff] [blame] | 34 | #endif |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 35 | |
| 36 | static void usage(void) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 37 | printf("Usage: albumart -d -i <fileid/trackid> -n <albumname> -s <storage_id> -p <parent_id> <imagefile>\n"); |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 38 | exit(0); |
| 39 | } |
| 40 | |
| 41 | int main (int argc, char **argv) { |
| 42 | int opt; |
| 43 | extern int optind; |
| 44 | extern char *optarg; |
| 45 | LIBMTP_mtpdevice_t *device = NULL; |
| 46 | int idcount = 0; |
| 47 | int fd; |
| 48 | uint32_t *ids = NULL; |
| 49 | uint32_t *tmp = NULL; |
| 50 | uint64_t filesize; |
| 51 | char *imagedata = NULL; |
| 52 | char *albumname = NULL; |
| 53 | char *path = NULL; |
Linus Walleij | 20c3b67 | 2007-11-09 19:02:19 +0000 | [diff] [blame] | 54 | char *rest; |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 55 | struct stat statbuff; |
nicklas79 | aac4729 | 2009-09-28 18:16:45 +0000 | [diff] [blame] | 56 | uint32_t storageid = 0; |
| 57 | uint32_t parentid = 0; |
Marcus Meissner | 9a28fa6 | 2015-10-04 15:11:40 +0200 | [diff] [blame] | 58 | int ret; |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 59 | |
tedbullock | cd9f499 | 2007-03-29 06:00:40 +0000 | [diff] [blame] | 60 | fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n"); |
| 61 | |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 62 | while ( (opt = getopt(argc, argv, "dhn:i:s:p:")) != -1 ) { |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 63 | switch (opt) { |
| 64 | case 'h': |
| 65 | usage(); |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 66 | case 'd': |
Catalin Patulea | 406d5f9 | 2012-07-20 19:00:24 -0400 | [diff] [blame] | 67 | LIBMTP_Set_Debug(LIBMTP_DEBUG_PTP | LIBMTP_DEBUG_DATA); |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 68 | break; |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 69 | case 'i': |
| 70 | idcount++; |
| 71 | if ((tmp = realloc(ids, sizeof(uint32_t) * (idcount))) == NULL) { |
| 72 | printf("realloc failed\n"); |
| 73 | return 1; |
| 74 | } |
| 75 | ids = tmp; |
Linus Walleij | 20c3b67 | 2007-11-09 19:02:19 +0000 | [diff] [blame] | 76 | ids[(idcount-1)] = strtoul(optarg, &rest, 0); |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 77 | break; |
| 78 | case 'n': |
| 79 | albumname = strdup(optarg); |
| 80 | break; |
nicklas79 | aac4729 | 2009-09-28 18:16:45 +0000 | [diff] [blame] | 81 | case 's': |
| 82 | storageid = (uint32_t) strtoul(optarg, NULL, 0); |
| 83 | break; |
| 84 | case 'p': |
| 85 | parentid = (uint32_t) strtoul(optarg, NULL, 0); |
| 86 | break; |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 87 | default: |
| 88 | usage(); |
| 89 | } |
| 90 | } |
| 91 | argc -= optind; |
| 92 | argv += optind; |
| 93 | |
| 94 | if ( argc != 1 ) { |
| 95 | printf("You need to pass a filename.\n"); |
| 96 | usage(); |
| 97 | } |
| 98 | |
| 99 | if ( albumname == NULL) { |
| 100 | printf("You need to supply an album name.\n"); |
| 101 | usage(); |
| 102 | } |
| 103 | |
| 104 | if (idcount == 0) { |
| 105 | printf("You need to supply one or more track IDs\n"); |
| 106 | usage(); |
| 107 | } |
| 108 | |
| 109 | path = argv[0]; |
| 110 | |
| 111 | if ( stat(path, &statbuff) == -1 ) { |
| 112 | fprintf(stderr, "%s: ", path); |
| 113 | perror("stat"); |
| 114 | exit(1); |
| 115 | } |
| 116 | filesize = (uint64_t) statbuff.st_size; |
| 117 | imagedata = malloc(filesize * sizeof(uint8_t)); |
| 118 | |
| 119 | #ifdef __WIN32__ |
Linus Walleij | 5b4a4d2 | 2009-01-10 12:18:14 +0000 | [diff] [blame] | 120 | if ( (fd = open(path, O_RDONLY|O_BINARY) == -1) ) { |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 121 | #else |
| 122 | if ( (fd = open(path, O_RDONLY)) == -1) { |
| 123 | #endif |
| 124 | printf("Couldn't open image file %s (%s)\n",path,strerror(errno)); |
| 125 | return 1; |
| 126 | } |
| 127 | else { |
Marcus Meissner | 9a28fa6 | 2015-10-04 15:11:40 +0200 | [diff] [blame] | 128 | ret = read(fd, imagedata, filesize); |
| 129 | if (ret == -1) perror("read"); |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 130 | close(fd); |
| 131 | } |
| 132 | |
| 133 | LIBMTP_Init(); |
| 134 | device = LIBMTP_Get_First_Device(); |
| 135 | if (device == NULL) { |
| 136 | printf("No devices.\n"); |
| 137 | return 0; |
| 138 | } |
| 139 | |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 140 | LIBMTP_filesampledata_t *albumart = LIBMTP_new_filesampledata_t(); |
| 141 | albumart->data = imagedata; |
| 142 | albumart->size = filesize; |
| 143 | albumart->filetype = LIBMTP_FILETYPE_JPEG; |
| 144 | |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 145 | LIBMTP_album_t *album = LIBMTP_new_album_t(); |
| 146 | album->name = albumname; |
| 147 | album->no_tracks = idcount; |
| 148 | album->tracks = ids; |
nicklas79 | aac4729 | 2009-09-28 18:16:45 +0000 | [diff] [blame] | 149 | album->parent_id = parentid; |
| 150 | album->storage_id = storageid; |
Marcus Meissner | 9a28fa6 | 2015-10-04 15:11:40 +0200 | [diff] [blame] | 151 | |
| 152 | ret = LIBMTP_Create_New_Album(device,album); |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 153 | if (ret == 0) { |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 154 | ret = LIBMTP_Send_Representative_Sample(device,album->album_id, albumart); |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 155 | if (ret != 0) { |
| 156 | printf("Couldn't send album art\n"); |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 157 | LIBMTP_Dump_Errorstack(device); |
| 158 | LIBMTP_Clear_Errorstack(device); |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | else { |
| 162 | printf("Couldn't create album object\n"); |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 163 | LIBMTP_Dump_Errorstack(device); |
| 164 | LIBMTP_Clear_Errorstack(device); |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 167 | LIBMTP_destroy_filesampledata_t(albumart); |
| 168 | LIBMTP_destroy_album_t(album); |
| 169 | |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 170 | LIBMTP_Release_Device(device); |
| 171 | printf("OK.\n"); |
| 172 | return 0; |
| 173 | } |
| 174 | |