Guest User

presentation_workspace.sh

a guest
Oct 4th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.12 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Names of the displays you want to use for presentation as well as mirroring of said presentation screen.
  4. # They can be easily read from arandr, or listed using `xrandr --listmonitors`
  5. PRESENTATION_DISPLAY="DP-1"
  6. MIRROR_DISPLAY="DVI-I-1"
  7.  
  8. # These variables depend on how you arranged your screens using arandr/xrandr.
  9. # To Verify/Correct them, open arandr, arrange your screens as you prefer, export the command
  10. # and then read the "--pos" value for the desired mirror and presentation output/display.
  11. MIRROR_ORIG_UPPER_LEFT="1920x1392"
  12. PRESENTATION_UPPER_LEFT="0x0"
  13.  
  14. # Space-separated list of workspaces you bound to the presentation display/output
  15. # using `workspace <num:name> output <display>`, as you are referring to them in the i3 config
  16. PRESENTATION_WORKSPACES='11:TV'
  17.  
  18.  
  19. TARGET_WORKSPACE=$1
  20.  
  21. # The below command automates extraction of the output resolution. However, it takes too long due to long xrandr response times.
  22. # Therefore, reading the resolution once (using the command) and then manually writing it into this variable is probably preferrable.
  23. # Alternatively, you could also setup your presentation display with --mode 1920x1080 using xrandr so the resultion I put below is correct from the get-go (assuming the display supports that high resolution).
  24. # presentation_output_resolution=$(xrandr | grep "$PRESENTATION_DISPLAY" | cut -d' ' -f3 | cut -d'+' -f1)
  25. presentation_output_resolution="1920x1080"
  26.  
  27.  
  28. # Underlying Idea: Overlay mirror display onto presentation display and scale it as necessary whenever presentation workspace is chosen
  29. RESTORE_COMMAND="xrandr --output $MIRROR_DISPLAY --pos $MIRROR_ORIG_UPPER_LEFT --scale 1"
  30. MIRROR_COMMAND="xrandr --output $MIRROR_DISPLAY --pos $PRESENTATION_UPPER_LEFT --scale-from $presentation_output_resolution"
  31.  
  32. focused_workspace=$(i3-msg -t get_workspaces | python3 -c "import sys,json
  33. for workspace in json.load(sys.stdin):
  34.    if workspace['focused']:
  35.        print(workspace['name'])
  36.        break")
  37.  
  38.  
  39. if [[ $TARGET_WORKSPACE == $focused_workspace ]]; then
  40.     exit 0 # Don't do anything when targeting already focused workspace. Workspace back-and-forth wouldn't be possible, since we don't know whether this sends us back to a normal or a presentation workspace.
  41.     # TODO: Idea: Read out workspace back_and_forth target, set this as the new target workspace and continue script with that
  42. fi
  43.  
  44. [[ " $PRESENTATION_WORKSPACES " =~ .*\ $focused_workspace\ .* ]]
  45. already_on_presentation=$? # exit status (i.e. here truth value) of previous command/conditional expression
  46.  
  47.  
  48. if [[ " $PRESENTATION_WORKSPACES " =~ .*\ $TARGET_WORKSPACE\ .* ]]; then # Check if targeted workspace is a presentation workspace. Taken from https://stackoverflow.com/a/46564084
  49.     if [[ $already_on_presentation == 1 ]]; then
  50.         # We are coming from a normal workspace. Setup screen before switching to new workspace.
  51.         eval $MIRROR_COMMAND
  52.     fi
  53.     i3-msg workspace $TARGET_WORKSPACE
  54. else
  55.     # We are targetting a normal workspace
  56.     if [[ $already_on_presentation == 0 ]]; then
  57.         eval $RESTORE_COMMAND
  58.     fi
  59.     i3-msg workspace $TARGET_WORKSPACE
  60. fi
Add Comment
Please, Sign In to add comment