My slstatus configuration
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

slstatus.c 9.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. /* global variables */
  17. static Display *dpy;
  18. /* statusbar configuration type and struct */
  19. typedef char *(*op_fun) (const char *);
  20. struct arg {
  21. op_fun func;
  22. const char *format;
  23. const char *args;
  24. };
  25. /* functions */
  26. void setstatus(const char *);
  27. char *smprintf(const char *, ...);
  28. char *get_battery(const char *);
  29. char *get_cpu_temperature(const char *);
  30. char *get_cpu_usage(const char *);
  31. char *get_datetime(const char *);
  32. char *get_diskusage(const char *);
  33. char *get_ram_usage(const char *);
  34. char *get_volume(const char *);
  35. char *get_wifi_signal(const char *);
  36. /* include config header */
  37. #include "config.h"
  38. /* set statusbar */
  39. void
  40. setstatus(const char *str)
  41. {
  42. /* set WM_NAME via X11 */
  43. XStoreName(dpy, DefaultRootWindow(dpy), str);
  44. XSync(dpy, False);
  45. }
  46. /* smprintf function */
  47. char *
  48. smprintf(const char *fmt, ...)
  49. {
  50. va_list fmtargs;
  51. char *ret = NULL;
  52. va_start(fmtargs, fmt);
  53. if (vasprintf(&ret, fmt, fmtargs) < 0)
  54. return NULL;
  55. va_end(fmtargs);
  56. return ret;
  57. }
  58. /* battery percentage */
  59. char *
  60. get_battery(const char *battery)
  61. {
  62. int now, full, perc;
  63. char batterynowfile[64] = "";
  64. char batteryfullfile[64] = "";
  65. FILE *fp;
  66. /* generate battery nowfile path */
  67. strcat(batterynowfile, batterypath);
  68. strcat(batterynowfile, battery);
  69. strcat(batterynowfile, "/");
  70. strcat(batterynowfile, batterynow);
  71. /* generate battery fullfile path */
  72. strcat(batteryfullfile, batterypath);
  73. strcat(batteryfullfile, battery);
  74. strcat(batteryfullfile, "/");
  75. strcat(batteryfullfile, batteryfull);
  76. /* open battery now file */
  77. if (!(fp = fopen(batterynowfile, "r"))) {
  78. fprintf(stderr, "Error opening battery file.%s",batterynowfile);
  79. return smprintf("n/a");
  80. }
  81. /* read value */
  82. fscanf(fp, "%i", &now);
  83. /* close battery now file */
  84. fclose(fp);
  85. /* open battery full file */
  86. if (!(fp = fopen(batteryfullfile, "r"))) {
  87. fprintf(stderr, "Error opening battery file.");
  88. return smprintf("n/a");
  89. }
  90. /* read value */
  91. fscanf(fp, "%i", &full);
  92. /* close battery full file */
  93. fclose(fp);
  94. /* calculate percent */
  95. perc = now / (full / 100);
  96. /* return perc as string */
  97. return smprintf("%d%%", perc);
  98. }
  99. /* cpu temperature */
  100. char *
  101. get_cpu_temperature(const char *file)
  102. {
  103. int temperature;
  104. FILE *fp;
  105. /* open temperature file */
  106. if (!(fp = fopen(file, "r"))) {
  107. fprintf(stderr, "Could not open temperature file.\n");
  108. return smprintf("n/a");
  109. }
  110. /* extract temperature */
  111. fscanf(fp, "%d", &temperature);
  112. /* close temperature file */
  113. fclose(fp);
  114. /* return temperature in degrees */
  115. return smprintf("%d°C", temperature / 1000);
  116. }
  117. /* cpu percentage */
  118. char *
  119. get_cpu_usage(const char *null)
  120. {
  121. int perc;
  122. long double a[4], b[4];
  123. FILE *fp;
  124. /* open stat file */
  125. if (!(fp = fopen("/proc/stat","r"))) {
  126. fprintf(stderr, "Error opening stat file.");
  127. return smprintf("n/a");
  128. }
  129. /* read values */
  130. fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
  131. /* close stat file */
  132. fclose(fp);
  133. /* wait a second (for avg values) */
  134. sleep(1);
  135. /* open stat file */
  136. if (!(fp = fopen("/proc/stat","r"))) {
  137. fprintf(stderr, "Error opening stat file.");
  138. return smprintf("n/a");
  139. }
  140. /* read values */
  141. fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]);
  142. /* close stat file */
  143. fclose(fp);
  144. /* calculate avg in this second */
  145. 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]));
  146. /* return perc as string */
  147. return smprintf("%d%%", perc);
  148. }
  149. /* date and time */
  150. char *
  151. get_datetime(const char *timeformat)
  152. {
  153. time_t tm;
  154. size_t bufsize = 64;
  155. char *buf = malloc(bufsize);
  156. /* get time in format */
  157. time(&tm);
  158. setlocale(LC_TIME, "");
  159. if(!strftime(buf, bufsize, timeformat, localtime(&tm))) {
  160. setlocale(LC_TIME, "C");
  161. fprintf(stderr, "Strftime failed.\n");
  162. return smprintf("n/a");
  163. }
  164. setlocale(LC_TIME, "C");
  165. /* return time */
  166. return smprintf("%s", buf);
  167. }
  168. /* disk usage percentage */
  169. char *
  170. get_diskusage(const char *mountpoint)
  171. {
  172. int perc = 0;
  173. struct statvfs fs;
  174. /* try to open mountpoint */
  175. if (statvfs(mountpoint, &fs) < 0) {
  176. fprintf(stderr, "Could not get filesystem info.\n");
  177. return smprintf("n/a");
  178. }
  179. /* calculate percent */
  180. perc = 100 * (1.0f - ((float)fs.f_bavail / (float)fs.f_blocks));
  181. /* return perc */
  182. return smprintf("%d%%", perc);
  183. }
  184. /* ram percentage */
  185. char *
  186. get_ram_usage(const char *null)
  187. {
  188. int perc;
  189. long total, free, buffers, cached;
  190. FILE *fp;
  191. /* open meminfo file */
  192. if (!(fp = fopen("/proc/meminfo", "r"))) {
  193. fprintf(stderr, "Error opening meminfo file.");
  194. return smprintf("n/a");
  195. }
  196. /* read the values */
  197. fscanf(fp, "MemTotal: %ld kB\n", &total);
  198. fscanf(fp, "MemFree: %ld kB\n", &free);
  199. fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
  200. fscanf(fp, "Cached: %ld kB\n", &cached);
  201. /* close meminfo file */
  202. fclose(fp);
  203. /* calculate percentage */
  204. perc = 100 * ((total - free) - (buffers + cached)) / total;
  205. /* return perc as string */
  206. return smprintf("%d%%", perc);
  207. }
  208. /* alsa volume percentage */
  209. char *
  210. get_volume(const char *soundcard)
  211. {
  212. int mute = 0;
  213. long vol = 0, max = 0, min = 0;
  214. /* get volume from alsa */
  215. snd_mixer_t *handle;
  216. snd_mixer_elem_t *pcm_mixer, *mas_mixer;
  217. snd_mixer_selem_id_t *vol_info, *mute_info;
  218. snd_mixer_open(&handle, 0);
  219. snd_mixer_attach(handle, soundcard);
  220. snd_mixer_selem_register(handle, NULL, NULL);
  221. snd_mixer_load(handle);
  222. snd_mixer_selem_id_malloc(&vol_info);
  223. snd_mixer_selem_id_malloc(&mute_info);
  224. snd_mixer_selem_id_set_name(vol_info, channel);
  225. snd_mixer_selem_id_set_name(mute_info, channel);
  226. pcm_mixer = snd_mixer_find_selem(handle, vol_info);
  227. mas_mixer = snd_mixer_find_selem(handle, mute_info);
  228. snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t *)pcm_mixer, &min, &max);
  229. snd_mixer_selem_get_playback_volume((snd_mixer_elem_t *)pcm_mixer, SND_MIXER_SCHN_MONO, &vol);
  230. snd_mixer_selem_get_playback_switch(mas_mixer, SND_MIXER_SCHN_MONO, &mute);
  231. if (vol_info)
  232. snd_mixer_selem_id_free(vol_info);
  233. if (mute_info)
  234. snd_mixer_selem_id_free(mute_info);
  235. if (handle)
  236. snd_mixer_close(handle);
  237. /* return the string (mute) */
  238. if (!mute)
  239. return smprintf("mute");
  240. else
  241. return smprintf("%d%%", (vol * 100) / max);
  242. }
  243. /* wifi percentage */
  244. char *
  245. get_wifi_signal(const char *wificard)
  246. {
  247. int bufsize = 255;
  248. int strength;
  249. char buf[bufsize];
  250. char *datastart;
  251. char path[64];
  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, "/sys/class/net/");
  258. strcat(path, wificard);
  259. strcat(path, "/operstate");
  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_string[1024];
  298. struct arg argument;
  299. /* try to open display */
  300. if (!(dpy = XOpenDisplay(0x0))) {
  301. fprintf(stderr, "Cannot open display!\n");
  302. exit(1);
  303. }
  304. /* return status every interval */
  305. for (;;) {
  306. /* clear the string */
  307. strcpy(status_string, "");
  308. /* generate status_string */
  309. for (size_t i = 0; i < sizeof(args) / sizeof(args[0]); ++i) {
  310. argument = args[i];
  311. char *res = argument.func(argument.args);
  312. char *element = smprintf(argument.format, res);
  313. strcat(status_string, element);
  314. }
  315. /* return the statusbar */
  316. setstatus(status_string);
  317. /* wait, "update_interval - 1" because of get_cpu_usage() which uses 1 second */
  318. sleep(update_interval -1);
  319. }
  320. /* close display */
  321. XCloseDisplay(dpy);
  322. /* exit successfully */
  323. return 0;
  324. }