Hi, DWM users! I wrote a daemon to the DWM status bar! I send it in
attachment. The program name "kajjam". Start it simple: ./kajjam

Compile it with this command:

g++ -funsigned-char -funsigned-bitfields -Wall -Wno-long-long -Wunused
-Wextra -pedantic -lX11 kajjam.cpp -o kajjam

How to stop it: kill its process.

The "kajjam" print to the status bar in every 1 sec the upload and download
speed of the internet connection, and the current date and time. (The name
of the day of week in the hungarian abbreviation. It can simple changed in
the source code).

Zoli
/* kajjam démon. Készült a DWM ablakkezelőhöz. Indítás után a statuszbaron kijelzi az aktuális időt és dátumot,
valamint az internetkapcsolatunk aktuális le- és feltöltési sebességét.
Így kell lefordítani:
g++ -funsigned-char -funsigned-bitfields -Wall -Wno-long-long -Wunused -Wextra -pedantic -lX11 kajjam.cpp -o kajjam

*/




#define _BSD_SOURCE

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>

#include <stdbool.h>
#include <stdarg.h>
#include <strings.h>
#include <sys/time.h>
#include <time.h>
#include <sys/wait.h>

#include <X11/Xlib.h>





static const char logfilepath[]="/Programs/Kajjam/log/kajjam.log";
static const char munkakonyvtar[]="/Programs/Kajjam";
static char idoformatum[]="%m.%d %H:%M ";
static const char hetnapjai[][4]={"V  ", "H  ", "K  ", "Sze", "Cs ", "P  ", "Szo"};
static char HET[]="      ";
static Display *dpy;
FILE *logfilefp;



char * smprintf(char *fmt, ...) // -------------------------------------------------------------
{
va_list fmtargs;
char *ret;
int len;

va_start(fmtargs, fmt);
len = vsnprintf(NULL, 0, fmt, fmtargs);
va_end(fmtargs);

ret = (char *)malloc(++len);
if (ret == NULL) {
fprintf(logfilefp, "malloc probléma!\n");
exit(1);
}

va_start(fmtargs, fmt);
vsnprintf(ret, len, fmt, fmtargs);
va_end(fmtargs);

return ret;
}
// ---------------------------------------------------------------------------------------------

void settz(char *tzname)
{
setenv("TZ", tzname, 1);
}
// ------------------------------------------------------------------------------------------------------------
int parse_netdev(unsigned long long int *receivedabs, unsigned long long int *sentabs)
{
char *buf;
char *eth0start;
static int bufsize;
FILE *devfd;

buf = (char *) calloc(255, 1);
bufsize = 255;
devfd = fopen("/proc/net/dev", "r");

// ignore the first two lines of the file
fgets(buf, bufsize, devfd);
fgets(buf, bufsize, devfd);

while (fgets(buf, bufsize, devfd)) {
    if ((eth0start = strstr(buf, "eth0:")) != NULL) {

// With thanks to the conky project at http://conky.sourceforge.net/
sscanf(eth0start + 6, "%llu  %*d     %*d  %*d  %*d  %*d   %*d        %*d       %llu",\
       receivedabs, sentabs);
fclose(devfd);
free(buf);
return 0;
    }
}
fclose(devfd);
free(buf);
return 1;
}
// ------------------------------------------------------------------------------------------------------
char * get_netusage()
{
unsigned long long int oldrec, oldsent, newrec, newsent;
double downspeed, upspeed;
char *downspeedstr, *upspeedstr;
char *retstr;
int retval;

downspeedstr = (char *) malloc(15);
upspeedstr = (char *) malloc(15);
retstr = (char *) malloc(42);

retval = parse_netdev(&oldrec, &oldsent);
if (retval) {
    fprintf(logfilefp, "Error when parsing /proc/net/dev file.\n");
    exit(1);
}

sleep(1);
retval = parse_netdev(&newrec, &newsent);
if (retval) {
    fprintf(logfilefp, "Error when parsing /proc/net/dev file.\n");
    exit(1);
}

downspeed = (newrec - oldrec) / 1024.0;
if (downspeed > 1024.0) {
    downspeed /= 1024.0;
    sprintf(downspeedstr, "%.2f M", downspeed);
} else {
//    sprintf(downspeedstr, "%.2f K", downspeed);
    sprintf(downspeedstr, "%.f K", downspeed);
}

upspeed = (newsent - oldsent) / 1024.0;
if (upspeed > 1024.0) {
    upspeed /= 1024.0;
    sprintf(upspeedstr, "%.2f M", upspeed);
} else {
//    sprintf(upspeedstr, "%.2f K", upspeed);
    sprintf(upspeedstr, "%.f K", upspeed);
}
sprintf(retstr, "ˇ%s ^%s", downspeedstr, upspeedstr);

free(downspeedstr);
free(upspeedstr);
return retstr;
}
// --------------------------------------------------------------------------------------------------
char * mktimes(char *fmt)
{
char buf[129];
time_t *ido;
time_t rawtime;
struct tm ideiglenes;
time(&rawtime);
ido=&rawtime;
ideiglenes = *localtime(ido);
    bzero(buf, sizeof(buf));
if (!strftime(buf, sizeof(buf)-1, fmt, &ideiglenes)) {
fprintf(logfilefp, "strftime == 0\n");
exit(1);
}
HET[0]=hetnapjai[ideiglenes.tm_wday][0];
HET[1]=hetnapjai[ideiglenes.tm_wday][1];
HET[2]=hetnapjai[ideiglenes.tm_wday][2];
HET[3]=0;
return smprintf("%s", buf);
}
// ------------------------------------------------------------------------------------------

