diff webtty.js @ 9:826a7ced69f8

Make the emulator screen resizable.
author John "Elwin" Edwards <elwin@sdf.org>
date Wed, 09 May 2012 13:38:05 -0700
parents d1b3c3af34d6
children d051aad3e95f
line wrap: on
line diff
--- a/webtty.js	Mon May 07 16:08:59 2012 -0700
+++ b/webtty.js	Wed May 09 13:38:05 2012 -0700
@@ -14,13 +14,23 @@
 /* Constructor for TermSessions.  Note that it opens the terminal and 
  * adds itself to the sessions dict. 
  */
-function TermSession(sessid) {
+function TermSession(sessid, h, w) {
+  /* Set up the sizes. */
+  w = Math.floor(Number(w));
+  if (!(w > 0 && w < 256))
+    w = 80;
+  this.w = w;
+  h = Math.floor(Number(h));
+  if (!(h > 0 && h < 256))
+    h = 25;
+  this.h = h;
+  /* Customize the environment. */
   var childenv = {};
   for (var key in process.env) {
     if (!(key in env_dontuse))
       childenv[key] = process.env[key];
   }
-  childenv["PTYHELPER"] = "25x80";
+  childenv["PTYHELPER"] = String(this.h) + "x" + String(this.w);
   // Should setsid get set?
   var spawnopts = {"env": childenv, "cwd": process.env["HOME"]};
   this.child = child_process.spawn(ptyhelp, ["bash"], spawnopts);
@@ -154,10 +164,25 @@
 function login(req, res, formdata) {
   var resheaders = {'Content-Type': 'text/plain'};
   var sessid = randkey();
-  var nsession = new TermSession(sessid);
+  /* The TermSession constructor will check these thoroughly too, but 
+   * you can't be too suspicious of client-supplied data. */
+  var w = 80;
+  var h = 25;
+  var t;
+  if ("w" in formdata) {
+    t = Math.floor(Number(formdata["w"]));
+    if (t > 0 && t < 256)
+      w = t;
+  }
+  if ("h" in formdata) {
+    t = Math.floor(Number(formdata["h"]));
+    if (t > 0 && t < 256)
+      h = t;
+  }
+  var nsession = new TermSession(sessid, h, w);
   resheaders["Set-Cookie"] = "ID=" + sessid;
   res.writeHead(200, resheaders);
-  res.write("l1\n" + sessid + "\n");
+  res.write("l1\n" + sessid + "\n" + String(h) + "x" + String(w) + "\n");
   res.end();
   console.log("Started new session with key " + sessid + ", pid " + nsession.child.pid);
   return;