gint collection_load_only_geometry(CollectionData *cd, const gchar *path) { gchar s_buf[2048]; FILE *f; gchar *pathl; if (!path && !cd->path) return FALSE; if (!path) path = cd->path; /* load it */ pathl = path_from_utf8(path); f = fopen(pathl, "r"); g_free(pathl); if (!f) return FALSE; while (fgets(s_buf, sizeof(s_buf), f)) { if (s_buf[0]=='#' && strncmp(s_buf, "#geometry:", 10 ) == 0 && scan_geometry(s_buf + 10, &cd->window_x, &cd->window_y, &cd->window_w, &cd->window_h) ) { cd->window_read = TRUE; fclose(f); return TRUE; } } fclose(f); return FALSE; }
static gint collection_load_private(CollectionData *cd, const gchar *path, gint append, gint flush) { gchar s_buf[2048]; FILE *f; gchar *pathl; gint official = FALSE; gint success = TRUE; guint total = 0; guint fail = 0; collection_load_stop(cd); if (flush) collect_manager_flush(); if (!append) { collection_list_free(cd->list); cd->list = NULL; } if (!path && !cd->path) return FALSE; if (!path) path = cd->path; /* load it */ pathl = path_from_utf8(path); f = fopen(pathl, "r"); g_free(pathl); if (!f) { printf("Failed to open collection file: \"%s\"\n", path); return FALSE; } while (fgets(s_buf, sizeof(s_buf), f)) { gchar *buf; if (s_buf[0]=='#') { if (strncasecmp(s_buf, GQVIEW_COLLECTION_MARKER, strlen(GQVIEW_COLLECTION_MARKER)) == 0) { /* Looks like an official collection, allow unchecked input. * All this does is allow adding files that may not exist, * which is needed for the collection manager to work. * Also unofficial files abort after too many invalid entries. */ official = TRUE; } else if (strncmp(s_buf, "#geometry:", 10 ) == 0 && scan_geometry(s_buf + 10, &cd->window_x, &cd->window_y, &cd->window_w, &cd->window_h) ) { cd->window_read = TRUE; } continue; } if (s_buf[0]=='\n') continue; buf = quoted_value(s_buf); if (buf) { gint valid; valid = (buf[0] == '/' && collection_add_check(cd, buf, FALSE, flush)); g_free(buf); total++; if (!valid && !official) { fail++; if (fail > GQVIEW_COLLECTION_FAIL_MIN && fail * 100 / total > GQVIEW_COLLECTION_FAIL_PERCENT) { printf("Too many invalid filenames in unoffical collection file, closing: %s\n", path); success = FALSE; break; } } } } fclose(f); cd->list = collection_list_sort(cd->list, cd->sort_method); if (!append) cd->changed = FALSE; return success; }
static gboolean collection_load_private(CollectionData *cd, const gchar *path, CollectionLoadFlags flags) { gchar s_buf[GQ_COLLECTION_READ_BUFSIZE]; FILE *f; gchar *pathl; gboolean limit_failures = TRUE; gboolean success = TRUE; gboolean has_official_header = FALSE; gboolean has_geometry_header = FALSE; gboolean has_gqview_header = FALSE; gboolean need_header = TRUE; guint total = 0; guint fail = 0; gboolean changed = FALSE; CollectManagerEntry *entry = NULL; guint flush = !!(flags & COLLECTION_LOAD_FLUSH); guint append = !!(flags & COLLECTION_LOAD_APPEND); guint only_geometry = !!(flags & COLLECTION_LOAD_GEOMETRY); if (!only_geometry) { collection_load_stop(cd); if (flush) collect_manager_flush(); else entry = collect_manager_get_entry(path); if (!append) { collection_list_free(cd->list); cd->list = NULL; } } if (!path && !cd->path) return FALSE; if (!path) path = cd->path; DEBUG_1("collection load: append=%d flush=%d only_geometry=%d path=%s", append, flush, only_geometry, path); /* load it */ pathl = path_from_utf8(path); f = fopen(pathl, "r"); g_free(pathl); if (!f) { log_printf("Failed to open collection file: \"%s\"\n", path); return FALSE; } while (fgets(s_buf, sizeof(s_buf), f)) { gchar *buf; gchar *p = s_buf; /* Skip whitespaces and empty lines */ while (*p && g_ascii_isspace(*p)) p++; if (*p == '\n' || *p == '\r') continue; /* Parse comments */ if (*p == '#') { if (!need_header) continue; if (g_ascii_strncasecmp(p, GQ_COLLECTION_MARKER, strlen(GQ_COLLECTION_MARKER)) == 0) { /* Looks like an official collection, allow unchecked input. * All this does is allow adding files that may not exist, * which is needed for the collection manager to work. * Also unofficial files abort after too many invalid entries. */ has_official_header = TRUE; limit_failures = FALSE; } else if (strncmp(p, "#geometry:", 10 ) == 0 && scan_geometry(p + 10, &cd->window_x, &cd->window_y, &cd->window_w, &cd->window_h)) { has_geometry_header = TRUE; cd->window_read = TRUE; if (only_geometry) break; } else if (g_ascii_strncasecmp(p, "#GQview collection", strlen("#GQview collection")) == 0) { /* As 2008/04/15 there is no difference between our collection file format * and GQview 2.1.5 collection file format so ignore failures as well. */ has_gqview_header = TRUE; limit_failures = FALSE; } need_header = (!has_official_header && !has_gqview_header) || !has_geometry_header; continue; } if (only_geometry) continue; /* Read filenames */ while (*p && *p != '"') p++; if (*p) p++; buf = p; while (*p && *p != '"') p++; *p = 0; if (*buf) { gboolean valid; if (!flush) changed |= collect_manager_process_action(entry, &buf); valid = (buf[0] == G_DIR_SEPARATOR && collection_add_check(cd, file_data_new_simple(buf), FALSE, TRUE)); if (!valid) DEBUG_1("collection invalid file: %s", buf); total++; if (!valid) { fail++; if (limit_failures && fail > GQ_COLLECTION_FAIL_MIN && fail * 100 / total > GQ_COLLECTION_FAIL_PERCENT) { log_printf("%d invalid filenames in unofficial collection file, closing: %s\n", fail, path); success = FALSE; break; } } } } DEBUG_1("collection files: total = %d fail = %d official=%d gqview=%d geometry=%d", total, fail, has_official_header, has_gqview_header, has_geometry_header); fclose(f); if (only_geometry) return has_geometry_header; if (!flush) { gchar *buf = NULL; while (collect_manager_process_action(entry, &buf)) { collection_add_check(cd, file_data_new_simple(buf), FALSE, TRUE); changed = TRUE; g_free(buf); buf = NULL; } } cd->list = collection_list_sort(cd->list, cd->sort_method); if (!flush && changed && success) collection_save_private(cd, path); if (!flush) collect_manager_entry_reset(entry); if (!append) cd->changed = FALSE; return success; }
int InitDisplayW(int argc, char **argv) { rgb_t color; struct timeval time; if (magstep < 1) { magstep = 1; } if (magstep > maxsize) { fprintf(stderr, "Warning: Enlargement factor %d is too large!\n", magstep); magstep = maxsize; } if (!(serverW = w_init())) { fprintf(stderr, "%s: W initialization failed\n", argv[0]); exit(EXIT_FAILURE); } bpp = serverW->planes; if (verbose) fprintf(stderr, "W: %dbpp [%s, %s] %d shared colors, %s blocks\n", bpp, (serverW->type == BM_PACKEDCOLORMONO) ? "bitplanes, monochrome" : (serverW->type == BM_PACKEDMONO) ? "1 bitplane, monochrome" : (serverW->type == BM_DIRECT8) ? "8-bit chunky (direct) colors" : (serverW->type == BM_PACKEDCOLOR) ? "bitplanes, colors settable" : (serverW->type == BM_DIRECT24) ? "24-bit (rgb) image" : "unknown scheme", (indexedcolor && (bpp > 1)) ? "dynamic" : "static", serverW->sharedcolors, (serverW->flags & WSERVER_SHM) ? "shared" : "normal"); if (renderer_config.inroot) { width = serverW->width; height = serverW->height; x0 = 0; y0 = 0; winW = WROOT; } else { WFONT *fontW; width = UNDEF; height = UNDEF; x0 = UNDEF; y0 = UNDEF; if (renderer_config.geometry) { char *s; int c; /* this little hack converts "NxM" geometry specs into "N,M" */ for (s = renderer_config.geometry; (c = *s); s++) if ((c == 'x') || (c == ',')) { *s = ','; break; } scan_geometry(renderer_config.geometry, &width, &height, &x0, &y0); } if ((width == UNDEF) || !width) width = 256 * magstep; if ((height == UNDEF) || !height) height = 240 * magstep; if (!(fontW = w_loadfont("lucidat", 13, F_BOLD))) if (!(fontW = w_loadfont(0, 0, 0))) { w_exit(); fprintf(stderr, "%s: failed to acquire font\n", argv[0]); exit(EXIT_FAILURE); } if ((iconW = w_create(w_strlen(fontW, PACKAGE) + 4, fontW->height + 2, W_MOVE | EV_MOUSE | EV_KEYS | EV_MODIFIERS))) { w_setfont(iconW, fontW); w_printstring(iconW, 2, 1, PACKAGE); } w_unloadfont(fontW); if (!(winW = w_create(width, height, W_MOVE | W_TITLE | W_CLOSE | (iconW ? W_ICON : 0) | W_RESIZE | EV_KEYS | EV_MODIFIERS))) { if (iconW) w_delete(iconW); w_exit(); fprintf(stderr, "%s: failed to create window\n", argv[0]); exit(EXIT_FAILURE); } w_settitle(winW, PACKAGE_NAME); limit2screen(winW, &x0, &y0); } /* Allocate rendering bitmap */ if (!(bitmapW = w_allocbm(256 * magstep, 240 * magstep, serverW->type, ((serverW->type == BM_DIRECT8) || (serverW->type == BM_PACKEDCOLOR)) ? 1 << serverW->planes : 0))) { w_delete(winW); if (iconW) w_delete(iconW); w_exit(); fprintf(stderr, "%s: failed to allocate bitmap\n", argv[0]); exit(EXIT_FAILURE); } /* Allocate black and white pixels */ for (blackpixel = 0; blackpixel < serverW->sharedcolors; blackpixel++) if (!w_getColor(winW, blackpixel, &(color.red), &(color.green), &(color.blue))) { if ((color.red <= 0x0FU) && (color.green <= 0x0FU) && (color.blue <= 0x0FU)) break; } if (blackpixel == serverW->sharedcolors) blackpixel = w_allocColor(winW, 0x00U, 0x00U, 0x00U); if (blackpixel < 0) { if (bpp > 1) fprintf(stderr, "W: can't allocate black pixel -- using index 1\n"); blackpixel = 1; } color.red = color.green = color.blue = 0x00U; if (bitmapW->palette) bitmapW->palette[blackpixel] = color; for (whitepixel = 0; whitepixel < serverW->sharedcolors; whitepixel++) if (!w_getColor(winW, whitepixel, &(color.red), &(color.green), &(color.blue))) { if ((color.red >= 0xF0U) && (color.green >= 0xF0U) && (color.blue >= 0xF0U)) break; } if (whitepixel == serverW->sharedcolors) whitepixel = w_allocColor(winW, 0xFFU, 0xFFU, 0xFFU); if (whitepixel < 0) { if (bpp > 1) fprintf(stderr, "W: can't allocate white pixel -- using index 0\n"); whitepixel = 0; } color.red = color.green = color.blue = 0xFFU; if (bitmapW->palette) bitmapW->palette[whitepixel] = color; for (int x=0; x < 24; x++) { palette[x] = palette2[x] = whitepixel; } if (indexedcolor && (bpp > 1)) { /* Pre-initialize the colormap to known values */ oldbgcolor = currentbgcolor = NES_palette[0]; color.red = ((NES_palette[0] & 0xFF0000) >> 16); color.green = ((NES_palette[0] & 0xFF00) >> 8); color.blue = (NES_palette[0] & 0xFF); for (int x = 0; x <= 24; x++) { palette[x] = w_allocColor(winW, color.red, color.green, color.blue); palette2[x] = blackpixel; if (palette[x] < 0) { if (winW != WROOT) w_delete(winW); if (iconW) w_delete(iconW); w_exit(); fprintf(stderr, "Can't allocate colors!\n"); exit(EXIT_FAILURE); } else if (bitmapW->palette) bitmapW->palette[palette[x]] = color; } if (scanlines && (scanlines != 100)) { fprintf(stderr, "Warning: Scanline intensity is ignored in indexed-color modes!\n"); scanlines = 0; } } else /* truecolor */ {