My slstatus configuration
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

slstatus.c 9.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /* See LICENSE file for copyright and license details. */
  2. /* global libraries */
  3. #include <alsa/asoundlib.h>
  4. #include <fcntl.h>
  5. #include <locale.h>
  6. #include <stdarg.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <sys/statvfs.h>
  13. #include <time.h>
  14. #include <unistd.h>
  15. #include <X11/Xlib.h>
  16. /* local libraries */
  17. #include "config.h"
  18. /* check file macro */
  19. #define CHECK_FILE(X,Y) do { \
  20. if (stat(X,&Y) < 0) return -1; \
  21. if (!S_ISREG(Y.st_mode)) return -1; \
  22. } while (0);
  23. /* functions */
  24. int config_check();
  25. void setstatus(char *str);
  26. char *smprintf(char *fmt, ...);
  27. char *get_battery();
  28. char *get_cpu_temperature();
  29. char *get_cpu_usage();
  30. char *get_datetime();
  31. char *get_diskusage();
  32. char *get_ram_usage();
  33. char *get_volume();
  34. char *get_wifi_signal();
  35. /* global variables */
  36. static Display *dpy;
  37. /* check configured paths */
  38. int
  39. config_check()
  40. {
  41. struct stat fs;
  42. /* check all files in the config.h file */
  43. CHECK_FILE(batterynowfile, fs);
  44. CHECK_FILE(batteryfullfile, fs);
  45. CHECK_FILE(tempfile, fs);
  46. /* check update interval */
  47. if (update_interval < 1)
  48. return -1;
  49. /* exit successfully */
  50. return 0;
  51. }
  52. /* set statusbar (WM_NAME) */
  53. void
  54. setstatus(char *str)
  55. {
  56. XStoreName(dpy, DefaultRootWindow(dpy), str);
  57. XSync(dpy, False);
  58. }
  59. /* smprintf function */
  60. char *
  61. smprintf(char *fmt, ...)
  62. {
  63. va_list fmtargs;
  64. char *ret = NULL;
  65. va_start(fmtargs, fmt);
  66. if (vasprintf(&ret, fmt, fmtargs) < 0)
  67. return NULL;
  68. va_end(fmtargs);
  69. return ret;
  70. }
  71. /* battery percentage */
  72. char *
  73. get_battery()
  74. {
  75. int now, full, perc;
  76. FILE *fp;
  77. /* open battery now file */
  78. if (!(fp = fopen(batterynowfile, "r"))) {
  79. fprintf(stderr, "Error opening battery file.");
  80. return smprintf("n/a");
  81. }
  82. /* read value */
  83. fscanf(fp, "%i", &now);
  84. /* close battery now file */
  85. fclose(fp);
  86. /* open battery full file */
  87. if (!(fp = fopen(batteryfullfile, "r"))) {
  88. fprintf(stderr, "Error opening battery file.");
  89. return smprintf("n/a");
  90. }
  91. /* read value */
  92. fscanf(fp, "%i", &full);
  93. /* close battery full file */
  94. fclose(fp);
  95. /* calculate percent */
  96. perc = now / (full / 100);
  97. /* return perc as string */
  98. return smprintf("%d%%", perc);
  99. }
  100. /* cpu temperature */
  101. char *
  102. get_cpu_temperature()
  103. {
  104. int temperature;
  105. FILE *fp;
  106. /* open temperature file */
  107. if (!(fp = fopen(tempfile, "r"))) {
  108. fprintf(stderr, "Could not open temperature file.\n");
  109. return smprintf("n/a");
  110. }
  111. /* extract temperature */
  112. fscanf(fp, "%d", &temperature);
  113. /* close temperature file */
  114. fclose(fp);
  115. /* return temperature in degrees */
  116. return smprintf("%d°C", temperature / 1000);
  117. }
  118. /* cpu percentage */
  119. char *
  120. get_cpu_usage()
  121. {
  122. int perc;
  123. long double a[4], b[4];
  124. FILE *fp;
  125. /* open stat file */
  126. if (!(fp = fopen("/proc/stat","r"))) {
  127. fprintf(stderr, "Error opening stat file.");
  128. return smprintf("n/a");
  129. }
  130. /* read values */
  131. fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
  132. /* close stat file */
  133. fclose(fp);
  134. /* wait a second (for avg values) */
  135. sleep(1);
  136. /* open stat file */
  137. if (!(fp = fopen("/proc/stat","r"))) {
  138. fprintf(stderr, "Error opening stat file.");
  139. return smprintf("n/a");
  140. }
  141. /* read values */
  142. fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]);
  143. /* close stat file */
  144. fclose(fp);
  145. /* calculate avg in this second */
  146. perc = 100 * ((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]));
  147. /* return perc as string */
  148. return smprintf("%d%%", perc);
  149. }
  150. /* date and time */
  151. char *
  152. get_datetime()
  153. {
  154. time_t tm;
  155. size_t bufsize = 64;
  156. char *buf = malloc(bufsize);
  157. /* get time in format */
  158. time(&tm);
  159. setlocale(LC_TIME, "");
  160. if(!strftime(buf, bufsize, timeformat, localtime(&tm))) {
  161. setlocale(LC_TIME, "C");
  162. fprintf(stderr, "Strftime failed.\n");
  163. return smprintf("n/a");
  164. }
  165. setlocale(LC_TIME, "C");
  166. /* return time */
  167. return smprintf("%s", buf);
  168. }
  169. /* disk usage percentage */
  170. char *
  171. get_diskusage()
  172. {
  173. int perc = 0;
  174. struct statvfs fs;
  175. /* try to open mountpoint */
  176. if (statvfs(mountpath, &fs) < 0) {
  177. fprintf(stderr, "Could not get filesystem info.\n");
  178. return smprintf("n/a");
  179. }
  180. /* calculate percent */
  181. perc = 100 * (1.0f - ((float)fs.f_bavail / (float)fs.f_blocks));
  182. /* return perc */
  183. return smprintf("%d%%", perc);
  184. }
  185. /* ram percentage */
  186. char *
  187. get_ram_usage()
  188. {
  189. int perc;
  190. long total, free, buffers, cached;
  191. FILE *fp;
  192. /* open meminfo file */
  193. if (!(fp = fopen("/proc/meminfo", "r"))) {
  194. fprintf(stderr, "Error opening meminfo file.");
  195. return smprintf("n/a");
  196. }
  197. /* read the values */
  198. fscanf(fp, "MemTotal: %ld kB\n", &total);
  199. fscanf(fp, "MemFree: %ld kB\n", &free);
  200. fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
  201. fscanf(fp, "Cached: %ld kB\n", &cached);
  202. /* close meminfo file */
  203. fclose(fp);
  204. /* calculate percentage */
  205. perc = 100 * ((total - free) - (buffers + cached)) / total;
  206. /* return perc as string */
  207. return smprintf("%d%%", perc);
  208. }
  209. /* alsa volume percentage */
  210. char *
  211. get_volume()
  212. {
  213. int mute = 0;
  214. long vol = 0, max = 0, min = 0;
  215. /* get volume from alsa */
  216. snd_mixer_t *handle;
  217. snd_mixer_elem_t *pcm_mixer, *mas_mixer;
  218. snd_mixer_selem_id_t *vol_info, *mute_info;
  219. snd_mixer_open(&handle, 0);
  220. snd_mixer_attach(handle, soundcard);
  221. snd_mixer_selem_register(handle, NULL, NULL);
  222. snd_mixer_load(handle);
  223. snd_mixer_selem_id_malloc(&vol_info);
  224. snd_mixer_selem_id_malloc(&mute_info);
  225. snd_mixer_selem_id_set_name(vol_info, channel);
  226. snd_mixer_selem_id_set_name(mute_info, channel);
  227. pcm_mixer = snd_mixer_find_selem(handle, vol_info);
  228. mas_mixer = snd_mixer_find_selem(handle, mute_info);
  229. snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t *)pcm_mixer, &min, &max);
  230. snd_mixer_selem_get_playback_volume((snd_mixer_elem_t *)pcm_mixer, SND_MIXER_SCHN_MONO, &vol);
  231. snd_mixer_selem_get_playback_switch(mas_mixer, SND_MIXER_SCHN_MONO, &mute);
  232. if (vol_info)
  233. snd_mixer_selem_id_free(vol_info);
  234. if (mute_info)
  235. snd_mixer_selem_id_free(mute_info);
  236. if (handle)
  237. snd_mixer_close(handle);
  238. /* return the string (mute) */
  239. if (!mute)
  240. return smprintf("mute");
  241. else
  242. return smprintf("%d%%", (vol * 100) / max);
  243. }
  244. /* wifi percentage */
  245. char *
  246. get_wifi_signal()
  247. {
  248. int bufsize = 255;
  249. int strength;
  250. char buf[bufsize];
  251. char *datastart;
  252. char path_start[16] = "/sys/class/net/";
  253. char path_end[11] = "/operstate";
  254. char path[32];
  255. char status[5];
  256. char needle[sizeof wificard + 1];
  257. FILE *fp;
  258. /* generate the path name */
  259. memset(path, 0, sizeof path);
  260. strcat(path, path_start);
  261. strcat(path, wificard);
  262. strcat(path, path_end);
  263. /* open wifi file */
  264. if(!(fp = fopen(path, "r"))) {
  265. fprintf(stderr, "Error opening wifi operstate file.");
  266. return smprintf("n/a");
  267. }
  268. /* read the status */
  269. fgets(status, 5, fp);
  270. /* close wifi file */
  271. fclose(fp);
  272. /* check if interface down */
  273. if(strcmp(status, "up\n") != 0){
  274. return smprintf("n/a");
  275. }
  276. /* open wifi file */
  277. if (!(fp = fopen("/proc/net/wireless", "r"))) {
  278. fprintf(stderr, "Error opening wireless file.");
  279. return smprintf("n/a");
  280. }
  281. /* extract the signal strength */
  282. strcpy(needle, wificard);
  283. strcat(needle, ":");
  284. fgets(buf, bufsize, fp);
  285. fgets(buf, bufsize, fp);
  286. fgets(buf, bufsize, fp);
  287. if ((datastart = strstr(buf, needle)) != NULL) {
  288. datastart = strstr(buf, ":");
  289. sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength);
  290. }
  291. /* close wifi file */
  292. fclose(fp);
  293. /* return strength in percent */
  294. return smprintf("%d%%", strength);
  295. }
  296. /* main function */
  297. int
  298. main()
  299. {
  300. char status[1024];
  301. char *battery = NULL;
  302. char *cpu_temperature = NULL;
  303. char *cpu_usage = NULL;
  304. char *datetime = NULL;
  305. char *diskusage = NULL;
  306. char *ram_usage = NULL;
  307. char *volume = NULL;
  308. char *wifi_signal = NULL;
  309. /* check config for sanity */
  310. if (config_check() < 0) {
  311. fprintf(stderr, "Config error, please check paths and interval and recompile!\n");
  312. exit(1);
  313. }
  314. /* open display */
  315. if (!(dpy = XOpenDisplay(0x0))) {
  316. fprintf(stderr, "Cannot open display!\n");
  317. exit(1);
  318. }
  319. /* return status every second */
  320. for (;;) {
  321. /* assign the values */
  322. battery = get_battery();
  323. cpu_temperature = get_cpu_temperature();
  324. cpu_usage = get_cpu_usage();
  325. datetime = get_datetime();
  326. diskusage = get_diskusage();
  327. ram_usage = get_ram_usage();
  328. volume = get_volume();
  329. wifi_signal = get_wifi_signal();
  330. /* return the status */
  331. sprintf(status, FORMATSTRING, ARGUMENTS);
  332. setstatus(status);
  333. /* free the values */
  334. free(battery);
  335. free(cpu_temperature);
  336. free(cpu_usage);
  337. free(datetime);
  338. free(diskusage);
  339. free(ram_usage);
  340. free(volume);
  341. free(wifi_signal);
  342. /* wait, "update_interval - 1" because of get_cpu_usage() which uses 1 second */
  343. sleep(update_interval -1);
  344. }
  345. /* close display */
  346. XCloseDisplay(dpy);
  347. /* exit successfully */
  348. return 0;
  349. }