2 * Asterisk -- A telephony toolkit for Linux.
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
23 #include <asterisk/sched.h>
24 #include <asterisk/options.h>
25 #include <asterisk/channel.h>
26 #include <asterisk/channel_pvt.h>
27 #include <asterisk/logger.h>
28 #include <asterisk/file.h>
29 #include <asterisk/image.h>
30 #include <asterisk/translate.h>
31 #include <asterisk/cli.h>
34 static struct ast_imager *list;
35 static pthread_mutex_t listlock = PTHREAD_MUTEX_INITIALIZER;
37 int ast_image_register(struct ast_imager *img)
39 if (option_verbose > 1)
40 ast_verbose(VERBOSE_PREFIX_2 "Registered format '%s' (%s)\n", img->name, img->desc);
41 ast_pthread_mutex_lock(&listlock);
44 ast_pthread_mutex_unlock(&listlock);
48 void ast_image_unregister(struct ast_imager *img)
50 struct ast_imager *i, *prev = NULL;
51 ast_pthread_mutex_lock(&listlock);
64 ast_pthread_mutex_unlock(&listlock);
65 if (i && (option_verbose > 1))
66 ast_verbose(VERBOSE_PREFIX_2 "Registered format '%s' (%s)\n", img->name, img->desc);
69 int ast_supports_images(struct ast_channel *chan)
71 if (!chan->pvt->send_image)
76 static int file_exists(char *filename)
80 res = stat(filename, &st);
86 static void make_filename(char *buf, int len, char *filename, char *preflang, char *ext)
88 if (filename[0] == '/') {
89 if (preflang && strlen(preflang))
90 snprintf(buf, len, "%s-%s.%s", filename, preflang, ext);
92 snprintf(buf, len, "%s.%s", filename, ext);
94 if (preflang && strlen(preflang))
95 snprintf(buf, len, "%s/%s-%s.%s", AST_IMAGES, filename, preflang, ext);
97 snprintf(buf, len, "%s/%s.%s", AST_IMAGES, filename, ext);
101 struct ast_frame *ast_read_image(char *filename, char *preflang, int format)
103 struct ast_imager *i;
107 struct ast_imager *found = NULL;
110 struct ast_frame *f = NULL;
111 #if 0 /* We need to have some sort of read-only lock */
112 ast_pthread_mutex_lock(&listlock);
116 if (i->format & format) {
117 strncpy(tmp, i->exts, sizeof(tmp));
118 e = strtok(tmp, "|");
120 make_filename(buf, sizeof(buf), filename, preflang, e);
121 if ((len = file_exists(buf))) {
125 make_filename(buf, sizeof(buf), filename, NULL, e);
126 if ((len = file_exists(buf))) {
130 e = strtok(NULL, "|");
136 fd = open(buf, O_RDONLY);
138 if (!found->identify || found->identify(fd)) {
139 /* Reset file pointer */
140 lseek(fd, 0, SEEK_SET);
141 f = found->read_image(fd,len);
143 ast_log(LOG_WARNING, "%s does not appear to be a %s file\n", buf, i->name);
146 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", buf, strerror(errno));
148 ast_log(LOG_WARNING, "Image file '%s' not found\n", filename);
150 ast_pthread_mutex_unlock(&listlock);
156 int ast_send_image(struct ast_channel *chan, char *filename)
160 if (chan->pvt->send_image) {
161 f = ast_read_image(filename, chan->language, -1);
163 res = chan->pvt->send_image(chan, f);
170 static int show_image_formats(int fd, int argc, char *argv[])
172 #define FORMAT "%10s %10s %50s %10s\n"
173 #define FORMAT2 "%10s %10s %50s %10d\n"
174 struct ast_imager *i;
176 return RESULT_SHOWUSAGE;
177 ast_cli(fd, FORMAT, "Name", "Extensions", "Description", "Format");
180 ast_cli(fd, FORMAT2, i->name, i->exts, i->desc, i->format);
183 return RESULT_SUCCESS;
186 struct ast_cli_entry show_images =
188 { "show", "image", "formats" },
190 "Displays image formats",
191 "Usage: show image formats\n"
192 " displays currently registered image formats (if any)\n"
196 int ast_image_init(void)
198 ast_cli_register(&show_images);