Skip to content

Commit

Permalink
Add volume toggle and refactor slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
alyosha committed Sep 7, 2020
1 parent 8f39d10 commit e24aecb
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 54 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ that lazy 😏
- `:Restart` => Replay current song from the beginning
- `:Mute` => Mute audio for all media players
- `:Unmute` => Unmute audio for all media players
- `:Vol` => Toggle volume louder/quieter
- `:Quit` => Send quit signal to active media player
- `:ActivePlayer` => Confirm active media player
- `:SelectPlayer` => Select/change active media player from list of all running options

Please keep in mind that some media players may not have implemented MPRIS
fully/at all and available functionality can vary with each player.
fully/at all and available functionality can vary with each player. At some point
I will go through and properly add error messages when certain functionality is
not supported.

## coming soon
- Seek forward/backwards
- Volume toggle
- Shuffle
- Optional status bar

Expand Down
115 changes: 68 additions & 47 deletions plugin/vimedia.vim
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,7 @@ import util
vmd = vimedia.Vimedia()
EOF

" *************************************************************************** "
" *************************** Command Bindngs *************************** "
" *************************************************************************** "

command! -nargs=0 Play call s:play()
command! -nargs=0 Pause call s:pause(0)
command! -nargs=0 PauseAll call s:pause(1)
command! -nargs=0 Skip call s:skip()
command! -nargs=0 Prev call s:previous()
command! -nargs=0 Restart call s:restart()
command! -nargs=0 Mute call s:mute()
command! -nargs=0 Unmute call s:unmute()
command! -nargs=0 Quit call s:quit()
command! -nargs=0 ActivePlayer call s:active_player()
command! -nargs=0 SelectPlayer call s:present_player_options()

" *************************************************************************** "
" **************************** Functionality **************************** "
" *************************************************************************** "

fu! s:play()
fu! s:Play()
if s:selected_player_suffix == ""
echom "Please select a media player"
else
Expand All @@ -56,76 +36,117 @@ fu! s:play()
endif
endfu

fu! s:pause(all_players)
if s:selected_player_suffix == ""
echom "Please select a media player"
elseif a:all_players
fu! s:Pause(all_players)
if a:all_players
python3 vmd.pause_all(False)
else
elseif s:selected_player_suffix == ""
echom "Please select a media player"
else
python3 vmd.selected_player.pause()
endif
endfu

fu! s:skip()
fu! s:Skip()
if s:selected_player_suffix == ""
return
endif
python3 vmd.selected_player.next()
endfu

fu! s:previous()
fu! s:Previous()
if s:selected_player_suffix == ""
return
endif
python3 vmd.selected_player.previous()
endfu

fu! s:restart()
fu! s:Restart()
if s:selected_player_suffix == ""
return
endif
python3 vmd.selected_player.restart()
endfu

fu! s:active_player()
fu! s:ActivePlayer()
if s:selected_player_suffix != ""
echom s:selected_player_suffix
else
echom "No media player configured"
endif
endfu

fu! s:present_player_options() abort
fu! s:Mute()
python3 vmd.set_volume_global(0.0)
endfu

fu! s:Unmute()
python3 vmd.set_volume_global(1.0)
endfu

fu! s:Quit()
python3 vmd.base.quit()
endfu

let s:interaction_type_select_player = "select_player_interaction"
let s:interaction_type_toggle_volume = "toggle_volume_interaction"

let s:toggle_volume_opt_up = "Louder"
let s:toggle_volume_opt_down = "Quieter"
let s:toggle_volume_opt_done = "Done"

let s:toggle_volume_options = [s:toggle_volume_opt_up, s:toggle_volume_opt_down, s:toggle_volume_opt_done]

