File: main.c

package info (click to toggle)
winwrangler 0.2.4-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 1,612 kB
  • ctags: 169
  • sloc: sh: 10,265; ansic: 935; makefile: 96
file content (130 lines) | stat: -rw-r--r-- 3,253 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * This file is part of WinWrangler.
 * Copyright (C) Mikkel Kamstrup Erlandsen 2008 <mikkel.kamstrup@gmail.com>
 *
 *  WinWrangler is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  WinWrangler is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with WinWranger.  If not, see <http://www.gnu.org/licenses/>.
 */


#include <glib.h>
#include <glib/gi18n.h>

#include "winwrangler.h"

static gchar *layout_name = NULL;
static gboolean print_layouts = FALSE;
static gboolean run_tray = FALSE;
static gboolean run_daemon = FALSE;

static GOptionEntry option_entries[] = {
	{ "layout", 'l', 0, G_OPTION_ARG_STRING, &layout_name,
	  N_("The layout function to apply") },
	{ "layouts", 0, 0, G_OPTION_ARG_NONE, &print_layouts,
	  N_("Print a list of layout functions") },
	{ "tray", 't', 0, G_OPTION_ARG_NONE, &run_tray,
	  N_("Add an icon in the system tray. This implies --daemon") },
	{ "daemon", 'd', 0, G_OPTION_ARG_NONE, &run_daemon,
	  N_("Run a background process listening for hotkey events") },
	{ NULL }
};

static void
do_print_layouts (const WwLayout *layouts)
{
	WwLayout *layout;
	
	g_return_if_fail (layouts != NULL);
	
	g_print ("Known layouts:\n");
	for (layout = layouts; layout->name != NULL; layout++)
	{
		g_return_if_fail (layout != NULL);
		g_print (" - %-15s %s\n", layout->name, layout->desc);
	}
}

void
do_bind_keys (void)
{
	WwLayout *iter, *layouts;
	
	layouts = ww_get_layouts ();
	
	for (iter = layouts; iter->name != NULL; iter++)
		ww_hotkey_bind_layout (iter);
}

int
main (int argc, char *argv[])
{
	const WwLayout  *layouts;
	GError			*error;
	GOptionContext  *options;
	GtkStatusIcon	*tray_icon;
	
#ifdef ENABLE_NLS
	bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	textdomain (GETTEXT_PACKAGE);
#endif
	
	gtk_init (&argc, &argv);
	
	layouts = ww_get_layouts ();
	
	options = g_option_context_new (NULL);
	g_option_context_add_main_entries (options, option_entries,
									   GETTEXT_PACKAGE);
	g_option_context_add_group (options, gtk_get_option_group (TRUE));
	
	error = NULL;
	if (!g_option_context_parse (options, &argc, &argv, &error))
	{
		g_printerr (_("Invalid command line: %s\n"), error->message);
		return 1;
	}
	
	if (print_layouts)
	{
		do_print_layouts (layouts);
	}
	else if (layout_name)
	{
		ww_apply_layout_by_name (layout_name);
	}
	
	if (run_tray) {
		run_daemon = TRUE;
		tray_icon = ww_tray_icon_new ();
	}
	
	if (run_daemon) {
		do_bind_keys();
		gtk_main();
	}
	
	else if (!layout_name &&
			 !print_layouts &&
			 !run_daemon &&
			 !run_tray)
	{
		gchar *help_msg = g_option_context_get_help (options, TRUE, NULL);
		g_print ("%s", help_msg);
		g_free (help_msg);
		return 1;
	}
	
	return 0;
}