OSDN Git Service

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