Add missing libraries to examples' Makefiles
[dockapps.git] / wmlongrun / src / main.c
blobfe9d7e688c100a05a2b2384c0a001e719a92c4e8
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 "backdrop_on_g.xpm"
42 #include "backdrop_off_g.xpm"
43 #include "backdrop_led_g.xpm"
44 #include "parts.xpm"
46 #define SIZE 58
47 #define WINDOWED_BG " \tc #AEAAAE"
49 typedef enum { LIGHTON, LIGHTOFF} light;
51 Pixmap pixmap;
52 Pixmap backdrop_on;
53 Pixmap backdrop_off;
54 Pixmap backdrop_led;
55 Pixmap backdrop_on_g;
56 Pixmap backdrop_off_g;
57 Pixmap backdrop_led_g;
58 Pixmap parts;
59 Pixmap mask;
60 static char *display_name = "";
61 static char *light_color = NULL;
62 static unsigned update_interval = 500;
63 static light backlight = LIGHTOFF;
64 static Bool is_led_style = False;
66 static char *cpuid_dev = NULL;
67 static char *msr_dev = NULL;
69 /* prototypes */
70 static void switch_light(void);
71 static void update(void);
72 static void parse_arguments(int argc, char **argv);
73 static void print_help(char *prog);
75 int main(int argc, char **argv)
77 XEvent event;
78 XpmColorSymbol colors[2] = { {"Back0", NULL, 0}, {"Back1", NULL, 0} };
79 int ncolor = 0;
81 /* Parse CommandLine */
82 parse_arguments(argc, argv);
84 /* Initialize Application */
85 dockapp_open_window(display_name, PACKAGE, SIZE, SIZE, argc, argv);
86 dockapp_set_eventmask(ButtonPressMask);
88 if (light_color) {
89 colors[0].pixel = dockapp_getcolor(light_color);
90 colors[1].pixel = dockapp_blendedcolor(light_color, -24, -24, -24, 1.0);
91 ncolor = 2;
94 /* change raw xpm data to pixmap */
95 if (dockapp_iswindowed)
96 backdrop_on_xpm[1] = backdrop_off_xpm[1] = WINDOWED_BG;
97 backdrop_on_g_xpm[1] = backdrop_off_g_xpm[1] = WINDOWED_BG;
98 if (!is_led_style) {
99 dockapp_xpm2pixmap(backdrop_on_xpm, &backdrop_on, &mask, colors,ncolor);
100 dockapp_xpm2pixmap(backdrop_off_xpm, &backdrop_off, NULL, NULL, 0);
101 dockapp_xpm2pixmap(backdrop_on_g_xpm, &backdrop_on_g, &mask, colors,ncolor);
102 dockapp_xpm2pixmap(backdrop_off_g_xpm, &backdrop_off_g, NULL, NULL, 0);
103 } else {
104 dockapp_xpm2pixmap(backdrop_led_xpm, &backdrop_led, &mask, colors, ncolor);
105 dockapp_xpm2pixmap(backdrop_led_g_xpm, &backdrop_led_g, &mask, colors, ncolor);
107 dockapp_xpm2pixmap(parts_xpm, &parts, NULL, colors, ncolor);
108 /* shape window */
109 if (!dockapp_iswindowed)
110 dockapp_setshape(mask, 0, 0);
111 if (mask) XFreePixmap(display, mask);
112 /* pixmap: draw area */
113 pixmap = dockapp_XCreatePixmap(SIZE, SIZE);
115 /* Initialize pixmap */
116 longrun_init(cpuid_dev, msr_dev);
117 update();
118 dockapp_set_background(pixmap);
119 dockapp_show();
121 /* Main loop */
122 for (;;) {
123 if (dockapp_nextevent_or_timeout(&event, update_interval)) {
124 /* Next Event */
125 switch(event.type) {
126 case ButtonPress:
127 if (!is_led_style) { /* led does not have back-light */
128 switch_light();
129 update();
131 break;
132 default: /* make gcc happy */
133 break;
135 } else {
136 /* Time Out */
137 update();
141 return 0;
144 /* called when mouse button pressed */
145 static void switch_light(void)
147 switch (backlight) {
148 case LIGHTOFF:
149 backlight = LIGHTON;
150 break;
151 case LIGHTON:
152 backlight = LIGHTOFF;
153 break;
158 static void update(void)
160 static int percent; /* LongRun performance level */
161 static int flags; /* LongRun flags */
162 static int mhz; /* LongRun frequency */
163 static int voltz; /* LongRun voltage */
165 int digit1 = 0, digit10 = 0, digit100 = 0, digit1000=0;
167 longrun_get_stat(&percent, &flags, &mhz, &voltz);
169 digit1000 = mhz / 1000;
170 digit100 = (mhz - digit1000 * 1000) / 100;
171 digit10 = (mhz - digit1000 * 1000 - digit100 * 100) / 10;
172 digit1 = mhz - digit1000 * 1000 - digit100 * 100 - digit10 * 10;
174 /* LCD interface */
175 if (!is_led_style) {
176 int y_lrmode = 0, y_gauge = 0, y_digit = 0;
178 /* clear */
179 switch (backlight) {
180 case LIGHTON:
181 if (digit1000==0) {
182 dockapp_copyarea(backdrop_on, pixmap, 0, 0, 58, 58, 0, 0);
184 else {
185 dockapp_copyarea(backdrop_on_g, pixmap, 0, 0, 58, 58, 0, 0);
187 y_lrmode = 11;
188 y_gauge = 14;
189 y_digit = 13;
190 break;
191 case LIGHTOFF:
192 if (digit1000==0) {
193 dockapp_copyarea(backdrop_off, pixmap, 0, 0, 58, 58, 0, 0);
195 else {
196 dockapp_copyarea(backdrop_off_g, pixmap, 0, 0, 58, 58, 0, 0);
198 break;
201 /* longrun flags (performance or economy) */
202 switch (flags) {
203 case LONGRUN_FLAGS_PEFORMANCE:
204 dockapp_copyarea(parts, pixmap, 24, y_lrmode, 24, 11, 31, 5);
205 break;
206 case LONGRUN_FLAGS_ECONOMY:
207 dockapp_copyarea(parts, pixmap, 0, y_lrmode, 24, 11, 5, 5);
208 break;
209 default:
210 break; /* make gcc happy */
213 /* draw digit (frequency) */
214 if (digit1000==0) {
215 dockapp_copyarea(parts, pixmap, digit100*7,y_digit+33, 7,13, 6,22);
216 dockapp_copyarea(parts, pixmap, digit10*7,y_digit+33, 7,13, 15,22);
217 dockapp_copyarea(parts, pixmap, digit1*7,y_digit+33, 7,13, 24,22);
219 else {
220 dockapp_copyarea(parts, pixmap, digit1000*7,y_digit+33, 7,13, 6,22);
221 dockapp_copyarea(parts, pixmap, digit100*7,y_digit+33, 7,13, 15,22);
222 dockapp_copyarea(parts, pixmap, digit10*7,y_digit+33, 7,13, 24,22);
224 /* draw level gauge */
225 dockapp_copyarea(parts, pixmap, 0,y_gauge+72, 49*percent/100,14,5,40);
228 /* LED interface */
229 else {
230 if (!digit1000) {
231 dockapp_copyarea(backdrop_led, pixmap, 0, 0, 58, 58, 0, 0);
233 else {
234 dockapp_copyarea(backdrop_led_g, pixmap, 0, 0, 58, 58, 0, 0);
236 /* longrun flags (performance or economy) */
237 switch (flags) {
238 case LONGRUN_FLAGS_PEFORMANCE:
239 dockapp_copyarea(parts, pixmap, 24, 22, 24, 11, 30, 3);
240 break;
241 case LONGRUN_FLAGS_ECONOMY:
242 dockapp_copyarea(parts, pixmap, 0, 22, 24, 11, 3, 3);
243 break;
244 default:
245 break; /* make gcc happy */
248 /* draw digit (frequency) */
249 if (digit1000==0) {
250 dockapp_copyarea(parts, pixmap, digit100*7,59, 7,13, 4,22);
251 dockapp_copyarea(parts, pixmap, digit10*7,59, 7,13, 13,22);
252 dockapp_copyarea(parts, pixmap, digit1*7,59, 7,13, 22,22);
254 else {
255 dockapp_copyarea(parts, pixmap, digit1000*7,59, 7,13, 4,22);
256 dockapp_copyarea(parts, pixmap, digit100*7,59, 7,13, 13,22);
257 dockapp_copyarea(parts, pixmap, digit10*7,59, 7,13, 22,22);
259 /* draw level gauge */
260 dockapp_copyarea(parts, pixmap, 0, 100, 56 * percent / 100, 16, 1, 41);
263 /* show */
264 dockapp_copy2window(pixmap);
267 static void parse_arguments(int argc, char **argv)
269 int i;
270 for (i = 1; i < argc; i++) {
271 if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h"))
272 print_help(argv[0]), exit(0);
274 else if (!strcmp(argv[i], "--version") || !strcmp(argv[i], "-v"))
275 printf("%s version %s\n", PACKAGE, VERSION), exit(0);
277 else if (!strcmp(argv[i], "--display") || !strcmp(argv[i], "-d")) {
278 display_name = argv[i + 1];
279 i++;
282 else if (!strcmp(argv[i], "--backlight")
283 || !strcmp(argv[i], "-bl"))
284 backlight = LIGHTON;
286 else if (!strcmp(argv[i], "--light-color")
287 || !strcmp(argv[i], "-lc")) {
288 if (argc == i + 1)
289 fprintf(stderr,
290 "%s: error parsing argument for option %s\n",
291 argv[0], argv[i]), exit(1);
292 light_color = argv[i + 1];
293 i++;
296 else if (!strcmp(argv[i], "--interval") || !strcmp(argv[i], "-i")) {
297 int integer;
298 if (argc == i + 1)
299 fprintf(stderr,
300 "%s: error parsing argument for option %s\n",
301 argv[0], argv[i]), exit(1);
302 if (sscanf(argv[i + 1], "%i", &integer) != 1)
303 fprintf(stderr,
304 "%s: error parsing argument for option %s\n",
305 argv[0], argv[i]), exit(1);
306 if (integer < 1)
307 fprintf(stderr, "%s: argument %s must be >=1\n",
308 argv[0], argv[i]), exit(1);
309 update_interval = integer;
310 i++;
313 else if (!strcmp(argv[i], "--led") || !strcmp(argv[i], "-l"))
314 is_led_style = True;
316 else if (!strcmp(argv[i], "--windowed")
317 || !strcmp(argv[i], "-w"))
318 dockapp_iswindowed = True;
320 else if (!strcmp(argv[i], "--broken-wm")
321 || !strcmp(argv[i], "-bw"))
322 dockapp_isbrokenwm = True;
324 #ifdef LINUX
325 else if (!strcmp(argv[i], "--cpuid-device")
326 || !strcmp(argv[i], "-cd")) {
327 cpuid_dev = argv[i + 1];
328 i++;
330 else if (!strcmp(argv[i], "--msr-device")
331 || !strcmp(argv[i], "-md")) {
332 msr_dev = argv[i + 1];
333 i++;
335 #endif
337 else {
338 fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0],
339 argv[i]);
340 print_help(argv[0]), exit(1);
345 static void print_help(char *prog)
347 printf("Usage : %s [OPTIONS]\n", prog);
348 printf
349 ("WMLongRun - A dockapp to monitor the LongRun(tm) status on Crusoe(tm) processors.\n");
350 printf(" -d, --display <string> display to use\n");
351 printf(" -bl, --backlight turn on back-light\n");
352 printf(" -lc, --light-color <string> back-light color(rgb:6E/C6/3B is default)\n");
353 printf(" -i, --interval <number> number of milliseconds between updates\n");
354 printf(" (500 is default)\n");
355 #ifdef LINUX
356 printf(" -cd, --cpuid-device <device> CPUID device ('/dev/cpu/0/cpuid' is default)\n");
357 printf(" -md, --msr-device <device> MSR device ('/dev/cpu/0/msr' is default)\n");
358 #endif
359 printf(" -l, --led run the application with LED interface\n");
360 printf(" -h, --help show this help text and exit\n");
361 printf(" -v, --version show program version and exit\n");
362 printf(" -w, --windowed run the application in windowed mode\n");
363 printf(" -bw, --broken-wm activate broken window manager fix\n");