sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

barM.c (3133B)


      1 /*
      2  * Copyright (C) 2014,2015 levi0x0 with enhancements by ProgrammerNerd
      3  * 
      4  * barM (bar_monitor or BarMonitor) is free software; you can redistribute it and/or
      5  * modify it under the terms of the GNU General Public License
      6  * as published by the Free Software Foundation; either version 2
      7  * of the License, or (at your option) any later version.
      8  * 
      9  *  This is a new version of bar monitor, even less lines of code more effective.
     10  *
     11  *  Read main() to configure your new status Bar.
     12  *
     13  *  compile: gcc -o barM barM.c -O2 -s -lX11
     14  *  
     15  *  mv barM /usr/local/bin/
     16  */
     17 
     18 #include <stdio.h>
     19 #include <stdlib.h>
     20 #include <time.h>
     21 #include <string.h>
     22 #include <stdarg.h>
     23 #include <X11/Xlib.h>
     24 #include <sys/utsname.h>
     25 #include <sys/sysinfo.h>
     26 
     27 /*
     28  *  Put this in your .xinitrc file: 
     29  *
     30  *  barM&
     31  *  
     32  */
     33 
     34 #define VERSION "0.12"
     35 #define TIME_FORMAT "%H:%M) (%d-%m-%Y"
     36 #define MAXSTR  1024
     37 
     38 static const char * date(void);
     39 static const char * getuname(void);
     40 static const char * ram(void);
     41 static void XSetRoot(const char *name);
     42 /*Append here your functions.*/
     43 static const char*(*const functab[])(void)={
     44         ram,date
     45 };
     46 
     47 int main(void){
     48         char status[MAXSTR];
     49         /* It is foolish to repeatedly update uname. */
     50         int ret;
     51         {struct utsname u;
     52         if(uname(&u)){
     53                 perror("uname failed");
     54                 return 1;
     55         }
     56         ret=snprintf(status,sizeof(status),"(%s %s %s) ",u.sysname,u.nodename,u.release);}
     57         char*off=status+ret;
     58         if(off>=(status+MAXSTR)){
     59                 XSetRoot(status);
     60                 return 1;/*This should not happen*/
     61         }
     62         for(;;){
     63                 int left=sizeof(status)-ret,i;
     64                 char*sta=off;
     65                 for(i = 0; i<sizeof(functab)/sizeof(functab[0]); ++i ) {
     66                         int ret=snprintf(sta,left,"(%s) ",functab[i]());
     67                         sta+=ret;
     68                         left-=ret;
     69                         if(sta>=(status+MAXSTR))/*When snprintf has to resort to truncating a string it will return the length as if it were not truncated.*/
     70                                 break;
     71                 }
     72                 XSetRoot(status);
     73                 sleep(1);
     74         }
     75         return 0;
     76 }
     77 
     78 /* Returns the date*/
     79 static const char * date(void){
     80         static char date[MAXSTR];
     81         time_t now = time(0);
     82 
     83         strftime(date, MAXSTR, TIME_FORMAT, localtime(&now));
     84         return date;
     85 }
     86 /* Returns a string that contains the amount of free and available ram in megabytes*/
     87 static const char * ram(void){
     88         static char ram[MAXSTR];
     89         struct sysinfo s;
     90         sysinfo(&s);
     91         snprintf(ram,sizeof(ram),"%.1fM,%.1fM",((double)(s.totalram-s.freeram))/1048576.,((double)s.totalram)/1048576.);
     92         return ram;
     93 }
     94 
     95 static void XSetRoot(const char *name){
     96         Display *display;
     97 
     98         if (( display = XOpenDisplay(0x0)) == NULL ) {
     99                 fprintf(stderr, "[barM] cannot open display!\n");
    100                 exit(1);
    101         }
    102 
    103         XStoreName(display, DefaultRootWindow(display), name);
    104         XSync(display, 0);
    105 
    106         XCloseDisplay(display);
    107 }
    108