forked from sidorares/xrandr-parse
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
126 lines (119 loc) · 4.76 KB
/
index.js
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var type = {
connected: /^(\S+) connected (?:(\d+)x(\d+)\+(\d+)\+(\d))*\s*(\(\w+\))*\s*(\w*)/,
disconnected: /^(\S+) disconnected/,
mode: /^\s+(\d+)x([0-9i]+)\s+(\(\w+\))\s+([0-9]+\.[0-9]+)MHz\s+((\s*[\+]*[\-]*\w*\s)*)/,
dimension_horizontal: /^\s+h\:\s+width\s+(\d+)\s+start\s+(\d+)\s+end\s+(\d+)\s+total\s+(\d+)\s*skew\s*(\w)\s*clock\s*([0-9]*.[0-9]*)/,
dimension_vertical: /^\s+v\:\s+height\s+(\d+)\s+start\s+(\d+)\s+end\s+(\d+)\s+total\s+(\d+)\s*clock\s*([0-9]*.[0-9]*)/
};
/**
* This function parses an xrandr output with the following format
* Screen 0: minimum 320 x 200, current 1080 x 1920, maximum 2048 x 2048
* HDMI-1 connected 1080x1920+0+0 (0x44) right (normal left inverted right x axis y axis) 477mm x 268mm
* Identifier: 0x42
* Timestamp: 96798461
* Subpixel: unknown
* Gamma: 1.0:1.0:1.0
* Brightness: 1.0
* Clones:
* CRTC: 2
* CRTCs: 2
* Transform: 1.000000 0.000000 0.000000
* 0.000000 1.000000 0.000000
* 0.000000 0.000000 1.000000
* filter:
* EDID:
* 00ffffffffffff001e6dd75801010101
* 1920x1080 (0x44) 148.500MHz +HSync +VSync *current +preferred
* h: width 1920 start 2008 end 2052 total 2200 skew 0 clock 67.50KHz
* v: height 1080 start 1084 end 1089 total 1125 clock 60.00Hz
* 1920x1080 (0x45) 148.500MHz +HSync +VSync
* h: width 1920 start 2448 end 2492 total 2640 skew 0 clock 56.25KHz
* v: height 1080 start 1084 end 1089 total 1125 clock 50.00Hz
*
* @param {*} data
*/
module.exports = function (data) {
var lines = data.split('\n');
var result = {};
var last_connection = null;
var last_mode = 0;
var index = 0;
lines.forEach(function (line) {
var tmp;
if ((tmp = type.connected.exec(line))) {
if (tmp[3] !== undefined) {
result[tmp[1]] = {
connected: true,
orientation: tmp[7],
modes: [],
index: index++
};
if (tmp[2] && tmp[3]) {
result[tmp[1]].width = parseInt(tmp[2]);
result[tmp[1]].height = parseInt(tmp[3]);
}
if (tmp[4] && tmp[5]) {
result[tmp[1]].left = parseInt(tmp[4]);
result[tmp[1]].top = parseInt(tmp[5]);
}
last_connection = tmp[1];
} else {
result[tmp[1]] = {
connected: false,
orientation: null,
modes: [],
index: index++,
width: 0,
height: 0,
top: 0,
left: 0
};
last_connection = tmp[1];
}
} else if ((tmp = type.disconnected.exec(line))) {
result[tmp[1]] = {
connected: false,
modes: [],
index: index++
};
last_connection = tmp[1];
} else if ((tmp = type.mode.exec(line))) {
var dimensions = {
vertical: null,
horizontal: null
};
var r = {
name: tmp[1] + 'x' + tmp[2],
width: tmp[1],
height: tmp[2],
rate: parseFloat(tmp[4]),
optionals: tmp[5],
current: line.includes('current') ? true : false,
preferred: line.includes('preferred') ? true : false,
dimensions: dimensions
};
result[last_connection].modes.push(r);
last_mode++;
} else if ((tmp = type.dimension_horizontal.exec(line))) {
var dimension_h = {
width: tmp[1],
start: tmp[2],
end: tmp[3],
total: tmp[4],
skew: tmp[5],
clock: parseFloat(tmp[6])
};
result[last_connection].modes[last_mode - 1].dimensions.horizontal = dimension_h;
} else if ((tmp = type.dimension_vertical.exec(line))) {
var dimension_v = {
width: tmp[1],
start: tmp[2],
end: tmp[3],
total: tmp[4],
clock: parseFloat(tmp[5])
};
result[last_connection].modes[last_mode - 1].dimensions.vertical = dimension_v;
}
});
return result;
};