From 25bf9e97392c94a3328fc3d452578ad1b968cb32 Mon Sep 17 00:00:00 2001 From: Georg Reinke Date: Tue, 24 Jul 2012 12:08:02 +0200 Subject: [PATCH 1/6] Make BAR_BOTTOM a command line option --- README.md | 3 ++- bar.c | 38 +++++++++++++++++++++++--------------- config.def.h | 2 -- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 0f34553..8235b39 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,9 @@ Options bar accepts a couple of command line switches. ``` --h Shows the help and bails out. +-h Show the help and bail out. -p Make the bar permanent. +-b Show the bar at the bottom of the screen. ``` Configuration diff --git a/bar.c b/bar.c index 44a9686..7145cfc 100644 --- a/bar.c +++ b/bar.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -36,6 +37,7 @@ static xcb_gcontext_t draw_gc; static xcb_gcontext_t clear_gc; static xcb_gcontext_t underl_gc; static int bar_width; +static bool bar_bottom; static fontset_item_t fontset[FONT_MAX]; static fontset_item_t *sel_font = &fontset[FONT_MAIN]; @@ -231,6 +233,7 @@ set_ewmh_atoms (xcb_window_t root) xcb_get_property_reply_t *reply1; xcb_atom_t atoms[5]; int compliance_lvl; + unsigned int v[12] = {0}; cookies[0] = xcb_intern_atom (c, 0, strlen ("_NET_WM_WINDOW_TYPE") , "_NET_WM_WINDOW_TYPE"); cookies[1] = xcb_intern_atom (c, 0, strlen ("_NET_WM_WINDOW_TYPE_DOCK"), "_NET_WM_WINDOW_TYPE_DOCK"); @@ -272,12 +275,15 @@ set_ewmh_atoms (xcb_window_t root) } /* Tell the WM that this space is for the bar */ if (*a == atoms[3]) { - xcb_change_property (c, XCB_PROP_MODE_REPLACE, win, atoms[3], XCB_ATOM_CARDINAL, 32, 12, -#if (BAR_BOTTOM == 1) - (const unsigned []) { 0, 0, 0, BAR_HEIGHT, 0, 0, 0, 0, 0, 0, 0, bar_width } ); -#else - (const unsigned []) { 0, 0, BAR_HEIGHT, 0, 0, 0, 0, 0, bar_width, 0, 0, 0 } ); -#endif + if (bar_bottom) { + v[3] = BAR_HEIGHT; + v[11] = bar_width; + } + else { + v[2] = BAR_HEIGHT; + v[8] = bar_width; + } + xcb_change_property (c, XCB_PROP_MODE_REPLACE, win, atoms[3], XCB_ATOM_CARDINAL, 32, 12, v); compliance_lvl++; } } @@ -292,6 +298,7 @@ init (void) { xcb_screen_t *scr; xcb_window_t root; + int y; /* Connect to X */ c = xcb_connect (NULL, NULL); @@ -311,12 +318,11 @@ init (void) /* Create the main window */ win = xcb_generate_id (c); - xcb_create_window (c, XCB_COPY_FROM_PARENT, win, root, 0, -#if (BAR_BOTTOM == 1) - scr->height_in_pixels - BAR_HEIGHT, -#else - 0, -#endif + if (bar_bottom) + y = scr->height_in_pixels - BAR_HEIGHT; + else + y = 0; + xcb_create_window (c, XCB_COPY_FROM_PARENT, win, root, 0, y, bar_width, BAR_HEIGHT, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, scr->root_visual, XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK, (const unsigned []){ palette[0], XCB_EVENT_MASK_EXPOSURE }); @@ -380,14 +386,16 @@ main (int argc, char **argv) int permanent = 0; char ch; - while ((ch = getopt (argc, argv, "ph")) != -1) { + while ((ch = getopt (argc, argv, "phb")) != -1) { switch (ch) { case 'h': - printf ("usage: %s [-p | -h]\n" - "\t-h Shows this help\n" + printf ("usage: %s [-p | -h] [-b]\n" + "\t-h Show this help\n" + "\t-b Put bar at the bottom of the screen\n" "\t-p Don't close after the data ends\n", argv[0]); exit (0); case 'p': permanent = 1; break; + case 'b': bar_bottom = 1; break; } } diff --git a/config.def.h b/config.def.h index 44a719b..56f1177 100644 --- a/config.def.h +++ b/config.def.h @@ -2,8 +2,6 @@ #define BAR_HEIGHT 18 /* The thickness of the underline (in pixels) */ #define BAR_UNDERLINE_HEIGHT 2 -/* Whether to put the bar at the screen bottom or not */ -#define BAR_BOTTOM 0 /* The font used for the bar */ #define BAR_MAIN_FONT "-*-terminus-medium-r-normal-*-12-*-*-*-c-*-*-1" #define BAR_FALLBACK_FONT "fixed" From 8a35dc76952ae5055c87d4be34d4baf146354011 Mon Sep 17 00:00:00 2001 From: Georg Reinke Date: Tue, 24 Jul 2012 12:08:12 +0200 Subject: [PATCH 2/6] Format LICENSE correctly --- LICENSE | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/LICENSE b/LICENSE index ec32196..53f985a 100644 --- a/LICENSE +++ b/LICENSE @@ -1,7 +1,19 @@ Copyright (C) 2012 The Lemon Man -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation the +rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +sell copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. From ab350c86f7ab08a237ddaaf6429fc175bb1f1170 Mon Sep 17 00:00:00 2001 From: Georg Reinke Date: Tue, 24 Jul 2012 12:25:45 +0200 Subject: [PATCH 3/6] Several small corrections --- Makefile | 2 +- bar.c | 40 +++++++++++++++++++++++----------------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index ddc40b2..cf17f93 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC ?= gcc STRIP ?= strip -CFLAGS ?= --std=c99 -fshort-wchar -Os +CFLAGS ?= -std=c99 -fshort-wchar -Os LDFLAGS += -lxcb CFDEBUG = -g3 -pedantic -Wall -Wunused-parameter -Wlong-long\ -Wsign-conversion -Wconversion -Wimplicit-function-declaration diff --git a/bar.c b/bar.c index 7145cfc..c84921e 100644 --- a/bar.c +++ b/bar.c @@ -30,6 +30,12 @@ enum { FONT_MAX }; +enum { + ALIGN_L = 0, + ALIGN_C, + ALIGN_R +}; + static xcb_connection_t *c; static xcb_window_t win; static xcb_drawable_t canvas; @@ -54,13 +60,13 @@ xcb_set_bg (int i) void xcb_set_fg (int i) { - xcb_change_gc (c, draw_gc , XCB_GC_FOREGROUND, (const unsigned []){ palette[i] }); + xcb_change_gc (c, draw_gc , XCB_GC_FOREGROUND, (const uint32_t []){ palette[i] }); } void xcb_set_ud (int i) { - xcb_change_gc (c, underl_gc, XCB_GC_FOREGROUND, (const unsigned []){ palette[i] }); + xcb_change_gc (c, underl_gc, XCB_GC_FOREGROUND, (const uint32_t []){ palette[i] }); } void @@ -73,19 +79,19 @@ void xcb_set_fontset (int i) { sel_font = &fontset[i&1]; - xcb_change_gc (c, draw_gc , XCB_GC_FONT, (const unsigned []){ sel_font->xcb_ft }); + xcb_change_gc (c, draw_gc , XCB_GC_FONT, (const uint32_t []){ sel_font->xcb_ft }); } int draw_char (int x, int align, wchar_t ch) { switch (align) { - case 1: + case ALIGN_C: xcb_copy_area (c, canvas, canvas, draw_gc, bar_width / 2 - x / 2, 0, bar_width / 2 - (x + sel_font->width) / 2, 0, x, BAR_HEIGHT); x = bar_width / 2 - (x + sel_font->width) / 2 + x; break; - case 2: + case ALIGN_R: xcb_copy_area (c, canvas, canvas, draw_gc, bar_width - x, 0, bar_width - x - sel_font->width, 0, x, BAR_HEIGHT); x = bar_width - sel_font->width; @@ -141,15 +147,15 @@ parse (char *text) break; case 'l': - align = 0; + align = ALIGN_L; pos_x = 0; break; case 'c': - align = 1; + align = ALIGN_C; pos_x = 0; break; case 'r': - align = 2; + align = ALIGN_R; pos_x = 0; break; } @@ -233,7 +239,7 @@ set_ewmh_atoms (xcb_window_t root) xcb_get_property_reply_t *reply1; xcb_atom_t atoms[5]; int compliance_lvl; - unsigned int v[12] = {0}; + uint32_t v[12] = {0}; cookies[0] = xcb_intern_atom (c, 0, strlen ("_NET_WM_WINDOW_TYPE") , "_NET_WM_WINDOW_TYPE"); cookies[1] = xcb_intern_atom (c, 0, strlen ("_NET_WM_WINDOW_TYPE_DOCK"), "_NET_WM_WINDOW_TYPE_DOCK"); @@ -270,7 +276,7 @@ set_ewmh_atoms (xcb_window_t root) /* Show on every desktop */ if (*a == atoms[2]) { xcb_change_property (c, XCB_PROP_MODE_REPLACE, win, atoms[2], XCB_ATOM_CARDINAL, 32, 1, - (const unsigned []){ 0xffffffff } ); + (const uint32_t []){ 0xffffffff } ); compliance_lvl++; } /* Tell the WM that this space is for the bar */ @@ -324,13 +330,13 @@ init (void) y = 0; xcb_create_window (c, XCB_COPY_FROM_PARENT, win, root, 0, y, bar_width, BAR_HEIGHT, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, scr->root_visual, - XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK, (const unsigned []){ palette[0], XCB_EVENT_MASK_EXPOSURE }); + XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK, (const uint32_t []){ palette[0], XCB_EVENT_MASK_EXPOSURE }); /* Set EWMH hints */ int ewmh_docking = set_ewmh_atoms (root); /* Quirk for wm not supporting the EWMH docking method */ - xcb_change_window_attributes (c, win, XCB_CW_OVERRIDE_REDIRECT, (const unsigned []){ !ewmh_docking }); + xcb_change_window_attributes (c, win, XCB_CW_OVERRIDE_REDIRECT, (const uint32_t []){ !ewmh_docking }); /* Create a temporary canvas */ canvas = xcb_generate_id (c); @@ -338,13 +344,13 @@ init (void) /* Create the gc for drawing */ draw_gc = xcb_generate_id (c); - xcb_create_gc (c, draw_gc, root, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND, (const unsigned []){ palette[1], palette[0] }); + xcb_create_gc (c, draw_gc, root, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND, (const uint32_t []){ palette[1], palette[0] }); clear_gc = xcb_generate_id (c); - xcb_create_gc (c, clear_gc, root, XCB_GC_FOREGROUND, (const unsigned []){ palette[0] }); + xcb_create_gc (c, clear_gc, root, XCB_GC_FOREGROUND, (const uint32_t []){ palette[0] }); underl_gc = xcb_generate_id (c); - xcb_create_gc (c, underl_gc, root, XCB_GC_FOREGROUND, (const unsigned []){ palette[0] }); + xcb_create_gc (c, underl_gc, root, XCB_GC_FOREGROUND, (const uint32_t []){ palette[0] }); /* Make the bar visible */ xcb_map_window (c, win); @@ -399,10 +405,10 @@ main (int argc, char **argv) } } - atexit (cleanup); signal (SIGINT, sighandle); signal (SIGTERM, sighandle); init (); + atexit (cleanup); /* Get the fd to Xserver */ pollin[1].fd = xcb_get_file_descriptor (c); @@ -412,7 +418,7 @@ main (int argc, char **argv) for (;;) { int redraw = 0; - if (poll ((struct pollfd *)&pollin, 2, -1) > 0) { + if (poll (pollin, 2, -1) > 0) { if (pollin[0].revents & POLLHUP) { /* No more data... */ if (permanent) pollin[0].fd = -1; /* ...null the fd and continue polling :D */ else break; /* ...bail out */ From ed067579b9b4a32c68ad11ab6ff9303f9932cdaf Mon Sep 17 00:00:00 2001 From: Georg Reinke Date: Tue, 24 Jul 2012 14:12:04 +0200 Subject: [PATCH 4/6] Correct cleanup() behaviour --- bar.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/bar.c b/bar.c index c84921e..5b5bf11 100644 --- a/bar.c +++ b/bar.c @@ -360,14 +360,22 @@ init (void) void cleanup (void) { - xcb_close_font (c, fontset[FONT_MAIN].xcb_ft); - xcb_close_font (c, fontset[FONT_FALLBACK].xcb_ft); - xcb_free_pixmap (c, canvas); - xcb_destroy_window (c, win); - xcb_free_gc (c, draw_gc); - xcb_free_gc (c, clear_gc); - xcb_free_gc (c, underl_gc); - xcb_disconnect (c); + if (fontset[FONT_MAIN].xcb_ft) + xcb_close_font (c, fontset[FONT_MAIN].xcb_ft); + if (fontset[FONT_FALLBACK].xcb_ft) + xcb_close_font (c, fontset[FONT_FALLBACK].xcb_ft); + if (canvas) + xcb_free_pixmap (c, canvas); + if (win) + xcb_destroy_window (c, win); + if (draw_gc) + xcb_free_gc (c, draw_gc); + if (clear_gc) + xcb_free_gc (c, clear_gc); + if (underl_gc) + xcb_free_gc (c, underl_gc); + if (c) + xcb_disconnect (c); } void @@ -405,10 +413,10 @@ main (int argc, char **argv) } } + atexit (cleanup); signal (SIGINT, sighandle); signal (SIGTERM, sighandle); init (); - atexit (cleanup); /* Get the fd to Xserver */ pollin[1].fd = xcb_get_file_descriptor (c); From 2b9ca04290591fe0ae82a94450a0b2f2c91b900b Mon Sep 17 00:00:00 2001 From: Georg Reinke Date: Thu, 26 Jul 2012 11:52:32 +0200 Subject: [PATCH 5/6] Minor coding style fixes --- bar.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bar.c b/bar.c index 5b5bf11..152db52 100644 --- a/bar.c +++ b/bar.c @@ -43,7 +43,7 @@ static xcb_gcontext_t draw_gc; static xcb_gcontext_t clear_gc; static xcb_gcontext_t underl_gc; static int bar_width; -static bool bar_bottom; +static int bar_bottom; static fontset_item_t fontset[FONT_MAX]; static fontset_item_t *sel_font = &fontset[FONT_MAIN]; @@ -398,6 +398,7 @@ main (int argc, char **argv) xcb_expose_event_t *expose_ev; int permanent = 0; + bar_bottom = 0; char ch; while ((ch = getopt (argc, argv, "phb")) != -1) { From 06d2db7c78a6a0b5854cd640fc74e7be6ea1904d Mon Sep 17 00:00:00 2001 From: Georg Reinke Date: Fri, 17 Aug 2012 13:32:48 +0200 Subject: [PATCH 6/6] Add default setting for bar_bottom in config.h --- bar.c | 2 +- config.def.h | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/bar.c b/bar.c index 152db52..c6d5677 100644 --- a/bar.c +++ b/bar.c @@ -43,7 +43,7 @@ static xcb_gcontext_t draw_gc; static xcb_gcontext_t clear_gc; static xcb_gcontext_t underl_gc; static int bar_width; -static int bar_bottom; +static int bar_bottom = BAR_BOTTOM; static fontset_item_t fontset[FONT_MAX]; static fontset_item_t *sel_font = &fontset[FONT_MAIN]; diff --git a/config.def.h b/config.def.h index 56f1177..422ffae 100644 --- a/config.def.h +++ b/config.def.h @@ -2,6 +2,8 @@ #define BAR_HEIGHT 18 /* The thickness of the underline (in pixels) */ #define BAR_UNDERLINE_HEIGHT 2 +/* Default bar position, overwritten by '-b' switch */ +#define BAR_BOTTOM 0 /* The font used for the bar */ #define BAR_MAIN_FONT "-*-terminus-medium-r-normal-*-12-*-*-*-c-*-*-1" #define BAR_FALLBACK_FONT "fixed"