libmpv.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <mpv/client.h>
  4. #include "libmpv.h"
  5. mpv_handle *ctx;
  6. static int
  7. check_error(int status)
  8. {
  9. if (status < 0) {
  10. fprintf(stderr, "mpv API error: %s\n", mpv_error_string(status));
  11. }
  12. return status;
  13. }
  14. int
  15. libmpv_init(void)
  16. {
  17. int val;
  18. ctx = mpv_create();
  19. if (!ctx) {
  20. fprintf(stderr, "failed creating context\n");
  21. return 1;
  22. }
  23. check_error(mpv_set_option_string(ctx, "include", "~/.config/mpvd/mpv.conf"));
  24. check_error(mpv_set_option_string(ctx, "input-conf", "~/.config/mpvd/input.conf"));
  25. val = 1;
  26. check_error(mpv_set_option(ctx, "osc", MPV_FORMAT_FLAG, &val));
  27. /* Done setting up options. */
  28. check_error(mpv_initialize(ctx));
  29. return 0;
  30. }
  31. int
  32. libmpv_play(char *file)
  33. {
  34. const char *cmd[] = {"loadfile", file, NULL};
  35. return check_error(mpv_command(ctx, cmd));
  36. }
  37. int
  38. libmpv_queue(char *file)
  39. {
  40. const char *cmd[] = {"loadfile", file, "append-play", NULL};
  41. return check_error(mpv_command(ctx, cmd));
  42. }
  43. int
  44. libmpv_next(void)
  45. {
  46. const char *cmd[] = {"playlist-next", NULL};
  47. return check_error(mpv_command(ctx, cmd));
  48. }
  49. int
  50. libmpv_prev(void)
  51. {
  52. const char *cmd[] = {"playlist-prev", NULL};
  53. return check_error(mpv_command(ctx, cmd));
  54. }
  55. int
  56. libmpv_stop(void)
  57. {
  58. const char *cmd[] = {"stop", NULL};
  59. return check_error(mpv_command(ctx, cmd));
  60. }
  61. int
  62. libmpv_keypress(char *key)
  63. {
  64. const char *cmd[] = {"keypress", key, NULL};
  65. return check_error(mpv_command(ctx, cmd));
  66. }
  67. int
  68. libmpv_event_file_loaded(void)
  69. {
  70. char *prop = NULL;
  71. int ret;
  72. /* FIXME this, and check_error(), is fucking shit. */
  73. ret = mpv_get_property(ctx, "path", MPV_FORMAT_STRING, &prop);
  74. if (0 < ret) {
  75. return check_error(ret);
  76. }
  77. printf("%s\n", prop);
  78. mpv_free(prop);
  79. return ret;
  80. }