| 1 |
#!/usr/bin/perl -w |
| 2 |
# $Id: async,v 1.4 2013/07/17 19:34:06 tom Exp $ |
| 3 |
|
| 4 |
# |
| 5 |
# This demo demonstrates how to use async functions with Cdk... |
| 6 |
# |
| 7 |
|
| 8 |
use strict; |
| 9 |
|
| 10 |
# |
| 11 |
# Initialize Cdk. |
| 12 |
# |
| 13 |
use Cdk; |
| 14 |
Cdk::init(); |
| 15 |
|
| 16 |
# Set the async function... |
| 17 |
$SIG{'ALRM'} = "littleWeeClock"; |
| 18 |
|
| 19 |
our @listItems; |
| 20 |
|
| 21 |
# Set up the scrolling list. |
| 22 |
setpwent(); |
| 23 |
while ( my $item = getpwent() ) { |
| 24 |
push( @listItems, $item ); |
| 25 |
} |
| 26 |
|
| 27 |
# Get the screen dimensions. |
| 28 |
my ( $rows, $cols ) = Cdk::getCdkScreenDim(); |
| 29 |
|
| 30 |
# Create the scrolling list object. |
| 31 |
my $scroll = new Cdk::Scroll( |
| 32 |
'Title' => "<C></U/5>Pick An Account", |
| 33 |
'Height' => 10, |
| 34 |
'Numbers' => "TRUE", |
| 35 |
'Highlight' => "</B/29>", |
| 36 |
'Width' => 25, |
| 37 |
'List' => \@listItems |
| 38 |
); |
| 39 |
|
| 40 |
# Create the label... |
| 41 |
my $clock = |
| 42 |
new Cdk::Label( 'Message' => ["Current Time: HH:MM:SS"], 'Xpos' => "TOP" ); |
| 43 |
|
| 44 |
# Draw the scrolling window. |
| 45 |
$clock->draw(); |
| 46 |
|
| 47 |
# Set the alarm to go off. |
| 48 |
alarm(1); |
| 49 |
|
| 50 |
# Do this forever. |
| 51 |
for ( ; ; ) { |
| 52 |
|
| 53 |
# Activate the scrolling list. |
| 54 |
my $itemPicked = $scroll->activate(); |
| 55 |
|
| 56 |
# Do we need to exit... |
| 57 |
exit if !defined $itemPicked; |
| 58 |
|
| 59 |
# Get the password info. |
| 60 |
my ( $name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell ) = |
| 61 |
getpwnam( $listItems[$itemPicked] ); |
| 62 |
|
| 63 |
# Display it. |
| 64 |
my $info = [ |
| 65 |
"</B/5>Account Name<!B!5> $name", |
| 66 |
"</B/5>UID <!B!5> $uid", |
| 67 |
"</B/5>GID <!B!5> $gid", |
| 68 |
"</B/5>Directory <!B!5> $dir", |
| 69 |
"</B/5>Shell <!B!5> $shell" |
| 70 |
]; |
| 71 |
popupLabel($info); |
| 72 |
} |
| 73 |
|
| 74 |
# Exit Cdk. |
| 75 |
Cdk::end(); |
| 76 |
|
| 77 |
# |
| 78 |
# This is the async function. |
| 79 |
# |
| 80 |
sub littleWeeClock { |
| 81 |
|
| 82 |
# |
| 83 |
# Turn off the alarm |
| 84 |
# |
| 85 |
$SIG{'ALRM'} = "IGNORE"; |
| 86 |
alarm(0); |
| 87 |
|
| 88 |
# |
| 89 |
# Get the current time/date. |
| 90 |
# |
| 91 |
my ( $sec, $min, $hour ) = ( localtime(time) ); |
| 92 |
my $mesg = sprintf( "%02d:%02d:%02d", $hour, $min, $sec ); |
| 93 |
|
| 94 |
# |
| 95 |
# Add the line to the scrolling window. |
| 96 |
# |
| 97 |
$clock->set( 'Message' => ["Current Time: </B/$sec>$mesg"] ); |
| 98 |
|
| 99 |
# |
| 100 |
# Reset the alarm. |
| 101 |
# |
| 102 |
$SIG{'ALRM'} = "littleWeeClock"; |
| 103 |
alarm(1); |
| 104 |
} |