slstatus.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /* See LICENSE file for copyright and license details. */
  2. /* global libraries */
  3. #include <alsa/asoundlib.h>
  4. #include <arpa/inet.h>
  5. #include <fcntl.h>
  6. #include <ifaddrs.h>
  7. #include <limits.h>
  8. #include <locale.h>
  9. #include <netdb.h>
  10. #include <pwd.h>
  11. #include <stdarg.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <sys/statvfs.h>
  18. #include <sys/socket.h>
  19. #include <time.h>
  20. #include <unistd.h>
  21. #include <X11/Xlib.h>
  22. /* local headers */
  23. #include "slstatus.h"
  24. #include "config.h"
  25. /* set statusbar */
  26. void
  27. setstatus(const char *str)
  28. {
  29. /* set WM_NAME via X11 */
  30. XStoreName(dpy, DefaultRootWindow(dpy), str);
  31. XSync(dpy, False);
  32. }
  33. /* smprintf function */
  34. char *
  35. smprintf(const char *fmt, ...)
  36. {
  37. va_list fmtargs;
  38. char *ret = NULL;
  39. va_start(fmtargs, fmt);
  40. if (vasprintf(&ret, fmt, fmtargs) < 0)
  41. return NULL;
  42. va_end(fmtargs);
  43. return ret;
  44. }
  45. /* battery percentage */
  46. char *
  47. battery_perc(const char *battery)
  48. {
  49. int now, full, perc;
  50. char batterynowfile[64] = "";
  51. char batteryfullfile[64] = "";
  52. FILE *fp;
  53. /* generate battery nowfile path */
  54. strcat(batterynowfile, batterypath);
  55. strcat(batterynowfile, battery);
  56. strcat(batterynowfile, "/");
  57. strcat(batterynowfile, batterynow);
  58. /* generate battery fullfile path */
  59. strcat(batteryfullfile, batterypath);
  60. strcat(batteryfullfile, battery);
  61. strcat(batteryfullfile, "/");
  62. strcat(batteryfullfile, batteryfull);
  63. /* open battery now file */
  64. if (!(fp = fopen(batterynowfile, "r"))) {
  65. fprintf(stderr, "Error opening battery file.%s",batterynowfile);
  66. return smprintf("n/a");
  67. }
  68. /* read value */
  69. fscanf(fp, "%i", &now);
  70. /* close battery now file */
  71. fclose(fp);
  72. /* open battery full file */
  73. if (!(fp = fopen(batteryfullfile, "r"))) {
  74. fprintf(stderr, "Error opening battery file.");
  75. return smprintf("n/a");
  76. }
  77. /* read value */
  78. fscanf(fp, "%i", &full);
  79. /* close battery full file */
  80. fclose(fp);
  81. /* calculate percent */
  82. perc = now / (full / 100);
  83. /* return perc as string */
  84. return smprintf("%d%%", perc);
  85. }
  86. /* cpu percentage */
  87. char *
  88. cpu_perc(const char *null)
  89. {
  90. int perc;
  91. long double a[4], b[4];
  92. FILE *fp;
  93. /* open stat file */
  94. if (!(fp = fopen("/proc/stat","r"))) {
  95. fprintf(stderr, "Error opening stat file.");
  96. return smprintf("n/a");
  97. }
  98. /* read values */
  99. fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
  100. /* close stat file */
  101. fclose(fp);
  102. /* wait a second (for avg values) */
  103. sleep(1);
  104. /* open stat file */
  105. if (!(fp = fopen("/proc/stat","r"))) {
  106. fprintf(stderr, "Error opening stat file.");
  107. return smprintf("n/a");
  108. }
  109. /* read values */
  110. fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]);
  111. /* close stat file */
  112. fclose(fp);
  113. /* calculate avg in this second */
  114. 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]));
  115. /* return perc as string */
  116. return smprintf("%d%%", perc);
  117. }
  118. /* date and time */
  119. char *
  120. datetime(const char *timeformat)
  121. {
  122. time_t tm;
  123. size_t bufsize = 64;
  124. char *buf = malloc(bufsize);
  125. /* get time in format */
  126. time(&tm);
  127. setlocale(LC_TIME, "");
  128. if(!strftime(buf, bufsize, timeformat, localtime(&tm))) {
  129. setlocale(LC_TIME, "C");
  130. fprintf(stderr, "Strftime failed.\n");
  131. return smprintf("n/a");
  132. }
  133. setlocale(LC_TIME, "C");
  134. /* return time */
  135. char *ret = smprintf("%s", buf);
  136. free(buf);
  137. return ret;
  138. }
  139. /* disk free */
  140. char *
  141. disk_free(const char *mountpoint)
  142. {
  143. struct statvfs fs;
  144. /* try to open mountpoint */
  145. if (statvfs(mountpoint, &fs) < 0) {
  146. fprintf(stderr, "Could not get filesystem info.\n");
  147. return smprintf("n/a");
  148. }
  149. /* return free */
  150. return smprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024);
  151. }
  152. /* disk usage percentage */
  153. char *
  154. disk_perc(const char *mountpoint)
  155. {
  156. int perc = 0;
  157. struct statvfs fs;
  158. /* try to open mountpoint */
  159. if (statvfs(mountpoint, &fs) < 0) {
  160. fprintf(stderr, "Could not get filesystem info.\n");
  161. return smprintf("n/a");
  162. }
  163. /* calculate percent */
  164. perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks));
  165. /* return perc */
  166. return smprintf("%d%%", perc);
  167. }
  168. /* disk total */
  169. char *
  170. disk_total(const char *mountpoint)
  171. {
  172. struct statvfs fs;
  173. /* try to open mountpoint */
  174. if (statvfs(mountpoint, &fs) < 0) {
  175. fprintf(stderr, "Could not get filesystem info.\n");
  176. return smprintf("n/a");
  177. }
  178. /* return total */
  179. return smprintf("%f", (float)fs.f_bsize * (float)fs.f_blocks / 1024 / 1024 / 1024);
  180. }
  181. /* disk used */
  182. char *
  183. disk_used(const char *mountpoint)
  184. {
  185. struct statvfs fs;
  186. /* try to open mountpoint */
  187. if (statvfs(mountpoint, &fs) < 0) {
  188. fprintf(stderr, "Could not get filesystem info.\n");
  189. return smprintf("n/a");
  190. }
  191. /* return used */
  192. return smprintf("%f", (float)fs.f_bsize * ((float)fs.f_blocks - (float)fs.f_bfree) / 1024 / 1024 / 1024);
  193. }
  194. /* entropy available */
  195. char *
  196. entropy(const char *null)
  197. {
  198. int entropy = 0;
  199. FILE *fp;
  200. /* open entropy file */
  201. if (!(fp = fopen("/proc/sys/kernel/random/entropy_avail", "r"))) {
  202. fprintf(stderr, "Could not open entropy file.\n");
  203. return smprintf("n/a");
  204. }
  205. /* extract entropy */
  206. fscanf(fp, "%d", &entropy);
  207. /* close entropy file */
  208. fclose(fp);
  209. /* return entropy */
  210. return smprintf("%d", entropy);
  211. }
  212. /* gid */
  213. char *
  214. gid(const char *null)
  215. {
  216. gid_t gid;
  217. if ((gid = getgid()) < 0) {
  218. fprintf(stderr, "Could no get gid.");
  219. return smprintf("n/a");
  220. } else {
  221. return smprintf("%d", gid);
  222. }
  223. return smprintf("n/a");
  224. }
  225. /* hostname */
  226. char *
  227. hostname(const char *null)
  228. {
  229. char hostname[HOST_NAME_MAX];
  230. FILE *fp;
  231. /* open hostname file */
  232. if (!(fp = fopen("/proc/sys/kernel/hostname", "r"))) {
  233. fprintf(stderr, "Could not open hostname file.\n");
  234. return smprintf("n/a");
  235. }
  236. /* extract hostname */
  237. fscanf(fp, "%s\n", hostname);
  238. /* close hostname file */
  239. fclose(fp);
  240. /* return entropy */
  241. return smprintf("%s", hostname);
  242. }
  243. /* ip address */
  244. char *
  245. ip(const char *interface)
  246. {
  247. struct ifaddrs *ifaddr, *ifa;
  248. int s;
  249. char host[NI_MAXHOST];
  250. /* check if getting ip address works */
  251. if (getifaddrs(&ifaddr) == -1)
  252. {
  253. fprintf(stderr, "Error getting IP address.");
  254. return smprintf("n/a");
  255. }
  256. /* get the ip address */
  257. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
  258. {
  259. if (ifa->ifa_addr == NULL)
  260. continue;
  261. s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
  262. if ((strcmp(ifa->ifa_name, interface) == 0) && (ifa->ifa_addr->sa_family == AF_INET))
  263. {
  264. if (s != 0)
  265. {
  266. fprintf(stderr, "Error getting IP address.");
  267. return smprintf("n/a");
  268. }
  269. return smprintf("%s", host);
  270. }
  271. }
  272. /* free the address */
  273. freeifaddrs(ifaddr);
  274. /* return n/a if nothing works */
  275. return smprintf("n/a");
  276. }
  277. /* ram free */
  278. char *
  279. ram_free(const char *null)
  280. {
  281. long free;
  282. FILE *fp;
  283. /* open meminfo file */
  284. if (!(fp = fopen("/proc/meminfo", "r"))) {
  285. fprintf(stderr, "Error opening meminfo file.");
  286. return smprintf("n/a");
  287. }
  288. /* read the values */
  289. fscanf(fp, "MemTotal: %*d kB\n");
  290. fscanf(fp, "MemFree: %ld kB\n", &free);
  291. /* close meminfo file */
  292. fclose(fp);
  293. /* return free ram as string */
  294. return smprintf("%f", (float)free / 1024 / 1024);
  295. }
  296. /* ram percentage */
  297. char *
  298. ram_perc(const char *null)
  299. {
  300. int perc;
  301. long total, free, buffers, cached;
  302. FILE *fp;
  303. /* open meminfo file */
  304. if (!(fp = fopen("/proc/meminfo", "r"))) {
  305. fprintf(stderr, "Error opening meminfo file.");
  306. return smprintf("n/a");
  307. }
  308. /* read the values */
  309. fscanf(fp, "MemTotal: %ld kB\n", &total);
  310. fscanf(fp, "MemFree: %ld kB\n", &free);
  311. fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
  312. fscanf(fp, "Cached: %ld kB\n", &cached);
  313. /* close meminfo file */
  314. fclose(fp);
  315. /* calculate percentage */
  316. perc = 100 * ((total - free) - (buffers + cached)) / total;
  317. /* return perc as string */
  318. return smprintf("%d%%", perc);
  319. }
  320. /* ram total */
  321. char *
  322. ram_total(const char *null)
  323. {
  324. long total;
  325. FILE *fp;
  326. /* open meminfo file */
  327. if (!(fp = fopen("/proc/meminfo", "r"))) {
  328. fprintf(stderr, "Error opening meminfo file.");
  329. return smprintf("n/a");
  330. }
  331. /* read the values */
  332. fscanf(fp, "MemTotal: %ld kB\n", &total);
  333. /* close meminfo file */
  334. fclose(fp);
  335. /* return total ram as string */
  336. return smprintf("%f", (float)total / 1024 / 1024);
  337. }
  338. /* ram used */
  339. char *
  340. ram_used(const char *null)
  341. {
  342. long free, total, buffers, cached, used;
  343. FILE *fp;
  344. /* open meminfo file */
  345. if (!(fp = fopen("/proc/meminfo", "r"))) {
  346. fprintf(stderr, "Error opening meminfo file.");
  347. return smprintf("n/a");
  348. }
  349. /* read the values */
  350. fscanf(fp, "MemTotal: %ld kB\n", &total);
  351. fscanf(fp, "MemFree: %ld kB\n", &free);
  352. fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
  353. fscanf(fp, "Cached: %ld kB\n", &cached);
  354. /* close meminfo file */
  355. fclose(fp);
  356. /* calculate used */
  357. used = total - free - buffers - cached;
  358. /* return used ram as string */
  359. return smprintf("%f", (float)used / 1024 / 1024);
  360. }
  361. /* custom shell command */
  362. char *
  363. run_command(const char* command)
  364. {
  365. FILE *fp;
  366. char buffer[64];
  367. /* try to open the command output */
  368. if (!(fp = popen(command, "r"))) {
  369. fprintf(stderr, "Could not get command output for: %s.\n", command);
  370. return smprintf("n/a");
  371. }
  372. /* get command output text, save it to buffer */
  373. fgets(buffer, sizeof(buffer)-1, fp);
  374. /* close it again */
  375. pclose(fp);
  376. /* add nullchar at the end */
  377. buffer[strlen(buffer) - 1] = '\0';
  378. /* return the output */
  379. return smprintf("%s", buffer);
  380. }
  381. /* temperature */
  382. char *
  383. temp(const char *file)
  384. {
  385. int temperature;
  386. FILE *fp;
  387. /* open temperature file */
  388. if (!(fp = fopen(file, "r"))) {
  389. fprintf(stderr, "Could not open temperature file.\n");
  390. return smprintf("n/a");
  391. }
  392. /* extract temperature */
  393. fscanf(fp, "%d", &temperature);
  394. /* close temperature file */
  395. fclose(fp);
  396. /* return temperature in degrees */
  397. return smprintf("%d°C", temperature / 1000);
  398. }
  399. /* username */
  400. char *
  401. username(const char *null)
  402. {
  403. register struct passwd *pw;
  404. register uid_t uid;
  405. /* get the values */
  406. uid = geteuid ();
  407. pw = getpwuid (uid);
  408. /* if it worked, return */
  409. if (pw) {
  410. return smprintf("%s", pw->pw_name);
  411. }
  412. else {
  413. fprintf(stderr, "Could not get username.\n");
  414. return smprintf("n/a");
  415. }
  416. return smprintf("n/a");
  417. }
  418. /* uid */
  419. char *
  420. uid(const char *null)
  421. {
  422. register uid_t uid;
  423. /* get the values */
  424. uid = geteuid ();
  425. /* if it worked, return */
  426. if (uid) {
  427. return smprintf("%d", uid);
  428. }
  429. else {
  430. fprintf(stderr, "Could not get uid.\n");
  431. return smprintf("n/a");
  432. }
  433. return smprintf("n/a");
  434. }
  435. /* alsa volume percentage */
  436. char *
  437. vol_perc(const char *soundcard)
  438. {
  439. int mute = 0;
  440. long vol = 0, max = 0, min = 0;
  441. /* get volume from alsa */
  442. snd_mixer_t *handle;
  443. snd_mixer_elem_t *pcm_mixer, *mas_mixer;
  444. snd_mixer_selem_id_t *vol_info, *mute_info;
  445. snd_mixer_open(&handle, 0);
  446. snd_mixer_attach(handle, soundcard);
  447. snd_mixer_selem_register(handle, NULL, NULL);
  448. snd_mixer_load(handle);
  449. snd_mixer_selem_id_malloc(&vol_info);
  450. snd_mixer_selem_id_malloc(&mute_info);
  451. snd_mixer_selem_id_set_name(vol_info, channel);
  452. snd_mixer_selem_id_set_name(mute_info, channel);
  453. pcm_mixer = snd_mixer_find_selem(handle, vol_info);
  454. mas_mixer = snd_mixer_find_selem(handle, mute_info);
  455. snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t *)pcm_mixer, &min, &max);
  456. snd_mixer_selem_get_playback_volume((snd_mixer_elem_t *)pcm_mixer, SND_MIXER_SCHN_MONO, &vol);
  457. snd_mixer_selem_get_playback_switch(mas_mixer, SND_MIXER_SCHN_MONO, &mute);
  458. if (vol_info)
  459. snd_mixer_selem_id_free(vol_info);
  460. if (mute_info)
  461. snd_mixer_selem_id_free(mute_info);
  462. if (handle)
  463. snd_mixer_close(handle);
  464. /* return the string (mute) */
  465. if (!mute)
  466. return smprintf("mute");
  467. else
  468. return smprintf("%d%%", (vol * 100) / max);
  469. }
  470. /* wifi percentage */
  471. char *
  472. wifi_perc(const char *wificard)
  473. {
  474. int bufsize = 255;
  475. int strength;
  476. char buf[bufsize];
  477. char *datastart;
  478. char path[64];
  479. char status[5];
  480. char needle[sizeof wificard + 1];
  481. FILE *fp;
  482. /* generate the path name */
  483. memset(path, 0, sizeof path);
  484. strcat(path, "/sys/class/net/");
  485. strcat(path, wificard);
  486. strcat(path, "/operstate");
  487. /* open wifi file */
  488. if(!(fp = fopen(path, "r"))) {
  489. fprintf(stderr, "Error opening wifi operstate file.");
  490. return smprintf("n/a");
  491. }
  492. /* read the status */
  493. fgets(status, 5, fp);
  494. /* close wifi file */
  495. fclose(fp);
  496. /* check if interface down */
  497. if(strcmp(status, "up\n") != 0){
  498. return smprintf("n/a");
  499. }
  500. /* open wifi file */
  501. if (!(fp = fopen("/proc/net/wireless", "r"))) {
  502. fprintf(stderr, "Error opening wireless file.");
  503. return smprintf("n/a");
  504. }
  505. /* extract the signal strength */
  506. strcpy(needle, wificard);
  507. strcat(needle, ":");
  508. fgets(buf, bufsize, fp);
  509. fgets(buf, bufsize, fp);
  510. fgets(buf, bufsize, fp);
  511. if ((datastart = strstr(buf, needle)) != NULL) {
  512. datastart = strstr(buf, ":");
  513. sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength);
  514. }
  515. /* close wifi file */
  516. fclose(fp);
  517. /* return strength in percent */
  518. return smprintf("%d%%", strength);
  519. }
  520. /* main function */
  521. int
  522. main()
  523. {
  524. char status_string[1024];
  525. struct arg argument;
  526. /* try to open display */
  527. if (!(dpy = XOpenDisplay(0x0))) {
  528. fprintf(stderr, "Cannot open display!\n");
  529. exit(1);
  530. }
  531. /* return status every interval */
  532. for (;;) {
  533. /* clear the string */
  534. memset(status_string, 0, sizeof(status_string));
  535. /* generate status_string */
  536. for (size_t i = 0; i < sizeof(args) / sizeof(args[0]); ++i) {
  537. argument = args[i];
  538. char *res = argument.func(argument.args);
  539. char *element = smprintf(argument.format, res);
  540. strcat(status_string, element);
  541. free(res);
  542. free(element);
  543. }
  544. /* return the statusbar */
  545. setstatus(status_string);
  546. /* wait, "update_interval - 1" because of get_cpu_usage() which uses 1 second */
  547. sleep(update_interval -1);
  548. }
  549. /* close display */
  550. XCloseDisplay(dpy);
  551. /* exit successfully */
  552. return 0;
  553. }