wmbiff: added support for TLS SNI.
[dockapps.git] / wmlongrun / src / main.c
blob59deb893094e2a4ff47b8ae55a665baea2b289c8
1 /*
2 * WMLongRun - A dockapp to monitor LongRun status
3 * Copyright (C) 2001,2002 Seiichi SATO <ssato@sh.rim.or.jp>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
25 #include <stdio.h>
26 #if defined(HAVE_STRING_H)
27 #include <string.h>
28 #elif defined(HAVE_STRINGS_H)
29 #include <strings.h>
30 #endif
31 #include <unistd.h>
32 #include <time.h>
33 #include <signal.h>
34 #include <sys/time.h>
35 #include "dockapp.h"
36 #include "longrun.h"
37 #include "common.h"
38 #include "backdrop_on.xpm"
39 #include "backdrop_off.xpm"
40 #include "backdrop_led.xpm"
41 #include "parts.xpm"
43 #define SIZE 58
44 #define WINDOWED_BG " \tc #AEAAAE"
46 typedef enum { LIGHTON, LIGHTOFF} light;
48 Pixmap pixmap;
49 Pixmap backdrop_on;
50 Pixmap backdrop_off;
51 Pixmap backdrop_led;
52 Pixmap parts;
53 Pixmap mask;
54 static char *display_name = "";
55 static char *light_color = NULL;
56 static unsigned update_interval = 500;
57 static light backlight = LIGHTOFF;
58 static Bool is_led_style = False;
60 static char *cpuid_dev = NULL;
61 static char *msr_dev = NULL;
63 /* prototypes */
64 static void switch_light(void);
65 static void update(void);
66 static void parse_arguments(int argc, char **argv);
67 static void print_help(char *prog);
69 int main(int argc, char **argv)
71 XEvent event;
72 XpmColorSymbol colors[2] = { {"Back0", NULL, 0}, {"Back1", NULL, 0} };
73 int ncolor = 0;
75 /* Parse CommandLine */
76 parse_arguments(argc, argv);
78 /* Initialize Application */
79 dockapp_open_window(display_name, PACKAGE, SIZE, SIZE, argc, argv);
80 dockapp_set_eventmask(ButtonPressMask);
82 if (light_color) {
83 colors[0].pixel = dockapp_getcolor(light_color);
84 colors[1].pixel = dockapp_blendedcolor(light_color, -24, -24, -24, 1.0);
85 ncolor = 2;
88 /* change raw xpm data to pixmap */
89 if (dockapp_iswindowed)
90 backdrop_on_xpm[1] = backdrop_off_xpm[1] = WINDOWED_BG;
91 if (!is_led_style) {
92 dockapp_xpm2pixmap(backdrop_on_xpm, &backdrop_on, &mask, colors,ncolor);
93 dockapp_xpm2pixmap(backdrop_off_xpm, &backdrop_off, NULL, NULL, 0);
94 } else {
95 dockapp_xpm2pixmap(backdrop_led_xpm, &backdrop_led, &mask, colors, ncolor);
97 dockapp_xpm2pixmap(parts_xpm, &parts, NULL, colors, ncolor);
98 /* shape window */
99 if (!dockapp_iswindowed)
100 dockapp_setshape(mask, 0, 0);
101 if (mask) XFreePixmap(display, mask);
102 /* pixmap: draw area */
103 pixmap = dockapp_XCreatePixmap(SIZE, SIZE);
105 /* Initialize pixmap */
106 longrun_init(cpuid_dev, msr_dev);
107 update();
108 dockapp_set_background(pixmap);
109 dockapp_show();
111 /* Main loop */
112 for (;;) {
113 if (dockapp_nextevent_or_timeout(&event, update_interval)) {
114 /* Next Event */
115 switch(event.type) {
116 case ButtonPress:
117 if (!is_led_style) { /* led does not have back-light */
118 switch_light();
119 update();
121 break;
122 default: /* make gcc happy */
123 break;
125 } else {
126 /* Time Out */
127 update();
131 return 0;
134 /* called when mouse button pressed */
135 static void switch_light(void)
137 switch (backlight) {
138 case LIGHTOFF:
139 backlight = LIGHTON;
140 break;
141 case LIGHTON:
142 backlight = LIGHTOFF;
143 break;
148 static void update(void)
150 static int percent; /* LongRun performance level */
151 static int flags; /* LongRun flags */
152 static int mhz; /* LongRun frequency */
153 static int voltz; /* LongRun voltage */
155 int digit1 = 0, digit10 = 0, digit100 = 0;
157 longrun_get_stat(&percent, &flags, &mhz, &voltz);
159 digit100 = mhz / 100;
160 digit10 = (mhz - digit100 * 100) / 10;
161 digit1 = mhz - digit100 * 100 - digit10 * 10;
163 /* LCD interface */
164 if (!is_led_style) {
165 int y_lrmode = 0, y_gauge = 0, y_digit = 0;
167 /* clear */
168 switch (backlight) {
169 case LIGHTON:
170 dockapp_copyarea(backdrop_on, pixmap, 0, 0, 58, 58, 0, 0);
171 y_lrmode = 11;
172 y_gauge = 14;
173 y_digit = 13;
174 break;
175 case LIGHTOFF:
176 dockapp_copyarea(backdrop_off, pixmap, 0, 0, 58, 58, 0, 0);
177 break;
180 /* longrun flags (performance or economy) */
181 switch (flags) {
182 case LONGRUN_FLAGS_PEFORMANCE:
183 dockapp_copyarea(parts, pixmap, 24, y_lrmode, 24, 11, 31, 5);
184 break;
185 case LONGRUN_FLAGS_ECONOMY:
186 dockapp_copyarea(parts, pixmap, 0, y_lrmode, 24, 11, 5, 5);
187 break;
188 default:
189 break; /* make gcc happy */
192 /* draw digit (frequency) */
193 dockapp_copyarea(parts, pixmap, digit100*7,y_digit+33, 7,13, 6,22);
194 dockapp_copyarea(parts, pixmap, digit10*7,y_digit+33, 7,13, 15,22);
195 dockapp_copyarea(parts, pixmap, digit1*7,y_digit+33, 7,13, 24,22);
197 /* draw level gauge */
198 dockapp_copyarea(parts, pixmap, 0,y_gauge+72, 49*percent/100,14,5,40);
201 /* LED interface */
202 else {
203 dockapp_copyarea(backdrop_led, pixmap, 0, 0, 58, 58, 0, 0);
204 /* longrun flags (performance or economy) */
205 switch (flags) {
206 case LONGRUN_FLAGS_PEFORMANCE:
207 dockapp_copyarea(parts, pixmap, 24, 22, 24, 11, 30, 3);
208 break;
209 case LONGRUN_FLAGS_ECONOMY:
210 dockapp_copyarea(parts, pixmap, 0, 22, 24, 11, 3, 3);
211 break;
212 default:
213 break; /* make gcc happy */
216 /* draw digit (frequency) */
217 dockapp_copyarea(parts, pixmap, digit100*7,59, 7,13, 4,22);
218 dockapp_copyarea(parts, pixmap, digit10*7,59, 7,13, 13,22);
219 dockapp_copyarea(parts, pixmap, digit1*7,59, 7,13, 22,22);
221 /* draw level gauge */
222 dockapp_copyarea(parts, pixmap, 0, 100, 56 * percent / 100, 16, 1, 41);
225 /* show */
226 dockapp_copy2window(pixmap);
229 static void parse_arguments(int argc, char **argv)
231 int i;
232 for (i = 1; i < argc; i++) {
233 if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h"))
234 print_help(argv[0]), exit(0);
236 else if (!strcmp(argv[i], "--version") || !strcmp(argv[i], "-v"))
237 printf("%s version %s\n", PACKAGE, VERSION), exit(0);
239 else if (!strcmp(argv[i], "--display") || !strcmp(argv[i], "-d")) {
240 display_name = argv[i + 1];
241 i++;
244 else if (!strcmp(argv[i], "--backlight")
245 || !strcmp(argv[i], "-bl"))
246 backlight = LIGHTON;
248 else if (!strcmp(argv[i], "--light-color")
249 || !strcmp(argv[i], "-lc")) {
250 if (argc == i + 1)
251 fprintf(stderr,
252 "%s: error parsing argument for option %s\n",
253 argv[0], argv[i]), exit(1);
254 light_color = argv[i + 1];
255 i++;
258 else if (!strcmp(argv[i], "--interval") || !strcmp(argv[i], "-i")) {
259 int integer;
260 if (argc == i + 1)
261 fprintf(stderr,
262 "%s: error parsing argument for option %s\n",
263 argv[0], argv[i]), exit(1);
264 if (sscanf(argv[i + 1], "%i", &integer) != 1)
265 fprintf(stderr,
266 "%s: error parsing argument for option %s\n",
267 argv[0], argv[i]), exit(1);
268 if (integer < 1)
269 fprintf(stderr, "%s: argument %s must be >=1\n",
270 argv[0], argv[i]), exit(1);
271 update_interval = integer;
272 i++;
275 else if (!strcmp(argv[i], "--led") || !strcmp(argv[i], "-l"))
276 is_led_style = True;
278 else if (!strcmp(argv[i], "--windowed")
279 || !strcmp(argv[i], "-w"))
280 dockapp_iswindowed = True;
282 else if (!strcmp(argv[i], "--broken-wm")
283 || !strcmp(argv[i], "-bw"))
284 dockapp_isbrokenwm = True;
286 #ifdef LINUX
287 else if (!strcmp(argv[i], "--cpuid-device")
288 || !strcmp(argv[i], "-cd")) {
289 cpuid_dev = argv[i + 1];
290 i++;
292 else if (!strcmp(argv[i], "--msr-device")
293 || !strcmp(argv[i], "-md")) {
294 msr_dev = argv[i + 1];
295 i++;
297 #endif
299 else {
300 fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0],
301 argv[i]);
302 print_help(argv[0]), exit(1);
307 static void print_help(char *prog)
309 printf("Usage : %s [OPTIONS]\n", prog);
310 printf
311 ("WMLongRun - A dockapp to monitor the LongRun(tm) status on Crusoe(tm) processors.\n");
312 printf(" -d, --display <string> display to use\n");
313 printf(" -bl, --backlight turn on back-light\n");
314 printf(" -lc, --light-color <string> back-light color(rgb:6E/C6/3B is default)\n");
315 printf(" -i, --interval <number> number of milliseconds between updates\n");
316 printf(" (500 is default)\n");
317 #ifdef LINUX
318 printf(" -cd, --cpuid-device <device> CPUID device ('/dev/cpu/0/cpuid' is default)\n");
319 printf(" -md, --msr-device <device> MSR device ('/dev/cpu/0/msr' is default)\n");
320 #endif
321 printf(" -l, --led run the application with LED interface\n");
322 printf(" -h, --help show this help text and exit\n");
323 printf(" -v, --version show program version and exit\n");
324 printf(" -w, --windowed run the application in windowed mode\n");
325 printf(" -bw, --broken-wm activate broken window manager fix\n");