Merge pull request #7 from guelfey/master

Some small improvements (with correct cleanup)
This commit is contained in:
Giuseppe 2012-08-18 02:56:46 -07:00
commit f6801f83e3
5 changed files with 82 additions and 45 deletions

18
LICENSE
View File

@ -1,7 +1,19 @@
Copyright (C) 2012 The Lemon Man 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.

View File

@ -1,6 +1,6 @@
CC ?= gcc CC ?= gcc
STRIP ?= strip STRIP ?= strip
CFLAGS ?= --std=c99 -fshort-wchar -Os CFLAGS ?= -std=c99 -fshort-wchar -Os
LDFLAGS += -lxcb LDFLAGS += -lxcb
CFDEBUG = -g3 -pedantic -Wall -Wunused-parameter -Wlong-long\ CFDEBUG = -g3 -pedantic -Wall -Wunused-parameter -Wlong-long\
-Wsign-conversion -Wconversion -Wimplicit-function-declaration -Wsign-conversion -Wconversion -Wimplicit-function-declaration

View File

@ -11,8 +11,9 @@ Options
bar accepts a couple of command line switches. 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. -p Make the bar permanent.
-b Show the bar at the bottom of the screen.
``` ```
Configuration Configuration

102
bar.c
View File

@ -1,3 +1,4 @@
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -29,6 +30,12 @@ enum {
FONT_MAX FONT_MAX
}; };
enum {
ALIGN_L = 0,
ALIGN_C,
ALIGN_R
};
static xcb_connection_t *c; static xcb_connection_t *c;
static xcb_window_t win; static xcb_window_t win;
static xcb_drawable_t canvas; static xcb_drawable_t canvas;
@ -36,6 +43,7 @@ static xcb_gcontext_t draw_gc;
static xcb_gcontext_t clear_gc; static xcb_gcontext_t clear_gc;
static xcb_gcontext_t underl_gc; static xcb_gcontext_t underl_gc;
static int bar_width; static int bar_width;
static int bar_bottom = BAR_BOTTOM;
static fontset_item_t fontset[FONT_MAX]; static fontset_item_t fontset[FONT_MAX];
static fontset_item_t *sel_font = &fontset[FONT_MAIN]; static fontset_item_t *sel_font = &fontset[FONT_MAIN];
@ -51,13 +59,13 @@ xcb_set_bg (int i)
void void
xcb_set_fg (int i) 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 void
xcb_set_ud (int i) 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 void
@ -70,7 +78,7 @@ void
xcb_set_fontset (int i) xcb_set_fontset (int i)
{ {
sel_font = &fontset[i&1]; 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 int
@ -87,12 +95,12 @@ draw_char (int x, int align, wchar_t ch)
return 0; return 0;
switch (align) { switch (align) {
case 1: case ALIGN_C:
xcb_copy_area (c, canvas, canvas, draw_gc, bar_width / 2 - x / 2, 0, xcb_copy_area (c, canvas, canvas, draw_gc, bar_width / 2 - x / 2, 0,
bar_width / 2 - (x + ch_width) / 2, 0, x, BAR_HEIGHT); bar_width / 2 - (x + ch_width) / 2, 0, x, BAR_HEIGHT);
x = bar_width / 2 - (x + ch_width) / 2 + x; x = bar_width / 2 - (x + ch_width) / 2 + x;
break; break;
case 2: case ALIGN_R:
xcb_copy_area (c, canvas, canvas, draw_gc, bar_width - x, 0, xcb_copy_area (c, canvas, canvas, draw_gc, bar_width - x, 0,
bar_width - x - ch_width, 0, x, BAR_HEIGHT); bar_width - x - ch_width, 0, x, BAR_HEIGHT);
x = bar_width - ch_width; x = bar_width - ch_width;
@ -148,15 +156,15 @@ parse (char *text)
break; break;
case 'l': case 'l':
align = 0; align = ALIGN_L;
pos_x = 0; pos_x = 0;
break; break;
case 'c': case 'c':
align = 1; align = ALIGN_C;
pos_x = 0; pos_x = 0;
break; break;
case 'r': case 'r':
align = 2; align = ALIGN_R;
pos_x = 0; pos_x = 0;
break; break;
} }
@ -238,6 +246,7 @@ set_ewmh_atoms (xcb_window_t root)
xcb_get_property_reply_t *reply1; xcb_get_property_reply_t *reply1;
xcb_atom_t atoms[5]; xcb_atom_t atoms[5];
int compliance_lvl; int compliance_lvl;
uint32_t v[12] = {0};
cookies[0] = xcb_intern_atom (c, 0, strlen ("_NET_WM_WINDOW_TYPE") , "_NET_WM_WINDOW_TYPE"); 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"); cookies[1] = xcb_intern_atom (c, 0, strlen ("_NET_WM_WINDOW_TYPE_DOCK"), "_NET_WM_WINDOW_TYPE_DOCK");
@ -274,17 +283,20 @@ set_ewmh_atoms (xcb_window_t root)
/* Show on every desktop */ /* Show on every desktop */
if (*a == atoms[2]) { if (*a == atoms[2]) {
xcb_change_property (c, XCB_PROP_MODE_REPLACE, win, atoms[2], XCB_ATOM_CARDINAL, 32, 1, 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++; compliance_lvl++;
} }
/* Tell the WM that this space is for the bar */ /* Tell the WM that this space is for the bar */
if (*a == atoms[3]) { if (*a == atoms[3]) {
xcb_change_property (c, XCB_PROP_MODE_REPLACE, win, atoms[3], XCB_ATOM_CARDINAL, 32, 12, if (bar_bottom) {
#if (BAR_BOTTOM == 1) v[3] = BAR_HEIGHT;
(const unsigned []) { 0, 0, 0, BAR_HEIGHT, 0, 0, 0, 0, 0, 0, 0, bar_width } ); v[11] = bar_width;
#else }
(const unsigned []) { 0, 0, BAR_HEIGHT, 0, 0, 0, 0, 0, bar_width, 0, 0, 0 } ); else {
#endif 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++; compliance_lvl++;
} }
} }
@ -299,6 +311,7 @@ init (void)
{ {
xcb_screen_t *scr; xcb_screen_t *scr;
xcb_window_t root; xcb_window_t root;
int y;
/* Connect to X */ /* Connect to X */
c = xcb_connect (NULL, NULL); c = xcb_connect (NULL, NULL);
@ -318,20 +331,19 @@ init (void)
/* Create the main window */ /* Create the main window */
win = xcb_generate_id (c); win = xcb_generate_id (c);
xcb_create_window (c, XCB_COPY_FROM_PARENT, win, root, 0, if (bar_bottom)
#if (BAR_BOTTOM == 1) y = scr->height_in_pixels - BAR_HEIGHT;
scr->height_in_pixels - BAR_HEIGHT, else
#else y = 0;
0, xcb_create_window (c, XCB_COPY_FROM_PARENT, win, root, 0, y,
#endif
bar_width, BAR_HEIGHT, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, scr->root_visual, 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 */ /* Set EWMH hints */
int ewmh_docking = set_ewmh_atoms (root); int ewmh_docking = set_ewmh_atoms (root);
/* Quirk for wm not supporting the EWMH docking method */ /* 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 */ /* Create a temporary canvas */
canvas = xcb_generate_id (c); canvas = xcb_generate_id (c);
@ -339,13 +351,13 @@ init (void)
/* Create the gc for drawing */ /* Create the gc for drawing */
draw_gc = xcb_generate_id (c); 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); 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); 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 */ /* Make the bar visible */
xcb_map_window (c, win); xcb_map_window (c, win);
@ -355,16 +367,25 @@ init (void)
void void
cleanup (void) cleanup (void)
{ {
for (int i = 0; i < FONT_MAX; i++) { int i;
free (fontset[i].info); for (i = 0; i < FONT_MAX; i++) {
xcb_close_font (c, fontset[i].xcb_ft); if (fontset[i].info)
free (fontset[i].info);
if (fontset[i].xcb_ft)
xcb_close_font (c, fontset[i].xcb_ft);
} }
xcb_free_pixmap (c, canvas); if (canvas)
xcb_destroy_window (c, win); xcb_free_pixmap (c, canvas);
xcb_free_gc (c, draw_gc); if (win)
xcb_free_gc (c, clear_gc); xcb_destroy_window (c, win);
xcb_free_gc (c, underl_gc); if (draw_gc)
xcb_disconnect (c); 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 void
@ -387,16 +408,19 @@ main (int argc, char **argv)
xcb_expose_event_t *expose_ev; xcb_expose_event_t *expose_ev;
int permanent = 0; int permanent = 0;
bar_bottom = 0;
char ch; char ch;
while ((ch = getopt (argc, argv, "ph")) != -1) { while ((ch = getopt (argc, argv, "phb")) != -1) {
switch (ch) { switch (ch) {
case 'h': case 'h':
printf ("usage: %s [-p | -h]\n" printf ("usage: %s [-p | -h] [-b]\n"
"\t-h Shows this help\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]); "\t-p Don't close after the data ends\n", argv[0]);
exit (0); exit (0);
case 'p': permanent = 1; break; case 'p': permanent = 1; break;
case 'b': bar_bottom = 1; break;
} }
} }
@ -413,7 +437,7 @@ main (int argc, char **argv)
for (;;) { for (;;) {
int redraw = 0; 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 (pollin[0].revents & POLLHUP) { /* No more data... */
if (permanent) pollin[0].fd = -1; /* ...null the fd and continue polling :D */ if (permanent) pollin[0].fd = -1; /* ...null the fd and continue polling :D */
else break; /* ...bail out */ else break; /* ...bail out */

View File

@ -4,7 +4,7 @@
#define BAR_UNDERLINE 1 #define BAR_UNDERLINE 1
/* The thickness of the underline (in pixels). Set to 0 to disable. */ /* The thickness of the underline (in pixels). Set to 0 to disable. */
#define BAR_UNDERLINE_HEIGHT 2 #define BAR_UNDERLINE_HEIGHT 2
/* Whether to put the bar at the screen bottom or not */ /* Default bar position, overwritten by '-b' switch */
#define BAR_BOTTOM 0 #define BAR_BOTTOM 0
/* The fonts used for the bar, comma separated. Only the first 2 will be used. */ /* The fonts used for the bar, comma separated. Only the first 2 will be used. */
#define BAR_FONT "-*-terminus-medium-r-normal-*-12-*-*-*-c-*-*-1","fixed" #define BAR_FONT "-*-terminus-medium-r-normal-*-12-*-*-*-c-*-*-1","fixed"