]> uap-core.de Git - uwplayer.git/commitdiff
add search for mpv/mplayer binary
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Thu, 6 Jan 2022 11:08:16 +0000 (12:08 +0100)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Thu, 6 Jan 2022 11:08:16 +0000 (12:08 +0100)
application/main.c
application/main.h
application/settings.c
application/settings.h

index 29f6ba9e6a27d81094262163db0276dc3b9d16b4..6dcca3ba8aadefc099a3d35996faed6b002ed663 100644 (file)
@@ -89,3 +89,7 @@ XtAppContext* GetAppContext(void) {
 void ApplicationExit(void) {
     XtAppSetExitFlag(app);
 }
+
+void AppAddTimeOut(unsigned long interval, XtTimerCallbackProc proc, XtPointer data) {
+    XtAppAddTimeOut(app, interval, proc, data);
+}
index b16f5ec55428ce624ce0ef275e407add4f353506..7a233c241ec1b2382bcb1d9a150dec566c083a23 100644 (file)
@@ -20,8 +20,8 @@
  * DEALINGS IN THE SOFTWARE.
  */
 
-#ifndef MAIN_H
-#define MAIN_H
+#ifndef UWP_MAIN_H
+#define UWP_MAIN_H
 
 #include <Xm/XmAll.h>
 
@@ -36,10 +36,12 @@ XtAppContext* GetAppContext(void);
 
 void ApplicationExit(void);
 
+void AppAddTimeOut(unsigned long interval, XtTimerCallbackProc proc, XtPointer data);
+
 
 #ifdef __cplusplus
 }
 #endif
 
-#endif /* MAIN_H */
+#endif /* UWPMAIN_H */
 
index d651a81ed714d288a3545e16cf86b11bb35058f4..22793b46bd9d6eac3b698f7a6cdbb1f5dadcde8f 100644 (file)
 #include <sys/stat.h>
 #include <pthread.h>
 
+#include "main.h"
 #include "utils.h"
 
 #include <ucx/map.h>
 #include <ucx/properties.h>
+#include <ucx/buffer.h>
+#include <ucx/utils.h>
 
 #define CONFIG_BASE_DIR ".config"
 #define UWP_CONFIG_DIR  "uwplayer"
@@ -84,18 +87,20 @@ int load_settings(void) {
     char *cfgfile_path = util_concat_path(uwp_config_dir, UWP_CONFIG_FILE);
     FILE *cfgfile = fopen(cfgfile_path, "r");
     free(cfgfile_path);
-    if(!cfgfile) return 0;
     
     int ret = 0;
-    if(ucx_properties_load(uwp_settings, cfgfile)) {
-        fprintf(stderr, "Error: Cannot read uwplayer settings\n");
-        ret = 1;
-    }
-    fclose(cfgfile);
-    
-    if(ret) {
-        return ret;
+    if(cfgfile) {
+        if(ucx_properties_load(uwp_settings, cfgfile)) {
+            fprintf(stderr, "Error: Cannot read uwplayer settings\n");
+            ret = 1;
+        }
+        fclose(cfgfile);
+
+        if(ret) {
+            return ret;
+        }
     }
     
     // check if mpv or mplayer binaries are configured
     char *player_bin = ucx_map_cstr_get(uwp_settings, UWP_PLAYER_BIN);
@@ -112,9 +117,59 @@ int load_settings(void) {
     return 0;
 }
 
+static char* get_which_output(FILE *f, UcxBuffer *buf) {
+    buf->pos = 0;
+    buf->size = 0;
+    ucx_stream_copy(f, buf, (read_func)fread, (write_func)ucx_buffer_write);
+    if(!pclose(f)) {
+        ucx_buffer_putc(buf, 0);
+        size_t i;
+        for(i=0;i<buf->pos;i++) {
+            if(buf->space[i] == '\n') {
+                buf->space[i] = 0;
+                break;
+            }
+        }
+        return buf->space;
+    }
+    return NULL;
+}
+
+static void finish_bin_search(XtPointer data, XtIntervalId *id) {
+    PlayerInfo *playerInfo = data;
+    ucx_map_cstr_put(uwp_settings, UWP_PLAYER_BIN, playerInfo->bin);
+    ucx_map_cstr_put(uwp_settings, UWP_PLAYER_TYPE, playerInfo->type);
+    free(playerInfo);
+}
+
 static void* player_bin_search_thread(void *data) {
-    // TODO:
-    //printf("search\n");
+    UcxBuffer *buf = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND);
+    
+    FILE *f = popen("which mpv", "r");
+    if(f) {
+        char *bin = get_which_output(f, buf);
+        if(bin) {
+            PlayerInfo *playerInfo = malloc(sizeof(PlayerInfo));
+            playerInfo->bin = strdup(bin);
+            playerInfo->type = strdup("mpv");
+            AppAddTimeOut(0, finish_bin_search, playerInfo);
+            
+            ucx_buffer_free(buf);
+            return NULL;
+        }
+    }
+    
+    f = popen("which mplayer", "r");
+    if(f) {
+        char *bin = get_which_output(f, buf);
+        if(bin) {
+            PlayerInfo *playerInfo = malloc(sizeof(PlayerInfo));
+            playerInfo->bin = strdup(bin);
+            playerInfo->type = strdup("mplayer");
+            AppAddTimeOut(0, finish_bin_search, playerInfo);
+        }
+    }
     
+    ucx_buffer_free(buf);
     return NULL;
 }
index b7ada4e4dc0027e049e8adb0bc3dca3616680708..57be624d1a24bfe98b210041e0b0ee3007f9349c 100644 (file)
@@ -29,6 +29,11 @@ extern "C" {
     
 #define UWP_PLAYER_BIN    "player"
 #define UWP_PLAYER_TYPE   "player_type"
+    
+typedef struct PlayerInfo {
+    char *bin;
+    char *type;
+} PlayerInfo;
 
 int load_settings(void);