danielfm / smudge

Control the Spotify app from within Emacs.
https://asciinema.org/a/218654
GNU General Public License v3.0
317 stars 47 forks source link

Remeber selected device #87

Closed jsilve24 closed 2 years ago

jsilve24 commented 2 years ago

Is there a way to just remember the previously selected device rather thank requiring that I keep reselecting it after restarting emacs?

Thank you again for all the hard work putting this package together!

jsilve24 commented 2 years ago

So I am not sure this is the best approach or not but I sort of solved my problem with the following function.

note the smudge-api-transfer-player form was extracted from the text properties of smudge-select-device. Other users will have to replace the device Id. Also, this function autostarts and buries the spotify player so I don't need to start it myself and I don't have to look at it.

(defun jds/hydra-spotify-wrapper ()
  "Start Spotify app in background and set device if not already set."
  (interactive)
  (if (not (get-buffer "Spotify"))
      (let* ((buf (current-buffer)))
    (exwm-async-run "spotify")
    (while (not (get-buffer "Spotify"))
      (sit-for 1))
    (bury-buffer)
    (switch-to-buffer buf)
    (smudge-api-transfer-player "5f71875fb87f5bec7a8281603f00a7ca858bbff8"
                  (lambda (json)
                (setq smudge-selected-device-id "5f71875fb87f5bec7a8281603f00a7ca858bbff8")
                (message "Device '%s' selected" "lenovoGen4Sil")))))
  (hydra-spotify/body))

Finally, to help really keep the spotify player from rising to the top of my buffer list and being displayed I noticed I had to change the hydra suggested somewhat:

(defhydra hydra-spotify (:hint nil)
    "
^Search^                  ^Control^               ^Manage^
^^^^^^^^-----------------------------------------------------------------
_t_: Track               _SPC_: Play/Pause        _+_: Volume up
_m_: My Playlists        _n_  : Next Track        _-_: Volume down
_f_: Featured Playlists  _p_  : Previous Track    _x_: Mute
_u_: User Playlists      _r_  : Repeat            _d_: Device
_c_: Current Track       _s_  : Shuffle           _q_: Quit
"
    ("t" smudge-track-search :exit t)
    ("m" smudge-my-playlists :exit t)
    ("f" smudge-featured-playlists :exit t)
    ("c" jds~smudge-status :exit nil)
    ("u" smudge-user-playlists :exit t)
    ("SPC" smudge-controller-toggle-play :exit nil)
    ("n" smudge-controller-next-track :exit nil)
    ("p" smudge-controller-previous-track :exit nil)
    ("r" smudge-controller-toggle-repeat :exit nil)
    ("s" smudge-controller-toggle-shuffle :exit nil)
    ("+" smudge-controller-volume-up :exit nil)
    ("-" smudge-controller-volume-down :exit nil)
    ("x" smudge-controller-volume-mute-unmute :exit nil)
    ("d" smudge-select-device :exit nil)
    ("q" hydra-keyboard-quit "quit" :color blue))

Note I changed the q binding to hydra-keyboard-quit which I think is a better default. I have also added the c binding to show the current track (since I don't use the modeline status).

Anyways hope this helps someone else.

jsilve24 commented 2 years ago

Here is a slightly more robust version of my hydra wrapper. The problem is that it needs to setup the device each time it is called. Not sure of a better way as my other attemps seem to fail for some strange reason.

;;;###autoload
(defun jds/hydra-spotify-wrapper ()
  "Start Spotify app in background and set device if not already set."
  (interactive)
  (if (not (get-buffer "Spotify"))
      (let* ((buf (current-buffer)))
    (exwm-async-run "spotify")
    (while (not (get-buffer "Spotify"))
      (sit-for 1))
    (bury-buffer)
    (switch-to-buffer buf)))
  (smudge-api-transfer-player "5f71875fb87f5bec7a8281603f00a7ca858bbff8"
                  (lambda (json)
                    (setq smudge-selected-device-id "5f71875fb87f5bec7a8281603f00a7ca858bbff8")
                    (message "Device '%s' selected" "lenovoGen4Sil")))
  (hydra-spotify/body))