void setstatus(char *str)
{
XStoreName(dpy, DefaultRootWindow(dpy), str);
XSync(dpy, False);
}
// --------------------------------------------------------------------------------------------------

// --------------------------------------------------------------------------------------------------



int main(void) {

char *status;
char *tmutc;
char *netstats;


        
        /* Our process ID and Session ID */
        pid_t pid, sid;
        
        /* Fork off the parent process */
        pid = fork();
        if (pid < 0) {
                exit(EXIT_FAILURE);
        }
        /* If we got a good PID, then
           we can exit the parent process. */
        if (pid > 0) {
                exit(EXIT_SUCCESS);
        }

        /* Change the file mode mask */
        umask(0);

// Nyitunk egy log file-ot

logfilefp=fopen(logfilepath,"wb+");
if (!logfilefp) { printf ("Nem tudom megnyitni a %s állományt!\n", logfilepath); return(1); }



    /* Create a new SID for the child process */
    sid = setsid();
    if (sid < 0) {
    /* Log the failure */
    exit(EXIT_FAILURE);
}
                                                                                                        
                                                                                                                        
                                                                
// Az aktuális munkakönyvtár váltása
if ((chdir(munkakonyvtar)) < 0) {
/* Log the failure */
exit(EXIT_FAILURE);
}
                                                                                                                                                                                /* Close out the standard file descriptors */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);

/* Demon-specifikus inicializaciok */


if (!(dpy = XOpenDisplay(NULL))) {
fprintf(logfilefp, "kajjámstátusz: cannot open display.\n");
return 1;
}




// *************************************************************
// *************************************************************
// *************************************************************
// *************************************************************
//  FŐCIKLUS !!!!
// *************************************************************
// *************************************************************
// *************************************************************
// *************************************************************

while (1) {

// Ide jönnek a démon által végrehajtott feladatok



tmutc = mktimes(idoformatum);
netstats = get_netusage();

status = smprintf("%s %s %s",netstats, tmutc, HET);


setstatus(status);




sleep(1); // Vár 1 másodpercig


}

// FŐCIKLUS VÉGE
// *************************************************************
// *************************************************************
// *************************************************************



XCloseDisplay(dpy);

free(netstats);
free(tmutc);
free(status);


fclose(logfilefp); // bezárjuk a logfile állományt

exit(EXIT_SUCCESS);
}

Reply via email to