color.pl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use File::Temp qw( tempfile );
  5. use Time::HiRes qw( usleep sleep time );
  6. use IPC::System::Simple qw( system capture );
  7. # Local modules
  8. use Cwd qw(abs_path);
  9. use File::Basename qw(dirname);
  10. use lib dirname(abs_path($0));
  11. use DMX;
  12. # Config
  13. my $DISPLAY = 1;
  14. my %PROFILES = (
  15. 'HIGH' => 'Epson-High',
  16. 'PLAY' => 'Epson-Theater',
  17. 'LOW' => 'Epson-Theater_Black',
  18. );
  19. # App config
  20. my $DATA_DIR = DMX::dataDir();
  21. my $OUTPUT_FILE = $DATA_DIR . 'COLOR';
  22. my $STATE_SOCK = $OUTPUT_FILE . '.socket';
  23. my $PUSH_TIMEOUT = 20;
  24. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  25. my $DELAY = $PULL_TIMEOUT / 2;
  26. # Prototypes
  27. sub runApplescript($);
  28. sub getProfile($);
  29. sub setProfile($$);
  30. sub profileExists($$);
  31. # Debug
  32. my $DEBUG = 0;
  33. if ($ENV{'DEBUG'}) {
  34. $DEBUG = 1;
  35. }
  36. # Sockets
  37. DMX::stateSocket($STATE_SOCK);
  38. DMX::stateSubscribe($STATE_SOCK);
  39. # State
  40. my $state = 'OFF';
  41. my $stateLast = $state;
  42. my $color = 'OFF';
  43. my $colorLast = $color;
  44. my %exists = ();
  45. my $pushLast = 0;
  46. my $pullLast = time();
  47. my $update = 0;
  48. # Validate our profiles
  49. foreach my $profile (keys(%PROFILES)) {
  50. if (!profileExists($DISPLAY, $PROFILES{$profile})) {
  51. die('Invalid profile: ' . $profile . ' => ' . $PROFILES{$profile} . "\n");
  52. }
  53. }
  54. # Loop forever
  55. while (1) {
  56. # Grab the current color profile
  57. $colorLast = $color;
  58. $color = getProfile($DISPLAY);
  59. # If the color profile has changed, save the state to disk
  60. if ($colorLast ne $color) {
  61. if ($DEBUG) {
  62. print STDERR 'New color profile: ' . $colorLast . ' => ' . $color . "\n";
  63. }
  64. my ($fh, $tmp) = tempfile($OUTPUT_FILE . '.XXXXXXXX', 'UNLINK' => 0);
  65. print $fh $color . "\n";
  66. close($fh);
  67. rename($tmp, $OUTPUT_FILE);
  68. }
  69. # State is calculated; use newState to gather data
  70. my $newState = $state;
  71. # Wait for state updates
  72. my $cmdState = DMX::readState($DELAY, \%exists, undef(), undef());
  73. if (defined($cmdState)) {
  74. $newState = $cmdState;
  75. $pullLast = time();
  76. }
  77. # Die if we don't see regular updates
  78. if (time() - $pullLast > $PULL_TIMEOUT) {
  79. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  80. }
  81. # Calculate the new state
  82. $stateLast = $state;
  83. if ($exists{'PROJECTOR_COLOR'} eq 'DYNAMIC') {
  84. $state = 'HIGH';
  85. } elsif ($exists{'PROJECTOR_COLOR'} eq 'THEATER') {
  86. $state = 'PLAY';
  87. } else {
  88. $state = 'LOW';
  89. }
  90. # Force updates on a periodic basis
  91. if (!$update && time() - $pushLast > $PUSH_TIMEOUT) {
  92. # Not for the color profile
  93. #if ($DEBUG) {
  94. # print STDERR "Forcing periodic update\n";
  95. #}
  96. #$update = 1;
  97. }
  98. # Force updates when there is a physical state mistmatch
  99. if (!$update && $PROFILES{$state} ne $color) {
  100. if ($DEBUG) {
  101. print STDERR 'State mismatch: ' . $color . ' => ' . $PROFILES{$state} . "\n";
  102. }
  103. $update = 1;
  104. }
  105. # Update the color profile
  106. if ($update) {
  107. # Update
  108. if ($DEBUG) {
  109. print STDERR 'Setting output to: ' . $PROFILES{$state} . "\n";
  110. }
  111. setProfile($DISPLAY, $PROFILES{$state});
  112. # Update the push time
  113. $pushLast = time();
  114. # Clear the update flag
  115. $update = 0;
  116. }
  117. }
  118. sub runApplescript($) {
  119. my ($script) = @_;
  120. if ($DEBUG) {
  121. print STDERR 'Running AppleScript: ' . $script . "\n";
  122. }
  123. my $retval = capture('osascript', '-e', $script);
  124. if ($DEBUG) {
  125. print STDERR "\tAppleScript result: " . $retval . "\n";
  126. }
  127. return $retval;
  128. }
  129. sub getProfile($) {
  130. my ($display) = @_;
  131. if ($DEBUG) {
  132. print STDERR "getProfile()\n";
  133. }
  134. # Validate the input
  135. $display = int($display);
  136. if (!$display) {
  137. die('Invalid display ID: ' . $display . "\n");
  138. }
  139. # Ask SwitchResX what color profile is installed
  140. my $profile = runApplescript('tell application "SwitchResX Daemon" to get display profile of display ' . $display);
  141. # Cleanup the name
  142. $profile =~ s/^\s*profile //;
  143. $profile =~ s/\s+$//;
  144. return $profile;
  145. }
  146. sub setProfile($$) {
  147. my ($display, $profile) = @_;
  148. if ($DEBUG) {
  149. print STDERR "setProfile()\n";
  150. }
  151. # Validate the input
  152. $profile =~ s/\"/\\\"/g;
  153. $profile = '"' . $profile . '"';
  154. $display = int($display);
  155. if (!$display) {
  156. die('Invalid display ID: ' . $display . "\n");
  157. }
  158. # Tell SwitchResX to change our color profile
  159. my @cmd = ('tell application "SwitchResX Daemon"');
  160. push(@cmd, 'set prof to profile ' . $profile . ' of display ' . $display);
  161. push(@cmd, 'set display profile of display ' . $display . ' to prof');
  162. push(@cmd, 'end tell');
  163. return runApplescript(join("\n", @cmd));
  164. }
  165. sub profileExists($$) {
  166. my ($display, $profile) = @_;
  167. if ($DEBUG) {
  168. print STDERR "profileExists()\n";
  169. }
  170. # Validate the input
  171. $profile =~ s/\"/\\\"/g;
  172. $profile = '"' . $profile . '"';
  173. $display = int($display);
  174. if (!$display) {
  175. die('Invalid display ID: ' . $display . "\n");
  176. }
  177. # Ask SwitchResX if the named color profile exists
  178. my @cmd = ('tell application "SwitchResX Daemon"');
  179. push(@cmd, 'set profs to get name of every profile of display ' . $display);
  180. push(@cmd, 'get profs contains ' . $profile);
  181. push(@cmd, 'end tell');
  182. my $result = runApplescript(join("\n", @cmd));
  183. if ($result =~ /true/i) {
  184. return 1;
  185. }
  186. return 0;
  187. }