OSDN Git Service

fix dpi offer
[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 echo "virtual machine detected.
161 Would you like to switch to a 1080p resolution?" | imenu -C; then
162             echo "applying virtual machine workaround"
163             /usr/share/instantassist/assists/t/v.sh
164         else
165             if ! imenu -c "ask again next session"; then
166                 iconf -i novmfix 1
167             fi
168         fi
169     fi
170 fi
171
172 if ! islive; then
173     echo "not a live session"
174     if [ -e /opt/instantos/installtrigger ]; then
175         zenity --info --text "finishing installation in background" &
176
177         # ask for password if postinstall already ran
178         if ! timeout 2 sudo echo test; then
179             instantsudo instantpostinstall
180             sudo rm /opt/instantos/installtrigger
181         else
182             sudo instantpostinstall
183         fi
184
185         pkill zenity
186     fi
187
188     cd ~/instantos || exit 1
189     if ! iconf -i max; then
190         instantmonitor
191     fi
192
193     if [ -e ~/instantos/monitor.sh ]; then
194         echo "restoring resolution"
195         bash ~/instantos/monitor.sh &
196     elif [ -e ~/.config/autorandr/instantos/config ]; then
197         echo "restoring autorandr resolution"
198         autorandr instantos &
199     fi
200
201     if checkinternet; then
202         onlinetrigger
203     else
204         # fall back to already installed wallpapers
205         instantwallpaper offline
206         for i in $(seq 10); do
207             if checkinternet; then
208                 onlinetrigger
209                 break
210             else
211                 sleep 10
212             fi
213         done
214     fi &
215
216     # apply keybpard layout
217     if iconf layout; then
218         setxkbmap -layout "$(iconf layout)"
219     else
220         CURLOCALE=$(locale | grep LANG | sed 's/.*=\(.*\)\..*/\1/')
221         case "$CURLOCALE" in
222         de_DE)
223             setxkbmap -layout de
224             ;;
225         *)
226             echo "no keyboard layout found for your locale"
227             ;;
228         esac
229     fi
230
231     # read cursor speed
232     if iconf mousespeed; then
233         echo "setting mousespeed"
234         instantmouse s "$(iconf mousespeed)"
235     fi
236
237     if iconf -i reversemouse; then
238         instantmouse r 1
239     else
240         instantmouse r 0
241     fi
242
243     if ! iconf -i noconky; then
244         shuf /usr/share/instantwidgets/tooltips.txt | head -1 >~/.cache/tooltip
245         conky -c /usr/share/instantwidgets/tooltips.conf &
246     fi
247
248 else
249     echo "live session detected"
250     instantmonitor
251
252     echo "disabling compositing for qxl graphics"
253     if lshw -c video | grep -i 'qxl' || xrandr | grep -i '^qxl'; then
254         iconf -i potato 1
255     fi
256
257     sudo systemctl start NetworkManager
258
259     iconf -b welcome 1
260     iconf -i wifiapplet 1
261     instantwallpaper set /usr/share/instantwallpaper/defaultphoto.png
262     conky -c /usr/share/instantwidgets/install.conf &
263     sleep 0.3
264     while :; do
265         if ! pgrep python; then
266             installapplet
267         fi &
268         sleep 6m
269     done &
270     sleep 1
271 fi
272
273 # make built in status optional
274 if ! iconf -i nostatus; then
275     source /usr/bin/instantstatus &
276 fi
277
278 offerdpi() {
279     HEIGHT=$(iconf max | grep -o '[0-9]*$')
280     WIDTH=$(iconf max | grep -o '^[0-9]*')
281     RESOLUTION="$((HEIGHT * WIDTH))"
282     DPIMESSAGE="HiDpi settings can be found in settings->display->dpi"
283     if ! imenu -C <<<"high resolution display detected
284 would you like to enable HiDpi?"; then
285         if imenu -c "ask again next time?"; then
286             return
287         fi
288         iconf -i nohidpi 1
289         imenu -m "$DPIMESSAGE"
290         return
291     fi
292
293     DPI=$(imenu -i 'enter dpi (default is 96)')
294     while ! [ "$DPI" -eq "$DPI" ] || [ "$DPI" -gt 500 ] || [ "$DPI" -lt "20" ]; do
295         imenu -m "please enter a number between 20 and 500 (default is 96), enter q to skip hidpi"
296         DPI=$(imenu -i 'enter dpi (default is 96)')
297         if grep -q 'q' <<<"$DPI"; then
298             imenu -m "$DPIMESSAGE"
299             return
300         fi
301     done
302
303     iconf dpi "$DPI"
304
305     instantdpi
306     xrdb ~/.Xresources
307     imenu -m "a restart is needed to globally apply dpi"
308
309 }
310
311 if ! iconf -i nohidpi && iconf max; then
312     if [ "$RESOLUTION" -gt 8294000 ]; then
313         offerdpi
314     fi
315 fi
316
317 # compositing
318 if iconf -i potato || iconf -i nocompositing; then
319     echo "compositing disabled"
320 else
321     ipicom &
322 fi
323
324 while :; do
325     lxpolkit
326     sleep 10
327 done &
328
329 xfce4-power-manager &
330
331 # auto open menu when connecting/disconnecting monitor
332 if ! (iconf -i noautoswitch && iconf -i islaptop) || iconf -i autoswitch; then
333
334     if nvidia-xconfig --query-gpu-info; then
335         DISPLAYCOUNT="$(nvidia-xconfig --query-gpu-info | grep -oi 'number of dis.*' | grep -o '[0-9]*')"
336     else
337         DISPLAYCOUNT="$(xrandr | grep -c '[^s]connected')"
338     fi
339
340     if [ "$DISPLAYCOUNT" -eq "$DISPLAYCOUNT" ]; then
341         while :; do
342             NEWDISPLAYCOUNT="$(xrandr | grep -c '[^s]connected')"
343             if ! [ "$DISPLAYCOUNT" = "$NEWDISPLAYCOUNT" ]; then
344                 notify-send "display changed"
345                 echo "displays changed"
346                 if [ "$NEWDISPLAYCOUNT" -gt 1 ]; then
347                     instantdisper
348                     echo "multi monitor setup"
349                 else
350                     disper -e
351                 fi
352                 DISPLAYCOUNT="$NEWDISPLAYCOUNT"
353                 # todo: open menu
354             fi
355             sleep 10
356         done &
357     else
358         echo "error detecting display count"
359     fi
360 fi
361
362 # welcome app
363 if iconf -b welcome; then
364     instantwelcome
365 fi &
366
367 # prompt to fix configuration if installed from the AUR
368 if ! iconf -i norootinstall && ! islive; then
369     if ! command -v imenu || ! command -v instantmenu; then
370         notify-send "please install instantmenu and imenu"
371     else
372         if ! [ -e /opt/instantos/rootinstall ]; then
373             imenu -m "instantOS is missing some configuration"
374             while ! [ -e /tmp/rootskip ]; do
375                 if imenu -c "would you like to fix that?"; then
376                     touch /tmp/topinstall
377                     instantsudo bash -c "instantutils root"
378                     touch /tmp/rootskip
379                 else
380                     if imenu -c "Are you sure? this will prevent parts of instantOS from functioning correctly"; then
381                         touch /tmp/rootskip
382                     fi
383                 fi
384
385             done
386         fi
387     fi
388 fi
389
390 TODAY="$(date '+%d%m')"
391 OTHERTODAY="$(iconf today)"
392
393 if [ -z "$OTHERTODAY" ]; then
394     iconf today "$(date '+%d%m')"
395     OTHERTODAY="$(iconf today)"
396 fi
397
398 if ! [ "$TODAY" = "$OTHERTODAY" ]; then
399     iconf today "$(date '+%d%m')"
400     echo "running daily routine"
401     menuclean
402 fi &
403
404 # displays message user opens the terminal for the first time
405 if ! iconf -i nohelp; then
406     if ! grep -q 'instantterminalhelp' ~/.zshrc; then
407         echo '[[ $- == *i* ]] && instantterminalhelp' >>~/.zshrc
408     fi
409 fi
410
411 confkey() {
412     [ -n "$2" ] || return
413     iconf -i "$1" || return
414     xdotool key "$2"
415 }
416
417 # run command if iconf option is set
418 confcommand() {
419     if iconf -i "$1"; then
420         shift 1
421         "$@"
422     fi &
423 }
424
425 if iconf savebright; then
426     /usr/share/instantassist/utils/b.sh 2 "$(iconf savebright)"
427 fi
428
429 confkey highfps "super+alt+shift+d"
430 confkey noanimations "super+alt+shift+s"
431 # disable wm alttab for graphical alttab
432 confkey alttab "super+alt+control+shift+Tab"
433
434 # desktop icons
435 confcommand desktopicons rox --pinboard Default
436 # auto mount disks
437 confcommand udiskie udiskie -t
438 # clipboard manager
439 confcommand clipmanager clipmenud
440
441 # user declared autostart
442 if [ -e ~/.config/instantos/autostart.sh ]; then
443     bash ~/.config/instantos/autostart.sh
444 fi &
445
446 # update notifier
447 if ! iconf -i noupdates; then
448     sleep 2m
449     if checkinternet; then
450         instantupdatenotify
451     else
452         if command -v checkinternet; then
453             while :; do
454                 sleep 5m
455                 if checkinternet; then
456                     instantupdatenotify
457                     break
458                 fi
459             done
460         fi
461     fi
462 fi &
463
464 # start processes that need to be kept running
465 while :; do
466     sleep 10
467     if iconf -i wifiapplet && ! pgrep nm-applet; then
468         echo "starting bluetooth applet"
469         nm-applet &
470     fi
471
472     if iconf -i bluetoothapplet && ! pgrep blueman-applet; then
473         echo "starting bluetooth applet"
474         blueman-applet &
475     fi
476
477     if iconf -i alttab && ! pgrep alttab; then
478         alttab -fg "#ffffff" -bg "#292F3A" -frame "#5293E1" -d 0 -s 1 -t 128x150 -i 127x64 -w 1 -vp pointer &
479     fi
480     sleep 2m
481
482 done