Skip to content

Commit 250ea06

Browse files
authored
Update Dimmer.vala (indicator postponed for now)
1 parent f857747 commit 250ea06

File tree

1 file changed

+281
-0
lines changed

1 file changed

+281
-0
lines changed

src/Dimmer.vala

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
/*
2+
* Copyright (c) 2018-2019 Panos P. (https://github.com/panosx2/brightness)
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2 of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public
15+
* License along with this program; if not, write to the
16+
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17+
* Boston, MA 02110-1301 USA
18+
*
19+
* Authored by: Panos P. <[email protected]>
20+
*/
21+
22+
using Gtk;
23+
24+
public class Dimmer : Gtk.Application {
25+
public static Gtk.Scale slider;
26+
public static Gtk.Scale slider1;
27+
public static Gtk.Scale slider2;
28+
public static Gtk.Scale slider3;
29+
public static Gtk.Scale slider4;
30+
31+
public Dimmer () {
32+
Object (
33+
application_id: "com.github.panosx2.brightness",
34+
flags: ApplicationFlags.FLAGS_NONE
35+
);
36+
}
37+
38+
protected override void activate () {
39+
Window window = new Gtk.ApplicationWindow(this);
40+
window.title = "Dimmer";
41+
window.window_position = WindowPosition.CENTER;
42+
window.set_decorated(true);
43+
window.set_deletable(true);
44+
window.set_resizable(false);
45+
window.destroy.connect(Gtk.main_quit);
46+
window.border_width = 20;
47+
48+
var vboxMain = new Box (Orientation.VERTICAL, 0);
49+
vboxMain.homogeneous = false;
50+
51+
var vboxInd = new Box (Orientation.VERTICAL, 0);
52+
vboxInd.homogeneous = false;
53+
54+
string currentBr, /*currentGm,*/ names;
55+
56+
try {
57+
GLib.Process.spawn_command_line_sync("sh -c \"xrandr --verbose | grep -m 1 -i brightness | cut -f2 -d ' '\"", out currentBr);
58+
} catch (SpawnError se) { currentBr = "100"; }
59+
60+
/*
61+
try {
62+
GLib.Process.spawn_command_line_sync("sh -c \"xrandr --verbose | grep -m 1 -i gamma | cut -f2 -d ' '\"", out currentGm);
63+
} catch (SpawnError se) { currentGm = "1.0:1.0:1.0"; }
64+
*/
65+
66+
try {
67+
GLib.Process.spawn_command_line_sync("sh -c \"xrandr | grep ' connected ' | awk '{ print$1 }'\"", out names);
68+
} catch (SpawnError se) { names = ""; }
69+
70+
string[] lines = names.split("\n");
71+
72+
if (lines.length > 1) {
73+
var hboxAll = new Box (Orientation.HORIZONTAL, 0);
74+
hboxAll.homogeneous = false;
75+
76+
var label = new Label("All Monitors:");
77+
label.halign = Align.START;
78+
label.set_margin_bottom(30);
79+
80+
var switcher = new Switch();
81+
switcher.active = true;
82+
switcher.state_changed.connect(() => {
83+
if (switcher.active == true) {
84+
slider.visible = true;
85+
vboxInd.visible = false;
86+
slider.adjustment.value = slider.get_value();
87+
}
88+
else {
89+
slider.visible = false;
90+
vboxInd.visible = true;
91+
slider1.set_value(slider1.get_value());
92+
slider2.set_value(slider2.get_value());
93+
slider3.set_value(slider3.get_value());
94+
slider4.set_value(slider4.get_value());
95+
}
96+
});
97+
switcher.set_margin_bottom(30);
98+
99+
hboxAll.pack_start(label, false, false, 0);
100+
hboxAll.pack_end(switcher, false, false, 0);
101+
102+
vboxMain.add(hboxAll);
103+
}
104+
105+
string[] monitors = {"", "", "", ""};
106+
107+
for (int i = 0; i < lines.length - 1; i++) {
108+
monitors[i] = lines[i];
109+
}
110+
111+
if (lines.length > 1) {
112+
//slider1
113+
Label label1 = new Label(monitors[0]);
114+
label1.halign = Align.START;
115+
116+
try {
117+
GLib.Process.spawn_command_line_sync("sh -c \"xrandr --verbose | grep -m 1 -i brightness | cut -f2 -d ' '\"", out currentBr);
118+
} catch (SpawnError se) { currentBr = "100"; }
119+
120+
slider1 = new Scale.with_range(Orientation.HORIZONTAL, 40, 150, 1);
121+
slider1.set_size_request(380, 30);
122+
slider1.adjustment.value = double.parse(currentBr) * 100;
123+
slider1.adjustment.value_changed.connect (() => {
124+
try {
125+
string edited = (slider1.adjustment.value / 100).to_string();
126+
127+
if ((slider1.adjustment.value / 100) >= 1.0) {
128+
GLib.Process.spawn_command_line_async("xrandr --output " + monitors[0] + " --gamma " + edited + ":" + edited + ":" + edited);
129+
}
130+
else GLib.Process.spawn_command_line_async("xrandr --output " + monitors[0] + " --brightness " + edited);
131+
132+
if ((slider1.adjustment.value / 100) > 1.2) slider1.tooltip_text = "Too Bright";
133+
else if ((slider1.adjustment.value / 100) < 0.6) slider1.tooltip_text = "Too Dark";
134+
else slider1.tooltip_text = "";
135+
} catch (SpawnError se) {}
136+
});
137+
slider1.set_margin_bottom(30);
138+
139+
//slider2
140+
Label label2 = new Label(monitors[1]);
141+
label2.halign = Align.START;
142+
143+
try {
144+
GLib.Process.spawn_command_line_sync("sh -c \"xrandr --verbose | grep -m 1 -i brightness | cut -f2 -d ' '\"", out currentBr);
145+
} catch (SpawnError se) { currentBr = "100"; }
146+
147+
slider2 = new Scale.with_range(Orientation.HORIZONTAL, 40, 150, 1);
148+
slider2.set_size_request(380, 30);
149+
slider2.adjustment.value = double.parse(currentBr) * 100;
150+
slider2.adjustment.value_changed.connect (() => {
151+
try {
152+
string edited = (slider2.adjustment.value / 100).to_string();
153+
154+
if ((slider2.adjustment.value / 100) >= 1.0) {
155+
GLib.Process.spawn_command_line_async("xrandr --output " + monitors[1] + " --gamma " + edited + ":" + edited + ":" + edited);
156+
}
157+
else GLib.Process.spawn_command_line_async("xrandr --output " + monitors[1] + " --brightness " + edited);
158+
159+
if ((slider2.adjustment.value / 100) > 1.2) slider2.tooltip_text = "Too Bright";
160+
else if ((slider2.adjustment.value / 100) < 0.6) slider2.tooltip_text = "Too Dark";
161+
else slider2.tooltip_text = "";
162+
} catch (SpawnError se) {}
163+
});
164+
slider2.set_margin_bottom(30);
165+
166+
//slider3
167+
Label label3 = new Label(monitors[2]);
168+
label3.halign = Align.START;
169+
170+
try {
171+
GLib.Process.spawn_command_line_sync("sh -c \"xrandr --verbose | grep -m 1 -i brightness | cut -f2 -d ' '\"", out currentBr);
172+
} catch (SpawnError se) { currentBr = "100"; }
173+
174+
slider3 = new Scale.with_range(Orientation.HORIZONTAL, 40, 150, 1);
175+
slider3.set_size_request(380, 30);
176+
slider3.adjustment.value = double.parse(currentBr) * 100;
177+
slider3.adjustment.value_changed.connect (() => {
178+
try {
179+
string edited = (slider3.adjustment.value / 100).to_string();
180+
181+
if ((slider3.adjustment.value / 100) >= 1.0) {
182+
GLib.Process.spawn_command_line_async("xrandr --output " + monitors[2] + " --gamma " + edited + ":" + edited + ":" + edited);
183+
}
184+
else GLib.Process.spawn_command_line_async("xrandr --output " + monitors[2] + " --brightness " + edited);
185+
186+
if ((slider3.adjustment.value / 100) > 1.2) slider3.tooltip_text = "Too Bright";
187+
else if ((slider3.adjustment.value / 100) < 0.6) slider3.tooltip_text = "Too Dark";
188+
else slider3.tooltip_text = "";
189+
} catch (SpawnError se) {}
190+
});
191+
slider3.set_margin_bottom(30);
192+
193+
//slider4
194+
Label label4 = new Label(monitors[3]);
195+
label4.halign = Align.START;
196+
197+
try {
198+
GLib.Process.spawn_command_line_sync("sh -c \"xrandr --verbose | grep -m 1 -i brightness | cut -f2 -d ' '\"", out currentBr);
199+
} catch (SpawnError se) { currentBr = "100"; }
200+
201+
slider4 = new Scale.with_range(Orientation.HORIZONTAL, 40, 150, 1);
202+
slider4.set_size_request(380, 30);
203+
slider4.adjustment.value = double.parse(currentBr) * 100;
204+
slider4.adjustment.value_changed.connect (() => {
205+
try {
206+
string edited = (slider4.adjustment.value / 100).to_string();
207+
208+
if ((slider4.adjustment.value / 100) >= 1.0) {
209+
GLib.Process.spawn_command_line_async("xrandr --output " + monitors[3] + " --gamma " + edited + ":" + edited + ":" + edited);
210+
}
211+
else GLib.Process.spawn_command_line_async("xrandr --output " + monitors[3] + " --brightness " + edited);
212+
213+
if ((slider4.adjustment.value / 100) > 1.2) slider4.tooltip_text = "Too Bright";
214+
else if ((slider4.adjustment.value / 100) < 0.6) slider4.tooltip_text = "Too Dark";
215+
else slider4.tooltip_text = "";
216+
} catch (SpawnError se) {}
217+
});
218+
219+
//slider for all monitors
220+
slider = new Scale.with_range(Orientation.HORIZONTAL, 40, 150, 1);
221+
slider.set_size_request(380, 50);
222+
slider.adjustment.value = double.parse(currentBr) * 100;
223+
slider.adjustment.value_changed.connect (() => {
224+
try {
225+
string edited = (slider.adjustment.value / 100).to_string();
226+
227+
for (int i = 0; i < lines.length - 1; i++) {
228+
if ((slider.adjustment.value / 100) >= 1.0) {
229+
GLib.Process.spawn_command_line_async("xrandr --output " + lines[i] + " --gamma " + edited + ":" + edited + ":" + edited);
230+
}
231+
else GLib.Process.spawn_command_line_async("xrandr --output " + lines[i] + " --brightness " + edited);
232+
}
233+
234+
if (lines.length > 1) {
235+
for (int j = 0; j < lines.length - 1; j++) {
236+
if (j == 0) slider1.adjustment.value = slider.adjustment.value;
237+
else if (j == 1) slider2.adjustment.value = slider.adjustment.value;
238+
else if (j == 2) slider3.adjustment.value = slider.adjustment.value;
239+
else if (j == 3) slider4.adjustment.value = slider.adjustment.value;
240+
}
241+
}
242+
243+
if ((slider.adjustment.value / 100) > 1.2) slider.tooltip_text = "Too Bright";
244+
else if ((slider.adjustment.value / 100) < 0.6) slider.tooltip_text = "Too Dark";
245+
else slider.tooltip_text = "";
246+
} catch (SpawnError se) {}
247+
});
248+
249+
vboxMain.add(slider);
250+
251+
for (int i = 0; i < lines.length - 1; i++) {
252+
if (i == 0) {
253+
vboxInd.add(label1);
254+
vboxInd.add(slider1);
255+
}
256+
else if (i == 1) {
257+
vboxInd.add(label2);
258+
vboxInd.add(slider2);
259+
}
260+
else if (i == 2) {
261+
vboxInd.add(label3);
262+
vboxInd.add(slider3);
263+
}
264+
else if (i == 3) {
265+
vboxInd.add(label4);
266+
vboxInd.add(slider4);
267+
}
268+
}
269+
}
270+
271+
vboxMain.pack_end(vboxInd, false, false, 0);
272+
273+
window.add(vboxMain);
274+
275+
window.show_all();
276+
}
277+
278+
public static int main(string[] args) {
279+
return new Dimmer().run (args);
280+
}
281+
}

0 commit comments

Comments
 (0)