-
Notifications
You must be signed in to change notification settings - Fork 0
/
solve.java
205 lines (159 loc) · 5.57 KB
/
solve.java
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// [Description]
// Suboptimal java gui to change brightness levels through software rendering
//Useful for Linux devices that are too new for compatbility.
//This would not be a good long-term solution as this doesn't use the hardware controller
// to adjust the system brightness.
import javax.swing.event.*;
import java.awt.*;
import java.awt.Font;
import javax.swing.*;
import java.io.IOException;
import java.io.*;
/* This should determine your display identifier:
xrandr | grep -w connected | awk -F'[ ]' '{print $1}'
Pass it as a parameter when you run 'java solve.java' `<Arguments-Here>`
This gives the progaram the ID of your connected display.
*Note* the above arguments are enclosed with back-ticks(`) - not the apostrophe.
Alternatively there's radio buttons for all display ID types.
This includes types not on your system.
Overall it's meant to provide a reliable trial & error approach...
...at the cost of speed, optimization, & everything good.
<todo> allow for entering in your display type, JTextField
*/
public class solve extends JFrame implements ChangeListener
{
public static JTextField manualInput;
static Font font;
static String ConnectedDisplay, SystemBrightness;
static JRadioButton[] buttonDisplays;
static JFrame frame;
// Slider to adjust brightness
static JSlider sliderButton;
// Label to show % set to
static JLabel label;
// main class
public static void main(String[] args) throws IOException
{
// Argument 0 is system command to retrieve current brightness level
SystemBrightness = args[0];
// Argument 1 is system command to retrieve connected display's ID
ConnectedDisplay = args[1];
float currentBrightness = Float.parseFloat(SystemBrightness);
System.out.println("\nCurrent brightness: " + currentBrightness);
// Standard jframe being initialized
frame = new JFrame("Set Current Brightness");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Serves as the reference
solve s = new solve();
// create label
label = new JLabel();
// create a panel
JPanel p = new JPanel();
p.setBackground(Color.black);
// All this does is allow 1 radio-button selected at a time
ButtonGroup group = new ButtonGroup();
int count = 0;
// By default supports: DP1-4, LVDS-1, and HDMI-1
buttonDisplays = new JRadioButton[7];
for (int i = 0; i < 4; i++)
{
buttonDisplays[i] = new JRadioButton();
buttonDisplays[i].setText("DP-" + count);
buttonDisplays[i].setBounds(i+10,i+20,20,20);
buttonDisplays[i].setVisible(true);
p.add(buttonDisplays[i]);
group.add(buttonDisplays[i]);
++count;
}
// LVDS Support:
buttonDisplays[4] = new JRadioButton();
buttonDisplays[4].setText("LVDS-1");
buttonDisplays[4].setBounds(30, 200,10,10);
buttonDisplays[4].setVisible(true);
p.add(buttonDisplays[4]);
group.add(buttonDisplays[4]);
// HDMI-1 Support:
buttonDisplays[5] = new JRadioButton();
buttonDisplays[5].setText("HDMI-1");
buttonDisplays[5].setBounds(30, 200,10,10);
buttonDisplays[5].setVisible(true);
p.add(buttonDisplays[5]);
group.add(buttonDisplays[5]);
// Auto Setting:
buttonDisplays[6] = new JRadioButton();
buttonDisplays[6].setText(ConnectedDisplay);
buttonDisplays[6].setBounds(30, 200,10,10);
buttonDisplays[6].setVisible(true);
p.add(buttonDisplays[6]);
group.add(buttonDisplays[6]);
buttonDisplays[6].setBounds(30, 200,10,10);
// Last param ensures slider starts at current system brightness
sliderButton = new JSlider(0, 100, (int)(currentBrightness * 100 ));
// paint the ticks and tracks
sliderButton.setPaintTrack(true);
sliderButton.setPaintTicks(true);
sliderButton.setPaintLabels(true);
// set spacing
sliderButton.setMajorTickSpacing(50);
sliderButton.setMinorTickSpacing(5);
// setChangeListener
sliderButton.addChangeListener(s);
// set orientation of slider
sliderButton.setOrientation(SwingConstants.HORIZONTAL);
// set Font for the slider
sliderButton.setFont(new Font("Ubuntu Bold", Font.BOLD, 20));
// Manual input through a textfield:
manualInput = new JTextField("");
manualInput.setBounds(150,200,400,400);
manualInput.setEditable(true);
manualInput.setPreferredSize(new Dimension(300,100));
Font font = new Font("Monospace", Font.BOLD, 22);
manualInput.setFont(font);
manualInput.setVisible(true);
p.add(manualInput);
// add slider to panel
p.add(sliderButton);
p.add(label);
frame.add(p);
// Set the text of label on slider
label.setText("Brightness: " + sliderButton.getValue() + "% ");
// set the size of slider menu
frame.setSize(2000, 1500);
frame.setVisible(true);
// Main event loop to switch all the ID types
while (true){
JRadioButton currentEnabledID = getChosenDisplay(buttonDisplays);
if (manualInput.getText() != "") { ConnectedDisplay = manualInput.getText(); }
else { ConnectedDisplay = currentEnabledID.getText(); }
}
}
// Each time slider is changed, will execute a shell command
// to adjust brightness with last parameter being the desired
// brightness. This parameter will be equal to value of the slider.
public void stateChanged(ChangeEvent e)
{
float currentLevel = ((float)sliderButton.getValue() / 100);
label.setText("Brightness: " + currentLevel + "% ");
System.out.println("Using: " + ConnectedDisplay);
try{
Process p = Runtime.getRuntime().exec(new String[]{"xrandr",
"--output", ConnectedDisplay, "--brightness", Float.toString(currentLevel)});
} catch (IOException e2) {
e2.printStackTrace();
}
}
// Returns current display type user selected
public static JRadioButton getChosenDisplay(JRadioButton[] buttonsOther)
{
for (JRadioButton b : buttonsOther)
{
if (b.isSelected())
{
// System.out.println("Selected: " + b.getText());
return b;
}
}
// Returns connected display by default:
return buttonDisplays[6];
}
}