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>
35 static struct ast_imager *list;
36 static ast_mutex_t listlock = AST_MUTEX_INITIALIZER;
38 int ast_image_register(struct ast_imager *img)
40 if (option_verbose > 1)
41 ast_verbose(VERBOSE_PREFIX_2 "Registered format '%s' (%s)\n", img->name, img->desc);
42 ast_mutex_lock(&listlock);
45 ast_mutex_unlock(&listlock);
49 void ast_image_unregister(struct ast_imager *img)
51 struct ast_imager *i, *prev = NULL;
52 ast_mutex_lock(&listlock);
65 ast_mutex_unlock(&listlock);
66 if (i && (option_verbose > 1))
67 ast_verbose(VERBOSE_PREFIX_2 "Unregistered format '%s' (%s)\n", img->name, img->desc);
70 int ast_supports_images(struct ast_channel *chan)
72 if (!chan || !chan->pvt)
74 if (!chan->pvt->send_image)
79 static int file_exists(char *filename)
83 res = stat(filename, &st);
89 static void make_filename(char *buf, int len, char *filename, char *preflang, char *ext)
91 if (filename[0] == '/') {
92 if (preflang && strlen(preflang))
93 snprintf(buf, len, "%s-%s.%s", filename, preflang, ext);
95 snprintf(buf, len, "%s.%s", filename, ext);
97 if (preflang && strlen(preflang))
98 snprintf(buf, len, "%s/%s/%s-%s.%s", ast_config_AST_VAR_DIR, "images", filename, preflang, ext);
100 snprintf(buf, len, "%s/%s/%s.%s", ast_config_AST_VAR_DIR, "images", filename, ext);
104 struct ast_frame *ast_read_image(char *filename, char *preflang, int format)
106 struct ast_imager *i;
110 struct ast_imager *found = NULL;
113 struct ast_frame *f = NULL;
114 #if 0 /* We need to have some sort of read-only lock */
115 ast_mutex_lock(&listlock);
119 if (i->format & format) {
121 strncpy(tmp, i->exts, sizeof(tmp)-1);
123 e = strsep(&stringp, "|");
125 make_filename(buf, sizeof(buf), filename, preflang, e);
126 if ((len = file_exists(buf))) {
130 make_filename(buf, sizeof(buf), filename, NULL, e);
131 if ((len = file_exists(buf))) {
135 e = strsep(&stringp, "|");
141 fd = open(buf, O_RDONLY);
143 if (!found->identify || found->identify(fd)) {
144 /* Reset file pointer */
145 lseek(fd, 0, SEEK_SET);
146 f = found->read_image(fd,len);
148 ast_log(LOG_WARNING, "%s does not appear to be a %s file\n", buf, i->name);
151 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", buf, strerror(errno));
153 ast_log(LOG_WARNING, "Image file '%s' not found\n", filename);
155 ast_mutex_unlock(&listlock);
161 int ast_send_image(struct ast_channel *chan, char *filename)
165 if (chan->pvt->send_image) {
166 f = ast_read_image(filename, chan->language, -1);
168 res = chan->pvt->send_image(chan, f);
175 static int show_image_formats(int fd, int argc, char *argv[])
177 #define FORMAT "%10s %10s %50s %10s\n"
178 #define FORMAT2 "%10s %10s %50s %10s\n"
179 struct ast_imager *i;
181 return RESULT_SHOWUSAGE;
182 ast_cli(fd, FORMAT, "Name", "Extensions", "Description", "Format");
185 ast_cli(fd, FORMAT2, i->name, i->exts, i->desc, ast_getformatname(i->format));
188 return RESULT_SUCCESS;
191 struct ast_cli_entry show_images =
193 { "show", "image", "formats" },
195 "Displays image formats",
196 "Usage: show image formats\n"
197 " displays currently registered image formats (if any)\n"
201 int ast_image_init(void)
203 ast_cli_register(&show_images);