Bläddra i källkod

list names of all available outputs

Fabian Peter Hammerle 7 år sedan
incheckning
8b7681436a
6 ändrade filer med 290 tillägg och 0 borttagningar
  1. 75 0
      .gitignore
  2. 21 0
      LICENSE
  3. 7 0
      README.md
  4. 164 0
      scripts/xrandrl
  5. 2 0
      setup.cfg
  6. 21 0
      setup.py

+ 75 - 0
.gitignore

@@ -0,0 +1,75 @@
+# Editing
+.*.swp
+.~lock.*#
+
+# Auto Completion
+.ycm_extra_conf.py
+
+# Build Process
+CMakeCache.txt
+CMakeFiles
+Makefile
+cmake_install.cmake
+
+# Byte-compiled / optimized / DLL files
+__pycache__/
+*.py[cod]
+*$py.class
+
+# C extensions
+*.so
+
+# Distribution / packaging
+.Python
+env/
+build/
+develop-eggs/
+dist/
+downloads/
+eggs/
+.eggs/
+lib/
+lib64/
+parts/
+sdist/
+var/
+*.egg-info/
+.installed.cfg
+*.egg
+
+# PyInstaller
+#  Usually these files are written by a python script from a template
+#  before PyInstaller builds the exe, so as to inject date/other infos into it.
+*.manifest
+*.spec
+
+# Installer logs
+pip-log.txt
+pip-delete-this-directory.txt
+
+# Unit test / coverage reports
+htmlcov/
+.tox/
+.coverage
+.coverage.*
+.cache
+nosetests.xml
+coverage.xml
+*,cover
+.hypothesis/
+
+# Translations
+*.mo
+*.pot
+
+# Django stuff:
+*.log
+
+# Sphinx documentation
+docs/_build/
+
+# PyBuilder
+target/
+
+#Ipython Notebook
+.ipynb_checkpoints

+ 21 - 0
LICENSE

@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2018 Fabian Peter Hammerle
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.

+ 7 - 0
README.md

@@ -0,0 +1,7 @@
+# xrandrl
+
+## Requirements
+
+```{sh}
+sudo apt-get install python3 libx11-dev libxrandr-dev
+```

+ 164 - 0
scripts/xrandrl

