grub/grub-core/lib/progress.c
Paulo Flabiano Smorigo 84a0e9699f Add progress module to display load progress of files.
* grub-core/lib/progress.c: New file.
	* grub-core/Makefile.core.def (progress): New module.
	* grub-core/kern/file.c (grub_file_open): File name added.
	* (grub_file_read): Progress hook added.
	* grub-core/fs/cbfs.c (grub_cbfs_read): Likewise.
	* grub-core/fs/cpio_common.c (grub_cpio_read): Likewise.
	* grub-core/net/net.c (grub_net_fs_read_real): Likewise.
	* include/grub/file.h (struct grub_file): Add progress module
	* members.
	* include/grub/term.h (struct grub_term_output): Likewise.
	* grub-core/osdep/unix/emuconsole.c (grub_console_term_output):
	Terminal velocity added.
	* grub-core/osdep/windows/emuconsole.c (grub_console_term_output):
	* Likewise.
	* grub-core/term/arc/console.c (grub_console_term_output): Likewise.
	* grub-core/term/efi/console.c (grub_console_term_output): Likewise.
	* grub-core/term/gfxterm.c (grub_video_term): Likewise.
	* grub-core/term/i386/coreboot/cbmemc.c (grub_cbmemc_term_output):
	* Likewise.
	* grub-core/term/i386/pc/console.c (grub_console_term_output):
	* Likewise.
	* grub-core/term/i386/pc/vga_text.c (grub_vga_text_term): Likewise.
	* grub-core/term/ieee1275/console.c (grub_console_term_output):
	* Likewise.
	* grub-core/term/morse.c (grub_audio_term_output): Likewise.
	* grub-core/term/serial.c (grub_serial_term_output): Likewise.
	* grub-core/term/spkmodem.c (grub_spkmodem_term_output): Likewise.
	* grub-core/term/uboot/console.c (uboot_console_term_output):
	* Likewise.
2013-10-22 16:42:20 -02:00

100 lines
3.6 KiB
C

/* progress.c - show loading progress */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2013 Free Software Foundation, Inc.
*
* GRUB 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 3 of the License, or
* (at your option) any later version.
*
* GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/types.h>
#include <grub/time.h>
#include <grub/term.h>
#include <grub/dl.h>
#include <grub/misc.h>
#include <grub/normal.h>
GRUB_MOD_LICENSE ("GPLv3+");
#define UPDATE_INTERVAL 800
static void
grub_file_progress_hook_real (grub_disk_addr_t sector __attribute__ ((unused)),
unsigned offset __attribute__ ((unused)),
unsigned length, void *data)
{
grub_uint64_t now = grub_get_time_ms ();
static grub_uint64_t last_progress_update_time;
grub_file_t file = data;
file->progress_offset += length;
if (((now - last_progress_update_time > UPDATE_INTERVAL) &&
(file->progress_offset - file->offset > 0)) ||
(file->progress_offset == file->size))
{
char buffer[80];
struct grub_term_output *term;
char *partial_file_name = grub_strrchr (file->name, '/') + 1;
grub_uint64_t current_speed = grub_divmod64 ((file->progress_offset -
file->last_progress_offset)
* 100ULL * 1000ULL,
now - file->last_progress_time, 0);
file->estimated_speed = (file->estimated_speed + current_speed) >> 1;
grub_snprintf (buffer, sizeof (buffer), " [ %.20s %s %llu%% ",
partial_file_name,
grub_get_human_size (file->progress_offset,
GRUB_HUMAN_SIZE_NORMAL),
(100 * file->progress_offset) / file->size);
char *ptr = buffer + grub_strlen (buffer);
grub_snprintf (ptr, sizeof (buffer) - (ptr - buffer), "%s ]",
grub_get_human_size (file->estimated_speed,
GRUB_HUMAN_SIZE_SPEED));
grub_uint16_t len = grub_strlen (buffer);
FOR_ACTIVE_TERM_OUTPUTS (term)
{
if (term->progress_update_counter++ > term->progress_update_divisor ||
(file->progress_offset == file->size &&
term->progress_update_divisor != (unsigned) GRUB_PROGRESS_NO_UPDATE))
{
struct grub_term_coordinate old_pos = grub_term_getxy (term);
struct grub_term_coordinate new_pos = old_pos;
new_pos.x = grub_term_width (term) - len - 1;
grub_term_gotoxy (term, new_pos);
grub_puts_terminal (buffer, term);
grub_term_gotoxy (term, old_pos);
term->progress_update_counter = 0;
}
}
file->last_progress_offset = file->progress_offset;
file->last_progress_time = now;
last_progress_update_time = now;
}
}
GRUB_MOD_INIT(progress)
{
grub_file_progress_hook = grub_file_progress_hook_real;
}
GRUB_MOD_FINI(progress)
{
grub_file_progress_hook = 0;
}