1e8fc4975SFrançois Revol<?php 2e8fc4975SFrançois Revol 3e8fc4975SFrançois Revol/* 4e8fc4975SFrançois Revol * haiku.php - an online Haiku demo using qemu and vnc. 5e8fc4975SFrançois Revol * Copyright 2007, Francois Revol, revol@free.fr. 6e8fc4975SFrançois Revol */ 7e8fc4975SFrançois Revol 8e8fc4975SFrançois Revol// relative path to the vnc java applet jar 9e8fc4975SFrançois Revol// you must *copy* (apache doesn't seem to like symlinks) it there. 10e8fc4975SFrançois Revol 11e8fc4975SFrançois Revol// on debian, apt-get install vnc-java will put them in 12e8fc4975SFrançois Revol// /usr/share/vnc-java 13e8fc4975SFrançois Revol//define("VNCJAVA_PATH", "vnc-java"); 14e8fc4975SFrançois Revol//define("VNCJAR", "vncviewer.jar"); 15e8fc4975SFrançois Revol//define("VNCCLASS", "vncviewer.class"); 16e8fc4975SFrançois Revol 17e8fc4975SFrançois Revol// to use the tightvnc applet instead (supports > 8bpp): 18e8fc4975SFrançois Revol// on debian, apt-get install tightvnc-java will put them in 19e8fc4975SFrançois Revol// /usr/share/tightvnc-java 20e8fc4975SFrançois Revoldefine("VNCJAVA_PATH", "tightvnc-java"); 21e8fc4975SFrançois Revoldefine("VNCJAR", "VncViewer.jar"); 22e8fc4975SFrançois Revoldefine("VNCCLASS", "VncViewer.class"); 23e8fc4975SFrançois Revol 24e8fc4975SFrançois Revol// maximum count of qemu instances. 25e8fc4975SFrançois Revoldefine("MAX_QEMUS", 8); 26e8fc4975SFrançois Revol 27e8fc4975SFrançois Revol// size of the java applet, must match the default resolution of the image. 28*52a7cf96SFrançois Revol//define("APPLET_WIDTH", "800"); 29*52a7cf96SFrançois Revol//define("APPLET_HEIGHT", "600"); 30*52a7cf96SFrançois Revoldefine("APPLET_WIDTH", "1024"); 31*52a7cf96SFrançois Revoldefine("APPLET_HEIGHT", "768"); 32e8fc4975SFrançois Revol// vnc protocol base port. 33e8fc4975SFrançois Revoldefine("VNCPORTBASE", 5900); 34e8fc4975SFrançois Revol 35e8fc4975SFrançois Revol// timeout before the demo session is killed, as argument to /bin/sleep 36e8fc4975SFrançois Revoldefine("SESSION_TIMEOUT", "10m"); 37e8fc4975SFrançois Revol 38e8fc4975SFrançois Revol// path to qemu binary 398ad2eabaSFrançois Revol//define("QEMU_BIN", "/usr/bin/qemu"); 408ad2eabaSFrançois Revoldefine("QEMU_BIN", "/usr/local/bin/qemu"); 41e8fc4975SFrançois Revol// default arguments: no network, emulate tablet, readonly image file. 428ad2eabaSFrançois Revoldefine("QEMU_ARGS","-net none -usbdevice wacom-tablet -k en-us -snapshot"); 438ad2eabaSFrançois Revol//define("QEMU_ARGS","-net none -usbdevice wacom-tablet -k fr -snapshot"); 44e8fc4975SFrançois Revol// absolute path to the image. 458ad2eabaSFrançois Revoldefine("QEMU_IMAGE_PATH","/home/revol/haiku/trunk/generated.x86/haiku.image"); 468ad2eabaSFrançois Revol// qemu 0.8.2 needs "", qemu 0.9.1 needs ":" 478ad2eabaSFrançois Revoldefine("QEMU_VNC_PREFIX", ":"); 48e8fc4975SFrançois Revol 49e8fc4975SFrançois Revol// name of session and pid files in /tmp 50e8fc4975SFrançois Revoldefine("QEMU_SESSFILE_TMPL", "qemu-haiku-session-"); 51e8fc4975SFrançois Revoldefine("QEMU_PIDFILE_TMPL", "qemu-haiku-pid-"); 52e8fc4975SFrançois Revol// name of session variable holding the qemu slot; not yet used correctly 53e8fc4975SFrançois Revoldefine("QEMU_IDX_VAR", "QEMU_HAIKU_SESSION_VAR"); 54e8fc4975SFrançois Revol 55e8fc4975SFrançois Revolsession_start(); 56e8fc4975SFrançois Revol 57e8fc4975SFrançois Revolif (isset($_GET['frame'])) { 58e8fc4975SFrançois Revol} 59e8fc4975SFrançois Revol 60e8fc4975SFrançois Revol?> 61e8fc4975SFrançois Revol<html> 62e8fc4975SFrançois Revol<head> 63e8fc4975SFrançois Revol<title>Haiku Test</title> 64e8fc4975SFrançois Revol</head> 65e8fc4975SFrançois Revol<script> 66e8fc4975SFrançois Revolfunction onPageUnload() { 67e8fc4975SFrançois Revol //window.open("<?php echo $_SERVER["SCRIPT_NAME"] . "?close"; ?>", "closing", "width=100,height=30,location=no,menubar=no,toolbar=no,scrollbars=no"); 68e8fc4975SFrançois Revol} 69e8fc4975SFrançois Revol</script> 70e8fc4975SFrançois Revol<?php 71e8fc4975SFrançois Revol 72e8fc4975SFrançois Revol 73e8fc4975SFrançois Revol// statics 74e8fc4975SFrançois Revol 75e8fc4975SFrançois Revol$count = $_SESSION['compteur']; 76e8fc4975SFrançois Revol//$count = $GLOBALS['compteur']; 77e8fc4975SFrançois Revol$closing = 0; 78e8fc4975SFrançois Revolif (isset($_GET['close'])) { 79e8fc4975SFrançois Revol $closing = 1; 80e8fc4975SFrançois Revol echo "<body>"; 81e8fc4975SFrançois Revol} else 82e8fc4975SFrançois Revol echo "<body onunload=\"onPageUnload();\">"; 83e8fc4975SFrançois Revol 84e8fc4975SFrançois Revolfunction dbg($str) 85e8fc4975SFrançois Revol{ 86e8fc4975SFrançois Revol echo "<div class=\"debug\">$str</div>\n"; 87e8fc4975SFrançois Revol} 88e8fc4975SFrançois Revol 89e8fc4975SFrançois Revolfunction err($str) 90e8fc4975SFrançois Revol{ 91e8fc4975SFrançois Revol echo "<div class=\"error\">$str</div>\n"; 92e8fc4975SFrançois Revol} 93e8fc4975SFrançois Revol 94e8fc4975SFrançois Revolfunction make_qemu_sessionfile_name($idx) 95e8fc4975SFrançois Revol{ 96e8fc4975SFrançois Revol return "/tmp/" . QEMU_SESSFILE_TMPL . $idx; 97e8fc4975SFrançois Revol} 98e8fc4975SFrançois Revol 99e8fc4975SFrançois Revolfunction make_qemu_pidfile_name($idx) 100e8fc4975SFrançois Revol{ 101e8fc4975SFrançois Revol return "/tmp/" . QEMU_PIDFILE_TMPL . $idx; 102e8fc4975SFrançois Revol} 103e8fc4975SFrançois Revol 104e8fc4975SFrançois Revolfunction find_qemu_slot() 105e8fc4975SFrançois Revol{ 106e8fc4975SFrançois Revol for ($idx = 0; $idx < MAX_QEMUS; $idx++) { 107e8fc4975SFrançois Revol $pidfile = make_qemu_pidfile_name($idx); 108e8fc4975SFrançois Revol $sessfile = make_qemu_sessionfile_name($idx); 109e8fc4975SFrançois Revol dbg("checking \"$pidfile\", \"$sessfile\"..."); 110e8fc4975SFrançois Revol if (!file_exists($pidfile) && !file_exists($sessfile)) { 111e8fc4975SFrançois Revol file_put_contents($sessfile, session_id()); 112e8fc4975SFrançois Revol $sid = file_get_contents($sessfile); 113e8fc4975SFrançois Revol if ($sid != session_id()) 114e8fc4975SFrançois Revol continue; 115e8fc4975SFrançois Revol $_SESSION[QEMU_IDX_VAR] = $idx; 116e8fc4975SFrançois Revol return $idx; 117e8fc4975SFrançois Revol } 118e8fc4975SFrançois Revol } 119e8fc4975SFrançois Revol return -1; 120e8fc4975SFrançois Revol} 121e8fc4975SFrançois Revol 122e8fc4975SFrançois Revolfunction qemu_slot() 123e8fc4975SFrançois Revol{ 124e8fc4975SFrançois Revol return $_SESSION[QEMU_IDX_VAR]; 125e8fc4975SFrançois Revol} 126e8fc4975SFrançois Revol 127e8fc4975SFrançois Revolfunction vnc_display() 128e8fc4975SFrançois Revol{ 129e8fc4975SFrançois Revol return qemu_slot(); 130e8fc4975SFrançois Revol} 131e8fc4975SFrançois Revol 132e8fc4975SFrançois Revolfunction vnc_port() 133e8fc4975SFrançois Revol{ 134e8fc4975SFrançois Revol return VNCPORTBASE + vnc_display(); 135e8fc4975SFrançois Revol} 136e8fc4975SFrançois Revol 137e8fc4975SFrançois Revolfunction is_my_session_valid() 138e8fc4975SFrançois Revol{ 139e8fc4975SFrançois Revol if (!isset($_SESSION[QEMU_IDX_VAR])) 140e8fc4975SFrançois Revol return 0; 141e8fc4975SFrançois Revol $idx = $_SESSION[QEMU_IDX_VAR]; 142e8fc4975SFrançois Revol $sessfile = make_qemu_sessionfile_name($idx); 143e8fc4975SFrançois Revol if (!file_exists($sessfile)) 144e8fc4975SFrançois Revol return 0; 145e8fc4975SFrançois Revol $qemusession=file_get_contents($sessfile); 146e8fc4975SFrançois Revol // has expired 147e8fc4975SFrançois Revol if ($qemusession != session_id()) { 148e8fc4975SFrançois Revol return 0; 149e8fc4975SFrançois Revol } 150e8fc4975SFrançois Revol return 1; 151e8fc4975SFrançois Revol} 152e8fc4975SFrançois Revol 153e8fc4975SFrançois Revol 154e8fc4975SFrançois Revolfunction start_qemu() 155e8fc4975SFrançois Revol{ 156e8fc4975SFrançois Revol $idx = find_qemu_slot(); 157e8fc4975SFrançois Revol if ($idx < 0) { 158e8fc4975SFrançois Revol err("No available qemu slot, please try later."); 159e8fc4975SFrançois Revol return $idx; 160e8fc4975SFrançois Revol } 161e8fc4975SFrançois Revol $pidfile = make_qemu_pidfile_name($idx); 1628ad2eabaSFrançois Revol $cmd = QEMU_BIN . " " . QEMU_ARGS . " -vnc " . QEMU_VNC_PREFIX . vnc_display() . " -pidfile " . $pidfile . " " . QEMU_IMAGE_PATH; 163e8fc4975SFrançois Revol 164e8fc4975SFrançois Revol if (file_exists($pidfile)) 165e8fc4975SFrançois Revol unlink($pidfile); 166e8fc4975SFrançois Revol dbg("Starting <tt>" . $cmd . "</tt>..."); 167e8fc4975SFrançois Revol 168e8fc4975SFrançois Revol $descriptorspec = array( 169e8fc4975SFrançois Revol // 0 => array("pipe", "r"), // stdin 170e8fc4975SFrançois Revol // 1 => array("pipe", "w"), // stdout 171e8fc4975SFrançois Revol // 2 => array("pipe", "w") // stderr 172e8fc4975SFrançois Revol ); 173e8fc4975SFrançois Revol //$cmd="/bin/ls"; 174e8fc4975SFrançois Revol //passthru($cmd, $ret); 175e8fc4975SFrançois Revol //dbg("ret=$ret"); 176e8fc4975SFrançois Revol $cmd .= " &"; 177e8fc4975SFrançois Revol $process = proc_open($cmd, $descriptorspec, $pipes); 178e8fc4975SFrançois Revol sleep(1); 179e8fc4975SFrançois Revol proc_close($process); 180e8fc4975SFrançois Revol 181e8fc4975SFrançois Revol dbg("Started QEMU."); 182e8fc4975SFrançois Revol $sessfile = make_qemu_sessionfile_name($idx); 183e8fc4975SFrançois Revol $cmd = "(sleep " . SESSION_TIMEOUT . "; kill -9 `cat " . $pidfile . "`; rm " . $pidfile . " " . $sessfile . ") &"; 184e8fc4975SFrançois Revol 185e8fc4975SFrançois Revol $process = proc_open($cmd, $descriptorspec, $wkpipes); 186e8fc4975SFrançois Revol sleep(1); 187e8fc4975SFrançois Revol proc_close($process); 188e8fc4975SFrançois Revol 189e8fc4975SFrançois Revol dbg("Started timed kill."); 190e8fc4975SFrançois Revol dbg("Ready for a " . SESSION_TIMEOUT . " session."); 191e8fc4975SFrançois Revol} 192e8fc4975SFrançois Revol 193e8fc4975SFrançois Revol 194e8fc4975SFrançois Revolfunction output_applet_code() 195e8fc4975SFrançois Revol{ 196e8fc4975SFrançois Revol $w = APPLET_WIDTH; 197e8fc4975SFrançois Revol $h = APPLET_HEIGHT; 198e8fc4975SFrançois Revol $port = vnc_port(); 199e8fc4975SFrançois Revol $vncjpath = VNCJAVA_PATH; 200e8fc4975SFrançois Revol $jar = VNCJAR; 201e8fc4975SFrançois Revol $class = VNCCLASS; 202e8fc4975SFrançois Revol echo "<applet code=$class codebase=\"$vncjpath/\" archive=\"$vncjpath/$jar\" width=$w height=$h> 203e8fc4975SFrançois Revol <param name=\"PORT\" value=\"$port\"> 204e8fc4975SFrançois Revol <!param name=\"HOST\" value=\"$HTTP_HOST\"><!-- no need --> 205e8fc4975SFrançois Revol <param name=\"PORT\" value=\"$port\"> 206e8fc4975SFrançois Revol <param name=\"PASSWORD\" value=\"\"> 207e8fc4975SFrançois Revol There should be a java applet here... make sure you have a JVM and it's enabled! 208e8fc4975SFrançois Revol </applet>"; 209e8fc4975SFrançois Revol} 210e8fc4975SFrançois Revol 211e8fc4975SFrançois Revoldbg("Checking if session is running..."); 212e8fc4975SFrançois Revol 213e8fc4975SFrançois Revolif (is_my_session_valid()) { 214e8fc4975SFrançois Revol dbg("Session running"); 215e8fc4975SFrançois Revol $qemuidx = qemu_slot(); 216e8fc4975SFrançois Revol} else if ($closing != 1) { 217e8fc4975SFrançois Revol dbg("Need to start qemu"); 218e8fc4975SFrançois Revol 219e8fc4975SFrançois Revol $qemuidx = start_qemu(); 220e8fc4975SFrançois Revol} 221e8fc4975SFrançois Revol 222e8fc4975SFrançois Revolif ($qemuidx >= 0) { 223e8fc4975SFrançois Revol if ($closing) { 224e8fc4975SFrançois Revol dbg("closing..."); 225e8fc4975SFrançois Revol unlink(make_qemu_sessionfile_name($qemuidx)); 226e8fc4975SFrançois Revol //unlink(make_qemu_sessionfile_name($qemuidx)); 227e8fc4975SFrançois Revol sleep(1); 228e8fc4975SFrançois Revol //echo "<script> self.close(\"closing\"); </script>"; 229e8fc4975SFrançois Revol } else { 230e8fc4975SFrançois Revol dbg("Waiting for vnc server..."); 231e8fc4975SFrançois Revol sleep(5); 232e8fc4975SFrançois Revol output_applet_code(); 233e8fc4975SFrançois Revol } 234e8fc4975SFrançois Revol} 235e8fc4975SFrançois Revol 236e8fc4975SFrançois Revol 237e8fc4975SFrançois Revol 238e8fc4975SFrançois Revol?> 239e8fc4975SFrançois Revol 240e8fc4975SFrançois Revol</body> 241e8fc4975SFrançois Revol</html> 242