summaryrefslogtreecommitdiffstats
path: root/remote/modules/beamergui/data/opt/openslx/bin/screenRecognizer.sh
blob: feabe7ee16cb9bb5d7ab975c0fe745807ce73f79 (plain) (blame)
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#! /bin/bash

RES1610=(1920x1200 1680x1050 1440x900 1280x800 )
RES169=(1920x1080 1600x900 1280x720 )
RES54=(1280x1024 )
RES43=(1280x960 1152x864 1024x768 )
ALLRES=( ${RES1610[@]} ${RES169[@]} ${RES54[@]} ${RES43[@]} ) 

echo -e "\e[32mSupported resolutions\e[0m"
for i in ${ALLRES[@]}
do
  echo -n "$i - "
  echo "scale=2; $i" | tr "x" "/" | bc -l
done 

# Get initial data
XRANDR=$( xrandr )
XRANDRV=$( xrandr --verbose )

# Get the connected outputs
declare -a OUTPUTNAMES
while read line; do
  OUTPUTNAMES+=("$(echo "$line" | grep -o "^\S*" )")
done < <( echo "$XRANDR" | grep -i " connected" )
echo -e "Connected outputs: \e[32m${OUTPUTNAMES[@]}\e[0m"


if [ ${#OUTPUTNAMES[@]} -eq 1 ]; then 


  # In case of one connected output
  xrandr --auto
  exit


elif [ ${#OUTPUTNAMES[@]} -eq 2 ]; then
  

  # In case of two connected outputs
  # If one of the two connected outputs is a beamer, based on the assumption
  # that a beamer can not output a reasonable dimension.
  if ! ( echo "$XRANDR" | egrep "^${OUTPUTNAMES[0]}.*[[:digit:]]{2,}mm x [[:digit:]]{2,}mm" > /dev/null \
     && echo "$XRANDR" | egrep "^${OUTPUTNAMES[1]}.*[[:digit:]]{2,}mm x [[:digit:]]{2,}mm" > /dev/null ); then    
    

    # If one scree is a beamer. First check which one is the BEAMER
    if [[ -z "$(echo "$XRANDR" | egrep "^${OUTPUTNAMES[0]}.*[[:digit:]]{2,}mm x [[:digit:]]{2,}mm")" ]]
    then BEAMER=0; else BEAMER=1; fi
    echo -ne "${OUTPUTNAMES[$BEAMER]} is a beamer. "

  
    # Get the supported modes of the outputs
    # Get resolutions of output #0
    for RES in $( echo "$XRANDR" | grep -Pzo \
      "(?s)^${OUTPUTNAMES[0]} connected\N*\n(\t+\N*\n)*(\ +\N*\n)*" \
      | egrep -o "\ \ [[:digit:]]+x[[:digit:]]+" ); do
      OUTPUT0+=("$RES");
    done
    echo -e "\e[31mOUTPUT0:\e[0m"
    for i in ${OUTPUT0[@]}; do echo "$i";done

    # Get resolutions of output #1
    for RES in $( echo "$XRANDR" | grep -Pzo \
      "(?s)^${OUTPUTNAMES[1]} connected\N*\n(\t+\N*\n)*(\ +\N*\n)*" \
      | egrep -o "\ \ [[:digit:]]+x[[:digit:]]+" ); do
      OUTPUT1+=("$RES");
    done
    echo -e "\e[31mOUTPUT1:\e[0m"
    for i in ${OUTPUT1[@]}; do echo "$i";done


    # Find out, if the beamer transmits reliable EDID data. The data in xrandr
    # should be reliable if the EDID is present.
    if echo "$XRANDRV" | grep -Pzo \
      "^${OUTPUTNAMES[$BEAMER]}\N*\n((\ |\t)+\N*\n)+" \
      | grep EDID > /dev/null ; then


      echo "EDID present"

      # If the beamer transmits reliable EDID data, get the perfect match
      # Presumed the preferred resolution is the native one, get both native 
      # resolutions
      BEAMERPREF=$(echo "$XRANDRV" \
        | grep -Pzo "^${OUTPUTNAMES[$BEAMER]}\N*\n((\ |\t)+\N*\n)+" \
        | grep preferred | awk '{print $1}')
      echo -e "\e[32mBEAMERPREF\e[0m=$BEAMERPREF"
      MONITORPREF=$(echo "$XRANDRV" \
        | grep -Pzo "^${OUTPUTNAMES[$((1-$BEAMER))]}\N*\n((\ |\t)+\N*\n)+" \
        | grep preferred | awk '{print $1}')
      echo -e "\e[32mMONITORPREF\e[0m=$MONITORPREF"
     
      # Compute the optimal resolution for presentations 
      # which is min( 1280, M.X, B.X )
      OPTIMALRESX=$(( ${MONITORPREF%x*} <= ${BEAMERPREF%x*} ? \
                     ${MONITORPREF%x*} : ${BEAMERPREF%x*} ))
      OPTIMALRESX=$(( $OPTIMALRESX <= 1280 ? $OPTIMALRESX : 1280 ))
      AR=$(echo "$BEAMERPREF" | tr "x" "/" | bc -l) # "scale=2; 5/4"
      OPTIMALRESY=$(echo "scale=0; $OPTIMALRESX / $(echo "$AR" | bc -l)" | bc -l)
      OPTIMALRES="$OPTIMALRESX"x"$OPTIMALRESY"
      echo -e "\e[32mOPTIMALRESX\e[0m=$OPTIMALRESX"
      echo -e "\e[32mOPTIMALRESY\e[0m=$OPTIMALRESY"
      echo -e "\e[32mOPTIMALRES\e[0m=$OPTIMALRES"
      echo -e "\e[32mAR\e[0m=$AR"

      # Generate a list of common resolutions (Greedy fun)
      for RES1 in ${OUTPUT0[@]}; do
        for RES2 in ${OUTPUT1[@]}; do
          if [ $RES1 = $RES2 ]; then
            COMMONMATCHES+=("$RES1")
          fi
        done
      done
      echo -e "\e[31mCOMMONMATCHES:\e[0m"
      for i in "${COMMONMATCHES[@]}"
        do echo "$i - $( echo "scale=2; $i" | tr "x" "/" | bc -l)"
      done

      # Compute the additional resolutions
      for i in ${ALLRES[@]}; do
        if (( $( echo "$i == $AR" | tr "x" "/" | bc -l ) )); then
          if echo ${COMMONMATCHES[@]} | grep "$i" > /dev/null; then
            if [ "$OPTIMALRES" = "$i" ]; then continue; fi
            ADDITIONALRES+=("$i")
          else
            xrandr \
              --addmode ${OUTPUTNAMES[$((1-$BEAMER))]} "$i" \
              --addmode ${OUTPUTNAMES[$BEAMER]} "$i" \
            && ADDITIONALRES+=("$i")
          fi
        fi
      done
      echo -e "\e[31mADDITIONALRES:\e[0m"
      for i in "${ADDITIONALRES[@]}"
        do echo "$i - $( echo "scale=2; ${i% *}" | tr "x" "/" | bc -l)"
      done

      # Apply the optimal resolution
      xrandr \
        --addmode ${OUTPUTNAMES[$((1-$BEAMER))]} $OPTIMALRES \
        --addmode ${OUTPUTNAMES[$BEAMER]} $OPTIMALRES;
      xrandr \
        --output ${OUTPUTNAMES[$((1-$BEAMER))]} --mode $OPTIMALRES \
        --primary \
        --output ${OUTPUTNAMES[$BEAMER]} --mode $OPTIMALRES \
        --same-as ${OUTPUTNAMES[$((1-$BEAMER))]}

      # Ask for a resolution
      CHOSENRES=$( zenity --list \
        --title="Resolution Chooser" \
        --ok-label="Apply" \
        --cancel-label="Cancel" \
        --text "Choose a resolution\nor press cancel."  \
        --width=300 --height=400 \
        --column="Resolution" "$OPTIMALRES (optimal)" "${ADDITIONALRES[@]}" \
        --print-column=1 \
        --hide-header ) || exit 
      echo "Chosen resolution $CHOSENRES"


      
      # Apply resolution
      xrandr \
        --output ${OUTPUTNAMES[0]} --mode ${CHOSENRES%% *} \
        --output ${OUTPUTNAMES[1]} --mode ${CHOSENRES%% *} \
        --same-as ${OUTPUTNAMES[0]}


    # If beamer DOES NOT transmit reliable EDID data 
    else
   
      echo "EDID is not present"
     
      # Compute the additional resolutions
      for i in ${ALLRES[@]}; do
        if ! xrandr \
           --addmode ${OUTPUTNAMES[$((1-$BEAMER))]} "$i" \
           --addmode ${OUTPUTNAMES[$BEAMER]} "$i"; then
          if xrandr --newmode \
             "$i" $( cvt $(echo "$i" | tr "x" " " ) \
             | grep Modeline | cut -d " " -f3-); then
            if ! xrandr --dryrun \
               --addmode ${OUTPUTNAMES[$((1-$BEAMER))]} "$i" \
               --addmode ${OUTPUTNAMES[$BEAMER]} "$i"; then
              continue;
            fi
          else
            continue
          fi
        fi
        AR=$(echo "scale=2; $i" | tr "x" "/" | bc -l )
        case $AR in
          "1.60")
            ADDITIONALRES+=("$i (16:10)")
            ;;
          "1.77")
            ADDITIONALRES+=("$i (16:9)")
            ;;
          "1.25")
            ADDITIONALRES+=("$i (5:4)")
            ;;
          "1.33")
            ADDITIONALRES+=("$i (4:3)")
            ;;
          *)
        esac
      done
      echo -e "\e[31mADDITIONALRES:\e[0m"
      for i in "${ADDITIONALRES[@]}"
        do echo "$i - $( echo "scale=2; ${i% *}" | tr "x" "/" | bc -l)"
      done

      TEXT="The connected beamer did not transmit reliable configuration\n" \
      TEXT+="data. To avoid clipping or streching, it is recommended to\n" \
      TEXT+="choose a resolution equal to the aspect ratio of the native\n" \
      TEXT+="resolution of the beamer." \

      # Ask for a resolution
      CHOSENRES=$( zenity --list \
        --title="Resolution Chooser" \
        --ok-label="Apply" \
        --cancel-label="Cancel" \
        --text "$TEXT" \
        --width=300 --height=400 \
        --column="Resolution" "${ADDITIONALRES[@]}"  \
        --print-column=1 \
        --hide-header ) || exit 
      echo "Chosen resolution ${CHOSENRES%% *}"
      
      # Apply resolution
      xrandr \
        --addmode ${OUTPUTNAMES[0]} ${CHOSENRES%% *} \
        --addmode ${OUTPUTNAMES[1]} ${CHOSENRES%% *};
      xrandr \
        --output ${OUTPUTNAMES[$((1-$BEAMER))]} --mode ${CHOSENRES%% *} \
        --primary \
        --output ${OUTPUTNAMES[$BEAMER]} --mode ${CHOSENRES%% *} \
        --same-as ${OUTPUTNAMES[$((1-$BEAMER))]}

    fi
  else


    # If neiter of the outputs is a beamer (likely dualscreen
    # setup) just apply preferred settings
    echo "no beamer"
    xrandr \
      --output ${OUTPUTNAMES[0]} --preferred \
      --output ${OUTPUTNAMES[1]} --preferred \
      --right-of ${OUTPUTNAMES[0]}


  fi

