Linus Walleij | 3d78c4c | 2007-02-15 12:18:12 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file thumb.c |
| 3 | * Example program to send and associate album art to an entity |
| 4 | * on a device. |
| 5 | * |
| 6 | * Copyright (C) 2006 Robert Reardon <rreardon@monkshatch.vispa.com> |
| 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 | 2f1b640 | 2009-06-15 19:49:33 +0000 | [diff] [blame] | 23 | #include "config.h" |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 24 | #include "common.h" |
| 25 | #include "string.h" |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 26 | #include <fcntl.h> |
| 27 | #include <errno.h> |
Linus Walleij | 2eaaff0 | 2009-01-15 21:30:36 +0000 | [diff] [blame] | 28 | #include <stdlib.h> |
| 29 | #include <unistd.h> |
| 30 | #include <limits.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 |
Linus Walleij | 2eaaff0 | 2009-01-15 21:30:36 +0000 | [diff] [blame] | 35 | #include <sys/stat.h> |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 36 | |
| 37 | static void usage(void) { |
| 38 | printf("Usage: thumb -i <fileid/trackid> <imagefile>\n"); |
| 39 | exit(0); |
| 40 | } |
| 41 | |
| 42 | int main (int argc, char **argv) { |
| 43 | int opt; |
| 44 | extern int optind; |
| 45 | extern char *optarg; |
| 46 | LIBMTP_mtpdevice_t *device = NULL; |
| 47 | int fd; |
| 48 | uint32_t id = 0; |
| 49 | uint64_t filesize; |
Marcus Meissner | f59e7fd | 2015-10-04 14:56:03 +0200 | [diff] [blame] | 50 | char *imagedata = NULL; |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 51 | char *path = NULL; |
Linus Walleij | 20c3b67 | 2007-11-09 19:02:19 +0000 | [diff] [blame] | 52 | char *rest; |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 53 | struct stat statbuff; |
| 54 | int ret; |
| 55 | |
tedbullock | cd9f499 | 2007-03-29 06:00:40 +0000 | [diff] [blame] | 56 | fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n"); |
| 57 | |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 58 | while ( (opt = getopt(argc, argv, "hi:")) != -1 ) { |
| 59 | switch (opt) { |
| 60 | case 'h': |
| 61 | usage(); |
| 62 | case 'i': |
Linus Walleij | 20c3b67 | 2007-11-09 19:02:19 +0000 | [diff] [blame] | 63 | id = strtoul(optarg, &rest, 0); |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 64 | break; |
| 65 | default: |
| 66 | usage(); |
| 67 | } |
| 68 | } |
| 69 | argc -= optind; |
| 70 | argv += optind; |
| 71 | |
| 72 | if ( argc != 1 ) { |
| 73 | printf("You need to pass a filename.\n"); |
| 74 | usage(); |
| 75 | } |
| 76 | |
| 77 | path = argv[0]; |
| 78 | |
| 79 | if ( stat(path, &statbuff) == -1 ) { |
| 80 | fprintf(stderr, "%s: ", path); |
| 81 | perror("stat"); |
| 82 | exit(1); |
| 83 | } |
Marcus Meissner | f59e7fd | 2015-10-04 14:56:03 +0200 | [diff] [blame] | 84 | filesize = statbuff.st_size; |
| 85 | imagedata = malloc(filesize); |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 86 | |
| 87 | #ifdef __WIN32__ |
Linus Walleij | 5b4a4d2 | 2009-01-10 12:18:14 +0000 | [diff] [blame] | 88 | if ( (fd = open(path, O_RDONLY|O_BINARY) == -1) ) { |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 89 | #else |
| 90 | if ( (fd = open(path, O_RDONLY)) == -1) { |
| 91 | #endif |
| 92 | printf("Couldn't open image file %s (%s)\n",path,strerror(errno)); |
| 93 | return 1; |
Marcus Meissner | f59e7fd | 2015-10-04 14:56:03 +0200 | [diff] [blame] | 94 | } else { |
| 95 | ret = read(fd, imagedata, filesize); |
| 96 | if (ret == -1) perror("read thumb data"); |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 97 | close(fd); |
| 98 | } |
| 99 | |
| 100 | LIBMTP_Init(); |
| 101 | device = LIBMTP_Get_First_Device(); |
| 102 | if (device == NULL) { |
| 103 | printf("No devices.\n"); |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | LIBMTP_filesampledata_t *thumb = LIBMTP_new_filesampledata_t(); |
Marcus Meissner | f59e7fd | 2015-10-04 14:56:03 +0200 | [diff] [blame] | 108 | thumb->data = imagedata; |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 109 | thumb->size = filesize; |
| 110 | thumb->filetype = LIBMTP_FILETYPE_JPEG; |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 111 | ret = LIBMTP_Send_Representative_Sample(device,id,thumb); |
| 112 | if (ret != 0) { |
| 113 | printf("Couldn't send thumbnail\n"); |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 114 | LIBMTP_Dump_Errorstack(device); |
| 115 | LIBMTP_Clear_Errorstack(device); |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 118 | LIBMTP_destroy_filesampledata_t(thumb); |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 119 | |
| 120 | LIBMTP_Release_Device(device); |
| 121 | printf("OK.\n"); |
| 122 | return 0; |
| 123 | } |