| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # | ||
| 4 | # Author: Slaven Rezic | ||
| 5 | # | ||
| 6 | # Copyright (C) 2003,2004,2012 Slaven Rezic. All rights reserved. | ||
| 7 | # This program is free software; you can redistribute it and/or | ||
| 8 | # modify it under the same terms as Perl itself. | ||
| 9 | # | ||
| 10 | # Mail: slaven@rezic.de | ||
| 11 | # WWW: http://www.rezic.de/eserte/ | ||
| 12 | # | ||
| 13 | |||
| 14 | package Tk::Pod::Util; | ||
| 15 | use strict; | ||
| 16 | use vars qw($VERSION @EXPORT_OK); | ||
| 17 | $VERSION = '5.05'; | ||
| 18 | |||
| 19 | use base qw(Exporter); | ||
| 20 | @EXPORT_OK = qw(is_in_path is_interactive detect_window_manager start_browser); | ||
| 21 | |||
| 22 | # REPO BEGIN | ||
| 23 | # REPO NAME is_in_path /home/e/eserte/src/repository | ||
| 24 | # REPO MD5 1b42243230d92021e6c361e37c9771d1 | ||
| 25 | |||
| 26 | sub is_in_path { | ||
| 27 | my($prog) = @_; | ||
| 28 | require Config; | ||
| 29 | my $sep = $Config::Config{'path_sep'} || ':'; | ||
| 30 | foreach (split(/$sep/o, $ENV{PATH})) { | ||
| 31 | if ($^O eq 'MSWin32') { | ||
| 32 | return "$_\\$prog" | ||
| 33 | if (-x "$_\\$prog.bat" || | ||
| 34 | -x "$_\\$prog.com" || | ||
| 35 | -x "$_\\$prog.exe" || | ||
| 36 | -x "$_\\$prog.cmd" | ||
| 37 | ); | ||
| 38 | } else { | ||
| 39 | return "$_/$prog" if (-x "$_/$prog" && !-d "$_/$prog"); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | undef; | ||
| 43 | } | ||
| 44 | # REPO END | ||
| 45 | |||
| 46 | sub is_interactive { | ||
| 47 | if ($^O eq 'MSWin32' || !eval { require POSIX; 1 }) { | ||
| 48 | # fallback | ||
| 49 | return -t STDIN && -t STDOUT; | ||
| 50 | } | ||
| 51 | |||
| 52 | # from perlfaq8 (with glitches) | ||
| 53 | open(TTY, "/dev/tty") or return 0; | ||
| 54 | my $tpgrp = POSIX::tcgetpgrp(fileno(*TTY)); | ||
| 55 | my $pgrp = getpgrp(); | ||
| 56 | if ($tpgrp == $pgrp) { | ||
| 57 | 1; | ||
| 58 | } else { | ||
| 59 | 0; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | sub detect_window_manager { | ||
| 64 | my $top = shift; | ||
| 65 | if ($Tk::platform eq 'MSWin32') { | ||
| 66 | return "win32"; | ||
| 67 | } | ||
| 68 | if ( get_property($top, "GNOME_NAME_SERVER")) { | ||
| 69 | return "gnome"; | ||
| 70 | } | ||
| 71 | if ( get_property($top, "KWM_RUNNING") # KDE 1 | ||
| 72 | || get_property($top, "KWIN_RUNNING") # KDE 2 | ||
| 73 | ) { | ||
| 74 | return "kde"; | ||
| 75 | } | ||
| 76 | "x11"; # generic X11 window manager | ||
| 77 | } | ||
| 78 | |||
| 79 | sub get_property { | ||
| 80 | my($top, $prop) = @_; | ||
| 81 | my @ret; | ||
| 82 | if ($top->property('exists', $prop, 'root')) { | ||
| 83 | @ret = $top->property('get', $prop, 'root'); | ||
| 84 | shift @ret; # get rid of property name | ||
| 85 | } | ||
| 86 | @ret; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub start_browser { | ||
| 90 | my($url) = @_; | ||
| 91 | |||
| 92 | if (!defined &Tk::Pod::WWWBrowser::start_browser && !eval { require Tk::Pod::WWWBrowser }) { | ||
| 93 | *Tk::Pod::WWWBrowser::start_browser = sub { | ||
| 94 | my $url = shift; | ||
| 95 | if ($^O eq 'MSWin32') { | ||
| 96 | system(qq{start explorer "$url"}); | ||
| 97 | } elsif ($^O eq 'cygwin') { | ||
| 98 | system(qq{explorer "$url" &}); | ||
| 99 | } elsif (is_in_path("firefox")) { | ||
| 100 | system(qq{firefox "$url" &}); | ||
| 101 | } else { # last fallback | ||
| 102 | system(qq{mozilla "$url" &}); | ||
| 103 | } | ||
| 104 | }; | ||
| 105 | } | ||
| 106 | |||
| 107 | Tk::Pod::WWWBrowser::start_browser($url); | ||
| 108 | } | ||
| 109 | |||
| 110 | 1; | ||
| 111 | |||
| 112 | __END__ |