/* scivi - visualization plugin for XMMS * Copyright (C) 2003 Vitaly V. Bursov * * This program 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. * * This program 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 */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include #include #include #include #include #include "common.h" #include "utilf.h" static char **split_path(char *p) { #ifdef PRESETSDIR static const char *PRESETSDIR_scivi = PRESETSDIR; #endif glob_t gr; char **tmp; void *rel; int tmplen; int tmpptr; int lstart; int pos; int plen; int i, l; if (!p) tmplen = 3; else tmplen = 256; l = sizeof(char*)*tmplen; tmp = (char**)malloc(l); if (tmp == NULL){ eprintf("Failed to allocate %d bytes\n", l); return NULL; } lstart = 0; pos = 0; tmpptr = 0; /* First add default paths */ #ifdef PRESETSDIR l = strlen(PRESETSDIR_scivi)+1; tmp[tmpptr] = malloc(l); if (tmp[tmpptr] == NULL){ eprintf("Failed to allocate %d bytes\n",l); free(tmp); return NULL; } memcpy(tmp[tmpptr], PRESETSDIR_scivi, l); tmpptr++; #endif if (glob("~", GLOB_TILDE, NULL, &gr) == 0){ static const char *scivid = "/.scivi"; struct stat ddstat; char *t; int i; if (gr.gl_pathc > 0){ l = strlen(gr.gl_pathv[0]); t = malloc(l+strlen(scivid)+1); if (t == NULL){ eprintf("Failed to allocate %d bytes\n",l); return tmp; } memcpy(t, gr.gl_pathv[0], l); memcpy(t+l, scivid, strlen(scivid)+1); if (stat(t, &ddstat) != 0) if (errno == ENOENT){ if (mkdir(t, S_IRWXU)) eprintf("Can not create directory '%s'\n", t); } free(t); } globfree(&gr); } if (glob("~/.scivi/presets", GLOB_TILDE, NULL, &gr) == 0){ int i; if (gr.gl_pathc > 0){ l = strlen(gr.gl_pathv[0])+1; tmp[tmpptr] = malloc(l); if (tmp[tmpptr] == NULL){ eprintf("Failed to allocate %d bytes\n",l); tmp[tmpptr] = NULL; return tmp; } memcpy(tmp[tmpptr], gr.gl_pathv[0], l); tmpptr++; } globfree(&gr); } if (!p){ tmp[tmpptr] = NULL; return tmp; } while (1){ if ((p[pos] == ':') || (p[pos] == '\0')){ plen = pos - lstart; if (plen > 0){ tmp[tmpptr] = malloc(plen+1); if (tmp[tmpptr] == NULL){ eprintf("Failed to allocate %d bytes\n",plen+1); break; } memcpy(tmp[tmpptr], p+lstart, plen); tmp[tmpptr][plen] = '\0'; /*dprintf("read string: >%s<\n",tmp[tmpptr]);*/ tmpptr++; /* "-1" leaves space for NULL terminator */ if (tmpptr == tmplen-1){ tmplen += 256; rel = realloc(tmp,sizeof(char*)*tmplen); if (rel == NULL){ eprintf("Failed to reallocate %d bytes\n",sizeof(char*)*tmplen); break; } tmp = rel; } } lstart = pos+1; } if (p[pos] == '\0'){ break; } pos++; } /* remove duplicated entries */ for (i=0;i%s<\n", tmp[i]); } #endif /* decrease block size */ rel = realloc(tmp,sizeof(char*)*(tmpptr+1)); if (rel == NULL) eprintf("Failed to reallocate %d bytes\n",sizeof(char*)*(tmpptr+1)); else tmp = rel; return tmp; } static void free_dirs_list(char **p) { int i; if (!p) return; for (i=0;p[i];i++) free(p[i]); free(p); } void scivi_plugin_read_config(Scivi *scivi) { glob_t gr; ConfigFile *cfg; gchar *presets_dir = NULL; gchar *font_filename = NULL; memset(&gr, 0, sizeof(glob_t)); if (glob("~/.scivi/config", GLOB_TILDE, NULL, &gr) == 0){ int i; if (gr.gl_pathc > 0){ cfg = xmms_cfg_open_file(gr.gl_pathv[0]); if (cfg){ xmms_cfg_read_string(cfg, "main", "presets_dir", &presets_dir); xmms_cfg_read_string(cfg, "main", "font_filename", &font_filename); xmms_cfg_read_int(cfg, "main", "fps_limit_method", &scivi->fps_limit_method); xmms_cfg_read_int(cfg, "main", "max_fps", &scivi->max_fps); xmms_cfg_read_boolean(cfg, "main", "vsync_enable", &scivi->vsync_enable); xmms_cfg_read_int(cfg, "main", "width", &scivi->width); xmms_cfg_read_int(cfg, "main", "height", &scivi->height); xmms_cfg_read_int(cfg, "main", "tex_width", &scivi->pbuf_want_w); xmms_cfg_read_int(cfg, "main", "tex_height", &scivi->pbuf_want_h); xmms_cfg_free(cfg); if ((scivi->max_fps<0) || (scivi->max_fps>1000)) scivi->max_fps = 0; if ((scivi->width<80) || (scivi->width>10240)) scivi->width = 640; if ((scivi->height<80) || (scivi->height>10240)) scivi->height = 480; } } globfree(&gr); } if (scivi->preset_dirs) free_dirs_list(scivi->preset_dirs); scivi->preset_dirs = split_path(presets_dir); g_free(presets_dir); if (font_filename){ int l = strlen(font_filename)+1; if (scivi->font_filename) free(scivi->font_filename); scivi->font_filename = (char*)malloc(l); if (scivi->font_filename) strcpy(scivi->font_filename, font_filename); else eprintf("Failed to allocate %d bytes\n",l); g_free(font_filename); } else { scivi->font_filename = NULL; } } void scivi_plugin_save_config(Scivi *scivi) { glob_t gr; ConfigFile *cfg; memset(&gr, 0, sizeof(glob_t)); if (glob("~/.scivi", GLOB_TILDE, NULL, &gr) == 0){ int i; gchar *pb; if (gr.gl_pathc > 0){ char *conffile; conffile = malloc(strlen(gr.gl_pathv[0])+7+1); if (conffile == NULL){ eprintf("Failed to allocate %d bytes\n", strlen(gr.gl_pathv[0])+7+1); globfree(&gr); return; } strcpy(conffile, gr.gl_pathv[0]); strcat(conffile, "/config"); dprintf("Saving config to file '%s'\n", conffile); cfg = xmms_cfg_open_file(conffile); if (cfg == NULL){ cfg = xmms_cfg_new(); } if (cfg){ pb = g_strjoinv(":", scivi->preset_dirs); xmms_cfg_write_string(cfg, "main", "presets_dir", pb); g_free(pb); /*xmms_cfg_write_string(cfg, "main", "font_filename", &font_filename);*/ xmms_cfg_write_int(cfg, "main", "fps_limit_method", scivi->fps_limit_method); xmms_cfg_write_int(cfg, "main", "max_fps", scivi->max_fps); xmms_cfg_write_boolean(cfg, "main", "vsync_enable", scivi->vsync_enable); xmms_cfg_write_int(cfg, "main", "width", scivi->width); xmms_cfg_write_int(cfg, "main", "height", scivi->height); xmms_cfg_write_int(cfg, "main", "tex_width", scivi->pbuf_want_w); xmms_cfg_write_int(cfg, "main", "tex_height", scivi->pbuf_want_h); xmms_cfg_write_file(cfg, conffile); xmms_cfg_free(cfg); } else Xprintf("failed to open/create config file '%s'\n", conffile); free(conffile); } globfree(&gr); } } void scivi_plugin_read_config_finit(Scivi *scivi) { if (scivi->preset_dirs){ free_dirs_list(scivi->preset_dirs); scivi->preset_dirs = NULL; } if (scivi->font_filename) free(scivi->font_filename); }