keyindicator 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/perl
  2. #
  3. # Copyright 2014 Marcelo Cerri <mhcerri at gmail dot com>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. use strict;
  18. use warnings;
  19. use utf8;
  20. use Getopt::Long;
  21. use File::Basename;
  22. # Default values
  23. my $indicator = $ENV{BLOCK_INSTANCE} || "CAPS";
  24. my $color_on = "#00FF00";
  25. my $color_off = "#222222";
  26. sub help {
  27. my $program = basename($0);
  28. printf "Usage: %s [-c <color on>] [-C <color off>]\n", $program;
  29. printf " -c <color on>: hex color to use when indicator is on\n";
  30. printf " -C <color off>: hex color to use when indicator is off\n";
  31. printf "\n";
  32. printf "Note: environment variable \$BLOCK_INSTANCE should be one of:\n";
  33. printf " CAPS, NUM (default is CAPS).\n";
  34. exit 0;
  35. }
  36. Getopt::Long::config qw(no_ignore_case);
  37. GetOptions("help|h" => \&help,
  38. "c=s" => \$color_on,
  39. "C=s" => \$color_off) or exit 1;
  40. # Key mapping
  41. my %indicators = (
  42. CAPS => 0x00000001,
  43. NUM => 0x00000002,
  44. );
  45. # Retrieve key flags
  46. my $mask = 0;
  47. open(XSET, "xset -q |") or die;
  48. while (<XSET>) {
  49. if (/LED mask:\s*([0-9]+)/) {
  50. $mask = $1;
  51. last;
  52. }
  53. }
  54. close(XSET);
  55. # Output
  56. printf "%s\n", $indicator;
  57. printf "%s\n", $indicator;
  58. if (($indicators{$indicator} || 0) & $mask) {
  59. printf "%s\n", $color_on;
  60. } else {
  61. printf "%s\n", $color_off;
  62. }
  63. exit 0