#
# This file is a part of the NsCDE - Not so Common Desktop Environment
# Author: Hegel3DReloaded
# Licence: GPLv3
#

UseGettext {$NSCDE_ROOT/share/locale;NsCDE-PowerSaveMgr}
WindowLocaleTitle {Style Manager - Powersave}
WindowSize 386 420
Colorset 22

Init
Begin
   Set $DefFont = (GetOutput {$NSCDE_TOOLSDIR/getfont -v -t normal -s medium -Z 12} 1 -1)
   ChangeFont 4 $DefFont
   ChangeFont 7 $DefFont
   ChangeFont 9 $DefFont
   ChangeFont 6 $DefFont

   Set $LabelFont = (GetOutput {$NSCDE_TOOLSDIR/getfont -v -t normal -s medium -Z 11} 1 -1)
   ChangeFont 5 $LabelFont
   ChangeFont 8 $LabelFont
   ChangeFont 10 $LabelFont

   Set $BtnFont = (GetOutput {$NSCDE_TOOLSDIR/getfont -v -t normal -s medium -Z 14} 1 -1)
   ChangeFont 1 $BtnFont
   ChangeFont 2 $BtnFont
   ChangeFont 3 $BtnFont
   ChangeFont 13 $BtnFont
   ChangeFont 14 $BtnFont

   Set $genfile = (GetOutput {sh -c "echo $FVWM_USERDIR/Xset.conf"} -1 1)
   Set $FILE = $genfile
   Set $Wrong = 0

   Set $pos=0
   Set $finish=1
   While $finish=={1} Do
   Begin
      Set $pos=(Add $pos 1)
      Set $tmp=(GetOutput {xset -q} $pos 1)
      If $tmp=={Standby:} Then
         Set $finish=0
      If $pos=={50} Then
         Set $finish=0
   End

   Set $pos2=(Add $pos 1)

   If $pos == {50} Then
   Begin
      Do {None ("Power Style Manager Error")}
          { f_Notifier "Power Style Manager Error" "Dismiss" "NsCDE/Error.xpm" }
          {"Error: Incompatible xset(1) tool detected or there is no DPMS functionality on this display." "e1"}
      Quit
   End

   ChangeValue 4 (GetOutput {xset -q} $pos 2)
   ChangeValue 7 (GetOutput {xset -q} $pos 6)
   ChangeValue 9 (GetOutput {xset -q} $pos 4)

   Set $Enabled = (GetOutput {xset -q} $pos2 3)

   If $Enabled == {Disabled} Then
   Begin
      ChangeValue 6 0
      HideWidget 4
      HideWidget 5
      HideWidget 7
      HideWidget 8
      HideWidget 9
      HideWidget 10
   End

   # XScreenSaver Integration / sync in DPMS part
   Set $XSCREENSAVERCONF = (GetOutput {echo $HOME/.xscreensaver} -1 1)

   # Key bindings
   Key Escape A 3 1 {KeyClose}
   Key Q C 3 1 {KeyClose}
   Key Return M 1 1 {KeyApply}
   Key Return C 2 2 {KeySave}
   Key F1 A 13 1 {KeyHelp}
   Key Help A 13 1 {KeyHelp}

   # Locale stuff: Hack to keep leading or ending space(s)
   # which will be trimmed by LocaleTitle Widget directive.
   ChangeLocaleTitle 6 (GetTitle 6)
   # ChangeLocaleTitle 5 (GetTitle 5)
   # ChangeLocaleTitle 8 (GetTitle 8)
   # ChangeLocaleTitle 10 (GetTitle 10)
End
 
