A variety of simple automation scripts and enhancements to Sway and i3.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

306 lines
8.1KB

  1. #!/usr/bin/perl -w
  2. # Files containing current and max brightness values
  3. my $cur_file = "/sys/class/backlight/intel_backlight/brightness";
  4. my $max_file = "/sys/class/backlight/intel_backlight/max_brightness";
  5. my $last_file = "/home/jpm/.config/blc.last";
  6. sub to_percent {
  7. my $value = shift;
  8. if ($value eq "Permission Denied") {
  9. return $value;
  10. } else {
  11. $value = int($value/get_max()*100);
  12. return $value . '%';
  13. }
  14. }
  15. sub get_offset {
  16. return int(get_max()/100);
  17. }
  18. sub get_current {
  19. open(my $c,'<',"$cur_file");
  20. my $current = <$c>;
  21. close $c;
  22. chomp $current;
  23. return $current;
  24. }
  25. sub get_max {
  26. open(my $m,'<',"$max_file");
  27. my $max = <$m>;
  28. close $m;
  29. chomp $max;
  30. return $max;
  31. }
  32. sub get_min {
  33. return int((get_max()/100)+2);
  34. }
  35. sub writable {
  36. if (! -w $cur_file) {
  37. return 0;
  38. } else {
  39. return 1;
  40. }
  41. }
  42. sub increment {
  43. if (writable()) {
  44. my $current = get_current();
  45. my $max = get_max();
  46. my $target = $current+get_offset();
  47. if ($target > $max) {
  48. $target = $max;
  49. }
  50. open(my $c,'>',"$cur_file");
  51. print $c $target;
  52. close $c;
  53. return $target;
  54. } else {
  55. return "Permission Denied";
  56. }
  57. }
  58. sub decrement {
  59. if (writable()) {
  60. my $current = get_current();
  61. my $min = get_min();
  62. my $target = $current-get_offset();
  63. if ($target < $min) {
  64. $target = $min;
  65. }
  66. open(my $c,'>',"$cur_file");
  67. print $c $target;
  68. close $c;
  69. return $target;
  70. } else {
  71. return "Permission Denied";
  72. }
  73. }
  74. sub set {
  75. my $value = shift;
  76. if (writable()) {
  77. $current = get_current();
  78. if ($value > $current) {
  79. for (my $i=$current;$i<=$value;$i++) {
  80. open(my $c,'>',"$cur_file");
  81. print $c $i;
  82. close $c;
  83. }
  84. } else {
  85. for (my $i=$current;$i>=$value;$i--) {
  86. open(my $c,'>',"$cur_file");
  87. print $c $i;
  88. close $c;
  89. }
  90. }
  91. return $value;
  92. } else {
  93. return "Permission Denied";
  94. }
  95. }
  96. sub help {
  97. print "
  98. Backlight Control
  99. Usage: blc.pl [--silent|--notify] [OPTION] [VALUE]
  100. With no option the backlight information is printed as JSON.
  101. Output printed to STDOUT unless:
  102. --silent Do not display output (overrides --notify)
  103. --notify Send output to notification daemon. --notify=N will
  104. change the display duration in ms. Default is 200ms
  105. Output option must preceed the first Action option.
  106. Actions:
  107. = VALUE Set backlight to specific value. VALUE greater than
  108. 100 will be treated as absolute value. VALUE eqaul to
  109. or less than 100 will be treated as a percentage
  110. ++ Increment by 1%
  111. += VALUE Increment by VALUE percent
  112. -- Decrement by 1%
  113. -= VALUE Decrement by VALUE percent
  114. Actions corrected to 1% or 100% if over or under. All actions
  115. provide output as a percentage, with the % symbol.
  116. Final percentage printed as below, skipping other options.
  117. Print:
  118. == Print current absolute value
  119. ^ Print maximum absolute value
  120. % Print current percentage (does not include % symbol)
  121. --help Help (not impacted by --silent or --notify)
  122. --HELP Advanced help (same as above)
  123. Any other option will be printed literally";
  124. }
  125. sub advanced {
  126. $current = get_current();
  127. $max = get_max();
  128. print ".
  129. Print functions can be strung together but command will exit
  130. with the first non-print option. eg.
  131. \$ blc.pl == / ^
  132. 21/100
  133. \$ blc.pl == ++
  134. ('==' ignored)
  135. (Backlight incremented)
  136. Escape options with \\ in quotes to display them literally. eg.
  137. \$ blc.pl == / ^ '\\=' % \'\\%\'
  138. " . $current . "/" . $max . "=" . int($current/$max*100) . "%
  139. Only one \'\\' is removed per block. eg.
  140. \$ blc.pl '\\% \\'
  141. % \\
  142. ";
  143. }
  144. my $current = get_current();
  145. my (@output, $target, $silent, $notify);
  146. if (scalar @ARGV) {
  147. for (my $i=0;$i<scalar @ARGV;$i++) {
  148. if ($ARGV[$i] eq '++') {
  149. $target = increment();
  150. @output = to_percent($target);
  151. last;
  152. } elsif ($ARGV[$i] eq '+=') {
  153. for (my $i=0;$i<scalar @ARGV;$i++) {
  154. if ($ARGV[$i] eq '+=') {
  155. if (defined $ARGV[($i+1)]) {
  156. $count = $ARGV[($i+1)];
  157. for (my $j=0;$j<$count;$j++) {
  158. $target = increment();
  159. if ($target eq "Permission Denied") {
  160. last;
  161. }
  162. }
  163. @output = to_percent($target);
  164. } else {
  165. @output = ("No value after $ARGV[$i]");
  166. }
  167. last;
  168. }
  169. }
  170. last;
  171. } elsif ($ARGV[$i] eq '--') {
  172. $target = decrement();
  173. @output = to_percent($target);
  174. last;
  175. } elsif ($ARGV[$i] eq '-=') {
  176. for (my $i=0;$i<scalar @ARGV;$i++) {
  177. if ($ARGV[$i] eq '-=') {
  178. if (defined $ARGV[($i+1)]) {
  179. $count = $ARGV[($i+1)];
  180. for (my $j=0;$j<$count;$j++) {
  181. $target = decrement();
  182. if ($target eq "Permission Denied") {
  183. last;
  184. }
  185. }
  186. @output = to_percent($target);
  187. } else {
  188. @output = ("No value after $ARGV[$i]");
  189. }
  190. last;
  191. }
  192. }
  193. last;
  194. } elsif ($ARGV[$i] eq '=') {
  195. $target = $ARGV[($i+1)];
  196. if (defined $target) {
  197. if ($target < 1) {
  198. $target = set(get_min());
  199. } elsif ($target > get_max()) {
  200. $target = set(get_max());
  201. } elsif ($target > 100) {
  202. $target = set($target);
  203. } else {
  204. $target = set(int((get_max()*$target/100)+1));
  205. }
  206. if ($target eq "Permission Denied") {
  207. @output = $target;
  208. } else {
  209. @output = to_percent($target);
  210. }
  211. last;
  212. } else {
  213. @output = ("No value after $ARGV[$i]");
  214. last;
  215. }
  216. } elsif ($ARGV[$i] eq '%') {
  217. push @output,int(get_current()/get_max()*100);
  218. } elsif ($ARGV[$i] eq '^') {
  219. push @output,get_max();
  220. } elsif ($ARGV[$i] eq '==') {
  221. push @output,get_current();
  222. } elsif ($ARGV[$i] eq '--help') {
  223. help();
  224. print " (see --HELP).\n\n";
  225. exit();
  226. } elsif ($ARGV[$i] eq '--HELP') {
  227. help();
  228. advanced();
  229. exit();
  230. } elsif ($ARGV[$i] eq '--silent') {
  231. $silent = 'TRUE';
  232. } elsif ($ARGV[$i] =~ /^--notify/) {
  233. $notify = 'TRUE';
  234. if ($ARGV[$i] =~ /=[0-9]+$/) {
  235. $duration = $ARGV[$i];
  236. $duration =~ s/.*=([0-9]+)/$1/;
  237. } else {
  238. $duration = 200;
  239. }
  240. } else {
  241. my $add = $ARGV[$i];
  242. $add =~ s/\\//;
  243. push @output,$add;
  244. }
  245. }
  246. } else {
  247. @output = ('{"Backlight":{"Max":"' . get_max() . '","Current":"' . get_current() . '","Percentage","' . int(get_current()/get_max()*100) . '%"}}');
  248. }
  249. open(my $fh,'>',$last_file);
  250. print $fh get_current();
  251. close($fh);
  252. if ($silent) {
  253. exit();
  254. } elsif ($notify) {
  255. # Don't output anything if the value didn't change
  256. if ($current == get_current()) {
  257. exit();
  258. }
  259. my $concat;
  260. foreach (@output) {
  261. $concat .= $_;
  262. }
  263. system "notify-send --urgency=normal -i /usr/share/icons/Papirus-Dark-Grey/48x48/status/notification-display-brightness.svg -t $duration \"$concat\"";
  264. exit();
  265. } else {
  266. print foreach @output;
  267. print "\n";
  268. exit();
  269. }