main.c

来自「Gqview,Linux下基于GTK+库写成的轻量级而能丰富的图像浏览程序。」· C语言 代码 · 共 1,367 行 · 第 1/3 页

C
1,367
字号
		layout_set_path(NULL, text);		}	else		{		printf("remote sent filename that does not exist:\"%s\"\n", text);		}}static void gr_file_view(const gchar *text, gpointer data){	view_window_new(text);}static void gr_list_clear(const gchar *text, gpointer data){	if (gqview_command_collection) collection_unref(gqview_command_collection);	gqview_command_collection = NULL;}static void gr_list_add(const gchar *text, gpointer data){	gint new = TRUE;	if (!gqview_command_collection)		{		CollectionData *cd;		cd = collection_new("");		g_free(cd->path);		cd->path = NULL;		g_free(cd->name);		cd->name = g_strdup(_("Command line"));		gqview_command_collection = cd;		}	else		{		new = (!collection_get_first(gqview_command_collection));		}	if (collection_add(gqview_command_collection, text, FALSE) && new)		{		layout_image_set_collection(NULL, gqview_command_collection,					    collection_get_first(gqview_command_collection));		}}static void gr_raise(const gchar *text, gpointer data){	LayoutWindow *lw = NULL;	if (layout_valid(&lw))		{		gtk_window_present(GTK_WINDOW(lw->window));		}}typedef struct _RemoteCommandEntry RemoteCommandEntry;struct _RemoteCommandEntry {	gchar *opt_s;	gchar *opt_l;	void (*func)(const gchar *text, gpointer data);	gint needs_extra;	gint prefer_command_line;	gchar *description;};static RemoteCommandEntry remote_commands[] = {	/* short, long                  callback,               extra, prefer,description */	{ "-n", "--next",               gr_image_next,          FALSE, FALSE, N_("next image") },	{ "-b", "--back",               gr_image_prev,          FALSE, FALSE, N_("previous image") },	{ NULL, "--first",              gr_image_first,         FALSE, FALSE, N_("first image") },        { NULL, "--last",               gr_image_last,          FALSE, FALSE, N_("last image") },	{ "-f", "--fullscreen",         gr_fullscreen_toggle,   FALSE, TRUE,  N_("toggle full screen") },	{ "-fs","--fullscreen-start",   gr_fullscreen_start,    FALSE, FALSE, N_("start full screen") },	{ "-fS","--fullscreen-stop",    gr_fullscreen_stop,     FALSE, FALSE, N_("stop full screen") },	{ "-s", "--slideshow",          gr_slideshow_toggle,	FALSE, TRUE,  N_("toggle slide show") },	{ "-ss","--slideshow-start",    gr_slideshow_start,     FALSE, FALSE, N_("start slide show") },	{ "-sS","--slideshow-stop",     gr_slideshow_stop,      FALSE, FALSE, N_("stop slide show") },	{ "-sr","--slideshow-recurse",  gr_slideshow_start_rec, TRUE,  FALSE, N_("start recursive slide show") },	{ "-d", "--delay=",             gr_slideshow_delay,     TRUE,  FALSE, N_("set slide show delay in seconds") },	{ "+t", "--tools-show",         gr_tools_show,          FALSE, TRUE,  N_("show tools") },	{ "-t", "--tools-hide",	        gr_tools_hide,          FALSE, TRUE,  N_("hide tools") },	{ "-q", "--quit",               gr_quit,                FALSE, FALSE, N_("quit") },	{ NULL, "file:",                gr_file_load,           TRUE,  FALSE, N_("open file") },	{ NULL, "view:",		gr_file_view,		TRUE,  FALSE, N_("open file in new window") },	{ NULL, "--list-clear",         gr_list_clear,          FALSE, FALSE, NULL },	{ NULL, "--list-add:",          gr_list_add,            TRUE,  FALSE, NULL },	{ NULL, "raise",		gr_raise,		FALSE, FALSE, NULL },	{ NULL, NULL, NULL, FALSE, FALSE, NULL }};static RemoteCommandEntry *gqview_remote_command_find(const gchar *text, const gchar **offset){	gint match = FALSE;	gint i;	i = 0;	while (!match && remote_commands[i].func != NULL)		{		if (remote_commands[i].needs_extra)			{			if (remote_commands[i].opt_s &&			    strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)				{				if (offset) *offset = text + strlen(remote_commands[i].opt_s);				return &remote_commands[i];				}			else if (remote_commands[i].opt_l &&				 strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)				{				if (offset) *offset = text + strlen(remote_commands[i].opt_l);				return &remote_commands[i];				}			}		else			{			if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||			    (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))				{				if (offset) *offset = text;				return &remote_commands[i];				}			}		i++;		}	return NULL;}static void gqview_remote_cb(RemoteConnection *rc, const gchar *text, gpointer data){	RemoteCommandEntry *entry;	const gchar *offset;	entry = gqview_remote_command_find(text, &offset);	if (entry && entry->func)		{		entry->func(offset, data);		}	else		{		printf("unknown remote command:%s\n", text);		}}static void gqview_remote_help(void){	gint i;	print_term(_("Remote command list:\n"));	i = 0;	while (remote_commands[i].func != NULL)		{		if (remote_commands[i].description)			{			gchar *buf;			buf = g_strdup_printf("  %-3s%s %-20s %s\n",				(remote_commands[i].opt_s) ? remote_commands[i].opt_s : "",				(remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ",				(remote_commands[i].opt_l) ? remote_commands[i].opt_l : "",				_(remote_commands[i].description));			print_term(buf);			g_free(buf);			}		i++;		}}static GList *gqview_remote_build_list(GList *list, int argc, char *argv[]){	gint i;	i = 1;	while (i < argc)		{		RemoteCommandEntry *entry;		entry = gqview_remote_command_find(argv[i], NULL);		if (entry)			{			list = g_list_append(list, argv[i]);			}		i++;		}	return list;}static void gqview_remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,				  GList *cmd_list, GList *collection_list){	RemoteConnection *rc;	gint started = FALSE;	gchar *buf;	buf = g_strconcat(homedir(), "/", GQVIEW_RC_DIR, "/.command", NULL);	rc = remote_client_open(buf);	if (!rc)		{		GString *command;		GList *work;		gint retry_count = 12;		gint blank = FALSE;		print_term(_("Remote GQview not running, starting..."));		command = g_string_new(arg_exec);		work = remote_list;		while (work)			{			gchar *text;			RemoteCommandEntry *entry;			text = work->data;			work = work->next;			entry = gqview_remote_command_find(text, NULL);			if (entry)				{				if (entry->prefer_command_line)					{					remote_list = g_list_remove(remote_list, text);					g_string_append(command, " ");					g_string_append(command, text);					}				if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)					{					blank = TRUE;					}				}			}		if (blank || cmd_list || path) g_string_append(command, " --blank");		if (debug) g_string_append(command, " --debug");		g_string_append(command, " &");		system(command->str);		g_string_free(command, TRUE);		while (!rc && retry_count > 0)			{			usleep((retry_count > 10) ? 500000 : 1000000);			rc = remote_client_open(buf);			if (!rc) print_term(".");			retry_count--;			}		print_term("\n");		started = TRUE;		}	g_free(buf);	if (rc)		{		GList *work;		const gchar *prefix;		gint use_path = TRUE;		gint sent = FALSE;		work = remote_list;		while (work)			{			gchar *text;			RemoteCommandEntry *entry;			text = work->data;			work = work->next;			entry = gqview_remote_command_find(text, NULL);			if (entry &&			    entry->opt_l &&			    strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;			remote_client_send(rc, text);			sent = TRUE;			}		if (cmd_list && cmd_list->next)			{			prefix = "--list-add:";			remote_client_send(rc, "--list-clear");			}		else			{			prefix = "file:";			}		work = cmd_list;		while (work)			{			const gchar *name;			gchar *text;			name = work->data;			work = work->next;			text = g_strconcat(prefix, name, NULL);			remote_client_send(rc, text);			g_free(text);			sent = TRUE;			}		if (path && !cmd_list && use_path)			{			gchar *text;			text = g_strdup_printf("file:%s", path);			remote_client_send(rc, text);			g_free(text);			sent = TRUE;			}		work = collection_list;		while (work)			{			const gchar *name;			gchar *text;			name = work->data;			work = work->next;			text = g_strdup_printf("file:%s", name);			remote_client_send(rc, text);			g_free(text);			sent = TRUE;			}		if (!started && !sent)			{			remote_client_send(rc, "raise");			}		}	else		{		print_term(_("Remote not available\n"));		}	_exit(0);}/* *----------------------------------------------------------------------------- * command line parser (private) hehe, who needs popt anyway? *----------------------------------------------------------------------------- */ static gint startup_blank = FALSE;static gint startup_full_screen = FALSE;static gint startup_in_slideshow = FALSE;static gint startup_command_line_collection = FALSE;static void parse_command_line_add_file(const gchar *new_path, gchar **path, gchar **file,				        GList **list, GList **collection_list){	gchar *path_parsed;	path_parsed = g_strdup(new_path);	parse_out_relatives(path_parsed);	if (file_extension_match(new_path, ".gqv"))		{		*collection_list = g_list_append(*collection_list, path_parsed);		}	else		{		if (!*path) *path = remove_level_from_path(path_parsed);		if (!*file) *file = g_strdup(path_parsed);		*list = g_list_append(*list, path_parsed);		}}static void parse_command_line(int argc, char *argv[], gchar **path, gchar **file,			       GList **cmd_list, GList **collection_list){	GList *list = NULL;	GList *remote_list = NULL;	gint remote_do = FALSE;	if (argc > 1)		{		gint i;		gchar *base_dir = get_current_dir();		i = 1;		while (i < argc)			{			const gchar *cmd_line = argv[i];			gchar *cmd_all = concat_dir_and_file(base_dir, cmd_line);			if (!*path && cmd_line[0] == '/' && isdir(cmd_line))				{				*path = g_strdup(cmd_line);				}			else if (!*path && isdir(cmd_all))				{				*path = g_strdup(cmd_all);				}			else if (cmd_line[0] == '/' && isfile(cmd_line))				{				parse_command_line_add_file(cmd_line, path, file, &list, collection_list);				}			else if (isfile(cmd_all))				{				parse_command_line_add_file(cmd_all, path, file, &list, collection_list);				}			else if (strcmp(cmd_line, "--debug") == 0)				{				/* we now increment the debug state for verbosity */				debug++;				printf("debugging output enabled (level %d)\n", debug);				}			else if (strcmp(cmd_line, "+t") == 0 ||				 strcmp(cmd_line, "--with-tools") == 0)				{				tools_float = FALSE;				tools_hidden = FALSE;				remote_list = g_list_append(remote_list, "+t");				}			else if (strcmp(cmd_line, "-t") == 0 ||				 strcmp(cmd_line, "--without-tools") == 0)				{				tools_hidden = TRUE;				remote_list = g_list_append(remote_list, "-t");				}			else if (strcmp(cmd_line, "-f") == 0 ||				 strcmp(cmd_line, "--fullscreen") == 0)				{				startup_full_screen = TRUE;				}			else if (strcmp(cmd_line, "-s") == 0 ||				 strcmp(cmd_line, "--slideshow") == 0)				{				startup_in_slideshow = TRUE;				}			else if (strcmp(cmd_line, "-l") == 0 ||				 strcmp(cmd_line, "--list") == 0)				{				startup_command_line_collection = TRUE;				}			else if (strcmp(cmd_line, "-r") == 0 ||				 strcmp(cmd_line, "--remote") == 0)				{				if (!remote_do)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?