Re: userui: new option to change VT

Julien Muchembled <[email protected]> Thu, 06 Feb 2014 13:13:35 +0100
Newsgroups gmane.linux.swsusp.devel
Message-ID <[email protected]>
Le 02/02/14 16:42, Julien Muchembled a écrit :
> I've just noticed that I didn't touch fbsplash_unprepare, but it's probably better to do nothing if the VT will be trashed at exit, so that the user does not see a black screen for a fraction of second.

Oops, I meant fbsplash_cleanup instead of fbsplash_unprepare. I don't have a patch yet.

Attached patches are:
- 0001-Fix-corrupted-display-of-PNG.patch: I recently found a regression while packaging last revision
- 0002-New-option-to-change-VT.patch: same patch as the one sent previously, but with a more detailed message commit

There are also 2 other pending patches, not by me:
- http://anonscm.debian.org/gitweb/?p=collab-maint/tuxonice-userui.git;a=blob;f=debian/patches/fix-duplicate-symbol-definition.patch;h=689601521b5702a272865a20ecc9814a6ed3b1f7;hb=HEAD
- https://github.com/NigelCunningham/Tuxonice-Userui/pull/7

_______________________________________________
TuxOnIce-devel mailing list
[email protected]
http://lists.tuxonice.net/listinfo/tuxonice-devel
0001-Fix-corrupted-display-of-PNG.patch (text/x-diff, 1.7 KB)
From 4e1ddb97e8592eb8736d780124cf10a5b7c467d7 Mon Sep 17 00:00:00 2001
From: Julien Muchembled <[email protected]>
Date: Wed, 5 Feb 2014 01:40:40 +0100
Subject: [PATCH 1/2] Fix corrupted display of PNG

This is broken since commit 3ac7e257510dfd4a6d92cabd1cd2dcff381bcf96,
which does more than simply applying original Debian patch.
---
 fbsplash/common.c | 1 -
 fbsplash/image.c  | 3 +--
 fbsplash/parse.c  | 1 -
 3 files changed, 1 insertion(+), 4 deletions(-)

diff --git a/fbsplash/common.c b/fbsplash/common.c
index 4450192..fb34c7c 100644
--- a/fbsplash/common.c
+++ b/fbsplash/common.c
@@ -28,7 +28,6 @@
 
 #include "../userui.h"
 #include "splash.h"
-#include "../userui.h"
 
 struct fb_var_screeninfo   fb_var;
 struct fb_fix_screeninfo   fb_fix;
diff --git a/fbsplash/image.c b/fbsplash/image.c
index 51682a3..2fb3c92 100644
--- a/fbsplash/image.c
+++ b/fbsplash/image.c
@@ -18,7 +18,6 @@
 #include <linux/fb.h>
 
 #include "config.h"
-#include "../userui.h"
 
 #ifdef CONFIG_PNG
 #ifdef TARGET_KERNEL
