コード例 #1
0
ファイル: unmark.c プロジェクト: thejan2009/sway
struct cmd_results *cmd_unmark(int argc, char **argv) {
	// Determine the container
	struct sway_container *con = NULL;
	if (config->handler_context.using_criteria) {
		con = config->handler_context.container;
	}

	// Determine the mark
	char *mark = NULL;
	if (argc > 0) {
		mark = join_args(argv, argc);
	}

	if (con && mark) {
		// Remove the mark from the given container
		if (container_has_mark(con, mark)) {
			container_find_and_unmark(mark);
		}
	} else if (con && !mark) {
		// Clear all marks from the given container
		container_clear_marks(con);
		container_update_marks_textures(con);
	} else if (!con && mark) {
		// Remove mark from whichever container has it
		container_find_and_unmark(mark);
	} else {
		// Remove all marks from all containers
		root_for_each_container(remove_all_marks_iterator, NULL);
	}
	free(mark);

	return cmd_results_new(CMD_SUCCESS, NULL);
}
コード例 #2
0
bool
Env::getDelimitedStringV2Raw(MyString *result,MyString * /*error_msg*/,bool mark_v2) const
{
	MyString var, val;
	SimpleList<MyString> env_list;

	ASSERT(result);

	_envTable->startIterations();
	while( _envTable->iterate( var, val ) ) {
		if(val == NO_ENVIRONMENT_VALUE) {
			env_list.Append(var);
		}
		else {
			MyString var_val;
			var_val.sprintf("%s=%s",var.Value(),val.Value());
			env_list.Append(var_val);
		}
	}

	if(mark_v2) {
		(*result) += RAW_V2_ENV_MARKER;
	}
	join_args(env_list,result);
	return true;
}
コード例 #3
0
ファイル: assign.c プロジェクト: thejan2009/sway
struct cmd_results *cmd_assign(int argc, char **argv) {
	struct cmd_results *error = NULL;
	if ((error = checkarg(argc, "assign", EXPECTED_AT_LEAST, 2))) {
		return error;
	}

	// Create criteria
	char *err_str = NULL;
	struct criteria *criteria = criteria_parse(argv[0], &err_str);
	if (!criteria) {
		error = cmd_results_new(CMD_INVALID, err_str);
		free(err_str);
		return error;
	}

	--argc; ++argv;

	if (strncmp(*argv, "→", strlen("→")) == 0) {
		if (argc < 2) {
			free(criteria);
			return cmd_results_new(CMD_INVALID, "Missing workspace");
		}
		--argc;
		++argv;
	}

	if (strcmp(*argv, "output") == 0) {
		criteria->type = CT_ASSIGN_OUTPUT;
		--argc; ++argv;
	} else {
		if (strcmp(*argv, "workspace") == 0) {
			--argc; ++argv;
		}
		if (strcmp(*argv, "number") == 0) {
			--argc; ++argv;
			if (argv[0][0] < '0' || argv[0][0] > '9') {
				free(criteria);
				return cmd_results_new(CMD_INVALID,
						"Invalid workspace number '%s'", argv[0]);
			}
			criteria->type = CT_ASSIGN_WORKSPACE_NUMBER;
		} else {
			criteria->type = CT_ASSIGN_WORKSPACE;
		}
	}

	criteria->target = join_args(argv, argc);

	list_add(config->criteria, criteria);
	sway_log(SWAY_DEBUG, "assign: '%s' -> '%s' added", criteria->raw,
			criteria->target);

