Add support for keyboard backlight on Apple laptops
[wmlaptop2.git] / src / main.c
blobb9485c7879a5eb531a04f61ebc01f595e52d4c78
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 #include <X11/extensions/scrnsaver.h>
19 #include "main.h"
21 /* cpu's stuff */
22 struct cpuFreq cpuState;
23 u_int8 cpuLoad;
25 /* battery's stuff */
26 struct power_str powerState;
28 /* time counter */
29 time_t secondsCounter;
31 /* command line's args */
32 u_int32 args_cpuUpdate = ARGSDEF_LEAVE;
33 u_int32 args_powerUpdate = ARGSDEF_LEAVE;
34 u_int32 args_tempUpdate = ARGSDEF_LEAVE;
35 u_int32 args_batteryUpdate = ARGSDEF_LEAVE;
36 u_int32 args_sessionIdle = ARGSDEF_LEAVE;
37 char * args_XDisplayName = NULL;
38 char args_autoShutdown = AUTOSHUTDOWN_LEAVE;
39 short args_shutdownDelay = AUTOSHUTDOWN_LEAVE;
40 char args_autoAlarm = AUTOALARM_LEAVE;
41 bool args_beQuiet = ARGSDEF_LEAVE;
42 bool args_dontBlink100 = ARGSDEF_DONTBLINK100;
43 double powerrate = 1;
44 double cap_total = 0;
45 int apple = 0;
47 /* X stuff */
48 struct mouseRegion mouse_region[MAX_MOUSE_REGION];
50 Display *display;
51 Window Root;
52 Window iconwin;
53 Window win;
54 int screen;
55 int x_fd;
56 int d_depth;
57 XSizeHints mysizehints;
58 XWMHints mywmhints;
59 GC NormalGC;
60 XEvent e;
61 struct XpmIcon wmgen;
62 Pixmap pixmask;
64 char wmlaptop_mask_bits[64 * 64];
65 int wmlaptop_mask_width = 64;
66 int wmlaptop_mask_height = 64;
69 int main(int argc, char *argv[])
71 setArgs(argc,argv);
72 init_display();
74 if(!ACPI_canSupport()){
75 fprintf(stderr, "Cannot support ACPI (the file %s doesn't exist)\n", ACPI_ACCESS_TEST);
76 PRINTQ(stderr, ".. and now ? ... I must die\n");
77 free_and_exit(ERROR);
80 battery_capacity();
82 if(args_autoShutdown != AUTOSHUTDOWN_OFF) {
83 PRINTQ(stdout, "Enable autoShutdown at %d%% battery state\n", args_autoShutdown);
84 if(args_shutdownDelay != 0) {
85 PRINTQ(stdout, "Use a shutdown delay of %d minutes\n", args_shutdownDelay);
86 PRINTQ(stdout, "The real poweroff will run at %d%% battery state + %d minutes\n",
87 args_autoShutdown, args_shutdownDelay);
91 fprintf(stdout,"Cpu update = %d milliseconds \n", args_cpuUpdate);
92 fprintf(stdout,"Temperature update = %d seconds \n", args_tempUpdate);
93 fprintf(stdout,"Power update = %d seconds \n", args_powerUpdate);
94 fprintf(stdout,"Battery update = %d seconds \n", args_batteryUpdate);
96 cpuReadTemp();
97 print_battery();
98 event_handler();
99 return SUCCESS;
102 void free_and_exit(int code)
104 if(display)
105 XCloseDisplay(display);
106 exit(code);
110 /* prints help and exits */
111 void usage(char *prog, char *unknowOpt)
113 fprintf(stderr, "%s dockapp (version %s)\n",
114 PROGNAME, VERSION);
115 fprintf(stderr, "It shows battery, cpu and power information\n");
116 fprintf(stderr, "\n");
117 fprintf(stderr, "[usage]: %s [options]\n", prog );
118 fprintf(stderr, "\n");
119 fprintf(stderr, "[X-options]:\n");
120 fprintf(stderr, " --display=NAME set the display to open\n");
121 fprintf( stderr, " --dontblink if you dont want to see \"CPULOAD\" blinking at 100%%\n");
122 fprintf(stderr, "[AUTOSCRIPT-options]:\n");
123 fprintf(stderr, " --auto-shutdown=off|N enable autoshutdown when battery percentage is at\n");
124 fprintf(stderr, " N%% (and AC adapter is not plugged in), or disable\n");
125 fprintf(stderr, " it with 'off'. ('/sbin/shutdown' is used)\n");
126 fprintf(stderr, " --shutdown-delay=N insert a delay in minutes to the shutdown call\n");
127 fprintf(stderr, " --auto-alarm=off|N enable the speaker to play a alarm when battery\n");
128 fprintf(stderr, " percentage is at N%%. (or diseable it with 'off')\n");
129 fprintf(stderr, "[GENERAL-options]:\n");
130 fprintf(stderr, " --cpu-update=N how often, in milliseconds, to update CPU display\n");
131 fprintf(stderr, " --battery-update=N how often, in seconds, to update battery state\n");
132 fprintf(stderr, " --power-update=N how often, in seconds, to update power state\n");
133 fprintf(stderr, " --temperature-update=N how often, in seconds, to update temperature state\n");
134 fprintf(stderr, " --session-idle=N X session inactivity time in seconds after which backlight is reduced\n");
135 fprintf(stderr, " -q --quiet do not print messages and warnings\n");
136 fprintf(stderr, " -d --default show the default compiled settings and exit\n");
137 fprintf(stderr, " -v --version show the version, and exit\n");
138 fprintf(stderr, " -h --help show this message, and exit\n");
139 fprintf(stderr, "\n");
141 if(unknowOpt) {
142 fprintf(stderr, "Unkown Options: %s\n", unknowOpt);
143 fprintf(stderr, "\n");
144 free_and_exit(ERROR);
146 free_and_exit(SUCCESS);
149 void version()
151 fprintf(stderr, "%s dockapp, version %s\n", PROGNAME, VERSION);
152 fprintf(stderr, "\n");
153 fprintf(stderr, "Author: Giacomo, alias ]Matic[, Galilei: matic at libero dot it\n");
154 fprintf(stderr, "Author: Lorenzo, alias ]StClaus[, Marcon: stclaus at libero dot it\n");
155 fprintf(stderr, "Author: Carlos R. Mafra crmafra at gmail.com\n");
156 fprintf(stderr, "\n");
157 free_and_exit(SUCCESS);
160 void setArgs(int argc, char **argv)
162 register int i;
163 char *ptr;
165 for(i = 1; i < argc; i++) {
166 if(!strncmp(argv[i], "--display=", 10)) {
167 EXIT_IF_ALREADY_SET(args_XDisplayName, NULL, "XDisplayName");
168 args_XDisplayName = strchr(argv[i], '=');
169 args_XDisplayName++;
170 continue;
172 if(!strcmp(argv[i], "--dontblink")){
173 args_dontBlink100 = true;
174 continue;
176 if(!strcmp(argv[i], "--apple")){
177 apple = 1;
178 continue;
180 if(!strncmp(argv[i], "--auto-shutdown=", 16)) {
181 EXIT_IF_ALREADY_SET( args_autoShutdown, AUTOSHUTDOWN_LEAVE, "autoShutDown" );
182 if(!strcmp( &argv[i][16], "off"))
183 args_autoShutdown = AUTOSHUTDOWN_OFF;
184 else
185 args_autoShutdown = atoi(&argv[i][16]);
186 continue;
188 if(!strncmp( argv[i], "--shutdown-delay=", 17 ) ) {
189 EXIT_IF_ALREADY_SET( args_shutdownDelay, AUTOSHUTDOWN_LEAVE, "shutDownDelay" );
190 args_shutdownDelay = atoi( &argv[i][17] );
191 continue;
193 if(!strncmp( argv[i], "--auto-alarm=", 13 ) ) {
194 EXIT_IF_ALREADY_SET( args_autoAlarm, AUTOALARM_LEAVE, "autoAlarm" );
195 if(!strcmp( &argv[i][13], "off" ) )
196 args_autoAlarm = AUTOALARM_OFF;
197 else
198 args_autoAlarm = atoi( &argv[i][13] );
199 continue;
201 if(!strncmp(argv[i], "--cpu-update=", 13 ) ) {
202 EXIT_IF_ALREADY_SET( args_cpuUpdate, ARGSDEF_LEAVE, "cpuUpdate");
203 ptr = strchr(argv[i], '=');
204 ptr++;
205 args_cpuUpdate = atoi(ptr);
206 WARNING_IS_SET_TO_ZERO(args_cpuUpdate, "cpuUpdate");
207 continue;
209 if(!strncmp(argv[i], "--battery-update=", 17)) {
210 EXIT_IF_ALREADY_SET(args_batteryUpdate, ARGSDEF_LEAVE, "batteryUpdate");
211 ptr = strchr(argv[i], '=');
212 ptr++;
213 args_batteryUpdate = atoi(ptr);
214 WARNING_IS_SET_TO_ZERO(args_batteryUpdate, "batteryUpdate");
215 continue;
217 if(!strncmp(argv[i], "--power-update=", 11)) {
218 EXIT_IF_ALREADY_SET(args_powerUpdate, ARGSDEF_LEAVE, "powerUpdate");
219 ptr = strchr(argv[i], '=' );
220 ptr++;
221 args_powerUpdate = atoi( ptr );
222 WARNING_IS_SET_TO_ZERO( args_powerUpdate, "powerUpdate");
223 continue;
225 if(!strncmp(argv[i], "--temperature-update=", 11)) {
226 EXIT_IF_ALREADY_SET( args_tempUpdate, ARGSDEF_LEAVE, "tempUpdate");
227 ptr = strchr(argv[i], '=');
228 ptr++;
229 args_tempUpdate = atoi(ptr);
230 WARNING_IS_SET_TO_ZERO(args_tempUpdate, "tempUpdate");
231 continue;
233 if(!strncmp(argv[i], "--session-idle=", 11)) {
234 EXIT_IF_ALREADY_SET( args_sessionIdle, ARGSDEF_LEAVE, "sessionIdle");
235 ptr = strchr(argv[i], '=');
236 ptr++;
237 args_sessionIdle = atoi(ptr);
238 WARNING_IS_SET_TO_ZERO(args_sessionIdle, "sessionIdle");
239 continue;
241 if(!strcmp(argv[i], "-q") || !strcmp(argv[i], "--quiet")) {
242 EXIT_IF_ALREADY_SET(args_beQuiet, ARGSDEF_LEAVE, "beQuiet");
243 args_beQuiet = !ARGSDEF_BEQUIET;
244 continue;
246 if(!strcmp(argv[i], "-d") || !strcmp(argv[i], "--default"))
247 defaultSettings();
248 if(!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version"))
249 version( );
250 if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help"))
251 usage(argv[0], NULL );
252 usage(argv[0], argv[i]);
255 /* setting default values not set by command line */
256 SET_DEFAULT( args_cpuUpdate, ARGSDEF_LEAVE, ARGSDEF_CPUUPDATE );
257 SET_DEFAULT( args_batteryUpdate, ARGSDEF_LEAVE, ARGSDEF_BATTERYUPDATE );
258 SET_DEFAULT( args_powerUpdate, ARGSDEF_LEAVE, ARGSDEF_POWERUPDATE );
259 SET_DEFAULT( args_tempUpdate, ARGSDEF_LEAVE, ARGSDEF_TEMPUPDATE );
260 SET_DEFAULT( args_sessionIdle, ARGSDEF_LEAVE, ARGSDEF_SESSIONIDLE );
261 SET_DEFAULT( args_XDisplayName, NULL, ARGSDEF_XDISPLAYNAME );
262 SET_DEFAULT( args_autoShutdown, AUTOSHUTDOWN_LEAVE, ARGSDEF_AUTOSHUTDOWN );
263 SET_DEFAULT( args_shutdownDelay, AUTOSHUTDOWN_LEAVE, ARGSDEF_SHUTDOWNDELAY );
264 SET_DEFAULT( args_autoAlarm, AUTOALARM_LEAVE, ARGSDEF_AUTOALARM );
265 SET_DEFAULT( args_beQuiet, ARGSDEF_LEAVE, ARGSDEF_BEQUIET );
268 void defaultSettings(void)
270 fprintf( stdout, "The default compiled settings are:\n");
271 fprintf( stdout, "[X-options]:\n");
272 fprintf( stdout, " --display=%s\n", ARGSDEF_XDISPLAYNAME == NULL ? "NULL" : ARGSDEF_XDISPLAYNAME );
273 fprintf( stdout, "[CPU-options]:\n");
274 fprintf( stdout, " --cpu-update=%d\n", ARGSDEF_CPUUPDATE );
275 fprintf( stdout, " --temperature-update=%d\n", ARGSDEF_TEMPUPDATE );
276 fprintf( stdout, " --power-update=%d\n", ARGSDEF_POWERUPDATE );
277 fprintf( stdout, "[BATTERY-options]:\n");
278 fprintf( stdout, " --battery-update=%d\n", ARGSDEF_BATTERYUPDATE );
280 if (ARGSDEF_AUTOSHUTDOWN != AUTOSHUTDOWN_OFF)
281 fprintf( stdout, " --auto-shutdown=%d\n", ARGSDEF_AUTOSHUTDOWN);
282 else
283 fprintf( stdout, " --auto-shutdown=off\n");
284 fprintf( stdout, " --shutdown-delay=%d\n", ARGSDEF_SHUTDOWNDELAY);
286 if (ARGSDEF_AUTOALARM != AUTOALARM_OFF)
287 fprintf(stdout, " --auto-alarm=%d\n", ARGSDEF_AUTOALARM);
288 else
289 fprintf(stdout, " --auto-alarm=off\n");
291 free_and_exit(SUCCESS);
294 float get_session_idle_time(Display *display)
297 XScreenSaverInfo info;
298 float seconds;
300 XScreenSaverQueryInfo(display, DefaultRootWindow(display), &info);
301 seconds = (float)info.idle/1000.0f;
302 return(seconds);