Class: Gtk2Mp3

Inherits:
Object
  • Object
show all
Extended by:
Rafini::Empty
Defined in:
lib/gtk2mp3/trash_button.rb,
lib/gtk2mp3.rb,
lib/gtk2mp3/gui.rb,
lib/gtk2mp3/mpd.rb,
lib/gtk2mp3/config.rb,
lib/gtk2mp3/picard_button.rb

Overview

[Picard](musicbrainz.org/doc/Picard_Linux_Install) To activate this hook, edit your current configuration file:

~/.config/gtk3app/gtk2mp3/config-?.?.rbon

Set:

MonkeyPatch: "gtk2mp3/picard_button",

Constant Summary collapse

VERSION =
'3.3.230113'
HELP =
<<~HELP
  Usage:
    gtk2mp3 [:options+]
  Options:
    -h --help
    -v --version
    -k --mpd_kill  \t Kill the mpd daemon on exit
    --minime       \t Real minime
    --notoggle     \t Minime wont toggle decorated and keep above
    --notdecorated \t Dont decorate window
    --update       \t Updates and sets playlist to the entire collection
  # Note:
  # Requires MPD/MPC.
  # See https://www.musicpd.org/clients/mpc/.
HELP
CONFIG =
{
  MonkeyPatch: s0,
  BUTTONS: [:next_button!, :stop_button!],

  NEXT_BUTTON: [label: 'Next!'],
  next_button: h0,
  next_button!: [:NEXT_BUTTON, :next_button, 'clicked'],

  STOP_BUTTON: [label: 'Stop'],
  stop_button: h0,
  stop_button!: [:STOP_BUTTON, :stop_button, 'clicked'],

  ID_LABEL: a0,
  id_label: {set_selectable: true},
  id_label!: [:ID_LABEL, :id_label],

  TOOLBOX: [:horizontal],
  toolbox: h0,
  toolbox!: [:TOOLBOX,:toolbox],

  HelpFile: 'https://github.com/carlosjhr64/gtk2mp3',
  Logo: "#{UserSpace::XDG['data']}/gtk3app/gtk2mp3/logo.png",
  about_dialog: {
    set_program_name: 'Gtk2Mp3',
    set_version: VERSION.semantic(0..1),
    set_copyright: '(c) 2023 CarlosJHR64',
    set_comments: 'A MPD/MPC "Next!" Button',
    set_website: 'https://github.com/carlosjhr64/gtk2mp3',
    set_website_label: 'See it at GitHub!',
  },

  app_menu: {
    add_menu_item: [:minime!, :about!, :quit!]
  },
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stage, toolbar, options, &block) ⇒ Gtk2Mp3

gui = Gtk2Mp3.new(stage, toolbar, options)_1



7
8
9
10
11
12
13
14
# File 'lib/gtk2mp3/gui.rb', line 7

def initialize(stage, toolbar, options, &block)
  @toolbox   = Such::Box.new toolbar, :toolbox!
  CONFIG[:BUTTONS].each do |button|
    Such::Button.new(@toolbox, button){block.call button}
  end
  @label = Such::Label.new(stage, :id_label!)
  build_logo_menu(block)
end

Class Method Details

.hook(command) ⇒ Object



42
43
44
# File 'lib/gtk2mp3/mpd.rb', line 42

def Gtk2Mp3.hook(command)
  # You can monkey-patch this function
end

.initObject



19
20
21
22
23
# File 'lib/gtk2mp3.rb', line 19

def Gtk2Mp3.init
  require_relative 'gtk2mp3/mpd'
  Gtk2Mp3.system_mpd
  Gtk2Mp3.system_mpc if ARGV.include? '--update'
end

.mpc_command(command) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gtk2mp3/mpd.rb', line 26

def Gtk2Mp3.mpc_command(command)
  case command
  when :next_button!
    if system 'mpc pause-if-playing'
      sleep 0.25
      system 'mpc next'
    else
      system 'mpc play'
    end
  when :stop_button!
    system 'mpc stop'
  else
    Gtk2Mp3.hook(command)
  end
end

.mpc_idleloop(gui) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/gtk2mp3/mpd.rb', line 46

def Gtk2Mp3.mpc_idleloop(gui)
  Thread.new do
    IO.popen('mpc idleloop player', 'r') do |pipe|
      while line = pipe.gets
        gui.set_label File.basename(`mpc current`.strip, '.*')
      end
    end
  end
end

.runObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gtk2mp3.rb', line 25

def Gtk2Mp3.run
  # This is a Gtk3App.
  require 'gtk3app'
  # This Gem.
  require_relative 'gtk2mp3/config'
  require_relative 'gtk2mp3/gui'
  mpd_kill = nil
  Gtk3App.run(klass:Gtk2Mp3) do  |stage, toolbar, options|
    mpd_kill = options.mpd_kill?
    require CONFIG[:MonkeyPatch] unless CONFIG[:MonkeyPatch].empty?
    gui = Gtk2Mp3.new(stage, toolbar, options){Gtk2Mp3.mpc_command _1}
    Gtk2Mp3.mpc_idleloop(gui)
  end
  system 'mpd --kill' if mpd_kill
end

.system_mpcObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/gtk2mp3/mpd.rb', line 12

def Gtk2Mp3.system_mpc
  # initialize playlist
  unless system 'mpc --wait update' and
      system 'mpc clear'            and
      system 'mpc ls | mpc add'     and
      system 'mpc consume off'      and
      system 'mpc repeat off'       and
      system 'mpc random on'        and
      system 'mpc single off'
    $stderr.puts %q(Could not initialize mpd's playlist)
    exit 76 # EX_PROTOCOL
  end
end

.system_mpdObject



2
3
4
5
6
7
8
9
10
# File 'lib/gtk2mp3/mpd.rb', line 2

def Gtk2Mp3.system_mpd
  # check if mpd is already running
  unless system 'ps -C mpd'
    unless system 'mpd'
      $stderr.puts 'Could not start the mpd daemon.'
      exit 69 # EX_UNAVAILABLE
    end
  end
end

Instance Method Details

#build_logo_menu(block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/gtk2mp3/gui.rb', line 16

def build_logo_menu(block)
  Gtk3App.logo_press_event do |button|
    case button
    when 1
      block.call :next_button!
    when 2
      block.call :stop_button!
    when 3
      # Gtk3App's main menu
    end
  end
end

#set_label(text) ⇒ Object



2
3
4
# File 'lib/gtk2mp3/gui.rb', line 2

def set_label(text)
  @label.text = text
end