-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
135 lines (106 loc) · 3.07 KB
/
main.c
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <X11/Xlib.h>
#include <alsa/asoundlib.h>
#include <unistd.h>
#define MAXSTR 1024
#define MAXVOLUME 65535
static void XSetRoot(const char * name)
{
Display * display ;
display = XOpenDisplay(0x0) ;
if (display == NULL)
{
//This should not happen, but, you know...
fprintf(stderr, "Cannot open display!\n");
exit(1);
}
XStoreName(display, DefaultRootWindow(display), name) ;
XSync(display, 0) ;
XCloseDisplay(display) ;
}
static void GetCPUUsage(long double * result)
{
long double a[4] ;
long double b[4] ;
FILE * fp ;
fp = fopen("/proc/stat", "r");
fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
fclose(fp);
sleep(1);
fp = fopen("/proc/stat", "r");
fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]);
fclose(fp);
*result = ((b[0] + b[1] + b[2]) - (a[0] + a[1] + a[2]))
/ ((b[0] + b[1] + b[2] + b[3]) - (a[0] + a[1] + a[2] + a[3])) ;
}
static void GetTime(char * result)
{
time_t curtime ;
struct tm * loc_time ;
char temp[99] ;
//Initialize result to empty string.
result[0] = '\0' ;
//Getting current time of system
curtime = time (NULL) ;
// Converting current time to local time
loc_time = localtime (&curtime);
sprintf(temp, "%02d", loc_time->tm_hour) ;
strcat(result, temp);
strcat(result, ":");
sprintf(temp, "%02d", loc_time->tm_min) ;
strcat(result, temp);
strcat(result, ":");
sprintf(temp, "%02d", loc_time->tm_sec) ;
strcat(result, temp);
}
static void GetVolume(int * result)
{
snd_mixer_t * handle ;
snd_mixer_selem_id_t * sid ;
const char * card = "default" ;
const char * selem_name = "Master" ; //Relevant control.
int x ;
long i ;
long currentVolume ;
long currentVdB ;
//Set up ALSA access.
snd_mixer_open(&handle, 0) ;
snd_mixer_attach(handle, card) ;
snd_mixer_selem_register(handle, NULL, NULL) ;
snd_mixer_load(handle) ;
//I don't know what those do...
snd_mixer_selem_id_alloca(&sid) ;
snd_mixer_selem_id_set_index(sid, 0) ;
snd_mixer_selem_id_set_name(sid, selem_name) ;
snd_mixer_elem_t* elem = snd_mixer_find_selem(handle, sid) ;
//Get the volume.
snd_mixer_selem_get_playback_volume (elem, SND_MIXER_SCHN_FRONT_LEFT, ¤tVolume) ;
*result = (double)currentVolume / (double)MAXVOLUME * 100 ;
}
int main(void)
{
char status[MAXSTR] ;
char time [MAXSTR] ;
long double cpuUsage = 0 ;
int temp = 0 ;
char currentVolume[MAXSTR] ;
char tempString[MAXSTR] ;
GetVolume(&temp) ;
snprintf(currentVolume, MAXSTR, "%d", temp) ;
for (;;)
{
GetCPUUsage(&cpuUsage) ;
GetTime(time) ;
snprintf(status, MAXSTR, currentVolume) ;
strcat(status, "|") ;
snprintf(tempString, MAXSTR, "%.2Lf%%", cpuUsage * 100) ;
strcat(status, tempString) ;
strcat(status, "|") ;
strcat(status, time) ;
XSetRoot(status) ;
}
return (0) ;
}