summaryrefslogtreecommitdiff
path: root/bin/grux.rb
blob: e4006908f9e7595d855bb86b9ea56676385dd7a2 (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
#!/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' )

    @cntrlBtnBox    = Gtk::HButtonBox.new
    @statusLblBox   = Gtk::VBox.new
    @playlistViewWin  = Gtk::ScrolledWindow.new

    @playlistTextRenderer   = Gtk::CellRendererText.new
    @playlistView           = Gtk::TreeView.new
    @playlistList           = Gtk::ListStore.new( String, String )
    @playlistColumns        = [ 'artist', 'title' ]
    @playlistColumns.each_with_index do |tag, index|
      @playlistView.append_column Gtk::TreeViewColumn.new( tag, @playlistTextRenderer, :text => index )
    end
    @playlistViewWin << @playlistView
    @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::Button.new('Re_peat')
    ]
    @statusLbls     = [
      @stateLbl       = Gtk::Label.new,
      @repeatLbl      = Gtk::Label.new,
      @playbackLbl    = Gtk::Label.new,
      @nextSongLbl    = Gtk::Label.new
    ]

    @cntrlbtns.each do |button|
      @cntrlBtnBox.pack_start button
    end
    @statusLbls.each do |label|
      @statusLblBox.pack_start label, false
    end

    self.pack_start @cntrlBtnBox, false
    self.pack_start @statusLblBox
    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
    @rptBtn.signal_connect :clicked do
      self.toggle_repeat
    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 )
    @server.medialib_get_info( id ).notifier do |songinfo|
      @playbackLbl.text = ""
      songinfo.each_key do |key|
        songinfo[key].each_pair do |src,value|
#        info.each_pair do |src, value|
          @playbackLbl.text += "[#{src}] #{key} = #{value}\n"
        end
      end
      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 )
    playlist.each_with_index do |id, index|
      current_row = @playlistList.append
      @server.medialib_get_info( id ).notifier do |songinfo|
        current_row[0] = songinfo[:artist] ? songinfo[:artist].first[1] : "NONE"
        current_row[1] = songinfo[:title] ? songinfo[:title].first[1] : "NONE"
        true
      end
    end
    @playlistView.model = @playlistList
    true
  end

  def toggle_repeat
    if @currentconfig[:"playlist.repeat_all"] == "1"
      if @currentconfig[:"playlist.repeat_one"] == "1"
        @repeatLbl.text = "none"
        @server.config_set_value( "playlist.repeat_all", "0" ).notifier do |res|
          @server.config_get_value( "playlist.repeat_all" ).notifier do |value|
            @currentconfig[:"playlist.repeat_all"] = value
            true
          end
          true
        end
        @server.config_set_value( "playlist.repeat_one", "0" ).notifier do |res|
          @server.config_get_value( "playlist.repeat_one" ).notifier do |value|
            @currentconfig[:"playlist.repeat_one"] = value
            true
          end
          true
        end
      elsif @currentconfig[:"playlist.repeat_one"] == "0"
        @repeatLbl.text = "one"
        @server.config_set_value( "playlist.repeat_one", "1" ).notifier do |res|
          @server.config_get_value( "playlist.repeat_one" ).notifier do |value|
            @currentconfig[:"playlist.repeat_one"] = value
            true
          end
          true
        end
      else
        warn "Unknown state"
      end
    elsif @currentconfig[:"playlist.repeat_all"] == "0"
      @repeatLbl.text = "all"
      @server.config_set_value( "playlist.repeat_one", "0" ).notifier do |res|
          @server.config_get_value( "playlist.repeat_one" ).notifier do |value|
            @currentconfig[:"playlist.repeat_one"] = value
            true
          end
        true
      end
      @server.config_set_value( "playlist.repeat_all", "1" ).notifier do |res|
          @server.config_get_value( "playlist.repeat_all" ).notifier do |value|
            @currentconfig[:"playlist.repeat_all"] = value
            true
          end
        true
      end
    else
      warn "Unknown state"
    end
    true
  end

  def resync_state
    @server.config_list_values.notifier do |res|
      @currentconfig = {}
      res.each_pair do |key, value|
        @currentconfig[key] = value
      end

      @repeatLbl.text = if( @currentconfig[:"playlist.repeat_one"] == "1" )
        "one"
      elsif @currentconfig[:"playlist.repeat_all"] == "1"
        "all"
      else
        "none"
      end
      true
    end
    true

    @server.playlist.entries.notifier do |playlist|
      self.handle_playlist playlist
      true
    end
  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