	return cmd_results_new(CMD_SUCCESS, NULL);
}
コード例 #4
0
ファイル: title_format.c プロジェクト: thejan2009/sway
struct cmd_results *cmd_title_format(int argc, char **argv) {
	struct cmd_results *error = NULL;
	if ((error = checkarg(argc, "title_format", EXPECTED_AT_LEAST, 1))) {
		return error;
	}
	struct sway_container *container = config->handler_context.container;
	if (!container || !container->view) {
		return cmd_results_new(CMD_INVALID,
				"Only views can have a title_format");
	}
	struct sway_view *view = container->view;
	char *format = join_args(argv, argc);
	if (view->title_format) {
		free(view->title_format);
	}
	view->title_format = format;
	view_update_title(view, true);
	config_update_font_height(true);
	return cmd_results_new(CMD_SUCCESS, NULL);
}
コード例 #5
0
bool
ArgList::GetArgsStringV2Raw(MyString *result,MyString * /*error_msg*/,int start_arg) const
{
	join_args(args_list,result,start_arg);
	return true;
}
コード例 #6
0
ファイル: main.c プロジェクト: thejan2009/sway
int main(int argc, char **argv) {
	static int verbose = 0, debug = 0, validate = 0, allow_unsupported_gpu = 0;

	static struct option long_options[] = {
		{"help", no_argument, NULL, 'h'},
		{"config", required_argument, NULL, 'c'},
		{"validate", no_argument, NULL, 'C'},
		{"debug", no_argument, NULL, 'd'},
		{"version", no_argument, NULL, 'v'},
		{"verbose", no_argument, NULL, 'V'},
		{"get-socketpath", no_argument, NULL, 'p'},
		{"unsupported-gpu", no_argument, NULL, 'u'},
		{"my-next-gpu-wont-be-nvidia", no_argument, NULL, 'u'},
		{0, 0, 0, 0}
	};

	char *config_path = NULL;

	const char* usage =
		"Usage: sway [options] [command]\n"
		"\n"
		"  -h, --help             Show help message and quit.\n"
		"  -c, --config <config>  Specify a config file.\n"
		"  -C, --validate         Check the validity of the config file, then exit.\n"
		"  -d, --debug            Enables full logging, including debug information.\n"
		"  -v, --version          Show the version number and quit.\n"
		"  -V, --verbose          Enables more verbose logging.\n"
		"      --get-socketpath   Gets the IPC socket path and prints it, then exits.\n"
		"\n";

	int c;
	while (1) {
		int option_index = 0;
		c = getopt_long(argc, argv, "hCdD:vVc:", long_options, &option_index);
		if (c == -1) {
			break;
		}
		switch (c) {
		case 'h': // help
			fprintf(stdout, "%s", usage);
			exit(EXIT_SUCCESS);
			break;
		case 'c': // config
			config_path = strdup(optarg);
			break;
		case 'C': // validate
			validate = 1;
			break;
		case 'd': // debug
			debug = 1;
			break;
		case 'D': // extended debug options
			enable_debug_flag(optarg);
			break;
		case 'u':
			allow_unsupported_gpu = 1;
			break;
		case 'v': // version
			fprintf(stdout, "sway version " SWAY_VERSION "\n");
			exit(EXIT_SUCCESS);
			break;
		case 'V': // verbose
			verbose = 1;
			break;
		case 'p': ; // --get-socketpath
			if (getenv("SWAYSOCK")) {
				fprintf(stdout, "%s\n", getenv("SWAYSOCK"));
				exit(EXIT_SUCCESS);
			} else {
				fprintf(stderr, "sway socket not detected.\n");
				exit(EXIT_FAILURE);
			}
			break;
		default:
			fprintf(stderr, "%s", usage);
			exit(EXIT_FAILURE);
		}
	}

	// Since wayland requires XDG_RUNTIME_DIR to be set, abort with just the
	// clear error message (when not running as an IPC client).
	if (!getenv("XDG_RUNTIME_DIR") && optind == argc) {
		fprintf(stderr,
				"XDG_RUNTIME_DIR is not set in the environment. Aborting.\n");
		exit(EXIT_FAILURE);
	}

	// As the 'callback' function for wlr_log is equivalent to that for
	// sway, we do not need to override it.
	if (debug) {
		sway_log_init(SWAY_DEBUG, sway_terminate);
		wlr_log_init(WLR_DEBUG, NULL);
	} else if (verbose || validate) {
		sway_log_init(SWAY_INFO, sway_terminate);
		wlr_log_init(WLR_INFO, NULL);
	} else {
		sway_log_init(SWAY_ERROR, sway_terminate);
		wlr_log_init(WLR_ERROR, NULL);
	}

	log_kernel();
	log_distro();
	log_env();
	detect_proprietary(allow_unsupported_gpu);
	detect_raspi();

	if (optind < argc) { // Behave as IPC client
		if (optind != 1) {
			sway_log(SWAY_ERROR,
					"Detected both options and positional arguments. If you "
					"are trying to use the IPC client, options are not "
					"supported. Otherwise, check the provided arguments for "
					"issues. See `man 1 sway` or `sway -h` for usage. If you "
					"are trying to generate a debug log, use "
					"`sway -d 2>sway.log`.");
			exit(EXIT_FAILURE);
		}
		if (!drop_permissions()) {
			exit(EXIT_FAILURE);
		}
		char *socket_path = getenv("SWAYSOCK");
		if (!socket_path) {
			sway_log(SWAY_ERROR, "Unable to retrieve socket path");
			exit(EXIT_FAILURE);
		}
		char *command = join_args(argv + optind, argc - optind);
		run_as_ipc_client(command, socket_path);
		free(command);
		return 0;
	}

	if (!server_privileged_prepare(&server)) {
		return 1;
	}

	if (!drop_permissions()) {
		server_fini(&server);
		exit(EXIT_FAILURE);
	}

	// handle SIGTERM signals
	signal(SIGTERM, sig_handler);

	// prevent ipc from crashing sway
	signal(SIGPIPE, SIG_IGN);

	sway_log(SWAY_INFO, "Starting sway version " SWAY_VERSION);

	root = root_create();

	if (!server_init(&server)) {
		return 1;
	}

	if (validate) {
		bool valid = load_main_config(config_path, false, true);
		free(config_path);
		return valid ? 0 : 1;
	}

	ipc_init(&server);

	setenv("WAYLAND_DISPLAY", server.socket, true);
	if (!load_main_config(config_path, false, false)) {
		sway_terminate(EXIT_FAILURE);
		goto shutdown;
	}

	if (!server_start(&server)) {
		sway_terminate(EXIT_FAILURE);
		goto shutdown;
	}

	config->active = true;
	load_swaybars();
	run_deferred_commands();

	if (config->swaynag_config_errors.pid > 0) {
		swaynag_show(&config->swaynag_config_errors);
	}

	server_run(&server);

shutdown:
	sway_log(SWAY_INFO, "Shutting down sway");

	server_fini(&server);
	root_destroy(root);
	root = NULL;

	free(config_path);
	free_config(config);

	pango_cairo_font_map_set_default(NULL);

	return exit_value;
}
コード例 #7
0
ファイル: main.c プロジェクト: i7otep9wka/sway
int main(int argc, char **argv) {
	static int verbose = 0, debug = 0, validate = 0;

	static struct option long_options[] = {
		{"help", no_argument, NULL, 'h'},
		{"config", required_argument, NULL, 'c'},
		{"validate", no_argument, NULL, 'C'},
		{"debug", no_argument, NULL, 'd'},
		{"version", no_argument, NULL, 'v'},
		{"verbose", no_argument, NULL, 'V'},
		{"get-socketpath", no_argument, NULL, 'p'},
		{0, 0, 0, 0}
	};

	char *config_path = NULL;

	const char* usage =
		"Usage: sway [options] [command]\n"
		"\n"
		"  -h, --help             Show help message and quit.\n"
		"  -c, --config <config>  Specify a config file.\n"
		"  -C, --validate         Check the validity of the config file, then exit.\n"
		"  -d, --debug            Enables full logging, including debug information.\n"
		"  -v, --version          Show the version number and quit.\n"
		"  -V, --verbose          Enables more verbose logging.\n"
		"      --get-socketpath   Gets the IPC socket path and prints it, then exits.\n"
		"\n";

	int c;
	while (1) {
		int option_index = 0;
		c = getopt_long(argc, argv, "hCdvVpc:", long_options, &option_index);
		if (c == -1) {
			break;
		}
		switch (c) {
		case 'h': // help
			fprintf(stdout, "%s", usage);
			exit(EXIT_SUCCESS);
			break;
		case 'c': // config
			config_path = strdup(optarg);
			break;
		case 'C': // validate
			validate = 1;
			break;
		case 'd': // debug
			debug = 1;
			break;
		case 'v': // version
#if defined SWAY_GIT_VERSION && defined SWAY_GIT_BRANCH && defined SWAY_VERSION_DATE
			fprintf(stdout, "sway version %s (%s, branch \"%s\")\n", SWAY_GIT_VERSION, SWAY_VERSION_DATE, SWAY_GIT_BRANCH);
#else
			fprintf(stdout, "version not detected\n");
#endif
			exit(EXIT_SUCCESS);
			break;
		case 'V': // verbose
			verbose = 1;
			break;
		case 'p': ; // --get-socketpath
			if (getenv("SWAYSOCK")) {
				fprintf(stdout, "%s\n", getenv("SWAYSOCK"));
				exit(EXIT_SUCCESS);
			} else {
				fprintf(stderr, "sway socket not detected.\n");
				exit(EXIT_FAILURE);
			}
			break;
		default:
			fprintf(stderr, "%s", usage);
			exit(EXIT_FAILURE);
		}
	}

	if (optind < argc) { // Behave as IPC client
		if (getuid() != geteuid() || getgid() != getegid()) {
			if (setgid(getgid()) != 0 || setuid(getuid()) != 0) {
				sway_abort("Unable to drop root");
			}
		}
		char *socket_path = getenv("SWAYSOCK");
		if (!socket_path) {
			sway_abort("Unable to retrieve socket path");
		}
		char *command = join_args(argv + optind, argc - optind);
		run_as_ipc_client(command, socket_path);
		return 0;
	}

	// we need to setup logging before wlc_init in case it fails.
	if (debug) {
		init_log(L_DEBUG);
	} else if (verbose || validate) {
		init_log(L_INFO);
	} else {
		init_log(L_ERROR);
	}
	setenv("WLC_DIM", "0", 0);
	wlc_log_set_handler(wlc_log_handler);
	detect_proprietary();

	input_devices = create_list();

	/* Changing code earlier than this point requires detailed review */
	/* (That code runs as root on systems without logind, and wlc_init drops to
	 * another user.) */
	if (!wlc_init(&interface, argc, argv)) {
		return 1;
	}
	register_extensions();

	// handle SIGTERM signals
	signal(SIGTERM, sig_handler);

	// prevent ipc from crashing sway
	signal(SIGPIPE, SIG_IGN);

#if defined SWAY_GIT_VERSION && defined SWAY_GIT_BRANCH && defined SWAY_VERSION_DATE
	sway_log(L_INFO, "Starting sway version %s (%s, branch \"%s\")\n", SWAY_GIT_VERSION, SWAY_VERSION_DATE, SWAY_GIT_BRANCH);
#endif

	init_layout();

	if (validate) {
		bool valid = load_config(config_path);
		return valid ? 0 : 1;
	}

	if (!load_config(config_path)) {
		sway_log(L_ERROR, "Error(s) loading config!");
	}
	if (config_path) {
		free(config_path);
	}

	ipc_init();

	if (!terminate_request) {
		wlc_run();
	}

	if (input_devices) {
		free(input_devices);
	}

	ipc_terminate();

	return 0;
}
コード例 #8
0
ファイル: output.c プロジェクト: SirCmpwn/sway
struct cmd_results *cmd_output(int argc, char **argv) {
	struct cmd_results *error = NULL;
	if ((error = checkarg(argc, "output", EXPECTED_AT_LEAST, 1))) {
		return error;
	}
	const char *name = argv[0];

