My slstatus configuration
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

slstatus.c 16 KiB

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