forked from Zarfir/Screenz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreenz
More file actions
executable file
·260 lines (234 loc) · 6.9 KB
/
screenz
File metadata and controls
executable file
·260 lines (234 loc) · 6.9 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/bin/bash
# {VARIABLE ENVIRONEMENT}
## {CONFIG PATH}
CONFIG_DIR=~/.config/screenz
CONFIG_FILE=$CONFIG_DIR/config.txt
## {APPLICATION PATH}
TEMP_DIR=/tmp
TEMP_FILE=$TEMP_DIR/screenz-tmp.txt
## {APPLICATION PATH}
APPLICATION_PATH=/usr/bin
SCREENZ_PATH=$APPLICATION_PATH/screenz
XRANDR_PATH=$APPLICATION_PATH/xrandr
WZPAPER_PATH=$APPLICATION_PATH/wzpaper
# { FONCTION TO CHECK THE INITIALIZATION } #
function check_init() {
nb_line=0
if [[ ! -e $XRANDR_PATH ]]; then
echo -e "You must install 'xrandr' to use this command."
exit 84
fi
if [[ ! -d $CONFIG_DIR ]] || [[ ! -e $CONFIG_FILE ]]; then
echo "Error config file make a 'screenz -i'."
exit 84
fi
if [[ ! -e $CONFIG_FILE ]]; then
echo -e "\n" >> $CONFIG_FILE
fi
while read lines || [[ -n "$lines" ]]; do
if [[ $nb_line == 1 ]] && [[ -z $lines ]]; then
echo "the position of your production in relation to your computer is not notified, make a 'screenz -i' to notify it"
exit 84
fi
((nb_line++))
done < $CONFIG_FILE
if [[ nb_line -ne 2 ]]; then
rm -f $CONFIG_FILE
echo "Error config file make a 'screenz -i'."
exit 84
fi
xrandr --current >> $TEMP_FILE
}
# {FUNCTION FOR PRINT THE INFO (-H)} #
function help() {
echo -e "\n\e[1mNAME\e[0m"
echo -e "\tscreenz - set the orientation or output to configuring multiple monitors"
echo -e ""
echo -e "\e[1mSYNOPSIS\e[0m"
echo -e "\tscreenz [OPTIONS]"
echo -e ""
echo -e "\e[1mDESCRIPTION\e[0m"
echo -e ""
echo -e "\e[1mOPTIONS\e[0m"
echo -e "\t-h\tPrint the different information of this programme."
echo -e "\t-i\tInitialize information to set the program."
echo -e "\t-p\tAutomatic connection if there is an output."
echo -e "\t-c\tChoose the output you want."
echo -e ""
echo -e "\e[1mRETURN VALUE\e[0m"
echo -e "\tOn success, screenz return 0."
echo -e "\tOn error, screenz return 84."
echo -e ""
echo -e "\e[1mOTHER\e[0m"
echo -e "\tThis application generates a hidden directory nammed 'screenz/' in your '.config/' directory wich contains the parameters files and in the '/tmp/' directory for the script to work properly."
echo -e "\t\t'screenz-tmp.txt' is a hidden file that contains information temporaly (do not modify)."
echo -e "\t\t'config.txt' is a hidden file that contains your parameters (do not modify)."
echo -e ""
}
# {FUNCTION TO INITIALIZED THE PROGRAM (-I)} #
function init() {
while [[ 1 ]]; do
printf "Where is the position of your output relative to your computer? (left-of/right-of/above/below): "
read answer
if [[ $answer == "left-of" ]] || [[ $answer == "right-of" ]] || [[ $answer == "above" ]] || [[ $answer == "below" ]]; then
if [[ ! -d $CONFIG_DIR ]]; then
mkdir $CONFIG_DIR
fi
if [[ ! -e $CONFIG_FILE ]]; then
echo -e "\n" >> $CONFIG_FILE
fi
sed -i '2d' $CONFIG_FILE
sed -i 1a"$answer" $CONFIG_FILE
break
else
echo "Invalid answer"
fi
done
if [[ ! -e $SCREENZ_PATH ]]; then
while [[ 1 ]]; do
printf "You want to add 'screenz' in your system command ? (y/N): "
read answer
if [[ $answer == "y" ]]; then
sudo cp -f screenz $APPLICATION_PATH
break
elif [[ $answer == "N" ]]; then
break
else
echo "Invalid answer"
fi
done
fi
}
# {FUNCTION FOR AUTOMATIC CONNECTION (-P)} #
function auto () {
nb_output=0
nb_line=0
main_screen="\0"
# variable get in CONFIG_FILE
last_output="\0"
position="\0"
rm -f $TEMP_FILE
while [[ 1 ]]; do
xrandr --current >> $TEMP_FILE
if [[ -e $CONFIG_FILE ]]; then
while read lines || [[ -n "$lines" ]]; do
if [[ $nb_line == 0 ]] && [[ -n $lines ]]; then
last_output=$lines
elif [[ $nb_line == 1 ]] && [[ -n $lines ]]; then
position=$lines
fi
((nb_line++))
done < $CONFIG_FILE
nb_line=0
else
rm -f $CONFIG_FILE
echo "Error config file make a 'screenz -i'."
exit 84
fi
while read lines || [[ -n "$lines" ]]; do
line=($lines)
if [[ ${line[1]} == "connected" ]] && [[ $nb_output == 0 ]]; then
main_screen=${line[0]}
((nb_output++))
elif [[ ${line[0]} != $main_screen ]] && [[ ${line[1]} == "connected" ]] && [[ ${line[0]} != $last_output ]] && [[ $nb_output == 1 ]]; then
sed -i '1d' $CONFIG_FILE
sed -i 1i"${line[0]}" $CONFIG_FILE
xrandr --output ${line[0]} --auto --$position $main_screen
if [[ ! -e $WZPAPER_PATH ]]; then
killall wzpaper > /dev/null 2>&1
wzpaper -p 0.25
fi
elif [[ ${line[0]} == "$last_output" ]] && [[ ${line[1]} == "disconnected" ]]; then
xrandr --output ${line[0]} --off
sed -i '1d' $CONFIG_FILE
sed -i 1i'\ \' $CONFIG_FILE
last_output="\0"
fi
done < $TEMP_FILE
rm -f $TEMP_FILE
sleep 1s
done
}
# {FUNCTION FOR CHOICE YOUR SCREEN (-C)} #
function choise() {
nb_output=0
count=0
main_screen="\0"
selected_output="\0"
write_text=false
rm -f $TEMP_FILE
xrandr --current >> $TEMP_FILE
while read lines || [[ -n "$lines" ]]; do
line=($lines)
if [[ ${line[1]} == "connected" ]] && [[ $nb_output == 0 ]]; then
echo -e "---[Output from the main screen]---\n"
main_screen=${line[0]}
echo -e "\t\t$main_screen\n"
((nb_output++))
elif [[ $main_screen != '\0' ]] && [[ ${line[0]} != $main_screen ]] && [[ ${line[1]} == "connected" ]]; then
if [[ $write_text == false ]]; then
echo -e "---------[External output]---------\n"
write_text=true
fi
echo -e "\t\t${line[0]}\n"
selected_output=( ${line[0]} )
((nb_output++))
fi
done < $TEMP_FILE
if [[ $selected_output == '\0' ]]; then
echo -e "No external output."
exit 84
fi
while [[ 1 ]]; do
printf "which output do you want ? : "
read answer
if [[ $answer == $selected_output ]]; then
while [[ 1 ]]; do
printf "what position relative to the main screen? (left-of/right-of/above/below): "
read answer
if [[ $answer == "left-of" ]] || [[ $answer == "right-of" ]] || [[ $answer == "above" ]] || [[ $answer == "below" ]]; then
xrandr --output $selected_output --auto --$answer $main_screen
echo -e "$selected_output is on the $answer side of your main screen"
if [[ ! -e $WZPAPER_PATH ]]; then
killall wzpaper > /dev/null 2>&1
wzpaper -p 0.25
fi
exit 0
else
echo "Invalid answer."
fi
done
fi
((count++))
if [[ $nb_output == $count ]]; then
echo "Invalid output."
fi
done
}
# {SWITCH CASE} #
case $1 in
"-h" | "--help")
help
;;
"-i")
init
;;
"-p")
check_init
auto &
;;
"-c")
check_init
choise
;;
*)
if [[ -z $1 ]]; then
echo "screenz: invalid argument."
else
echo "screenz: invalid option - '$1'"
fi
echo "Try 'screenz -h' for more information."
exit 84
;;
esac
exit 0