Blame alsamixer/textbox.c

Packit Service a9274b
/*
Packit Service a9274b
 * textbox.c - show a text box for messages, files or help
Packit Service a9274b
 * Copyright (c) 1998,1999 Tim Janik
Packit Service a9274b
 *                         Jaroslav Kysela <perex@perex.cz>
Packit Service a9274b
 * Copyright (c) 2009      Clemens Ladisch <clemens@ladisch.de>
Packit Service a9274b
 *
Packit Service a9274b
 * This program is free software: you can redistribute it and/or modify
Packit Service a9274b
 * it under the terms of the GNU General Public License as published by
Packit Service a9274b
 * the Free Software Foundation, either version 2 of the License, or
Packit Service a9274b
 * (at your option) any later version.
Packit Service a9274b
 *
Packit Service a9274b
 * This program is distributed in the hope that it will be useful,
Packit Service a9274b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service a9274b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service a9274b
 * GNU General Public License for more details.
Packit Service a9274b
 *
Packit Service a9274b
 * You should have received a copy of the GNU General Public License
Packit Service a9274b
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit Service a9274b
 */
Packit Service a9274b
Packit Service a9274b
#include "aconfig.h"
Packit Service a9274b
#include <stdio.h>
Packit Service a9274b
#include <stdlib.h>
Packit Service a9274b
#include <string.h>
Packit Service a9274b
#include <errno.h>
Packit Service a9274b
#include CURSESINC
Packit Service a9274b
#include <alsa/asoundlib.h>
Packit Service a9274b
#include "gettext_curses.h"
Packit Service a9274b
#include "utils.h"
Packit Service a9274b
#include "die.h"
Packit Service a9274b
#include "mem.h"
Packit Service a9274b
#include "colors.h"
Packit Service a9274b
#include "widget.h"
Packit Service a9274b
#include "textbox.h"
Packit Service a9274b
#include "bindings.h"
Packit Service a9274b
Packit Service a9274b
static void create_text_box(const char *const *lines, unsigned int count,
Packit Service a9274b
			    const char *title, int attrs);
