Sunday, May 5, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 2029  / 1 Year ago, sun, february 12, 2023, 7:39:29

I'm using i3-wm on my ubuntu laptop. My screen size is 3200x1800. Which is great for something but horrible for other things. (Netflix vs gvim).



I'd like to change the resolution of one of my work-spaces, or multiple ones. But I have no idea if that is even possible... can I use compton to do this or maybe another program?


More From » unity

 Answers
4

1. Assuming you are on Unity



The script below should change your resolution, depending on the current viewport in Unity. I tested it on several computers for a while, and it ran without an error.



I'd suggest however to also test it for a while and see if it runs without a single break; repeated wmctrl commands can sometimes exit "non-zero" incidentally. If so, we need to build in a try / except.



Notes




  • I tested it on a single-monitor setup. multiple monitors would probably need another way of parsing the output of xrandr.

  • In the head of the script, you need to set the desired resolution for each of your viewports, I set it like mentioned in your comment, but you can change it anytime. Use the format:



    resolutions = [[<horizontal>, <vertical], [<horizontal>, <vertical], ......]


    like it shows in the script.


  • No need to say that you should use supported resolutions, as in the output of xrandr for your monitor.



How to use




  1. The script needs wmctrl:



    sudo apt-get install wmctrl

  2. copy the script below into an empty file, save it as screen_res.py


  3. Test run it for a while in a terminal window by the command:



    python3 screen_res.py

  4. If all works fine, add it to your startup applications: Dash > Startup Applications > Add...




The script



#!/usr/bin/env python3
import subprocess
import time

# list of resolution per viewport, for each viewport a separate [hor, vert]
# I pre- entered your viewports. quite some! listing takes more space than the script :)
resolutions = [
[3200, 1800],
[3200, 1800],
[3200, 1800],
[3200, 1800],
[3200, 1800],
[3200, 1800],
[3200, 1800],
[1920, 1080],
[1920, 1080],
[1920, 1080],
]

def get_xr():
return subprocess.check_output(["xrandr"]).decode("utf-8").split()

check = get_xr()
plus = 2 if check[check.index("connected")+1] == "primary" else 1

while True:
# resolution:
xr = get_xr()
res = [int(n) for n in xr[xr.index("connected")+plus].split("+")[0].split("x")]
# get current viewport
vp_data = subprocess.check_output(["wmctrl", "-d"]).decode("utf-8").split()
dt = [int(n) for n in vp_data[3].split("x")]
cols = int(dt[0]/res[0])
curr_vpdata = [int(n) for n in vp_data[5].split(",")]
curr_col = int(curr_vpdata[0]/res[0])+1; curr_row = int(curr_vpdata[1]/res[1])
curr_vp = curr_col+curr_row*cols
# check and change resolution if needed
if res != resolutions[curr_vp-1]:
new_res = ("x").join([str(n) for n in resolutions[curr_vp-1]])
subprocess.call(["xrandr", "-s", new_res])
time.sleep(1)


Explanation




  • In a loop, the script first checks the screen's current resolution from the command:



    xrandr

  • Subsequently, the script checks the total desktop size (all viewports) from the command:



    wmctrl -d

  • Then, from the resolution i.c.w. the total desktop size, it calculates the number of columns, which is equal to the total desktop size, divided by the horizontal resolution.


  • Also in the output ofwmctrl -d is the position of the current viewport on the spanning desktop: e.g.: VP: 1680,0.

  • With this information we can conclude on which column & row we are, and check if the currently set resolution matches the resolution as we defined it in the list in the head section of the script.

    If not, the script gives the command to change the resolution with the command:



    xrandr -s <xres>x<yres>



2. XFCE version




  • set up like the version above (also needs wmctrl)



#!/usr/bin/env python3
import subprocess
import time

# list of resolution per viewport, for each viewport a separate [hor, vert]
# below just an example! set resolutions for your own screen
resolutions = [
[1280, 1024],
[1280, 960],
[1280, 1024],
[1280, 1024],
]

def get_xr():
return subprocess.check_output(["xrandr"]).decode("utf-8").split()

check = get_xr()
plus = 2 if check[check.index("connected")+1] == "primary" else 1

while True:
# resolution:
xr = get_xr()
res = [int(n) for n in xr[xr.index("connected")+plus].split("+")[0].split("x")]
# get current workspace
vp_data = subprocess.check_output(["wmctrl", "-d"]).decode("utf-8").splitlines()
curr_ws = int([l.split()[0] for l in vp_data if "*" in l][0])
# check and change resolution if needed
if res != resolutions[curr_ws]:
new_res = ("x").join([str(n) for n in resolutions[curr_ws]])
subprocess.call(["xrandr", "-s", new_res])
time.sleep(1)

[#21263] Tuesday, February 14, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dresuitable

Total Points: 69
Total Questions: 116
Total Answers: 122

Location: Honduras
Member since Sun, Dec 26, 2021
2 Years ago
;