def display_count(self): res = randr.get_screen_resources(self.root) n = 0 for i in res.outputs: o = randr.get_output_info(self.root, i, config_timestamp=0) if o.modes: # has modes, empty if there's no monitor connected here n += 1 return n
def get_connected_outputs(i_screen=0): "Yield the X11-index for each connected monitor" with get_window(i_screen) as window: res = randr.get_screen_resources(window) outputs = res.outputs for output in outputs: info = randr.get_output_info(window, output, 0) if info.connection == 0: yield output
def get_screen_count(): count = 0 d = display.Display() s = d.screen() window = s.root.create_window(0, 0, 1, 1, 1, s.root_depth) res = randr.get_screen_resources(window) for id in res.outputs: output = randr.get_output_info(window, id, 0) count += output.connection ^ 1 # connection = 0 means display active window.destroy() return count
def get_outputs(d): # based on refs 1 and 2 global info screen = d.get_default_screen() info = d.screen(screen) window = info.root res = randr.get_screen_resources(window) outputs = {} for output in res.outputs: params = d.xrandr_get_output_info(output, res.config_timestamp) if params.crtc: outputs[params.name] = get_output(d, params.crtc) return outputs
def get_monitors(): resources = get_screen_resources(display.screen().root) monitors = {} for crtc in resources.crtcs: crtc_info = display.xrandr_get_crtc_info( crtc, resources.config_timestamp) for output_id in crtc_info.outputs: monitor = Monitor(output_id, crtc_info.x, crtc_info.y, crtc_info.width, crtc_info.height) monitors[output_id] = monitor output_ids = resources.outputs for monitor in monitors.values(): output_info = get_output_info(display.screen().root, monitor.id, 0) monitor.name = output_info.name return monitors.values()
def __init__(self, configManager): self.configManager = configManager self.display = display.Display() self.screen = self.display.screen( ) # Screen as an X11 screen, not the actual output self.window = self.screen.root.create_window(0, 0, 1, 1, 1, self.screen.root_depth) outputs = randr.get_screen_resources(self.window).outputs self.logger.debug('Found [{}] outputs available'.format(len(outputs))) # Detect the outputs self.outputs = [] for output in outputs: outInfo = randr.get_output_info(self.window, output, 0) if outInfo.connection == 0: self.logger.debug('Output [{}] is connected'.format( outInfo.name)) if outInfo.crtc > 0: self.logger.debug('Output [{}] is rendered'.format( outInfo.name)) edid = self.__getEDID(output) if edid is None: self.logger.info( 'Output [{}] cannot be identified, skipping ...'. format(outInfo.name)) else: crtc = self.__getCRTC(outInfo) self.outputs.append({ 'output': output, 'info': outInfo, 'crtc': crtc, 'meta': { 'edid': edid, 'name': outInfo.name, 'height': crtc.height, 'width': crtc.width, 'crtc': outInfo.crtc, 'x': crtc.x, 'y': crtc.y, 'mode': crtc.mode, 'rotation': crtc.rotation } })
def mouseToJoy(): # initialize node rospy.init_node('mouseToJoy', anonymous=True) #### Setup MouseToJoy Publisher mouseToJoyPublisher = rospy.Publisher("joy", Joy, queue_size=5) rate = rospy.Rate(10) # 10hz msg = Joy() while not rospy.is_shutdown(): #### Initialize joy msg every loop msg.axes = [] msg.buttons = [] pos_x = 0.0 pos_y = 0.0 #### Get Display Dependent Parameters d = display.Display() screen = d.screen() window = screen.root.create_window(0, 0, 1, 1, 1, screen.root_depth) res = randr.get_screen_resources(window) resolution_x = res.modes[0].width # i.e. 1920 resolution_y = res.modes[0].height # i.e. 1080 middlePoint_x = resolution_x / 2.0 middlePoint_y = resolution_y / 2.0 #### Start Getting Postion of Mouse MouseData = display.Display().screen().root.query_pointer()._data pos_x = MouseData["root_x"] pos_y = MouseData["root_y"] #### Start Mapping from Mouse Position to Joy vel_linear = (pos_y - middlePoint_y) / resolution_y * (2) vel_angular = (pos_x - middlePoint_x) / resolution_x * (2) msg.axes.append(vel_angular) msg.axes.append(vel_linear) msg.axes.append(vel_linear) #### Publish msg rospy.loginfo([pos_x, pos_y]) rospy.loginfo(msg) mouseToJoyPublisher.publish(msg)
def get_display_info(): d = display.Display(':0') screen_count = d.screen_count() default_screen = d.get_default_screen() result = {} screen = 0 info = d.screen(screen) window = info.root res = randr.get_screen_resources(window) for output in res.outputs: params = d.xrandr_get_output_info(output, res.config_timestamp) if not params.crtc: continue crtc = d.xrandr_get_crtc_info(params.crtc, res.config_timestamp) modes = set() kind = "" ratio_w = "" ratio_h = "" if crtc.width >= 2560: kind = "ultra" else: kind = "normal" if kind == "normal": ratio_w = "16" ratio_h = "9" else: ratio_w = "21" ratio_h = "9" result[params.name] = { 'name': params.name, 'kind': kind, 'resolution': "{}x{}".format(crtc.width, crtc.height), 'width': crtc.width, 'height': crtc.height, 'ratio_w': ratio_w, 'ratio_h': ratio_h } return result
def get_monitors(): """ Checks xrandr for connected outputs, finds their position and dimensions, queries bspwm for list of available desktops. """ _display = display.Display() _s = _display.screen() _w = _s.root.create_window(0, 0, 1, 1, 1, _s.root_depth) _resources = randr.get_screen_resources(_w)._data def monitor_info(crtc): _crt = _display.xrandr_get_crtc_info(crtc, _resources["config_timestamp"])._data if len(_crt["outputs"]) == 1: _info = _display.xrandr_get_output_info( _crt["outputs"][0], _resources["config_timestamp"] )._data _desktops = subprocess.run( ["/usr/bin/bspc", "query", "-D", "-m", _info["name"]], universal_newlines=True, stdout=subprocess.PIPE ).stdout.strip() _current = subprocess.run( ["/usr/bin/bspc", "query", "-D", "-m", _info["name"], "-d", ".focused"], universal_newlines=True, stdout=subprocess.PIPE ).stdout.strip() _bspc_id = subprocess.run( ["/usr/bin/bspc", "query", "-M", "-m", _info["name"]], universal_newlines=True, stdout=subprocess.PIPE ).stdout.strip() return { "name": _info["name"], "monitor_id": _bspc_id, "x": _crt["x"], "width": _crt["width"], "desktops": list(filter(None, _desktops.split("\n"))), "current": _current } return None return list(filter(None, list(map(monitor_info, _resources["crtcs"]))))
def build_menu(): d = display.Display() s = d.screen() screen_res = s.root.get_geometry() current_resolution = (screen_res.width, screen_res.height) window = s.root.create_window(0, 0, 1, 1, 1, s.root_depth) res = randr.get_screen_resources(window) for mode in res.modes: w, h = mode.width, mode.height if abs(1 - (w / h) / (16 / 9)) < 0.01 and w >= 1360: resolutions[(w, h)] = current_resolution == (w, h) menu = gtk.Menu() for res in sorted(resolutions): res_button = gtk.MenuItem('%d x %d' % res) res_button.connect('activate', set_resoltuion, res, window) menu.append(res_button) button_quit = gtk.MenuItem('Quit') button_quit.connect('activate', quit) menu.append(button_quit) menu.show_all() return menu
def get_display_info(name: str = ":0") -> tp.Dict[str, tp.Tuple[int, int]]: """Adapted from https://stackoverflow.com/a/64502961/ """ d = display.Display(name) # screen_count = d.screen_count() default_screen = d.get_default_screen() monitors = {} info = d.screen(default_screen) window = info.root res = randr.get_screen_resources(window) # print(res) for output in res.outputs: params = d.xrandr_get_output_info(output, res.config_timestamp) # print(params) if not params.crtc: continue crtc = d.xrandr_get_crtc_info(params.crtc, res.config_timestamp) # Available modes modes = {find_mode(mode, res.modes) for mode in params.modes} monitors[params.name] = (crtc.width, crtc.height) return monitors
# sudo apt install python3-xlib from Xlib import X, display from Xlib.ext import randr d = display.Display() s = d.screen() window = s.root.create_window(0, 0, 1, 1, 1, s.root_depth) res = randr.get_screen_resources(window) import IPython IPython.embed() for mode in res.modes: w, h = mode.width, mode.height print("Width: {}, height: {}".format(w, h))
def _get_modes(self): with get_window() as win: res = randr.get_screen_resources(win) modes = res._data['modes'] modes = [x._data for x in modes] return modes
def get_randr_outputs(d=None, window=None): '''Return all randr outputs as a list. Outputs are represented by a dictionary with at least the ``name``, ``width``, ``height``, ``primary``, ``x`` and ``y`` keys. ''' from Xlib import X, display from Xlib.ext import randr d = d or display.Display() s = d.screen() if not window: window = s.root.create_window(0, 0, 1, 1, 1, s.root_depth) try: ress = randr.get_screen_resources(window) outputs = ress.outputs primary = randr.get_output_primary(window).output npos = 0 modes = {} for mode in ress.modes: data = mode._data data['name'] = ress.mode_names[npos:npos + mode.name_length] npos += data['name_length'] modes[data['id']] = data outputs = [(o, d.xrandr_get_output_info(o, 0)) for o in outputs] outputs = [{ 'name': o[1].name, 'crtc_id': o[1].crtc, 'crtc': d.xrandr_get_crtc_info(o[1].crtc, 0) if o[1].crtc else None, 'primary': 'primary' if o[0] == primary else None, 'connection': o[1].connection, 'status': ['on', 'off'][o[1].crtc == 0], 'modes': [modes[i] for i in o[1].modes if i in modes], 'mode_ids': o[1].modes, 'crtcs': o[1].crtcs, 'mm_width': o[1].mm_width, 'mm_height': o[1].mm_height, 'id': o[0] } for o in outputs] # only return connectad outputs outputs = [{ 'name': o['name'], 'primary': o['primary'], 'crtc_id': o['crtc_id'], 'x': o['crtc'].x if o['crtc'] else None, 'y': o['crtc'].y if o['crtc'] else None, 'height': o['crtc'].height if o['crtc'] else None, 'width': o['crtc'].width if o['crtc'] else None, 'crtc': o['crtc'], 'status': o['status'], 'connection': not o['connection'], 'modes': o['modes'], 'mode_ids': o['mode_ids'], 'crtcs': o['crtcs'], 'current_mode': o['crtc'].mode if o['crtc'] else None, 'mm_width': o['mm_width'], 'mm_height': o['mm_height'], 'id': o['id'] } for o in outputs] return outputs except RuntimeError: return None
def output_list_names(): s = display.Display().screen() window = s.root.create_window(0, 0, 1, 1, 1, s.root_depth) outputs = randr.get_screen_resources(window).outputs return [randr.get_output_info(window, o, 0).name for o in outputs]
loglevel = 'verbose' if args.verbose else 'error' monitors_list = dict() monitors_list_inactive = list() primary_monitor = None if 'DISPLAY' not in os.environ: print("error: DISPLAY variable not set") sys.exit(1) # list monitors and get active monitor display_num = os.environ['DISPLAY'] d = display.Display(display_num) root_window = d.screen().root primary_output_id = randr.get_output_primary(root_window).output for output_id in randr.get_screen_resources(root_window).outputs: o = randr.get_output_info(root_window, output_id, 0) if o.crtc != 0: c = randr.get_crtc_info(root_window, o.crtc, 0) monitors_list[o.name] = { 'width': c.width, 'height': c.height, 'x': c.x, 'y': c.y, 'primary': False } if output_id == primary_output_id: monitors_list[o.name]['primary'] = True primary_monitor = o.name else: monitors_list_inactive.append(o.name) # list windows xdo = Xdo() windows_list = xdo.search_windows(winname = b'.*') windows_list.sort()
from Xlib import X, display from Xlib.ext import randr d = display.Display() s = d.screen() window = s.root.create_window(0, 0, 1, 1, 1, s.root_depth) res = randr.get_screen_resources(window) for mode in res.modes: w, h = mode.width, mode.height print("Width: {}, height: {}".format(w, h)) outputs = randr.get_screen_resources(window).outputs print(outputs) for index, output in enumerate(outputs): print(output) print(randr.get_output_info(window, outputs[index], 0))