eef74d0e26da4679522f3b62068ab72505fc66c5
[enigma2.git] / lib / python / Components / VolumeControl.py
1 from enigma import eDVBVolumecontrol, eTimer
2 from Tools.Profile import profile
3 from Screens.Volume import Volume
4 from Screens.Mute import Mute
5 from GlobalActions import globalActionMap
6 from config import config, ConfigSubsection, ConfigInteger
7
8 instance = None
9
10 profile("VolumeControl")
11 #TODO .. move this to a own .py file
12 class VolumeControl:
13         """Volume control, handles volUp, volDown, volMute actions and display
14         a corresponding dialog"""
15         def __init__(self, session):
16                 global globalActionMap
17                 global instance
18                 globalActionMap.actions["volumeUp"]=self.volUp
19                 globalActionMap.actions["volumeDown"]=self.volDown
20                 globalActionMap.actions["volumeMute"]=self.volMute
21
22                 assert not instance, "only one VolumeControl instance is allowed!"
23                 instance = self
24
25                 config.audio = ConfigSubsection()
26                 config.audio.volume = ConfigInteger(default = 100, limits = (0, 100))
27
28                 self.volumeDialog = session.instantiateDialog(Volume)
29                 self.muteDialog = session.instantiateDialog(Mute)
30
31                 self.hideVolTimer = eTimer()
32                 self.hideVolTimer.callback.append(self.volHide)
33
34                 vol = config.audio.volume.value
35                 self.volumeDialog.setValue(vol)
36                 self.volctrl = eDVBVolumecontrol.getInstance()
37                 self.volctrl.setVolume(vol, vol)
38
39         def volSave(self):
40                 if self.volctrl.isMuted():
41                         config.audio.volume.value = 0
42                 else:
43                         config.audio.volume.value = self.volctrl.getVolume()
44                 config.audio.volume.save()
45
46         def volUp(self):
47                 self.setVolume(+1)
48
49         def volDown(self):
50                 self.setVolume(-1)
51
52         def setVolume(self, direction):
53                 oldvol = self.volctrl.getVolume()
54                 if direction > 0:
55                         self.volctrl.volumeUp()
56                 else:
57                         self.volctrl.volumeDown()
58                 is_muted = self.volctrl.isMuted()
59                 vol = self.volctrl.getVolume()
60                 self.volumeDialog.show()
61                 if is_muted:
62                         self.volMute() # unmute
63                 elif not vol:
64                         self.volMute(False, True) # mute but dont show mute symbol
65                 if self.volctrl.isMuted():
66                         self.volumeDialog.setValue(0)
67                 else:
68                         self.volumeDialog.setValue(self.volctrl.getVolume())
69                 self.volSave()
70                 self.hideVolTimer.start(3000, True)
71
72         def volHide(self):
73                 self.volumeDialog.hide()
74
75         def volMute(self, showMuteSymbol=True, force=False):
76                 vol = self.volctrl.getVolume()
77                 if vol or force:
78                         self.volctrl.volumeToggleMute()
79                         if self.volctrl.isMuted():
80                                 if showMuteSymbol:
81                                         self.muteDialog.show()
82                                 self.volumeDialog.setValue(0)
83                         else:
84                                 self.muteDialog.hide()
85                                 self.volumeDialog.setValue(vol)