commit 9ae8ee32e280e3afb287b041aeeb5f25c9eaf28a
parent 2563348e0bab5126fa332b6886c2d39fe985807c
Author: levi0x0 <levi0x0x@gmail.com>
Date: Fri, 7 Nov 2014 14:45:53 +0200
BarMonitor 0.9 New Version
Diffstat:
2 files changed, 212 insertions(+), 193 deletions(-)
diff --git a/dwm.suckless.org/dwmstatus/bar_monitor.c b/dwm.suckless.org/dwmstatus/bar_monitor.c
@@ -1,258 +1,278 @@
/*
* bar_monitor.c - another version of dwmstatus.
*
- * Author: levi0x0.
+ * Author: levi0x0
*
+ *
+ *
+ * if you Want to write new fucntion to bar_monitor please do so!
+ * and let me know (:
+ *
+ *
+ * XXX:
+ * IN VERSION 0.9 I DIDN'T CHECKED bar_monitor ON a LAPTOP
+ * IF YOU FOUND ANY BUGS PLEASE LET ME KNOW!
+ *
+ *-----------
+ * News:
+ *-----------
+ * What's New in Version 0.9?
+ *
+ * 1) Check Mail function for gmail using libcurl!
+ * 2) a Laptop Support
+ * 3) Porto options
+ * 4) Many Features has been added
+ *
+ * And many more!
+ *
+ *-------------
* ChangeLog:
+ * ------------
+ * 07/11/2014, - 0.9
* 13/09/2014, - 0.8
* 08/08/2014, - 0.7
* 19/07/2014, - 0.3
* 02/07/2014, - 0.1
*
+ *
+ * -----------
+ * Usage
+ * ----------
* .xinitrc:
*
* while [ true ];do
- * xsetroot -name "$(bar_monitor)"
+ * $(bar_monitor)
* done &
*
* bar_monitor contain sleep(1) function.
*
+ *--------------
+ * Compile
+ *--------------
*
- * gcc bar_monitor.c -o bar_monitor
- * mv bar_monitor /usr/local/bin/bar_monitor
+ * Compile:
+ * gcc bar_monitor.c -o bar_monitor -lX11
*
+ * Compile with CHECK_GMAIL_FUNCTION:
+ * gcc bar_monitor.c -o bar_monitor -lX11 -lcurl
*/
-#define _GNU_SOURCE
#include <stdio.h>
-#include <sys/sysinfo.h>
-#include <sys/utsname.h>
#include <stdlib.h>
-#include <string.h>
#include <time.h>
+#include <string.h>
+#include <X11/Xlib.h>
-//BAT1 change if needed
-#define STATUS_FILE "/sys/class/power_supply/BAT1/status"
-#define CAPACITY_FILE "/sys/class/power_supply/BAT1/capacity"
-#define net_file "/proc/net/wireless"
-#define temp_file "/sys/class/hwmon/hwmon0/temp1_input"
-#define STR_SIZE 30
-#define TIME_FORMAT "%H:%M | %d-%m-%y"
-#define date read_date()
-#define status open_status()
-#define pcapacity open_capacity()
-#define temp read_temp()
-#define nett net()
+/* globals*/
+#define VERSION "0.9"
+#define TIME_FORMAT "%H:%M (%d/%m/%Y)"
+#define MEXSTR 1024
+#define CHECK_GMAIL_MAIL 0
-/*
-1 - True
-0 - False
-*/
-#define NET 0
-#define DISPLAY_SYS 1
-/*short bar*/
-#define SHORT 1
-/*SECTION: You can enable only one of the options!*/
-/*sys section uptime, procs*/
-#define DISPLAY_PROC 0 // display proc number
-#define DISPLAY_UPTIME 0 //display uptime
-/*kernel section*/
-#define DISPLAY_KERNEL 1
-#define DISPLAY_KVER 0 //display kernel version
-#define DISPLAY_SYSNAME 0 //display system name
-#define DISPLAY_NODENAME 1
-#define DISPLAY_ARCH 0 //display Machine
-/*system*/
-#define SLEEP 1 //contain sleep(1) fucntion by default
-#define battery 1 //Display battery level
-
-static char status_linecp[STR_SIZE];
-static char time_buffer[STR_SIZE];
-static char net_buffer[STR_SIZE];
-static char uptime[STR_SIZE];
-int capacity;
-
-/*prototypes*/
-char * read_date(void);
-char * open_status(void);
-char * net(void);
-int open_capacity(void);
-int read_temp(void);
+
+/* Laptop config*/
+#define LAPTOP 0
+
+#if LAPTOP
+ #define SHOW_BATTERY 0
+ #define SHOW_TEMP 0
+ #define TEMP_FILE "/sys/class/hwmon/hwmon0/temp1_input"
+
+ /* BAT1 or BAT0 change if needed*/
+ #define CAPACITY_FILE "/sys/class/power_supply/BAT1/capacity"
+ #define BATTERY_STATUS_FILE "/sys/class/power_supply/BAT1/status"
+ char status_linecp[MEXSTR];
+
+ /* Functions prototypes*/
+ char * ReadBatteryStat(void);
+ int ReadBatteryCap(void);
+ int ReadTemp(void);
+
+#endif
+
+/* include libcurl and gmail stuff*/
+#if CHECK_GMAIL_MAIL
+ #include <curl/curl.h>
+ #define CURL_VERBOSE 0
+ static char * GMAIL_MAIL_FEED = "https://mail.google.com/mail/feed/atom";
+ static char * GMAIL_MAIL_USER = "USERNAME";
+ static char * GMAIL_MAIL_PASSWORD = "PASSWORD";
+ static char * USER_AGENT = "Mozilla/5.0 (X11; Linux; rv:0.0) Mail Reader BarMon "VERSION;
+
+ /* Functions prototypes*/
+ int CheckGmail(void);
+#endif
+
+/* Variables */
+static char buffer[MEXSTR];
+static char data_size[MEXSTR];
+
+/* functions prototypes */
+char * TimeADate(void);
+void XSetRoot(char *);
int main(int argc, char **argv) {
- #if battery
- if (!strcmp(status, "Discharging")) {
- //printf("[%d%], Discharging\n", pcapacity);
- if ( pcapacity < 10 ) {
- printf(" %d%% **Low Battery**", pcapacity);
- }
- else {
- printf(" %d%%", pcapacity);
- }
- }
+ char status[MEXSTR];
+ int Newmail = 0;
+
+ #if CHECK_GMAIL_MAIL
+ CheckGmail();
+ /* 348 can be change by the size of your email address,
+ * you can check the size of the curl_buffer and change it*/
+ if ( strlen(data_size) > 348 )
+ Newmail = 1;
+ else
+ Newmail = 0;
+ #endif
+
+ #if LAPTOP
+ sprintf(status, "( %d%%m %s ) (%d) %s", ReadBatteryCap,
+ ReadBatteryStat, Newmail, TimeADate());
+ XSetRoot(status);
+ #else
+ sprintf(status, "(%d) %s", Newmail, TimeADate());
+ XSetRoot(status);
+ #endif
+
+
+
+ /* Sleep function sleep 30 sec and update the bar_monitor
+ * you can inc/dec */
+ sleep(30);
+
+ return 0;
+}
- else if (!strcmp(status, "Charging")) {
- //printf("[%d%], Charging\n", pcapacity);
- printf(" %d%%", pcapacity);
- }
- else if (!strcmp(status, "Full")) {
- printf(" [Full]");
+char * TimeADate(void) {
+ /* get time*/
+ time_t now = time(0);
- }
- else {
- //What?! the program can't read the battery status
- printf(" w00t?\n");
- }
- #endif
+ /* Read the time by the foramt*/
+ strftime(buffer, MEXSTR, TIME_FORMAT, localtime(&now));
- #if DISPLAY_SYS
- struct sysinfo sys;
- int h = 0;
- int m = 0;
+ /* Return the buffer content*/
+ return buffer;
+}
- sysinfo(&sys);
-
- #if DISPLAY_UPTIME
- h = sys.uptime / 3600;
- m = ( sys.uptime - h * 3600) / 60;
- printf(" | (%dh, %dm) |", h, m);
- #elif DISPLAY_PROC
- int procs = 0;
+void XSetRoot(char *name) {
+ Display *display;
- printf("| %dP | ", sys.procs);
- #endif
- #endif
+ /* try open display*/
+ if (( display = XOpenDisplay(0x0)) == NULL ) {
+ fprintf(stderr, "[BarMon] Oh NO! Cannot open display!\n");
+ exit(1);
+ }
-
- /*
- * it will only display one of the Options,
- * We need to save space for the title.
- */
- #if DISPLAY_KERNEL
- struct utsname name;
- uname(&name);
-
- #if DISPLAY_KVER
- printf(" | %s", name.release);
- #elif DISPLAY_SYSNAME
- printf(" | %s", name.sysname);
- #elif DISPLAY_NODENAME
- printf(" | %s", name.nodename);
- #elif DISPLAY_ARCH
- printf(" | %s", name.machine);
- #endif
- #endif
-
- #if NET
- printf(" | %s | %dC | %s", nett,temp, date);
- #else
- printf(" | %dC | %s", temp, date);
- #endif
-
- #if SLEEP
- sleep(1);
- #endif
- return 0;
+ /* set window name*/
+ XStoreName(display, DefaultRootWindow(display), name);
+ XSync(display, 0);
+
+ /* close display*/
+ XCloseDisplay(display);
}
-char * read_date(void) {
- time_t now = time(0);
+void * WriteBackData(void *ptr, size_t nmemb, size_t size, void *stream) {
+ strncpy(data_size, ptr, MEXSTR);
+}
- strftime(time_buffer, STR_SIZE, TIME_FORMAT, localtime(&now));
+#if CHECK_GMAIL_MAIL
+ int CheckGmail(void) {
+ CURL *curl_handler;
+ CURLcode r;
- return time_buffer;
-}
+ /* init curl*/
+ curl_handler = curl_easy_init();
-int open_capacity(void) {
- FILE *capacity_file;
-
- capacity_file = fopen(CAPACITY_FILE, "r");
- if ( capacity_file == NULL ) {
- fprintf(stderr, "Failed to open: %s\n", CAPACITY_FILE);
- exit(EXIT_FAILURE);
- }
+ /* start*/
- fscanf(capacity_file, "%d", &capacity);
- fclose(capacity_file);
+ if ( curl_handler ) {
+ /* feed url*/
+ curl_easy_setopt(curl_handler, CURLOPT_URL, GMAIL_MAIL_FEED);
+
+ /* login det*/
+ curl_easy_setopt(curl_handler, CURLOPT_USERNAME, GMAIL_MAIL_USER);
+ curl_easy_setopt(curl_handler, CURLOPT_PASSWORD, GMAIL_MAIL_PASSWORD);
+
+ /* USER Agent*/
+ curl_easy_setopt(curl_handler, CURLOPT_USERAGENT , USER_AGENT);
+
+ /* SSL*/
+ curl_easy_setopt(curl_handler, CURLOPT_SSL_VERIFYPEER, 1);
+ curl_easy_setopt(curl_handler, CURLOPT_SSL_VERIFYHOST, 1);
+ curl_easy_setopt(curl_handler, CURLOPT_VERBOSE, CURL_VERBOSE);
- return capacity;
-}
+ /* write back function*/
+ curl_easy_setopt(curl_handler, CURLOPT_WRITEFUNCTION, WriteBackData);
+ }
-char * open_status(void) {
- FILE *status_file;
- char *line = NULL;
- size_t len = 0;
- ssize_t read;
-
- status_file = fopen(STATUS_FILE, "rt");
-
- if ( status_file == NULL ) {
- fprintf(stderr, "Failed to open: %s\n");
- exit(EXIT_FAILURE);
- }
+ curl_easy_perform(curl_handler);
+ curl_easy_cleanup(curl_handler);
- while (( read = getline(&line, &len, status_file)) != -1 ) {
- ;
- }
+ }
+#endif
- strncpy(status_linecp, line, strlen(line));
-
- strtok(status_linecp, "\n");
- free(line);
- return status_linecp;
-}
+#if LAPTOP
+ /* read the temp*/
+ int ReadTemp(void) {
+ FILE *t_file;
+ int ret = 0;
-char * net(void) {
- FILE *fp;
- char buffer[STR_SIZE];
- char *line = NULL;
- ssize_t len = 0;
- ssize_t read;
+ if (( t_file = fopen(TEMP_FILE, "rt")) == NULL) {
+ fprintf(stderr, "Failed to open: %s\n", TEMP_FILE);
+ exit(1);
+ }
- fp = fopen(net_file, "rt");
+ fscanf(t_file, "%d", &ret);
- while (( read = getline(&line, &len, fp)) != -1 ) {
- ;
+ return ret / 1000;
}
- sscanf(line,"%s\n", &buffer);
- free(line);
- //if( strstr(buffer, "wlan") != NULL) {
- if( strstr(buffer, "wlp") != NULL) {
- return "(ON)";
- }
- else {
- return "(OFF)";
+ int ReadBatteryCap(void) {
+ int capacity = 0;
+ FILE *fp;
+
+ if (( fp = fopen(CAPACITY_FILE, "r")) == NULL ){
+ fprintf(stderr, "Failed to open: %s\n", CAPACITY_FILE);
+ exit(EXIT_FAILURE);
+ }
+
+ fscanf(fp, "%d", &capacity);
+ fclose(fp);
+
+ return capacity;
}
-}
-/*
-* This fucntion read the temperature form temp_file
-*/
-int read_temp(void) {
- FILE *t_file;
- int ret = 0;
+ char * ReadBatteryStat(void) {
+ FILE *status_file;
+ char *line = NULL;
+ size_t len = 0;
+ ssize_t read;
+ if (( status_file = fopen(BATTERY_STATUS_FILE, "rt")) == NULL ) {
+ fprintf(stderr, "Failed to open: %s\n", BATTERY_STATUS_FILE);
+ exit(EXIT_FAILURE);
+ }
- t_file = fopen(temp_file, "rt");
+ while (( read = getline(&line, &len, status_file)) != -1 ) {
+ ;
+ }
+ strncpy(status_linecp, line, strlen(line));
+
+ strtok(status_linecp, "\n");
+ free(line);
- if ( t_file == NULL ) {
- fprintf(stderr, "Failed to open: %s\n", temp_file);
- exit(1);
+ return status_linecp;
}
- fscanf(t_file, "%d", &ret);
-
- return ret / 1000;
-}
+#endif
diff --git a/dwm.suckless.org/dwmstatus/index.md b/dwm.suckless.org/dwmstatus/index.md
@@ -24,8 +24,7 @@ Please add your own version of dwmstatus here.
* [suspend-statusbar.c](suspend-statusbar.c) - loadavg, wifi, battery and date. If battery goes below threshold - run suspend command
* [gods](https://github.com/schachmat/gods) - implemented in Go. prints network speed, cpu, ram, date/time
* [go-dwmstatus](https://github.com/oniichaNj/go-dwmstatus) - A Go bar that prints current MPD song, load averages, time/date and battery percentage.
-* [barmonitor](bar_monitor.c) - displays, battery status, date/time, temperature, network status, Uptime (Optinal).
-
+* [BarMonitor](bar_monitor.c) - displays, battery status, date/time, temperature, check mail function.
Helper functions
----------------