@@ -0,0 +1,164 @@
+#!/usr/bin/env python3
+# PYTHON_ARGCOMPLETE_OK
+
+"""
+sudo apt-get install libx11-dev libxrandr-dev
+https://cgit.freedesktop.org/xorg/app/xrandr/tree/xrandr.c
+"""
+
+import ctypes
+import sys
+
+Time = ctypes.c_ulong
+"""
+#ifndef _XTYPEDEF_XID
+#  define _XTYPEDEF_XID
+#  ifndef _XSERVER64
+typedef unsigned long XID;
+#  else
+typedef CARD32 XID;
+#  endif
+#endif
+"""
+XID = ctypes.c_ulong
+"""
+typedef XID RROutput;
+"""
+RROutput = XID
+"""
+typedef XID RRCrtc;
+"""
+RRCrtc = XID
+""" /usr/include/X11/extensions/Xrandr.h
+typedef XID RRMode;
+"""
+RRMode = XID
+""" /usr/include/X11/extensions/randr.h
+#define RR_Connected		0
+"""
+RR_Connected = 0
+""" /usr/include/X11/extensions/randr.h
+typedef unsigned short	Connection;
+"""
+Connection = ctypes.c_ushort
+""" /usr/include/X11/extensions/randr.h
+typedef unsigned short	SubpixelOrder;
+"""
+SubpixelOrder = ctypes.c_ushort
+
+""" /usr/include/X11/extensions/Xrandr.h
+typedef struct _XRRScreenResources {
+    Time	timestamp;
+    Time	configTimestamp;
+    int		ncrtc;
+    RRCrtc	*crtcs;
+    int		noutput;
+    RROutput	*outputs;
+    int		nmode;
+    XRRModeInfo	*modes;
+} XRRScreenResources;
+"""
+
+
+class XRRScreenResources(ctypes.Structure):
+    _fields_ = [
+        ('timestamp', Time),
+        ('configTimestamp', Time),
+        ('ncrtc', ctypes.c_int),
+        ('crtcs', ctypes.POINTER(RRCrtc)),
+        ('noutput', ctypes.c_int),
+        ('outputs', ctypes.POINTER(RROutput)),
+        ('nmode', ctypes.c_int),
+        ('modes', ctypes.c_void_p),  # ctypes.POINTER(XRRModeInfo)
+    ]
+
+"""
+typedef struct _XRROutputInfo {
+    Time	    timestamp;
+    RRCrtc	    crtc;
+    char	    *name;
+    int		    nameLen;
+    unsigned long   mm_width;
+    unsigned long   mm_height;
+    Connection	    connection;
+    SubpixelOrder   subpixel_order;
+    int		    ncrtc;
+    RRCrtc	    *crtcs;
+    int		    nclone;
+    RROutput	    *clones;
+    int		    nmode;
+    int		    npreferred;
+    RRMode	    *modes;
+} XRROutputInfo;
+"""
+
+
+class XRROutputInfo(ctypes.Structure):
+    _fields_ = [
+        ('timestamp', Time),
+        ('crtc', RRCrtc),
+        ('name', ctypes.c_char_p),
+        ('nameLen', ctypes.c_int),
+        ('mm_width', ctypes.c_ulong),
+        ('mm_height', ctypes.c_ulong),
+        ('connection', Connection),
+        ('subpixel_order', SubpixelOrder),
+        ('ncrtc', ctypes.c_int),
+        ('crtcs', ctypes.POINTER(RRCrtc)),
+        ('nclone', ctypes.c_int),
+        ('clones', ctypes.POINTER(RROutput)),
+        ('nmode', ctypes.c_int),
+        ('npreferred', ctypes.c_int),
+        ('modes', ctypes.POINTER(RRMode)),
+    ]
+
+X11 = ctypes.cdll.LoadLibrary("libX11.so")
+Xrandr = ctypes.cdll.LoadLibrary("libXrandr.so")
+Xrandr.XRRGetScreenResourcesCurrent.restype = ctypes.POINTER(
+    XRRScreenResources)
+"""
+XRROutputInfo *
+XRRGetOutputInfo (Display *dpy, XRRScreenResources *resources, RROutput output);
+"""
+Xrandr.XRRGetOutputInfo.restype = ctypes.POINTER(XRROutputInfo)
+
+
+def get_xrandr_output_infos(xdisplay):
+    screen_resrcs = Xrandr.XRRGetScreenResourcesCurrent(
+        xdisplay,
+        X11.XDefaultRootWindow(xdisplay),
+    )
+    assert isinstance(screen_resrcs.contents, XRRScreenResources)
+    return [Xrandr.XRRGetOutputInfo(xdisplay, screen_resrcs, screen_resrcs.contents.outputs[o])
+            for o in range(screen_resrcs.contents.noutput)]
+
+
+def process():
+    xdisplay = X11.XOpenDisplay(None)
+    for output_info in get_xrandr_output_infos(xdisplay):
+        output_name = output_info.contents.name \
+            .decode(sys.getfilesystemencoding())
+        print(output_name)
+        # if output_info.contents.connection == RR_Connected:
+        #     print(output_name)
+    X11.XCloseDisplay(xdisplay)
+
+
+def _init_argparser():
+    import argparse
+    argparser = argparse.ArgumentParser(description=None)
+    return argparser
+
+
+def main(argv):
+    argparser = _init_argparser()
+    try:
+        import argcomplete
+    except ImportError:
+        pass
+    args = argparser.parse_args(argv)
+    process(**vars(args))
+    return 0
+
+if __name__ == "__main__":
+    sys.exit(main(sys.argv[1:]))

+ 2 - 0
setup.cfg

@@ -0,0 +1,2 @@
+[metadata]
+description-file = README.md

+ 21 - 0
setup.py

@@ -0,0 +1,21 @@
+from setuptools import setup
+
+import glob
+
+version = '0.1'
+
+setup(
+    name='xrandrl',
+    version=version,
+    description='',
+    author='Fabian Peter Hammerle',
+    author_email='fabian@hammerle.me',
+    url='https://git.hammerle.me/fphammerle/xrandrl',
+    # download_url =
+    # 'https://github.com/fphammerle//tarball/{}'.format(version),
+    keywords=[],
+    classifiers=[],
+    scripts=glob.glob('scripts/*'),
+    install_requires=[],
+    tests_require=['pytest'],
+)