Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

import os 

import subprocess 

 

from lutris.util.log import logger 

 

 

def iter_xrandr_output(): 

    xrandr_output = subprocess.Popen("xrandr", 

                                     stdout=subprocess.PIPE, 

                                     stderr=subprocess.PIPE).communicate()[0] 

    for line in xrandr_output.split("\n"): 

        yield line 

 

 

def get_outputs(): 

    """ Return list of tuples containing output name and geometry """ 

    outputs = list() 

    for line in iter_xrandr_output(): 

        parts = line.split() 

        if len(parts) < 2: 

            continue 

        if parts[1] == 'connected': 

            geom = parts[2] if parts[2] != 'primary' else parts[3] 

            outputs.append((parts[0], geom)) 

    return outputs 

 

 

def get_output_names(): 

    return [output[0] for output in get_outputs()] 

 

 

def turn_off_except(display): 

    for output in get_outputs(): 

        if output[0] != display: 

            subprocess.Popen("xrandr --output %s --off" % output[0], shell=True) 

 

 

def get_resolutions(): 

    """Return the list of supported screen resolutions.""" 

    resolution_list = [] 

    for line in iter_xrandr_output(): 

        if line.startswith("  "): 

            resolution_list.append(line.split()[0]) 

    return resolution_list 

 

 

def get_current_resolution(monitor=0): 

    """Return the current resolution for the desktop.""" 

    resolution = list() 

    for line in iter_xrandr_output(): 

        if line.startswith("  ") and "*" in line: 

            resolution.append(line.split()[0]) 

    if monitor == 'all': 

        return resolution 

    else: 

        return resolution[monitor] 

 

 

def change_resolution(resolution): 

    """ Change display resolution. 

        Takes a string for single monitors or a list of displays as returned 

        by get_outputs() 

    """ 

    if isinstance(resolution, basestring): 

        logger.debug("Switching resolution to %s", resolution) 

 

        if resolution not in get_resolutions(): 

            logger.warning("Resolution %s doesn't exist." % resolution) 

        else: 

            subprocess.Popen("xrandr -s %s" % resolution, shell=True) 

    else: 

        print resolution 

        for display in resolution: 

            display_name = display[0] 

            display_geom = display[1] 

            logger.debug("Switching to %s on %s", display[1], display[0]) 

            display_resolution = display_geom.split('+')[0] 

 

            cmd = "xrandr --output %s --mode %s" % (display_name, 

                                                    display_resolution) 

 

            subprocess.Popen(cmd, shell=True).communicate() 

            cmd = "xrandr --output %s --panning %s" % (display_name, 

                                                       display_geom) 

            logger.debug(cmd) 

            subprocess.Popen(cmd, shell=True) 

 

 

def reset_desktop(): 

    """Restore the desktop to its original state.""" 

    #Restore resolution 

    resolution = get_resolutions()[0] 

    change_resolution(resolution) 

    #Restore gamma 

    os.popen("xgamma -gamma 1.0")