PATCH: Move scrollback out of low-level console drivers
Hello,
Here is a patch that moves scrollback handling out of the low-level *con
drivers and into the virtual console layer in vt.c. It's non-intrusive insofar as low-level drivers that do not take advantage of it continue to function as usual, but drivers that do want to use it (eg, vgacon after this patch is applied) can do so through a rather simple API. Basically a low-level driver signals it wants the vt layer to maintain scrollback through vc_data->vc_use_scrollback, and then grabs chunks of scrollback by calling vt_get_scrollback from the consw->con_scrolldelta function. The payoff for users is (a) scrollback can now be an arbitrary length, limited only by memory and (b) scrollback is preserved between console switches, something vgacon/etc are unable to do when they are relying solely on origin tricks to make scrollback happen.
A few points:
- ioctls are used to control the vt scrollback. This is probably not the
best way to do it, but I wasn't sure if procfs, sysfs, or sysctl were the way to go. If anyone can recommend a better way to do it I'd be happy to implement it.
- A userspace program, cfgscroll (attached) is used to configure scrollback.
It has reasonable defaults, but cfgscroll -e needs to be called to enable scrollback. This is because we can't start saving scrollback before kmalloc works, and I couldn't determine a reasonable way to find out if kmalloc is working yet (is there some kind of kmalloc_running flag somewhere?). The source is junky because hopefully it will go away.
- /dev/vcs* will return whatever is really on the screen, so if the
user is in the middle of scrollback that's what vcs will return. If/when gpm starts using the vcs devices, this means users will be able to select text out of scrollback.
- This patch is against 2.6.13-rc1-mm1.
Finally, this is my first attempt at a kernel patch so there's probably some
other things that will need attention. Any constructive criticism would be appreciated - hopefully this patch doesn't clash with the work being done by the linuxconsole folks.
Thanks,
Steve.
drivers/char/selection.c +3 -0
@@ -118,6 +118,9 @@poke_blanked_console (); +if (vc->vc_cumdelta) + vc->vc_sw->con_scrolldelta (vc,0 ); + {unsigned short xs, ys, xe, ye;if (!access_ok (VERIFY_READ , sel,sizeof (*sel)))
drivers/char/vt.c +214 -5
299 lines not on this page · raw message
drivers/char/vt_ioctl.c +50 -0
62 lines not on this page · raw message
drivers/video/console/vgacon.c +62 -28
155 lines not on this page · raw message
include/linux/console_struct.h +15 -0
@@ -13,6 +13,14 @@#define NPAR 16 +struct vc_scrback { +struct list_head vsc_list; +unsigned char *vsc_buf; +unsigned char *vsc_bufptr; +unsigned int vsc_bufleft; +unsigned int vsc_bufsize; +}; +struct vc_data {unsigned short vc_num;/* Console number */ unsigned int vc_cols;/* [#] Console size */
@@ -26,6 +34,13 @@const struct consw *vc_sw;unsigned short *vc_screenbuf;/* In-memory character/attribute buffer */ unsigned int vc_screenbuf_size; +/* scrollback */ +struct list_head vc_scrollback; +unsigned int vc_scrollbacksz;/* no. bytes in scrollback */ +unsigned int vc_maxscrollback;/* max scrollback size */ +int vc_cumdelta;/* cumulative delta */ +short vc_scrollback_needsave; + int vc_support_scrollback : 1; /* driver support? */unsigned char vc_mode;/* KD_TEXT, ... */ /* attributes for all characters on screen */ unsigned char vc_attr;/* Current attributes */
include/linux/vt.h +8 -0
@@ -51,4 +51,12 @@#define VT_LOCKSWITCH 0x560B/* disallow vt switching */ #define VT_UNLOCKSWITCH 0x560C/* allow vt switching */ +#define VT_ENABLESCROLLBACK 0x560D/* turn on global scrollback */ +#define VT_SETSCROLLBACK 0x560E/* change scrollback properties */ +#define VT_GETSCROLLBACK 0x560F/* get scrollback properties */ + +struct vt_scrollparms { +unsigned int vp_con; +unsigned int vp_size; +};#endif /* _LINUX_VT_H */
include/linux/vt_kern.h +6 -0
@@ -46,6 +46,12 @@void scrollfront (struct vc_data *vc,int lines);void update_region (struct vc_data *vc,unsigned long start,int count);void redraw_screen (struct vc_data *vc,int is_switch); +int vt_set_scrollback (struct vt_scrollparms *sc); +void vt_clear_scrollback (struct vc_data *vc); +unsigned int vt_get_scrollback (unsigned char **rb,struct vc_data *vc, +unsigned int needlines); +int vt_update_cumdelta (struct vc_data *c,int delta); +#define update_screen (x) redraw_screen(x, 0)#define switch_screen (x) redraw_screen(x, 1)
#include <stdio.h>
#include <unistd.h>
#include <sysexits.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/vt.h>
struct vt_scrollparms sp;
int openconsole(void) {
char *dev = "/dev/console";
int fd;
fd = open(dev, O_RDWR);
if(fd == -1) {
perror("unable to open console");
return(-1);
}
return(fd);
}int enable_scrollback(void) {
int ret, fd;
fd = openconsole();
if(fd == -1)
return(EX_TEMPFAIL);
ret = ioctl(fd, VT_ENABLESCROLLBACK);
if(ret == -1) {
perror("unable to enable scrollback");
return(EX_TEMPFAIL);
}
return(EX_OK);
}int set_scrollback(void) {
int ret, fd;
fd = openconsole();
if(fd == -1)
return(EX_TEMPFAIL);
ret = ioctl(fd, VT_SETSCROLLBACK, &sp);
if(ret == -1) {
perror("unable to configure scrollback");
return(EX_TEMPFAIL);
}
return(EX_OK);
}int get_scrollback(void) {
int ret, fd;
fd = openconsole();
if(fd == -1)
return(EX_TEMPFAIL);
ret = ioctl(fd, VT_GETSCROLLBACK, &sp);
if(ret == -1) {
perror("unable to get scrollback parameters");
return(EX_TEMPFAIL);
}
return(EX_OK);
}int clear_scrollback(void) {
unsigned int savesize;
int ret;
ret = get_scrollback();
if(ret != EX_OK)
return(ret);
savesize = sp.vp_size;
sp.vp_size = 0;
ret = set_scrollback();
if(ret != EX_OK)
return(ret);
sp.vp_size = savesize;
return(set_scrollback());
}int main(int argc, char **argv) {
/* usage: cfgscroll -e
enable global scrollback (cannot be disabled)
usage: cfgscroll -s con size
set scrollback for specific console
usage: cfgscroll -g con
get scrollback for specific console
usage: cfgscroll -c con
clear scrollback for specific console */
int ret;
if(argc == 2) {
if(!strcmp(argv[1], "-e"))
return(enable_scrollback());
goto dispusage;
} else if(argc == 3) {
if(!strcmp(argv[1], "-g")) {
sscanf(argv[2], "%u", &sp.vp_con);
ret = get_scrollback();
if(ret == EX_OK);
printf("console %u: max scrollback = %u\n", sp.vp_con, sp.vp_size);
return(ret);
} else if(!strcmp(argv[1], "-c")) {
sscanf(argv[2], "%u", &sp.vp_con);
return(clear_scrollback());
}
goto dispusage;
} else if(argc == 4) {
if(!strcmp(argv[1], "-s")) {
sscanf(argv[2], "%u", &sp.vp_con);
sscanf(argv[3], "%u", &sp.vp_size);
return(set_scrollback());
}
}dispusage:
printf("usage: cfgscroll [-e] || [-s console size] || [-g console] || [-c console]\n");
exit(EX_USAGE);
}