Widget 1
   Property
   Size 86 0
   Position 10 378
   Flags NoReliefString
   Type PushButton
   LocaleTitle {Apply}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
         SendSignal 1 1
      End
      1 :
      Begin
         If (GetValue 4) > (GetValue 9) Then
         Begin
            Do {f_Notifier "Powersave Style Manager" "Dismiss" "NsCDE/Warning.xpm" "Standby time cannot be bigger than Suspend time." "e2"}
            Set $Wrong = 1
         End
         Else
         Begin
            Set $Wrong = 0
         End

         If (GetValue 4) > (GetValue 7) Then
         Begin
            Do {f_Notifier "Powersave Style Manager" "Dismiss" "NsCDE/Warning.xpm" "Standby time cannot be bigger than Off time." "e3"}
            Set $Wrong = 1
         End
         Else
         Begin
            Set $Wrong = 0
         End

         If (GetValue 9) > (GetValue 7) Then
         Begin
            Do {f_Notifier "Powersave Style Manager" "Dismiss" "NsCDE/Warning.xpm" "Suspend time cannot be bigger than Off time." "e4"}
            Set $Wrong = 1
         End
         Else
         Begin
            Set $Wrong = 0
         End

         If (GetValue 9) < (GetValue 4) Then
         Begin
            Do {f_Notifier "Powersave Style Manager" "Dismiss" "NsCDE/Warning.xpm" "Suspend time cannot be smaller than Standby time." "e5"}
            Set $Wrong = 1
         End
         Else
         Begin
            Set $Wrong = 0
         End

         If (GetBack 4) == {ff0000} Then
         Begin
            Set $Wrong = 1
         End

         If (GetBack 7) == {ff0000} Then
         Begin
            Set $Wrong = 1
         End

         If (GetBack 9)=={ff0000} Then
         Begin
            Set $Wrong = 1
         End

         If $Wrong == 0 Then
         Begin
            If (GetValue 6) == 1 Then
            Begin
               Do {Exec xset +dpms dpms } (GetValue 4) { } (GetValue 9) { } (GetValue 7)
            End
            Else
            Begin
               Do {Exec xset -dpms }
            End
         End
      End
End

