My slstatus configuration
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

415 рядки
9.5 KiB

  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. struct statvfs fs;
  174. float perc = 0;
  175. if (statvfs(mountpath, &fs) < 0) {
  176. fprintf(stderr, "Could not get filesystem info.\n");
  177. return smprintf("n/a");
  178. }
  179. perc = 1.0f - ((float)fs.f_bavail/(float)fs.f_blocks);
  180. return smprintf("%2f%%", perc);
  181. }
  182. /* ram percentage */
  183. char *
  184. get_ram_usage()
  185. {
  186. int perc;
  187. long total, free, buffers, cached;
  188. FILE *fp;
  189. /* open meminfo file */
  190. if (!(fp = fopen("/proc/meminfo", "r"))) {
  191. fprintf(stderr, "Error opening meminfo file.");
  192. return smprintf("n/a");
  193. }
  194. /* read the values */
  195. fscanf(fp, "MemTotal: %ld kB\n", &total);
  196. fscanf(fp, "MemFree: %ld kB\n", &free);
  197. fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
  198. fscanf(fp, "Cached: %ld kB\n", &cached);
  199. /* close meminfo file */
  200. fclose(fp);
  201. /* calculate percentage */
  202. perc = 100 * ((total - free) - (buffers + cached)) / total;
  203. /* return perc as string */
  204. return smprintf("%d%%", perc);
  205. }
  206. /* alsa volume percentage */
  207. char *
  208. get_volume()
  209. {
  210. int mute = 0;
  211. long vol = 0, max = 0, min = 0;
  212. /* get volume from alsa */
  213. snd_mixer_t *handle;
  214. snd_mixer_elem_t *pcm_mixer, *mas_mixer;
  215. snd_mixer_selem_id_t *vol_info, *mute_info;
  216. snd_mixer_open(&handle, 0);
  217. snd_mixer_attach(handle, soundcard);
  218. snd_mixer_selem_register(handle, NULL, NULL);
  219. snd_mixer_load(handle);
  220. snd_mixer_selem_id_malloc(&vol_info);
  221. snd_mixer_selem_id_malloc(&mute_info);
  222. snd_mixer_selem_id_set_name(vol_info, channel);
  223. snd_mixer_selem_id_set_name(mute_info, channel);
  224. pcm_mixer = snd_mixer_find_selem(handle, vol_info);
  225. mas_mixer = snd_mixer_find_selem(handle, mute_info);
  226. snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t *)pcm_mixer, &min, &max);
  227. snd_mixer_selem_get_playback_volume((snd_mixer_elem_t *)pcm_mixer, SND_MIXER_SCHN_MONO, &vol);
  228. snd_mixer_selem_get_playback_switch(mas_mixer, SND_MIXER_SCHN_MONO, &mute);
  229. if (vol_info)
  230. snd_mixer_selem_id_free(vol_info);
  231. if (mute_info)
  232. snd_mixer_selem_id_free(mute_info);
  233. if (handle)
  234. snd_mixer_close(handle);
  235. /* return the string (mute) */
  236. if (!mute)
  237. return smprintf("mute");
  238. else
  239. return smprintf("%d%%", (vol * 100) / max);
  240. }
  241. /* wifi percentage */
  242. char *
  243. get_wifi_signal()
  244. {
  245. int bufsize = 255;
  246. int strength;
  247. char buf[bufsize];
  248. char *datastart;
  249. char path_start[16] = "/sys/class/net/";
  250. char path_end[11] = "/operstate";
  251. char path[32];
  252. char status[5];
  253. char needle[sizeof wificard + 1];
  254. FILE *fp;
  255. /* generate the path name */
  256. memset(path, 0, sizeof path);
  257. strcat(path, path_start);
  258. strcat(path, wificard);
  259. strcat(path, path_end);
  260. /* open wifi file */
  261. if(!(fp = fopen(path, "r"))) {
  262. fprintf(stderr, "Error opening wifi operstate file.");
  263. return smprintf("n/a");
  264. }
  265. /* read the status */
  266. fgets(status, 5, fp);
  267. /* close wifi file */
  268. fclose(fp);
  269. /* check if interface down */
  270. if(strcmp(status, "up\n") != 0){
  271. return smprintf("n/a");
  272. }
  273. /* open wifi file */
  274. if (!(fp = fopen("/proc/net/wireless", "r"))) {
  275. fprintf(stderr, "Error opening wireless file.");
  276. return smprintf("n/a");
  277. }
  278. /* extract the signal strength */
  279. strcpy(needle, wificard);
  280. strcat(needle, ":");
  281. fgets(buf, bufsize, fp);
  282. fgets(buf, bufsize, fp);
  283. fgets(buf, bufsize, fp);
  284. if ((datastart = strstr(buf, needle)) != NULL) {
  285. datastart = strstr(buf, ":");
  286. sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength);
  287. }
  288. /* close wifi file */
  289. fclose(fp);
  290. /* return strength in percent */
  291. return smprintf("%d%%", strength);
  292. }
  293. /* main function */
  294. int
  295. main()
  296. {
  297. char status[1024];
  298. char *battery = NULL;
  299. char *cpu_temperature = NULL;
  300. char *cpu_usage = NULL;
  301. char *datetime = NULL;
  302. char *diskusage = NULL;
  303. char *ram_usage = NULL;
  304. char *volume = NULL;
  305. char *wifi_signal = NULL;
  306. /* check config for sanity */
  307. if (config_check() < 0) {
  308. fprintf(stderr, "Config error, please check paths and interval and recompile!\n");
  309. exit(1);
  310. }
  311. /* open display */
  312. if (!(dpy = XOpenDisplay(0x0))) {
  313. fprintf(stderr, "Cannot open display!\n");
  314. exit(1);
  315. }
  316. /* return status every second */
  317. for (;;) {
  318. /* assign the values */
  319. battery = get_battery();
  320. cpu_temperature = get_cpu_temperature();
  321. cpu_usage = get_cpu_usage();
  322. datetime = get_datetime();
  323. diskusage = get_diskusage();
  324. ram_usage = get_ram_usage();
  325. volume = get_volume();
  326. wifi_signal = get_wifi_signal();
  327. /* return the status */
  328. sprintf(status, FORMATSTRING, ARGUMENTS);
  329. setstatus(status);
  330. /* free the values */
  331. free(battery);
  332. free(cpu_temperature);
  333. free(cpu_usage);
  334. free(datetime);
  335. free(diskusage);
  336. free(ram_usage);
  337. free(volume);
  338. free(wifi_signal);
  339. /* wait, "update_interval - 1" because of get_cpu_usage() which uses 1 second */
  340. sleep(update_interval -1);
  341. }
  342. /* close display */
  343. XCloseDisplay(dpy);
  344. /* exit successfully */
  345. return 0;
  346. }