From 6d02fe98f29bc10cb37d946b1d4474971dcce80d Mon Sep 17 00:00:00 2001 From: dan Date: Tue, 13 Apr 2004 03:49:38 +0000 Subject: [PATCH] - rewrote the font conversion routines to avoid the need to allocate memory for font options while parsing the xlfd. now all processing is done using the original xlfd string only. - removed memleaks introduced by previous commit. --- util/getstyle.c | 888 +++++++++++++++++--------------- util/setstyle.c | 1530 +++++++++++++++++++++++++++++-------------------------- 2 files changed, 1279 insertions(+), 1139 deletions(-) rewrite util/setstyle.c (65%) diff --git a/util/getstyle.c b/util/getstyle.c index 061a2065..e0c54625 100644 --- a/util/getstyle.c +++ b/util/getstyle.c @@ -1,9 +1,9 @@ /* getstyle.c - outputs style related options from WindowMaker to stdout * * WindowMaker window manager - * + * * Copyright (c) 1997-2003 Alfredo K. Kojima - * + * * 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 @@ -16,7 +16,7 @@ * * 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, + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, * USA. */ @@ -43,34 +43,34 @@ /* table of style related options */ static char *options[] = { - "TitleJustify", + "TitleJustify", "ClipTitleFont", - "WindowTitleFont", - "MenuTitleFont", - "MenuTextFont", + "WindowTitleFont", + "MenuTitleFont", + "MenuTextFont", "IconTitleFont", "DisplayFont", "LargeDisplayFont", "WindowTitleExtendSpace", "MenuTitleExtendSpace", "MenuTextExtendSpace", - "HighlightColor", + "HighlightColor", "HighlightTextColor", "ClipTitleColor", "CClipTitleColor", - "FTitleColor", - "PTitleColor", - "UTitleColor", - "FTitleBack", - "PTitleBack", + "FTitleColor", + "PTitleColor", + "UTitleColor", + "FTitleBack", + "PTitleBack", "UTitleBack", "ResizebarBack", - "MenuTitleColor", - "MenuTextColor", - "MenuDisabledColor", - "MenuTitleBack", + "MenuTitleColor", + "MenuTextColor", + "MenuDisabledColor", + "MenuTitleBack", "MenuTextBack", - "IconBack", + "IconBack", "IconTitleColor", "IconTitleBack", "MenuStyle", @@ -104,18 +104,18 @@ static char *theme_options[] = { /* table of style related fonts */ -static char *font_styles[] = { +static char *font_options[] = { "ClipTitleFont", - "WindowTitleFont", - "MenuTitleFont", - "MenuTextFont", + "WindowTitleFont", + "MenuTitleFont", + "MenuTextFont", "IconTitleFont", "DisplayFont", "LargeDisplayFont", NULL }; -char *default_font = "sans:pixelsize=12"; +#define DEFAULT_FONT "sans-serif:pixelsize=12" char *ProgName; @@ -156,18 +156,18 @@ defaultsPathForDomain(char *domain) gspath = getenv("GNUSTEP_USER_ROOT"); if (gspath) { - strcpy(path, gspath); - strcat(path, "/"); + strcpy(path, gspath); + strcat(path, "/"); } else { - char *home; - - home = getenv("HOME"); - if (!home) { - printf("%s:could not get HOME environment variable!\n", ProgName); - exit(0); - } - strcpy(path, home); - strcat(path, "/GNUstep/"); + char *home; + + home = getenv("HOME"); + if (!home) { + printf("%s:could not get HOME environment variable!\n", ProgName); + exit(0); + } + strcpy(path, home); + strcat(path, "/GNUstep/"); } strcat(path, DEFAULTS_DIR); strcat(path, "/"); @@ -185,12 +185,12 @@ abortar(char *reason) printf("%s: %s\n", ProgName, reason); if (ThemePath) { - printf("Removing unfinished theme pack\n"); - sprintf(buffer, "/bin/rm -fr \"%s\"", ThemePath); - - if (system(buffer)!=0) { - printf("%s: could not execute command %s\n", ProgName, buffer); - } + printf("Removing unfinished theme pack\n"); + sprintf(buffer, "/bin/rm -fr \"%s\"", ThemePath); + + if (system(buffer)!=0) { + printf("%s: could not execute command %s\n", ProgName, buffer); + } } exit(1); } @@ -205,74 +205,45 @@ wgethomedir() struct passwd *user; if (home) - return home; - + return home; + user = getpwuid(getuid()); if (!user) { - char buffer[80]; - - sprintf(buffer, "could not get password entry for UID %i", getuid()); - perror(buffer); - return "/"; + char buffer[80]; + + sprintf(buffer, "could not get password entry for UID %i", getuid()); + perror(buffer); + return "/"; } if (!user->pw_dir) { - return "/"; + return "/"; } else { - return user->pw_dir; + return user->pw_dir; } } -void* -mymalloc(int size) -{ - void *tmp; - - tmp = malloc(size); - if (!tmp) { - abortar("out of memory"); - } - - return tmp; -} - - -char* -mystrdup(char *str) -{ - char *tmp; - - tmp = mymalloc(strlen(str)+1); - - strcpy(tmp, str); - - return tmp; -} - - static char* getuserhomedir(char *username) { struct passwd *user; - + user = getpwnam(username); if (!user) { - char buffer[100]; + char buffer[100]; - sprintf(buffer,"could not get password entry for user %s", username); - perror(buffer); - return NULL; + sprintf(buffer,"could not get password entry for user %s", username); + perror(buffer); + return NULL; } if (!user->pw_dir) { - return "/"; + return "/"; } else { - return user->pw_dir; + return user->pw_dir; } } - - char* wexpandpath(char *path) { @@ -281,79 +252,79 @@ wexpandpath(char *path) int i; memset(buffer, 0, PATH_MAX+2); - + if (*path=='~') { - char *home; - - path++; - if (*path=='/' || *path==0) { - home = wgethomedir(); - strcat(buffer, home); - } else { - int j; - j = 0; - while (*path!=0 && *path!='/') { - buffer2[j++] = *path; - buffer2[j] = 0; - path++; - } - home = getuserhomedir(buffer2); - if (!home) - return NULL; - strcat(buffer, home); - } + char *home; + + path++; + if (*path=='/' || *path==0) { + home = wgethomedir(); + strcat(buffer, home); + } else { + int j; + j = 0; + while (*path!=0 && *path!='/') { + buffer2[j++] = *path; + buffer2[j] = 0; + path++; + } + home = getuserhomedir(buffer2); + if (!home) + return NULL; + strcat(buffer, home); + } } - + i = strlen(buffer); while (*path!=0) { - char *tmp; - - if (*path=='$') { - int j = 0; - path++; - /* expand $(HOME) or $HOME style environment variables */ - if (*path=='(') { - path++; - while (*path!=0 && *path!=')') { - buffer2[j++] = *(path++); - buffer2[j] = 0; + char *tmp; + + if (*path=='$') { + int j = 0; + path++; + /* expand $(HOME) or $HOME style environment variables */ + if (*path=='(') { + path++; + while (*path!=0 && *path!=')') { + buffer2[j++] = *(path++); + buffer2[j] = 0; } if (*path==')') path++; - tmp = getenv(buffer2); - if (!tmp) { - buffer[i] = 0; - strcat(buffer, "$("); - strcat(buffer, buffer2); - strcat(buffer, ")"); - i += strlen(buffer2)+3; - } else { - strcat(buffer, tmp); - i += strlen(tmp); - } - } else { - while (*path!=0 && *path!='/') { - buffer2[j++] = *(path++); - buffer2[j] = 0; - } - tmp = getenv(buffer2); - if (!tmp) { - strcat(buffer, "$"); - strcat(buffer, buffer2); - i += strlen(buffer2)+1; - } else { - strcat(buffer, tmp); - i += strlen(tmp); - } - } - } else { - buffer[i++] = *path; - path++; - } + tmp = getenv(buffer2); + if (!tmp) { + buffer[i] = 0; + strcat(buffer, "$("); + strcat(buffer, buffer2); + strcat(buffer, ")"); + i += strlen(buffer2)+3; + } else { + strcat(buffer, tmp); + i += strlen(tmp); + } + } else { + while (*path!=0 && *path!='/') { + buffer2[j++] = *(path++); + buffer2[j] = 0; + } + tmp = getenv(buffer2); + if (!tmp) { + strcat(buffer, "$"); + strcat(buffer, buffer2); + i += strlen(buffer2)+1; + } else { + strcat(buffer, tmp); + i += strlen(tmp); + } + } + } else { + buffer[i++] = *path; + path++; + } } - - return mystrdup(buffer); + + return wstrdup(buffer); } @@ -367,151 +338,244 @@ wfindfileinarray(WMPropList *paths, char *file) char *fullpath; if (!file) - return NULL; - - if (*file=='/' || *file=='~' || !paths || !WMIsPLArray(paths) - || WMGetPropListItemCount(paths)==0) { - if (access(file, R_OK)<0) { - fullpath = wexpandpath(file); - if (!fullpath) - return NULL; - - if (access(fullpath, R_OK)<0) { - free(fullpath); - return NULL; - } else { - return fullpath; - } - } else { - return mystrdup(file); - } + return NULL; + + if (*file=='/' || *file=='~' || !paths || !WMIsPLArray(paths) + || WMGetPropListItemCount(paths)==0) { + if (access(file, R_OK)<0) { + fullpath = wexpandpath(file); + if (!fullpath) + return NULL; + + if (access(fullpath, R_OK)<0) { + free(fullpath); + return NULL; + } else { + return fullpath; + } + } else { + return wstrdup(file); + } } flen = strlen(file); for (i=0; i < WMGetPropListItemCount(paths); i++) { - WMPropList *tmp; - char *dir; - - tmp = WMGetFromPLArray(paths, i); - if (!WMIsPLString(tmp) || !(dir = WMGetFromPLString(tmp))) - continue; - - len = strlen(dir); - path = mymalloc(len+flen+2); - path = memcpy(path, dir, len); - path[len]=0; - strcat(path, "/"); - strcat(path, file); - /* expand tilde */ - fullpath = wexpandpath(path); - free(path); - if (fullpath) { - /* check if file is readable */ - if (access(fullpath, R_OK)==0) { - return fullpath; - } - free(fullpath); - } + WMPropList *tmp; + char *dir; + + tmp = WMGetFromPLArray(paths, i); + if (!WMIsPLString(tmp) || !(dir = WMGetFromPLString(tmp))) + continue; + + len = strlen(dir); + path = wmalloc(len+flen+2); + path = memcpy(path, dir, len); + path[len]=0; + strcat(path, "/"); + strcat(path, file); + /* expand tilde */ + fullpath = wexpandpath(path); + free(path); + if (fullpath) { + /* check if file is readable */ + if (access(fullpath, R_OK)==0) { + return fullpath; + } + free(fullpath); + } } return NULL; } -char* -capitalize(char *element) + +static Bool +isFontOption(char *option) { - unsigned int first = 1; - char *p; - char *b; - b = element; - for (p = b; *p != 0; p++) - { - if (isalpha(*p) && first) { - first = 0; - *p = toupper(*p); - } - else if (*p == '-' || *p == ' ') { - first = 1; - } + int i; + + for (i=0; font_options[i]!=NULL; i++) { + if (strcasecmp(option, font_options[i])==0) { + return True; + } } - return b; + + return False; } -char* -getElementFromXLFD(const char *xlfd, int index) + +static int +countChar(char *str, char c) { - const char *p = xlfd; - while (*p != 0) { - if (*p == '-' && --index == 0) { - const char *end = strchr(p + 1, '-'); - char *buf; - size_t len; - if (end == 0) end = p + strlen(p); - len = end - (p + 1); - buf = wmalloc(len); - memcpy(buf, p + 1, len); - buf[len] = 0; - return buf; - } - p++; + int count = 0; + + if (!str) + return 0; + + for (; *str!=0; str++) { + if (*str == c) { + count++; + } } - return "*"; + + return count; } + +typedef struct str { + char *str; + int len; +} str; + +#define XLFD_TOKENS 14 + +static str* +getXLFDTokens(char *xlfd) +{ + static str tokens[XLFD_TOKENS]; + int i, len, size; + char *ptr; + + if (!xlfd || countChar(xlfd, '-')0; i++) { + size = strspn(ptr, "-"); + ptr += size; + len -= size; + if (len <= 0) + break; + size = strcspn(ptr, "-"); + if (size==0) + break; + tokens[i].str = ptr; + tokens[i].len = size; + ptr += size; + len -= size; + } + + return tokens; +} + + +static int +strToInt(str *token) +{ + int res=0, pos, c; + + if (token->len==0 || token->str[0]=='*') { + return -1; + } else { + for (res=0, pos=0; poslen; pos++) { + c = token->str[pos] - '0'; + if (c<0 || c>9) + break; + res = res*10 + c; + } + } + return res; +} + + +static char* +mapSlantToName(str *slant) +{ + if (slant->len==0 || slant->str[0]=='*') + return "roman"; + + switch(slant->str[0]) { + case 'i': + return "italic"; + case 'o': + return "oblique"; + case 'r': + default: + return "roman"; + } +} + + char* xlfdToFc(char *xlfd) { - char *Fcname = NULL; + str *tokens, *family, *weight, *slant; + char *name, buf[512]; + int size, pixelsize; - char *family = getElementFromXLFD(xlfd, 2); - char *size = getElementFromXLFD(xlfd, 7); - char *weight = getElementFromXLFD(xlfd, 3); - char *slant = getElementFromXLFD(xlfd, 4); + tokens = getXLFDTokens(xlfd); + if (!tokens) + return wstrdup(DEFAULT_FONT); - if (strcmp(family, "*") != 0) { - Fcname = wstrconcat(Fcname, capitalize(family)); - } - if (strcmp(size, "*") != 0) { - Fcname = wstrconcat(Fcname, ":pixelsize="); - Fcname = wstrconcat(Fcname, size); + family = &(tokens[1]); + weight = &(tokens[2]); + slant = &(tokens[3]); + + if (family->len==0 || family->str[0]=='*') + return wstrdup(DEFAULT_FONT); + + sprintf(buf, "%.*s", family->len, family->str); + name = wstrdup(buf); + + pixelsize = strToInt(&tokens[6]); + size = strToInt(&tokens[7]); + + if (size<=0 && pixelsize<=0) { + name = wstrappend(name, ":pixelsize=12"); + } else if (pixelsize>0) { + /* if pixelsize is present size will be ignored so we skip it */ + sprintf(buf, ":pixelsize=%d", pixelsize); + name = wstrappend(name, buf); + } else { + sprintf(buf, "-%d", size/10); + name = wstrappend(name, buf); } - if (strcmp(weight, "*") != 0) { - Fcname = wstrconcat(Fcname, ":weight="); - Fcname = wstrconcat(Fcname, capitalize(weight)); + + if (weight->len>0 && weight->str[0]!='*') { + sprintf(buf, ":weight=%.*s", weight->len, weight->str); + name = wstrappend(name, buf); } - if (strcmp(slant, "*") != 0) { - if (strcmp(slant, "i") == 0) { - Fcname = wstrconcat(Fcname, ":slant="); - Fcname = wstrconcat(Fcname, "Italic"); - } else if (strcmp(slant, "o") == 0) { - Fcname = wstrconcat(Fcname, ":slant="); - Fcname = wstrconcat(Fcname, "Oblique"); - } else if (strcmp(slant, "ri") == 0) { - Fcname = wstrconcat(Fcname, ":slant="); - Fcname = wstrconcat(Fcname, "Rev Italic"); - } else if (strcmp(slant, "ro") == 0) { - Fcname = wstrconcat(Fcname, ":slant="); - Fcname = wstrconcat(Fcname, "Rev Oblique"); - } + + if (slant->len>0 && slant->str[0]!='*') { + sprintf(buf, ":slant=%s", mapSlantToName(slant)); + name = wstrappend(name, buf); } - if (!Fcname) - Fcname = wstrdup(default_font); - wfree(family); - wfree(size); - wfree(weight); - wfree(slant); + name = wstrappend(name, ":xlfd="); + name = wstrappend(name, xlfd); + + return name; +} + - return Fcname; +/* return converted font (if conversion is needed) else the original font */ +static char* +convertFont(char *font) +{ + if (font[0]=='-') { + if (!strchr(font, ',')) { + return xlfdToFc(font); + } else { + wwarning("fontsets are not supported. replaced " + "with default %s", DEFAULT_FONT); + return wstrdup(DEFAULT_FONT); + } + } else { + return font; + } } + void copyFile(char *dir, char *file) { char buffer[4000]; - + sprintf(buffer, "/bin/cp \"%s\" \"%s\"", file, dir); if (system(buffer)!=0) { - printf("%s: could not copy file %s\n", ProgName, file); + printf("%s: could not copy file %s\n", ProgName, file); } } @@ -523,10 +587,10 @@ findCopyFile(char *dir, char *file) fullPath = wfindfileinarray(PixmapPath, file); if (!fullPath) { - char buffer[4000]; + char buffer[4000]; - sprintf(buffer, "coould not find file %s", file); - abortar(buffer); + sprintf(buffer, "coould not find file %s", file); + abortar(buffer); } copyFile(dir, fullPath); free(fullPath); @@ -542,142 +606,141 @@ makeThemePack(WMPropList *style, char *themeName) int i; char *themeDir; - themeDir = mymalloc(strlen(themeName)+50); + themeDir = wmalloc(strlen(themeName)+50); sprintf(themeDir, "%s.themed", themeName); ThemePath = themeDir; { - char *tmp; - - tmp = mymalloc(strlen(themeDir)+20); - sprintf(tmp, "/bin/mkdir \"%s\"", themeDir); - if (system(tmp)!=0) { - printf("%s: could not create directory %s. Probably there's already a theme with that name in this directory.\n", ProgName, themeDir); - exit(1); - } - free(tmp); + char *tmp; + + tmp = wmalloc(strlen(themeDir)+20); + sprintf(tmp, "/bin/mkdir \"%s\"", themeDir); + if (system(tmp)!=0) { + printf("%s: could not create directory %s. Probably there's already a theme with that name in this directory.\n", ProgName, themeDir); + exit(1); + } + free(tmp); } keys = WMGetPLDictionaryKeys(style); for (i = 0; i < WMGetPropListItemCount(keys); i++) { - key = WMGetFromPLArray(keys, i); - - value = WMGetFromPLDictionary(style, key); - if (value && WMIsPLArray(value) && WMGetPropListItemCount(value) > 2) { - WMPropList *type; - char *t; - - type = WMGetFromPLArray(value, 0); - t = WMGetFromPLString(type); - if (t == NULL) - continue; - - if (strcasecmp(t, "tpixmap")==0 - || strcasecmp(t, "spixmap")==0 - || strcasecmp(t, "cpixmap")==0 - || strcasecmp(t, "mpixmap")==0 - || strcasecmp(t, "tdgradient")==0 - || strcasecmp(t, "tvgradient")==0 - || strcasecmp(t, "thgradient")==0) { - WMPropList *file; - char *p; - char *newPath; - - file = WMGetFromPLArray(value, 1); - - p = strrchr(WMGetFromPLString(file), '/'); - if (p) { - copyFile(themeDir, WMGetFromPLString(file)); - - newPath = mystrdup(p+1); - WMDeleteFromPLArray(value, 1); - WMInsertInPLArray(value, 1, WMCreatePLString(newPath)); - free(newPath); - } else { - findCopyFile(themeDir, WMGetFromPLString(file)); - } - } else if (strcasecmp(t, "bitmap")==0) { - WMPropList *file; - char *p; - char *newPath; - - file = WMGetFromPLArray(value, 1); - - p = strrchr(WMGetFromPLString(file), '/'); - if (p) { - copyFile(themeDir, WMGetFromPLString(file)); - - newPath = mystrdup(p+1); - WMDeleteFromPLArray(value, 1); - WMInsertInPLArray(value, 1, WMCreatePLString(newPath)); - free(newPath); - } else { - findCopyFile(themeDir, WMGetFromPLString(file)); - } - - - file = WMGetFromPLArray(value, 2); - - p = strrchr(WMGetFromPLString(file), '/'); - if (p) { - copyFile(themeDir, WMGetFromPLString(file)); - - newPath = mystrdup(p+1); - WMDeleteFromPLArray(value, 2); - WMInsertInPLArray(value, 2, WMCreatePLString(newPath)); - free(newPath); - } else { - findCopyFile(themeDir, WMGetFromPLString(file)); - } - } - } + key = WMGetFromPLArray(keys, i); + + value = WMGetFromPLDictionary(style, key); + if (value && WMIsPLArray(value) && WMGetPropListItemCount(value) > 2) { + WMPropList *type; + char *t; + + type = WMGetFromPLArray(value, 0); + t = WMGetFromPLString(type); + if (t == NULL) + continue; + + if (strcasecmp(t, "tpixmap")==0 + || strcasecmp(t, "spixmap")==0 + || strcasecmp(t, "cpixmap")==0 + || strcasecmp(t, "mpixmap")==0 + || strcasecmp(t, "tdgradient")==0 + || strcasecmp(t, "tvgradient")==0 + || strcasecmp(t, "thgradient")==0) { + WMPropList *file; + char *p; + char *newPath; + + file = WMGetFromPLArray(value, 1); + + p = strrchr(WMGetFromPLString(file), '/'); + if (p) { + copyFile(themeDir, WMGetFromPLString(file)); + + newPath = wstrdup(p+1); + WMDeleteFromPLArray(value, 1); + WMInsertInPLArray(value, 1, WMCreatePLString(newPath)); + free(newPath); + } else { + findCopyFile(themeDir, WMGetFromPLString(file)); + } + } else if (strcasecmp(t, "bitmap")==0) { + WMPropList *file; + char *p; + char *newPath; + + file = WMGetFromPLArray(value, 1); + + p = strrchr(WMGetFromPLString(file), '/'); + if (p) { + copyFile(themeDir, WMGetFromPLString(file)); + + newPath = wstrdup(p+1); + WMDeleteFromPLArray(value, 1); + WMInsertInPLArray(value, 1, WMCreatePLString(newPath)); + free(newPath); + } else { + findCopyFile(themeDir, WMGetFromPLString(file)); + } + + + file = WMGetFromPLArray(value, 2); + + p = strrchr(WMGetFromPLString(file), '/'); + if (p) { + copyFile(themeDir, WMGetFromPLString(file)); + + newPath = wstrdup(p+1); + WMDeleteFromPLArray(value, 2); + WMInsertInPLArray(value, 2, WMCreatePLString(newPath)); + free(newPath); + } else { + findCopyFile(themeDir, WMGetFromPLString(file)); + } + } + } } return themeDir; } -int +int main(int argc, char **argv) { WMPropList *prop, *style, *key, *val; char *path; - int i, theme_too=0; - int make_pack = 0; + int i, theme_too=0, make_pack=0; char *style_file = NULL; ProgName = argv[0]; if (argc>1) { - for (i=1; i -#include -#include -#include -#include -#include -#include - - - -#include "../src/wconfig.h" - -#define MAX_OPTIONS 128 - -char *default_font = "sans:pixelsize=12"; - -char *FontOptions[] = { - "IconTitleFont", - "ClipTitleFont", - "DisplayFont", - "LargeDisplayFont", - "MenuTextFont", - "MenuTitleFont", - "WindowTitleFont", - NULL -}; - -char *CursorOptions[] = { - "NormalCursor", - "ArrowCursor", - "MoveCursor", - "ResizeCursor", - "TopLeftResizeCursor", - "TopRightResizeCursor", - "BottomLeftResizeCursor", - "BottomRightResizeCursor", - "VerticalResizeCursor", - "HorizontalResizeCursor", - "WaitCursor", - "QuestionCursor", - "TextCursor", - "SelectCursor", - NULL -}; - - - -char *ProgName; -int ignoreFonts = 0; -int ignoreCursors = 0; - -Display *dpy; - - - -WMPropList *readBlackBoxStyle(char *path); - -char* -capitalize(char *element) -{ - unsigned int first = 1; - char *p; - char *b; - b = element; - for (p = b; *p != 0; p++) { - if (isalpha(*p) && first) { - first = 0; - *p = toupper(*p); - } else if (*p == '-' || *p == ' ') { - first = 1; - } - } - return b; -} - -char* -getElementFromXLFD(const char *xlfd, int index) -{ - const char *p = xlfd; - while (*p != 0) { - if (*p == '-' && --index == 0) { - const char *end = strchr(p + 1, '-'); - char *buf; - size_t len; - if (end == 0) end = p + strlen(p); - len = end - (p + 1); - buf = wmalloc(len); - memcpy(buf, p + 1, len); - buf[len] = 0; - return buf; - } - p++; - } - return "*"; -} - -char* -xlfdToFc(char *xlfd) -{ - char *Fcname = NULL; - - char *family = getElementFromXLFD(xlfd, 2); - char *size = getElementFromXLFD(xlfd, 7); - char *weight = getElementFromXLFD(xlfd, 3); - char *slant = getElementFromXLFD(xlfd, 4); - - if (strcmp(family, "*") != 0) { - Fcname = wstrconcat(Fcname, capitalize(family)); - } - if (strcmp(size, "*") != 0) { - Fcname = wstrconcat(Fcname, ":pixelsize="); - Fcname = wstrconcat(Fcname, size); - } - if (strcmp(weight, "*") != 0) { - Fcname = wstrconcat(Fcname, ":weight="); - Fcname = wstrconcat(Fcname, capitalize(weight)); - } - if (strcmp(slant, "*") != 0) { - if (strcmp(slant, "i") == 0) { - Fcname = wstrconcat(Fcname, ":slant="); - Fcname = wstrconcat(Fcname, "Italic"); - } else if (strcmp(slant, "o") == 0) { - Fcname = wstrconcat(Fcname, ":slant="); - Fcname = wstrconcat(Fcname, "Oblique"); - } else if (strcmp(slant, "ri") == 0) { - Fcname = wstrconcat(Fcname, ":slant="); - Fcname = wstrconcat(Fcname, "Rev Italic"); - } else if (strcmp(slant, "ro") == 0) { - Fcname = wstrconcat(Fcname, ":slant="); - Fcname = wstrconcat(Fcname, "Rev Oblique"); - } - } - if (!Fcname) - Fcname = wstrdup(default_font); - - wfree(family); - wfree(size); - wfree(weight); - wfree(slant); - - return Fcname; -} - -char* -defaultsPathForDomain(char *domain) -{ - static char path[1024]; - char *gspath; - - gspath = getenv("GNUSTEP_USER_ROOT"); - if (gspath) { - strcpy(path, gspath); - strcat(path, "/"); - } else { - char *home; - - home = getenv("HOME"); - if (!home) { - printf("%s:could not get HOME environment variable!\n", ProgName); - exit(0); - } - - strcpy(path, home); - strcat(path, "/GNUstep/"); - } - strcat(path, DEFAULTS_DIR); - strcat(path, "/"); - strcat(path, domain); - - return path; -} - - - -void -hackPathInTexture(WMPropList *texture, char *prefix) -{ - WMPropList *type; - char *t; - - /* get texture type */ - type = WMGetFromPLArray(texture, 0); - t = WMGetFromPLString(type); - if (t == NULL) - return; - if (strcasecmp(t, "tpixmap")==0 - || strcasecmp(t, "spixmap")==0 - || strcasecmp(t, "mpixmap")==0 - || strcasecmp(t, "cpixmap")==0 - || strcasecmp(t, "tvgradient")==0 - || strcasecmp(t, "thgradient")==0 - || strcasecmp(t, "tdgradient")==0) { - WMPropList *file; - char buffer[4018]; - - /* get pixmap file path */ - file = WMGetFromPLArray(texture, 1); - sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file)); - /* replace path with full path */ - WMDeleteFromPLArray(texture, 1); - WMInsertInPLArray(texture, 1, WMCreatePLString(buffer)); - } else if (strcasecmp(t, "bitmap") == 0) { - WMPropList *file; - char buffer[4018]; - - /* get bitmap file path */ - file = WMGetFromPLArray(texture, 1); - sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file)); - /* replace path with full path */ - WMDeleteFromPLArray(texture, 1); - WMInsertInPLArray(texture, 1, WMCreatePLString(buffer)); - - /* get mask file path */ - file = WMGetFromPLArray(texture, 2); - sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file)); - /* replace path with full path */ - WMDeleteFromPLArray(texture, 2); - WMInsertInPLArray(texture, 2, WMCreatePLString(buffer)); - } -} - - -void -hackPaths(WMPropList *style, char *prefix) -{ - WMPropList *keys; - WMPropList *key; - WMPropList *value; - int i; - - - keys = WMGetPLDictionaryKeys(style); - - for (i = 0; i < WMGetPropListItemCount(keys); i++) { - key = WMGetFromPLArray(keys, i); - - value = WMGetFromPLDictionary(style, key); - if (!value) - continue; - - if (strcasecmp(WMGetFromPLString(key), "WorkspaceSpecificBack")==0) { - if (WMIsPLArray(value)) { - int j; - WMPropList *texture; - - for (j = 0; j < WMGetPropListItemCount(value); j++) { - texture = WMGetFromPLArray(value, j); - - if (texture && WMIsPLArray(texture) - && WMGetPropListItemCount(texture) > 2) { - - hackPathInTexture(texture, prefix); - } - } - } - } else { - - if (WMIsPLArray(value) && WMGetPropListItemCount(value) > 2) { - - hackPathInTexture(value, prefix); - } - } - } - -} - - -static WMPropList* -getColor(WMPropList *texture) -{ - WMPropList *value, *type; - char *str; - - type = WMGetFromPLArray(texture, 0); - if (!type) - return NULL; - - value = NULL; - - str = WMGetFromPLString(type); - if (strcasecmp(str, "solid")==0) { - value = WMGetFromPLArray(texture, 1); - } else if (strcasecmp(str, "dgradient")==0 - || strcasecmp(str, "hgradient")==0 - || strcasecmp(str, "vgradient")==0) { - WMPropList *c1, *c2; - int r1, g1, b1, r2, g2, b2; - char buffer[32]; - - c1 = WMGetFromPLArray(texture, 1); - c2 = WMGetFromPLArray(texture, 2); - if (!dpy) { - if (sscanf(WMGetFromPLString(c1), "#%2x%2x%2x", &r1, &g1, &b1)==3 - && sscanf(WMGetFromPLString(c2), "#%2x%2x%2x", &r2, &g2, &b2)==3) { - sprintf(buffer, "#%02x%02x%02x", (r1+r2)/2, (g1+g2)/2, - (b1+b2)/2); - value = WMCreatePLString(buffer); - } else { - value = c1; - } - } else { - XColor color1; - XColor color2; - - XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)), - WMGetFromPLString(c1), &color1); - XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)), - WMGetFromPLString(c2), &color2); - - sprintf(buffer, "#%02x%02x%02x", - (color1.red+color2.red)>>9, - (color1.green+color2.green)>>9, - (color1.blue+color2.blue)>>9); - value = WMCreatePLString(buffer); - } - } else if (strcasecmp(str, "mdgradient")==0 - || strcasecmp(str, "mhgradient")==0 - || strcasecmp(str, "mvgradient")==0) { - - value = WMGetFromPLArray(texture, 1); - - } else if (strcasecmp(str, "tpixmap")==0 - || strcasecmp(str, "cpixmap")==0 - || strcasecmp(str, "spixmap")==0) { - - value = WMGetFromPLArray(texture, 2); - } - - return value; -} - - -/* - * since some of the options introduce incompatibilities, we will need - * to do a kluge here or the themes ppl will get real annoying. - * So, treat for the absence of the following options: - * IconTitleColor - * IconTitleBack - */ -void -hackStyle(WMPropList *style) -{ - WMPropList *keys, *tmp; - int foundIconTitle = 0, foundResizebarBack = 0; - int i; - - keys = WMGetPLDictionaryKeys(style); - - for (i = 0; i < WMGetPropListItemCount(keys); i++) { - char *str; - - tmp = WMGetFromPLArray(keys, i); - str = WMGetFromPLString(tmp); - if (str) { - int j, found; - - if (ignoreFonts) { - for (j = 0, found = 0; FontOptions[j]!=NULL; j++) { - if (strcasecmp(str, FontOptions[j])==0) { - WMRemoveFromPLDictionary(style, tmp); - found = 1; - break; - } - } - if (found) - continue; - } - if (ignoreCursors) { - for (j = 0, found = 0; CursorOptions[j] != NULL; j++) { - if (strcasecmp(str, CursorOptions[j]) == 0) { - WMRemoveFromPLDictionary(style, tmp); - found = 1; - break; - } - } - if (found) - continue; - } - for (j = 0; FontOptions[j]!=NULL; j++) { - if (strcasecmp(str, FontOptions[j]) == 0) { - char *oldfont; - WMPropList *value; - value = WMGetFromPLDictionary(style, tmp); - oldfont = WMGetFromPLString(value); - if (oldfont[0] == '-') { - if (!strchr(oldfont, ',')) { - char *newfont; - newfont = xlfdToFc(oldfont); - WMPutInPLDictionary(style, tmp, WMCreatePLString(newfont)); - break; - } else { - wwarning("fontsets are not supported. replaced with default: %s", default_font); - WMPutInPLDictionary(style, tmp, - WMCreatePLString(default_font)); - break; - } - } else { - break; - } - } - } - - if (strcasecmp(str, "IconTitleColor")==0 - || strcasecmp(str, "IconTitleBack")==0) { - foundIconTitle = 1; - } else if (strcasecmp(str, "ResizebarBack")==0) { - foundResizebarBack = 1; - } - } - } - - if (!foundIconTitle) { - /* set the default values */ - tmp = WMGetFromPLDictionary(style, WMCreatePLString("FTitleColor")); - if (tmp) { - WMPutInPLDictionary(style, WMCreatePLString("IconTitleColor"), - tmp); - } - - tmp = WMGetFromPLDictionary(style, WMCreatePLString("FTitleBack")); - if (tmp) { - WMPropList *value; - - value = getColor(tmp); - - if (value) { - WMPutInPLDictionary(style, WMCreatePLString("IconTitleBack"), - value); - } - } - } - - if (!foundResizebarBack) { - /* set the default values */ - tmp = WMGetFromPLDictionary(style, WMCreatePLString("UTitleBack")); - if (tmp) { - WMPropList *value; - - value = getColor(tmp); - - if (value) { - WMPropList *t; - - t = WMCreatePLArray(WMCreatePLString("solid"), value, - NULL); - WMPutInPLDictionary(style, WMCreatePLString("ResizebarBack"), - t); - } - } - } - - - if (!WMGetFromPLDictionary(style, WMCreatePLString("MenuStyle"))) { - WMPutInPLDictionary(style, WMCreatePLString("MenuStyle"), - WMCreatePLString("normal")); - } -} - - -void -print_help() -{ - printf("Usage: %s [OPTIONS] FILE\n", ProgName); - puts("Reads style/theme configuration from FILE and updates Window Maker."); - puts(""); - puts(" --no-fonts ignore font related options"); - puts(" --no-cursors ignore cursor related options"); - puts(" --ignore