OSDN Git Service

add lockscreen setting
[instantos/instantOS.git] / autostart.sh
1 #!/usr/bin/env bash
2
3 ###############################################
4 ## script for instantOS autostart            ##
5 ###############################################
6
7 INSTANTVERSION="$(cat /usr/share/instantutils/version)"
8 if iconf version && [ "$(iconf version)" = "$INSTANTVERSION" ]; then
9     echo "version check successful"
10     echo "running version $INSTANTVERSION"
11 else
12     echo "running update hooks"
13     /usr/share/instantutils/userinstall.sh
14     iconf -i userinstall 1
15     iconf version "$INSTANTVERSION"
16     instantutils default
17 fi
18
19 # architecture detection
20 if [ -z "$1" ]; then
21     if uname -m | grep -q '^arm'; then
22         export ISRASPI=true
23     fi
24
25     if iconf -i noautostart; then
26         echo "autostart disabled"
27         exit
28     fi
29
30     if [ "$(ps aux | grep bash | grep instantautostart | wc -l)" -gt 3 ]; then
31         echo "already running"
32         exit
33     fi
34 else
35     echo "forced run"
36 fi
37
38 cd
39 command -v instantdotfiles && instantdotfiles
40
41 if ! iconf -i rangerplugins && command -v rangerplugins; then
42     mkdir instantos
43     echo "installing ranger plugins"
44     mkdir -p ~/.config/ranger/plugins
45     cp -r /usr/share/rangerplugins/* ~/.config/ranger/plugins/
46     iconf -i rangerplugins 1
47 fi
48
49 # find out if it's a live session
50 if [ -e /usr/share/liveutils ] &>/dev/null; then
51     ISLIVE="True"
52     echo "live session detected"
53 fi
54
55 if iconf -i islaptop; then
56     export ISLAPTOP="true"
57     echo "laptop detected"
58 else
59     echo "not a laptop"
60 fi
61
62 islive() {
63     if [ -n "$ISLIVE" ]; then
64         return 0
65     else
66         return 1
67     fi
68 }
69
70 # optionally disable status bar
71 if iconf -i nobar; then
72     NMON=$(iconf names | wc -l)
73     for i in $(eval "echo {1..$NMON}"); do
74         echo "found monitor $i"
75         xdotool key super+comma
76         xdotool key super+b
77     done &
78 fi
79
80 if [ -n "$ISRASPI" ]; then
81     # enable double drawing for moving floating windows
82     # greatly increases smoothness
83     iconf -i highfps 1
84     if ! [ -e ~/.config/instantos/israspi ]; then
85         echo "marking machine as raspi"
86         mkdir -p ~/.config/instantos
87         touch ~/.config/instantos/israspi
88         # logo does not work on raspi
89         iconf -i nologo 1
90     fi
91 fi
92
93 if ! iconf -i notheming; then
94     instantthemes a
95     xrdb ~/.Xresources
96     iconf -i instantthemes 1
97
98     # dynamically switch between light and dark gtk theme
99     DATEHOUR=$(date +%H)
100     if [ "$DATEHOUR" -gt "20" ] || [ "$DATEHOUR" -lt "7" ]; then
101         instantthemes d &
102         touch /tmp/instantdarkmode
103         [ -e /tmp/instantlightmode ] && rm /tmp/instantlightmode
104     else
105         instantthemes l &
106         touch /tmp/instantlightmode
107         [ -e /tmp/instantdarkmode ] && rm /tmp/instantdarkmode
108     fi &
109 else
110     touch /tmp/instantlightmode
111 fi
112
113 mkdir -p /tmp/notifications &>/dev/null
114 if ! pgrep dunst; then
115     while :; do
116         # wait for theming before starting dunst
117         if [ -e /tmp/instantdarkmode ] || [ -e /tmp/instantlightmode ]; then
118             dunst
119         fi
120         sleep 2
121     done &
122 fi
123
124 onlinetrigger() {
125     instantwallpaper &
126 }
127
128 # set up oh-my-zsh config if not existing already
129 iconf -i nozsh || instantshell &
130
131 # fix resolution on virtual machine
132 if ! iconf -i novmfix && grep -q 'hypervisor' /proc/cpuinfo; then
133     # indicator file only exists on kvm/QEMU on amd
134     if [ -e /opt/instantos/kvm ]; then
135         iconf -i highfps 1
136         if lshw -c video | grep -i 'qxl' || xrandr | grep -i '^qxl'; then
137             iconf -i qxl 1
138             # iconf -i noanimations 1
139             if ! iconf -i potato && ! iconf -i nopotato; then
140                 if echo "please set your video card to virtio or passthrough
141 QXL on AMD on QEMU/kvm has been known to cause a severe Xorg memory leak. 
142 Disabling compositing makes this somewhat bearable,
143 but switching really is recommended.
144 (or switch to virtualbox, no issues there...)
145 Disable compositing for this VM?" | imenu -C; then
146                     iconf -i potato 1
147                     pkill picom
148                 else
149                     if ! imenu -c "ask again next time?"; then
150                         iconf -i nopotato 1
151                     fi
152                 fi
153             fi
154         else
155             iconf -i qxl 1
156         fi
157     fi
158
159     if ! [ -e /opt/instantos/guestadditions ]; then
160         if lsmod | grep -q vboxguest; then
161             echo "guestadditions detected"
162         else
163             if echo "virtual machine detected.
164 Would you like to switch to a 1080p resolution?" | imenu -C; then
165                 echo "applying virtual machine workaround"
166                 /usr/share/instantassist/assists/t/v.sh
167             else
168                 if [ -z "$ISLIVE" ]; then
169                     if ! imenu -c "ask again next session"; then
170                         iconf -i novmfix 1
171                     fi
172                 fi
173             fi
174         fi
175     fi
176 fi
177
178 if ! islive; then
179     echo "not a live session"
180     if [ -e /opt/instantos/installtrigger ]; then
181         zenity --info --text "finishing installation in background" &
182
183         # ask for password if postinstall already ran
184         if ! timeout 2 sudo echo test; then
185             instantsudo instantpostinstall
186             sudo rm /opt/instantos/installtrigger
187         else
188             sudo instantpostinstall
189         fi
190
191         pkill zenity
192     fi
193
194     cd ~/instantos || exit 1
195     if ! iconf -i max; then
196         instantmonitor
197     fi
198
199     if [ -e ~/instantos/monitor.sh ]; then
200         echo "restoring resolution"
201         bash ~/instantos/monitor.sh &
202     elif [ -e ~/.config/autorandr/instantos/config ]; then
203         echo "restoring autorandr resolution"
204         autorandr instantos &
205     fi
206
207     if checkinternet; then
208         onlinetrigger
209     else
210         # fall back to already installed wallpapers
211         instantwallpaper offline
212         for i in $(seq 10); do
213             if checkinternet; then
214                 onlinetrigger
215                 break
216             else
217                 sleep 10
218             fi
219         done
220     fi &
221
222     # apply keybpard layout
223     if iconf layout; then
224         setxkbmap -layout "$(iconf layout)"
225     else
226         CURLOCALE=$(locale | grep LANG | sed 's/.*=\(.*\)\..*/\1/')
227         case "$CURLOCALE" in
228         de_DE)
229             setxkbmap -layout de
230             ;;
231         *)
232             echo "no keyboard layout found for your locale"
233             ;;
234         esac
235     fi
236
237     # read cursor speed
238     if iconf mousespeed; then
239         echo "setting mousespeed"
240         instantmouse s "$(iconf mousespeed)"
241     fi
242
243     if iconf -i reversemouse; then
244         instantmouse r 1
245     else
246         instantmouse r 0
247     fi
248
249     if ! iconf -i noconky; then
250         shuf /usr/share/instantwidgets/tooltips.txt | head -1 >~/.cache/tooltip
251         conky -c /usr/share/instantwidgets/tooltips.conf &
252     fi
253
254 else
255     echo "live session detected"
256     instantmonitor
257
258     echo "disabling compositing for qxl graphics"
259     if lshw -c video | grep -i 'qxl' || xrandr | grep -i '^qxl'; then
260         iconf -i potato 1
261     fi
262
263     sudo systemctl start NetworkManager
264
265     iconf -b welcome 1
266     iconf -i wifiapplet 1
267     instantwallpaper set /usr/share/instantwallpaper/defaultphoto.png
268     conky -c /usr/share/instantwidgets/install.conf &
269     sleep 0.3
270     while :; do
271         if ! pgrep python; then
272             installapplet
273         fi &
274         sleep 6m
275     done &
276     sleep 1
277 fi
278
279 # make built in status optional
280 if ! iconf -i nostatus; then
281     source /usr/bin/instantstatus &
282 fi
283
284 offerdpi() {
285     HEIGHT=$(iconf max | grep -o '[0-9]*$')
286     WIDTH=$(iconf max | grep -o '^[0-9]*')
287     RESOLUTION="$((HEIGHT * WIDTH))"
288     DPIMESSAGE="HiDpi settings can be found in settings->display->dpi"
289     if ! imenu -C <<<"high resolution display detected
290 would you like to enable HiDpi?"; then
291         if imenu -c "ask again next time?"; then
292             return
293         fi
294         iconf -i nohidpi 1
295         imenu -m "$DPIMESSAGE"
296         return
297     fi
298
299     DPI=$(imenu -i 'enter dpi (default is 96)')
300     while ! [ "$DPI" -eq "$DPI" ] || [ "$DPI" -gt 500 ] || [ "$DPI" -lt "20" ]; do
301         imenu -m "please enter a number between 20 and 500 (default is 96), enter q to skip hidpi"
302         DPI=$(imenu -i 'enter dpi (default is 96)')
303         if grep -q 'q' <<<"$DPI"; then
304             imenu -m "$DPIMESSAGE"
305             return
306         fi
307     done
308
309     iconf dpi "$DPI"
310
311     instantdpi
312     xrdb ~/.Xresources
313     imenu -m "a restart is needed to globally apply dpi"
314
315 }
316
317 if ! iconf -i nohidpi && iconf max; then
318     if [ "$RESOLUTION" -gt 8294000 ]; then
319         offerdpi
320     fi
321 fi
322
323 # compositing
324 if iconf -i potato || iconf -i nocompositing; then
325     echo "compositing disabled"
326 else
327     ipicom &
328 fi
329
330 while :; do
331     lxpolkit
332     sleep 10
333 done &
334
335 xfce4-power-manager &
336
337 # auto open menu when connecting/disconnecting monitor
338 if ! (iconf -i noautoswitch && iconf -i islaptop) || iconf -i autoswitch; then
339
340     if nvidia-xconfig --query-gpu-info; then
341         DISPLAYCOUNT="$(nvidia-xconfig --query-gpu-info | grep -oi 'number of dis.*' | grep -o '[0-9]*')"
342     else
343         DISPLAYCOUNT="$(xrandr | grep -c '[^s]connected')"
344     fi
345
346     if [ "$DISPLAYCOUNT" -eq "$DISPLAYCOUNT" ]; then
347         while :; do
348             NEWDISPLAYCOUNT="$(xrandr | grep -c '[^s]connected')"
349             if ! [ "$DISPLAYCOUNT" = "$NEWDISPLAYCOUNT" ]; then
350                 notify-send "display changed"
351                 echo "displays changed"
352                 if [ "$NEWDISPLAYCOUNT" -gt 1 ]; then
353                     instantdisper
354                     echo "multi monitor setup"
355                 else
356                     disper -e
357                 fi
358                 DISPLAYCOUNT="$NEWDISPLAYCOUNT"
359                 # todo: open menu
360             fi
361             sleep 10
362         done &
363     else
364         echo "error detecting display count"
365     fi
366 fi
367
368 # welcome app
369 if iconf -b welcome; then
370     instantwelcome
371 fi &
372
373 # prompt to fix configuration if installed from the AUR
374 if ! iconf -i norootinstall && ! islive; then
375     if ! command -v imenu || ! command -v instantmenu; then
376         notify-send "please install instantmenu and imenu"
377     else
378         if ! [ -e /opt/instantos/rootinstall ]; then
379             imenu -m "instantOS is missing some configuration"
380             while ! [ -e /tmp/rootskip ]; do
381                 if imenu -c "would you like to fix that?"; then
382                     touch /tmp/topinstall
383                     instantsudo bash -c "instantutils root"
384                     touch /tmp/rootskip
385                 else
386                     if imenu -c "Are you sure? this will prevent parts of instantOS from functioning correctly"; then
387                         touch /tmp/rootskip
388                     fi
389                 fi
390
391             done
392         fi
393     fi
394 fi
395
396 TODAY="$(date '+%d%m')"
397 OTHERTODAY="$(iconf today)"
398
399 if [ -z "$OTHERTODAY" ]; then
400     iconf today "$(date '+%d%m')"
401     OTHERTODAY="$(iconf today)"
402 fi
403
404 if ! [ "$TODAY" = "$OTHERTODAY" ]; then
405     iconf today "$(date '+%d%m')"
406     echo "running daily routine"
407     menuclean
408 fi &
409
410 # displays message user opens the terminal for the first time
411 if ! iconf -i nohelp; then
412     if ! grep -q 'instantterminalhelp' ~/.zshrc; then
413         echo '[[ $- == *i* ]] && instantterminalhelp' >>~/.zshrc
414     fi
415 fi
416
417 confkey() {
418     [ -n "$2" ] || return
419     iconf -i "$1" || return
420     xdotool key "$2"
421 }
422
423 # run command if iconf option is set
424 confcommand() {
425     if iconf -i "$1"; then
426         shift 1
427         "$@"
428     fi &
429 }
430
431 if iconf savebright; then
432     export NOBRIGHTMESSAGE=true
433     /usr/share/instantassist/utils/b.sh 2 "$(iconf savebright)"
434 fi
435
436 confkey highfps "super+alt+shift+d"
437 confkey noanimations "super+alt+shift+s"
438 # disable wm alttab for graphical alttab
439 confkey alttab "super+alt+control+shift+Tab"
440
441 # desktop icons
442 confcommand desktopicons rox --pinboard Default
443 # auto mount disks
444 confcommand udiskie udiskie -t
445 # clipboard manager
446 confcommand clipmanager clipmenud
447
448 # user declared autostart
449 if [ -e ~/.config/instantos/autostart.sh ]; then
450     bash ~/.config/instantos/autostart.sh
451 fi &
452
453 # update notifier
454 if ! iconf -i noupdates && [ -z "$ISLIVE" ]; then
455     sleep 2m
456     if checkinternet; then
457         instantupdatenotify
458     else
459         if command -v checkinternet; then
460             while :; do
461                 sleep 5m
462                 if checkinternet; then
463                     instantupdatenotify
464                     break
465                 fi
466             done
467         fi
468     fi
469 fi &
470
471 # start processes that need to be kept running
472 while :; do
473     sleep 10
474     if iconf -i wifiapplet && ! pgrep nm-applet; then
475         echo "starting bluetooth applet"
476         nm-applet &
477     fi
478
479     if iconf -i bluetoothapplet && ! pgrep blueman-applet; then
480         echo "starting bluetooth applet"
481         blueman-applet &
482     fi
483
484     if iconf -i alttab && ! pgrep alttab; then
485         alttab -fg "#ffffff" -bg "#292F3A" -frame "#5293E1" -d 0 -s 1 -t 128x150 -i 127x64 -w 1 -vp pointer &
486     fi
487     sleep 2m
488
489 done