-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatus.c
More file actions
102 lines (79 loc) · 2.8 KB
/
status.c
File metadata and controls
102 lines (79 loc) · 2.8 KB
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
/*
Copyright 2023 Jamie Dennis
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <ctype.h>
static int status_date_and_time(char *buffer, int max) {
time_t time_value = time(NULL);
struct tm *calender_time = gmtime(&time_value);
return strftime(buffer, max, "%a %d %T", calender_time);
}
static int status_memory_usage(char *buffer, int max) {
FILE *meminfo = fopen("/proc/meminfo", "r");
if (!meminfo) return 0;
int ret = 0;
unsigned long total_memory_kb = 0, used_memory_kb = 0;
char line[128];
while (fgets(line, sizeof(line), meminfo)) {
char *value_string;
unsigned long value;
value_string = strchr(line, ':');
if (!value_string) continue;
for (; *value_string; ++value_string) {
if (isdigit(*value_string)) break;
}
value = atoll(value_string);
if (strstr(line, "MemTotal") == line) total_memory_kb = value;
else if (strstr(line, "MemFree") == line) used_memory_kb += value;
else if (strstr(line, "SReclaimable") == line) used_memory_kb += value;
else if (strstr(line, "Buffers") == line) used_memory_kb += value;
else if (strstr(line, "Cached") == line) used_memory_kb += value;
}
ret = snprintf(buffer, max, "%.2g/%.2gGB",
(total_memory_kb - used_memory_kb) / (double)(1<<20),
total_memory_kb / (double)(1<<20));
fclose(meminfo);
return ret;
}
#ifdef ENABLE_MPD_STATUS
#include <mpd/connection.h>
#include <mpd/client.h>
#include <mpd/song.h>
static int status_mpd(char *buffer, int max) {
int ret = 0;
struct mpd_connection *mpd;
mpd = mpd_connection_new(NULL, 0, 0);
if (!mpd) return 0;
if (mpd_connection_get_error(mpd) != MPD_ERROR_SUCCESS) {
printf("Failed to connect to MPD: %s\n", mpd_connection_get_error_message(mpd));
mpd_connection_free(mpd);
return 0;
}
struct mpd_song *song = mpd_run_current_song(mpd);
if (song) {
const char *artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
const char *title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
if (!artist || !title) {
const char *uri = mpd_song_get_uri(song);
title = strrchr(uri, '/');
if (title) title++;
else title = uri;
ret = snprintf(buffer, max, "%s", title);
}
else {
ret = snprintf(buffer, max, "%s - %s", artist, title);
}
}
mpd_song_free(song);
mpd_connection_free(mpd);
return ret;
}
#endif