| 1 | package Device::ParallelPort::drv::auto; | ||
| 2 | use strict; | ||
| 3 | use Carp; | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Device::ParallelPort::drv::auto - Automatically choose driver. | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use Device::ParallelPort; | ||
| 12 | my $pp = Device::ParallelPort->new('auto:0'); | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | This module should be used if you do not care what driver is used. | ||
| 17 | It is very handy for writing cross platform applications in that it | ||
| 18 | will autoamtically determine which parallel port driver is appropriate. | ||
| 19 | |||
| 20 | See L<Device::ParallelPort> for full details. | ||
| 21 | |||
| 22 | =head1 DEVELOPMENT | ||
| 23 | |||
| 24 | The current nature of it requires modifications to this module to add new | ||
| 25 | drivers. Longer term it would be better if it tried each driver installed on | ||
| 26 | the system in turn allowing new drivers to add their own interfaces. | ||
| 27 | |||
| 28 | As such, the current version only detects between three drivers. | ||
| 29 | |||
| 30 | * parport - If linux and has a writable /dev/parportX | ||
| 31 | * linux - If linux and running as root (and no parport) | ||
| 32 | * win32 - If running on windows | ||
| 33 | |||
| 34 | NOTE - You MUST have the driver mentioned above loaded on your system for this | ||
| 35 | to work. They are each SEPARATELY available in CPAN. | ||
| 36 | |||
| 37 | =head1 COPYRIGHT | ||
| 38 | |||
| 39 | Copyright (c) 2002,2003,2004 Scott Penrose. All rights reserved. | ||
| 40 | This program is free software; you can redistribute it and/or modify | ||
| 41 | it under the same terms as Perl itself. | ||
| 42 | |||
| 43 | =head1 AUTHOR | ||
| 44 | |||
| 45 | Scott Penrose L<scottp@dd.com.au>, L<http://linux.dd.com.au/> | ||
| 46 | |||
| 47 | =head1 SEE ALSO | ||
| 48 | |||
| 49 | L<Device::ParallelPort> | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | sub new { | ||
| 54 | my ($this, $str, @params) = @_; | ||
| 55 | |||
| 56 | # Try and find us who we are... | ||
| 57 | if ($^O =~ /linux/i) { | ||
| 58 | # Do we have 'parport' support ? | ||
| 59 | if (-e "/dev/parport0" || -e "/dev/ppuser00") { | ||
| 60 | # Check the permissions | ||
| 61 | if ((-w "/dev/parport" . $str) || (-w "/dev/ppuser0" . $str)) { | ||
| 62 | return _load('parport', $str, @params); | ||
| 63 | } else { | ||
| 64 | return _error( | ||
| 65 | "automatically detected linux parport support." | ||
| 66 | . "but unabel to write to /dev/parport$str or /dev/ppuser0$str ." | ||
| 67 | . "Install 'Device::ParallelPort::drv::parport' from CPAN " | ||
| 68 | . "OR Fix access to parport in linux " | ||
| 69 | . "OR use driver 'linux:0' " | ||
| 70 | ); | ||
| 71 | } | ||
| 72 | } else { | ||
| 73 | # Check we are root and report problems if not | ||
| 74 | if ($> == 0) { | ||
| 75 | return _load('linux', $str, @params); | ||
| 76 | } else { | ||
| 77 | return _error( | ||
| 78 | "automatically detected linux support." | ||
| 79 | . "but you are not root. Either use parport support " | ||
| 80 | . "(see Device::ParallelPort::drv::parport) or run as root" | ||
| 81 | ); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | } elsif ($^O =~ /win32/i) { | ||
| 85 | # Load library - that should | ||
| 86 | return _load('win32', $str, @params); | ||
| 87 | } else { | ||
| 88 | _error( | ||
| 89 | "Unable to automatically detect a parllel port\n" | ||
| 90 | . "(currently only auto detect windows and linux drivers).\n" | ||
| 91 | . "You are running $^O" | ||
| 92 | ); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | sub _load { | ||
| 97 | my ($drv, $str, @params) = @_; | ||
| 98 | my $this = undef; | ||
| 99 | eval qq{ | ||
| 100 | use Device::ParallelPort::drv::$drv; | ||
| 101 | \$this = Device::ParallelPort::drv::$drv->new(\$str, \@params); | ||
| 102 | }; | ||
| 103 | if ($@) { | ||
| 104 | _error( | ||
| 105 | "failed to load $drv - $@." | ||
| 106 | . " Make sure you have loaded and installed Device::ParallelPort::drv::$drv" | ||
| 107 | . " from CPAN. Install Device::ParallelPort::drv::(linux|parport|win32) and try again" | ||
| 108 | ); | ||
| 109 | } | ||
| 110 | return $this; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub _error { | ||
| 114 | my ($msg) = @_; | ||
| 115 | croak "Device::ParallelPort::drv::auto (auto detect) error\n$msg"; | ||
| 116 | } | ||
| 117 | |||
| 118 | 1; |