2009-07-08 20:03:03 +02:00
|
|
|
/* $Id: screen.c,v 1.90 2009-07-08 18:03:03 nicm Exp $ */
|
2007-07-09 21:04:12 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2009-06-25 17:54:38 +02:00
|
|
|
#include <stdlib.h>
|
2007-07-09 21:04:12 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2008-09-25 22:08:57 +02:00
|
|
|
void screen_resize_x(struct screen *, u_int);
|
|
|
|
void screen_resize_y(struct screen *, u_int);
|
2007-07-09 21:04:12 +02:00
|
|
|
|
|
|
|
/* Create a new screen. */
|
|
|
|
void
|
2008-09-25 22:08:57 +02:00
|
|
|
screen_init(struct screen *s, u_int sx, u_int sy, u_int hlimit)
|
2007-07-09 21:04:12 +02:00
|
|
|
{
|
2008-09-25 22:08:57 +02:00
|
|
|
s->grid = grid_create(sx, sy, hlimit);
|
2007-07-09 21:04:12 +02:00
|
|
|
|
2007-11-30 12:08:35 +01:00
|
|
|
s->title = xstrdup("");
|
2007-07-09 21:04:12 +02:00
|
|
|
|
2009-06-25 17:54:38 +02:00
|
|
|
s->tabs = NULL;
|
|
|
|
|
2008-09-25 22:08:57 +02:00
|
|
|
screen_reinit(s);
|
2007-07-09 21:04:12 +02:00
|
|
|
}
|
|
|
|
|
2008-06-29 09:04:31 +02:00
|
|
|
/* Reinitialise screen. */
|
|
|
|
void
|
2008-09-10 21:15:06 +02:00
|
|
|
screen_reinit(struct screen *s)
|
2008-06-29 09:04:31 +02:00
|
|
|
{
|
|
|
|
s->cx = 0;
|
|
|
|
s->cy = 0;
|
|
|
|
|
|
|
|
s->rupper = 0;
|
2008-09-25 22:08:57 +02:00
|
|
|
s->rlower = screen_size_y(s) - 1;
|
2008-06-29 09:04:31 +02:00
|
|
|
|
2008-07-24 09:01:57 +02:00
|
|
|
s->mode = MODE_CURSOR;
|
2009-06-25 17:54:38 +02:00
|
|
|
|
|
|
|
screen_reset_tabs(s);
|
2008-06-29 09:04:31 +02:00
|
|
|
|
2009-07-08 20:02:31 +02:00
|
|
|
grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy);
|
2008-09-26 08:45:28 +02:00
|
|
|
|
|
|
|
screen_clear_selection(s);
|
2008-06-29 09:04:31 +02:00
|
|
|
}
|
|
|
|
|
2008-09-10 21:15:06 +02:00
|
|
|
/* Destroy a screen. */
|
|
|
|
void
|
|
|
|
screen_free(struct screen *s)
|
|
|
|
{
|
|
|
|
xfree(s->title);
|
2008-09-25 22:08:57 +02:00
|
|
|
grid_destroy(s->grid);
|
|
|
|
}
|
|
|
|
|
2009-06-25 17:54:38 +02:00
|
|
|
/* Reset tabs to default, eight spaces apart. */
|
|
|
|
void
|
|
|
|
screen_reset_tabs(struct screen *s)
|
|
|
|
{
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
if (s->tabs != NULL)
|
|
|
|
xfree(s->tabs);
|
|
|
|
|
|
|
|
if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL)
|
|
|
|
fatal("bit_alloc failed");
|
|
|
|
for (i = 8; i < screen_size_x(s); i += 8)
|
|
|
|
bit_set(s->tabs, i);
|
|
|
|
}
|
|
|
|
|
2008-09-25 22:08:57 +02:00
|
|
|
/* Set screen title. */
|
|
|
|
void
|
|
|
|
screen_set_title(struct screen *s, const char *title)
|
|
|
|
{
|
2009-06-25 17:49:27 +02:00
|
|
|
char tmp[BUFSIZ];
|
|
|
|
|
|
|
|
strnvis(tmp, title, sizeof tmp, VIS_OCTAL|VIS_TAB|VIS_NL);
|
|
|
|
|
2008-09-25 22:08:57 +02:00
|
|
|
xfree(s->title);
|
2009-06-25 17:49:27 +02:00
|
|
|
s->title = xstrdup(tmp);
|
2008-09-10 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2007-07-09 21:04:12 +02:00
|
|
|
/* Resize screen. */
|
|
|
|
void
|
|
|
|
screen_resize(struct screen *s, u_int sx, u_int sy)
|
|
|
|
{
|
|
|
|
if (sx < 1)
|
|
|
|
sx = 1;
|
|
|
|
if (sy < 1)
|
|
|
|
sy = 1;
|
|
|
|
|
2009-06-25 17:54:38 +02:00
|
|
|
if (sx != screen_size_x(s)) {
|
2008-09-25 22:08:57 +02:00
|
|
|
screen_resize_x(s, sx);
|
2009-06-25 17:54:38 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* It is unclear what should happen to tabs on resize. xterm
|
|
|
|
* seems to try and maintain them, rxvt resets them. Resetting
|
|
|
|
* is simpler and more reliable so let's do that.
|
|
|
|
*/
|
|
|
|
screen_reset_tabs(s);
|
|
|
|
}
|
|
|
|
|
2008-09-25 22:08:57 +02:00
|
|
|
if (sy != screen_size_y(s))
|
|
|
|
screen_resize_y(s, sy);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
screen_resize_x(struct screen *s, u_int sx)
|
|
|
|
{
|
2009-03-28 16:43:41 +01:00
|
|
|
struct grid *gd = s->grid;
|
2008-09-25 22:08:57 +02:00
|
|
|
const struct grid_cell *gc;
|
2009-03-28 21:17:29 +01:00
|
|
|
const struct grid_utf8 *gu;
|
2008-09-25 22:08:57 +02:00
|
|
|
u_int xx, yy;
|
|
|
|
|
2008-09-29 18:58:02 +02:00
|
|
|
if (sx == 0)
|
|
|
|
fatalx("zero size");
|
|
|
|
|
2008-09-25 22:08:57 +02:00
|
|
|
/* If getting larger, not much to do. */
|
|
|
|
if (sx > screen_size_x(s)) {
|
|
|
|
gd->sx = sx;
|
2007-11-21 14:11:41 +01:00
|
|
|
return;
|
2008-09-25 22:08:57 +02:00
|
|
|
}
|
2007-10-04 21:22:26 +02:00
|
|
|
|
2008-09-25 22:08:57 +02:00
|
|
|
/* If getting smaller, nuke any data in lines over the new size. */
|
|
|
|
for (yy = gd->hsize; yy < gd->hsize + screen_size_y(s); yy++) {
|
2007-12-06 10:46:23 +01:00
|
|
|
/*
|
2008-09-25 22:08:57 +02:00
|
|
|
* If the character after the last is wide or padding, remove
|
|
|
|
* it and any leading padding.
|
2007-11-22 20:26:20 +01:00
|
|
|
*/
|
2009-03-28 21:17:29 +01:00
|
|
|
gc = &grid_default_cell;
|
2008-09-25 22:08:57 +02:00
|
|
|
for (xx = sx; xx > 0; xx--) {
|
|
|
|
gc = grid_peek_cell(gd, xx - 1, yy);
|
|
|
|
if (!(gc->flags & GRID_FLAG_PADDING))
|
|
|
|
break;
|
|
|
|
grid_set_cell(gd, xx - 1, yy, &grid_default_cell);
|
2007-11-22 19:09:43 +01:00
|
|
|
}
|
2009-03-28 21:17:29 +01:00
|
|
|
if (xx > 0 && xx != sx && gc->flags & GRID_FLAG_UTF8) {
|
|
|
|
gu = grid_peek_utf8(gd, xx - 1, yy);
|
|
|
|
if (gu->width > 1) {
|
|
|
|
grid_set_cell(
|
|
|
|
gd, xx - 1, yy, &grid_default_cell);
|
|
|
|
}
|
|
|
|
}
|
2007-11-22 19:09:43 +01:00
|
|
|
|
2008-09-25 22:08:57 +02:00
|
|
|
/* Reduce the line size. */
|
|
|
|
grid_reduce_line(gd, yy, sx);
|
2007-07-09 21:04:12 +02:00
|
|
|
}
|
2007-11-21 14:11:41 +01:00
|
|
|
|
2008-09-25 22:08:57 +02:00
|
|
|
if (s->cx >= sx)
|
|
|
|
s->cx = sx - 1;
|
|
|
|
gd->sx = sx;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
screen_resize_y(struct screen *s, u_int sy)
|
|
|
|
{
|
2009-03-28 16:43:41 +01:00
|
|
|
struct grid *gd = s->grid;
|
2009-06-25 18:02:10 +02:00
|
|
|
u_int needed, available, oldy, i;
|
2007-11-21 14:11:41 +01:00
|
|
|
|
2008-09-29 18:58:02 +02:00
|
|
|
if (sy == 0)
|
|
|
|
fatalx("zero size");
|
2009-06-25 18:02:10 +02:00
|
|
|
oldy = screen_size_y(s);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When resizing:
|
|
|
|
*
|
|
|
|
* If the height is decreasing, delete lines from the bottom until
|
|
|
|
* hitting the cursor, then push lines from the top into the history.
|
|
|
|
*
|
|
|
|
* When increasing, pull as many lines as possible from the history to
|
|
|
|
* the top, then fill the remaining with blanks at the bottom.
|
|
|
|
*/
|
2008-09-29 18:58:02 +02:00
|
|
|
|
2007-11-21 14:11:41 +01:00
|
|
|
/* Size decreasing. */
|
2009-06-25 18:02:10 +02:00
|
|
|
if (sy < oldy) {
|
|
|
|
needed = oldy - sy;
|
|
|
|
|
|
|
|
/* Delete as many lines as possible from the bottom. */
|
|
|
|
available = oldy - 1 - s->cy;
|
|
|
|
if (available > 0) {
|
|
|
|
if (available > needed)
|
|
|
|
available = needed;
|
|
|
|
grid_view_delete_lines(gd, oldy - available, available);
|
2007-12-06 10:46:23 +01:00
|
|
|
}
|
2009-06-25 18:02:10 +02:00
|
|
|
needed -= available;
|
2007-11-21 14:11:41 +01:00
|
|
|
|
2009-06-25 18:02:10 +02:00
|
|
|
/*
|
|
|
|
* Now just increase the history size to take over the lines
|
|
|
|
* which are left. XXX Should apply history limit?
|
|
|
|
*/
|
|
|
|
gd->hsize += needed;
|
|
|
|
s->cy -= needed;
|
|
|
|
}
|
2007-12-06 10:46:23 +01:00
|
|
|
|
2007-11-21 14:11:41 +01:00
|
|
|
/* Resize line arrays. */
|
2008-09-25 22:08:57 +02:00
|
|
|
gd->size = xrealloc(gd->size, gd->hsize + sy, sizeof *gd->size);
|
|
|
|
gd->data = xrealloc(gd->data, gd->hsize + sy, sizeof *gd->data);
|
2009-03-28 21:17:29 +01:00
|
|
|
gd->usize = xrealloc(gd->usize, gd->hsize + sy, sizeof *gd->usize);
|
|
|
|
gd->udata = xrealloc(gd->udata, gd->hsize + sy, sizeof *gd->udata);
|
2007-11-21 14:11:41 +01:00
|
|
|
|
|
|
|
/* Size increasing. */
|
2009-06-25 18:02:10 +02:00
|
|
|
if (sy > oldy) {
|
|
|
|
needed = sy - oldy;
|
|
|
|
|
|
|
|
/* Try to pull as much as possible out of the history. */
|
|
|
|
available = gd->hsize;
|
|
|
|
if (available > 0) {
|
|
|
|
if (available > needed)
|
|
|
|
available = needed;
|
|
|
|
gd->hsize -= available;
|
|
|
|
s->cy += available;
|
|
|
|
}
|
|
|
|
needed -= available;
|
|
|
|
|
|
|
|
/* Then fill the rest in with blanks. */
|
|
|
|
for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++) {
|
2009-06-25 18:04:43 +02:00
|
|
|
gd->size[i] = 0;
|
|
|
|
gd->data[i] = NULL;
|
|
|
|
gd->usize[i] = 0;
|
|
|
|
gd->udata[i] = NULL;
|
2008-09-25 22:08:57 +02:00
|
|
|
}
|
2007-07-09 21:04:12 +02:00
|
|
|
}
|
|
|
|
|
2009-06-25 18:02:10 +02:00
|
|
|
/* Set the new size, and reset the scroll region. */
|
2008-09-25 22:08:57 +02:00
|
|
|
gd->sy = sy;
|
|
|
|
s->rupper = 0;
|
|
|
|
s->rlower = screen_size_y(s) - 1;
|
2007-07-09 21:04:12 +02:00
|
|
|
}
|
|
|
|
|
2007-12-06 10:46:23 +01:00
|
|
|
/* Set selection. */
|
2007-09-29 20:48:04 +02:00
|
|
|
void
|
2009-01-10 02:51:22 +01:00
|
|
|
screen_set_selection(struct screen *s,
|
2008-12-08 17:19:51 +01:00
|
|
|
u_int sx, u_int sy, u_int ex, u_int ey, struct grid_cell *gc)
|
2007-09-29 20:48:04 +02:00
|
|
|
{
|
2007-12-06 10:46:23 +01:00
|
|
|
struct screen_sel *sel = &s->sel;
|
2007-11-21 23:20:44 +01:00
|
|
|
|
2008-12-08 17:19:51 +01:00
|
|
|
memcpy(&sel->cell, gc, sizeof sel->cell);
|
|
|
|
|
2007-12-06 10:46:23 +01:00
|
|
|
sel->flag = 1;
|
|
|
|
if (ey < sy || (sy == ey && ex < sx)) {
|
|
|
|
sel->sx = ex; sel->sy = ey;
|
|
|
|
sel->ex = sx; sel->ey = sy;
|
|
|
|
} else {
|
|
|
|
sel->sx = sx; sel->sy = sy;
|
|
|
|
sel->ex = ex; sel->ey = ey;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear selection. */
|
|
|
|
void
|
|
|
|
screen_clear_selection(struct screen *s)
|
|
|
|
{
|
|
|
|
struct screen_sel *sel = &s->sel;
|
|
|
|
|
|
|
|
sel->flag = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if cell in selection. */
|
|
|
|
int
|
|
|
|
screen_check_selection(struct screen *s, u_int px, u_int py)
|
|
|
|
{
|
|
|
|
struct screen_sel *sel = &s->sel;
|
|
|
|
|
|
|
|
if (!sel->flag || py < sel->sy || py > sel->ey)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
if (py == sel->sy && py == sel->ey) {
|
|
|
|
if (px < sel->sx || px > sel->ex)
|
|
|
|
return (0);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((py == sel->sy && px < sel->sx) || (py == sel->ey && px > sel->ex))
|
|
|
|
return (0);
|
|
|
|
return (1);
|
2007-07-09 21:04:12 +02:00
|
|
|
}
|