Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | # |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 3 | # Copyright (C) 2005-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 4 | # Copyright (C) 2002-2003 Constantin Kaplinsky. All Rights Reserved. |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 5 | # Copyright (C) 2002-2005 RealVNC Ltd. |
| 6 | # Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved. |
| 7 | # |
| 8 | # This is free software; you can redistribute it and/or modify |
| 9 | # it under the terms of the GNU General Public License as published by |
| 10 | # the Free Software Foundation; either version 2 of the License, or |
| 11 | # (at your option) any later version. |
| 12 | # |
| 13 | # This software is distributed in the hope that it will be useful, |
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | # GNU General Public License for more details. |
| 17 | # |
| 18 | # You should have received a copy of the GNU General Public License |
| 19 | # along with this software; if not, write to the Free Software |
| 20 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 21 | # USA. |
| 22 | # |
| 23 | |
| 24 | # |
| 25 | # vncserver - wrapper script to start an X VNC server. |
| 26 | # |
| 27 | |
| 28 | # |
| 29 | # First make sure we're operating in a sane environment. |
| 30 | # |
| 31 | |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 32 | $exedir = ""; |
| 33 | $slashndx = rindex($0, "/"); |
| 34 | if($slashndx>=0) { |
| 35 | $exedir = substr($0, 0, $slashndx+1); |
| 36 | } |
| 37 | |
| 38 | $vncClasses = ""; |
| 39 | |
| 40 | $xauth = "xauth"; |
| 41 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 42 | &SanityCheck(); |
| 43 | |
| 44 | # |
| 45 | # Global variables. You may want to configure some of these for your site. |
| 46 | # |
| 47 | |
| 48 | $geometry = "1024x768"; |
DRC | 9d1c157 | 2009-03-26 18:18:51 +0000 | [diff] [blame] | 49 | #$depth = 16; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 50 | $vncJavaFiles = (((-d "/usr/share/vnc/classes") && "/usr/share/vnc/classes") || |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 51 | ((-d "/usr/local/vnc/classes") && "/usr/local/vnc/classes") || |
| 52 | ((-d "$vncClasses") && "$vncClasses")); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 53 | $vncUserDir = "$ENV{HOME}/.vnc"; |
| 54 | $xauthorityFile = "$ENV{XAUTHORITY}" || "$ENV{HOME}/.Xauthority"; |
| 55 | |
| 56 | $defaultXStartup |
| 57 | = ("#!/bin/sh\n\n". |
Adam Tkac | 804d86f | 2009-04-03 14:33:51 +0000 | [diff] [blame] | 58 | "vncconfig -iconic &\n". |
DRC | 9324898 | 2009-03-26 18:14:38 +0000 | [diff] [blame] | 59 | "unset SESSION_MANAGER\n". |
| 60 | "OS=`uname -s`\n". |
| 61 | "if [ \$OS = 'Linux' ]; then\n". |
| 62 | " case \"\$WINDOWMANAGER\" in\n". |
| 63 | " \*gnome\*)\n". |
| 64 | " if [ -e /etc/SuSE-release ]; then\n". |
| 65 | " PATH=\$PATH:/opt/gnome/bin\n". |
| 66 | " export PATH\n". |
| 67 | " fi\n". |
| 68 | " ;;\n". |
| 69 | " esac\n". |
| 70 | "fi\n". |
| 71 | "if [ -x /etc/X11/xinit/xinitrc ]; then\n". |
| 72 | " exec /etc/X11/xinit/xinitrc\n". |
| 73 | "fi\n". |
| 74 | "if [ -f /etc/X11/xinit/xinitrc ]; then\n". |
| 75 | " exec sh /etc/X11/xinit/xinitrc\n". |
| 76 | "fi\n". |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 77 | "[ -r \$HOME/.Xresources ] && xrdb \$HOME/.Xresources\n". |
| 78 | "xsetroot -solid grey\n". |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 79 | "xterm -geometry 80x24+10+10 -ls -title \"\$VNCDESKTOP Desktop\" &\n". |
| 80 | "twm &\n"); |
| 81 | |
| 82 | chop($host = `uname -n`); |
| 83 | |
DRC | d6821bf | 2009-03-26 18:17:49 +0000 | [diff] [blame] | 84 | @fontpaths = ('/usr/share/X11/fonts', '/usr/share/fonts', '/usr/share/fonts/X11/'); |
| 85 | if (! -l "/usr/lib/X11") {push(@fontpaths, '/usr/lib/X11/fonts');} |
| 86 | if (! -l "/usr/X11") {push(@fontpaths, '/usr/X11/lib/X11/fonts');} |
| 87 | if (! -l "/usr/X11R6") {push(@fontpaths, '/usr/X11R6/lib/X11/fonts');} |
| 88 | push(@fontpaths, '/usr/share/fonts/default'); |
| 89 | |
| 90 | @fonttypes = ('misc', |
| 91 | '75dpi', |
| 92 | '100dpi', |
| 93 | 'Speedo', |
| 94 | 'Type1'); |
| 95 | |
| 96 | foreach $_fpath (@fontpaths) { |
| 97 | foreach $_ftype (@fonttypes) { |
| 98 | if (-f "$_fpath/$_ftype/fonts.dir") { |
| 99 | if (! -l "$_fpath/$_ftype") { |
| 100 | $fontPath .= "$_fpath/$_ftype,"; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | if ($fontPath) { |
| 106 | if (substr($fontPath, -1, 1) == ',') { |
| 107 | chop $fontPath; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | $defFontPath = "unix/:7100"; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 112 | |
| 113 | # Check command line options |
| 114 | |
| 115 | &ParseOptions("-geometry",1,"-depth",1,"-pixelformat",1,"-name",1,"-kill",1, |
DRC | eed5d1f | 2009-03-26 19:16:19 +0000 | [diff] [blame] | 116 | "-help",0,"-h",0,"--help",0,"-fp",1); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 117 | |
| 118 | &Usage() if ($opt{'-help'} || $opt{'-h'} || $opt{'--help'}); |
| 119 | |
| 120 | &Kill() if ($opt{'-kill'}); |
| 121 | |
| 122 | # Uncomment this line if you want default geometry, depth and pixelformat |
| 123 | # to match the current X display: |
| 124 | # &GetXDisplayDefaults(); |
| 125 | |
| 126 | if ($opt{'-geometry'}) { |
| 127 | $geometry = $opt{'-geometry'}; |
| 128 | } |
| 129 | if ($opt{'-depth'}) { |
| 130 | $depth = $opt{'-depth'}; |
| 131 | $pixelformat = ""; |
| 132 | } |
| 133 | if ($opt{'-pixelformat'}) { |
| 134 | $pixelformat = $opt{'-pixelformat'}; |
| 135 | } |
DRC | eed5d1f | 2009-03-26 19:16:19 +0000 | [diff] [blame] | 136 | if ($opt{'-fp'}) { |
| 137 | $fontPath = $opt{'-fp'}; |
| 138 | } |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 139 | |
| 140 | &CheckGeometryAndDepth(); |
| 141 | |
| 142 | |
| 143 | # Create the user's vnc directory if necessary. |
| 144 | |
| 145 | if (!(-e $vncUserDir)) { |
| 146 | if (!mkdir($vncUserDir,0755)) { |
| 147 | die "$prog: Could not create $vncUserDir.\n"; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | # Make sure the user has a password. |
| 152 | |
| 153 | ($z,$z,$mode) = stat("$vncUserDir/passwd"); |
| 154 | if (!(-e "$vncUserDir/passwd") || ($mode & 077)) { |
| 155 | warn "\nYou will require a password to access your desktops.\n\n"; |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 156 | system($exedir."vncpasswd -q $vncUserDir/passwd"); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 157 | if (($? >> 8) != 0) { |
| 158 | exit 1; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | # Find display number. |
| 163 | |
| 164 | if ((@ARGV > 0) && ($ARGV[0] =~ /^:(\d+)$/)) { |
| 165 | $displayNumber = $1; |
| 166 | shift(@ARGV); |
| 167 | if (!&CheckDisplayNumber($displayNumber)) { |
| 168 | die "A VNC server is already running as :$displayNumber\n"; |
| 169 | } |
| 170 | } elsif ((@ARGV > 0) && ($ARGV[0] !~ /^-/)) { |
| 171 | &Usage(); |
| 172 | } else { |
| 173 | $displayNumber = &GetDisplayNumber(); |
| 174 | } |
| 175 | |
| 176 | $vncPort = 5900 + $displayNumber; |
| 177 | |
| 178 | $desktopLog = "$vncUserDir/$host:$displayNumber.log"; |
| 179 | unlink($desktopLog); |
| 180 | |
| 181 | # Make an X server cookie - use as the seed the sum of the current time, our |
| 182 | # PID and part of the encrypted form of the password. Ideally we'd use |
| 183 | # /dev/urandom, but that's only available on Linux. |
| 184 | |
| 185 | srand(time+$$+unpack("L",`cat $vncUserDir/passwd`)); |
| 186 | $cookie = ""; |
| 187 | for (1..16) { |
| 188 | $cookie .= sprintf("%02x", int(rand(256)) % 256); |
| 189 | } |
| 190 | |
| 191 | system("xauth -f $xauthorityFile add $host:$displayNumber . $cookie"); |
| 192 | system("xauth -f $xauthorityFile add $host/unix:$displayNumber . $cookie"); |
| 193 | |
| 194 | if ($opt{'-name'}) { |
| 195 | $desktopName = $opt{'-name'}; |
| 196 | } else { |
| 197 | $desktopName = "$host:$displayNumber ($ENV{USER})"; |
| 198 | } |
| 199 | |
| 200 | # Now start the X VNC Server |
| 201 | |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 202 | $cmd = $exedir."Xvnc :$displayNumber"; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 203 | $cmd .= " -desktop " . "edString($desktopName); |
| 204 | $cmd .= " -httpd $vncJavaFiles" if ($vncJavaFiles); |
| 205 | $cmd .= " -auth $xauthorityFile"; |
| 206 | $cmd .= " -geometry $geometry" if ($geometry); |
| 207 | $cmd .= " -depth $depth" if ($depth); |
| 208 | $cmd .= " -pixelformat $pixelformat" if ($pixelformat); |
| 209 | $cmd .= " -rfbwait 30000"; |
| 210 | $cmd .= " -rfbauth $vncUserDir/passwd"; |
| 211 | $cmd .= " -rfbport $vncPort"; |
DRC | d6821bf | 2009-03-26 18:17:49 +0000 | [diff] [blame] | 212 | $cmd .= " -fp $fontPath" if ($fontPath); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 213 | $cmd .= " -pn"; |
| 214 | |
DRC | d6821bf | 2009-03-26 18:17:49 +0000 | [diff] [blame] | 215 | # Add color database stuff here, e.g.: |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 216 | # |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 217 | # $cmd .= " -co /usr/lib/X11/rgb"; |
| 218 | # |
| 219 | |
| 220 | foreach $arg (@ARGV) { |
| 221 | $cmd .= " " . "edString($arg); |
| 222 | } |
| 223 | $cmd .= " >> " . "edString($desktopLog) . " 2>&1"; |
| 224 | |
| 225 | # Run $cmd and record the process ID. |
| 226 | |
| 227 | $pidFile = "$vncUserDir/$host:$displayNumber.pid"; |
| 228 | system("$cmd & echo \$! >$pidFile"); |
| 229 | |
| 230 | # Give Xvnc a chance to start up |
| 231 | |
| 232 | sleep(3); |
DRC | d6821bf | 2009-03-26 18:17:49 +0000 | [diff] [blame] | 233 | unless (kill 0, `cat $pidFile`) { |
| 234 | warn "\nWARNING: The first attempt to start Xvnc failed, possibly because the vncserver\n"; |
DRC | eed5d1f | 2009-03-26 19:16:19 +0000 | [diff] [blame] | 235 | warn "script was not able to figure out an appropriate X11 font path for this system\n"; |
| 236 | warn "or because the font path you specified with the -fp argument was not valid.\n"; |
DRC | d6821bf | 2009-03-26 18:17:49 +0000 | [diff] [blame] | 237 | warn "Attempting to restart Xvnc using the X Font Server (xfs) ...\n"; |
| 238 | $cmd =~ s@-fp [^ ]+@@; |
| 239 | $cmd .= " -fp $defFontPath" if ($defFontPath); |
| 240 | system("$cmd & echo \$! >$pidFile"); |
| 241 | sleep(3); |
| 242 | } |
| 243 | unless (kill 0, `cat $pidFile`) { |
| 244 | warn "Could not start Xvnc.\n\n"; |
| 245 | open(LOG, "<$desktopLog"); |
| 246 | while (<LOG>) { print; } |
| 247 | close(LOG); |
| 248 | die "\n"; |
| 249 | } |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 250 | |
| 251 | warn "\nNew '$desktopName' desktop is $host:$displayNumber\n\n"; |
| 252 | |
| 253 | # Create the user's xstartup script if necessary. |
| 254 | |
| 255 | if (!(-e "$vncUserDir/xstartup")) { |
| 256 | warn "Creating default startup script $vncUserDir/xstartup\n"; |
| 257 | open(XSTARTUP, ">$vncUserDir/xstartup"); |
| 258 | print XSTARTUP $defaultXStartup; |
| 259 | close(XSTARTUP); |
| 260 | chmod 0755, "$vncUserDir/xstartup"; |
| 261 | } |
| 262 | |
| 263 | # Run the X startup script. |
| 264 | |
| 265 | warn "Starting applications specified in $vncUserDir/xstartup\n"; |
| 266 | warn "Log file is $desktopLog\n\n"; |
| 267 | |
| 268 | # If the unix domain socket exists then use that (DISPLAY=:n) otherwise use |
| 269 | # TCP (DISPLAY=host:n) |
| 270 | |
| 271 | if (-e "/tmp/.X11-unix/X$displayNumber" || |
| 272 | -e "/usr/spool/sockets/X11/$displayNumber") |
| 273 | { |
| 274 | $ENV{DISPLAY}= ":$displayNumber"; |
| 275 | } else { |
| 276 | $ENV{DISPLAY}= "$host:$displayNumber"; |
| 277 | } |
| 278 | $ENV{VNCDESKTOP}= $desktopName; |
| 279 | |
| 280 | system("$vncUserDir/xstartup >> " . "edString($desktopLog) . " 2>&1 &"); |
| 281 | |
| 282 | exit; |
| 283 | |
| 284 | |
| 285 | ############################################################################### |
| 286 | # |
| 287 | # CheckGeometryAndDepth simply makes sure that the geometry and depth values |
| 288 | # are sensible. |
| 289 | # |
| 290 | |
| 291 | sub CheckGeometryAndDepth |
| 292 | { |
| 293 | if ($geometry =~ /^(\d+)x(\d+)$/) { |
| 294 | $width = $1; $height = $2; |
| 295 | |
| 296 | if (($width<1) || ($height<1)) { |
| 297 | die "$prog: geometry $geometry is invalid\n"; |
| 298 | } |
| 299 | |
| 300 | while (($width % 4)!=0) { |
| 301 | $width = $width + 1; |
| 302 | } |
| 303 | |
| 304 | while (($height % 2)!=0) { |
| 305 | $height = $height + 1; |
| 306 | } |
| 307 | |
| 308 | $geometry = "${width}x$height"; |
| 309 | } else { |
| 310 | die "$prog: geometry $geometry is invalid\n"; |
| 311 | } |
| 312 | |
DRC | e5b4f75 | 2009-03-26 18:23:29 +0000 | [diff] [blame] | 313 | if ($depth && (($depth < 8) || ($depth > 32))) { |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 314 | die "Depth must be between 8 and 32\n"; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | |
| 319 | # |
| 320 | # GetDisplayNumber gets the lowest available display number. A display number |
| 321 | # n is taken if something is listening on the VNC server port (5900+n) or the |
| 322 | # X server port (6000+n). |
| 323 | # |
| 324 | |
| 325 | sub GetDisplayNumber |
| 326 | { |
| 327 | foreach $n (1..99) { |
| 328 | if (&CheckDisplayNumber($n)) { |
| 329 | return $n+0; # Bruce Mah's workaround for bug in perl 5.005_02 |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | die "$prog: no free display number on $host.\n"; |
| 334 | } |
| 335 | |
| 336 | |
| 337 | # |
| 338 | # CheckDisplayNumber checks if the given display number is available. A |
| 339 | # display number n is taken if something is listening on the VNC server port |
| 340 | # (5900+n) or the X server port (6000+n). |
| 341 | # |
| 342 | |
| 343 | sub CheckDisplayNumber |
| 344 | { |
| 345 | local ($n) = @_; |
| 346 | |
| 347 | socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n"; |
| 348 | eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))'; |
| 349 | if (!bind(S, pack('S n x12', $AF_INET, 6000 + $n))) { |
| 350 | close(S); |
| 351 | return 0; |
| 352 | } |
| 353 | close(S); |
| 354 | |
| 355 | socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n"; |
| 356 | eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))'; |
| 357 | if (!bind(S, pack('S n x12', $AF_INET, 5900 + $n))) { |
| 358 | close(S); |
| 359 | return 0; |
| 360 | } |
| 361 | close(S); |
| 362 | |
| 363 | if (-e "/tmp/.X$n-lock") { |
| 364 | warn "\nWarning: $host:$n is taken because of /tmp/.X$n-lock\n"; |
| 365 | warn "Remove this file if there is no X server $host:$n\n"; |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | if (-e "/tmp/.X11-unix/X$n") { |
| 370 | warn "\nWarning: $host:$n is taken because of /tmp/.X11-unix/X$n\n"; |
| 371 | warn "Remove this file if there is no X server $host:$n\n"; |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | if (-e "/usr/spool/sockets/X11/$n") { |
| 376 | warn("\nWarning: $host:$n is taken because of ". |
| 377 | "/usr/spool/sockets/X11/$n\n"); |
| 378 | warn "Remove this file if there is no X server $host:$n\n"; |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | return 1; |
| 383 | } |
| 384 | |
| 385 | |
| 386 | # |
| 387 | # GetXDisplayDefaults uses xdpyinfo to find out the geometry, depth and pixel |
| 388 | # format of the current X display being used. If successful, it sets the |
| 389 | # options as appropriate so that the X VNC server will use the same settings |
| 390 | # (minus an allowance for window manager decorations on the geometry). Using |
| 391 | # the same depth and pixel format means that the VNC server won't have to |
| 392 | # translate pixels when the desktop is being viewed on this X display (for |
| 393 | # TrueColor displays anyway). |
| 394 | # |
| 395 | |
| 396 | sub GetXDisplayDefaults |
| 397 | { |
| 398 | local (@lines, @matchlines, $width, $height, $defaultVisualId, $i, |
| 399 | $red, $green, $blue); |
| 400 | |
| 401 | $wmDecorationWidth = 4; # a guess at typical size for window manager |
| 402 | $wmDecorationHeight = 24; # decoration size |
| 403 | |
| 404 | return if (!defined($ENV{DISPLAY})); |
| 405 | |
| 406 | @lines = `xdpyinfo 2>/dev/null`; |
| 407 | |
| 408 | return if ($? != 0); |
| 409 | |
| 410 | @matchlines = grep(/dimensions/, @lines); |
| 411 | if (@matchlines) { |
| 412 | ($width, $height) = ($matchlines[0] =~ /(\d+)x(\d+) pixels/); |
| 413 | |
| 414 | $width -= $wmDecorationWidth; |
| 415 | $height -= $wmDecorationHeight; |
| 416 | |
| 417 | $geometry = "${width}x$height"; |
| 418 | } |
| 419 | |
| 420 | @matchlines = grep(/default visual id/, @lines); |
| 421 | if (@matchlines) { |
| 422 | ($defaultVisualId) = ($matchlines[0] =~ /id:\s+(\S+)/); |
| 423 | |
| 424 | for ($i = 0; $i < @lines; $i++) { |
| 425 | if ($lines[$i] =~ /^\s*visual id:\s+$defaultVisualId$/) { |
| 426 | if (($lines[$i+1] !~ /TrueColor/) || |
| 427 | ($lines[$i+2] !~ /depth/) || |
| 428 | ($lines[$i+4] !~ /red, green, blue masks/)) |
| 429 | { |
| 430 | return; |
| 431 | } |
| 432 | last; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | return if ($i >= @lines); |
| 437 | |
| 438 | ($depth) = ($lines[$i+2] =~ /depth:\s+(\d+)/); |
| 439 | ($red,$green,$blue) |
| 440 | = ($lines[$i+4] |
| 441 | =~ /masks:\s+0x([0-9a-f]+), 0x([0-9a-f]+), 0x([0-9a-f]+)/); |
| 442 | |
| 443 | $red = hex($red); |
| 444 | $green = hex($green); |
| 445 | $blue = hex($blue); |
| 446 | |
| 447 | if ($red > $blue) { |
| 448 | $red = int(log($red) / log(2)) - int(log($green) / log(2)); |
| 449 | $green = int(log($green) / log(2)) - int(log($blue) / log(2)); |
| 450 | $blue = int(log($blue) / log(2)) + 1; |
| 451 | $pixelformat = "rgb$red$green$blue"; |
| 452 | } else { |
| 453 | $blue = int(log($blue) / log(2)) - int(log($green) / log(2)); |
| 454 | $green = int(log($green) / log(2)) - int(log($red) / log(2)); |
| 455 | $red = int(log($red) / log(2)) + 1; |
| 456 | $pixelformat = "bgr$blue$green$red"; |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | |
| 462 | # |
| 463 | # quotedString returns a string which yields the original string when parsed |
| 464 | # by a shell. |
| 465 | # |
| 466 | |
| 467 | sub quotedString |
| 468 | { |
| 469 | local ($in) = @_; |
| 470 | |
| 471 | $in =~ s/\'/\'\"\'\"\'/g; |
| 472 | |
| 473 | return "'$in'"; |
| 474 | } |
| 475 | |
| 476 | |
| 477 | # |
| 478 | # removeSlashes turns slashes into underscores for use as a file name. |
| 479 | # |
| 480 | |
| 481 | sub removeSlashes |
| 482 | { |
| 483 | local ($in) = @_; |
| 484 | |
| 485 | $in =~ s|/|_|g; |
| 486 | |
| 487 | return "$in"; |
| 488 | } |
| 489 | |
| 490 | |
| 491 | # |
| 492 | # Usage |
| 493 | # |
| 494 | |
| 495 | sub Usage |
| 496 | { |
| 497 | die("\nusage: $prog [:<number>] [-name <desktop-name>] [-depth <depth>]\n". |
| 498 | " [-geometry <width>x<height>]\n". |
| 499 | " [-pixelformat rgbNNN|bgrNNN]\n". |
DRC | eed5d1f | 2009-03-26 19:16:19 +0000 | [diff] [blame] | 500 | " [-fp <font-path>]\n". |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 501 | " <Xvnc-options>...\n\n". |
| 502 | " $prog -kill <X-display>\n\n"); |
| 503 | } |
| 504 | |
| 505 | |
| 506 | # |
| 507 | # Kill |
| 508 | # |
| 509 | |
| 510 | sub Kill |
| 511 | { |
| 512 | $opt{'-kill'} =~ s/(:\d+)\.\d+$/$1/; # e.g. turn :1.0 into :1 |
| 513 | |
| 514 | if ($opt{'-kill'} =~ /^:\d+$/) { |
| 515 | $pidFile = "$vncUserDir/$host$opt{'-kill'}.pid"; |
| 516 | } else { |
| 517 | if ($opt{'-kill'} !~ /^$host:/) { |
| 518 | die "\nCan't tell if $opt{'-kill'} is on $host\n". |
| 519 | "Use -kill :<number> instead\n\n"; |
| 520 | } |
| 521 | $pidFile = "$vncUserDir/$opt{'-kill'}.pid"; |
| 522 | } |
| 523 | |
| 524 | if (! -r $pidFile) { |
| 525 | die "\nCan't find file $pidFile\n". |
| 526 | "You'll have to kill the Xvnc process manually\n\n"; |
| 527 | } |
| 528 | |
| 529 | $SIG{'HUP'} = 'IGNORE'; |
| 530 | chop($pid = `cat $pidFile`); |
| 531 | warn "Killing Xvnc process ID $pid\n"; |
| 532 | system("kill $pid"); |
| 533 | unlink $pidFile; |
| 534 | exit; |
| 535 | } |
| 536 | |
| 537 | |
| 538 | # |
| 539 | # ParseOptions takes a list of possible options and a boolean indicating |
| 540 | # whether the option has a value following, and sets up an associative array |
| 541 | # %opt of the values of the options given on the command line. It removes all |
| 542 | # the arguments it uses from @ARGV and returns them in @optArgs. |
| 543 | # |
| 544 | |
| 545 | sub ParseOptions |
| 546 | { |
| 547 | local (@optval) = @_; |
| 548 | local ($opt, @opts, %valFollows, @newargs); |
| 549 | |
| 550 | while (@optval) { |
| 551 | $opt = shift(@optval); |
| 552 | push(@opts,$opt); |
| 553 | $valFollows{$opt} = shift(@optval); |
| 554 | } |
| 555 | |
| 556 | @optArgs = (); |
| 557 | %opt = (); |
| 558 | |
| 559 | arg: while (defined($arg = shift(@ARGV))) { |
| 560 | foreach $opt (@opts) { |
| 561 | if ($arg eq $opt) { |
| 562 | push(@optArgs, $arg); |
| 563 | if ($valFollows{$opt}) { |
| 564 | if (@ARGV == 0) { |
| 565 | &Usage(); |
| 566 | } |
| 567 | $opt{$opt} = shift(@ARGV); |
| 568 | push(@optArgs, $opt{$opt}); |
| 569 | } else { |
| 570 | $opt{$opt} = 1; |
| 571 | } |
| 572 | next arg; |
| 573 | } |
| 574 | } |
| 575 | push(@newargs,$arg); |
| 576 | } |
| 577 | |
| 578 | @ARGV = @newargs; |
| 579 | } |
| 580 | |
| 581 | |
| 582 | # |
| 583 | # Routine to make sure we're operating in a sane environment. |
| 584 | # |
| 585 | |
| 586 | sub SanityCheck |
| 587 | { |
| 588 | local ($cmd); |
| 589 | |
| 590 | # |
| 591 | # Get the program name |
| 592 | # |
| 593 | |
| 594 | ($prog) = ($0 =~ m|([^/]+)$|); |
| 595 | |
| 596 | # |
| 597 | # Check we have all the commands we'll need on the path. |
| 598 | # |
| 599 | |
| 600 | cmd: |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 601 | foreach $cmd ("uname") { |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 602 | for (split(/:/,$ENV{PATH})) { |
| 603 | if (-x "$_/$cmd") { |
| 604 | next cmd; |
| 605 | } |
| 606 | } |
| 607 | die "$prog: couldn't find \"$cmd\" on your PATH.\n"; |
| 608 | } |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 609 | if (-x "/usr/X11R6/bin/xauth") { |
| 610 | $xauth = "/usr/X11R6/bin/xauth"; |
| 611 | } |
| 612 | else { |
| 613 | cmd1: |
| 614 | foreach $cmd ("xauth") { |
| 615 | for (split(/:/,$ENV{PATH})) { |
| 616 | if (-x "$_/$cmd") { |
| 617 | next cmd1; |
| 618 | } |
| 619 | } |
| 620 | die "$prog: couldn't find \"$cmd\" on your PATH.\n"; |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | if($exedir eq "") { |
| 625 | cmd2: |
| 626 | foreach $cmd ("Xvnc","vncpasswd") { |
| 627 | for (split(/:/,$ENV{PATH})) { |
| 628 | if (-x "$_/$cmd") { |
| 629 | $vncClasses = "$_/../vnc/classes"; |
| 630 | next cmd2; |
| 631 | } |
| 632 | } |
| 633 | die "$prog: couldn't find \"$cmd\" on your PATH.\n"; |
| 634 | } |
| 635 | } |
| 636 | else { |
| 637 | cmd3: |
| 638 | foreach $cmd ($exedir."Xvnc",$exedir."vncpasswd") { |
| 639 | for (split(/:/,$ENV{PATH})) { |
| 640 | if (-x "$cmd") { |
| 641 | $vncClasses = $exedir."../vnc/classes"; |
| 642 | next cmd3; |
| 643 | } |
| 644 | } |
| 645 | die "$prog: couldn't find \"$cmd\".\n"; |
| 646 | } |
| 647 | } |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 648 | |
| 649 | # |
| 650 | # Check the HOME environment variable is set |
| 651 | # |
| 652 | |
| 653 | if (!defined($ENV{HOME})) { |
| 654 | die "$prog: The HOME environment variable is not set.\n"; |
| 655 | } |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 656 | # chdir($ENV{HOME}); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 657 | |
| 658 | # |
| 659 | # Find socket constants. 'use Socket' is a perl5-ism, so we wrap it in an |
| 660 | # eval, and if it fails we try 'require "sys/socket.ph"'. If this fails, |
| 661 | # we just guess at the values. If you find perl moaning here, just |
| 662 | # hard-code the values of AF_INET and SOCK_STREAM. You can find these out |
| 663 | # for your platform by looking in /usr/include/sys/socket.h and related |
| 664 | # files. |
| 665 | # |
| 666 | |
| 667 | chop($os = `uname`); |
| 668 | chop($osrev = `uname -r`); |
| 669 | |
| 670 | eval 'use Socket'; |
| 671 | if ($@) { |
| 672 | eval 'require "sys/socket.ph"'; |
| 673 | if ($@) { |
| 674 | if (($os eq "SunOS") && ($osrev !~ /^4/)) { |
| 675 | $AF_INET = 2; |
| 676 | $SOCK_STREAM = 2; |
| 677 | } else { |
| 678 | $AF_INET = 2; |
| 679 | $SOCK_STREAM = 1; |
| 680 | } |
| 681 | } else { |
| 682 | $AF_INET = &AF_INET; |
| 683 | $SOCK_STREAM = &SOCK_STREAM; |
| 684 | } |
| 685 | } else { |
| 686 | $AF_INET = &AF_INET; |
| 687 | $SOCK_STREAM = &SOCK_STREAM; |
| 688 | } |
| 689 | } |