From 7bb3671a09043178ae56d410a626e63b9f3914f5 Mon Sep 17 00:00:00 2001
From: Aron Heinecke <aron.heinecke@t-online.de>
Date: Fri, 26 Oct 2018 18:01:00 +0200
Subject: [PATCH] Add args functionality to Instance

Signed-off-by: Aron Heinecke <aron.heinecke@t-online.de>
---
 src/core.rs | 35 ++++++++++++++++++++++++++++++-----
 1 file changed, 30 insertions(+), 5 deletions(-)

diff --git a/src/core.rs b/src/core.rs
index eb9f924..955dc3d 100644
--- a/src/core.rs
+++ b/src/core.rs
@@ -2,9 +2,11 @@
 // This file is part of vlc-rs.
 // Licensed under the MIT license, see the LICENSE file.
 
-use std::ptr::null;
+use std::ptr;
 use std::borrow::Cow;
 use std::marker::PhantomData;
+use std::ffi::CString;
+use std::i32;
 use sys;
 use ::tools::{to_cstr, from_cstr, from_cstr_ref};
 use ::libc::{c_void, c_char, c_int};
@@ -26,14 +28,32 @@ pub fn compiler() -> String {
 
 pub struct Instance {
     pub(crate) ptr: *mut sys::libvlc_instance_t,
+
 }
 
 impl Instance {
-    /// Create and initialize a libvlc instance. 
-    pub fn new() -> Option<Instance> {
+    /// Create and initialize a libvlc instance with specified args.
+    /// Note: args.len() has to be less or equal to i32::MAX
+    /// Note: libvlc discourages using arguments as these are not guaranteed to be stable between different versions of libvlc
+    pub fn with_args(args: Option<Vec<String>>) -> Option<Instance> {
+        let args_c_ptr: Vec<*const c_char> ;
+        let args_c: Vec<CString>;
+        if let Some(argv) = args {
+            args_c = argv.into_iter()
+                .map(|x| CString::new(x).expect("Error: Unexpected null byte")).collect();
+            args_c_ptr = args_c.iter().map(|x| x.as_ptr()).collect();
+        } else {
+            args_c_ptr = Vec::new();
+        }
+        
+
         unsafe{
-            let p = sys::libvlc_new(0, null());
-            
+            let p = if args_c_ptr.is_empty() {
+                sys::libvlc_new(0, ptr::null())
+            } else {
+                sys::libvlc_new(args_c_ptr.len() as i32, args_c_ptr.as_ptr())
+            };
+
             if p.is_null() {
                 return None;
             }
@@ -42,6 +62,11 @@ impl Instance {
         }
     }
 
+    /// Create and initialize a libvlc instance. 
+    pub fn new() -> Option<Instance> {
+        Instance::with_args(None)
+    }
+
     /// Try to start a user interface for the libvlc instance.
     pub fn add_intf(&self, name: &str) -> Result<(), ()> {
         let cstr = to_cstr(name);