@ -12,7 +12,7 @@
*/
*/
// --------------
// --------------
// TextBar v0.1. 1
// TextBar v0.1. 2
// --------------
// --------------
// Usage: <text generator> | textbar | <input handler>
// Usage: <text generator> | textbar | <input handler>
//
//
@ -23,9 +23,6 @@
// Line buffered
// Line buffered
// Prints all mouse clicks to stdout
// Prints all mouse clicks to stdout
// In the format of `X Y` (without the '`')
// In the format of `X Y` (without the '`')
//
// When the X server is lagging, the bar will set the background color to 0,0,0,0 for a single frame in a given frequency. The more X is lagging, the more frequently it will do so.
// I swear this is an intented feature.
# include <pthread.h>
# include <pthread.h>
@ -48,7 +45,9 @@
Display * dpy ;
Display * dpy ;
Window win ;
Window win ;
cairo_surface_t * sfc ;
cairo_surface_t * sfc ;
cairo_surface_t * sfc2 ;
cairo_t * ctx ;
cairo_t * ctx ;
cairo_t * ctx2 ;
char text [ 256 ] = { 0 } ;
char text [ 256 ] = { 0 } ;
char text_other [ 256 ] = { 0 } ;
char text_other [ 256 ] = { 0 } ;
int width ;
int width ;
@ -74,8 +73,8 @@ void init() {
XMatchVisualInfo ( dpy , screen , 32 , TrueColor , & visualinfo ) ;
XMatchVisualInfo ( dpy , screen , 32 , TrueColor , & visualinfo ) ;
XSetWindowAttributes attr ;
XSetWindowAttributes attr ;
attr . colormap = XCreateColormap ( dpy , DefaultRootWindow ( dpy ) , visualinfo . visual , AllocNone ) ;
attr . colormap = XCreateColormap ( dpy , DefaultRootWindow ( dpy ) , visualinfo . visual , AllocNone ) ;
attr . background_pixel = 0 ;
attr . background_pixel = 0 ;
attr . border_pixel = 0 ;
attr . border_pixel = 0 ;
win = XCreateWindow ( dpy , DefaultRootWindow ( dpy ) ,
win = XCreateWindow ( dpy , DefaultRootWindow ( dpy ) ,
0 , 0 , width , HEIGHT ,
0 , 0 , width , HEIGHT ,
0 , visualinfo . depth , InputOutput , visualinfo . visual , CWBackPixel | CWColormap | CWBorderPixel , & attr ) ;
0 , visualinfo . depth , InputOutput , visualinfo . visual , CWBackPixel | CWColormap | CWBorderPixel , & attr ) ;
@ -102,35 +101,46 @@ void init() {
XMapWindow ( dpy , win ) ;
XMapWindow ( dpy , win ) ;
sfc = cairo_xlib_surface_create ( dpy , win , visualinfo . visual , DisplayWidth ( dpy , screen ) , HEIGHT ) ;
sfc = cairo_xlib_surface_create ( dpy , win , visualinfo . visual , DisplayWidth ( dpy , screen ) , HEIGHT ) ;
sfc2 = cairo_surface_create_similar ( sfc , CAIRO_CONTENT_COLOR_ALPHA , DisplayWidth ( dpy , screen ) , HEIGHT ) ;
ctx = cairo_create ( sfc ) ;
ctx = cairo_create ( sfc ) ;
ctx2 = cairo_create ( sfc2 ) ;
}
}
pthread_mutex_t do_paint ;
pthread_mutex_t do_paint ;
void paint ( Display * dpy ) {
void paint ( Display * dpy ) {
if ( ! pthread_mutex_lock ( & do_paint ) ) {
if ( ! pthread_mutex_lock ( & do_paint ) ) {
//cairo_set_source_rgba(ctx, 0.0, 0.0, 0.0, 0.0);
// Clear
//cairo_set_operator(ctx, CAIRO_OPERATOR_SOURCE);
cairo_set_source_rgba ( ctx2 , 0.0 , 0.0 , 0.0 , 0.0 ) ;
//cairo_paint(ctx);
cairo_set_operator ( ctx2 , CAIRO_OPERATOR_SOURCE ) ;
//TODO: fix alpha
cairo_paint ( ctx2 ) ;
//NOTE: comment the following line if you do not use transparency.
cairo_set_operator ( ctx2 , CAIRO_OPERATOR_OVER ) ;
XClearWindow ( dpy , win ) ;
cairo_push_group ( ctx ) ;
// Background
cairo_set_operator ( ctx , CAIRO_OPERATOR_OVER ) ;
cairo_set_source_rgba ( ctx2 , BG_COLOR ) ;
cairo_set_source_rgba ( ctx , BG_COLOR ) ;
cairo_paint ( ctx2 ) ;
cairo_paint ( ctx ) ;
cairo_move_to ( ctx , FONT_SIZE / 2 , FONT_POS ) ;
// Left text
cairo_select_font_face ( ctx , FONT , CAIRO_FONT_SLANT_NORMAL , CAIRO_FONT_WEIGHT_NORMAL ) ;
cairo_move_to ( ctx2 , FONT_SIZE / 2 , FONT_POS ) ;
cairo_set_font_size ( ctx , FONT_SIZE ) ;
cairo_select_font_face ( ctx2 , FONT , CAIRO_FONT_SLANT_NORMAL , CAIRO_FONT_WEIGHT_NORMAL ) ;
cairo_set_source_rgba ( ctx , FG_COLOR ) ;
cairo_set_font_size ( ctx2 , FONT_SIZE ) ;
cairo_show_text ( ctx , text ) ;
cairo_set_source_rgba ( ctx2 , FG_COLOR ) ;
cairo_show_text ( ctx2 , text ) ;
// Right text
cairo_text_extents_t extents ;
cairo_text_extents_t extents ;
cairo_text_extents ( ctx , text_other , & extents ) ;
cairo_text_extents ( ctx2 , text_other , & extents ) ;
cairo_move_to ( ctx , ( width - extents . width ) - FONT_SIZE / 2 , FONT_POS ) ;
cairo_move_to ( ctx2 , ( width - extents . width ) - FONT_SIZE / 2 , FONT_POS ) ;
cairo_show_text ( ctx , text_other ) ;
cairo_show_text ( ctx2 , text_other ) ;
cairo_pop_group_to_source ( ctx ) ;
// Backbuffer -> Frontbuffer
cairo_set_source_surface ( ctx , sfc2 , 0 , 0 ) ;
cairo_set_operator ( ctx , CAIRO_OPERATOR_SOURCE ) ;
cairo_paint ( ctx ) ;
cairo_paint ( ctx ) ;
// Flushing
cairo_surface_flush ( sfc ) ;
cairo_surface_flush ( sfc ) ;
XFlush ( dpy ) ;
XFlush ( dpy ) ;
pthread_mutex_unlock ( & do_paint ) ;
pthread_mutex_unlock ( & do_paint ) ;
}
}
}
}
@ -156,8 +166,7 @@ void x_loop() {
if ( e . type = = ButtonPress ) {
if ( e . type = = ButtonPress ) {
printf ( " %d %d \n " , e . xbutton . x , e . xbutton . y ) ;
printf ( " %d %d \n " , e . xbutton . x , e . xbutton . y ) ;
fflush ( stdout ) ;
fflush ( stdout ) ;
} else
} else if ( e . type = = ConfigureNotify ) {
if ( e . type = = ConfigureNotify ) {
cairo_xlib_surface_set_size ( sfc , e . xconfigure . width , e . xconfigure . height ) ;
cairo_xlib_surface_set_size ( sfc , e . xconfigure . width , e . xconfigure . height ) ;
} else
} else
if ( e . type = = Expose ) {
if ( e . type = = Expose ) {