/* * enfle.c -- graphic loader Enfle main program * (C)Copyright 1998, 99, 2000 by Hiroshi Takekawa * This file is part of Enfle. * * Last Modified: Mon Jun 26 19:43:42 2000. * $Id: enfle.c,v 1.41 2000/06/27 18:19:29 sian Exp $ * * Enfle is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Enfle is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA */ #include "enfle.h" #include "browse.h" #include "browseaction.h" #include "utils.h" #include "dlist.h" #include "hash.h" #include "sort.h" #include "plugin.h" #include "version.h" /* static linked plugin prototypes */ PROTO_LINKED_PLUGINS #if HAVE_UNISTD_H # include # include #endif #if HAVE_SYS_TIME_H # include #endif #include #include static void title(void) { fprintf(stderr, "%s version %s\n%s\n\n", PACKAGE, _VERSION, COPYRIGHT); } static void list_plugins(Dlist *p, PluginType type, int long_format) { Dlist_data *t = dlist_gettop(p); PluginInfo *pinfo; if (!t) return; for (; t != NULL; t = t->next) { pinfo = t->data; if (pinfo->type == type) { fprintf(stderr, " %s", long_format ? pinfo->pluginname : pinfo->pluginshortname); if (long_format > 1) fprintf(stderr, " by %s", pinfo->author); if (long_format) fprintf(stderr, "\n"); } } if (!long_format) fprintf(stderr, "\n"); } static void usage(Info *info) { title(); fprintf(stderr, "Usage: enfle [options] filenames\n"); fprintf(stderr, "Options:\n"); fprintf(stderr, " -t: enable transparent(shape)\n"); fprintf(stderr, " -m: specify magnify method\n"); fprintf(stderr, " -q: always quantize in rendering 256-over-color image.\n"); fprintf(stderr, " -c: show comment if exists.\n"); fprintf(stderr, " -S: disable filename sort.\n"); fprintf(stderr, " -C: auto conversion, can specify destination format(default PNG).\n"); fprintf(stderr, " -W: show more verbose messages.\n"); fprintf(stderr, " -w: set first specified image as wallpaper.\n"); fprintf(stderr, " -x: specify x-threshold(default %d).\n", X_THRESHOLD); fprintf(stderr, " -y: specify y-threshold(default %d).\n", Y_THRESHOLD); fprintf(stderr, " -i: specify start-up index.\n"); fprintf(stderr, " -X: specify exclude.\n"); fprintf(stderr, " -s: clear threshold\n"); fprintf(stderr, " -p: scan specified directory to find plugins.\n"); fprintf(stderr, " -P: show long format plugin information.\n"); fprintf(stderr, " -PP: show longer format plugin information.\n"); fprintf(stderr, " -PD: disable default search path.\n"); fprintf(stderr, " -v: show version.\n"); fprintf(stderr, " -h: show help message.\n"); #if defined(SHAPE) || defined(SOME_EXTENSION) fprintf(stderr, "Extension: "); # ifdef SHAPE fprintf(stderr, "Shape "); # endif # ifdef SOME_EXTENSION fprintf(stderr, "Some "); # endif fprintf(stderr, "\n"); #endif #ifdef PLUGIN_SEARCH_PATH fprintf(stderr, "Default plugin search path: %s\n", PLUGIN_SEARCH_PATH); #endif fprintf(stderr, "Available Loader:\n"); list_plugins(info->pluginlist, _Loader, 0); fprintf(stderr, "Available Saver:\n"); list_plugins(info->pluginlist, _Saver, 0); fprintf(stderr, "Available Archiver:\n"); list_plugins(info->pluginlist, _Archiver, 0); } static void initialize(Info *info, Binfo *binfo) { Screen *sc; XVisualInfo *xvi, xvit; int screen = 0; int nvisual; /* Open display */ if ((info->disp = XOpenDisplay(NULL)) == NULL) { fprintf(stderr, "Can't open display\n"); exit(1); } #ifdef SHAPE { int eventbase, errorbase; if (!XShapeQueryExtension(info->disp, &eventbase, &errorbase)) { fprintf(stderr, "Enfle is compiled with shape extension, but server does not support it.\n"); XCloseDisplay(info->disp); exit(1); } } #endif /* Create main window */ info->root = RootWindow(info->disp, screen), info->win = XCreateSimpleWindow(info->disp, info->root, 400, 300, 400, 300, 1, WhitePixel(info->disp, screen), BlackPixel(info->disp, screen)); XSelectInput(info->disp, info->win, MAIN_WIN_MASK); /* create and set gc */ info->gc = XCreateGC(info->disp, info->win, 0, 0); XSetForeground(info->disp, info->gc, WhitePixel(info->disp, screen)); XSetBackground(info->disp, info->gc, BlackPixel(info->disp, screen)); /* set up font */ binfo->info_string_font = XLoadFont(info->disp, "a14"); XSetFont(info->disp, info->gc, binfo->info_string_font); XSetGraphicsExposures(info->disp, info->gc, False); /* get visual info */ xvit.screen = screen; xvit.depth = 24; xvit.class = TrueColor; xvi = XGetVisualInfo(info->disp, VisualScreenMask | VisualDepthMask | VisualClassMask, &xvit, &nvisual); if (nvisual) { /* Ok, depth24 TrueColor visual available */ #ifdef DEBUG fprintf(stderr, "Ok, depth24 TrueColor visual available\n"); #endif info->visual = xvi[0].visual; info->depth = 24; XFree(xvi); } else { /* Use default visual */ #ifdef DEBUG fprintf(stderr, "Use default visual\n"); #endif info->visual = DefaultVisual(info->disp, 0); info->depth = DefaultDepth(info->disp, 0); } if (info->depth < 15) info->if_quantize = 1; info->delay_factor = 1.0; sc = ScreenOfDisplay(info->disp, 0); binfo->swidth = WidthOfScreen(sc); binfo->sheight = HeightOfScreen(sc); info->xdata = NULL; info->comment = NULL; info->wait_cursor = XCreateFontCursor(info->disp, WAIT_CURSOR); info->normal_cursor = XCreateFontCursor(info->disp, NORMAL_CURSOR); } static int check_exclude(char *filename, Dlist *exclude) { Dlist_data *ex; if (exclude == NULL) return 0; for (ex = dlist_gettop(exclude); ex != NULL; ex = ex->next) { #ifdef MORE_DEBUG fprintf(stderr, "%s %s\n", get_basename(filename), (char *)ex->data); #endif /* Should I use fnmatch()? */ if (strlen(ex->data) && (strncmp(get_basename(filename), ex->data, strlen(ex->data)) == 0)) return 1; } return 0; } static void add_to_dlist(Dlist *p, char *filename, Dlist *exclude) { struct stat s; DIR *d; struct dirent *dir; if (strcmp(filename, "-") && stat(filename, &s) < 0) { fprintf(stderr, "Can't stat: %s\n", filename); return; } if (S_ISDIR(s.st_mode)) { /* This is directory... */ if ((d = opendir(filename)) == NULL) { fprintf(stderr, "Can't opendir: %s\n", filename); return; } while ((dir = readdir(d)) != NULL) if (strcmp(dir->d_name, ".") && strcmp(dir->d_name, "..")) { char *f = malloc(strlen(filename) + 1 + strlen(dir->d_name) + 1); if (f == NULL) { fprintf(stderr, "No enough memory\n"); exit(1); } strcpy(f, filename); strcat(f, "/"); strcat(f, dir->d_name); add_to_dlist(p, f, exclude); free(f); } closedir(d); } else { if (!check_exclude(filename, exclude)) { if (dlist_stradd(p, filename) == NULL) { fprintf(stderr, "Dlist error: (maybe) no enough memory\n"); exit(1); } } else { #ifdef DEBUG fprintf(stderr, "Excluded %s\n", filename); #endif } } } #define _REGISTER_LINKED_PLUGIN(getinfo) \ register_linked_plugin(info, getinfo) static int register_linked_plugin(Info *info, int (*get_plugininfo)(PluginInfo *)) { PluginInfo *p; if ((p = plugin_retrieve_info(get_plugininfo)) == NULL) { fprintf(stderr, "register_linked_plugin: get_plugininfo: returns NULL\n"); return 0; } return plugin_install(info->pluginlist, p); } static void initialize_plugins(Info *info) { char **plugin_dirs = split(getenv("ENFLE_PLUGIN_DIR"), ':'); int i = 0; if ((info->pluginlist = dlist_create()) == NULL) { fprintf(stderr, "initialize_plugins: FATAL: No enough memory for pluginlist\n"); exit(1); } if (plugin_dirs != NULL) { while (plugin_dirs[i] != NULL) if (plugin_dirs[i][0] != '\0') plugin_scan_directory(info->pluginlist, plugin_dirs[i++]); free_string_array(plugin_dirs); } /* register static linked plugins */ LINKED_PLUGINS } /* main routine */ int main(int argc, char **argv) { extern char *optarg; extern int optind; int c, show_info = 0, disable_default = 0, if_sort = 1; Info info; Binfo binfo; Dlist *p; Dlist *exclude = NULL; memset(&info, 0, sizeof(Info)); info.transparent_disposal = _DONOTHING; info.if_quantize = 0; info.delay_factor = 1.0; memset(&binfo, 0, sizeof(Binfo)); binfo.x_threshold = X_THRESHOLD; binfo.y_threshold = Y_THRESHOLD; binfo.interpolate_method = _BILINEAR; if ((binfo.act_hash = hash_create(ACT_HASH_SIZE)) == NULL) { fprintf(stderr, "No enough memory for act_hash\n"); exit(1); } #if 0 /* already zero cleared */ binfo.index = 0; binfo.if_magnify = binfo.if_comment = binfo.if_warning = binfo.if_wallpaper = 0; #endif initialize_plugins(&info); while ((c = getopt(argc, argv, "tqchsvwMSWC::P::x:y:i:p:m:X:")) != EOF) { switch (c) { case 't': #ifdef SHAPE info.transparent_disposal = (info.transparent_disposal == _SHAPE) ? _TRANSPARENT : _SHAPE; #else info.transparent_disposal = _TRANSPARENT; #endif break; case 'q': info.if_quantize = 1; break; case 'm': if ((binfo.if_magnify = atoi(optarg)) < 0 || binfo.if_magnify > 3) { fprintf(stderr, "Invalid magnify method %d\n", binfo.if_magnify); exit(1); } break; case 'c': binfo.if_comment = 1; break; case 'C': if (optarg == NULL) binfo.conv_format = DEFAULT_CONVERT_FORMAT; else binfo.conv_format = strdup(optarg); binfo.if_convert = 1; break; case 'W': binfo.if_warning = 1; break; case 'w': binfo.if_wallpaper = 1; break; case 's': binfo.x_threshold = binfo.y_threshold = 0; break; case 'S': if_sort = 0; break; case 'x': binfo.x_threshold = atoi(optarg); break; case 'y': binfo.y_threshold = atoi(optarg); break; case 'i': if ((info.index = atoi(optarg) - 1) < 0) { fprintf(stderr, "Invalid index %d\n", info.index); exit(1); } break; case 'X': if (exclude == NULL) if ((exclude = dlist_create()) == NULL) { fprintf(stderr, "dlist_create() failed\n"); exit(1); } dlist_add(exclude, optarg); break; case 'p': if (!plugin_scan_directory(info.pluginlist, optarg)) fprintf(stderr, "main: plugin_scan_directory %s: returns 0\n", optarg); break; case 'h': usage(&info); exit(0); case 'v': title(); exit(0); case 'P': if (optarg == NULL) show_info = 1; else if (!strcmp(optarg, "P")) show_info = 2; else if (!strcmp(optarg, "D")) disable_default = 1; else { usage(&info); exit(1); } break; default: usage(&info); exit(1); } } #ifdef PLUGIN_SEARCH_PATH if (!disable_default) if (!plugin_scan_directory(info.pluginlist, PLUGIN_SEARCH_PATH)) fprintf(stderr, "main: plugin_scan_directory %s: returns 0\n", PLUGIN_SEARCH_PATH); #endif if (show_info) { fprintf(stderr, "Loader:\n"); list_plugins(info.pluginlist, _Loader, show_info); fprintf(stderr, "Saver:\n"); list_plugins(info.pluginlist, _Saver, show_info); fprintf(stderr, "Archiver:\n"); list_plugins(info.pluginlist, _Archiver, show_info); exit(0); } if (optind == argc) { usage(&info); if (info.pluginlist != NULL) dlist_destroy(info.pluginlist, 1); exit(1); } /* set filenames to dual-list */ if ((p = dlist_create()) == NULL) { fprintf(stderr, "Cannot create Dlist\n"); exit(1); } while (optind < argc) add_to_dlist(p, argv[optind++], exclude); if (exclude) dlist_destroy(exclude, 0); /* sort it */ if (if_sort) sort_filenames(p); if (!binfo.if_convert) { initialize(&info, &binfo); #if defined(SHAPE) && defined(DEBUG) { int major, minor; XShapeQueryVersion(info.disp, &major, &minor); fprintf(stderr, "Shape extension version %d.%d\n", major, minor); } #endif browseaction_initialize(binfo.act_hash); } main_loop(p, &info, &binfo); dlist_destroy(p, 1); if (info.pluginlist != NULL) if (!plugin_destroy(info.pluginlist)) fprintf(stderr, "plugin_destroy failed... Ok, anyway this process will be finish soon.\n"); return 0; }