else
  # If there are more than 3 outputs
  # its up to the user. Quit.
  exit;
fi



    # OLD APPROACH
    # Find out, if the beamer transmits reliable EDID data.
    # For the first assumption the beamer transmits reliable data, if in
    # Xorg.log '*EDID for output $BEAMER*' is succeded by '*Manufacturer*'
    # and '*Year*'.
    # BEAMERELIABILITY=1
    # while read line
    # do
    #   if echo $line | grep "EDID for output ${OUTPUTNAMES[$BEAMER]}" > /dev/null
    #   then
    #     read line
    #     if  echo $line | grep "Manufacturer" > /dev/null
    #    then
    #       BEAMERELIABILITY=0
    #       break
    #     fi
    #   fi
    # done < "/var/log/Xorg.0.log"






#    # if one of the outputs is a beamer, offer a choice
#    declare -a OUTPUT1
#    declare -a OUTPUT2
#

#    # Make sure the desired resolutions are existent
#    # FULLHD
#    #for in
#
#
#    ####################################DEBUG###################################
#    echo ${NATIVEMATCHES[@]}
#    echo -e "\e[31m--------------------------------\e[0m"
#    ############################################################################
#
#    # Add some experimental standard resolutions, which the outputs should be
#    # able to display via interpolation.
#    declare -a EXPERIMENTALMATCHES=("1920x1080 (16:9)" "1366x768 (16:9)" "1280x960 (4:3)" "1280x720 (16:9)")
#
#    ## Form an interactive dialog to suit the users preferred resolution
#    #zenity --question \
#    #  --timeout=10 \
#    #  --text="Do you want to change the resolution?" \
#    #  --ok-label "Yes" \
#    #  --cancel-label "No"
#    
#OfferChoice() 
#{
#  false; while [ "$?" != 0 ] #bash do{...}while(...) imitation
#  do
#
#
#    CHOSENRES=$( zenity --list \
#      --title="Resolution Chooser" \
#      --ok-label="Apply" \
#      --cancel-label="Cancel" \
#      --text "Choose a resolution\nor press cancel."  \
#      --width=300 --height=400 \
#      --column="Resolution" 1920x1080 1366x768 1280x720 1024x768 \
#      --print-column=1 \
#      --hide-header )
#
#    ####################################DEBUG#################################
#    echo "Chosen resolution $CHOSENRES"
#    echo -e "\e[31m--------------------------------\e[0m"
#    ##########################################################################
#
#    if [ $? != 0 ]
#    then 
#      break
#    fi
#
#    xrandr \
#      --output ${OUTPUTNAMES[0]} --mode ${CHOSENRES%% *} \
#      --output ${OUTPUTNAMES[1]} --mode ${CHOSENRES%% *} \
#      --same-as ${OUTPUTNAMES[0]}
#
#    # in case the mode is unknown
#    if [ $? != 0 ]
#    then
#      xrandr \
#        --addmode ${OUTPUTNAMES[0]} ${CHOSENRES%% *} \
#        --addmode ${OUTPUTNAMES[1]} ${CHOSENRES%% *}
#      xrandr \
#        --output ${OUTPUTNAMES[0]} --mode ${CHOSENRES%% *} --primary \
#        --output ${OUTPUTNAMES[1]} --mode ${CHOSENRES%% *} --same-as ${OUTPUTNAMES[0]}     
#
#      #CVT="$(cvt $(echo ${CHOSENRES%% *} | cut -d " " -f1 | tr "x" " ") | grep Modeline)"
#      #MODENAME=echo $CVT | cut -d " " -f2)
#      #MODELINE=echo $CVT | cut -d " " -f3-) 
#    fi
#
#    zenity --question \
#      --text="Do you want to stay with this resolution?" \
#      --ok-label "Yes" \
#      --cancel-label "No"
#
#    # In case of timeout
#    #if [ $? -eq 5]; then ;fi
#
#  done  
#}
#        # If the beamer fits in the monitor, teach the monitor the preferred
#        # mode of the beamer
#        xrandr --addmode ${OUTPUTNAMES[$((1-$BEAMER))]} $BEAMERPREF
#        xrandr --output ${OUTPUTNAMES[$((1-$BEAMER))]} \
#               --mode $BEAMERPREF \
#               --primary \
#               --output ${OUTPUTNAMES[$BEAMER]} \
#               --mode $BEAMERPREF \
#               --same-as ${OUTPUTNAMES[$((1-$BEAMER))]}
#        echo -e "\e[32mPERFECTMATCH\e[0m=$BEAMERPREF"
#
#        # If the monitor fits in the beamer, teach the beamer the preferred
#        # mode of the monitor
#        xrandr --addmode ${OUTPUTNAMES[$BEAMER]} $MONITORPREF
#        xrandr --output ${OUTPUTNAMES[$((1-$BEAMER))]} \
#               --mode $MONITORPREF \
#               --primary \
#               --output ${OUTPUTNAMES[$BEAMER]} \
#               --mode $MONITORPREF \
#               --same-as ${OUTPUTNAMES[$((1-$BEAMER))]}
#
#
#
#
#
#       Offer a mode selection
#       1920x1080, 1366x768, 1280x800 and 1024x800 have to be available.
#       Check if the TFT supports the resolution 
#      if echo "$XRANDRV" \
#        | grep -Pzo "^${OUTPUTNAMES[$((1-$BEAMER))]}\N*\n((\ |\t)+\N*\n)+" \
#        | grep $MODE > /dev/null
#      then
#
#        # If so, just assign the beamers preferred resolution to the TFT
#        xrandr \
#          --output ${OUTPUTNAMES[0]} --mode $MODE \
#          --output ${OUTPUTNAMES[1]} --mode $MODE \
#          --same-as ${OUTPUTNAMES[0]} 
#      else
#
#        # If not, teach the TFT the mode
#        xrandr \
#          --addmode ${OUTPUTNAMES[$((1-$BEAMER))]} $MODE
#          --output  ${OUTPUTNAMES[0]} --mode $MODE \
#          --output  ${OUTPUTNAMES[1]} --mode $MODE \
#          --same-as ${OUTPUTNAMES[0]}
#      fi
#
#      #OfferChoice
#      #echo "Offered choice"