File: apmwatch.c

package info (click to toggle)
osdsh 0.7.0-10
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 264 kB
  • ctags: 136
  • sloc: ansic: 999; sh: 492; tcl: 321; makefile: 123
file content (140 lines) | stat: -rw-r--r-- 3,631 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "osdsh.h"
#include "../config.h"
#include <apm.h>
#define IAM 2
#define MYNAME "APM / Battery"

settings_t apmset;
void *apm_watch(void *arg);

/* ==========================================================================
   ====================== REQUIERED STUFF FOR PLUGINS =======================
   ==========================================================================

***** ID of the plugin, contact me before you asign this to your plugin!! *****
*/

int whoami(void) {
	return IAM;
}

/* Return a pointer to the name of the plugin */
char *mynameis() {
	char *a = MYNAME;
	return a;
}

/* This function sets the default (initual) values for our osd */
void initialize_myosd() {
	set_defaults(&apmset);
}

/* This function takes a command and arguments, if the command is ours, execute
   it and return 1, if not, return 0*/

int isitmine( char command[BUFSIZ], char arg_first[BUFSIZ],
			char arg_secound[BUFSIZ])
{
	int i=1;

	if(strcmp(command, "stop")==0) {
		apmset.displaying = 0;
	}
	else if(strcmp(command, "start")==0) {
		if(!apmset.displaying) {
			apmset.displaying = 1;
			pthread_create(&apmset.mythread, NULL, apm_watch, NULL);
		}
	}
	else if (strcmp(command, "apmw")==0) {
		if (atoi(arg_first)) {
			isitmine("start", NULL, NULL);
		}
		else {
			apmset.displaying = 0;
		}
	}
	else if (strcmp(command, "apms")==0) {
		isitmine("start", NULL, NULL);
		sleep(atoi(arg_first));
		apmset.displaying = 0;
	}
	else if (strcmp(command, "sapm")==0) {
		control_options(&apmset, arg_first, arg_secound);
	}
	else {
		i = 0;
	}
	return i;
}

/*=============================apm_watch()=========================== */
/* some ideas (shamelessly copied ) from apmd's "apm" utility*/
void *apm_watch(void *arg)
{
    int secs;
    apm_info i;
    char battery[255];
    char remainingtime[255];
    char report1[255];

    apmset.myosd = xosd_create(2);
    initialize_osd(&apmset);

    while(apmset.displaying) {
	    if (apm_read(&i))
	    {
		/*fprintf(stderr, "Cannot read APM information\n");*/
		apmset.displaying = 0;
		xosd_display(apmset.myosd, apmset.position==XOSD_bottom?1:0, XOSD_string,
			"Cannot read APM information");
		break;
	    }
	    secs = i.using_minutes ? i.battery_time * 60 : i.battery_time;
            if (i.battery_flags != 0xff	/* Have a 1.1 BIOS and a system battery. ?????? */
		&& i.battery_flags & 0x80) {
		sprintf(battery, "no system battery");
	    }
	    else {
		switch (i.battery_status) {
		case 0:
	    	    sprintf(battery, "high");
		    xosd_set_colour(apmset.myosd, apmset.color);
	    	break;
		case 1:
	    	    sprintf(battery, "low");
		    xosd_set_colour(apmset.myosd, "yellow");
	    	break;
		case 2:
	    	    sprintf(battery, "critical!!");
		    xosd_set_colour(apmset.myosd, "red");
	    	break;
		case 3:
	    	    sprintf(battery, "charging...");
	    	break;
	        }

	    }
	    if (strcmp(battery, "no system battery")==0) {
		xosd_display(apmset.myosd, apmset.position==XOSD_bottom?1:0, XOSD_string,
			"AC on line");
		xosd_display(apmset.myosd, apmset.position==XOSD_bottom?0:1, XOSD_string, battery);
	    }
	    else {
		sprintf(remainingtime, "%dhr, %dmin, %dsec",
				secs/3600,
				(secs%3600)/60,
				((secs%3600)%60)%60);
		sprintf(report1, "AC: %s Remaining battery: %d%%(%s) %s",
			i.ac_line_status?"on line":"off line",i.battery_percentage,
			remainingtime, battery);

		xosd_display(apmset.myosd, 0, XOSD_string, report1);
		if (apmset.displaying > 1)
			xosd_display(apmset.myosd, 1, XOSD_percentage, i.battery_percentage );
		else 
			xosd_display(apmset.myosd, 1, XOSD_string, "");
	    }
    }
    pthread_exit(0);
}