updated on Mon Jan 16 20:00:43 UTC 2012
[aur-mirror.git] / acpi-eee1000 / asusosd.patch
blob49da8f6f9d6e8715ca2934e59bb71ad0c72be736
1 diff -Naur old/asus_osd/asusosd.c new/asus_osd/asusosd.c
2 --- old/asus_osd/asusosd.c 2008-10-20 09:18:35.000000000 -0500
3 +++ new/asus_osd/asusosd.c 2008-10-22 21:26:38.000000000 -0500
4 @@ -3,6 +3,7 @@
5 #include <string.h>
6 #include <unistd.h>
7 #include <pthread.h>
8 +#include <syslog.h>
9 #include <fcntl.h>
10 #include <sys/socket.h>
11 #include <sys/stat.h>
12 @@ -14,20 +15,27 @@
13 #include <errno.h>
15 #define VOLUME_CONTROL "/usr/bin/volume_control.sh"
16 -#define RADIO_CONTROL "/usr/bin/radio_control.sh"
18 +#define PWRMGMT_FILE "/var/state/fsb/osd"
20 static pthread_mutex_t q_lock;
21 static pthread_cond_t q_empty;
23 static int thread_alive;
25 +static int cfg_flags = 0;
27 char filename[128];
28 +char btfilename[128] = {'\0'};
30 unsigned int img_data;
31 int wlan_value;
32 int bri_value;
33 int vol_value;
34 int vol_level;
35 +int rpause;
37 +int connect_acpid();
38 char* hotkey_get(int);
39 unsigned int hotkey_parse(char*);
41 @@ -40,7 +48,10 @@
42 void image_open();
44 int getvol(int);
45 +int getfsb();
46 +int getbt();
47 int pipe_cmd(char pipe_cmd[]);
48 +static int read_config(char config[]);
50 int
51 main (int argc, char *argv[])
52 @@ -48,11 +59,11 @@
53 queue *img_queue;
54 pthread_t img_thread;
55 int socket_fd;
56 - struct sockaddr_un socket_addr;
57 char *hotkey;
58 unsigned int data;
59 FILE *pfile;
60 int i;
61 + int j;
62 char cmd[64];
64 i = 1;
65 @@ -71,73 +82,68 @@
66 pthread_cond_init(&q_empty, NULL);
67 pthread_create(&img_thread, NULL, image_thread, img_queue);
69 - socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
70 - if (socket_fd < 0) {
71 - printf("cannot create socket\n");
72 - return socket_fd;
73 - }
75 - memset(&socket_addr, 0, sizeof(socket_addr));
76 - socket_addr.sun_family = AF_UNIX;
77 - sprintf(socket_addr.sun_path, "%s", "/var/run/acpid.socket");
79 - if(connect(socket_fd, (struct sockaddr *)&socket_addr, sizeof(socket_addr)) < 0)
80 - {
81 - printf("cannot connect socket\n");
82 - close(socket_fd);
83 - return -1;
84 - }
86 - fcntl(socket_fd, F_SETFD, FD_CLOEXEC);
87 + socket_fd = connect_acpid();
88 setvbuf(stdout, NULL, _IOLBF, 0);
90 bri_value = 0;
92 if((pfile=fopen("/proc/acpi/asus/brn","rb"))==NULL)
94 - printf("cannot open /proc/acpi/asus/brn\n");
95 + syslog(LOG_ERR, "cannot open /proc/acpi/asus/brn\n");
97 else
99 if(fscanf(pfile, "%d", &bri_value) != 1)
101 - printf("cannot read /proc/acpi/asus/brn\n");
102 + syslog(LOG_ERR, "cannot read /proc/acpi/asus/brn\n");
104 if(bri_value < 0 || 15 < bri_value)
106 bri_value = 0;
107 - printf("read an invalid value [%d] from /proc/acpi/asus/brn\n", bri_value);
108 + syslog(LOG_ERR, "read an invalid value [%d] from /proc/acpi/asus/brn\n", bri_value);
110 fclose(pfile);
113 - if((pfile=fopen("/proc/acpi/asus/wlan", "rb"))==NULL)
115 - printf("cannot read /proc/acpi/asus/wlan\n");
116 - close(socket_fd);
117 - return -1;
119 - if(fscanf(pfile, "%d", &wlan_value) != 1)
121 - printf("cannot read /proc/acpi/asus/wlan\n");
122 - close(socket_fd);
123 - fclose(pfile);
124 - return -1;
126 - fclose(pfile);
127 - if(wlan_value == 1)
128 - system("echo 1 > /proc/acpi/asus/wlan");
129 - else if(wlan_value == 0)
130 - system("echo 0 > /proc/acpi/asus/wlan");
131 - else
132 - printf("read an invalid value [%d] from /proc/acpi/asus/wlan\n", wlan_value);
133 - */
136 + /* Read the config file, see what is enabled */
137 + cfg_flags = read_config("/etc/acpi/eee.conf");
139 while(1)
141 hotkey = hotkey_get(socket_fd);
142 if(hotkey)
144 + if (!strncmp("-1", hotkey, 2))
146 + syslog(LOG_ERR, "Broken pipe: Attempting to reconnect\n");
147 + close(socket_fd);
148 + socket_fd = 0;
149 + rpause = 500000;
150 + for (j=1; j<6; j++)
152 + usleep(rpause);
153 + socket_fd = connect_acpid();
154 + if (socket_fd < 1)
156 + syslog(LOG_ERR, "Attempt %d: failed\n", j);
157 + rpause = rpause*2;
159 + else
161 + syslog(LOG_ERR, "Attempt %d: connected. Resuming.\n", j);
162 + break;
166 + if (socket_fd < 1)
168 + syslog(LOG_ERR, "Unable to connect. Exiting.\n");
169 + exit(1);
172 + continue;
175 if(strncmp("hotkey ATKD ", hotkey, 12))
176 continue;
177 data = hotkey_parse(hotkey+12);
178 @@ -180,6 +186,33 @@
179 return 0;
182 +int
183 +connect_acpid()
185 + int sfd = 0;
186 + struct sockaddr_un socket_addr;
188 + sfd = socket(AF_UNIX, SOCK_STREAM, 0);
189 + if (sfd < 0) {
190 + syslog(LOG_ERR, "cannot create socket\n");
191 + return sfd;
194 + memset(&socket_addr, 0, sizeof(socket_addr));
195 + socket_addr.sun_family = AF_UNIX;
196 + sprintf(socket_addr.sun_path, "%s", "/var/run/acpid.socket");
198 + if(connect(sfd, (struct sockaddr *)&socket_addr, sizeof(socket_addr)) < 0)
200 + syslog(LOG_ERR, "cannot connect to socket: /var/run/acpid.socket\n");
201 + close(sfd);
202 + return -1;
205 + fcntl(sfd, F_SETFD, FD_CLOEXEC);
206 + return sfd;
209 char*
210 hotkey_get(int sfd)
212 @@ -192,7 +225,7 @@
213 while (searching) {
214 buf = realloc(buf, buflen);
215 if (!buf) {
216 - fprintf(stderr, "ERR: malloc(%d): %s\n",
217 + syslog(LOG_ERR, "ERR: malloc(%d): %s\n",
218 buflen, strerror(errno));
219 return NULL;
221 @@ -202,13 +235,13 @@
222 r = read(sfd, buf+i, 1);
223 if (r < 0 && errno != EINTR) {
224 /* we should do something with the data */
225 - fprintf(stderr, "ERR: read(): %s\n",
226 + syslog(LOG_ERR, "ERR: read(): %s\n",
227 strerror(errno));
228 return NULL;
229 } else if (r == 0) {
230 /* signal this in an almost standard way */
231 errno = EPIPE;
232 - return NULL;
233 + return "-1";
234 } else if (r == 1) {
235 /* scan for a newline */
236 if (buf[i] == '\n') {
237 @@ -253,6 +286,18 @@
238 hotkey = IMG_BRN_DN|(val<<8);
239 bri_value = val;
241 + else if (data[6] == '1' && data[7] == 'c')
243 + hotkey = getfsb();
245 + else if (data[6] == '1' && data[7] == 'd')
247 + if ((cfg_flags & 4) == 4)
249 + hotkey = getbt();
251 + else return 0;
253 else
255 data[8] = 0;
256 @@ -261,8 +306,14 @@
258 case 13:printf("@@ audio mute %d\n", hotkey);
259 //audio mute
260 - if(!getvol(1)) //off
261 - hotkey = 16;//then on
262 + if(getvol(1)) //get volume status: 1=on 0=off
264 + hotkey = 16;
265 + printf("Volume on\n");
266 + }else{
267 + hotkey = 13;
268 + printf("Volume off\n");
269 + }
270 printf("@@ audio mute %d\n", hotkey);
271 break;
272 case 14: //volume down
273 @@ -306,10 +357,24 @@
274 sprintf(filename, "%s%d%s", "/usr/share/asus_osd/VolumeDN",val,".png");
275 else if ((img_data&IMAGE_MASK) == IMG_VOL_UP)
276 sprintf(filename, "%s%d%s", "/usr/share/asus_osd/VolumeUP",val,".png");
277 - else if ((img_data&IMAGE_MASK) == IMG_AC_IN)
278 + else if ((img_data&IMAGE_MASK) == IMG_AC_IN && (cfg_flags & 2) != 2)
279 sprintf(filename, "%s", "/usr/share/asus_osd/ACMode.png");
280 - else if ((img_data&IMAGE_MASK) == IMG_AC_OUT)
281 + else if ((img_data&IMAGE_MASK) == IMG_AC_IN && (cfg_flags & 2) == 2)
282 + sprintf(filename, "%s", "/usr/share/asus_osd/ACMode_performance.png");
283 + else if ((img_data&IMAGE_MASK) == IMG_AC_OUT && (cfg_flags & 2) != 2)
284 sprintf(filename, "%s", "/usr/share/asus_osd/BatteryMode.png");
285 + else if ((img_data&IMAGE_MASK) == IMG_AC_OUT && (cfg_flags & 2) == 2)
286 + sprintf(filename, "%s", "/usr/share/asus_osd/BatteryMode_powersave.png");
287 + else if ((img_data&IMAGE_MASK) == IMG_FSB_PWRSAVE && (cfg_flags & 1) == 1)
288 + sprintf(filename, "%s", "/usr/share/asus_osd/PowerSaving.png");
289 + else if ((img_data&IMAGE_MASK) == IMG_FSB_HIGH && (cfg_flags & 1) == 1)
290 + sprintf(filename, "%s", "/usr/share/asus_osd/HighPerformance.png");
291 + else if ((img_data&IMAGE_MASK) == IMG_FSB_SUPER && (cfg_flags & 1) == 1)
292 + sprintf(filename, "%s", "/usr/share/asus_osd/SuperPerformance.png");
293 + else if ((img_data&IMAGE_MASK) == IMG_BLUETOOTH_ON && (cfg_flags & 4) == 4)
294 + sprintf(filename, "%s", "/usr/share/asus_osd/BluetoothOn.png");
295 + else if ((img_data&IMAGE_MASK) == IMG_BLUETOOTH_OFF && (cfg_flags & 4) == 4)
296 + sprintf(filename, "%s", "/usr/share/asus_osd/BluetoothOff.png");
298 else return -1;
300 @@ -361,7 +426,7 @@
301 sleep(4);*/
302 create_image(filename);
304 - else printf("image_open error.\n");
305 + else syslog(LOG_ERR, "image_open error.\n");
308 void image_close()
309 @@ -392,6 +457,7 @@
311 else //get status
313 + usleep(100000);
314 sprintf(volcmd, "%s status", VOLUME_CONTROL);
315 if(!pipe_cmd(volcmd))
317 @@ -400,11 +466,141 @@
318 return 1;
319 else if(strstr(volcmd, "[off]"))
320 return 0;
321 - }else printf("@@ pipe cmd fail\n");
322 + }else syslog(LOG_ERR, "pipe cmd fail\n");
323 return 0;
327 +int getfsb()
329 + FILE *statefile = NULL;
330 + char line[80];
331 + int i = 0;
332 + int fsb_value = 0;
333 + int opened = 0;
335 + /* sleep a millisecond between attempts to open the file,
336 + * for two seconds, then fail */
337 + for (i=0; i<2000; i++)
339 + statefile = fopen(PWRMGMT_FILE, "r");
340 + if (statefile == NULL)
342 + usleep(10000);
343 + continue;
346 + opened = 1;
348 + /* Read the file. If it doesn't contain
349 + * a valid state, close it, sleep, then try again */
350 + if (fgets(line, 80, statefile) != NULL)
352 + line[strlen(line)-1] = '\0';
353 + if (!strcmp(line, "PWRSAV"))
355 + fsb_value = IMG_FSB_PWRSAVE;
357 + else if (!strcmp(line, "HIPERF"))
359 + fsb_value = IMG_FSB_HIGH;
361 + else if (!strcmp(line, "SPPERF"))
363 + fsb_value = IMG_FSB_SUPER;
368 + fclose(statefile);
369 + statefile = NULL;
371 + if (fsb_value != 0)
373 + /* Got the value. Reset the OSD state file and break. */
374 + statefile = fopen(PWRMGMT_FILE, "w");
375 + if (statefile == NULL)
377 + syslog(LOG_ERR, "Unable to reset OSD state file '%s': %s. Returning.", PWRMGMT_FILE, strerror(errno));
379 + else
381 + fclose(statefile);
382 + statefile = NULL;
384 + break;
387 + usleep(10000);
390 + if (opened == 0)
392 + syslog(LOG_ERR, "Unable to open OSD state file '%s': %s. Returning.", PWRMGMT_FILE, strerror(errno));
393 + return 0;
395 + else if (fsb_value == 0)
397 + syslog(LOG_ERR, "Unable to determine FSB state in file '%s'. Returning.", PWRMGMT_FILE);
398 + return 0;
401 + return fsb_value;
404 +int getbt()
406 + FILE *btRead = NULL;
407 + int bt_toggle = 999;
408 + char buf[2] = {'\0'};
410 + if (strlen(btfilename) == 0)
412 + syslog(LOG_ERR, "NO BLUETOOTH_SWITCH file configured. Returning.\n");
413 + return 0;
416 + usleep(500000);
417 + btRead = fopen(btfilename, "r");
418 + if (btRead != NULL)
420 + if (fread(buf, 1, 1, btRead) != 1)
422 + syslog(LOG_ERR, "Cannot read '%s'. Returning.\n", btfilename);
423 + fclose(btRead);
424 + return 0;
427 + buf[1] = '\0';
428 + bt_toggle = atoi(buf);
430 + if (bt_toggle == 0)
432 + return IMG_BLUETOOTH_OFF;
434 + else if (bt_toggle == 1)
436 + return IMG_BLUETOOTH_ON;
438 + else
440 + syslog(LOG_ERR, "Invalid value returned from '%s': '%d'. Returning.\n", btfilename, bt_toggle);
441 + fclose(btRead);
442 + return 0;
445 + fclose(btRead);
446 + btRead = NULL;
448 + else
450 + syslog(LOG_ERR, "Unable to read '%s': %s. Returning.", btfilename, strerror(errno));
451 + return 0;
454 + return 0;
457 int pipe_cmd(char cmd[])
459 int iData = 1,iCount = 0,iSize = sizeof(cmd);
460 @@ -438,3 +634,58 @@
461 return(EXIT_FAILURE);
464 +static int read_config(char config[])
466 + FILE *configfile;
467 + char line[80];
468 + int flags = 0;
470 + configfile = fopen(config, "r");
471 + if (configfile != NULL)
473 + while(fgets(line, 80, configfile) != NULL)
475 + if (line[0] == '#')
477 + //try to ignore comments
479 + else if (strstr(line, "ENABLE_PWRMGMT=Y") != NULL)
481 + flags = flags | 1;
483 + else if (strstr(line, "ENABLE_PWRMGMT=y") != NULL)
485 + flags = flags | 1;
487 + else if (strstr(line, "ENABLE_PWRMGMT_AUTO=Y") != NULL)
489 + flags = flags | 2;
491 + else if (strstr(line, "ENABLE_PWRMGMT_AUTO=y") != NULL)
493 + flags = flags | 2;
495 + else if (strstr(line, "ENABLE_BLUETOOTH=Y") != NULL)
497 + flags = flags | 4;
499 + else if (strstr(line, "ENABLE_BLUETOOTH=y") != NULL)
501 + flags = flags | 4;
503 + else if (strstr(line, "BLUETOOTH_SWITCH=") != NULL)
505 + sscanf(line, "BLUETOOTH_SWITCH=%*[\"\']%[^\"\']", btfilename);
510 + else
512 + syslog(LOG_ERR, "Unable to open configuration file '%s': defaulting.", config);
515 + fclose(configfile);
517 + return flags;
519 diff -Naur old/asus_osd/asusosd_image.c new/asus_osd/asusosd_image.c
520 --- old/asus_osd/asusosd_image.c 2008-10-20 09:18:36.000000000 -0500
521 +++ new/asus_osd/asusosd_image.c 2008-10-21 18:05:40.000000000 -0500
522 @@ -1,6 +1,7 @@
523 #include <stdio.h>
524 #include <stdlib.h>
525 #include <string.h>
526 +#include <syslog.h>
527 #include <X11/Xlib.h>
528 #include <X11/Xutil.h>
529 #include <X11/Xos.h>
530 @@ -17,6 +18,7 @@
531 static int xwindow_create(void);
532 static int ximage_create(void);
533 static int ximage_display(void);
534 +static void read_pos_from_config(char config[], int *x, int *y);
535 void destroy_osd(void);
536 //static void destroy_image(void);
537 static int x_msb(unsigned long u32val);
538 @@ -50,7 +52,7 @@
539 return -1;
540 else if (!(pngfile = fopen(filename, "rb")))
542 - printf(": can't open PNG file [%s]\n", filename);
543 + syslog(LOG_ERR, ": can't open PNG file [%s]\n", filename);
544 return -1;
546 else {
547 @@ -64,7 +66,7 @@
549 if (readpng_get_bgcolor(&bg_red, &bg_green, &bg_blue) > 1) {
550 readpng_cleanup(TRUE);
551 - printf(": libpng error while checking for background color\n");
552 + syslog(LOG_ERR, ": libpng error while checking for background color\n");
553 return -2;
556 @@ -85,7 +87,7 @@
557 fclose(pngfile);
559 if (!image_data) {
560 - printf(": unable to decode PNG image\n");
561 + syslog(LOG_ERR, ": unable to decode PNG image\n");
562 destroy_osd();
563 return -3;
565 @@ -115,7 +117,8 @@
566 XDestroyImage(ximage);
567 ximage = NULL;
570 + XDestroyWindow(display, window);
571 + have_window = FALSE;
573 else return -1;
574 return 0;
575 @@ -199,7 +202,7 @@
576 if (depth == 8 || need_colormap) {
577 colormap = XCreateColormap(display, root, visual, AllocNone);
578 if (!colormap) {
579 - printf("XCreateColormap() failed\n");
580 + syslog(LOG_ERR, "XCreateColormap() failed\n");
581 return 2;
583 have_colormap = TRUE;
584 @@ -221,7 +224,7 @@
585 #endif
587 if (depth >= 15 && (RShift < 0 || GShift < 0 || BShift < 0)) {
588 - printf("rpng internal logic error: negative X shift(s)!\n");
589 + syslog(LOG_ERR, "rpng internal logic error: negative X shift(s)!\n");
590 return 2;
593 @@ -236,11 +239,14 @@
594 attr.border_pixel = 0;
595 x = 2;//(XDefaultScreenOfDisplay(display)->width - image_width)/2;
596 y = 2;//(XDefaultScreenOfDisplay(display)->height - image_height)/2;
598 + read_pos_from_config("/etc/acpi/eee.conf", &x, &y);
600 window = XCreateWindow(display, root, x, y, image_width, image_height, 0,
601 depth, InputOutput, visual, attrmask, &attr);
603 if (window == None) {
604 - printf("XCreateWindow() failed\n");
605 + syslog(LOG_ERR, "XCreateWindow() failed\n");
606 return 2;
607 } else
608 have_window = TRUE;
609 @@ -343,7 +349,7 @@
612 if (!xdata) {
613 - printf(": unable to allocate image memory\n");
614 + syslog(LOG_ERR, ": unable to allocate image memory\n");
615 return 4;
618 @@ -360,7 +366,7 @@
619 (char *)xdata, image_width, image_height, pad, 0);
621 if (!ximage) {
622 - printf(": XCreateImage() failed\n");
623 + syslog(LOG_ERR, ": XCreateImage() failed\n");
624 free(xdata);
625 return 3;
627 @@ -545,3 +551,46 @@
629 return i;
632 +static void read_pos_from_config(char config[], int *x, int *y)
634 + FILE *configfile;
635 + char line[80];
637 + configfile = fopen(config, "r");
638 + if (configfile != NULL)
640 + while(fgets(line, 80, configfile) != NULL)
642 + if (line[0] == '#')
644 + //try to ignore comments
646 + else if (strstr(line, "ASUSOSD_XPOS=center") != NULL)
648 + *x = (XDefaultScreenOfDisplay(display)->width - image_width)/2;
650 + else if (strstr(line, "ASUSOSD_XPOS=") != NULL)
651 + {
652 + sscanf(line,"ASUSOSD_XPOS=%i" , x);
654 + else if (strstr(line, "ASUSOSD_YPOS=center") != NULL)
656 + *y = (XDefaultScreenOfDisplay(display)->height - image_height)/2;
658 + else if (strstr(line, "ASUSOSD_YPOS=") != NULL)
659 + {
660 + sscanf(line,"ASUSOSD_YPOS=%i" , y);
664 + else
666 + syslog(LOG_ERR, "Unable to open configuration file '%s': defaulting.", config);
669 + fclose(configfile);
674 diff -Naur old/asus_osd/asusosd_image.h new/asus_osd/asusosd_image.h
675 --- old/asus_osd/asusosd_image.h 2008-10-20 09:18:36.000000000 -0500
676 +++ new/asus_osd/asusosd_image.h 2008-10-21 17:48:13.000000000 -0500
677 @@ -42,4 +42,10 @@
678 #define IMG_AC_IN 50//0x0050
679 #define IMG_AC_OUT 51//0x0051
681 +#define IMG_FSB_PWRSAVE 60//0x0060
682 +#define IMG_FSB_HIGH 61//0x0061
683 +#define IMG_FSB_SUPER 62//0x0062
685 +#define IMG_BLUETOOTH_ON 70//0x0070
686 +#define IMG_BLUETOOTH_OFF 71//0x0071
687 #endif