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_styles
[] = {
118 char *default_font
= "sans: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());
233 abortar("out of memory");
245 tmp
= mymalloc(strlen(str
)+1);
254 getuserhomedir(char *username
)
258 user
= getpwnam(username
);
262 sprintf(buffer
,"could not get password entry for user %s", username
);
277 wexpandpath(char *path
)
279 char buffer2
[PATH_MAX
+2];
280 char buffer
[PATH_MAX
+2];
283 memset(buffer
, 0, PATH_MAX
+2);
289 if (*path
=='/' || *path
==0) {
290 home
= wgethomedir();
291 strcat(buffer
, home
);
295 while (*path
!=0 && *path
!='/') {
296 buffer2
[j
++] = *path
;
300 home
= getuserhomedir(buffer2
);
303 strcat(buffer
, home
);
315 /* expand $(HOME) or $HOME style environment variables */
318 while (*path
!=0 && *path
!=')') {
319 buffer2
[j
++] = *(path
++);
324 tmp
= getenv(buffer2
);
327 strcat(buffer
, "$(");
328 strcat(buffer
, buffer2
);
330 i
+= strlen(buffer2
)+3;
336 while (*path
!=0 && *path
!='/') {
337 buffer2
[j
++] = *(path
++);
340 tmp
= getenv(buffer2
);
343 strcat(buffer
, buffer2
);
344 i
+= strlen(buffer2
)+1;
356 return mystrdup(buffer
);
362 wfindfileinarray(WMPropList
*paths
, char *file
)
372 if (*file
=='/' || *file
=='~' || !paths
|| !WMIsPLArray(paths
)
373 || WMGetPropListItemCount(paths
)==0) {
374 if (access(file
, R_OK
)<0) {
375 fullpath
= wexpandpath(file
);
379 if (access(fullpath
, R_OK
)<0) {
386 return mystrdup(file
);
391 for (i
=0; i
< WMGetPropListItemCount(paths
); i
++) {
395 tmp
= WMGetFromPLArray(paths
, i
);
396 if (!WMIsPLString(tmp
) || !(dir
= WMGetFromPLString(tmp
)))
400 path
= mymalloc(len
+flen
+2);
401 path
= memcpy(path
, dir
, len
);
406 fullpath
= wexpandpath(path
);
409 /* check if file is readable */
410 if (access(fullpath
, R_OK
)==0) {
420 capitalize(char *element
)
422 unsigned int first
= 1;
426 for (p
= b
; *p
!= 0; p
++)
428 if (isalpha(*p
) && first
) {
432 else if (*p
== '-' || *p
== ' ') {
440 getElementFromXLFD(const char *xlfd
, int index
)
442 const char *p
= xlfd
;
444 if (*p
== '-' && --index
== 0) {
445 const char *end
= strchr(p
+ 1, '-');
448 if (end
== 0) end
= p
+ strlen(p
);
451 memcpy(buf
, p
+ 1, len
);
465 char *family
= getElementFromXLFD(xlfd
, 2);
466 char *size
= getElementFromXLFD(xlfd
, 7);
467 char *weight
= getElementFromXLFD(xlfd
, 3);
468 char *slant
= getElementFromXLFD(xlfd
, 4);
470 if (strcmp(family
, "*") != 0) {
471 Fcname
= wstrconcat(Fcname
, capitalize(family
));
473 if (strcmp(size
, "*") != 0) {
474 Fcname
= wstrconcat(Fcname
, ":pixelsize=");
475 Fcname
= wstrconcat(Fcname
, size
);
477 if (strcmp(weight
, "*") != 0) {
478 Fcname
= wstrconcat(Fcname
, ":weight=");
479 Fcname
= wstrconcat(Fcname
, capitalize(weight
));
481 if (strcmp(slant
, "*") != 0) {
482 if (strcmp(slant
, "i") == 0) {
483 Fcname
= wstrconcat(Fcname
, ":slant=");
484 Fcname
= wstrconcat(Fcname
, "Italic");
485 } else if (strcmp(slant
, "o") == 0) {
486 Fcname
= wstrconcat(Fcname
, ":slant=");
487 Fcname
= wstrconcat(Fcname
, "Oblique");
488 } else if (strcmp(slant
, "ri") == 0) {
489 Fcname
= wstrconcat(Fcname
, ":slant=");
490 Fcname
= wstrconcat(Fcname
, "Rev Italic");
491 } else if (strcmp(slant
, "ro") == 0) {
492 Fcname
= wstrconcat(Fcname
, ":slant=");
493 Fcname
= wstrconcat(Fcname
, "Rev Oblique");
497 Fcname
= wstrdup(default_font
);
508 copyFile(char *dir
, char *file
)
512 sprintf(buffer
, "/bin/cp \"%s\" \"%s\"", file
, dir
);
513 if (system(buffer
)!=0) {
514 printf("%s: could not copy file %s\n", ProgName
, file
);
520 findCopyFile(char *dir
, char *file
)
524 fullPath
= wfindfileinarray(PixmapPath
, file
);
528 sprintf(buffer
, "coould not find file %s", file
);
531 copyFile(dir
, fullPath
);
537 makeThemePack(WMPropList
*style
, char *themeName
)
545 themeDir
= mymalloc(strlen(themeName
)+50);
546 sprintf(themeDir
, "%s.themed", themeName
);
547 ThemePath
= themeDir
;
551 tmp
= mymalloc(strlen(themeDir
)+20);
552 sprintf(tmp
, "/bin/mkdir \"%s\"", themeDir
);
553 if (system(tmp
)!=0) {
554 printf("%s: could not create directory %s. Probably there's already a theme with that name in this directory.\n", ProgName
, themeDir
);
559 keys
= WMGetPLDictionaryKeys(style
);
561 for (i
= 0; i
< WMGetPropListItemCount(keys
); i
++) {
562 key
= WMGetFromPLArray(keys
, i
);
564 value
= WMGetFromPLDictionary(style
, key
);
565 if (value
&& WMIsPLArray(value
) && WMGetPropListItemCount(value
) > 2) {
569 type
= WMGetFromPLArray(value
, 0);
570 t
= WMGetFromPLString(type
);
574 if (strcasecmp(t
, "tpixmap")==0
575 || strcasecmp(t
, "spixmap")==0
576 || strcasecmp(t
, "cpixmap")==0
577 || strcasecmp(t
, "mpixmap")==0
578 || strcasecmp(t
, "tdgradient")==0
579 || strcasecmp(t
, "tvgradient")==0
580 || strcasecmp(t
, "thgradient")==0) {
585 file
= WMGetFromPLArray(value
, 1);
587 p
= strrchr(WMGetFromPLString(file
), '/');
589 copyFile(themeDir
, WMGetFromPLString(file
));
591 newPath
= mystrdup(p
+1);
592 WMDeleteFromPLArray(value
, 1);
593 WMInsertInPLArray(value
, 1, WMCreatePLString(newPath
));
596 findCopyFile(themeDir
, WMGetFromPLString(file
));
598 } else if (strcasecmp(t
, "bitmap")==0) {
603 file
= WMGetFromPLArray(value
, 1);
605 p
= strrchr(WMGetFromPLString(file
), '/');
607 copyFile(themeDir
, WMGetFromPLString(file
));
609 newPath
= mystrdup(p
+1);
610 WMDeleteFromPLArray(value
, 1);
611 WMInsertInPLArray(value
, 1, WMCreatePLString(newPath
));
614 findCopyFile(themeDir
, WMGetFromPLString(file
));
618 file
= WMGetFromPLArray(value
, 2);
620 p
= strrchr(WMGetFromPLString(file
), '/');
622 copyFile(themeDir
, WMGetFromPLString(file
));
624 newPath
= mystrdup(p
+1);
625 WMDeleteFromPLArray(value
, 2);
626 WMInsertInPLArray(value
, 2, WMCreatePLString(newPath
));
629 findCopyFile(themeDir
, WMGetFromPLString(file
));
640 main(int argc
, char **argv
)
642 WMPropList
*prop
, *style
, *key
, *val
;
646 char *style_file
= NULL
;
652 for (i
=1; i
<argc
; i
++) {
653 if (strcmp(argv
[i
], "-p")==0
654 || strcmp(argv
[i
], "--pack")==0) {
657 } else if (strcmp(argv
[i
], "-t")==0
658 || strcmp(argv
[i
], "--theme-options")==0) {
660 } else if (strcmp(argv
[i
], "--help")==0) {
663 } else if (strcmp(argv
[i
], "--version")==0) {
667 if (style_file
!=NULL
) {
668 printf("%s: invalid argument '%s'\n", argv
[0],
669 style_file
[0]=='-' ? style_file
: argv
[i
]);
670 printf("Try '%s --help' for more information\n", argv
[0]);
673 style_file
= argv
[i
];
678 if (make_pack
&& !style_file
) {
679 printf("%s: you must supply a name for the theme pack\n", ProgName
);
683 WMPLSetCaseSensitive(False
);
685 path
= defaultsPathForDomain("WindowMaker");
687 prop
= WMReadPropListFromFile(path
);
689 printf("%s:could not load WindowMaker configuration file \"%s\".\n",
694 /* get global value */
695 path
= globalDefaultsPathForDomain("WindowMaker");
696 val
= WMReadPropListFromFile(path
);
698 WMMergePLDictionaries(val
, prop
, True
);
699 WMReleasePropList(prop
);
703 style
= WMCreatePLDictionary(NULL
, NULL
, NULL
);
706 for (i
=0; options
[i
]!=NULL
; i
++) {
707 key
= WMCreatePLString(options
[i
]);
709 val
= WMGetFromPLDictionary(prop
, key
);
712 char *str
= WMGetFromPLString(key
);
713 for (j
= 0; font_styles
[j
]!=NULL
; j
++) {
714 if (strcasecmp(str
, font_styles
[j
]) == 0) {
716 oldfont
= WMGetFromPLString(val
);
717 if (oldfont
[0] == '-') {
718 if (!strchr(oldfont
, ','))
721 newfont
= xlfdToFc(oldfont
);
722 val
= WMCreatePLString(newfont
);
725 wwarning("fontsets are not supported. replaced with default %s", default_font
);
726 val
= WMCreatePLString(default_font
);
734 WMPutInPLDictionary(style
, key
, val
);
738 val
= WMGetFromPLDictionary(prop
, WMCreatePLString("PixmapPath"));
743 for (i
=0; theme_options
[i
]!=NULL
; i
++) {
744 key
= WMCreatePLString(theme_options
[i
]);
746 val
= WMGetFromPLDictionary(prop
, key
);
748 WMPutInPLDictionary(style
, key
, val
);
755 makeThemePack(style
, style_file
);
757 path
= mymalloc(strlen(ThemePath
)+32);
758 strcpy(path
, ThemePath
);
759 strcat(path
, "/style");
760 WMWritePropListToFile(style
, path
, False
);
764 WMWritePropListToFile(style
, style_file
, False
);
766 puts(WMGetPropListDescription(style
, True
));