1 #include "asclock.h"
2 
3 /* load the variables from a predefined theme ****************************************/
4 
config()5 void config ()
6 {
7   init_symbols();
8 #include "default_theme/config"
9 
10 }
11 
12 /*************************************************************************************/
13 
14 static char *help_message[] = {
15 "where options include:",
16 "    -h, --help              this help message",
17 "    -v, --version           asclock version",
18 "    -12                     12 hour format",
19 "    -24                     24 hour format",
20 "    -d                      WindowMaker docking",
21 "    -exe <program>          program to start on click",
22 "    -theme <theme-dir>      directory containing a set of xpms",
23 "    -noblink                don't blink",
24 NULL
25 };
26 
27 char *themes_directories[] = {
28   "./themes/",
29   "/usr/local/share/asclock/",
30   "/usr/local/share/asclock/",
31   "",
32   NULL
33 };
34 
usage(char * ProgName)35 void usage(char *ProgName)
36 {
37   char **cpp;
38   DIR *dfd;
39   struct dirent *dp;
40 
41   fprintf(stderr,"usage:  %s [-options ...] \n", ProgName);
42   for (cpp = help_message; *cpp; cpp++) {
43     fprintf(stderr, "%s\n", *cpp);
44   }
45   fprintf(stderr,"\n");
46 
47   /* list themes in all known themes directories */
48   fprintf(stderr, "----------------------------------------------------------------\n");
49   fprintf(stderr, "List of installed themes directories\n\n");
50 
51   for (cpp= themes_directories; *cpp; cpp++)
52     {
53       fprintf(stderr, "dir : %s\n", *cpp);
54 
55       if((dfd = opendir(*cpp)) == NULL)
56 	  printf(" not found\n\n");
57       else {
58 	while((dp = readdir(dfd)) != NULL)
59 	  if ( dp->d_name[0]!='.' )
60 	    printf("%s\n", dp->d_name);
61         closedir(dfd);
62       }
63       printf("\n\n");
64 
65     }
66 
67   exit(1);
68 }
69 
findTheme(char * input,char * ret)70 int findTheme(char*input, char *ret)
71 {
72   char **dir;
73   DIR *d;
74   int n;
75 
76   n = MAX_PATH_LEN - strlen(input);
77 
78   for(dir = themes_directories; *dir; dir++)
79     {
80 
81       if( strlen(*dir) < n )
82 	{
83 	  strncpy(ret, *dir, MAX_PATH_LEN);
84 	  strncat(ret, input, MAX_PATH_LEN);
85 
86 	  if( (d = opendir(ret)) != NULL )
87 	  {
88 	    closedir(d);
89 	    return TRUE;
90 	  }
91 	}
92     }
93   return FALSE;
94 }
95 
loadTheme(char * themesdir)96 int loadTheme(char *themesdir)
97 {
98   FILE *f;
99   char filename[MAX_PATH_LEN];
100 
101   char token[64];
102   int type;
103 
104   strcpy(filename, themesdir);
105   strcat(filename, "/config");
106 
107   f = fopen(filename, "r");
108 
109   if(!f)
110     {
111       fprintf(stderr, "%s not found\n", filename);
112       fprintf(stderr,"There is a problem with this theme.\n");
113       return FALSE;
114     }
115 
116   init_symbols();
117 
118   /* make sure nothing is accidentally visible */
119   led_visible = 0;
120   day_visible = 0;
121   week_visible = 0;
122   month_visible = 0;
123   analog_visible = 0;
124   hour_visible = 0;
125   min_visible = 0;
126   sec_visible = 0;
127   beats_visible = 0;
128   read_init(f);
129 
130   /* parse the config file for int's */
131   while(read_type(&type))
132     {
133       symbol *s;
134 
135       if(!read_token(token, 64))
136 	printf("read_token failed\n");
137 
138       s = config_symbols;
139       while((s->name) && (strcmp(s->name, token)!=0))
140         s++;
141 
142       if(!(s->name)) {
143 	fprintf(stderr, "There is no such thing as a variable called %s\n", token);
144 	exit(-1);
145       }
146 
147       if(!read_assign(f))
148 	printf("read_assign failed\n");
149 
150       if(!read_int(s->addr))
151 	printf("read_int failed\n");
152 
153       if(!read_semicolon(f))
154 	printf("read_semicolon failed\n");
155     }
156 
157   day_x = (day1_x + day2_x)/2;
158   analog_visible = hour_visible || min_visible || sec_visible;
159 
160   fclose(f);
161 
162   strcpy(clock_xpm_fn, themesdir);
163   strcat(clock_xpm_fn, "/clock.xpm");
164 
165   strcpy(month_xpm_fn, themesdir);
166   strcat(month_xpm_fn, "/month.xpm");
167 
168   strcpy(weekday_xpm_fn, themesdir);
169   strcat(weekday_xpm_fn, "/weekday.xpm");
170 
171   strcpy(date_xpm_fn, themesdir);
172   strcat(date_xpm_fn, "/date.xpm");
173 
174   strcpy(led_xpm_fn, themesdir);
175   strcat(led_xpm_fn, "/led.xpm");
176 
177   strcpy(hour_xpm_fn, themesdir);
178   strcat(hour_xpm_fn, "/hour.xpm");
179 
180   strcpy(min_xpm_fn, themesdir);
181   strcat(min_xpm_fn, "/minute.xpm");
182 
183   strcpy(sec_xpm_fn, themesdir);
184   strcat(sec_xpm_fn, "/second.xpm");
185 
186   strcpy(beats_xpm_fn, themesdir);
187   strcat(beats_xpm_fn, "/beats.xpm");
188 
189   return TRUE;
190 }
191 
parseArgs(int argc,char ** argv)192 void parseArgs(int argc, char **argv)
193 {
194   int i;
195   char themesdir[MAX_PATH_LEN];
196   char *ProgName = argv[0];
197 
198   itblinks=TRUE;
199   itdocks=FALSE;
200   for(i=1;i<argc;i++) {
201     char *arg= argv[i];
202 
203     if (arg[0] == '-') {
204       switch(arg[1]) {
205 
206       case 'v':
207 	printf("%s: Version %s\n", argv[0], VERSION);
208         exit(0);
209       case '-':
210         if(strncmp(arg+2, "version", 7)==0)
211         {
212           printf("%s: Version %s\n", argv[0], VERSION);
213           exit(0);
214 	}
215 	/* FALLTHROUGH */
216       case 'h':
217 	usage(ProgName);
218 	exit(0);
219 
220       case 'n':
221 	itblinks = FALSE;
222 	continue;
223       case 'd':
224 	itdocks = TRUE;
225 	continue;
226       case '1':
227         showampm=1;
228         continue;
229       case '2':
230         showampm=0;
231         continue;
232       case 't':
233         if(++i >=argc) usage(ProgName);
234 
235         if(! findTheme(argv[i], themesdir) )
236           {
237             fprintf(stderr, "Could not find theme %s\n\n", argv[i]);
238             exit(-1);
239           }
240 
241         if(!loadTheme(themesdir))
242           exit(-1);
243 
244         continue;
245       case 'e':
246         if(++i >=argc) usage(ProgName);
247         strcpy(exec_str, argv[i]);
248         strcat(exec_str, " &");
249         continue;
250 
251       }
252     }
253   }
254 }
255