1 /* getstyle.c - outputs style related options from WindowMaker to stdout
3 * WindowMaker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
24 #define PROG_VERSION "getstyle (Window Maker) 0.6"
36 #include <WINGs/WUtil.h>
42 #include "../src/wconfig.h"
44 /* table of style related options */
45 static char *options
[] = {
54 "WindowTitleExtendSpace",
55 "MenuTitleExtendSpace",
56 "MenuTextExtendSpace",
77 "WindowTitleExtendSpace",
78 "MenuTitleExtendSpace",
79 "MenuTextExtendSpace",
84 /* table of theme related options */
85 static char *theme_options
[] = {
91 "TopLeftResizeCursor",
92 "TopRightResizeCursor",
93 "BottomLeftResizeCursor",
94 "BottomRightResizeCursor",
95 "VerticalResizeCursor",
96 "HorizontalResizeCursor",
105 /* table of style related fonts */
107 static char *font_options
[] = {
118 #define DEFAULT_FONT "sans-serif:pixelsize=12"
122 WMPropList
*PixmapPath
= NULL
;
124 char *ThemePath
= NULL
;
130 printf("Usage: %s [OPTIONS] [FILE]\n", ProgName
);
131 puts("Retrieves style/theme configuration and output to FILE or to stdout");
133 puts(" -t, --theme-options output theme related options when producing a style file");
134 puts(" -p, --pack produce output as a theme pack");
135 puts(" --help display this help and exit");
136 puts(" --version output version information and exit");
141 globalDefaultsPathForDomain(char *domain
)
143 static char path
[1024];
145 sprintf(path
, "%s/WindowMaker/%s", SYSCONFDIR
, domain
);
152 defaultsPathForDomain(char *domain
)
154 static char path
[1024];
157 gspath
= getenv("GNUSTEP_USER_ROOT");
159 strcpy(path
, gspath
);
164 home
= getenv("HOME");
166 printf("%s:could not get HOME environment variable!\n", ProgName
);
170 strcat(path
, "/GNUstep/");
172 strcat(path
, DEFAULTS_DIR
);
174 strcat(path
, domain
);
181 abortar(char *reason
)
185 printf("%s: %s\n", ProgName
, reason
);
188 printf("Removing unfinished theme pack\n");
189 sprintf(buffer
, "/bin/rm -fr \"%s\"", ThemePath
);
191 if (system(buffer
)!=0) {
192 printf("%s: could not execute command %s\n", ProgName
, buffer
);
204 char *home
= getenv("HOME");
210 user
= getpwuid(getuid());
214 sprintf(buffer
, "could not get password entry for UID %i", getuid());
227 getuserhomedir(char *username
)
231 user
= getpwnam(username
);
235 sprintf(buffer
,"could not get password entry for user %s", username
);
248 wexpandpath(char *path
)
250 char buffer2
[PATH_MAX
+2];
251 char buffer
[PATH_MAX
+2];
254 memset(buffer
, 0, PATH_MAX
+2);
260 if (*path
=='/' || *path
==0) {
261 home
= wgethomedir();
262 strcat(buffer
, home
);
266 while (*path
!=0 && *path
!='/') {
267 buffer2
[j
++] = *path
;
271 home
= getuserhomedir(buffer2
);
274 strcat(buffer
, home
);
286 /* expand $(HOME) or $HOME style environment variables */
289 while (*path
!=0 && *path
!=')') {
290 buffer2
[j
++] = *(path
++);
295 tmp
= getenv(buffer2
);
298 strcat(buffer
, "$(");
299 strcat(buffer
, buffer2
);
301 i
+= strlen(buffer2
)+3;
307 while (*path
!=0 && *path
!='/') {
308 buffer2
[j
++] = *(path
++);
311 tmp
= getenv(buffer2
);
314 strcat(buffer
, buffer2
);
315 i
+= strlen(buffer2
)+1;
327 return wstrdup(buffer
);
333 wfindfileinarray(WMPropList
*paths
, char *file
)
343 if (*file
=='/' || *file
=='~' || !paths
|| !WMIsPLArray(paths
)
344 || WMGetPropListItemCount(paths
)==0) {
345 if (access(file
, R_OK
)<0) {
346 fullpath
= wexpandpath(file
);
350 if (access(fullpath
, R_OK
)<0) {
357 return wstrdup(file
);
362 for (i
=0; i
< WMGetPropListItemCount(paths
); i
++) {
366 tmp
= WMGetFromPLArray(paths
, i
);
367 if (!WMIsPLString(tmp
) || !(dir
= WMGetFromPLString(tmp
)))
371 path
= wmalloc(len
+flen
+2);
372 path
= memcpy(path
, dir
, len
);
377 fullpath
= wexpandpath(path
);
380 /* check if file is readable */
381 if (access(fullpath
, R_OK
)==0) {
392 isFontOption(char *option
)
396 for (i
=0; font_options
[i
]!=NULL
; i
++) {
397 if (strcasecmp(option
, font_options
[i
])==0) {
407 countChar(char *str
, char c
)
414 for (; *str
!=0; str
++) {
429 #define XLFD_TOKENS 14
432 getXLFDTokens(char *xlfd
)
434 static str tokens
[XLFD_TOKENS
];
438 if (!xlfd
|| countChar(xlfd
, '-')<XLFD_TOKENS
)
441 memset(tokens
, 0, sizeof(str
)*XLFD_TOKENS
);
445 for (ptr
=xlfd
, i
=0; i
<XLFD_TOKENS
&& len
>0; i
++) {
446 size
= strspn(ptr
, "-");
451 size
= strcspn(ptr
, "-");
455 tokens
[i
].len
= size
;
469 if (token
->len
==0 || token
->str
[0]=='*') {
472 for (res
=0, pos
=0; pos
<token
->len
; pos
++) {
473 c
= token
->str
[pos
] - '0';
484 mapSlantToName(str
*slant
)
486 if (slant
->len
==0 || slant
->str
[0]=='*')
489 switch(slant
->str
[0]) {
504 str
*tokens
, *family
, *weight
, *slant
;
505 char *name
, buf
[512];
508 tokens
= getXLFDTokens(xlfd
);
510 return wstrdup(DEFAULT_FONT
);
512 family
= &(tokens
[1]);
513 weight
= &(tokens
[2]);
514 slant
= &(tokens
[3]);
516 if (family
->len
==0 || family
->str
[0]=='*')
517 return wstrdup(DEFAULT_FONT
);
519 sprintf(buf
, "%.*s", family
->len
, family
->str
);
522 pixelsize
= strToInt(&tokens
[6]);
523 size
= strToInt(&tokens
[7]);
525 if (size
<=0 && pixelsize
<=0) {
526 name
= wstrappend(name
, ":pixelsize=12");
527 } else if (pixelsize
>0) {
528 /* if pixelsize is present size will be ignored so we skip it */
529 sprintf(buf
, ":pixelsize=%d", pixelsize
);
530 name
= wstrappend(name
, buf
);
532 sprintf(buf
, "-%d", size
/10);
533 name
= wstrappend(name
, buf
);
536 if (weight
->len
>0 && weight
->str
[0]!='*') {
537 sprintf(buf
, ":weight=%.*s", weight
->len
, weight
->str
);
538 name
= wstrappend(name
, buf
);
541 if (slant
->len
>0 && slant
->str
[0]!='*') {
542 sprintf(buf
, ":slant=%s", mapSlantToName(slant
));
543 name
= wstrappend(name
, buf
);
546 name
= wstrappend(name
, ":xlfd=");
547 name
= wstrappend(name
, xlfd
);
553 /* return converted font (if conversion is needed) else the original font */
555 convertFont(char *font
)
558 if (!strchr(font
, ',')) {
559 return xlfdToFc(font
);
561 wwarning("fontsets are not supported. replaced "
562 "with default %s", DEFAULT_FONT
);
563 return wstrdup(DEFAULT_FONT
);
572 copyFile(char *dir
, char *file
)
576 sprintf(buffer
, "/bin/cp \"%s\" \"%s\"", file
, dir
);
577 if (system(buffer
)!=0) {
578 printf("%s: could not copy file %s\n", ProgName
, file
);
584 findCopyFile(char *dir
, char *file
)
588 fullPath
= wfindfileinarray(PixmapPath
, file
);
592 sprintf(buffer
, "could not find file %s", file
);
595 copyFile(dir
, fullPath
);
601 makeThemePack(WMPropList
*style
, char *themeName
)
609 themeDir
= wmalloc(strlen(themeName
)+50);
610 sprintf(themeDir
, "%s.themed", themeName
);
611 ThemePath
= themeDir
;
615 tmp
= wmalloc(strlen(themeDir
)+20);
616 sprintf(tmp
, "/bin/mkdir \"%s\"", themeDir
);
617 if (system(tmp
)!=0) {
618 printf("%s: could not create directory %s. Probably there's already a theme with that name in this directory.\n", ProgName
, themeDir
);
623 keys
= WMGetPLDictionaryKeys(style
);
625 for (i
= 0; i
< WMGetPropListItemCount(keys
); i
++) {
626 key
= WMGetFromPLArray(keys
, i
);
628 value
= WMGetFromPLDictionary(style
, key
);
629 if (value
&& WMIsPLArray(value
) && WMGetPropListItemCount(value
) > 2) {
633 type
= WMGetFromPLArray(value
, 0);
634 t
= WMGetFromPLString(type
);
638 if (strcasecmp(t
, "tpixmap")==0
639 || strcasecmp(t
, "spixmap")==0
640 || strcasecmp(t
, "cpixmap")==0
641 || strcasecmp(t
, "mpixmap")==0
642 || strcasecmp(t
, "tdgradient")==0
643 || strcasecmp(t
, "tvgradient")==0
644 || strcasecmp(t
, "thgradient")==0) {
649 file
= WMGetFromPLArray(value
, 1);
651 p
= strrchr(WMGetFromPLString(file
), '/');
653 copyFile(themeDir
, WMGetFromPLString(file
));
655 newPath
= wstrdup(p
+1);
656 WMDeleteFromPLArray(value
, 1);
657 WMInsertInPLArray(value
, 1, WMCreatePLString(newPath
));
660 findCopyFile(themeDir
, WMGetFromPLString(file
));
662 } else if (strcasecmp(t
, "bitmap")==0) {
667 file
= WMGetFromPLArray(value
, 1);
669 p
= strrchr(WMGetFromPLString(file
), '/');
671 copyFile(themeDir
, WMGetFromPLString(file
));
673 newPath
= wstrdup(p
+1);
674 WMDeleteFromPLArray(value
, 1);
675 WMInsertInPLArray(value
, 1, WMCreatePLString(newPath
));
678 findCopyFile(themeDir
, WMGetFromPLString(file
));
682 file
= WMGetFromPLArray(value
, 2);
684 p
= strrchr(WMGetFromPLString(file
), '/');
686 copyFile(themeDir
, WMGetFromPLString(file
));
688 newPath
= wstrdup(p
+1);
689 WMDeleteFromPLArray(value
, 2);
690 WMInsertInPLArray(value
, 2, WMCreatePLString(newPath
));
693 findCopyFile(themeDir
, WMGetFromPLString(file
));
704 main(int argc
, char **argv
)
706 WMPropList
*prop
, *style
, *key
, *val
;
708 int i
, theme_too
=0, make_pack
=0;
709 char *style_file
= NULL
;
715 for (i
=1; i
<argc
; i
++) {
716 if (strcmp(argv
[i
], "-p")==0
717 || strcmp(argv
[i
], "--pack")==0) {
720 } else if (strcmp(argv
[i
], "-t")==0
721 || strcmp(argv
[i
], "--theme-options")==0) {
723 } else if (strcmp(argv
[i
], "--help")==0) {
726 } else if (strcmp(argv
[i
], "--version")==0) {
730 if (style_file
!=NULL
) {
731 printf("%s: invalid argument '%s'\n", argv
[0],
732 style_file
[0]=='-' ? style_file
: argv
[i
]);
733 printf("Try '%s --help' for more information\n", argv
[0]);
736 style_file
= argv
[i
];
741 if (make_pack
&& !style_file
) {
742 printf("%s: you must supply a name for the theme pack\n", ProgName
);
746 WMPLSetCaseSensitive(False
);
748 path
= defaultsPathForDomain("WindowMaker");
750 prop
= WMReadPropListFromFile(path
);
752 printf("%s:could not load WindowMaker configuration file \"%s\".\n",
757 /* get global value */
758 path
= globalDefaultsPathForDomain("WindowMaker");
759 val
= WMReadPropListFromFile(path
);
761 WMMergePLDictionaries(val
, prop
, True
);
762 WMReleasePropList(prop
);
766 style
= WMCreatePLDictionary(NULL
, NULL
, NULL
);
768 for (i
=0; options
[i
]!=NULL
; i
++) {
769 key
= WMCreatePLString(options
[i
]);
771 val
= WMGetFromPLDictionary(prop
, key
);
773 WMRetainPropList(val
);
774 if (isFontOption(options
[i
])) {
775 char *newfont
, *oldfont
;
777 oldfont
= WMGetFromPLString(val
);
778 newfont
= convertFont(oldfont
);
779 /* newfont is a reference to old if conversion is not needed */
780 if (newfont
!= oldfont
) {
781 WMReleasePropList(val
);
782 val
= WMCreatePLString(newfont
);
786 WMPutInPLDictionary(style
, key
, val
);
787 WMReleasePropList(val
);
789 WMReleasePropList(key
);
792 val
= WMGetFromPLDictionary(prop
, WMCreatePLString("PixmapPath"));
797 for (i
=0; theme_options
[i
]!=NULL
; i
++) {
798 key
= WMCreatePLString(theme_options
[i
]);
800 val
= WMGetFromPLDictionary(prop
, key
);
802 WMPutInPLDictionary(style
, key
, val
);
809 makeThemePack(style
, style_file
);
811 path
= wmalloc(strlen(ThemePath
)+32);
812 strcpy(path
, ThemePath
);
813 strcat(path
, "/style");
814 WMWritePropListToFile(style
, path
, False
);
818 WMWritePropListToFile(style
, style_file
, False
);
820 puts(WMGetPropListDescription(style
, True
));