From e3a971194c2441bf9d9c82c5f5f6b7d6fca26eaa Mon Sep 17 00:00:00 2001 From: Albert Cahalan Date: Mon, 23 Nov 2009 07:45:24 +0000 Subject: [PATCH] options working fairly well --- Makefile | 2 +- src/dirwalk.c | 10 ++-- src/dirwalk.h | 6 +- src/fonts.c | 6 +- src/fonts.h | 2 +- src/get_fname.c | 59 +++++-------------- src/get_fname.h | 4 +- src/parse.gperf | 12 ++-- src/parse.h | 2 +- src/postscript_print.c | 9 ++- src/postscript_print.h | 6 +- src/tuxpaint.c | 129 ++++++++++++++++++++++++++++++++++------- 12 files changed, 152 insertions(+), 95 deletions(-) diff --git a/Makefile b/Makefile index fddc28859..45e22d7ab 100644 --- a/Makefile +++ b/Makefile @@ -1033,7 +1033,7 @@ PLUGIN_LIBS:=$($(OS)_PLUGIN_LIBS) #MAGIC_CFLAGS:=-g3 -O2 -fvisibility=hidden -fno-common -W -Wstrict-prototypes -Wmissing-prototypes -Wall $(MAGIC_SDL_CPPFLAGS) -Isrc/ MAGIC_CFLAGS:=-g3 -O2 -fno-common -W -Wstrict-prototypes -Wmissing-prototypes -Wall $(MAGIC_SDL_CPPFLAGS) -Isrc/ -SHARED_FLAGS:=-shared -fpic +SHARED_FLAGS:=-shared -fpic -Wl,--warn-shared-textrel MAGIC_C:=$(wildcard magic/src/*.c) MAGIC_SO:=$(patsubst magic/src/%.c,magic/%.$(SO_TYPE),$(MAGIC_C)) diff --git a/src/dirwalk.c b/src/dirwalk.c index fc34504d7..5080e1c69 100644 --- a/src/dirwalk.c +++ b/src/dirwalk.c @@ -68,7 +68,7 @@ ///////////////// directory walking callers and callbacks ////////////////// void loadfont_callback(SDL_Surface * screen, const char *restrict const dir, - unsigned dirlen, tp_ftw_str * files, unsigned i, char * locale) + unsigned dirlen, tp_ftw_str * files, unsigned i, const char *restrict const locale) { dirlen = dirlen; @@ -107,8 +107,8 @@ void loadfont_callback(SDL_Surface * screen, const char *restrict const dir, char fname[512]; TuxPaint_Font *font; snprintf(fname, sizeof fname, "%s/%s", dir, files[i].str); -/* printf("Loading font: %s (locale is: %s)\n", fname, (locale != NULL ? locale : "NULL")); */ - if (locale != NULL && strstr(fname, "locale") != NULL && all_locale_fonts == 0) +/* printf("Loading font: %s (locale is: %s)\n", fname, (locale ? locale : "NULL")); */ + if (locale && strstr(fname, "locale") && !all_locale_fonts) { char fname_check[512]; /* We're (probably) loading from our locale fonts folder; ONLY load our locale's font */ @@ -261,8 +261,8 @@ void tp_ftw(SDL_Surface * screen, char *restrict const dir, unsigned dirlen, int rsrc, void (*fn) (SDL_Surface * screen, const char *restrict const dir, unsigned dirlen, tp_ftw_str * files, - unsigned count, char * locale), - char * locale) + unsigned count, const char *restrict const locale), + const char *restrict const locale) { DIR *d; unsigned num_file_names = 0; diff --git a/src/dirwalk.h b/src/dirwalk.h index 5b54f827c..ad1bdfd39 100644 --- a/src/dirwalk.h +++ b/src/dirwalk.h @@ -46,13 +46,13 @@ typedef struct tp_ftw_str void loadfont_callback(SDL_Surface * screen, const char *restrict const dir, - unsigned dirlen, tp_ftw_str * files, unsigned i, char * locale); + unsigned dirlen, tp_ftw_str * files, unsigned i, const char *restrict const locale); int compare_ftw_str(const void *v1, const void *v2); void tp_ftw(SDL_Surface * screen, char *restrict const dir, unsigned dirlen, int rsrc, void (*fn) (SDL_Surface * screen, const char *restrict const dir, unsigned dirlen, tp_ftw_str * files, - unsigned count, char * locale), - char * locale); + unsigned count, const char *restrict const locale), + const char *restrict const locale); #endif diff --git a/src/fonts.c b/src/fonts.c index d87bc3744..9fc6fe765 100644 --- a/src/fonts.c +++ b/src/fonts.c @@ -920,7 +920,7 @@ static void groupfonts(void) } -static void loadfonts_locale_filter(SDL_Surface * screen, const char *const dir, const char *restrict locale) +static void loadfonts_locale_filter(SDL_Surface * screen, const char *const dir, const char *restrict const locale) { char buf[TP_FTW_PATHSIZE]; unsigned dirlen = strlen(dir); @@ -935,7 +935,7 @@ static void loadfonts(SDL_Surface * screen, const char *const dir) } -static int load_user_fonts(SDL_Surface * screen, void *vp, const char *restrict locale) +static int load_user_fonts(SDL_Surface * screen, void *vp, const char *restrict const locale) { char *homedirdir; @@ -997,7 +997,7 @@ static int load_user_fonts(SDL_Surface * screen, void *vp, const char *restrict -void run_font_scanner(SDL_Surface * screen, const char *restrict locale) +void run_font_scanner(SDL_Surface * screen, const char *restrict const locale) { int sv[2]; int size, i; diff --git a/src/fonts.h b/src/fonts.h index 5b059a201..b8918eb2f 100644 --- a/src/fonts.h +++ b/src/fonts.h @@ -106,7 +106,7 @@ int TuxPaint_Font_FontHeight(TuxPaint_Font * tpf); #ifdef FORKED_FONTS void reliable_write(int fd, const void *buf, size_t count); -void run_font_scanner(SDL_Surface * screen, const char *restrict locale); +void run_font_scanner(SDL_Surface * screen, const char *restrict const locale); void receive_some_font_info(SDL_Surface * screen); #endif diff --git a/src/get_fname.c b/src/get_fname.c index 880dc3b04..c7f64bea9 100644 --- a/src/get_fname.c +++ b/src/get_fname.c @@ -28,18 +28,7 @@ #include "get_fname.h" #include "debug.h" - -char *savedir; -char *datadir; - - -/* The filename for the current image: */ - -char *get_fname(const char *const name, int kind) -{ - char f[512]; - char * dir; - +#include "compiler.h" /* DIR_SAVE: Where is the user's saved directory? This is where their saved files are stored @@ -58,43 +47,23 @@ char *get_fname(const char *const name, int kind) DIR_DATA: Where is the user's data directory? This is where local fonts, brushes and stamps can be found. */ - if (kind == DIR_SAVE) - dir = savedir; - else if (kind == DIR_DATA) - dir = datadir; - else - return NULL; +const char *savedir; +const char *datadir; -#ifdef WIN32 - snprintf(f, sizeof(f), "%s/%s", dir, name); -#elif __BEOS__ - if (*name == '\0') - strcpy(f, dir); - else - snprintf(f, sizeof(f), "%s/%s", dir, name); -#endif +// FIXME: We shouldn't be allocating memory all the time. +// There should be distinct functions for each directory. +// There should be distinct functions for each thread, +// for caller-provided space, and maybe callee strdup. +// That's at most 4 functions per Tux Paint thread. +char *get_fname(const char *const name, int kind) +{ + char f[512]; + const char *restrict const dir = (kind==DIR_SAVE) ? savedir : datadir; - /* Put together home directory path + settings directory + filename... */ - - if (dir == NULL) - { - fprintf(stderr, "Warning: get_fname() has a NULL dir...!?\n"); - return strdup(name);; - } - - if (*name != '\0') - { - /* (Some mkdir()'s don't like trailing slashes) */ - - snprintf(f, sizeof(f), "%s/%s", dir, name); - } - else - { - snprintf(f, sizeof(f), "%s", dir); - } + // Some mkdir()'s don't like trailing slashes + snprintf(f, sizeof(f), "%s%c%s", dir, (*name)?'/':'\0', name); return strdup(f); } - diff --git a/src/get_fname.h b/src/get_fname.h index 6d013a736..bf04e9369 100644 --- a/src/get_fname.h +++ b/src/get_fname.h @@ -25,8 +25,8 @@ #ifndef GET_FNAME_H #define GET_FNAME_H -extern char *savedir; -extern char *datadir; +extern const char *savedir; +extern const char *datadir; enum { DIR_SAVE, diff --git a/src/parse.gperf b/src/parse.gperf index 386376cbc..223ed8d4b 100644 --- a/src/parse.gperf +++ b/src/parse.gperf @@ -14,7 +14,7 @@ #include #include -static const char PARSE_YES[] = "yes"; +const char PARSE_YES[] = "yes"; const char PARSE_NO[] = "no"; const char PARSE_CLOBBER[] = ":-("; // for painful lang/locale priority situation @@ -27,13 +27,13 @@ struct cfg #define MULTIVAL 0x00000000 #define POS 0x00000001 #define NEG 0x00000002 -#define BOOL (POS|NEG) +#define BOOLMASK (POS|NEG) #define BITS 2 // if this grows past 2, must shift the offset #define FLAGMASK ((1<val : 0; unsigned flags = (uintptr #include #include "SDL.h" +#include "compiler.h" /* Method for printing images: */ @@ -78,10 +79,9 @@ #ifdef PRINTMETHOD_PS int do_ps_save(FILE * fi, - // const char *restrict const fname, - const char *fname, + const char *restrict const fname, SDL_Surface * surf, - char * pprsize, + const char *restrict pprsize, int is_pipe); #endif diff --git a/src/tuxpaint.c b/src/tuxpaint.c index aeecd4c46..f054d9c9d 100644 --- a/src/tuxpaint.c +++ b/src/tuxpaint.c @@ -1107,7 +1107,7 @@ static magic_api *magic_api_struct; /* Pointer to our internal functions; passed #endif static const char *printcommand = PRINTCOMMAND; static const char *altprintcommand = ALTPRINTCOMMAND; -char *papersize = NULL; +static const char *papersize; #endif @@ -5657,7 +5657,7 @@ static unsigned compute_default_scale_factor(double ratio) static void loadbrush_callback(SDL_Surface * screen, const char *restrict const dir, unsigned dirlen, tp_ftw_str * files, - unsigned i, char * locale) + unsigned i, const char *restrict const locale) { FILE * fi; char buf[64]; @@ -5745,7 +5745,7 @@ static void loadbrush_callback(SDL_Surface * screen, -static void load_brush_dir(SDL_Surface * screen, const char *const dir) +static void load_brush_dir(SDL_Surface * screen, const char *restrict const dir) { char buf[TP_FTW_PATHSIZE]; unsigned dirlen = strlen(dir); @@ -6489,7 +6489,7 @@ static void get_stamp_thumb(stamp_type * sd) static void loadstamp_callback(SDL_Surface * screen, const char *restrict const dir, unsigned dirlen, tp_ftw_str * files, - unsigned i, char * locale) + unsigned i, const char *restrict const locale) { (void)locale; #ifdef DEBUG @@ -11307,8 +11307,8 @@ static void cleanup(void) } #if !defined(WIN32) && !defined(__APPLE__) && !defined(__BEOS__) - if (papersize != NULL) - free(papersize); +// if (papersize != NULL) +// free(papersize); #endif @@ -14593,15 +14593,15 @@ void do_print(void) SDL_BlitSurface(label, NULL, save_canvas, NULL); #if !defined(WIN32) && !defined(__BEOS__) && !defined(__APPLE__) - char *pcmd; + const char *pcmd; FILE *pi; /* Linux, Unix, etc. */ if (want_alt_printcommand && !fullscreen) - pcmd = (char *) altprintcommand; + pcmd = altprintcommand; else - pcmd = (char *) printcommand; + pcmd = printcommand; pi = popen(pcmd, "w"); @@ -18901,12 +18901,13 @@ static void undo_tmp_applied_text() ///////////////////////////////////////////////////////////////////////////// #if !defined(WIN32) && !defined(__APPLE__) && !defined(__BEOS__) -static void show_available_papersizes(FILE * fi, const char * prg) +static void show_available_papersizes(int exitcode) { + FILE *fi = exitcode ? stderr : stdout; const struct paper * ppr; int cnt; - fprintf(fi, "Usage: %s [--papersize PAPERSIZE]\n", prg); + fprintf(fi, "Usage: %s [--papersize PAPERSIZE]\n", progname); fprintf(fi, "\n"); fprintf(fi, "PAPERSIZE may be one of:\n"); @@ -19083,7 +19084,7 @@ static void setup_config(char *argv[]) #elif __APPLE__ savedir = strdup(macosx.preferencesPath); #else - asprintf(&savedir, "%s/%s", home, ".tuxpaint"); + asprintf((char**)&savedir, "%s/%s", home, ".tuxpaint"); #endif } @@ -19127,7 +19128,7 @@ static void setup_config(char *argv[]) if(tmpcfg.savedir) { - free(savedir); + free((char*)savedir); savedir = tmpcfg.savedir; } @@ -19139,19 +19140,107 @@ static void setup_config(char *argv[]) tmpcfg.parsertmp_locale = NULL; button_label_y_nudge = setup_i18n(tmpcfg.parsertmp_lang, tmpcfg.parsertmp_locale); -#if 0 -all_locale_fonts -no_system_fonts - -parsertmp_windowsize -parsertmp_fullscreen_native + // FIXME: most of this is not required before starting the font scanner if(tmpcfg_cmd.papersize && !strcmp(tmpcfg_cmd.papersize, "help")) show_available_papersizes(0); -#endif + +#define SETBOOL(x) do{ if(tmpcfg.x) x = (tmpcfg.x==PARSE_YES); }while(0) + SETBOOL(all_locale_fonts); + SETBOOL(autosave_on_quit); + SETBOOL(disable_label); + SETBOOL(disable_magic_controls); + SETBOOL(disable_print); + SETBOOL(disable_quit); + SETBOOL(disable_save); + SETBOOL(disable_screensaver); + SETBOOL(disable_stamp_controls); + SETBOOL(dont_do_xor); + SETBOOL(dont_load_stamps); + SETBOOL(fullscreen); + SETBOOL(grab_input); + SETBOOL(hide_cursor); + SETBOOL(keymouse); + SETBOOL(mirrorstamps); + SETBOOL(native_screensize); + SETBOOL(no_button_distinction); + SETBOOL(no_fancy_cursors); + SETBOOL(no_system_fonts); + SETBOOL(noshortcuts); + SETBOOL(ok_to_use_lockfile); + SETBOOL(only_uppercase); + SETBOOL(simple_shapes); + SETBOOL(start_blank); + SETBOOL(use_print_config); + SETBOOL(use_sound); + SETBOOL(wheely); +#undef SETBOOL + + if(tmpcfg.parsertmp_windowsize) + { + char *endp1; + char *endp2; + int w = strtoul(tmpcfg.parsertmp_windowsize, &endp1, 10); + int h = strtoul(endp1 + 1, &endp2, 10); + if (tmpcfg.parsertmp_windowsize==endp1 || endp1+1==endp2 || *endp1!='x' || *endp2) + { + fprintf(stderr,"Window size '%s' is not understood.\n",tmpcfg.parsertmp_windowsize); + exit(97); + } + if (w<500 || w>32000 || h<480 || h>32000 || h>w*3 || w>h*4) + { + fprintf(stderr,"Window size '%s' is not reasonable.\n",tmpcfg.parsertmp_windowsize); + exit(93); + } + WINDOW_WIDTH = w; + WINDOW_HEIGHT = h; + } + if(tmpcfg.parsertmp_fullscreen_native) + { + // should conflict with other fullscreen/native_screensize setting? + if (!strcmp(tmpcfg.parsertmp_fullscreen_native, "native")) + native_screensize = 1; + fullscreen = strcmp(tmpcfg.parsertmp_fullscreen_native, "no"); + } + if(tmpcfg.stamp_size_override) + { + if (!strcmp(tmpcfg.stamp_size_override, "default")) + stamp_size_override = -1; + else + { + // FIXME: needs to be a scaling factor + stamp_size_override = atoi(tmpcfg.stamp_size_override); + if (stamp_size_override > 10) + stamp_size_override = 10; + } + } + // FIXME: make this dynamic (accelerometer or OLPC XO-1 rotation button) + if(tmpcfg.rotate_orientation) + rotate_orientation = !strcmp(tmpcfg.rotate_orientation, "portrait"); // alternative is "landscape" + if(tmpcfg.colorfile) + strcpy(colorfile, tmpcfg.colorfile); // FIXME can overflow + if(tmpcfg.print_delay) + print_delay = atoi(tmpcfg.print_delay); + if(tmpcfg.printcommand) + printcommand = tmpcfg.printcommand; + if(tmpcfg.altprintcommand) + altprintcommand = tmpcfg.altprintcommand; + if(tmpcfg.alt_print_command_default) + { + // FIXME: probably need extra variables + if (!strcmp(tmpcfg.alt_print_command_default, "always")) + alt_print_command_default = ALTPRINT_ALWAYS; + else if (!strcmp(tmpcfg.alt_print_command_default, "never")) + alt_print_command_default = ALTPRINT_NEVER; + else + alt_print_command_default = ALTPRINT_MOD; // default ("mod") + } + if(tmpcfg.papersize) + papersize = tmpcfg.papersize; } + static void chdir_to_binary(char *argv0) { #if defined(__BEOS__) || defined(WIN32)