#!/usr/bin/python3
import gi, os, time, sys
from shutil import which
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

# JWM Kit - A set of Graphical Apps to simplify use of JWM (Joe's Window Manager) <https://codeberg.org/JWMKit/JWM_Kit>
# Copyright © 2020-2022 Calvin Kent McNabb <apps.jwmkit@gmail.com>
#
# This file is part of JWM Kit.
#
# JWM Kit is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2,
# as published by the Free Software Foundation.
#
# JWM Kit is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with JWM Kit.  If not, see <https://www.gnu.org/licenses/>.


class Mixer(Gtk.Window):

    def __init__(self, style):
        Gtk.Window.__init__(self, title="JWM Kit Mixer")
        self.set_position(Gtk.WindowPosition.MOUSE)
        self.set_decorated(False)

        if style == 'v':
            self.set_default_size(-1, 200)
            main_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
            self.audio_widget = Gtk.Scale.new_with_range(1, 0, 100, .5)
            self.audio_widget.set_digits(0)
            self.audio_widget.set_inverted(True)
            self.audio_widget.set_sensitive(False)
        elif style == 'h':
            self.set_default_size(200, 20)
            main_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
            self.audio_widget = Gtk.Scale.new_with_range(0, 0, 100, .5)
            self.audio_widget.set_digits(0)
            self.audio_widget.set_value_pos(Gtk.PositionType.RIGHT)
            self.audio_widget.set_sensitive(False)
        else:
            self.set_default_size(32, 20)
            main_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
            self.audio_widget = Gtk.Label()

        self.add(main_box)
        main_box.pack_start(self.audio_widget, True, True, 0)

        os.nice(2)
        wait = time.time() + 1

        # use ALSA's amixer as the default
        if which('amixer') is not None:
            data_cmd = "amixer sget Master | tail -n1 | awk -F'[][]' '{ print $2 }'"
        else:
            # use sndio if amixer is not installed
            data_cmd = "sndioctl | sed -n -e 's/output.level=//p'"

        while time.time() < wait:
            data = os.popen(data_cmd).read()
            if "%" in data:
                data = data[:-2]
            else:
                data = str(round(float(data) * 100))
            if style in ['v', 'h']:
                if self.audio_widget.get_value != int(data):
                    self.audio_widget.set_value(int(data))
                    self.show_all()
            else:
                if self.audio_widget.get_text() != data:
                    self.audio_widget.set_markup('<big>' + data + '</big>')
                    self.show_all()
            while Gtk.events_pending():
                wait = time.time() + 1
                Gtk.main_iteration()
            time.sleep(.3)


if __name__ == "__main__":

    win = Mixer(sys.argv[1])

