def pa_sink_changed_cb(self, obj, index):
     obj.get_devices()
     current_sink = pypulse.get_fallback_sink_index()
     if current_sink is None or current_sink != index:
         return
     self.__set_output_status()
     self.__set_output_port_status()
 def speaker_port_changed(self, combo, content, value, index):
     current_sink = pypulse.get_fallback_sink_index()
     if current_sink is None:
         return
     if value in self.__current_sink_ports:
         port = self.__current_sink_ports[value][0]
         pypulse.PULSE.set_output_active_port(current_sink, port)
         self.set_status_text(_("Set output port to %s") % content)
 def speaker_value_changed_cb(self, widget, value):
     ''' speaker hscale value changed callback thread'''
     if not self.button_widgets["speaker"].get_active():
         self.button_widgets["speaker"].set_active(True)
     current_sink = pypulse.get_fallback_sink_index()
     if current_sink is None:
         return
     balance = self.scale_widgets["balance"].get_value()
     volume = int((self.scale_widgets["speaker"].get_value()) / 100.0 * pypulse.NORMAL_VOLUME_VALUE)
     pypulse.PULSE.set_output_volume_with_balance(current_sink, volume, balance)
    def __set_output_status(self):
        # if sinks list is empty, then can't set output volume
        current_sink = pypulse.get_fallback_sink_index()
        sinks = pypulse.PULSE.get_output_devices()
        if current_sink is None:
            self.label_widgets["speaker_port"].set_sensitive(False)
            self.label_widgets["speaker_mute"].set_sensitive(False)
            self.label_widgets["speaker_volume"].set_sensitive(False)
            self.label_widgets["speaker_balance"].set_sensitive(False)

            self.button_widgets["speaker_combo"].set_sensitive(False)
            self.button_widgets["speaker"].set_sensitive(False)
            self.scale_widgets["speaker"].set_sensitive(False)
            self.scale_widgets["balance"].set_sensitive(False)

            if self.button_widgets["speaker"].get_active() and not self.__first_time:
                self.button_widgets["speaker"].set_data("change-by-other", True)
            self.button_widgets["speaker"].set_active(False)
            self.scale_widgets["speaker"].set_enable(False)
            self.scale_widgets["balance"].set_enable(False)
        # set output volume
        elif current_sink in sinks:
            self.label_widgets["speaker_port"].set_sensitive(True)
            self.label_widgets["speaker_mute"].set_sensitive(True)
            self.label_widgets["speaker_volume"].set_sensitive(True)
            self.label_widgets["speaker_balance"].set_sensitive(True)

            self.button_widgets["speaker_combo"].set_sensitive(True)
            self.button_widgets["speaker"].set_sensitive(True)
            self.scale_widgets["speaker"].set_sensitive(True)
            self.scale_widgets["balance"].set_sensitive(True)

            is_mute = sinks[current_sink]['mute']
            if self.button_widgets["speaker"].get_active() == is_mute and not self.__first_time:
                self.button_widgets["speaker"].set_data("change-by-other", True)
            self.button_widgets["speaker"].set_active(not is_mute)
            self.scale_widgets["speaker"].set_enable(not is_mute)
            self.scale_widgets["balance"].set_enable(not is_mute)

            sink_volume = pypulse.PULSE.get_output_volume()
            sink_channel = pypulse.PULSE.get_output_channels_by_index(current_sink)
            balance = None
            if current_sink in sink_volume:
                volume = max(sink_volume[current_sink])
                if sink_channel and sink_channel['can_balance']:
                    balance = pypulse.get_volume_balance(sink_channel['channels'],
                                                         sink_volume[current_sink],
                                                         sink_channel['map'])
                if balance is None:
                    balance = 0
            else:
                volume = 0
                balance = 0
            self.scale_widgets["speaker"].set_value(volume * 100.0 / pypulse.NORMAL_VOLUME_VALUE)
            self.scale_widgets["balance"].set_value(balance)
 def speaker_value_changed_thread(self):
     ''' speaker hscale value changed callback thread'''
     if not self.speaker_mute_button.get_active():
         self.speaker_mute_button.set_active(True)
     current_sink = pypulse.get_fallback_sink_index()
     if current_sink is None:
         return
     volume_list = pypulse.PULSE.get_output_volume_by_index(current_sink)
     channel_list = pypulse.PULSE.get_output_channels_by_index(current_sink)
     if not volume_list or not channel_list:
         return
     balance = pypulse.get_volume_balance(channel_list['channels'], volume_list, channel_list['map'])
     volume = int((self.speaker_scale.get_value()) / 100.0 * pypulse.NORMAL_VOLUME_VALUE)
     pypulse.PULSE.set_output_volume_with_balance(current_sink, volume, balance)
 def speaker_toggled_cb(self, button):
     if button.get_data("change-by-other"):
         button.set_data("change-by-other", False)
         return
     active = button.get_active()
     current_sink = pypulse.get_fallback_sink_index()
     self.scale_widgets["speaker"].set_enable(active)
     if current_sink is not None:
         pypulse.PULSE.set_output_mute(current_sink, not active)
     if not active:
         self.label_widgets["speaker_volume"].set_text("<span foreground=\"%s\">%s</span>" % (MUTE_TEXT_COLOR, _("Output Volume")))
         self.label_widgets["speaker_balance"].set_text("<span foreground=\"%s\">%s</span>" % (MUTE_TEXT_COLOR, _("Balance")))
         self.set_status_text(_("Muted output"))
     else:
         self.label_widgets["speaker_volume"].set_text("%s" % _("Output Volume"))
         self.label_widgets["speaker_balance"].set_text("%s" % _("Balance"))
         self.set_status_text(_("Unmuted output"))
 def __set_output_treeview_status(self):
     current_sink = pypulse.get_fallback_sink_index()
     sinks = pypulse.PULSE.get_output_devices()
     output_list = []
     i = 0
     selected_row = -1
     for idx in sinks:
         if current_sink is not None and current_sink == idx:
             selected_row = i
         if 'device.description' in sinks[idx]['proplist']:
             description = sinks[idx]['proplist']['device.description'].strip('\x00')
         else:
             description = ""
         output_list.append(TreeItem(self.image_widgets["device"], description, sinks[idx]['name'], idx))
         i += 1
     self.view_widgets["ad_output"].add_items(output_list, clear_first=True)
     if not (selected_row < 0):
         self.view_widgets["ad_output"].set_select_rows([selected_row])
 def pa_server_changed_cb(self, obj):
     obj.get_devices()
     self.__set_output_status()
     self.__set_output_port_status()
     current_sink = pypulse.get_fallback_sink_index()
     if current_sink is not None:
         for item in self.view_widgets["ad_output"].get_items():
             if item.device_index == current_sink:
                 self.view_widgets["ad_output"].select_items([item])
                 break
     self.__set_input_status()
     self.__set_input_port_status()
     current_source = pypulse.get_fallback_source_index()
     if current_source is not None:
         for item in self.view_widgets["ad_input"].get_items():
             if item.device_index == current_source:
                 self.view_widgets["ad_input"].select_items([item])
                 break
 def __set_output_port_status(self):
     current_sink = pypulse.get_fallback_sink_index()
     sinks = pypulse.PULSE.get_output_devices()
     self.__current_sink_ports= {}
     if current_sink is None:
         return
     elif current_sink in sinks:
         ports = sinks[current_sink]['ports']
         active_port = pypulse.PULSE.get_output_active_ports_by_index(current_sink)
         if active_port is None:
             select_index = 0
         i = 0
         items = []
         for p in ports:
             self.__current_sink_ports[i] = (p[0], p[1])
             items.append((p[1], i))
             if active_port and active_port[0] == p[0]:
                 select_index = i
             i += 1
         if not items:
             items.append((" ", 0))
         self.button_widgets["speaker_combo"].add_items(items, select_index)
 def update_tray_icon(self):
     if self.tray_obj:
         current_sink = pypulse.get_fallback_sink_index()
         sinks = pypulse.PULSE.get_output_devices()
         sink_volume = pypulse.PULSE.get_output_volume()
         tip_text = "%s %d%%" % (_("Volume"), self.speaker_scale.get_value())
         if current_sink in sinks and current_sink in sink_volume:
             is_mute = sinks[current_sink]['mute']
             if is_mute:
                 tip_text = _("Mute")
             volume = max(sink_volume[current_sink]) * 100.0 / pypulse.NORMAL_VOLUME_VALUE
             if volume == 0:
                 volume_level = 0
             elif 0 < volume <= 40:
                 volume_level = 1
             elif 40 < volume <= 80:
                 volume_level = 2
             else:
                 volume_level = 3
             self.tray_obj.set_tray_icon(volume_level, is_mute)
         if self.tray_obj.tray_obj:
             self.tray_obj.tray_obj.set_tooltip_text(tip_text)
 def __set_output_status(self):
     # if sinks list is empty, then can't set output volume
     current_sink = pypulse.get_fallback_sink_index()
     sinks = pypulse.PULSE.get_output_devices()
     if current_sink is None:
         self.speaker_scale.set_sensitive(False)
         self.speaker_mute_button.set_sensitive(False)
         self.speaker_scale.set_enable(False)
         self.speaker_mute_button.set_active(False)
     # set output volume
     elif current_sink in sinks:
         self.speaker_scale.set_sensitive(True)
         self.speaker_mute_button.set_sensitive(True)
         is_mute = sinks[current_sink]['mute']
         self.speaker_mute_button.set_active(not is_mute)
         self.speaker_scale.set_enable(not is_mute)
         sink_volume = pypulse.PULSE.get_output_volume()
         if current_sink in sink_volume:
             volume = max(sink_volume[current_sink])
         else:
             volume = 0
         self.speaker_scale.set_value(volume * 100.0 / pypulse.NORMAL_VOLUME_VALUE)
     self.update_tray_icon()
 def output_treeview_clicked(self, tree_view, item, row, *args):
     if item.device_index == pypulse.get_fallback_sink_index():
         return
     pypulse.PULSE.set_fallback_sink(item.device_name)
     self.set_status_text(_("Output device %s selected") % item.content)
 def speaker_toggled(self, button):
     active = button.get_active()
     current_sink = pypulse.get_fallback_sink_index()
     self.speaker_scale.set_enable(active)
     if current_sink is not None:
         pypulse.PULSE.set_output_mute(current_sink, not active)