Widget 2
   Property
   Size 86 0
   Position 104 378
   Flags NoReliefString
   Type PushButton
   LocaleTitle {Save}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
         SendSignal 2 2
      End
      2 :
      Begin
         If (GetValue 4) > (GetValue 9) Then
         Begin
            Do {f_Notifier "Powersave Style Manager" "Dismiss" "NsCDE/Warning.xpm" "Standby time cannot be bigger than Suspend time." "e6"}
            Set $Wrong = 1
         End
         Else
         Begin
            Set $Wrong = 0
         End

         If (GetValue 4) > (GetValue 7) Then
         Begin
            Do {f_Notifier "Powersave Style Manager" "Dismiss" "NsCDE/Warning.xpm" "Standby time cannot be bigger than Off time." "e7"}
            Set $Wrong = 1
         End
         Else
         Begin
            Set $Wrong = 0
         End

         If (GetValue 9) > (GetValue 7) Then
         Begin
            Do {f_Notifier "Powersave Style Manager" "Dismiss" "NsCDE/Warning.xpm" "Suspend time cannot be bigger than Off time." "e8"}
            Set $Wrong = 1
         End
         Else
         Begin
            Set $Wrong = 0
         End

         If (GetValue 9) < (GetValue 4) Then
         Begin
            Do {f_Notifier "Powersave Style Manager" "Dismiss" "NsCDE/Warning.xpm" "Suspend time cannot be bigger than Standby time." "e9"}
            Set $Wrong = 1
         End
         Else
         Begin
            Set $Wrong = 0
         End

         If $Wrong == 0 Then
         Begin
            # Workaround for FvwmScript WriteToFile which truncates existing file if it
            # doesn't find script's signature in it.
            Set $CheckEntryCmd = {egrep -q '^#PowerSaveMgr,[1-9]*' "} $FILE {"; echo $?}
            Set $CheckEntry = (GetOutput $CheckEntryCmd 1 -1)

            If $CheckEntry <> 0 Then
            Begin
               Set $PrepareFileCmd = {(echo '#PowerSaveMgr,00000'; echo '#end') >> "} $FILE {"}
               Set $PrepareFile = (GetOutput $PrepareFileCmd 1 -1)
            End

            If (GetValue 6) == 1 Then
            Begin
               Do {Exec xset dpms } (GetValue 4) { } (GetValue 9) { } (GetValue 7)
               Do {Exec xset +dpms}
               WriteToFile $FILE {xset +dpms}
               If $XSCREENSAVERCONF <> {} Then
               Begin
                  Set $EditXscOneCmd= {$NSCDE_TOOLSDIR/ised -c 's/dpmsEnabled:.*/dpmsEnabled:	True/g' -f "} $XSCREENSAVERCONF {"}
                  Set $EditXscOne = (GetOutput $EditXscOneCmd 1 -1)
               End
            End
            Else
            Begin
               Do {Exec xset -dpms}
               WriteToFile $FILE {xset -dpms}
               If $XSCREENSAVERCONF <> {} Then
               Begin
                  Do {Exec $NSCDE_TOOLSDIR/ised -c 's/dpmsEnabled:.*/dpmsEnabled:	False/g' -f "} $XSCREENSAVERCONF {"}
               End
            End

            If (GetValue 6) == 1 Then
            Begin
               Do {Exec xset dpms } (GetValue 4) { } (GetValue 9) { } (GetValue 7)
               WriteToFile $FILE {xset dpms } (GetValue 4) { } (GetValue 9) { } (GetValue 7)
               If $XSCREENSAVERCONF <> {} Then
               Begin
                  Set $RawSec = (GetValue 4)
                  SendSignal 2 1
                  Set $StandbyTime = $SecToHour {:} $SecToMin {:} $SecToSec
                  Set $RawSec = (GetValue 9)
                  SendSignal 2 1
                  Set $SuspendTime = $SecToHour {:} $SecToMin {:} $SecToSec
                  Set $RawSec = (GetValue 7)
                  SendSignal 2 1
                  Set $OffTime = $SecToHour {:} $SecToMin {:} $SecToSec

                  Set $EditXscTwoCmd = {$NSCDE_TOOLSDIR/ised -c 's/^dpmsStandby:.*/dpmsStandby:	} $StandbyTime {/g' -f "} $XSCREENSAVERCONF {"}
                  Set $EditXscTwo = (GetOutput $EditXscTwoCmd 1 -1)
                  Set $EditXscThreeCmd = {$NSCDE_TOOLSDIR/ised -c 's/^dpmsSuspend:.*/dpmsSuspend:	} $SuspendTime {/g' -f "} $XSCREENSAVERCONF {"}
                  Set $EditXscThree = (GetOutput $EditXscThreeCmd 1 -1)
                  Set $EditXscFourCmd = {$NSCDE_TOOLSDIR/ised -c 's/^dpmsOff:.*/dpmsOff:	} $OffTime {/g' -f "} $XSCREENSAVERCONF {"}
                  Set $EditXscFour = (GetOutput $EditXscFourCmd 1 -1)
               End
            End

            Quit
         End
      End
      1 :
      Begin
         Set $SecToHourCmd = {/usr/bin/env ksh03 -c 'echo $((} $RawSec {/3600))'}
         Set $SecToHour = (GetOutput $SecToHourCmd 1 -1)
         Set $SecToMinCmd = {/usr/bin/ksh93 -c 'echo $(((} $RawSec {%3600)/60))'}
         Set $SecToMin = (GetOutput $SecToMinCmd 1 -1)
         Set $SecToSecCmd = {/usr/bin/ksh93 -c 'echo $(((} $RawSec {%60)))'}
         Set $SecToSec = (GetOutput $SecToSecCmd 1 -1)
         If (StrCopy $SecToHour 2 2) == {} Then
            Begin
               Set $SecToHour = {0} $SecToHour
            End
            If (StrCopy $SecToMin 2 2) == {} Then
            Begin
               Set $SecToMin = {0} $SecToMin
            End
            If (StrCopy $SecToSec 2 2) == {} Then
            Begin
               Set $SecToSec = {0} $SecToSec
            End
      End
