Example #1
0
func getActiveMprisClient() *libmpris.Player {
	list, ok := getMprisClients()
	if !ok {
		return nil
	}

	for _, dest := range list {
		obj, err := libmpris.NewPlayer(dest, MPRIS_PATH)
		if err != nil {
			logger.Warningf("New mpris player failed: '%v'' for sender: '%s'", err, dest)
			continue
		}
		if len(list) == 1 {
			return obj
		} else if len(list) == 2 && strings.Contains(dest, "vlc") {
			// vlc create two dbus sender
			return obj
		}
		if obj.PlaybackStatus.GetValue().(string) == "Playing" {
			prevSender = dest
			logger.Info("Current Media: ", dest)
			return obj
		} else if dest == prevSender {
			logger.Info("Current Media: ", dest)
			return obj
		}
	}

	if len(list) > 1 {
		obj, err := libmpris.NewPlayer(list[0], MPRIS_PATH)
		if err != nil {
			logger.Warningf("New mpris player failed: '%v'' for sender: '%s'", err, list[0])
			return nil
		}
		return obj
	}

	return nil
}
Example #2
0
func listenAudioSignal() {
	mediaKeyObj.ConnectAudioPlay(func(press bool) {
		if press {
			return
		}

		logger.Info("Received Play Signal")
		obj := getActiveMprisClient()
		if obj == nil {
			logger.Error("Get Active Mpris Failed")
			return
		}
		//obj.Play()
		obj.PlayPause()
	})

	mediaKeyObj.ConnectAudioPause(func(press bool) {
		if press {
			return
		}
		logger.Info("Received Pause Signal")

		obj := getActiveMprisClient()
		if obj == nil {
			return
		}
		obj.Pause()
	})

	mediaKeyObj.ConnectAudioStop(func(press bool) {
		if press {
			return
		}
		logger.Info("Received Stop Signal")

		obj := getActiveMprisClient()
		if obj == nil {
			return
		}
		obj.Stop()
	})

	mediaKeyObj.ConnectAudioPrevious(func(press bool) {
		if press {
			return
		}
		logger.Info("Received Previous Signal")

		obj := getActiveMprisClient()
		if obj == nil {
			return
		}
		obj.Previous()
		obj.Play()
	})

	mediaKeyObj.ConnectAudioNext(func(press bool) {
		if press {
			return
		}
		logger.Info("Received Next Signal")

		obj := getActiveMprisClient()
		if obj == nil {
			return
		}
		obj.Next()
		obj.Play()
	})

	mediaKeyObj.ConnectAudioRewind(func(press bool) {
		if press {
			return
		}
		logger.Info("Received Rewind Signal")

		obj := getActiveMprisClient()
		if obj == nil {
			return
		}
		pos := obj.Position.GetValue().(int64)
		//println("Current Position: ", pos)
		nextPos := pos - SEEK_DISTANCE
		if nextPos < 0 {
			nextPos = 0
		} else {
			nextPos = 0 - SEEK_DISTANCE
		}
		//println("Rewind Position: ", nextPos)
		obj.Seek(nextPos)
		if obj.PlaybackStatus.GetValue().(string) != "Playing" {
			obj.PlayPause()
		}
	})

	mediaKeyObj.ConnectAudioForward(func(press bool) {
		if press {
			return
		}
		logger.Info("Received Forward Signal")

		obj := getActiveMprisClient()
		if obj == nil {
			return
		}
		//pos := obj.Position.GetValue().(int64)
		//println("Current Position: ", pos)
		//nextPos := pos + SEEK_DISTANCE
		//println("Forward Position: ", nextPos)
		obj.Seek(SEEK_DISTANCE)
		if obj.PlaybackStatus.GetValue().(string) != "Playing" {
			obj.PlayPause()
		}
	})

	mediaKeyObj.ConnectAudioRepeat(func(press bool) {
		if press {
			return
		}
		logger.Info("Received Repeat Signal")

		obj := getActiveMprisClient()
		if obj == nil {
			return
		}
		obj.Play()
	})

	mediaKeyObj.ConnectLaunchEmail(func(press bool) {
		if press {
			return
		}

		if cmd, ok := getCommandByMimeType(MIME_TYPE_EMAIL); ok {
			go exec.Command(cmd).Run()
		}
	})

	mediaKeyObj.ConnectLaunchBrowser(func(press bool) {
		if press {
			return
		}

		if cmd, ok := getCommandByMimeType(MIME_TYPE_BROWSER); ok {
			go exec.Command(cmd).Run()
		}
	})

	mediaKeyObj.ConnectLaunchCalculator(func(press bool) {
		if press {
			return
		}

		go exec.Command(CALCULATOR_CMD).Run()
	})

	// Pause all media player
	loginObj.ConnectPrepareForSleep(func(active bool) {
		// Computer Sleep
		if active {
			list, ok := getMprisClients()
			if !ok {
				logger.Error("Get Mpris Clients Failed")
				return
			}

			for _, l := range list {
				obj, err := libmpris.NewPlayer(l, MPRIS_PATH)
				if err != nil {
					logger.Warningf("New Mpris Player For '%s' Failed: %v",
						l, err)
					continue
				}
				obj.Pause()
				//libmpris.DestroyPlayer(obj)
			}
		}
	})
}