#!/usr/bin/env ruby #$: << File.expand_path(File.dirname(FILE) + “/../lib”)) #require 'libgruxclient' require 'gtk2' require 'xmmsclient' require 'xmmsclient_glib' class Grux end class Grux::ClientWin < Gtk::VBox def initialize super @server = Xmms::Client.new( 'Grux' ) @infoBoxWin = Gtk::ScrolledWindow.new @upperBox = Gtk::HBox.new @leftUpperBox = Gtk::VBox.new @infoBox = Gtk::VBox.new @cntrlBtnBox = Gtk::VButtonBox.new @statusLblBoxWin = Gtk::ScrolledWindow.new @statusLblBox = Gtk::VBox.new @playlistViewWin = Gtk::ScrolledWindow.new @statusBar = Gtk::Statusbar.new @cntrlbtns = [ @playBtn = Gtk::Button.new('_Play/Pause'), @stopBtn = Gtk::Button.new('_Stop'), @nextBtn = Gtk::Button.new('_Next'), @prevBtn = Gtk::Button.new('Pre_vious'), @rndmBtn = Gtk::Button.new('_Random'), @rptBtn = Gtk::ToggleButton.new('Repeat _All', true), @rpt1Btn = Gtk::ToggleButton.new('Repeat _One', true) ] @statusLbls = [ @stateLbl = Gtk::Label.new, @playbackLbl = Gtk::Label.new, @nextSongLbl = Gtk::Label.new ] @cntrlbtns.each do |button| @cntrlBtnBox.pack_start button end @cntrlBtnBox.layout_style = Gtk::ButtonBox::START @statusLbls.each do |label| @statusLblBox.pack_start label, false end @infoBoxWin.add_with_viewport @infoBox @statusLblBoxWin.add_with_viewport @statusLblBox @upperBox.pack_start @cntrlBtnBox, false @upperBox.pack_start @infoBoxWin @upperBox.pack_start @statusLblBoxWin @playlistTextRenderer = Gtk::CellRendererText.new @playlistView = Gtk::TreeView.new @playlistList = Gtk::ListStore.new( String, String, String ) @playlistColumns = [ 'id', 'artist', 'title' ] @playlistColumns.each_with_index do |tag, index| @playlistView.append_column Gtk::TreeViewColumn.new( tag, @playlistTextRenderer, :text => index ) end @playlistViewWin.add @playlistView self.pack_start @upperBox self.pack_start @playlistViewWin self.pack_end @statusBar, false self.show_all begin @server.connect( ENV['XMMS_PATH'] ) @server.add_to_glib_mainloop rescue @statusBar.push( @statusBar.get_context_id( "connection" ), "Failed to connect to #{ENV['XMMS_PATH']}" ) end @playBtn.signal_connect :clicked do self.toggle_playback end @stopBtn.signal_connect :clicked do self.stop_playback end @nextBtn.signal_connect :clicked do self.step_in_playlist 1 end @prevBtn.signal_connect :clicked do self.step_in_playlist -1 end @rptBtnClkdHndlr = @rptBtn.signal_connect :clicked do self.toggle_repeat_all end @rpt1BtnClkdHndlr = @rpt1Btn.signal_connect :clicked do self.toggle_repeat_one end @playlistView.signal_connect :row_activated do |view, path, column| @server.playlist_set_next( path.to_str.to_i ).notifier do @server.playback_tickle.notifier do true end true end end @server.broadcast_coll_changed do end @server.broadcast_config_value_changed do |*foo| p foo end @server.broadcast_mediainfo_reader_status do end @server.broadcast_medialib_entry_added do end @server.broadcast_medialib_entry_changed do end @server.broadcast_playback_current_id.notifier do |id| self.handle_current_song id end @server.broadcast_playback_status.notifier do |playing| self.handle_playback_status playing end @server.broadcast_playback_volume_changed do end @server.broadcast_playlist_changed do end @server.broadcast_playlist_current_pos.notifier do |pos| self.handle_current_pos pos end @server.broadcast_playlist_loaded do end @server.broadcast_quit do end self.resync_state end def toggle_playback if @playbackState == 1 @server.playback_pause.notifier do |res| @statusBar.push( @statusBar.get_context_id( "playback" ), "Paused playback" ) true end elsif @playbackState == 0 || 2 @server.playback_start.notifier do |res| @statusBar.push( @statusBar.get_context_id( "playback" ), "Started playback" ) true end else warn "Playbackstate unknown" @server.playback_start.notifier do |res| @statusBar.push( @statusBar.get_context_id( "playback" ), "Started playback" ) true end end end def stop_playback @server.playback_stop.notifier do |res| @statusBar.push( @statusBar.get_context_id( "playback" ), "Stopped playback" ) true end end def step_in_playlist( offset = 1 ) @server.playlist_set_next_rel( offset ).notifier do @server.playback_tickle.notifier do |res| @statusBar.push( @statusBar.get_context_id( "playback" ), "Skipped playback by #{offset} song#{offset.abs >= 1 ? "s" : ''}" ) true end true end end def handle_playback_status( status ) @playbackState = status @stateLbl.text = case status when 0 then "stopped" when 1 then "playing" when 2 then "paused" else "unknown" end true end def handle_current_song( id ) songpath = Gtk::TreePath.new( @currentPlaylist.index id ) @playlistView.scroll_to_cell( songpath, nil, true, 0.5, 0.5 ) @playlistView.set_cursor( songpath, nil, false ) @server.medialib_get_info( id ).notifier do |songinfo| infos = {} @playbackLbl.text = "" @infoBox.each { |child| @infoBox.remove child } self.show_all songinfo.each_key do |key| songinfo[key].each_pair do |src,value| @playbackLbl.text += "[#{src}] #{key} = #{value}\n" infos[key] = ( ( infos[key] ? infos[key] + "\n" : "" ) + "#{value} (#{src})" ) end end [ :artist, :title, :album ].each do |key| @infoBox.pack_start( Gtk::Frame.new( key.to_s ).add( Gtk::Label.new( infos.delete( key ) ) ) ) end infos.each_pair do |key, value| if key == :artist @infoBox.pack_start( Gtk::Frame.new( key.to_s ).add( Gtk::Label.new( value ) ) ) next end @infoBox.pack_end( Gtk::Frame.new( key.to_s ).add( Gtk::Label.new( value ) ) ) end @infoBox.show_all true end true end def handle_current_pos( pos ) # @server.playlist.entries.notifier do |playlist| # @server.medialib_get_info( playlist[pos[:position]] ).notifier do |songinfo| # @nextSongLbl.text = "" # songinfo.each_pair do |key, info| # info.each_pair do |src, value| # @nextSongLbl.text += "[#{src}] #{key} = #{value}\n" # end # end # true # end # true # end true end def handle_playlist( playlist ) @currentPlaylist = playlist playlist.each_with_index do |id, index| current_row = @playlistList.append current_row[0] = index.to_s @server.medialib_get_info( id ).notifier do |songinfo| current_row[1] = songinfo[:artist] ? songinfo[:artist].first[1] : "NONE" current_row[2] = songinfo[:title] ? songinfo[:title].first[1] : "NONE" true end end @playlistView.model = @playlistList true end def handle_repeat_all_state( state ) @currentconfig[:"playlist.repeat_all"] = ( state ? "1" : "0" ) @rptBtn.signal_handler_block( @rptBtnClkdHndlr ) do @rptBtn.active = state end end def handle_repeat_one_state( state ) @currentconfig[:"playlist.repeat_one"] = ( state ? "1" : "0" ) @rpt1Btn.signal_handler_block( @rpt1BtnClkdHndlr ) do @rpt1Btn.active = state end end def toggle_repeat_all if @currentconfig[:"playlist.repeat_all"] == "1" @server.config_set_value( "playlist.repeat_all", "0" ).notifier do |res| @server.config_get_value( "playlist.repeat_all" ).notifier do |value| self.handle_repeat_all_state( value == "1" ? true : false ) true end true end elsif @currentconfig[:"playlist.repeat_all"] == "0" @server.config_set_value( "playlist.repeat_all", "1" ).notifier do |res| @server.config_get_value( "playlist.repeat_all" ).notifier do |value| self.handle_repeat_all_state( value == "1" ? true : false ) true end true end else warn "Unknown state: #{@currentconfig[:"playlist.repeat_all"]}" end true end def toggle_repeat_one if @currentconfig[:"playlist.repeat_one"] == "1" @server.config_set_value( "playlist.repeat_one", "0" ).notifier do |res| @server.config_get_value( "playlist.repeat_one" ).notifier do |value| self.handle_repeat_one_state( value == "1" ? true : false ) true end true end elsif @currentconfig[:"playlist.repeat_one"] == "0" @server.config_set_value( "playlist.repeat_one", "1" ).notifier do |res| @server.config_get_value( "playlist.repeat_one" ).notifier do |value| self.handle_repeat_one_state( value == "1" ? true : false ) true end true end else warn "Unknown state: #{@currentconfig[:"playlist.repeat_one"]}" end true end def resync_state @server.playlist.entries.notifier do |playlist| self.handle_playlist playlist true end @server.playback_current_id.notifier do |id| self.handle_current_song id end @server.config_list_values.notifier do |res| @currentconfig = {} res.each_pair do |key, value| @currentconfig[key] = value end @rpt1Btn.signal_handler_block( @rpt1BtnClkdHndlr ) do @rpt1Btn.active = if( @currentconfig[:"playlist.repeat_one"] == "1" ) true else false end end @rptBtn.signal_handler_block( @rptBtnClkdHndlr ) do @rptBtn.active = if @currentconfig[:"playlist.repeat_all"] == "1" true else false end end true end true end end class Grux::MainWin < Gtk::Window def initialize super @mainCont = Gtk::VBox.new @btnCont = Gtk::HBox.new @clntCont = Gtk::Notebook.new @cnnctBtn = Gtk::Button.new "_Connect" @quitBtn = Gtk::Button.new "_Quit" self.title = "Grux" @winDstryHndlr = self.signal_connect :destroy do Gtk.main_quit end @cnnctBtnHndlr = @cnnctBtn.signal_connect :clicked do @clntCont.append_page( Grux::ClientWin.new, Gtk::Label.new( "Client" ) ) end @quitBtnHndlr = @quitBtn.signal_connect :clicked do Gtk.main_quit end @btnCont.pack_start @cnnctBtn @btnCont.pack_end @quitBtn @mainCont.pack_start @btnCont, false @mainCont.pack_end @clntCont self.add @mainCont self.show_all end end gruxWin = Grux::MainWin.new Gtk.main