Initial import of wifi block

This commit is contained in:
Jack Henschel 2017-03-18 11:26:49 +01:00
parent 8f6039b291
commit 56436505c0
4 changed files with 114 additions and 5 deletions

View file

@ -2,7 +2,7 @@ VERSION ?= $(shell git describe)
CPPFLAGS += -DVERSION=\"$(VERSION)\" -D_POSIX_C_SOURCE=199309L -DYA_INTERNAL -DYA_DYN_COL \
-DYA_ENV_VARS -DYA_INTERNAL_EWMH -DYA_ICON -DYA_NOWIN_COL -DYA_MUTEX -DYA_VAR_WIDTH -DYA_BSPWM
CFLAGS += -std=c99 -Iinclude -pedantic -Wall -Os `pkg-config --cflags pango pangocairo libconfig gdk-pixbuf-2.0 alsa`
LDLIBS += -lxcb -lpthread -lxcb-randr -lxcb-ewmh -lxcb-icccm `pkg-config --libs pango pangocairo libconfig gdk-pixbuf-2.0 alsa`
LDLIBS += -liw -lxcb -lpthread -lxcb-randr -lxcb-ewmh -lxcb-icccm `pkg-config --libs pango pangocairo libconfig gdk-pixbuf-2.0 alsa`
PROGRAM := yabar
PREFIX ?= /usr
BINPREFIX ?= $(PREFIX)/bin

View file

@ -4,7 +4,7 @@
bar-list = ["topbar"];
topbar:{
font: "Droid Sans, FontAwesome Bold 9";
block-list: ["ya_diskspace", "ya_ws", "ya_title", "ya_date", "ya_volume", "ya_uptime", "ya_cpu", "ya_thermal", "ya_brightness", "ya_bw", "ya_mem", "ya_disk", "ya_bat"];
block-list: ["ya_diskspace", "ya_ws", "ya_title", "ya_date", "ya_volume", "ya_uptime", "ya_cpu", "ya_thermal", "ya_brightness", "ya_bw", "ya_mem", "ya_disk", "ya_bat", "ya_wifi"];
position: "top";
gap-horizontal: 10;
gap-vertical: 10;
@ -142,7 +142,7 @@ topbar:{
underline-color-rgb:0xECD078;
#internal-spacing: true;
}
ya_diskspace:{
ya_diskspace: {
exec: "YABAR_DISKSPACE";
align: "left";
fixed-size: 120;
@ -157,6 +157,11 @@ topbar:{
background-color-rgb:0x49708A;
underline-color-rgb:0xECD078;
}
ya_wifi: {
exec: "YABAR_WIFI";
internal-prefix: " ";
internal-option1: "wlan0";
}
# example for an external block
title: {
exec: "xtitle -s";

View file

@ -119,9 +119,9 @@ enum {
#ifdef YA_INTERNAL_EWMH
#define YA_INTERNAL_LEN 14
#define YA_INTERNAL_LEN 15
#else
#define YA_INTERNAL_LEN 12
#define YA_INTERNAL_LEN 13
#endif
enum {
YA_INT_DATE = 0,
@ -136,6 +136,7 @@ enum {
YA_INT_NETWORK,
YA_INT_BATTERY,
YA_INT_VOLUME,
YA_INT_WIFI,
YA_INT_TITLE,
YA_INT_WORKSPACE
};

View file

@ -20,6 +20,7 @@ void ya_int_diskio(ya_block_t *blk);
void ya_int_network(ya_block_t *blk);
void ya_int_battery(ya_block_t *blk);
void ya_int_volume(ya_block_t *blk);
void ya_int_wifi(ya_block_t *blk);
struct reserved_blk ya_reserved_blks[YA_INTERNAL_LEN] = {
{"YABAR_DATE", ya_int_date},
@ -34,6 +35,7 @@ struct reserved_blk ya_reserved_blks[YA_INTERNAL_LEN] = {
{"YABAR_NETWORK", ya_int_network},
{"YABAR_BATTERY", ya_int_battery},
{"YABAR_VOLUME", ya_int_volume},
{"YABAR_WIFI", ya_int_wifi},
#ifdef YA_INTERNAL_EWMH
{"YABAR_TITLE", NULL},
{"YABAR_WORKSPACE", NULL}
@ -573,6 +575,107 @@ ya_volume_error:
pthread_exit(NULL);
}
#include <net/if.h>
#include <iwlib.h>
#include <wireless.h>
struct wireless_stats {
struct sockaddr addr;
char name[IFNAMSIZ + 1];
char essid[IW_ESSID_MAX_SIZE + 1];
char ap[18];
char mode[16];
char freq[16];
int channel;
char bitrate[16];
int link_qual; /* calculate percentage from link_qual and link_qual_max */
int link_qual_max;
int link_level; /* dBm */
int link_noise; /* dBm */
};
static int ya_int_get_wireless_info(struct wireless_stats* ws, char *dev_name) {
int skfd;
struct wireless_info winfo;
/* not all fields may be initialized, so set them to zero */
memset(ws, 0, sizeof(struct wireless_stats));
memset(&winfo, 0, sizeof(struct wireless_info));
skfd = iw_sockets_open();
snprintf(ws->name, IFNAMSIZ+1, "%s", dev_name);
if (iw_get_basic_config(skfd, dev_name, &(winfo.b)) > -1) {
// set present winfo variables
if (iw_get_range_info(skfd, dev_name, &(winfo.range)) >= 0) {
winfo.has_range = 1;
}
if (iw_get_stats(skfd, dev_name, &(winfo.stats), &winfo.range, winfo.has_range) >= 0) {
winfo.has_stats = 1;
}
/* Link Quality */
if (winfo.has_range && winfo.has_stats && ((winfo.stats.qual.level != 0)
|| (winfo.stats.qual.updated & IW_QUAL_DBM))) {
if (!(winfo.stats.qual.updated & IW_QUAL_QUAL_INVALID)) {
ws->link_qual = winfo.stats.qual.qual;
ws->link_qual_max = winfo.range.max_qual.qual;
ws->link_noise = winfo.stats.qual.noise;
ws->link_level = winfo.stats.qual.level;
}
}
/* ESSID */
if (winfo.b.has_essid) {
if (winfo.b.essid_on) {
snprintf(ws->essid, 32, "%s", winfo.b.essid);
}
}
/* Channel and Frequency */
if (winfo.b.has_freq) {
if(winfo.has_range == 1) {
ws->channel = iw_freq_to_channel(winfo.b.freq, &(winfo.range));
iw_print_freq_value(ws->freq, 16, winfo.b.freq);
}
}
snprintf(ws->mode, 16, "%s", iw_operation_mode[winfo.b.mode]);
}
iw_sockets_close(skfd);
return 0;
}
void ya_int_wifi(ya_block_t *blk) {
struct wireless_stats ws;
char *startstr = blk->buf;
size_t prflen = 0, suflen = 0;
ya_setup_prefix_suffix(blk, &prflen, &suflen, &startstr);
while(1) {
if ( ya_int_get_wireless_info(&ws, blk->internal->option[0]) ) {
strcpy(blk->buf, "BLOCK ERROR!");
ya_draw_pango_text(blk);
sleep(blk->sleep);
continue;
}
sprintf(startstr, "%s (%d%%)", ws.essid, ws.link_qual*100 / ws.link_qual_max);
if(suflen)
strcat(blk->buf, blk->internal->suffix);
ya_draw_pango_text(blk);
sleep(blk->sleep);
}
}
#define _GNU_SOURCE
#include <sys/socket.h>
#include <ifaddrs.h>