2007-10-03 17:26:14 +02:00
|
|
|
/*
|
2007-10-17 10:53:32 +02:00
|
|
|
* statusbar.c - statusbar functions
|
2007-10-03 17:26:14 +02:00
|
|
|
*
|
2008-03-15 09:10:32 +01:00
|
|
|
* Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
|
2007-10-03 17:26:14 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
2007-09-15 15:16:53 +02:00
|
|
|
*/
|
|
|
|
|
2007-09-24 17:08:47 +02:00
|
|
|
#include <stdio.h>
|
2007-11-11 18:59:11 +01:00
|
|
|
#include <math.h>
|
2008-03-21 16:50:17 +01:00
|
|
|
#include <xcb/xcb.h>
|
|
|
|
#include <xcb/xcb_aux.h>
|
2007-09-24 17:08:47 +02:00
|
|
|
|
2007-09-15 15:16:53 +02:00
|
|
|
#include "statusbar.h"
|
2007-09-15 15:26:51 +02:00
|
|
|
#include "screen.h"
|
2007-12-14 14:29:32 +01:00
|
|
|
#include "tag.h"
|
2007-12-15 07:53:53 +01:00
|
|
|
#include "widget.h"
|
2008-01-23 15:54:30 +01:00
|
|
|
#include "window.h"
|
2007-12-15 03:42:15 +01:00
|
|
|
|
2007-12-19 04:46:44 +01:00
|
|
|
extern AwesomeConf globalconf;
|
2007-12-16 02:45:38 +01:00
|
|
|
|
2008-01-12 21:50:14 +01:00
|
|
|
static void
|
2008-04-11 11:27:36 +02:00
|
|
|
statusbar_draw(statusbar_t *statusbar)
|
2007-12-15 03:42:15 +01:00
|
|
|
{
|
2008-05-20 15:39:47 +02:00
|
|
|
widget_node_t *w;
|
2007-12-15 07:53:53 +01:00
|
|
|
int left = 0, right = 0;
|
2008-03-14 09:37:25 +01:00
|
|
|
area_t rectangle = { 0, 0, 0, 0, NULL, NULL };
|
2008-01-07 18:12:38 +01:00
|
|
|
|
2008-05-13 16:48:33 +02:00
|
|
|
statusbar->need_update = false;
|
|
|
|
|
2008-04-09 19:44:27 +02:00
|
|
|
if(!statusbar->position)
|
|
|
|
return;
|
|
|
|
|
2008-01-12 23:38:31 +01:00
|
|
|
rectangle.width = statusbar->width;
|
|
|
|
rectangle.height = statusbar->height;
|
2008-03-21 16:50:17 +01:00
|
|
|
draw_rectangle(statusbar->ctx, rectangle, 1.0, true,
|
2008-05-20 15:39:47 +02:00
|
|
|
statusbar->colors.bg);
|
2007-12-30 21:00:34 +01:00
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
for(w = statusbar->widgets; w; w = w->next)
|
|
|
|
if(w->widget->align == AlignLeft)
|
|
|
|
left += w->widget->draw(w, statusbar, left, (left + right));
|
2007-12-22 16:49:21 +01:00
|
|
|
|
|
|
|
/* renders right widget from last to first */
|
2008-05-20 15:39:47 +02:00
|
|
|
for(w = *widget_node_list_last(&statusbar->widgets); w; w = w->prev)
|
|
|
|
if(w->widget->align == AlignRight)
|
|
|
|
right += w->widget->draw(w, statusbar, right, (left + right));
|
2007-12-15 07:53:53 +01:00
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
for(w = statusbar->widgets; w; w = w->next)
|
|
|
|
if(w->widget->align == AlignFlex)
|
|
|
|
left += w->widget->draw(w, statusbar, left, (left + right));
|
2007-12-15 03:42:15 +01:00
|
|
|
|
2008-01-23 17:44:07 +01:00
|
|
|
switch(statusbar->position)
|
2007-12-30 12:33:57 +01:00
|
|
|
{
|
2008-01-23 17:44:07 +01:00
|
|
|
case Right:
|
2008-03-20 09:17:34 +01:00
|
|
|
draw_rotate(statusbar->ctx, statusbar->sw->drawable,
|
|
|
|
statusbar->ctx->height, statusbar->ctx->width,
|
|
|
|
M_PI_2, statusbar->height, 0);
|
2008-01-23 17:44:07 +01:00
|
|
|
break;
|
|
|
|
case Left:
|
2008-03-20 09:17:34 +01:00
|
|
|
draw_rotate(statusbar->ctx, statusbar->sw->drawable,
|
|
|
|
statusbar->ctx->height, statusbar->ctx->width,
|
|
|
|
- M_PI_2, 0, statusbar->width);
|
2008-01-23 17:44:07 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2007-12-30 12:33:57 +01:00
|
|
|
}
|
|
|
|
|
2008-04-09 19:44:27 +02:00
|
|
|
simplewindow_refresh_drawable(statusbar->sw);
|
2007-09-15 15:16:53 +02:00
|
|
|
}
|
2007-09-15 15:26:51 +02:00
|
|
|
|
2008-05-20 20:02:08 +02:00
|
|
|
static void
|
2008-05-20 15:39:47 +02:00
|
|
|
statusbar_position_update(statusbar_t *statusbar, position_t position)
|
2008-01-16 17:47:12 +01:00
|
|
|
{
|
2008-04-11 11:27:36 +02:00
|
|
|
statusbar_t *sb;
|
2008-05-20 15:39:47 +02:00
|
|
|
area_t area;
|
2008-03-21 16:50:17 +01:00
|
|
|
xcb_drawable_t dw;
|
|
|
|
xcb_screen_t *s = NULL;
|
2008-05-20 15:39:47 +02:00
|
|
|
bool ignore = false;
|
|
|
|
|
|
|
|
simplewindow_delete(&statusbar->sw);
|
|
|
|
draw_context_delete(&statusbar->ctx);
|
|
|
|
|
|
|
|
if((statusbar->position = position) == Off)
|
|
|
|
return;
|
2008-01-16 17:47:12 +01:00
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
area = screen_get_area(statusbar->screen,
|
|
|
|
NULL,
|
|
|
|
&globalconf.screens[statusbar->screen].padding);
|
2008-02-13 06:53:08 +01:00
|
|
|
|
2008-04-11 11:27:36 +02:00
|
|
|
/* Top and Bottom statusbar_t have prio */
|
2008-01-16 17:47:12 +01:00
|
|
|
for(sb = globalconf.screens[statusbar->screen].statusbar; sb; sb = sb->next)
|
2008-05-20 15:39:47 +02:00
|
|
|
{
|
|
|
|
/* Ignore every statusbar after me that is in the same position */
|
|
|
|
if(statusbar == sb)
|
|
|
|
{
|
|
|
|
ignore = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if(ignore && statusbar->position == sb->position)
|
|
|
|
continue;
|
2008-01-16 17:47:12 +01:00
|
|
|
switch(sb->position)
|
|
|
|
{
|
|
|
|
case Left:
|
2008-05-20 15:39:47 +02:00
|
|
|
switch(statusbar->position)
|
|
|
|
{
|
|
|
|
case Left:
|
|
|
|
area.x += statusbar->height;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2008-01-16 17:47:12 +01:00
|
|
|
case Right:
|
2008-05-20 15:39:47 +02:00
|
|
|
switch(statusbar->position)
|
|
|
|
{
|
|
|
|
case Right:
|
|
|
|
area.x -= statusbar->height;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2008-01-16 17:47:12 +01:00
|
|
|
break;
|
2008-05-20 15:39:47 +02:00
|
|
|
case Top:
|
|
|
|
switch(statusbar->position)
|
|
|
|
{
|
|
|
|
case Top:
|
|
|
|
area.y += sb->height;
|
|
|
|
break;
|
|
|
|
case Left:
|
|
|
|
case Right:
|
|
|
|
area.height -= sb->height;
|
|
|
|
area.y += sb->height;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2008-01-16 17:47:12 +01:00
|
|
|
break;
|
2008-05-20 15:39:47 +02:00
|
|
|
case Bottom:
|
|
|
|
switch(statusbar->position)
|
|
|
|
{
|
|
|
|
case Bottom:
|
|
|
|
area.y -= sb->height;
|
|
|
|
break;
|
|
|
|
case Left:
|
|
|
|
case Right:
|
|
|
|
area.height -= sb->height;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2008-01-24 10:37:16 +01:00
|
|
|
break;
|
2008-05-20 15:39:47 +02:00
|
|
|
default:
|
2008-01-24 10:37:16 +01:00
|
|
|
break;
|
2008-05-20 15:39:47 +02:00
|
|
|
}
|
2008-01-24 10:37:16 +01:00
|
|
|
}
|
2007-12-15 07:53:53 +01:00
|
|
|
|
2008-03-20 09:08:15 +01:00
|
|
|
switch(statusbar->position)
|
|
|
|
{
|
|
|
|
case Right:
|
|
|
|
case Left:
|
2008-05-20 15:39:47 +02:00
|
|
|
statusbar->width = area.height;
|
|
|
|
statusbar->sw =
|
|
|
|
simplewindow_new(globalconf.connection, statusbar->phys_screen, 0, 0,
|
|
|
|
statusbar->height, statusbar->width, 0);
|
|
|
|
s = xcb_aux_get_screen(globalconf.connection, statusbar->phys_screen);
|
2008-03-20 09:08:15 +01:00
|
|
|
/* we need a new pixmap this way [ ] to render */
|
2008-03-21 16:50:17 +01:00
|
|
|
dw = xcb_generate_id(globalconf.connection);
|
|
|
|
xcb_create_pixmap(globalconf.connection,
|
2008-03-27 16:05:37 +01:00
|
|
|
s->root_depth, dw, s->root,
|
2008-03-21 16:50:17 +01:00
|
|
|
statusbar->width, statusbar->height);
|
|
|
|
statusbar->ctx = draw_context_new(globalconf.connection,
|
2008-05-20 15:39:47 +02:00
|
|
|
statusbar->phys_screen,
|
2008-03-20 09:08:15 +01:00
|
|
|
statusbar->width,
|
|
|
|
statusbar->height,
|
|
|
|
dw);
|
|
|
|
break;
|
|
|
|
default:
|
2008-05-20 15:39:47 +02:00
|
|
|
statusbar->width = area.width;
|
|
|
|
statusbar->sw =
|
|
|
|
simplewindow_new(globalconf.connection, statusbar->phys_screen, 0, 0,
|
|
|
|
statusbar->width, statusbar->height, 0);
|
2008-03-21 16:50:17 +01:00
|
|
|
statusbar->ctx = draw_context_new(globalconf.connection,
|
2008-05-20 15:39:47 +02:00
|
|
|
statusbar->phys_screen,
|
2008-03-20 09:08:15 +01:00
|
|
|
statusbar->width,
|
|
|
|
statusbar->height,
|
|
|
|
statusbar->sw->drawable);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
switch(statusbar->position)
|
|
|
|
{
|
|
|
|
case Top:
|
|
|
|
simplewindow_move(statusbar->sw, area.x, area.y);
|
|
|
|
break;
|
|
|
|
case Bottom:
|
|
|
|
simplewindow_move(statusbar->sw, area.x, (area.y + area.height) - statusbar->height);
|
|
|
|
break;
|
|
|
|
case Left:
|
|
|
|
simplewindow_move(statusbar->sw, area.x,
|
|
|
|
(area.y + area.height) - statusbar->sw->geometry.height);
|
|
|
|
break;
|
|
|
|
case Right:
|
|
|
|
simplewindow_move(statusbar->sw, area.x + area.width - statusbar->height, area.y);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
globalconf.screens[statusbar->screen].need_arrange = true;
|
|
|
|
xcb_map_window(globalconf.connection, statusbar->sw->window);
|
2007-12-31 10:04:03 +01:00
|
|
|
statusbar_draw(statusbar);
|
2007-09-15 22:25:49 +02:00
|
|
|
}
|
2007-09-15 15:26:51 +02:00
|
|
|
|
2008-01-07 18:12:38 +01:00
|
|
|
void
|
|
|
|
statusbar_refresh()
|
|
|
|
{
|
|
|
|
int screen;
|
2008-04-11 11:27:36 +02:00
|
|
|
statusbar_t *statusbar;
|
2008-01-07 18:12:38 +01:00
|
|
|
|
2008-03-13 09:28:21 +01:00
|
|
|
for(screen = 0; screen < globalconf.screens_info->nscreen; screen++)
|
2008-01-07 18:12:38 +01:00
|
|
|
for(statusbar = globalconf.screens[screen].statusbar;
|
|
|
|
statusbar;
|
|
|
|
statusbar = statusbar->next)
|
2008-05-13 16:48:33 +02:00
|
|
|
if(statusbar->need_update)
|
|
|
|
statusbar_draw(statusbar);
|
2008-01-07 18:12:38 +01:00
|
|
|
}
|
|
|
|
|
2008-04-11 11:27:36 +02:00
|
|
|
statusbar_t *
|
2008-04-03 14:00:46 +02:00
|
|
|
statusbar_getbyname(int screen, const char *name)
|
2007-12-30 21:00:34 +01:00
|
|
|
{
|
2008-04-11 11:27:36 +02:00
|
|
|
statusbar_t *sb;
|
2007-12-30 21:00:34 +01:00
|
|
|
|
|
|
|
for(sb = globalconf.screens[screen].statusbar; sb; sb = sb->next)
|
|
|
|
if(!a_strcmp(sb->name, name))
|
|
|
|
return sb;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
static int
|
|
|
|
luaA_statusbar_eq(lua_State *L)
|
2007-09-15 15:34:28 +02:00
|
|
|
{
|
2008-05-20 15:39:47 +02:00
|
|
|
statusbar_t **t1 = luaL_checkudata(L, 1, "statusbar");
|
|
|
|
statusbar_t **t2 = luaL_checkudata(L, 2, "statusbar");
|
|
|
|
lua_pushboolean(L, (*t1 == *t2));
|
|
|
|
return 1;
|
2007-09-15 15:34:28 +02:00
|
|
|
}
|
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
static int
|
|
|
|
luaA_statusbar_position_set(lua_State *L)
|
2007-12-14 15:52:52 +01:00
|
|
|
{
|
2008-05-20 15:39:47 +02:00
|
|
|
statusbar_t *s, **sb = luaL_checkudata(L, 1, "statusbar");
|
|
|
|
const char *pos = luaL_checkstring(L, 2);
|
|
|
|
position_t position = position_get_from_str(pos);
|
|
|
|
|
|
|
|
if(position != (*sb)->position)
|
|
|
|
{
|
|
|
|
(*sb)->position = position;
|
|
|
|
for(s = globalconf.screens[(*sb)->screen].statusbar; s; s = s->next)
|
|
|
|
statusbar_position_update(s, s->position);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
luaA_statusbar_tostring(lua_State *L)
|
|
|
|
{
|
|
|
|
statusbar_t **p = luaL_checkudata(L, 1, "statusbar");
|
|
|
|
lua_pushfstring(L, "[statusbar udata(%p) name(%s)]", *p, (*p)->name);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
luaA_statusbar_widget_add(lua_State *L)
|
|
|
|
{
|
|
|
|
statusbar_t **sb = luaL_checkudata(L, 1, "statusbar");
|
|
|
|
widget_t **widget = luaL_checkudata(L, 2, "widget");
|
|
|
|
widget_node_t *w = p_new(widget_node_t, 1);
|
|
|
|
|
|
|
|
(*sb)->need_update = true;
|
|
|
|
w->widget = *widget;
|
|
|
|
widget_node_list_append(&(*sb)->widgets, w);
|
|
|
|
widget_ref(widget);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
luaA_statusbar_add(lua_State *L)
|
|
|
|
{
|
|
|
|
statusbar_t *s, **sb = luaL_checkudata(L, 1, "statusbar");
|
|
|
|
int screen = luaL_checknumber(L, 2) - 1;
|
|
|
|
|
|
|
|
luaA_checkscreen(screen);
|
|
|
|
|
|
|
|
(*sb)->screen = screen;
|
|
|
|
|
|
|
|
/* \todo check for uniq name */
|
|
|
|
statusbar_list_append(&globalconf.screens[screen].statusbar, *sb);
|
|
|
|
for(s = globalconf.screens[(*sb)->screen].statusbar; s; s = s->next)
|
|
|
|
statusbar_position_update(s, s->position);
|
|
|
|
statusbar_ref(sb);
|
|
|
|
(*sb)->screen = screen;
|
|
|
|
(*sb)->phys_screen = screen_virttophys(screen);
|
2007-12-30 21:00:34 +01:00
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
luaA_statusbar_new(lua_State *L)
|
|
|
|
{
|
|
|
|
statusbar_t **sb = lua_newuserdata(L, sizeof(statusbar_t *));
|
|
|
|
int objpos = lua_gettop(L);
|
|
|
|
const char *color;
|
|
|
|
|
|
|
|
luaA_checktable(L, 1);
|
|
|
|
|
|
|
|
*sb = p_new(statusbar_t, 1);
|
|
|
|
|
|
|
|
/* \todo check that the name is unique */
|
|
|
|
(*sb)->name = luaA_name_init(L);
|
|
|
|
|
|
|
|
lua_getfield(L, 1, "fg");
|
|
|
|
if((color = luaL_optstring(L, -1, NULL)))
|
|
|
|
draw_color_new(globalconf.connection, globalconf.default_screen,
|
|
|
|
color, &(*sb)->colors.fg);
|
2007-12-30 21:00:34 +01:00
|
|
|
else
|
2008-05-20 15:39:47 +02:00
|
|
|
(*sb)->colors.fg = globalconf.colors.fg;
|
2008-03-19 16:32:37 +01:00
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
lua_getfield(L, 1, "bg");
|
|
|
|
if((color = luaL_optstring(L, -1, NULL)))
|
|
|
|
draw_color_new(globalconf.connection, globalconf.default_screen,
|
|
|
|
color, &(*sb)->colors.bg);
|
|
|
|
else
|
|
|
|
(*sb)->colors.bg = globalconf.colors.bg;
|
|
|
|
|
|
|
|
(*sb)->width = luaA_getopt_number(L, 1, "width", 0);
|
|
|
|
(*sb)->height = luaA_getopt_number(L, 1, "height", 0);
|
|
|
|
if((*sb)->height <= 0)
|
|
|
|
/* 1.5 as default factor, it fits nice but no one knows why */
|
|
|
|
(*sb)->height = 1.5 * globalconf.font->height;
|
|
|
|
|
|
|
|
(*sb)->position = position_get_from_str(luaA_getopt_string(L, 1, "position", "top"));
|
|
|
|
|
|
|
|
statusbar_ref(sb);
|
|
|
|
|
|
|
|
lua_pushvalue(L, objpos);
|
|
|
|
return luaA_settype(L, "statusbar");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
luaA_statusbar_gc(lua_State *L)
|
|
|
|
{
|
|
|
|
statusbar_t **sb = luaL_checkudata(L, 1, "statusbar");
|
|
|
|
statusbar_unref(sb);
|
|
|
|
return 0;
|
2007-12-14 15:52:52 +01:00
|
|
|
}
|
2007-10-12 17:10:36 +02:00
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
const struct luaL_reg awesome_statusbar_methods[] =
|
|
|
|
{
|
|
|
|
{ "new", luaA_statusbar_new },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
const struct luaL_reg awesome_statusbar_meta[] =
|
|
|
|
{
|
|
|
|
{ "widget_add", luaA_statusbar_widget_add },
|
|
|
|
{ "position_set", luaA_statusbar_position_set },
|
|
|
|
{ "add", luaA_statusbar_add },
|
|
|
|
{ "__gc", luaA_statusbar_gc },
|
|
|
|
{ "__eq", luaA_statusbar_eq },
|
|
|
|
{ "__tostring", luaA_statusbar_tostring },
|
|
|
|
{ NULL, NULL },
|
|
|
|
};
|
|
|
|
|
2007-12-18 09:24:15 +01:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|