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.

337 lines
11KB

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