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