summaryrefslogtreecommitdiff
path: root/bin/grux.rb
blob: faf07fe4544704a8702693fef48b1d3fb79959dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!/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