Packit Service a9274b
Packit Service a9274b
void show_error(const char *msg, int err)
Packit Service a9274b
{
Packit Service a9274b
	const char *lines[2];
Packit Service a9274b
	unsigned int count;
Packit Service a9274b
Packit Service a9274b
	lines[0] = msg;
Packit Service a9274b
	count = 1;
Packit Service a9274b
	if (err) {
Packit Service a9274b
		lines[1] = strerror(err);
Packit Service a9274b
		count = 2;
Packit Service a9274b
	}
Packit Service a9274b
	create_text_box(lines, count, _("Error"), attrs.errormsg);
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
void show_alsa_error(const char *msg, int err)
Packit Service a9274b
{
Packit Service a9274b
	const char *lines[2];
Packit Service a9274b
	unsigned int count;
Packit Service a9274b
Packit Service a9274b
	lines[0] = msg;
Packit Service a9274b
	count = 1;
Packit Service a9274b
	if (err < 0) {
Packit Service a9274b
		lines[1] = snd_strerror(err);
Packit Service a9274b
		count = 2;
Packit Service a9274b
	}
Packit Service a9274b
	create_text_box(lines, count, _("Error"), attrs.errormsg);
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
void show_textfile(const char *file_name)
Packit Service a9274b
{
Packit Service a9274b
	char *buf;
Packit Service a9274b
	unsigned int file_size;
Packit Service a9274b
	unsigned int line_count;
Packit Service a9274b
	unsigned int i;
Packit Service a9274b
	const char **lines;
Packit Service a9274b
	const char *start_line;
Packit Service a9274b
Packit Service a9274b
	buf = read_file(file_name, &file_size);
Packit Service a9274b
	if (!buf) {
Packit Service a9274b
		int err = errno;
Packit Service a9274b
		buf = casprintf(_("Cannot open file \"%s\"."), file_name);
Packit Service a9274b
		show_error(buf, err);
Packit Service a9274b
		return;
Packit Service a9274b
	}
Packit Service a9274b
	line_count = 0;
Packit Service a9274b
	for (i = 0; i < file_size; ++i)
Packit Service a9274b
		line_count += buf[i] == '\n';
Packit Service a9274b
	lines = ccalloc(line_count, sizeof *lines);
Packit Service a9274b
	line_count = 0;
Packit Service a9274b
	start_line = buf;
Packit Service a9274b
	for (i = 0; i < file_size; ++i) {
Packit Service a9274b
		if (buf[i] == '\n') {
Packit Service a9274b
			lines[line_count++] = start_line;
Packit Service a9274b
			buf[i] = '\0';
Packit Service a9274b
			start_line = &buf[i + 1];
Packit Service a9274b
		}
Packit Service a9274b
		if (buf[i] == '\t')
Packit Service a9274b
			buf[i] = ' ';
Packit Service a9274b
	}
Packit Service a9274b
	create_text_box(lines, line_count, file_name, attrs.textbox);
Packit Service a9274b
	free(lines);
Packit Service a9274b
	free(buf);
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
void show_text(const char *const *lines, unsigned int count, const char *title)
Packit Service a9274b
{
Packit Service a9274b
	create_text_box(lines, count, title, attrs.textbox);
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
/**********************************************************************/
Packit Service a9274b
Packit Service a9274b
static struct widget text_widget;
Packit Service a9274b
static char *title;
Packit Service a9274b
static int widget_attrs;
Packit Service a9274b
static char **text_lines;
Packit Service a9274b
static unsigned int text_lines_count;
Packit Service a9274b
static int max_line_width;
Packit Service a9274b
static int text_box_y;
Packit Service a9274b
static int text_box_x;
Packit Service a9274b
static int max_scroll_y;
Packit Service a9274b
static int max_scroll_x;
Packit Service a9274b
static int current_top;
Packit Service a9274b
static int current_left;
Packit Service a9274b
Packit Service a9274b
static void update_text_lines(void)
Packit Service a9274b
{
Packit Service a9274b
	int i;
Packit Service a9274b
	int width;
Packit Service a9274b
	const char *line_begin;
Packit Service a9274b
	const char *line_end;
Packit Service a9274b
	int cur_y, cur_x;
Packit Service a9274b
	int rest_of_line;
Packit Service a9274b
Packit Service a9274b
	for (i = 0; i < text_box_y; ++i) {
Packit Service a9274b
		width = current_left;
Packit Service a9274b
		line_begin = mbs_at_width(text_lines[current_top + i], &width, 1);
Packit Service a9274b
		wmove(text_widget.window, i + 1, 1);
Packit Service a9274b
		if (width > current_left)
Packit Service a9274b
			waddch(text_widget.window, ' ');
Packit Service a9274b
		if (*line_begin != '\0') {
Packit Service a9274b
			width = text_box_x - (width > current_left);
Packit Service a9274b
			line_end = mbs_at_width(line_begin, &width, -1);
Packit Service a9274b
			if (width)
Packit Service a9274b
				waddnstr(text_widget.window, line_begin,
Packit Service a9274b
					 line_end - line_begin);
Packit Service a9274b
		}
Packit Service a9274b
		getyx(text_widget.window, cur_y, cur_x);
Packit Service a9274b
		if (cur_y == i + 1) {
Packit Service a9274b
			rest_of_line = text_box_x + 1 - cur_x;
Packit Service a9274b
			if (rest_of_line > 0)
Packit Service a9274b
				wprintw(text_widget.window, "%*s", rest_of_line, "");
Packit Service a9274b
		}
Packit Service a9274b
	}
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
static void update_y_scroll_bar(void)
Packit Service a9274b
{
Packit Service a9274b
	int length;
Packit Service a9274b
	int begin;
Packit Service a9274b
Packit Service a9274b
	if (max_scroll_y <= 0 || text_lines_count == 0)
Packit Service a9274b
		return;
Packit Service a9274b
	length = text_box_y * text_box_y / text_lines_count;
Packit Service a9274b
	if (length >= text_box_y)
Packit Service a9274b
		return;
Packit Service a9274b
	begin = current_top * (text_box_y - length) / max_scroll_y;
Packit Service a9274b
	mvwvline(text_widget.window, 1, text_box_x + 1, ' ', text_box_y);
Packit Service a9274b
	mvwvline(text_widget.window, begin + 1, text_box_x + 1, ACS_BOARD, length);
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
static void update_x_scroll_bar(void)
Packit Service a9274b
{
Packit Service a9274b
	int length;
Packit Service a9274b
	int begin;
Packit Service a9274b
Packit Service a9274b
	if (max_scroll_x <= 0 || max_line_width <= 0)
Packit Service a9274b
		return;
Packit Service a9274b
	length = text_box_x * text_box_x / max_line_width;
Packit Service a9274b
	if (length >= text_box_x)
Packit Service a9274b
		return;
Packit Service a9274b
	begin = current_left * (text_box_x - length) / max_scroll_x;
Packit Service a9274b
	mvwhline(text_widget.window, text_box_y + 1, 1, ' ', text_box_x);
Packit Service a9274b
	mvwhline(text_widget.window, text_box_y + 1, begin + 1, ACS_BOARD, length);
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
static void move_x(int delta)
Packit Service a9274b
{
Packit Service a9274b
	int left;
Packit Service a9274b
Packit Service a9274b
	left = current_left + delta;
Packit Service a9274b
	if (left < 0)
Packit Service a9274b
		left = 0;
Packit Service a9274b
	else if (left > max_scroll_x)
Packit Service a9274b
		left = max_scroll_x;
Packit Service a9274b
	if (left != current_left) {
Packit Service a9274b
		current_left = left;
Packit Service a9274b
		update_text_lines();
Packit Service a9274b
		update_x_scroll_bar();
Packit Service a9274b
	}
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
static void move_y(int delta)
Packit Service a9274b
{
Packit Service a9274b
	int top;
Packit Service a9274b
Packit Service a9274b
	top = current_top + delta;
Packit Service a9274b
	if (top < 0)
Packit Service a9274b
		top = 0;
Packit Service a9274b
	else if (top > max_scroll_y)
Packit Service a9274b
		top = max_scroll_y;
Packit Service a9274b
	if (top != current_top) {
Packit Service a9274b
		current_top = top;
Packit Service a9274b
		update_text_lines();
Packit Service a9274b
		update_y_scroll_bar();
Packit Service a9274b
	}
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
static void on_handle_key(int key)
Packit Service a9274b
{
Packit Service a9274b
	if (key >= ARRAY_SIZE(textbox_bindings))
Packit Service a9274b
		return;
Packit Service a9274b
Packit Service a9274b
	switch (textbox_bindings[key]) {
Packit Service a9274b
	case CMD_TEXTBOX_CLOSE:
Packit Service a9274b
		text_widget.close();
Packit Service a9274b
		break;
Packit Service a9274b
	case CMD_TEXTBOX_DOWN:
Packit Service a9274b
		move_y(1);
Packit Service a9274b
		break;
Packit Service a9274b
	case CMD_TEXTBOX_UP:
Packit Service a9274b
		move_y(-1);
Packit Service a9274b
		break;
Packit Service a9274b
	case CMD_TEXTBOX_LEFT:
Packit Service a9274b
		move_x(-1);
Packit Service a9274b
		break;
Packit Service a9274b
	case CMD_TEXTBOX_RIGHT:
Packit Service a9274b
		move_x(1);
Packit Service a9274b
		break;
Packit Service a9274b
	case CMD_TEXTBOX_PAGE_DOWN:
Packit Service a9274b
		move_y(text_box_y);
Packit Service a9274b
		break;
Packit Service a9274b
	case CMD_TEXTBOX_PAGE_UP:
Packit Service a9274b
		move_y(-text_box_y);
Packit Service a9274b
		break;
Packit Service a9274b
	case CMD_TEXTBOX_TOP:
Packit Service a9274b
		move_x(-max_scroll_x);
Packit Service a9274b
		break;
Packit Service a9274b
	case CMD_TEXTBOX_BOTTOM:
Packit Service a9274b
		move_x(max_scroll_x);
Packit Service a9274b
		break;
Packit Service a9274b
	case CMD_TEXTBOX_PAGE_RIGHT:
Packit Service a9274b
		move_x(8);
Packit Service a9274b
		break;
Packit Service a9274b
	case CMD_TEXTBOX_PAGE_LEFT:
Packit Service a9274b
		move_x(-8);
Packit Service a9274b
		break;
Packit Service a9274b
	}
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
static bool create(void)
Packit Service a9274b
{
Packit Service a9274b
	int len, width;
Packit Service a9274b
Packit Service a9274b
	if (screen_lines < 3 || screen_cols < 8) {
Packit Service a9274b
		text_widget.close();
Packit Service a9274b
		beep();
Packit Service a9274b
		return FALSE;
Packit Service a9274b
	}
Packit Service a9274b
Packit Service a9274b
	width = max_line_width;
Packit Service a9274b
	len = get_mbs_width(title) + 2;
Packit Service a9274b
	if (width < len)
Packit Service a9274b
		width = len;
Packit Service a9274b
Packit Service a9274b
	text_box_y = text_lines_count;
Packit Service a9274b
	if (text_box_y > screen_lines - 2)
Packit Service a9274b
		text_box_y = screen_lines - 2;
Packit Service a9274b
	max_scroll_y = text_lines_count - text_box_y;
Packit Service a9274b
	text_box_x = width;
Packit Service a9274b
	if (text_box_x > screen_cols - 2)
Packit Service a9274b
		text_box_x = screen_cols - 2;
Packit Service a9274b
	max_scroll_x = max_line_width - text_box_x;
Packit Service a9274b
Packit Service a9274b
	widget_init(&text_widget, text_box_y + 2, text_box_x + 2,
Packit Service a9274b
		    SCREEN_CENTER, SCREEN_CENTER, widget_attrs, WIDGET_BORDER);
Packit Service a9274b
	mvwprintw(text_widget.window, 0, (text_box_x + 2 - get_mbs_width(title) - 2) / 2, " %s ", title);
Packit Service a9274b
Packit Service a9274b
	if (current_top > max_scroll_y)
Packit Service a9274b
		current_top = max_scroll_y;
Packit Service a9274b
	if (current_left > max_scroll_x)
Packit Service a9274b
		current_left = max_scroll_x;
Packit Service a9274b
	update_text_lines();
Packit Service a9274b
	update_y_scroll_bar();
Packit Service a9274b
	update_x_scroll_bar();
Packit Service a9274b
	return TRUE;
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
static void on_window_size_changed(void)
Packit Service a9274b
{
Packit Service a9274b
	create();
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
static void on_close(void)
Packit Service a9274b
{
Packit Service a9274b
	unsigned int i;
Packit Service a9274b
Packit Service a9274b
	for (i = 0; i < text_lines_count; ++i)
Packit Service a9274b
		free(text_lines[i]);
Packit Service a9274b
	free(text_lines);
Packit Service a9274b
	widget_free(&text_widget);
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
static struct widget text_widget = {
Packit Service a9274b
	.handle_key = on_handle_key,
Packit Service a9274b
	.window_size_changed = on_window_size_changed,
Packit Service a9274b
	.close = on_close,
Packit Service a9274b
};
Packit Service a9274b
Packit Service a9274b
static void create_text_box(const char *const *lines, unsigned int count,
Packit Service a9274b
			    const char *title_, int attrs)
Packit Service a9274b
{
Packit Service a9274b
	unsigned int i;
Packit Service a9274b
Packit Service a9274b
	text_lines = ccalloc(count, sizeof *text_lines);
Packit Service a9274b
	for (i = 0; i < count; ++i)
Packit Service a9274b
		text_lines[i] = cstrdup(lines[i]);
Packit Service a9274b
	text_lines_count = count;
Packit Service a9274b
	max_line_width = get_max_mbs_width(lines, count);
Packit Service a9274b
	title = cstrdup(title_);
Packit Service a9274b
	widget_attrs = attrs;
Packit Service a9274b
Packit Service a9274b
	current_top = 0;
Packit Service a9274b
	current_left = 0;
Packit Service a9274b
Packit Service a9274b
	create();
Packit Service a9274b
}