fu! s:PresentOptions(interaction_type) abort
vnew | exe 'vert resize '.(&columns/4)
setl bh=wipe bt=nofile nobl noswf nowrap
if a:interaction_type == s:interaction_type_select_player
python3 util.update_player_options()
sil! 0put = s:active_player_names
nno <silent> <buffer> <nowait> <cr> :<c-u>call<sid>SetSelectedPlayer()<cr>
elseif a:interaction_type == s:interaction_type_toggle_volume
sil! 0put = s:toggle_volume_options
nno <silent> <buffer> <nowait> <cr> :<c-u>call<sid>ToggleVolume()<cr>
endif

python3 util.update_player_options()

sil! 0put = s:active_player_names
sil! $d_
setl noma ro

nno <silent> <buffer> <nowait> q :<c-u>close<cr>
nno <silent> <buffer> <nowait> <cr> :<c-u>call <sid>set_selected_player()<cr>

endfu

fu! s:set_selected_player() abort
fu! s:SetSelectedPlayer() abort
let s:selected_player_suffix = expand("<cword>")
python3 vmd = vimedia.Vimedia()
echom "Updated active player"
close
endfu

fu! s:mute()
python3 vmd.adjust_volume_all(0.0)
fu! s:ToggleVolume() abort
let l:selected_opt = expand("<cword>")
if l:selected_opt == s:toggle_volume_opt_up
python3 vmd.adjust_volume_global(0.1)
elseif l:selected_opt == s:toggle_volume_opt_down
python3 vmd.adjust_volume_global(-0.1)
elseif l:selected_opt == s:toggle_volume_opt_done
close
endif
endfu

fu! s:unmute()
python3 vmd.adjust_volume_all(1.0)
endfu

fu! s:quit()
python3 vmd.base.quit()
endfu
" *************************************************************************** "
" *************************** Command Bindngs *************************** "
" *************************************************************************** "

let g:vimedia_plugin_loaded = 1
command! -nargs=0 Play call s:Play()
command! -nargs=0 Pause call s:Pause(0)
command! -nargs=0 PauseAll call s:Pause(1)
command! -nargs=0 Skip call s:Skip()
command! -nargs=0 Prev call s:Previous()
command! -nargs=0 Restart call s:Restart()
command! -nargs=0 Mute call s:Mute()
command! -nargs=0 Unmute call s:Unmute()
command! -nargs=0 Vol call s:PresentOptions(s:interaction_type_toggle_volume)
command! -nargs=0 Quit call s:Quit()
command! -nargs=0 ActivePlayer call s:ActivePlayer()
command! -nargs=0 SelectPlayer call s:PresentOptions(s:interaction_type_select_player)

let g:vimedia_plugin_loaded = 1
6 changes: 5 additions & 1 deletion python/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,9 @@ def get_title(self):
def get_metadata(self):
return self.get_property('Metadata')

def adjust_volume(self, value):
def set_volume(self, value):
self.set_property('Volume', value)

def adjust_volume(self, value):
current_volume = self.get_property('Volume')
self.set_property('Volume', dbus.Double(current_volume + value))
3 changes: 0 additions & 3 deletions python/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ def get_active_player_names():
names = dbus.SessionBus(private=True).list_names()
return list(filter(lambda name: name.startswith(MPRIS_PREFIX), names))


def get_selected_player_name():
selected_service_suffix = vim.eval('s:selected_player_suffix')

Expand All @@ -23,12 +22,10 @@ def get_selected_player_name():

return ""


def update_player_options():
options = list(map(normalize_option, get_active_player_names()))
vim.command('let s:active_player_names = ' + str(options))


def normalize_option(dbus_name):
option = str(dbus_name).replace(MPRIS_PREFIX, "", 1)

Expand Down
6 changes: 5 additions & 1 deletion python/vimedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def pause_all(self, exclude_selected):
continue
player.pause()

def adjust_volume_all(self, value):
def set_volume_global(self, value):
for player in list(map(Player, get_active_player_names())):
player.set_volume(dbus.Double(value))

def adjust_volume_global(self, value):
for player in list(map(Player, get_active_player_names())):
player.adjust_volume(dbus.Double(value))

0 comments on commit e24aecb

Please sign in to comment.