2008-01-26 17:58:01 +01:00
|
|
|
/*
|
|
|
|
* swindow.c - simple window handling functions
|
|
|
|
*
|
|
|
|
* Copyright © 2008 Julien Danjou <julien@danjou.info>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-09-20 19:31:38 +02:00
|
|
|
#include <math.h>
|
|
|
|
|
2008-03-21 16:50:17 +01:00
|
|
|
#include <xcb/xcb.h>
|
2008-01-26 17:58:01 +01:00
|
|
|
|
2008-09-11 14:11:13 +02:00
|
|
|
#include "structs.h"
|
|
|
|
#include "swindow.h"
|
2008-09-20 19:31:38 +02:00
|
|
|
#include "draw.h"
|
2008-06-17 16:55:24 +02:00
|
|
|
#include "common/xutil.h"
|
2008-01-26 17:58:01 +01:00
|
|
|
|
2008-09-24 12:13:21 +02:00
|
|
|
static void
|
|
|
|
simplewindow_draw_context_update(simple_window_t *sw, xcb_screen_t *s)
|
|
|
|
{
|
|
|
|
xcolor_t fg = sw->ctx.fg, bg = sw->ctx.bg;
|
|
|
|
int phys_screen = sw->ctx.phys_screen;
|
|
|
|
|
|
|
|
draw_context_wipe(&sw->ctx);
|
|
|
|
|
|
|
|
/* update draw context */
|
|
|
|
switch(sw->orientation)
|
|
|
|
{
|
|
|
|
case South:
|
|
|
|
case North:
|
|
|
|
/* we need a new pixmap this way [ ] to render */
|
|
|
|
sw->ctx.pixmap = xcb_generate_id(globalconf.connection);
|
|
|
|
xcb_create_pixmap(globalconf.connection,
|
|
|
|
s->root_depth,
|
|
|
|
sw->ctx.pixmap, s->root,
|
2008-12-12 00:01:43 +01:00
|
|
|
sw->geometry.height, sw->geometries.internal.width);
|
2008-09-24 12:13:21 +02:00
|
|
|
draw_context_init(&sw->ctx, phys_screen,
|
2008-12-12 00:01:43 +01:00
|
|
|
sw->geometry.height, sw->geometries.internal.width,
|
2008-09-24 12:13:21 +02:00
|
|
|
sw->ctx.pixmap, &fg, &bg);
|
|
|
|
break;
|
|
|
|
case East:
|
|
|
|
draw_context_init(&sw->ctx, phys_screen,
|
2008-12-12 00:01:43 +01:00
|
|
|
sw->geometries.internal.width, sw->geometries.internal.height,
|
2008-09-24 12:13:21 +02:00
|
|
|
sw->pixmap, &fg, &bg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-20 21:24:16 +02:00
|
|
|
/** Initialize a simple window.
|
|
|
|
* \param sw The simple window to initialize.
|
2008-05-03 15:17:46 +02:00
|
|
|
* \param phys_screen Physical screen number.
|
2008-09-24 12:13:21 +02:00
|
|
|
* \param geometry Window geometry.
|
2008-05-03 15:17:46 +02:00
|
|
|
* \param border_width Window border width.
|
2008-09-24 12:13:21 +02:00
|
|
|
* \param orientation The rendering orientation.
|
2008-09-20 19:31:38 +02:00
|
|
|
* \param bg Default foreground color.
|
|
|
|
* \param bg Default background color.
|
2008-03-01 11:25:04 +01:00
|
|
|
*/
|
2008-09-20 21:24:16 +02:00
|
|
|
void
|
|
|
|
simplewindow_init(simple_window_t *sw,
|
|
|
|
int phys_screen,
|
2008-09-24 12:13:21 +02:00
|
|
|
area_t geometry,
|
2008-09-21 10:21:02 +02:00
|
|
|
uint16_t border_width,
|
2008-09-24 12:13:21 +02:00
|
|
|
orientation_t orientation,
|
2008-09-20 21:24:16 +02:00
|
|
|
const xcolor_t *fg, const xcolor_t *bg)
|
2008-01-26 17:58:01 +01:00
|
|
|
{
|
2008-09-11 14:11:13 +02:00
|
|
|
xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
|
2008-03-21 16:50:17 +01:00
|
|
|
uint32_t create_win_val[3];
|
2008-04-09 19:40:32 +02:00
|
|
|
const uint32_t gc_mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND;
|
|
|
|
const uint32_t gc_values[2] = { s->black_pixel, s->white_pixel };
|
2008-01-26 17:58:01 +01:00
|
|
|
|
2008-09-24 12:13:21 +02:00
|
|
|
sw->geometry.x = geometry.x;
|
|
|
|
sw->geometry.y = geometry.y;
|
|
|
|
sw->geometry.width = geometry.width;
|
|
|
|
sw->geometry.height = geometry.height;
|
|
|
|
sw->border.width = border_width;
|
2008-12-12 00:01:43 +01:00
|
|
|
|
|
|
|
/* The real protocol window. */
|
|
|
|
sw->geometries.internal.x = geometry.x;
|
|
|
|
sw->geometries.internal.y = geometry.y;
|
2009-03-17 17:58:28 +01:00
|
|
|
sw->geometries.internal.width = geometry.width;
|
|
|
|
sw->geometries.internal.height = geometry.height;
|
2008-12-12 00:01:43 +01:00
|
|
|
|
2008-09-24 12:13:21 +02:00
|
|
|
sw->orientation = orientation;
|
|
|
|
sw->ctx.fg = *fg;
|
|
|
|
sw->ctx.bg = *bg;
|
2008-01-26 17:58:01 +01:00
|
|
|
|
2008-03-21 16:50:17 +01:00
|
|
|
create_win_val[0] = XCB_BACK_PIXMAP_PARENT_RELATIVE;
|
|
|
|
create_win_val[1] = 1;
|
2008-08-12 13:23:10 +02:00
|
|
|
create_win_val[2] = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
|
|
|
| XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_ENTER_WINDOW
|
|
|
|
| XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
|
|
|
| XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE
|
2008-08-25 14:34:56 +02:00
|
|
|
| XCB_EVENT_MASK_POINTER_MOTION | XCB_EVENT_MASK_EXPOSURE;
|
2008-03-21 16:50:17 +01:00
|
|
|
|
2008-09-11 14:11:13 +02:00
|
|
|
sw->window = xcb_generate_id(globalconf.connection);
|
2008-09-24 12:13:21 +02:00
|
|
|
xcb_create_window(globalconf.connection, s->root_depth, sw->window, s->root,
|
2008-12-12 00:01:43 +01:00
|
|
|
sw->geometries.internal.x, sw->geometries.internal.y, sw->geometries.internal.width, sw->geometries.internal.height,
|
2008-03-27 16:05:37 +01:00
|
|
|
border_width, XCB_COPY_FROM_PARENT, s->root_visual,
|
2008-03-21 16:50:17 +01:00
|
|
|
XCB_CW_BACK_PIXMAP | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK,
|
|
|
|
create_win_val);
|
|
|
|
|
2008-09-11 14:11:13 +02:00
|
|
|
sw->pixmap = xcb_generate_id(globalconf.connection);
|
2008-09-24 12:13:21 +02:00
|
|
|
xcb_create_pixmap(globalconf.connection, s->root_depth, sw->pixmap, s->root,
|
2008-12-12 00:01:43 +01:00
|
|
|
sw->geometries.internal.width, sw->geometries.internal.height);
|
2008-03-21 16:50:17 +01:00
|
|
|
|
2008-11-04 18:16:33 +01:00
|
|
|
sw->ctx.phys_screen = phys_screen;
|
2008-09-24 12:13:21 +02:00
|
|
|
simplewindow_draw_context_update(sw, s);
|
2008-09-20 19:31:38 +02:00
|
|
|
|
|
|
|
/* The default GC is just a newly created associated to the root window */
|
2008-09-11 14:11:13 +02:00
|
|
|
sw->gc = xcb_generate_id(globalconf.connection);
|
|
|
|
xcb_create_gc(globalconf.connection, sw->gc, s->root, gc_mask, gc_values);
|
2008-01-26 17:58:01 +01:00
|
|
|
}
|
|
|
|
|
2008-09-20 21:24:16 +02:00
|
|
|
/** Destroy all resources of a simple window.
|
|
|
|
* \param sw The simple_window_t to wipe.
|
2008-09-11 14:11:13 +02:00
|
|
|
*/
|
|
|
|
void
|
2008-09-20 21:24:16 +02:00
|
|
|
simplewindow_wipe(simple_window_t *sw)
|
2008-09-11 14:11:13 +02:00
|
|
|
{
|
2008-09-24 12:13:21 +02:00
|
|
|
if(sw->window)
|
|
|
|
{
|
|
|
|
xcb_destroy_window(globalconf.connection, sw->window);
|
|
|
|
sw->window = XCB_NONE;
|
|
|
|
}
|
|
|
|
if(sw->pixmap)
|
|
|
|
{
|
|
|
|
xcb_free_pixmap(globalconf.connection, sw->pixmap);
|
|
|
|
sw->pixmap = XCB_NONE;
|
|
|
|
}
|
|
|
|
if(sw->gc)
|
|
|
|
{
|
|
|
|
xcb_free_gc(globalconf.connection, sw->gc);
|
|
|
|
sw->gc = XCB_NONE;
|
|
|
|
}
|
2008-09-20 21:24:16 +02:00
|
|
|
draw_context_wipe(&sw->ctx);
|
2008-09-11 14:11:13 +02:00
|
|
|
}
|
|
|
|
|
2008-05-03 15:17:46 +02:00
|
|
|
/** Move a simple window.
|
|
|
|
* \param sw The simple window to move.
|
|
|
|
* \param x New x coordinate.
|
|
|
|
* \param y New y coordinate.
|
2008-03-01 11:25:04 +01:00
|
|
|
*/
|
2008-03-21 16:50:17 +01:00
|
|
|
void
|
2008-04-09 19:41:28 +02:00
|
|
|
simplewindow_move(simple_window_t *sw, int x, int y)
|
2008-01-26 17:58:01 +01:00
|
|
|
{
|
2008-03-21 16:50:17 +01:00
|
|
|
const uint32_t move_win_vals[] = { x, y };
|
|
|
|
|
2008-12-12 00:01:43 +01:00
|
|
|
if(x != sw->geometries.internal.x || y != sw->geometries.internal.y)
|
2008-09-20 19:31:38 +02:00
|
|
|
{
|
2008-12-12 00:01:43 +01:00
|
|
|
sw->geometry.x = sw->geometries.internal.x = x;
|
|
|
|
sw->geometry.y = sw->geometries.internal.y = y;
|
2008-09-24 12:13:21 +02:00
|
|
|
xcb_configure_window(globalconf.connection, sw->window,
|
|
|
|
XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
|
|
|
|
move_win_vals);
|
2008-09-20 19:31:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-03 15:17:46 +02:00
|
|
|
/** Resize a simple window.
|
|
|
|
* \param sw The simple_window_t to resize.
|
|
|
|
* \param w New width.
|
|
|
|
* \param h New height.
|
2008-03-14 12:54:35 +01:00
|
|
|
*/
|
2008-03-21 16:50:17 +01:00
|
|
|
void
|
2008-06-18 13:50:50 +02:00
|
|
|
simplewindow_resize(simple_window_t *sw, int w, int h)
|
2008-03-14 12:54:35 +01:00
|
|
|
{
|
2008-12-12 00:01:43 +01:00
|
|
|
int iw = w - 2 * sw->border.width;
|
|
|
|
int ih = h - 2 * sw->border.width;
|
|
|
|
|
|
|
|
if(iw > 0 && ih > 0 &&
|
|
|
|
(sw->geometries.internal.width != iw || sw->geometries.internal.height != ih))
|
2008-06-18 13:50:50 +02:00
|
|
|
{
|
2008-09-20 21:24:16 +02:00
|
|
|
xcb_screen_t *s = xutil_screen_get(globalconf.connection, sw->ctx.phys_screen);
|
2008-09-20 19:31:38 +02:00
|
|
|
uint32_t resize_win_vals[2];
|
|
|
|
|
2008-12-12 00:01:43 +01:00
|
|
|
sw->geometries.internal.width = resize_win_vals[0] = iw;
|
|
|
|
sw->geometries.internal.height = resize_win_vals[1] = ih;
|
|
|
|
sw->geometry.width = w;
|
|
|
|
sw->geometry.height = h;
|
2008-09-20 19:31:38 +02:00
|
|
|
xcb_free_pixmap(globalconf.connection, sw->pixmap);
|
2008-09-24 12:13:21 +02:00
|
|
|
/* orientation != East */
|
|
|
|
if(sw->pixmap != sw->ctx.pixmap)
|
|
|
|
xcb_free_pixmap(globalconf.connection, sw->ctx.pixmap);
|
2008-09-11 14:11:13 +02:00
|
|
|
sw->pixmap = xcb_generate_id(globalconf.connection);
|
2008-12-12 00:01:43 +01:00
|
|
|
xcb_create_pixmap(globalconf.connection, s->root_depth, sw->pixmap, s->root, iw, ih);
|
2008-09-11 14:11:13 +02:00
|
|
|
xcb_configure_window(globalconf.connection, sw->window,
|
2008-06-18 13:50:50 +02:00
|
|
|
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
|
|
|
|
resize_win_vals);
|
2008-09-20 19:31:38 +02:00
|
|
|
simplewindow_draw_context_update(sw, s);
|
2008-06-18 13:50:50 +02:00
|
|
|
}
|
2008-03-14 12:54:35 +01:00
|
|
|
}
|
|
|
|
|
2008-06-16 11:44:33 +02:00
|
|
|
/** Move and resize a window in one call.
|
|
|
|
* \param sw The simple window to move and resize.
|
2008-12-12 00:01:43 +01:00
|
|
|
* \param geom The new geometry.
|
2008-06-16 11:44:33 +02:00
|
|
|
*/
|
|
|
|
void
|
2008-09-24 12:13:21 +02:00
|
|
|
simplewindow_moveresize(simple_window_t *sw, area_t geom)
|
2008-06-16 11:44:33 +02:00
|
|
|
{
|
2008-06-18 13:50:50 +02:00
|
|
|
uint32_t moveresize_win_vals[4], mask_vals = 0;
|
2008-09-20 21:24:16 +02:00
|
|
|
xcb_screen_t *s = xutil_screen_get(globalconf.connection, sw->ctx.phys_screen);
|
2008-06-16 11:44:33 +02:00
|
|
|
|
2008-12-12 00:01:43 +01:00
|
|
|
area_t geom_internal = geom;
|
|
|
|
geom_internal.width -= 2 * sw->border.width;
|
|
|
|
geom_internal.height -= 2* sw->border.width;
|
|
|
|
|
|
|
|
if(sw->geometries.internal.x != geom_internal.x || sw->geometries.internal.y != geom_internal.y)
|
2008-06-18 13:50:50 +02:00
|
|
|
{
|
2008-12-12 00:01:43 +01:00
|
|
|
sw->geometries.internal.x = moveresize_win_vals[0] = geom_internal.x;
|
|
|
|
sw->geometries.internal.y = moveresize_win_vals[1] = geom_internal.y;
|
2008-06-18 13:50:50 +02:00
|
|
|
mask_vals |= XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
|
|
|
|
}
|
|
|
|
|
2008-09-24 12:13:21 +02:00
|
|
|
if(sw->geometry.width != geom.width || sw->geometry.height != geom.height)
|
2008-06-18 13:50:50 +02:00
|
|
|
{
|
|
|
|
if(mask_vals)
|
|
|
|
{
|
2008-12-12 00:01:43 +01:00
|
|
|
sw->geometries.internal.width = moveresize_win_vals[2] = geom_internal.width;
|
|
|
|
sw->geometries.internal.height = moveresize_win_vals[3] = geom_internal.height;
|
2008-06-18 13:50:50 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-12-12 00:01:43 +01:00
|
|
|
sw->geometries.internal.width = moveresize_win_vals[0] = geom_internal.width;
|
|
|
|
sw->geometries.internal.height = moveresize_win_vals[1] = geom_internal.height;
|
2008-06-18 13:50:50 +02:00
|
|
|
}
|
|
|
|
mask_vals |= XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
|
2008-09-20 19:31:38 +02:00
|
|
|
xcb_free_pixmap(globalconf.connection, sw->pixmap);
|
2008-09-24 12:13:21 +02:00
|
|
|
/* orientation != East */
|
|
|
|
if(sw->pixmap != sw->ctx.pixmap)
|
|
|
|
xcb_free_pixmap(globalconf.connection, sw->ctx.pixmap);
|
2008-09-11 14:11:13 +02:00
|
|
|
sw->pixmap = xcb_generate_id(globalconf.connection);
|
2008-12-12 00:01:43 +01:00
|
|
|
xcb_create_pixmap(globalconf.connection, s->root_depth, sw->pixmap, s->root, geom_internal.width, geom_internal.height);
|
2008-09-20 19:31:38 +02:00
|
|
|
simplewindow_draw_context_update(sw, s);
|
2008-06-18 13:50:50 +02:00
|
|
|
}
|
|
|
|
|
2008-12-12 00:01:43 +01:00
|
|
|
/* Also save geometry including border. */
|
|
|
|
sw->geometry = geom;
|
|
|
|
|
2008-09-11 14:11:13 +02:00
|
|
|
xcb_configure_window(globalconf.connection, sw->window, mask_vals, moveresize_win_vals);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Refresh the window content by copying its pixmap data to its window.
|
|
|
|
* \param sw The simple window to refresh.
|
|
|
|
*/
|
|
|
|
void
|
2008-09-20 23:15:37 +02:00
|
|
|
simplewindow_refresh_pixmap_partial(simple_window_t *sw,
|
|
|
|
int16_t x, int16_t y,
|
|
|
|
uint16_t w, uint16_t h)
|
2008-09-11 14:11:13 +02:00
|
|
|
{
|
|
|
|
xcb_copy_area(globalconf.connection, sw->pixmap,
|
2008-09-20 23:15:37 +02:00
|
|
|
sw->window, sw->gc, x, y, x, y,
|
|
|
|
w, h);
|
2008-09-11 14:11:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Set a simple window border width.
|
|
|
|
* \param sw The simple window to change border width.
|
|
|
|
* \param border_width The border width in pixel.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
simplewindow_border_width_set(simple_window_t *sw, uint32_t border_width)
|
|
|
|
{
|
|
|
|
xcb_configure_window(globalconf.connection, sw->window, XCB_CONFIG_WINDOW_BORDER_WIDTH,
|
|
|
|
&border_width);
|
2008-09-20 21:24:16 +02:00
|
|
|
sw->border.width = border_width;
|
2008-09-11 14:11:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Set a simple window border color.
|
|
|
|
* \param sw The simple window to change border width.
|
|
|
|
* \param color The border color.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
simplewindow_border_color_set(simple_window_t *sw, const xcolor_t *color)
|
|
|
|
{
|
|
|
|
xcb_change_window_attributes(globalconf.connection, sw->window,
|
|
|
|
XCB_CW_BORDER_PIXEL, &color->pixel);
|
2008-09-20 21:24:16 +02:00
|
|
|
sw->border.color = *color;
|
2008-06-16 11:44:33 +02:00
|
|
|
}
|
|
|
|
|
2008-09-24 12:13:21 +02:00
|
|
|
/** Set simple window orientation.
|
|
|
|
* \param sw The simple window.
|
|
|
|
* \param o The new orientation
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
simplewindow_orientation_set(simple_window_t *sw, orientation_t o)
|
|
|
|
{
|
|
|
|
if(o != sw->orientation)
|
|
|
|
{
|
|
|
|
xcb_screen_t *s = xutil_screen_get(globalconf.connection, sw->ctx.phys_screen);
|
|
|
|
sw->orientation = o;
|
|
|
|
/* orientation != East */
|
|
|
|
if(sw->pixmap != sw->ctx.pixmap)
|
|
|
|
xcb_free_pixmap(globalconf.connection, sw->ctx.pixmap);
|
|
|
|
simplewindow_draw_context_update(sw, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-13 11:02:57 +01:00
|
|
|
/** Set simple window cursor.
|
|
|
|
* \param sw The simple window.
|
|
|
|
* \param c The cursor.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
simplewindow_cursor_set(simple_window_t *sw, xcb_cursor_t c)
|
|
|
|
{
|
|
|
|
if(sw->window)
|
|
|
|
{
|
|
|
|
const uint32_t change_win_vals[] = { c };
|
|
|
|
xcb_change_window_attributes(globalconf.connection, sw->window, XCB_CW_CURSOR, change_win_vals);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-26 17:58:01 +01:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|