End

Widget 3
   Property
   Size 86 0
   Position 197 378
   Flags NoReliefString
   Type PushButton
   LocaleTitle {Dismiss}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
         SendSignal 3 1
      End
      1 :
      Begin
         Quit
      End
End

Widget 4
   Property
   Size 352 2
   Position 16 116
   Flags NoReliefString
   Type HScrollBar
   Value 0
   Font "xft:::pixelsize=15"
   MinValue 0
   MaxValue 65535
   Main
      Case message of
      SingleClic :
      Begin
         ChangeColorset 2 20
      End
End

# Title spaces because of locale string space keeping
Widget 5
   Property
   Position 18 94
   Size 280 0
   Flags NoReliefString NoFocus Left
   Type ItemDraw
   LocaleTitle {Standby time after (seconds):}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
      End
End

# Title spaces because of locale string space keeping
Widget 6
   Property
   Position 18 62
   Flags NoReliefString
   Type CheckBox
   Title { DPMS Enabled/Disabled                }
   Font "xft:::pixelsize=15"
   Value 1
   Main
      Case message of
      SingleClic :
      Begin
         If (GetValue 6) == 0 Then
         Begin
            HideWidget 4
            HideWidget 5
            HideWidget 7
            HideWidget 8
            HideWidget 9
            HideWidget 10
         End
         Else
         Begin
            ShowWidget 4
            ShowWidget 5
            ShowWidget 7
            ShowWidget 8
            ShowWidget 9
            ShowWidget 10
         End
      End
End

Widget 7
   Property
   Size 352 1
   Position 16 288
   Flags NoReliefString
   Type HScrollBar
   Value 0
   Font "xft:::pixelsize=15"
   MinValue 0
   MaxValue 65535
   Main
      Case message of
      SingleClic :
      Begin
         ChangeColorset 2 20
      End
End

# Title spaces because of locale string space keeping
Widget 8
   Property
   Position 18 268
   Size 280 0
   Flags NoReliefString NoFocus Left
   Type ItemDraw
   Title {Off time after (seconds):}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 9
   Property
   Size 352 1
   Position 16 202
   Flags NoReliefString
   Type HScrollBar
   Value 0
   Font "xft:::pixelsize=15"
   MinValue 0
   MaxValue 65535
   Main
      Case message of
      SingleClic :
      Begin
         ChangeColorset 2 20
      End
End

# Title spaces because of locale string space keeping
Widget 10
   Property
   Position 18 182
   Size 280 0
   Flags NoReliefString NoFocus Left
   Type ItemDraw
   Title {Suspend time after (seconds):}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 11
   Property
   Size 380 0
   Position 2 360
   Type Rectangle
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 12
   Property
   Position 12 12
   Flags NoReliefString NoFocus
   Type ItemDraw
   Title {}
   Icon NsCDE/Dtpowermgr.m.pm
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 13
   Property
   Size 86 0
   Position 290 378
   Flags NoReliefString
   Type PushButton
   LocaleTitle {Help}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
         SendSignal 13 1
      End
      1 :
      Begin
         Do {f_DisplayURL "$[gt.Power Style Manager]" html/NsCDE-PowerSaveMgr.html}
      End
End

Widget 14
   Property
   Size 80 0
   Position 292 12
   Flags NoReliefString
   Type PushButton
   LocaleTitle {Default}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
         ShowWidget 4
         ShowWidget 5
         ShowWidget 7
         ShowWidget 8
         ShowWidget 9
         ShowWidget 10
         ChangeValue 6 1
         ChangeValue 4 1200
         ChangeValue 9 1800
         ChangeValue 7 2400
         Do {Exec xset +dpms dpms } (GetValue 4) { } (GetValue 9) { } (GetValue 7)
         WarpPointer 2
         ChangeColorset 4 22
         ChangeColorset 9 22
         ChangeColorset 7 22
         ChangeColorset 2 20
      End
End

