My slstatus configuration
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

467 linhas
11 KiB

  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 <locale.h>
  8. #include <netdb.h>
  9. #include <stdarg.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <sys/statvfs.h>
  16. #include <sys/socket.h>
  17. #include <time.h>
  18. #include <unistd.h>
  19. #include <X11/Xlib.h>
  20. /* local headers */
  21. #include "slstatus.h"
  22. #include "config.h"
  23. /* set statusbar */
  24. void
  25. setstatus(const char *str)
  26. {
  27. /* set WM_NAME via X11 */
  28. XStoreName(dpy, DefaultRootWindow(dpy), str);
  29. XSync(dpy, False);
  30. }
  31. /* smprintf function */
  32. char *
  33. smprintf(const char *fmt, ...)
  34. {
  35. va_list fmtargs;
  36. char *ret = NULL;
  37. va_start(fmtargs, fmt);
  38. if (vasprintf(&ret, fmt, fmtargs) < 0)
  39. return NULL;
  40. va_end(fmtargs);
  41. return ret;
  42. }
  43. /* battery percentage */
  44. char *
  45. battery_perc(const char *battery)
  46. {
  47. int now, full, perc;
  48. char batterynowfile[64] = "";
  49. char batteryfullfile[64] = "";
  50. FILE *fp;
  51. /* generate battery nowfile path */
  52. strcat(batterynowfile, batterypath);
  53. strcat(batterynowfile, battery);
  54. strcat(batterynowfile, "/");
  55. strcat(batterynowfile, batterynow);
  56. /* generate battery fullfile path */
  57. strcat(batteryfullfile, batterypath);
  58. strcat(batteryfullfile, battery);
  59. strcat(batteryfullfile, "/");
  60. strcat(batteryfullfile, batteryfull);
  61. /* open battery now file */
  62. if (!(fp = fopen(batterynowfile, "r"))) {
  63. fprintf(stderr, "Error opening battery file.%s",batterynowfile);
  64. return smprintf("n/a");
  65. }
  66. /* read value */
  67. fscanf(fp, "%i", &now);
  68. /* close battery now file */
  69. fclose(fp);
  70. /* open battery full file */
  71. if (!(fp = fopen(batteryfullfile, "r"))) {
  72. fprintf(stderr, "Error opening battery file.");
  73. return smprintf("n/a");
  74. }
  75. /* read value */
  76. fscanf(fp, "%i", &full);
  77. /* close battery full file */
  78. fclose(fp);
  79. /* calculate percent */
  80. perc = now / (full / 100);
  81. /* return perc as string */
  82. return smprintf("%d%%", perc);
  83. }
  84. /* cpu percentage */
  85. char *
  86. cpu_perc(const char *null)
  87. {
  88. int perc;
  89. long double a[4], b[4];
  90. FILE *fp;
  91. /* open stat file */
  92. if (!(fp = fopen("/proc/stat","r"))) {
  93. fprintf(stderr, "Error opening stat file.");
  94. return smprintf("n/a");
  95. }
  96. /* read values */
  97. fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
  98. /* close stat file */
  99. fclose(fp);
  100. /* wait a second (for avg values) */
  101. sleep(1);
  102. /* open stat file */
  103. if (!(fp = fopen("/proc/stat","r"))) {
  104. fprintf(stderr, "Error opening stat file.");
  105. return smprintf("n/a");
  106. }
  107. /* read values */
  108. fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]);
  109. /* close stat file */
  110. fclose(fp);
  111. /* calculate avg in this second */
  112. 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]));
  113. /* return perc as string */
  114. return smprintf("%d%%", perc);
  115. }
  116. /* date and time */
  117. char *
  118. datetime(const char *timeformat)
  119. {
  120. time_t tm;
  121. size_t bufsize = 64;
  122. char *buf = malloc(bufsize);
  123. /* get time in format */
  124. time(&tm);
  125. setlocale(LC_TIME, "");
  126. if(!strftime(buf, bufsize, timeformat, localtime(&tm))) {
  127. setlocale(LC_TIME, "C");
  128. fprintf(stderr, "Strftime failed.\n");
  129. return smprintf("n/a");
  130. }
  131. setlocale(LC_TIME, "C");
  132. /* return time */
  133. char *ret = smprintf("%s", buf);
  134. free(buf);
  135. return ret;
  136. }
  137. /* disk usage percentage */
  138. char *
  139. disk_perc(const char *mountpoint)
  140. {
  141. int perc = 0;
  142. struct statvfs fs;
  143. /* try to open mountpoint */
  144. if (statvfs(mountpoint, &fs) < 0) {
  145. fprintf(stderr, "Could not get filesystem info.\n");
  146. return smprintf("n/a");
  147. }
  148. /* calculate percent */
  149. perc = 100 * (1.0f - ((float)fs.f_bavail / (float)fs.f_blocks));
  150. /* return perc */
  151. return smprintf("%d%%", perc);
  152. }
  153. /* entropy available */
  154. char *
  155. entropy(const char *null)
  156. {
  157. int entropy = 0;
  158. FILE *fp;
  159. /* open entropy file */
  160. if (!(fp = fopen("/proc/sys/kernel/random/entropy_avail", "r"))) {
  161. fprintf(stderr, "Could not open entropy file.\n");
  162. return smprintf("n/a");
  163. }
  164. /* extract entropy */
  165. fscanf(fp, "%d", &entropy);
  166. /* close entropy file */
  167. fclose(fp);
  168. /* return entropy */
  169. return smprintf("%d", entropy);
  170. }
  171. /* hostname */
  172. char *
  173. hostname(const char *null)
  174. {
  175. char *hostname = "";
  176. FILE *fp;
  177. /* open hostname file */
  178. if (!(fp = fopen("/proc/sys/kernel/hostname", "r"))) {
  179. fprintf(stderr, "Could not open hostname file.\n");
  180. return smprintf("n/a");
  181. }
  182. /* extract hostname */
  183. fscanf(fp, "%s", hostname);
  184. /* close hostname file */
  185. fclose(fp);
  186. /* return entropy */
  187. return smprintf("%s", hostname);
  188. }
  189. /* ip address */
  190. char *
  191. ip(const char *interface)
  192. {
  193. struct ifaddrs *ifaddr, *ifa;
  194. int s;
  195. char host[NI_MAXHOST];
  196. /* check if getting ip address works */
  197. if (getifaddrs(&ifaddr) == -1)
  198. {
  199. fprintf(stderr, "Error getting IP address.");
  200. return smprintf("n/a");
  201. }
  202. /* get the ip address */
  203. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
  204. {
  205. if (ifa->ifa_addr == NULL)
  206. continue;
  207. s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
  208. if ((strcmp(ifa->ifa_name, interface) == 0) && (ifa->ifa_addr->sa_family == AF_INET))
  209. {
  210. if (s != 0)
  211. {
  212. fprintf(stderr, "Error getting IP address.");
  213. return smprintf("n/a");
  214. }
  215. return smprintf("%s", host);
  216. }
  217. }
  218. /* free the address */
  219. freeifaddrs(ifaddr);
  220. /* return n/a if nothing works */
  221. return smprintf("n/a");
  222. }
  223. /* ram percentage */
  224. char *
  225. ram_perc(const char *null)
  226. {
  227. int perc;
  228. long total, free, buffers, cached;
  229. FILE *fp;
  230. /* open meminfo file */
  231. if (!(fp = fopen("/proc/meminfo", "r"))) {
  232. fprintf(stderr, "Error opening meminfo file.");
  233. return smprintf("n/a");
  234. }
  235. /* read the values */
  236. fscanf(fp, "MemTotal: %ld kB\n", &total);
  237. fscanf(fp, "MemFree: %ld kB\n", &free);
  238. fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
  239. fscanf(fp, "Cached: %ld kB\n", &cached);
  240. /* close meminfo file */
  241. fclose(fp);
  242. /* calculate percentage */
  243. perc = 100 * ((total - free) - (buffers + cached)) / total;
  244. /* return perc as string */
  245. return smprintf("%d%%", perc);
  246. }
  247. /* temperature */
  248. char *
  249. temp(const char *file)
  250. {
  251. int temperature;
  252. FILE *fp;
  253. /* open temperature file */
  254. if (!(fp = fopen(file, "r"))) {
  255. fprintf(stderr, "Could not open temperature file.\n");
  256. return smprintf("n/a");
  257. }
  258. /* extract temperature */
  259. fscanf(fp, "%d", &temperature);
  260. /* close temperature file */
  261. fclose(fp);
  262. /* return temperature in degrees */
  263. return smprintf("%d°C", temperature / 1000);
  264. }
  265. /* alsa volume percentage */
  266. char *
  267. vol_perc(const char *soundcard)
  268. {
  269. int mute = 0;
  270. long vol = 0, max = 0, min = 0;
  271. /* get volume from alsa */
  272. snd_mixer_t *handle;
  273. snd_mixer_elem_t *pcm_mixer, *mas_mixer;
  274. snd_mixer_selem_id_t *vol_info, *mute_info;
  275. snd_mixer_open(&handle, 0);
  276. snd_mixer_attach(handle, soundcard);
  277. snd_mixer_selem_register(handle, NULL, NULL);
  278. snd_mixer_load(handle);
  279. snd_mixer_selem_id_malloc(&vol_info);
  280. snd_mixer_selem_id_malloc(&mute_info);
  281. snd_mixer_selem_id_set_name(vol_info, channel);
  282. snd_mixer_selem_id_set_name(mute_info, channel);
  283. pcm_mixer = snd_mixer_find_selem(handle, vol_info);
  284. mas_mixer = snd_mixer_find_selem(handle, mute_info);
  285. snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t *)pcm_mixer, &min, &max);
  286. snd_mixer_selem_get_playback_volume((snd_mixer_elem_t *)pcm_mixer, SND_MIXER_SCHN_MONO, &vol);
  287. snd_mixer_selem_get_playback_switch(mas_mixer, SND_MIXER_SCHN_MONO, &mute);
  288. if (vol_info)
  289. snd_mixer_selem_id_free(vol_info);
  290. if (mute_info)
  291. snd_mixer_selem_id_free(mute_info);
  292. if (handle)
  293. snd_mixer_close(handle);
  294. /* return the string (mute) */
  295. if (!mute)
  296. return smprintf("mute");
  297. else
  298. return smprintf("%d%%", (vol * 100) / max);
  299. }
  300. /* wifi percentage */
  301. char *
  302. wifi_perc(const char *wificard)
  303. {
  304. int bufsize = 255;
  305. int strength;
  306. char buf[bufsize];
  307. char *datastart;
  308. char path[64];
  309. char status[5];
  310. char needle[sizeof wificard + 1];
  311. FILE *fp;
  312. /* generate the path name */
  313. memset(path, 0, sizeof path);
  314. strcat(path, "/sys/class/net/");
  315. strcat(path, wificard);
  316. strcat(path, "/operstate");
  317. /* open wifi file */
  318. if(!(fp = fopen(path, "r"))) {
  319. fprintf(stderr, "Error opening wifi operstate file.");
  320. return smprintf("n/a");
  321. }
  322. /* read the status */
  323. fgets(status, 5, fp);
  324. /* close wifi file */
  325. fclose(fp);
  326. /* check if interface down */
  327. if(strcmp(status, "up\n") != 0){
  328. return smprintf("n/a");
  329. }
  330. /* open wifi file */
  331. if (!(fp = fopen("/proc/net/wireless", "r"))) {
  332. fprintf(stderr, "Error opening wireless file.");
  333. return smprintf("n/a");
  334. }
  335. /* extract the signal strength */
  336. strcpy(needle, wificard);
  337. strcat(needle, ":");
  338. fgets(buf, bufsize, fp);
  339. fgets(buf, bufsize, fp);
  340. fgets(buf, bufsize, fp);
  341. if ((datastart = strstr(buf, needle)) != NULL) {
  342. datastart = strstr(buf, ":");
  343. sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength);
  344. }
  345. /* close wifi file */
  346. fclose(fp);
  347. /* return strength in percent */
  348. return smprintf("%d%%", strength);
  349. }
  350. /* main function */
  351. int
  352. main()
  353. {
  354. char status_string[1024];
  355. struct arg argument;
  356. /* try to open display */
  357. if (!(dpy = XOpenDisplay(0x0))) {
  358. fprintf(stderr, "Cannot open display!\n");
  359. exit(1);
  360. }
  361. /* return status every interval */
  362. for (;;) {
  363. /* clear the string */
  364. memset(status_string, 0, sizeof(status_string));
  365. /* generate status_string */
  366. for (size_t i = 0; i < sizeof(args) / sizeof(args[0]); ++i) {
  367. argument = args[i];
  368. char *res = argument.func(argument.args);
  369. char *element = smprintf(argument.format, res);
  370. strcat(status_string, element);
  371. free(res);
  372. free(element);
  373. }
  374. /* return the statusbar */
  375. setstatus(status_string);
  376. /* wait, "update_interval - 1" because of get_cpu_usage() which uses 1 second */
  377. sleep(update_interval -1);
  378. }
  379. /* close display */
  380. XCloseDisplay(dpy);
  381. /* exit successfully */
  382. return 0;
  383. }