wmail: fixed possible NULL-pointer dereference in config-parser.
[dockapps.git] / wmhexaclock / src / main.c
blobed5a5354d9003225671af3772369ca07571592a0
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <X11/X.h>
9 #include <X11/xpm.h>
10 #include <X11/Xlib.h>
11 #include <unistd.h>
12 #include <time.h>
13 #include <stdbool.h>
14 #include <getopt.h>
16 #include "wmgeneral.h"
17 #include "pixmaps.h"
19 #define HOUR 1
20 #define MINUTE 2
21 #define SECOND 3
23 #define VERSION "0.1"
24 #define RELEASED "October 2006"
26 /* Prototypes */
27 void beat(void);
28 void display_time(int number,int time_unit);
29 void display_lines(time_t seconds);
30 void display_date(int day_of_week, int day_of_month, int month, int year,int mode,time_t seconds);
31 void setColor(char *color);
32 void setMode(char *mode);
33 void display_help(void);
34 void display_version(void);
35 bool errorColor(char *color);
36 bool errorMode(char *mode);
38 /*Global Variables*/
39 time_t actualtime;
40 int g_mode=0;
41 char color_line[12]="+\tc #";
44 bool errorMode(char *mode)
46 if (strncmp(mode,"0",1)!=0 && strncmp(mode,"1",1)!=0)
47 return true;
48 else
49 return false;
52 bool errorColor(char *color)
54 int i=0;
56 if (strlen(color)!=6)
57 return true;
60 for (i=0;i<6;i++)
62 if ((color[i]<48 || color[i]>57) && (color[i]<65 || color[i]>70) && (color[i]<97 || color[i]>102))
63 return true;
66 return false;
69 void print_version()
71 puts("\033[1mwmHexaClock\033[0m");
72 printf("Version: \t%s\n",VERSION);
73 printf("Released: \t%s\n",RELEASED);
74 puts("Info: \t\thttp://www.bjoernw.de/");
75 exit (0);
78 void print_help()
80 puts("Usage: wmHexaClock [OPTIONS]");
81 puts("Possible Options are:\n");
82 puts(" -v or --version:\t\tGives information about version and release date");
83 puts(" -h or --help:\t\t\tPrints this dialog");
84 puts(" -c [COLOR] or --color [COLOR]:\tSpecifies the foreground color; for [COLOR] you use the hexadecimal notation RRGGBB (R=red, G=green, B=blue)");
85 puts(" \t\t\t\tExample: \"wmHexaClock -c FF0000\" for red");
86 puts(" -m [i] or --mode [i]:\t\tThere are two different display modes; for [i] you type 0 or 1 to chose between them (0 is default)");
87 exit(0);
90 void setColor(char *color)
92 if (!errorColor(color))
93 xpm_numbers[3]=strncat(color_line,color,6); //Edit the Color-Line in xpm_numbers (-> pixmaps.h)
94 else
96 fprintf (stderr,"The Color Code is nor correct! Example: \"-c 00FF00\" for green!\n");
97 exit (1);
101 void setMode(char *mode)
103 if (!errorMode(mode))
104 g_mode=((int)*(mode))-48;
105 else
107 fprintf (stderr,"Please choose between mode 0 and 1. Example: \"-mode 0\"!\n");
108 exit (1);
112 int main(int argc, char *argv[])
114 XEvent event;
115 actualtime=time(0);
117 static struct option long_options[] =
119 /* These options set a flag. */
120 {"version", no_argument, 0, 'v'},
121 {"help", no_argument, 0, 'h'},
122 {"color", required_argument, 0, 'c'},
123 {"mode", required_argument, 0, 'm'},
124 {0, 0, 0, 0}
128 while (1)
130 int c;
131 int option_index = 0;
133 c = getopt_long (argc, argv, "vhc:m:", long_options, &option_index);
135 /* Detect the end of the options. */
136 if (c == -1)
137 break;
139 switch (c)
141 case 'v':
142 print_version();
143 break;
144 case 'h':
145 print_help();
146 break;
147 case 'c':
148 setColor(optarg);
149 break;
150 case 'm':
151 setMode(optarg);
152 break;
153 case '?':
154 puts("Type wmHexaClock -h for information");
155 exit(0);
156 break;
157 default:
158 abort();
162 openXwindow(argc, argv, xpm_numbers, xpm_master, xpm_mask_bits, xpm_mask_width, xpm_mask_height);
165 /* Loop Forever */
166 while (1)
168 if (actualtime!=time(0))
169 beat();
170 /* Process any pending X events. */
171 while (XPending(display))
173 XNextEvent(display, &event);
174 if (event.type==Expose)
175 RedrawWindow();
177 usleep(10000);
180 /* we should never get here */
181 return (0);
185 void beat()
187 struct tm *time_struct;
188 actualtime=time(0);
189 time_struct=localtime(&actualtime);
191 cleanXPMArea();
193 display_time(time_struct->tm_hour,HOUR);
194 display_time(time_struct->tm_min,MINUTE);
195 display_time(time_struct->tm_sec,SECOND);
196 copyXPMArea(177,0,8,12,27,3); /*Display the two dots*/
198 display_lines(time_struct->tm_sec);
200 display_date(time_struct->tm_wday,time_struct->tm_mday,time_struct->tm_mon,time_struct->tm_year+1900,g_mode,time_struct->tm_sec);
202 RedrawWindow();
205 void display_time(int number,int time_unit){
207 int first_pos=number/16;
208 int second_pos=number%16;
210 if (time_unit==HOUR)
212 copyXPMArea(first_pos*11,0,11,12,5,5);
213 copyXPMArea(second_pos*11,0,11,12,16,5);
215 else if (time_unit==MINUTE)
217 copyXPMArea(first_pos*11,0,11,12,35,5);
218 copyXPMArea(second_pos*11,0,11,12,46,5);
220 else if (time_unit==SECOND)
222 copyXPMArea(first_pos*7,12,7,8,40,20);
223 copyXPMArea(second_pos*7,12,7,8,48,20);
226 return;
230 void display_lines(time_t seconds)
232 int i;
234 for (i=0;i<(seconds/8);i++)
235 copyXPMArea(0,29,8,4,i*8+4,32);
237 return;
240 void display_date(int day_of_week, int day_of_month, int month, int year,int mode,time_t seconds)
242 int first_pos_day=day_of_month/16;
243 int second_pos_day=day_of_month%16;
244 int first_pos_month=(month+1)/16;
245 int second_pos_month=(month+1)%16;
246 int first_pos_year=year/256;
247 int second_pos_year=(year-first_pos_year*256)/16;
248 int third_pos_year=year-first_pos_year*256-second_pos_year*16;
250 static bool rec_mode;
252 if (mode==1)
254 copyXPMArea((day_of_week-1)*22,20,22,9,5,39);
255 copyXPMArea(first_pos_day*7,12,7,9,30,39);
256 copyXPMArea(second_pos_day*7,12,7,9,37,39);
257 copyXPMArea(8,30,2,2,45,46);
258 copyXPMArea(first_pos_month*7,12,7,9,47,39);
259 copyXPMArea(second_pos_month*7,12,7,9,54,39);
260 copyXPMArea(first_pos_year*7,12,7,8,34,50);
261 copyXPMArea(second_pos_year*7,12,7,8,44,50);
262 copyXPMArea(third_pos_year*7,12,7,8,53,50);
264 else
266 if (seconds%3==0)
267 rec_mode=!rec_mode;
270 if (rec_mode)
271 copyXPMArea((day_of_week-1)*31,32,31,12,16,39);
272 else
274 copyXPMArea(first_pos_day*11,0,11,12,3,36);
275 copyXPMArea(second_pos_day*11,0,11,12,14,36);
276 copyXPMArea(180,9,4,3,26,46);
277 copyXPMArea(first_pos_month*11,0,11,12,32,36);
278 copyXPMArea(second_pos_month*11,0,11,12,43,36);
279 copyXPMArea(180,9,4,3,55,46);
280 copyXPMArea(first_pos_year*7,12,7,8,18,51);
281 copyXPMArea(second_pos_year*7,12,7,8,28,51);
282 copyXPMArea(third_pos_year*7,12,7,8,37,51);