| 1 | |||
| 2 | $Tk::LockDisplay::VERSION = '1.3'; | ||
| 3 | |||
| 4 | package Tk::LockDisplay; | ||
| 5 | |||
| 6 | # An xlock-like dialog that requires authentication before unlocking the display. | ||
| 7 | # | ||
| 8 | # Stephen.O.Lidie@Lehigh.EDU, Lehigh University Computing Center. 98/08/12 | ||
| 9 | # | ||
| 10 | # This program is free software; you can redistribute it and/or modify it under | ||
| 11 | # the same terms as Perl itself. | ||
| 12 | |||
| 13 | use 5.004; | ||
| 14 | use Carp; | ||
| 15 | use Tk::Toplevel; | ||
| 16 | use strict; | ||
| 17 | use base qw/Tk::Derived Tk::Toplevel/; | ||
| 18 | Construct Tk::Widget 'LockDisplay'; | ||
| 19 | |||
| 20 | sub Lock { | ||
| 21 | |||
| 22 | # Realize the dialog, start the screensaver and snooze timer events, clear the password entry, save the current | ||
| 23 | # focus and grab and set ours, raise the dialog and wait for the password, release our grab, clear timers, hide | ||
| 24 | # the dialog and restore the original focus and grab. Whew. (Of course, no timer stuff unless we're animating.) | ||
| 25 | |||
| 26 | my($self) = @_; | ||
| 27 | |||
| 28 | my $mez = $self->{Configure}{-animation}; | ||
| 29 | unless ($mez eq 'none') { | ||
| 30 | $self->{mid} = $self->repeat($self->{Configure}{-animationinterval} => [$self => 'mesmerize']); | ||
| 31 | $self->{tid} = $self->after($self->{Configure}{-hide} * 1000 => [$self => 'snooze']); | ||
| 32 | } | ||
| 33 | $self->deiconify; | ||
| 34 | $self->waitVisibility; | ||
| 35 | $self->{entry}->delete(0 => 'end'); | ||
| 36 | my $old_focus = $self->focusSave; | ||
| 37 | my $old_grab = $self->grabSave; | ||
| 38 | $self->{entry}->focusForce; | ||
| 39 | $self->grab(-global); | ||
| 40 | $self->raise; | ||
| 41 | $self->waitVariable(\$self->{unlock}); | ||
| 42 | $self->grabRelease; | ||
| 43 | unless ($mez eq 'none') { | ||
| 44 | $self->afterCancel($self->{tid}); | ||
| 45 | $self->afterCancel($self->{mid}); | ||
| 46 | } | ||
| 47 | $self->withdraw; | ||
| 48 | &$old_focus; | ||
| 49 | &$old_grab; | ||
| 50 | |||
| 51 | } # end Lock | ||
| 52 | |||
| 53 | sub Populate { | ||
| 54 | |||
| 55 | # LockDisplay constructor. These are the composite widget instance keys: | ||
| 56 | # | ||
| 57 | # -authenticate => authentication subroutine | ||
| 58 | # user => username | ||
| 59 | # pw => password | ||
| 60 | # unlock => modified when user properly authenticates | ||
| 61 | # | ||
| 62 | # w => display width, pixels | ||
| 63 | # h => display height, pixels | ||
| 64 | # canvas => canvas widget reference | ||
| 65 | # label => label widget reference | ||
| 66 | # entry => entry widget reference | ||
| 67 | # | ||
| 68 | # tid => -hide after() timer id | ||
| 69 | # mid => -animationinterval repeat() id | ||
| 70 | # | ||
| 71 | # plug_init => 1 IFF plugin initialized | ||
| 72 | # -debug => 1 IFF <Double-1> can unlock display | ||
| 73 | |||
| 74 | my($cw, $args) = @_; | ||
| 75 | |||
| 76 | # Disable interactions with the window manager. | ||
| 77 | |||
| 78 | $cw->withdraw; | ||
| 79 | $cw->protocol('WM_DELETE_WINDOW' => sub {}); | ||
| 80 | $cw->overrideredirect(1); | ||
| 81 | |||
| 82 | # Process arguments. | ||
| 83 | |||
| 84 | my $user; | ||
| 85 | if (not $user = getlogin) { | ||
| 86 | if ($^O eq 'MSWin32') { | ||
| 87 | $user = $^O; | ||
| 88 | } else { | ||
| 89 | die "Can't get user name." if not $user = getpwuid($<); | ||
| 90 | } | ||
| 91 | } | ||
| 92 | $cw->{user} = $user; | ||
| 93 | $cw->{-authenticate} = delete $args->{-authenticate}; | ||
| 94 | die "-authenticate callback is improper or missing." unless ref($cw->{-authenticate}) eq 'CODE'; | ||
| 95 | $cw->{-debug} = delete $args->{-debug}; | ||
| 96 | $cw->{-debug} ||= 0; | ||
| 97 | $args->{-animation} ||= 'lines'; | ||
| 98 | $cw->SUPER::Populate($args); | ||
| 99 | |||
| 100 | # Miscellaneous constants. | ||
| 101 | |||
| 102 | my($w, $h) = ($cw->screenwidth, $cw->screenheight); | ||
| 103 | $cw->{w} = $w; | ||
| 104 | $cw->{h} = $h; | ||
| 105 | my $ti = "tklock $Tk::LockDisplay::VERSION"; | ||
| 106 | |||
| 107 | # The canvas/label/entry, et.al. | ||
| 108 | |||
| 109 | my $mez = $args->{-animation}; | ||
| 110 | my $pw; | ||
| 111 | $cw->{pw} = \$pw; | ||
| 112 | my $canvas = $cw->Canvas; | ||
| 113 | $cw->{canvas} = $canvas; | ||
| 114 | my $frame = $canvas->Frame; | ||
| 115 | my $l = $frame->Label(-text => $ti, -font => 'fixed')->grid; | ||
| 116 | $cw->{label} = $l; | ||
| 117 | my $e = $frame->Entry(-textvariable => \$pw, -show => '*', -width => 10)->grid; | ||
| 118 | $cw->{entry} = $e; | ||
| 119 | |||
| 120 | if ($mez eq 'none') { | ||
| 121 | $cw->geometry('+' . int($w/2) . '+' . int($h/2)); | ||
| 122 | $canvas->createWindow(64, 24, -window => $frame); | ||
| 123 | $canvas->configure(-width => 126, -height => 47); | ||
| 124 | } else { | ||
| 125 | $canvas->configure(-width => $w, -height => $h); | ||
| 126 | $canvas->createWindow($w/2, $h/2, -window => $frame); | ||
| 127 | } | ||
| 128 | $canvas->grid; | ||
| 129 | |||
| 130 | # Composite widget parameter definitions. | ||
| 131 | |||
| 132 | $cw->ConfigSpecs( | ||
| 133 | -animation => [qw/PASSIVE animation Animation lines/], | ||
| 134 | -animationinterval => [qw/PASSIVE animationInterval AnimationInterval 200/], | ||
| 135 | -background => [$canvas, qw/background Background black/], | ||
| 136 | -foreground => [$l, qw/ foreground Foreground blue/], | ||
| 137 | -hide => [qw/PASSIVE hide Hide 10/], | ||
| 138 | -text => [qw/METHOD text Text/, $ti], | ||
| 139 | ); | ||
| 140 | |||
| 141 | $cw->{tid} = undef; # timer ID | ||
| 142 | $cw->{mid} = undef; # mesmerizer ID | ||
| 143 | $cw->{unlock} = 1; # unlock flag | ||
| 144 | $cw->{plug_init} = 0; # 0 until plugin initialized | ||
| 145 | |||
| 146 | # Widget bindings. | ||
| 147 | |||
| 148 | $cw->bind('<Motion>' => [\&awake, $cw]); | ||
| 149 | $cw->bind('<Any-Key>' => [\&awake, $cw]); | ||
| 150 | $cw->bind('<Double-1>' => [$cw => 'unlock']) if $cw->{-debug}; | ||
| 151 | |||
| 152 | } # end Populate | ||
| 153 | |||
| 154 | # Private methods. | ||
| 155 | |||
| 156 | sub awake { | ||
| 157 | |||
| 158 | # Make title and password entry visible by moving them from hyperspace to a visible portion of the canvas. | ||
| 159 | |||
| 160 | my($subwidget, $self) = @_; | ||
| 161 | |||
| 162 | my $canvas = $self->{canvas}; | ||
| 163 | unless ($self->{Configure}{-animation} eq 'none') { | ||
| 164 | $canvas->afterCancel($self->{tid}); | ||
| 165 | my($w, $h) = ($self->{w}, $self->{h}); | ||
| 166 | $canvas->coords(1, $w/2, $h/2); | ||
| 167 | $self->{tid} = $canvas->after($self->{Configure}{-hide} * 1000 => [$self => 'snooze']); | ||
| 168 | } | ||
| 169 | |||
| 170 | if (ref($subwidget) eq 'Tk::Entry') { | ||
| 171 | if ($Tk::event->K eq 'Return') { | ||
| 172 | return unless ${$self->{pw}}; | ||
| 173 | if (&{$self->{-authenticate}}($self->{user}, ${$self->{pw}})) { | ||
| 174 | $self->unlock; | ||
| 175 | } else { | ||
| 176 | $self->{entry}->delete(0 => 'end'); | ||
| 177 | $self->bell; | ||
| 178 | } | ||
| 179 | } # ifend <Return> | ||
| 180 | } # ifend Entry widget | ||
| 181 | |||
| 182 | } # end awake | ||
| 183 | |||
| 184 | sub mesmerize { | ||
| 185 | |||
| 186 | # Animate mesmerizer, either user supplied <CODE>, no screensaver, or a builtin plugin. | ||
| 187 | |||
| 188 | my($self) = @_; | ||
| 189 | |||
| 190 | my $canvas = $self->{canvas}; | ||
| 191 | my $mez = $self->{Configure}{-animation}; | ||
| 192 | my $ai = undef; | ||
| 193 | |||
| 194 | if (ref($mez) eq 'CODE') { # user specified mesmerizing routine | ||
| 195 | exit unless &$mez($canvas) >= 1; | ||
| 196 | } elsif ($mez eq 'none') { | ||
| 197 | return; | ||
| 198 | } else { | ||
| 199 | if ($self->{plug_init}) { | ||
| 200 | exit unless &Animation($canvas) >= 1; | ||
| 201 | } else { | ||
| 202 | no strict qw/refs/; | ||
| 203 | unless (eval "require Tk::LockDisplay::$mez") { | ||
| 204 | warn "Couldn't load plugin file '$mez': $@"; | ||
| 205 | exit; | ||
| 206 | } | ||
| 207 | $ai = &Animation($canvas); | ||
| 208 | unless ($ai >= 1) { | ||
| 209 | warn "Plugin '$mez' failed to initialize."; | ||
| 210 | exit; | ||
| 211 | } | ||
| 212 | use strict qw/refs/; | ||
| 213 | if ($ai > 1) { # restart timer event with new cycle value | ||
| 214 | $self->{Configure}{-animationinterval} = $ai; | ||
| 215 | $self->afterCancel($self->{mid}); | ||
| 216 | $self->{mid} = $self->repeat($self->{Configure}{-animationinterval} => [$self => 'mesmerize']); | ||
| 217 | } | ||
| 218 | $self->{plug_init} = 1; | ||
| 219 | } | ||
| 220 | } # end lines | ||
| 221 | $canvas->idletasks; # update() gives deep recursion! | ||
| 222 | |||
| 223 | } # end mesmerize | ||
| 224 | |||
| 225 | sub snooze { | ||
| 226 | |||
| 227 | # Hide title and password entry by moving the items way off the canvas. | ||
| 228 | |||
| 229 | my($self) = @_; | ||
| 230 | |||
| 231 | my $canvas = $self->{canvas}; | ||
| 232 | my $mez = $self->{Configure}{-animation}; | ||
| 233 | $canvas->coords(1, -1000, -1000); | ||
| 234 | |||
| 235 | } # end snooze | ||
| 236 | |||
| 237 | sub text { | ||
| 238 | |||
| 239 | # Set canvas title text. | ||
| 240 | |||
| 241 | my($self, $text) = @_; | ||
| 242 | |||
| 243 | $self->{label}->configure(-text => $text); | ||
| 244 | |||
| 245 | } # end text | ||
| 246 | |||
| 247 | sub unlock {$_[0]->{unlock}++} # alert waitVariable() that we're done | ||
| 248 | |||
| 249 | 1; | ||
| 250 | |||
| 251 | =head1 NAME | ||
| 252 | |||
| 253 | Tk::LockDisplay - Create modal dialog and wait for a password response. | ||
| 254 | |||
| 255 | =for pm Tk/LockDisplay.pm | ||
| 256 | |||
| 257 | =for category Screensavers, Popups and Dialogs | ||
| 258 | |||
| 259 | =head1 SYNOPSIS | ||
| 260 | |||
| 261 | S< >I<$lock> = I<$parent>-E<gt>B<LockDisplay>(I<-option> =E<gt> I<value>, ... ); | ||
| 262 | |||
| 263 | =head1 DESCRIPTION | ||
| 264 | |||
| 265 | This widget fills the display with a screensaver-like animation, makes | ||
| 266 | a global grab and then waits for the user's authentication string, | ||
| 267 | usually their password. Until the password is entered the display | ||
| 268 | cannot be used: window manager commands are ignored, etcetera. Note, X | ||
| 269 | server requests are not blocked. | ||
| 270 | |||
| 271 | Password verification is perforemd via a callback passed during widget | ||
| 272 | creation. | ||
| 273 | |||
| 274 | While waiting for the user to respond, B<LockDisplay> sets a global | ||
| 275 | grab. This prevents the user from interacting with any application in | ||
| 276 | any way except to type characters in the LockDisplay entry box. See | ||
| 277 | the B<Lock()> method. | ||
| 278 | |||
| 279 | The following option/value pairs are supported: | ||
| 280 | |||
| 281 | =over 4 | ||
| 282 | |||
| 283 | =item B<-authenticate> | ||
| 284 | |||
| 285 | Password verification subroutine - it's passed two positional | ||
| 286 | parameters, the username and password, and should return 1 if success, | ||
| 287 | else 0. | ||
| 288 | |||
| 289 | =item B<-animation> | ||
| 290 | |||
| 291 | A string indicating what screensaver plugin to use, or 'none' to | ||
| 292 | disable the screensaver. Supplied plugins are 'lines' (default), 'neko' and | ||
| 293 | 'counter', which reside in the directory .../Tk/LockDisplay. You can | ||
| 294 | drop a plugin of your own in that directory - see the section Plugin | ||
| 295 | Format below for details. You can also supply your own animation | ||
| 296 | subroutine by passing a B<CODE> reference. The subroutine is passed a | ||
| 297 | single parameter, the canvas widget reference, which you can | ||
| 298 | draw upon as you please. | ||
| 299 | |||
| 300 | =item B<-animationinterval> | ||
| 301 | |||
| 302 | The number of milliseconds between calls to the screen saver animation code. | ||
| 303 | Default is 200 milliseconds. Plugins can specifiy their own interval by | ||
| 304 | returning the number of milliseconds (> 1) during initialization. | ||
| 305 | |||
| 306 | =item B<-hide> | ||
| 307 | |||
| 308 | How many seconds of display inactivity before hiding the password | ||
| 309 | entry widget and canvas title text. Default is 10 seconds. | ||
| 310 | |||
| 311 | =item B<-text> | ||
| 312 | |||
| 313 | Title text centered in canvas. | ||
| 314 | |||
| 315 | =item B<-foreground> | ||
| 316 | |||
| 317 | Title text color. | ||
| 318 | |||
| 319 | =item B<-background> | ||
| 320 | |||
| 321 | Canvas color. | ||
| 322 | |||
| 323 | =item B<-debug> | ||
| 324 | |||
| 325 | Set to 1 allows a <Double-1> event to unlock the display. Used while | ||
| 326 | debugging your authentication callback or plugin. | ||
| 327 | |||
| 328 | =back | ||
| 329 | |||
| 330 | =head1 METHODS | ||
| 331 | |||
| 332 | =over 4 | ||
| 333 | |||
| 334 | =item C<$lock-E<gt>B<Lock>;> | ||
| 335 | |||
| 336 | This method locks the display and waits for the user's authentication data. | ||
| 337 | |||
| 338 | =back | ||
| 339 | |||
| 340 | =head1 EXAMPLE | ||
| 341 | |||
| 342 | I<$lock> = I<$mw>-E<gt>B<LockDisplay>(-authenticate =E<gt> \&check_pw); | ||
| 343 | |||
| 344 | sub check_pw { | ||
| 345 | |||
| 346 | # Perform AFS validation unless on Win32. | ||
| 347 | |||
| 348 | my($user, $pw) = @_; | ||
| 349 | |||
| 350 | if ($^O eq 'MSWin32') { | ||
| 351 | ($pw eq $^O) ? exit(0) : return(0); | ||
| 352 | } else { | ||
| 353 | system "/usr/afsws/bin/klog $user " . quotemeta($pw) . " 2> /dev/null"; | ||
| 354 | ($? == 0) ? exit(0) : return(0); | ||
| 355 | } | ||
| 356 | |||
| 357 | } # end check_pw | ||
| 358 | |||
| 359 | =head1 PLUGIN FORMAT | ||
| 360 | |||
| 361 | Refer to the "counter" plugin file .../Tk/LockDisplay/counter.pm for | ||
| 362 | details on the structure of a LockDisplay animation plugin. Basically, | ||
| 363 | you create a ".pm" file that describes your plugin, "counter.pm" for | ||
| 364 | instance. This file must contain a subroutine called Animate($canvas), | ||
| 365 | where $canvas is the canvas widget reference passed to it. | ||
| 366 | |||
| 367 | LockDisplay first require()s your plugin file, and, if that succeeds, | ||
| 368 | calls it once to perform initialization. Animate() should return 0 for | ||
| 369 | failure, 1 for success, and > 1 for success I<and> to specifiy a private | ||
| 370 | -animationinterval (in milliseconds). Subsequent calls to Animate() are | ||
| 371 | for its "main loop" processing. As before, the return code should be | ||
| 372 | 0 or 1 for failure or success, respectively. | ||
| 373 | |||
| 374 | =head1 HISTORY | ||
| 375 | |||
| 376 | =over 4 | ||
| 377 | |||
| 378 | =item Version 1.0 | ||
| 379 | |||
| 380 | Beta Release. | ||
| 381 | |||
| 382 | =item Version 1.1 | ||
| 383 | |||
| 384 | . Implement plugins and other fixes suggested by Achim Bohnet. | ||
| 385 | Thanks! | ||
| 386 | . Allow plugin name 'none' to disable screensaver. Thanks to | ||
| 387 | Roderick Anderson! | ||
| 388 | |||
| 389 | =item Version 1.2 | ||
| 390 | |||
| 391 | . getlogin() fails on HPUX, so try getpwuid() as a fallback. | ||
| 392 | Thanks to Paul Schinder for the CPAN-Testers bug report. | ||
| 393 | . Plugins can return() their own -animationinterval value | ||
| 394 | during preset. | ||
| 395 | . Add 'neko' plugin. | ||
| 396 | |||
| 397 | =item Version 1.3 | ||
| 398 | |||
| 399 | . Fix value of pi in neko plugin! | ||
| 400 | . Add Windows 95 support | ||
| 401 | |||
| 402 | =back | ||
| 403 | |||
| 404 | =head1 AUTHOR | ||
| 405 | |||
| 406 | Stephen.O.Lidie@Lehigh.EDU | ||
| 407 | |||
| 408 | =head1 COPYRIGHT | ||
| 409 | |||
| 410 | Copyright (C) 1998 - 1998, Stephen O. Lidie. | ||
| 411 | |||
| 412 | This program is free software; you can redistribute it and/or modify it under | ||
| 413 | the same terms as Perl itself. | ||
| 414 | |||
| 415 | =head1 KEYWORDS | ||
| 416 | |||
| 417 | screeensaver, dialog, modal | ||
| 418 | |||
| 419 | =cut |