浏览代码

Merge pull request #1346 from dtcooper/master

libobs/UI: Allow Access To argc/argv
Jim 7 年之前
父节点
当前提交
78d940e0f4
共有 3 个文件被更改,包括 63 次插入0 次删除
  1. 2 0
      UI/obs-app.cpp
  2. 37 0
      libobs/obs.c
  3. 24 0
      libobs/obs.h

+ 2 - 0
UI/obs-app.cpp

@@ -2183,6 +2183,8 @@ int main(int argc, char *argv[])
 	move_to_xdg();
 #endif
 
+	obs_set_cmdline_args(argc, argv);
+
 	for (int i = 1; i < argc; i++) {
 		if (arg_is(argv[i], "--portable", "-p")) {
 			portable_mode = true;

+ 37 - 0
libobs/obs.c

@@ -853,6 +853,42 @@ bool obs_startup(const char *locale, const char *module_config_path,
 	return success;
 }
 
+static struct obs_cmdline_args cmdline_args = {0, NULL};
+void obs_set_cmdline_args(int argc, char **argv)
+{
+	char *data;
+	size_t len;
+	int i;
+
+	/* Once argc is set (non-zero) we shouldn't call again */
+	if (cmdline_args.argc)
+		return;
+
+	cmdline_args.argc = argc;
+
+	/* Safely copy over argv */
+	len = 0;
+	for (i = 0; i < argc; i++)
+		len += strlen(argv[i]) + 1;
+
+	cmdline_args.argv = bmalloc(sizeof(char *) * (argc + 1) + len);
+	data = (char *) cmdline_args.argv + sizeof(char *) * (argc + 1);
+
+	for (i = 0; i < argc; i++) {
+		cmdline_args.argv[i] = data;
+		len = strlen(argv[i]) + 1;
+		memcpy(data, argv[i], len);
+		data += len;
+	}
+
+	cmdline_args.argv[argc] = NULL;
+}
+
+struct obs_cmdline_args obs_get_cmdline_args(void)
+{
+	return cmdline_args;
+}
+
 void obs_shutdown(void)
 {
 	struct obs_module *module;
@@ -918,6 +954,7 @@ void obs_shutdown(void)
 	bfree(core->module_config_path);
 	bfree(core->locale);
 	bfree(core);
+	bfree(cmdline_args.argv);
 
 #ifdef _WIN32
 	uninitialize_com();

+ 24 - 0
libobs/obs.h

@@ -239,6 +239,12 @@ struct obs_source_frame {
 	bool                prev_frame;
 };
 
+/** Access to the argc/argv used to start OBS. What you see is what you get. */
+struct obs_cmdline_args {
+	int argc;
+	char **argv;
+};
+
 /* ------------------------------------------------------------------------- */
 /* OBS context */
 
@@ -291,6 +297,24 @@ EXPORT uint32_t obs_get_version(void);
 /** @return The current core version string */
 EXPORT const char *obs_get_version_string(void);
 
+/**
+ * Sets things up for calls to obs_get_cmdline_args. Called onl yonce at startup
+ * and safely copies argv/argc from main(). Subsequent calls do nothing.
+ *
+ * @param  argc  The count of command line arguments, from main()
+ * @param  argv  An array of command line arguments, copied from main() and ends
+ *               with NULL.
+ */
+EXPORT void obs_set_cmdline_args(int argc, char **argv);
+
+/**
+ * Get the argc/argv used to start OBS
+ *
+ * @return  The command line arguments used for main(). Don't modify this or
+ *          you'll mess things up for other callers.
+ */
+EXPORT struct obs_cmdline_args obs_get_cmdline_args(void);
+
 /**
  * Sets a new locale to use for modules.  This will call obs_module_set_locale
  * for each module with the new locale.