slstatus.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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. if (buf == NULL) {
  126. fprintf(stderr, "Failed to get date/time");
  127. return smprintf("n/a");
  128. }
  129. /* get time in format */
  130. time(&tm);
  131. setlocale(LC_TIME, "");
  132. if(!strftime(buf, bufsize, timeformat, localtime(&tm))) {
  133. setlocale(LC_TIME, "C");
  134. free(buf);
  135. fprintf(stderr, "Strftime failed.\n");
  136. return smprintf("n/a");
  137. }
  138. setlocale(LC_TIME, "C");
  139. /* return time */
  140. char *ret = smprintf("%s", buf);
  141. free(buf);
  142. return ret;
  143. }
  144. /* disk free */
  145. char *
  146. disk_free(const char *mountpoint)
  147. {
  148. struct statvfs fs;
  149. /* try to open mountpoint */
  150. if (statvfs(mountpoint, &fs) < 0) {
  151. fprintf(stderr, "Could not get filesystem info.\n");
  152. return smprintf("n/a");
  153. }
  154. /* return free */
  155. return smprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024);
  156. }
  157. /* disk usage percentage */
  158. char *
  159. disk_perc(const char *mountpoint)
  160. {
  161. int perc = 0;
  162. struct statvfs fs;
  163. /* try to open mountpoint */
  164. if (statvfs(mountpoint, &fs) < 0) {
  165. fprintf(stderr, "Could not get filesystem info.\n");
  166. return smprintf("n/a");
  167. }
  168. /* calculate percent */
  169. perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks));
  170. /* return perc */
  171. return smprintf("%d%%", perc);
  172. }
  173. /* disk total */
  174. char *
  175. disk_total(const char *mountpoint)
  176. {
  177. struct statvfs fs;
  178. /* try to open mountpoint */
  179. if (statvfs(mountpoint, &fs) < 0) {
  180. fprintf(stderr, "Could not get filesystem info.\n");
  181. return smprintf("n/a");
  182. }
  183. /* return total */
  184. return smprintf("%f", (float)fs.f_bsize * (float)fs.f_blocks / 1024 / 1024 / 1024);
  185. }
  186. /* disk used */
  187. char *
  188. disk_used(const char *mountpoint)
  189. {
  190. struct statvfs fs;
  191. /* try to open mountpoint */
  192. if (statvfs(mountpoint, &fs) < 0) {
  193. fprintf(stderr, "Could not get filesystem info.\n");
  194. return smprintf("n/a");
  195. }
  196. /* return used */
  197. return smprintf("%f", (float)fs.f_bsize * ((float)fs.f_blocks - (float)fs.f_bfree) / 1024 / 1024 / 1024);
  198. }
  199. /* entropy available */
  200. char *
  201. entropy(const char *null)
  202. {
  203. int entropy = 0;
  204. FILE *fp;
  205. /* open entropy file */
  206. if (!(fp = fopen("/proc/sys/kernel/random/entropy_avail", "r"))) {
  207. fprintf(stderr, "Could not open entropy file.\n");
  208. return smprintf("n/a");
  209. }
  210. /* extract entropy */
  211. fscanf(fp, "%d", &entropy);
  212. /* close entropy file */
  213. fclose(fp);
  214. /* return entropy */
  215. return smprintf("%d", entropy);
  216. }
  217. /* gid */
  218. char *
  219. gid(const char *null)
  220. {
  221. gid_t gid;
  222. if ((gid = getgid()) < 0) {
  223. fprintf(stderr, "Could no get gid.");
  224. return smprintf("n/a");
  225. } else {
  226. return smprintf("%d", gid);
  227. }
  228. return smprintf("n/a");
  229. }
  230. /* hostname */
  231. char *
  232. hostname(const char *null)
  233. {
  234. char hostname[HOST_NAME_MAX];
  235. FILE *fp;
  236. /* open hostname file */
  237. if (!(fp = fopen("/proc/sys/kernel/hostname", "r"))) {
  238. fprintf(stderr, "Could not open hostname file.\n");
  239. return smprintf("n/a");
  240. }
  241. /* extract hostname */
  242. fscanf(fp, "%s\n", hostname);
  243. /* close hostname file */
  244. fclose(fp);
  245. /* return entropy */
  246. return smprintf("%s", hostname);
  247. }
  248. /* ip address */
  249. char *
  250. ip(const char *interface)
  251. {
  252. struct ifaddrs *ifaddr, *ifa;
  253. int s;
  254. char host[NI_MAXHOST];
  255. /* check if getting ip address works */
  256. if (getifaddrs(&ifaddr) == -1)
  257. {
  258. fprintf(stderr, "Error getting IP address.");
  259. return smprintf("n/a");
  260. }
  261. /* get the ip address */
  262. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
  263. {
  264. if (ifa->ifa_addr == NULL)
  265. continue;
  266. s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
  267. if ((strcmp(ifa->ifa_name, interface) == 0) && (ifa->ifa_addr->sa_family == AF_INET))
  268. {
  269. if (s != 0)
  270. {
  271. fprintf(stderr, "Error getting IP address.");
  272. return smprintf("n/a");
  273. }
  274. return smprintf("%s", host);
  275. }
  276. }
  277. /* free the address */
  278. freeifaddrs(ifaddr);
  279. /* return n/a if nothing works */
  280. return smprintf("n/a");
  281. }
  282. /* ram free */
  283. char *
  284. ram_free(const char *null)
  285. {
  286. long free;
  287. FILE *fp;
  288. /* open meminfo file */
  289. if (!(fp = fopen("/proc/meminfo", "r"))) {
  290. fprintf(stderr, "Error opening meminfo file.");
  291. return smprintf("n/a");
  292. }
  293. /* read the values */
  294. fscanf(fp, "MemTotal: %*d kB\n");
  295. fscanf(fp, "MemFree: %ld kB\n", &free);
  296. /* close meminfo file */
  297. fclose(fp);
  298. /* return free ram as string */
  299. return smprintf("%f", (float)free / 1024 / 1024);
  300. }
  301. /* ram percentage */
  302. char *
  303. ram_perc(const char *null)
  304. {
  305. int perc;
  306. long total, free, buffers, cached;
  307. FILE *fp;
  308. /* open meminfo file */
  309. if (!(fp = fopen("/proc/meminfo", "r"))) {
  310. fprintf(stderr, "Error opening meminfo file.");
  311. return smprintf("n/a");
  312. }
  313. /* read the values */
  314. fscanf(fp, "MemTotal: %ld kB\n", &total);
  315. fscanf(fp, "MemFree: %ld kB\n", &free);
  316. fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
  317. fscanf(fp, "Cached: %ld kB\n", &cached);
  318. /* close meminfo file */
  319. fclose(fp);
  320. /* calculate percentage */
  321. perc = 100 * ((total - free) - (buffers + cached)) / total;
  322. /* return perc as string */
  323. return smprintf("%d%%", perc);
  324. }
  325. /* ram total */
  326. char *
  327. ram_total(const char *null)
  328. {
  329. long total;
  330. FILE *fp;
  331. /* open meminfo file */
  332. if (!(fp = fopen("/proc/meminfo", "r"))) {
  333. fprintf(stderr, "Error opening meminfo file.");
  334. return smprintf("n/a");
  335. }
  336. /* read the values */
  337. fscanf(fp, "MemTotal: %ld kB\n", &total);
  338. /* close meminfo file */
  339. fclose(fp);
  340. /* return total ram as string */
  341. return smprintf("%f", (float)total / 1024 / 1024);
  342. }
  343. /* ram used */
  344. char *
  345. ram_used(const char *null)
  346. {
  347. long free, total, buffers, cached, used;
  348. FILE *fp;
  349. /* open meminfo file */
  350. if (!(fp = fopen("/proc/meminfo", "r"))) {
  351. fprintf(stderr, "Error opening meminfo file.");
  352. return smprintf("n/a");
  353. }
  354. /* read the values */
  355. fscanf(fp, "MemTotal: %ld kB\n", &total);
  356. fscanf(fp, "MemFree: %ld kB\n", &free);
  357. fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
  358. fscanf(fp, "Cached: %ld kB\n", &cached);
  359. /* close meminfo file */
  360. fclose(fp);
  361. /* calculate used */
  362. used = total - free - buffers - cached;
  363. /* return used ram as string */
  364. return smprintf("%f", (float)used / 1024 / 1024);
  365. }
  366. /* custom shell command */
  367. char *
  368. run_command(const char* command)
  369. {
  370. FILE *fp;
  371. char buffer[64];
  372. /* try to open the command output */
  373. if (!(fp = popen(command, "r"))) {
  374. fprintf(stderr, "Could not get command output for: %s.\n", command);
  375. return smprintf("n/a");
  376. }
  377. /* get command output text, save it to buffer */
  378. fgets(buffer, sizeof(buffer)-1, fp);
  379. /* close it again */
  380. pclose(fp);
  381. /* add nullchar at the end */
  382. buffer[strlen(buffer) - 1] = '\0';
  383. /* return the output */
  384. return smprintf("%s", buffer);
  385. }
  386. /* temperature */
  387. char *
  388. temp(const char *file)
  389. {
  390. int temperature;
  391. FILE *fp;
  392. /* open temperature file */
  393. if (!(fp = fopen(file, "r"))) {
  394. fprintf(stderr, "Could not open temperature file.\n");
  395. return smprintf("n/a");
  396. }
  397. /* extract temperature */
  398. fscanf(fp, "%d", &temperature);
  399. /* close temperature file */
  400. fclose(fp);
  401. /* return temperature in degrees */
  402. return smprintf("%d°C", temperature / 1000);
  403. }
  404. /* username */
  405. char *
  406. username(const char *null)
  407. {
  408. register struct passwd *pw;
  409. register uid_t uid;
  410. /* get the values */
  411. uid = geteuid ();
  412. pw = getpwuid (uid);
  413. /* if it worked, return */
  414. if (pw) {
  415. return smprintf("%s", pw->pw_name);
  416. }
  417. else {
  418. fprintf(stderr, "Could not get username.\n");
  419. return smprintf("n/a");
  420. }
  421. return smprintf("n/a");
  422. }
  423. /* uid */
  424. char *
  425. uid(const char *null)
  426. {
  427. register uid_t uid;
  428. /* get the values */
  429. uid = geteuid ();
  430. /* if it worked, return */
  431. if (uid) {
  432. return smprintf("%d", uid);
  433. }
  434. else {
  435. fprintf(stderr, "Could not get uid.\n");
  436. return smprintf("n/a");
  437. }
  438. return smprintf("n/a");
  439. }
  440. /* alsa volume percentage */
  441. char *
  442. vol_perc(const char *soundcard)
  443. {
  444. int mute = 0;
  445. long vol = 0, max = 0, min = 0;
  446. snd_mixer_t *handle;
  447. snd_mixer_elem_t *pcm_mixer, *mas_mixer;
  448. snd_mixer_selem_id_t *vol_info, *mute_info;
  449. /* open everything */
  450. snd_mixer_open(&handle, 0);
  451. snd_mixer_attach(handle, soundcard);
  452. snd_mixer_selem_register(handle, NULL, NULL);
  453. snd_mixer_load(handle);
  454. /* prepare everything */
  455. snd_mixer_selem_id_malloc(&vol_info);
  456. snd_mixer_selem_id_malloc(&mute_info);
  457. /* check */
  458. if (vol_info == NULL || mute_info == NULL) {
  459. fprintf(stderr, "Could not get alsa volume");
  460. return smprintf("n/a");
  461. }
  462. snd_mixer_selem_id_set_name(vol_info, channel);
  463. snd_mixer_selem_id_set_name(mute_info, channel);
  464. pcm_mixer = snd_mixer_find_selem(handle, vol_info);
  465. mas_mixer = snd_mixer_find_selem(handle, mute_info);
  466. /* get the info */
  467. snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t *)pcm_mixer, &min, &max);
  468. snd_mixer_selem_get_playback_volume((snd_mixer_elem_t *)pcm_mixer, SND_MIXER_SCHN_MONO, &vol);
  469. snd_mixer_selem_get_playback_switch(mas_mixer, SND_MIXER_SCHN_MONO, &mute);
  470. /* clean up */
  471. if (vol_info) {
  472. snd_mixer_selem_id_free(vol_info);
  473. }
  474. if (mute_info) {
  475. snd_mixer_selem_id_free(mute_info);
  476. }
  477. if (handle) {
  478. snd_mixer_close(handle);
  479. }
  480. /* return the string (mute) */
  481. if (!mute) {
  482. return smprintf("mute");
  483. }
  484. else {
  485. return smprintf("%d%%", (vol * 100) / max);
  486. }
  487. }
  488. /* wifi percentage */
  489. char *
  490. wifi_perc(const char *wificard)
  491. {
  492. int bufsize = 255;
  493. int strength;
  494. char buf[bufsize];
  495. char *datastart;
  496. char path[64];
  497. char status[5];
  498. char needle[sizeof wificard + 1];
  499. FILE *fp;
  500. /* generate the path name */
  501. memset(path, 0, sizeof path);
  502. strcat(path, "/sys/class/net/");
  503. strcat(path, wificard);
  504. strcat(path, "/operstate");
  505. /* open wifi file */
  506. if(!(fp = fopen(path, "r"))) {
  507. fprintf(stderr, "Error opening wifi operstate file.");
  508. return smprintf("n/a");
  509. }
  510. /* read the status */
  511. fgets(status, 5, fp);
  512. /* close wifi file */
  513. fclose(fp);
  514. /* check if interface down */
  515. if(strcmp(status, "up\n") != 0){
  516. return smprintf("n/a");
  517. }
  518. /* open wifi file */
  519. if (!(fp = fopen("/proc/net/wireless", "r"))) {
  520. fprintf(stderr, "Error opening wireless file.");
  521. return smprintf("n/a");
  522. }
  523. /* extract the signal strength */
  524. strcpy(needle, wificard);
  525. strcat(needle, ":");
  526. fgets(buf, bufsize, fp);
  527. fgets(buf, bufsize, fp);
  528. fgets(buf, bufsize, fp);
  529. if ((datastart = strstr(buf, needle)) != NULL) {
  530. datastart = strstr(buf, ":");
  531. sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength);
  532. }
  533. /* close wifi file */
  534. fclose(fp);
  535. /* return strength in percent */
  536. return smprintf("%d%%", strength);
  537. }
  538. /* main function */
  539. int
  540. main(void)
  541. {
  542. char status_string[1024];
  543. struct arg argument;
  544. /* try to open display */
  545. if (!(dpy = XOpenDisplay(0x0))) {
  546. fprintf(stderr, "Cannot open display!\n");
  547. exit(1);
  548. }
  549. /* return status every interval */
  550. for (;;) {
  551. /* clear the string */
  552. memset(status_string, 0, sizeof(status_string));
  553. /* generate status_string */
  554. for (size_t i = 0; i < sizeof(args) / sizeof(args[0]); ++i) {
  555. argument = args[i];
  556. char *res = argument.func(argument.args);
  557. char *element = smprintf(argument.format, res);
  558. strcat(status_string, element);
  559. free(res);
  560. free(element);
  561. }
  562. /* return the statusbar */
  563. setstatus(status_string);
  564. /* wait, "update_interval - 1" because of get_cpu_usage() which uses 1 second */
  565. sleep(update_interval -1);
  566. }
  567. /* close display */
  568. XCloseDisplay(dpy);
  569. /* exit successfully */
  570. return 0;
  571. }