	struct output_config *output = calloc(1, sizeof(struct output_config));
	output->x = output->y = output->width = output->height = -1;
	output->name = strdup(name);
	output->enabled = -1;
	output->scale = 1;

	// TODO: atoi doesn't handle invalid numbers

	int i;
	for (i = 1; i < argc; ++i) {
		const char *command = argv[i];

		if (strcasecmp(command, "disable") == 0) {
			output->enabled = 0;
		} else if (strcasecmp(command, "resolution") == 0 || strcasecmp(command, "res") == 0) {
			if (++i >= argc) {
				return cmd_results_new(CMD_INVALID, "output", "Missing resolution argument.");
			}
			char *res = argv[i];
			char *x = strchr(res, 'x');
			int width = -1, height = -1;
			if (x != NULL) {
				// Format is 1234x4321
				*x = '\0';
				width = atoi(res);
				height = atoi(x + 1);
				*x = 'x';
			} else {
				// Format is 1234 4321
				width = atoi(res);
				if (++i >= argc) {
					return cmd_results_new(CMD_INVALID, "output", "Missing resolution argument (height).");
				}
				res = argv[i];
				height = atoi(res);
			}
			output->width = width;
			output->height = height;
		} else if (strcasecmp(command, "position") == 0 || strcasecmp(command, "pos") == 0) {
			if (++i >= argc) {
				return cmd_results_new(CMD_INVALID, "output", "Missing position argument.");
			}
			char *res = argv[i];
			char *c = strchr(res, ',');
			int x = -1, y = -1;
			if (c != NULL) {
				// Format is 1234,4321
				*c = '\0';
				x = atoi(res);
				y = atoi(c + 1);
				*c = ',';
			} else {
				// Format is 1234 4321
				x = atoi(res);
				if (++i >= argc) {
					return cmd_results_new(CMD_INVALID, "output", "Missing position argument (y).");
				}
				res = argv[i];
				y = atoi(res);
			}
			output->x = x;
			output->y = y;
		} else if (strcasecmp(command, "scale") == 0) {
			if (++i >= argc) {
				return cmd_results_new(CMD_INVALID, "output", "Missing scale parameter.");
			}
			output->scale = atoi(argv[i]);
		} else if (strcasecmp(command, "background") == 0 || strcasecmp(command, "bg") == 0) {
			wordexp_t p;
			if (++i >= argc) {
				return cmd_results_new(CMD_INVALID, "output", "Missing background file or color specification.");
			}
			if (i + 1 >= argc) {
				return cmd_results_new(CMD_INVALID, "output", "Missing background scaling mode or `solid_color`.");
			}
			if (strcasecmp(argv[argc - 1], "solid_color") == 0) {
				output->background = strdup(argv[argc - 2]);
				output->background_option = strdup("solid_color");
			} else {
				char *src = join_args(argv + i, argc - i - 1);
				char *mode = argv[argc - 1];
				if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) {
					return cmd_results_new(CMD_INVALID, "output", "Invalid syntax (%s)", src);
				}
				free(src);
				src = p.we_wordv[0];
				if (config->reading && *src != '/') {
					char *conf = strdup(config->current_config);
					char *conf_path = dirname(conf);
					src = malloc(strlen(conf_path) + strlen(src) + 2);
					sprintf(src, "%s/%s", conf_path, p.we_wordv[0]);
					free(conf);
				}
				if (access(src, F_OK) == -1) {
					return cmd_results_new(CMD_INVALID, "output", "Background file unreadable (%s)", src);
				}
				for (char *m = mode; *m; ++m) *m = tolower(*m);
				// Check mode
				bool valid = false;
				size_t j;
				for (j = 0; j < sizeof(bg_options) / sizeof(char *); ++j) {
					if (strcasecmp(mode, bg_options[j]) == 0) {
						valid = true;
						break;
					}
				}
				if (!valid) {
					return cmd_results_new(CMD_INVALID, "output", "Invalid background scaling mode.");
				}
				output->background = strdup(src);
				output->background_option = strdup(mode);
				if (src != p.we_wordv[0]) {
					free(src);
				}
				wordfree(&p);
			}
		}
	}

	i = list_seq_find(config->output_configs, output_name_cmp, name);
	if (i >= 0) {
		// merge existing config
		struct output_config *oc = config->output_configs->items[i];
		merge_output_config(oc, output);
		free_output_config(output);
		output = oc;
	} else {
		list_add(config->output_configs, output);
	}

	sway_log(L_DEBUG, "Config stored for output %s (enabled:%d) (%d x %d @ %d, %d scale %d) (bg %s %s)",
			output->name, output->enabled, output->width,
			output->height, output->x, output->y, output->scale,
			output->background, output->background_option);

	if (output->name) {
		// Try to find the output container and apply configuration now. If
		// this is during startup then there will be no container and config
		// will be applied during normal "new output" event from wlc.
		swayc_t *cont = NULL;
		for (int i = 0; i < root_container.children->length; ++i) {
			cont = root_container.children->items[i];
			if (cont->name && ((strcmp(cont->name, output->name) == 0) || (strcmp(output->name, "*") == 0))) {
				apply_output_config(output, cont);

				if (strcmp(output->name, "*") != 0) {
					// stop looking if the output config isn't applicable to all outputs
					break;
				}
			}
		}
	}

	return cmd_results_new(CMD_SUCCESS, NULL, NULL);
}
コード例 #9
0
ファイル: CubitProcess.cpp プロジェクト: chrismullins/cgma
PidType CubitProcess::start(const std::string& app, const std::vector<std::string>& args, bool hide)
{
#ifndef WIN32
  std::vector<const char*> c_args(args.size()+2);
  int idx = 0;
  c_args[idx++] = app.c_str();
  for(unsigned int i=0; i<args.size(); i++)
  {
    c_args[idx++] = args[i].c_str();
  }
  c_args[idx++] = NULL;
  
  std::string app_real = find_executable(app);

  // temporarily block delivery of child signals
  // TODO: does this overwrite currently set signals for the process?
  sigset_t newsig;
  sigemptyset(&newsig);
  sigaddset(&newsig, SIGCHLD);
  sigprocmask(SIG_BLOCK, &newsig, &oldsig);

  pid_t pid = fork();
  if(pid < 0)
    return 0;

  if(pid == 0)
  {
    execv(app_real.c_str(), const_cast<char**>(&c_args[0]));
    perror(app_real.c_str());
    exit(EXIT_FAILURE);
  }

  return pid;
#else

  STARTUPINFOW si;
  PROCESS_INFORMATION pi;
  
  ZeroMemory( &si, sizeof(si) );
  si.cb = sizeof(si);
  ZeroMemory( &pi, sizeof(pi) );
  
  // hide child window
  if(hide)
  {
    si.dwFlags |= STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_HIDE;
  }

  std::string real_app = find_executable(app);
  std::replace(real_app.begin(), real_app.end(), '/', '\\');
  
  std::string q_string = quote_string(app);
  std::wstring call = CubitString::toUtf16(q_string);
  std::string joined_args = join_args(args);
  call += L" ";
  call += CubitString::toUtf16(joined_args).c_str();


    // Start the child process. 
  if( CreateProcessW( CubitString::toUtf16(real_app).c_str(),  // path to cubit executable 
                      const_cast<wchar_t*>(call.c_str()), // Command line. 
                      NULL,      // Process handle not inheritable. 
                      NULL,      // Thread handle not inheritable. 
                      TRUE,     // Set handle inheritance to TRUE.
                      0,         // No creation flags. 
                      NULL,      // Use parent's environment block. 
                      NULL,      // Use parent's starting directory. 
                      &si,       // Pointer to STARTUPINFO structure.
                      &pi )      // Pointer to PROCESS_INFORMATION structure.
      )
  {
    return pi;
  }
  pi.hProcess = 0;
  return pi;
#endif
}
コード例 #10
0
ファイル: main.c プロジェクト: i7otep9wka/sway
int main(int argc, char **argv) {
	static int quiet = 0;
	char *socket_path = NULL;
	char *cmdtype = NULL;

	init_log(L_INFO);

	static struct option long_options[] = {
		{"help", no_argument, NULL, 'h'},
		{"quiet", no_argument, NULL, 'q'},
		{"version", no_argument, NULL, 'v'},
		{"socket", required_argument, NULL, 's'},
		{"type", required_argument, NULL, 't'},
		{0, 0, 0, 0}
	};

	const char *usage =
		"Usage: swaymsg [options] [message]\n"
		"\n"
		"  -h, --help             Show help message and quit.\n"
		"  -q, --quiet            Be quiet.\n"
		"  -v, --version          Show the version number and quit.\n"
		"  -s, --socket <socket>  Use the specified socket.\n"
		"  -t, --type <type>      Specify the message type.\n";

	int c;
	while (1) {
		int option_index = 0;
		c = getopt_long(argc, argv, "hqvs:t:", long_options, &option_index);
		if (c == -1) {
			break;
		}
		switch (c) {
		case 'q': // Quiet
			quiet = 1;
			break;
		case 's': // Socket
			socket_path = strdup(optarg);
			break;
		case 't': // Type
			cmdtype = strdup(optarg);
			break;
		case 'v':
#if defined SWAY_GIT_VERSION && defined SWAY_GIT_BRANCH && defined SWAY_VERSION_DATE
			fprintf(stdout, "sway version %s (%s, branch \"%s\")\n", SWAY_GIT_VERSION, SWAY_VERSION_DATE, SWAY_GIT_BRANCH);
#else
			fprintf(stdout, "version not detected\n");
#endif
			exit(EXIT_SUCCESS);
			break;
		default:
			fprintf(stderr, "%s", usage);
			exit(EXIT_FAILURE);
		}
	}

	if (!cmdtype) {
		cmdtype = strdup("command");
	}
	if (!socket_path) {
		socket_path = get_socketpath();
		if (!socket_path) {
			sway_abort("Unable to retrieve socket path");
		}
	}

	uint32_t type = IPC_COMMAND;

	if (strcasecmp(cmdtype, "command") == 0) {
		type = IPC_COMMAND;
	} else if (strcasecmp(cmdtype, "get_workspaces") == 0) {
		type = IPC_GET_WORKSPACES;
	} else if (strcasecmp(cmdtype, "get_inputs") == 0) {
		type = IPC_GET_INPUTS;
	} else if (strcasecmp(cmdtype, "get_outputs") == 0) {
		type = IPC_GET_OUTPUTS;
	} else if (strcasecmp(cmdtype, "get_tree") == 0) {
		type = IPC_GET_TREE;
	} else if (strcasecmp(cmdtype, "get_marks") == 0) {
		type = IPC_GET_MARKS;
	} else if (strcasecmp(cmdtype, "get_bar_config") == 0) {
		type = IPC_GET_BAR_CONFIG;
	} else if (strcasecmp(cmdtype, "get_version") == 0) {
		type = IPC_GET_VERSION;
	} else {
		sway_abort("Unknown message type %s", cmdtype);
	}
	free(cmdtype);

	char *command = strdup("");
	if (optind < argc) {
		command = join_args(argv + optind, argc - optind);
	}

	int socketfd = ipc_open_socket(socket_path);
	uint32_t len = strlen(command);
	char *resp = ipc_single_command(socketfd, type, command, &len);
	if (!quiet) {
		printf("%s\n", resp);
	}
	close(socketfd);

	free(command);
	free(resp);
	free(socket_path);
	return 0;
}
コード例 #11
0
ファイル: main.c プロジェクト: sleep-walker/sway
int main(int argc, char **argv) {
	static int quiet = 0;
	static int raw = 0;
	char *socket_path = NULL;
	char *cmdtype = NULL;

	init_log(L_INFO);

	static struct option long_options[] = {
		{"help", no_argument, NULL, 'h'},
		{"quiet", no_argument, NULL, 'q'},
		{"raw", no_argument, NULL, 'r'},
		{"socket", required_argument, NULL, 's'},
		{"type", required_argument, NULL, 't'},
		{"version", no_argument, NULL, 'v'},
		{0, 0, 0, 0}
	};

	const char *usage =
		"Usage: swaymsg [options] [message]\n"
		"\n"
		"  -h, --help             Show help message and quit.\n"
		"  -q, --quiet            Be quiet.\n"
		"  -r, --raw              Use raw output even if using a tty\n"
		"  -s, --socket <socket>  Use the specified socket.\n"
		"  -t, --type <type>      Specify the message type.\n"
		"  -v, --version          Show the version number and quit.\n";

	raw = !isatty(STDOUT_FILENO);

	int c;
	while (1) {
		int option_index = 0;
		c = getopt_long(argc, argv, "hqrs:t:v", long_options, &option_index);
		if (c == -1) {
			break;
		}
		switch (c) {
		case 'q': // Quiet
			quiet = 1;
			break;
		case 'r': // Raw
			raw = 1;
			break;
		case 's': // Socket
			socket_path = strdup(optarg);
			break;
		case 't': // Type
			cmdtype = strdup(optarg);
			break;
		case 'v':
#if defined SWAY_GIT_VERSION && defined SWAY_GIT_BRANCH && defined SWAY_VERSION_DATE
			fprintf(stdout, "sway version %s (%s, branch \"%s\")\n", SWAY_GIT_VERSION, SWAY_VERSION_DATE, SWAY_GIT_BRANCH);
#else
			fprintf(stdout, "version not detected\n");
#endif
			exit(EXIT_SUCCESS);
			break;
		default:
			fprintf(stderr, "%s", usage);
			exit(EXIT_FAILURE);
		}
	}

	if (!cmdtype) {
		cmdtype = strdup("command");
	}
	if (!socket_path) {
		socket_path = get_socketpath();
		if (!socket_path) {
			sway_abort("Unable to retrieve socket path");
		}
	}

	uint32_t type = IPC_COMMAND;

	if (strcasecmp(cmdtype, "command") == 0) {
		type = IPC_COMMAND;
	} else if (strcasecmp(cmdtype, "get_workspaces") == 0) {
		type = IPC_GET_WORKSPACES;
	} else if (strcasecmp(cmdtype, "get_inputs") == 0) {
		type = IPC_GET_INPUTS;
	} else if (strcasecmp(cmdtype, "get_outputs") == 0) {
		type = IPC_GET_OUTPUTS;
	} else if (strcasecmp(cmdtype, "get_tree") == 0) {
		type = IPC_GET_TREE;
	} else if (strcasecmp(cmdtype, "get_marks") == 0) {
		type = IPC_GET_MARKS;
	} else if (strcasecmp(cmdtype, "get_bar_config") == 0) {
		type = IPC_GET_BAR_CONFIG;
	} else if (strcasecmp(cmdtype, "get_version") == 0) {
		type = IPC_GET_VERSION;
	} else {
		sway_abort("Unknown message type %s", cmdtype);
	}
	free(cmdtype);

	char *command = strdup("");
	if (optind < argc) {
		command = join_args(argv + optind, argc - optind);
	}

	int ret = 0;
	int socketfd = ipc_open_socket(socket_path);
	uint32_t len = strlen(command);
	char *resp = ipc_single_command(socketfd, type, command, &len);
	if (!quiet) {
		// pretty print the json
		json_object *obj = json_tokener_parse(resp);

		if (obj == NULL) {
			fprintf(stderr, "ERROR: Could not parse json response from ipc. This is a bug in sway.");
			printf("%s\n", resp);
			ret = 1;
		} else {
			if (raw) {
				printf("%s\n", json_object_to_json_string_ext(obj,
					JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED));
			} else {
				pretty_print(type, obj);
			}
			free(obj);
		}
	}
	close(socketfd);

	free(command);
	free(resp);
	free(socket_path);
	return ret;
}