@@ -136,7 +135,7 @@ int load_png(char *filename, u8 **data, struct fb_cmap *cmap, unsigned int *widt
 		png_set_strip_alpha(png_ptr);
 
 #ifndef TARGET_KERNEL	
-	if (!((png_get_color_type(png_ptr, info_ptr) & PNG_COLOR_MASK_ALPHA) & want_alpha)) {
+	if (want_alpha && !(png_get_color_type(png_ptr, info_ptr) & PNG_COLOR_MASK_ALPHA)) {
 		png_set_add_alpha(png_ptr, 0xff, PNG_FILLER_AFTER);
 	}
 #endif
diff --git a/fbsplash/parse.c b/fbsplash/parse.c
index 5f01bd2..3577dc8 100644
--- a/fbsplash/parse.c
+++ b/fbsplash/parse.c
@@ -21,7 +21,6 @@
 #include <ctype.h>
 #include "../userui.h"
 #include "splash.h"
-#include "../userui.h"
 
 struct config_opt {
 	char *name;
-- 
1.8.5.2.988.g9b015e5.dirty
0002-New-option-to-change-VT.patch (text/x-diff, 7.8 KB)
From 2d24f82ea3c036078c9420ac1b4267352b4e6551 Mon Sep 17 00:00:00 2001
From: Julien Muchembled <[email protected]>
Date: Mon, 23 Dec 2013 06:30:06 +0100
Subject: [PATCH 2/2] New option to change VT

When invoked with -vN (or --chvt=N) option, userui makes /dev/ttyN the
foreground terminal before doing anything else, and restore previous VT at exit.
If /dev/ttyN is already the foreground terminal, the new option does nothing.
There's also no change in behaviour if this new option is not passed.

TOI UI needs its own VT and without such option, it would be required
to work around this in every power management hibernation.
---
 fbsplash/cmd.c                  | 11 ++++----
 fbsplash/common.c               |  1 -
 fbsplash/splash.h               |  1 -
 fbsplash/userui_fbsplash_core.c | 22 ----------------
 userui.h                        |  1 +
 userui_core.c                   | 56 +++++++++++++++++++++++++++++++++++++----
 userui_text.c                   |  2 --
 7 files changed, 58 insertions(+), 36 deletions(-)

diff --git a/fbsplash/cmd.c b/fbsplash/cmd.c
index 2672d69..0a418df 100644
--- a/fbsplash/cmd.c
+++ b/fbsplash/cmd.c
@@ -25,6 +25,7 @@
 #include <errno.h>
 #include <linux/fb.h>
 #include "splash.h"
+#include "../userui.h"
 
 #ifdef CONFIG_FBSPLASH
 #include <linux/console_splash.h>
@@ -32,7 +33,7 @@
 void cmd_setstate(unsigned int state, unsigned char origin)
 {
 	struct fb_splash_iowrapper wrapper = {
-		.vc = arg_vc,
+		.vc = suspend_vt,
 		.origin = origin,
 		.data = &state,
 	};
@@ -46,7 +47,7 @@ void cmd_setstate(unsigned int state, unsigned char origin)
 void cmd_setpic(struct fb_image *img, unsigned char origin)
 {
 	struct fb_splash_iowrapper wrapper = {
-		.vc = arg_vc,
+		.vc = suspend_vt,
 		.origin = origin,
 		.data = img,
 	};
@@ -60,7 +61,7 @@ void cmd_setcfg(unsigned char origin)
 {
 	struct vc_splash vc_cfg;
 	struct fb_splash_iowrapper wrapper = {
-		.vc = arg_vc,
+		.vc = suspend_vt,
 		.origin = origin,
 		.data = &vc_cfg,
 	};
@@ -82,7 +83,7 @@ void cmd_getcfg()
 {
 	struct vc_splash vc_cfg;
 	struct fb_splash_iowrapper wrapper = {
-		.vc = arg_vc,
+		.vc = suspend_vt,
 		.origin = FB_SPLASH_IO_ORIG_USER,
 		.data = &vc_cfg,
 	};
@@ -97,7 +98,7 @@ void cmd_getcfg()
 		strcpy(vc_cfg.theme, "<none>");
 	} 
 		
-	printf("Splash config on console %d:\n", arg_vc);
+	printf("Splash config on console %d:\n", suspend_vt);
 	printf("tx:       %d\n", vc_cfg.tx);
 	printf("ty:	  %d\n", vc_cfg.ty);
 	printf("twidth:	  %d\n", vc_cfg.twidth);
diff --git a/fbsplash/common.c b/fbsplash/common.c
index fb34c7c..370007d 100644
--- a/fbsplash/common.c
+++ b/fbsplash/common.c
@@ -37,7 +37,6 @@ char *config_file = NULL;
 
 enum TASK arg_task = none; 
 int arg_fb = 0;
-int arg_vc = 0;
 char arg_mode = 'v';
 char *arg_theme = NULL;
 u16 arg_progress = 0;
diff --git a/fbsplash/splash.h b/fbsplash/splash.h
index 4b37ff1..bd0d100 100644
--- a/fbsplash/splash.h
+++ b/fbsplash/splash.h
@@ -265,7 +265,6 @@ extern struct fb_fix_screeninfo   fb_fix;
 extern enum ENDIANESS endianess;
 extern enum TASK arg_task;
 extern int arg_fb;
-extern int arg_vc;
 extern char *arg_theme;
 extern char arg_mode;
 extern u16 arg_progress;
diff --git a/fbsplash/userui_fbsplash_core.c b/fbsplash/userui_fbsplash_core.c
index 2a4099b..09f1504 100644
--- a/fbsplash/userui_fbsplash_core.c
+++ b/fbsplash/userui_fbsplash_core.c
@@ -67,27 +67,6 @@ static void silent_off() {
 	show_cursor();
 }
 
-static int get_active_vt() {
-	int vt, fd;
-	struct vt_stat vt_stat;
-
-	vt = 62; /* default */
-
-	if ((fd = open("/dev/tty0", O_RDONLY)) == -1)
-		goto out;
-
-	if (ioctl(fd, VT_GETSTATE, &vt_stat) == -1)
-		goto out;
-
-	vt = vt_stat.v_active - 1;
-
-out:
-	if (fd >= 0)
-		close(fd);
-
-	return vt;
-}
-
 static int fbsplash_load() {
 	fb_fd = -1;
 	last_pos = 0;
@@ -103,7 +82,6 @@ static int fbsplash_load() {
 		return 1;
 	}
 
-	arg_vc = get_active_vt();
 	arg_mode = 's';
 
 	/* Read theme config file */
diff --git a/userui.h b/userui.h
index ca59826..920f704 100644
--- a/userui.h
+++ b/userui.h
@@ -53,6 +53,7 @@ void printk(char *msg, ...);
 
 extern char software_suspend_version[32];
 extern int can_use_escape;
+extern int suspend_vt;
 extern volatile __uint32_t console_loglevel;
 extern volatile __uint32_t suspend_action;
 extern volatile __uint32_t suspend_debug;
diff --git a/userui_core.c b/userui_core.c
index 74d7aa6..5e3f713 100644
--- a/userui_core.c
+++ b/userui_core.c
@@ -96,6 +96,8 @@ static void might_switch_ops(void)
 }
 
 static int netlink_socket_num = 0;
+static int saved_vt = 0;
+int suspend_vt = 0;
 
 static char* descriptions[] = {
 	"General",
@@ -373,7 +375,7 @@ int common_keypress_handler(int key)
 }
 
 static void handle_params(int argc, char **argv) {
-	static char global_optstring[] = "htc:fu";
+	static char global_optstring[] = "htc:fuv:";
 	static struct option global_longopts[] = {
 		{"help", 0, 0, 'h'},
 		{"test", 0, 0, 't'},
@@ -382,6 +384,7 @@ static void handle_params(int argc, char **argv) {
 #ifdef USE_USPLASH
 		{"usplash", 0, 0, 'u'},
 #endif
+		{"chvt", 1, 0, 'v'},
 		{NULL, 0, 0, 0},
 	};
 
@@ -437,6 +440,9 @@ static void handle_params(int argc, char **argv) {
 			case 't':
 				test_run++;
 				break;
+			case 'v':
+				suspend_vt = atoi(optarg);
+				break;
 			case 'h':
 				fprintf(stderr,
 "Usage: %s [options]\n"
@@ -445,6 +451,9 @@ static void handle_params(int argc, char **argv) {
 "     Specifying -t once will give an demo of this module.\n"
 "     Specifying -t twice will make the demo run as fast as it can.\n"
 "     (useful for performance testing).\n"
+"  -v, --chvt=N\n"
+"     Makes /dev/ttyN the foreground terminal before doing anything else\n"
+"     and restore previous VT at the end.\n"
 #ifdef USE_USPLASH
 "  -u\n"
 "     Use usersplash interface by default.\n"
@@ -603,6 +612,27 @@ static void enforce_lifesavers() {
 	setrlimit(RLIMIT_CORE, &r);
 }
 
+static int chvt(int vt) {
+	return ioctl(console_fd, VT_ACTIVATE, vt)
+	    || ioctl(console_fd, VT_WAITACTIVE, vt)
+	    || close(console_fd)
+	    || (console_fd = open("/dev/console", O_RDWR)) == -1;
+}
+
+static void restore_vt() {
+	struct rlimit r;
+	r.rlim_cur = r.rlim_max = console_fd + 1;
+	setrlimit(RLIMIT_NOFILE, &r);
+	close(STDOUT_FILENO);
+	close(STDERR_FILENO);
+	/* It does not matter whether other files are still opened or not.
+	 * RLIMIT_NOFILE only prevents files from being opened with a fd greater
+	 * or equal than the limit. chvt should reopen console with fd=0
+	 */
+	chvt(saved_vt);
+	ioctl(console_fd, VT_DISALLOCATE, suspend_vt);
+}
+
 static void restore_console() {
 	int result;
 	ioctl(console_fd, KDSKBMODE, K_XLATE);
@@ -726,14 +756,30 @@ static void setup_signal_handlers() {
 }
 
 static void open_console() {
+	struct vt_stat vtstat;
+
 	if ((console_fd = open("/dev/console", O_RDWR)) == -1)
 		bail_err("open(\"/dev/console\", O_RDWR)");
 
-	if (dup2(console_fd, STDIN_FILENO) == -1)
-		bail_err("dup2(fd, STDIN_FILENO)");
-	if (!test_run && dup2(console_fd, STDOUT_FILENO) == -1)
+	if (ioctl(console_fd, VT_GETSTATE, &vtstat))
+		bail_err("VT_GETSTATE");
+	if (!suspend_vt)
+		suspend_vt = vtstat.v_active;
+	else if (suspend_vt != vtstat.v_active) {
+		if (chvt(suspend_vt))
+			bail_err("failed to change VT");
+		saved_vt = vtstat.v_active;
+		atexit(restore_vt);
+	}
+
+	if (console_fd != STDIN_FILENO) {
+		if (dup2(console_fd, STDIN_FILENO) == -1 || close(console_fd))
+			bail_err("dup2(fd, STDIN_FILENO)");
+		console_fd = STDIN_FILENO;
+	}
+	if (dup2(console_fd, STDOUT_FILENO) == -1)
 		bail_err("dup2(fd, STDOUT_FILENO)");
-	if (!test_run && dup2(console_fd, STDERR_FILENO) == -1)
+	if (dup2(console_fd, STDERR_FILENO) == -1)
 		bail_err("dup2(fd, STDERR_FILENO)");
 }
 
diff --git a/userui_text.c b/userui_text.c
index 0b9ff2a..5502f4b 100644
--- a/userui_text.c
+++ b/userui_text.c
@@ -362,8 +362,6 @@ static void text_cleanup()
 		close(vcsa_fd);
 
 	ioctl(STDOUT_FILENO, TCSETSF, (long)&termios);
-
-	/* chvt back? */
 }
 
 static void text_redraw()
-- 
1.8.5.2.988.g9b015e5.dirty
signature.asc (application/pgp-signature, 836 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iQIcBAEBCAAGBQJS83xvAAoJEEwjvlkacWMke5IP+gN/0PlSgOsYW5XtQIJObzGd
zeOWWir8SIH7/mIzivD+StQ9K1MFccZ2mG796rd/E3A/FPL4nIEB/Qy1jnuIjUQI
70DXdpVQLgQVy0TMSbTk/J/9WbtZlAc17c9DUziYx0QREl7IqO5DptoO+0kKtvQb
HwzPnVBqDg6cogb2uGQV7KPYZORbf0uIeHarGi9PcgNC0yPl0iyKS7m+4D8VtNOB
+S/GXzomyR2PNikF4EyIlypNmcgGi08VVpqEWSh8jLcAc90Xtm+iqIbClCf7ZD1s
DdqYnu+oA519UlYN37D6gkAm6kySs8o+H24pT1DlT8O2E8g1zYQxLTAQv76bLDLf
ygEHYwDRvq74KxL+JKAJhv5L+615XUT30xPczQoOrxPdsnL3rt+TVBwE+oQALLbG
YJWneX9mhyjUB9mWruEraBkq5JoMoE5MAIVQecAxC9tMp2kwTwlrHWg8HMVRmpsO
LJUOpM9iaQUYnLRfJSXNvfirPz7YXTNYbFAIldx39MG6EG1GxLD7/kNbMLgBIg4M
UtYfMs8kSHJKtl8u02udhtgk6p1KVIwHagTJMJUywLn2SJY0KsUCC7tAfROw8y0Z
ZCxWDIkk+gGIA9tyb3873m2vMdnLHOJ594Qx2vCtmYcN8qNfkdwYWLaIV2qX6pSl
Pbn5LWjH2EYq/XQX9hWY
=Kz0r
-----END PGP SIGNATURE-----