/tags/v2-72/mh/code/common/tk_photos.pl

# · Perl · 132 lines · 87 code · 31 blank · 14 comment · 21 complexity · fa3cc57a5c23be9e5a7fee67ec614114 MD5 · raw file

  1. # Category = Entertainment
  2. #@ This script displays a fullscreen photo slideshow on the local monitor.
  3. #@ It is particularly nice if your computer is hooked up to a TV.
  4. =begin comment
  5. This script displays a fullscreen photo slideshow on the local monitor.
  6. It is particularly nice if your computer is hooked up to a TV.
  7. It will not work if Misterhouse is started with the Tk option turned off.
  8. For a web browser option, see mh/web/misc/photos.shtml
  9. More info on creating the photo list can be found
  10. in mh/code/test/photo_index.pl
  11. See mh/bin/mh.ini for the various photo_* parms
  12. 03/30/2002 Created by David Norwood and Bruce Winter
  13. =cut
  14. use Tk::JPEG;
  15. $photo_slideshow = new Voice_Cmd '[Start,Stop,Next,Previous] the photo slideshow';
  16. $photo_slideshow_timer = new Timer;
  17. $photo_slideshow_process = new Process_Item;
  18. use vars '@photos'; # This will be persistent across passes and code reloads
  19. if ($Reload) {
  20. @photos = file_read $config_parms{photo_index} unless @photos;
  21. $config_parms{photo_time} = 60 unless defined $config_parms{photo_time};
  22. }
  23. my ($mw_photo, $mw_photo_label, $mw_photo_image);
  24. if (said $photo_slideshow eq 'Start') {
  25. # Create a full screen, frameless window
  26. $mw_photo = $MW->Toplevel(-bg => "black");
  27. $mw_photo->overrideredirect(1);
  28. geometry $mw_photo $MW->screenwidth . "x" . $MW->screenheight . "+0+0";
  29. $mw_photo -> bind("<q>", sub { run_voice_cmd 'Stop the photo slideshow' });
  30. $mw_photo -> bind("<n>", sub { &photo_change('next') });
  31. $mw_photo -> bind("<p>", sub { &photo_change('previous') });
  32. $mw_photo_label = $mw_photo -> Label(-border => 0) ->
  33. pack(-anchor => 'center', -expand => 1);
  34. &photo_change('next');
  35. }
  36. if (said $photo_slideshow eq 'Stop') {
  37. $mw_photo -> destroy if $mw_photo;
  38. $mw_photo_image -> delete if $mw_photo_image;
  39. undef $mw_photo_image;
  40. unset $photo_slideshow_timer;
  41. }
  42. if (said $photo_slideshow eq 'Next') {
  43. &photo_change('next');
  44. }
  45. if (said $photo_slideshow eq 'Previous') {
  46. &photo_change('previous');
  47. }
  48. &photo_change('next') if expired $photo_slideshow_timer;
  49. my $photo_temp_file = "$config_parms{data_dir}/mh_temp.tk_photo.jpg";
  50. sub photo_change {
  51. my ($mode) = @_;
  52. return unless $mw_photo; # If mh restared, timer will still trigger
  53. my $i = $Save{photo_index};
  54. ($mode eq 'previous') ? $i-- : $i++;
  55. $i = 0 if $i > $#photos;
  56. $i = $#photos if $i < 0;
  57. $Save{photo_index} = $i;
  58. # Find real path to the photo
  59. my ($jpeg) = &http_get_local_file($photos[$i]);
  60. # Optionally resize the photo to full screen
  61. # Do it with a process, as it can take seconds
  62. if ($config_parms{photo_resize}) {
  63. my $size = $MW->screenwidth . "x" . $MW->screenheight;
  64. set $photo_slideshow_process qq|image_resize --size "$size" --file_in "$jpeg" --file_out "$photo_temp_file"|;
  65. start $photo_slideshow_process;
  66. }
  67. # This on-the-fly resize option causes mh to pause
  68. elsif (0) {
  69. # use Image::Magick;
  70. my $img = new Image::Magick;
  71. my $rc;
  72. warn($rc) if $rc = $img->Read($jpeg);
  73. # This step takes 1-2 seconds
  74. warn($rc) if $rc = $img->Scale(geometry => '1280x1024');
  75. warn($rc) if $rc = $img->Write($photo_temp_file);
  76. &photo_insert($photo_temp_file);
  77. }
  78. else {
  79. &photo_insert($jpeg);
  80. }
  81. }
  82. &photo_insert($photo_temp_file) if done_now $photo_slideshow_process;
  83. sub photo_insert {
  84. my ($jpeg) = @_;
  85. # Use delete on photo (destroy does not free up the memory?)
  86. # $mw_photo_image -> destroy if $mw_photo_image;
  87. $mw_photo_image -> delete if $mw_photo_image;
  88. $mw_photo_image = $mw_photo -> Photo(-file => $jpeg);
  89. # This step takes 1-2 seconds for large images :(
  90. $mw_photo_label->configure(-image => $mw_photo_image);
  91. # Sync up with web based photo viewers
  92. my $time = $config_parms{photo_time};
  93. my $time_diff = $Time - $Save{photo_index_time};
  94. if ($time_diff > 5 and $time_diff < $time - 5) {
  95. $time = $time - $time_diff;
  96. }
  97. else {
  98. $Save{photo_index_time} = $Time;
  99. }
  100. set $photo_slideshow_timer $time;
  101. }