chamesle

chamesle.org: the site of Chamesle, a small Linux distribution
git clone https://github.com/tortr-rs/chamesle
Log | Files | Refs | README

set-hidpi.py (1419B)


      1 #!/usr/bin/python3
      2 
      3 import gi
      4 gi.require_version('Gdk', '3.0')
      5 from gi.repository import Gdk
      6 import sys
      7 
      8 HIDPI_LIMIT = 192
      9 
     10 def get_window_scale():
     11     window_scale = 1
     12     try:
     13         display = Gdk.Display.get_default()
     14         monitor = display.get_primary_monitor()
     15         rect = monitor.get_geometry()
     16         width_mm = monitor.get_width_mm()
     17         height_mm = monitor.get_height_mm()
     18         monitor_scale = monitor.get_scale_factor()
     19 
     20         # Return 1 if the screen size isn't available (some TVs report their aspect ratio instead ... 16/9 or 16/10)
     21         if ((width_mm == 160 and height_mm == 90) \
     22             or (width_mm == 160 and height_mm == 100) \
     23             or (width_mm == 16 and height_mm == 9) \
     24             or (width_mm == 16 and height_mm == 10)):
     25             return 1
     26 
     27         if rect.height * monitor_scale < 1500:
     28             return 1
     29 
     30         if width_mm > 0 and height_mm > 0:
     31             witdh_inch = width_mm / 25.4
     32             height_inch = height_mm / 25.4
     33             dpi_x = rect.width * monitor_scale / witdh_inch
     34             dpi_y = rect.height * monitor_scale / height_inch
     35             if dpi_x > HIDPI_LIMIT and dpi_y > HIDPI_LIMIT:
     36                 window_scale = 2
     37 
     38     except Exception as e:
     39         print("Error while detecting hidpi mode: %s" % str(e), file=sys.stderr)
     40     return window_scale
     41 
     42 if __name__ == '__main__':
     43     print (get_window_scale())
     44     sys.exit(0)
     45