-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.c
124 lines (115 loc) · 3.67 KB
/
main.c
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
/*
* =============================================================================
*
* Filename: main.c
*
* Description: g3kb-switch program
*
* Created: 10.12.2019 14:59:38
* Revision: none
* Compiler: gcc
*
* Author: Alexey Radkov (),
* Company:
*
* =============================================================================
*/
#include "config.h"
#include "switch.h"
static void version( void )
{
g_print( "g3kb-switch version %s\n", PROJECT_VERSION );
}
static void usage( void )
{
version();
g_print(
"Usage: g3kb-switch [-p] Show the current layout group\n"
" g3kb-switch -l Show available layout groups\n"
" g3kb-switch -n Switch to the next layout group\n"
" g3kb-switch -s ARG Switch to layout group ARG\n"
" g3kb-switch -h Show this message and exit\n"
" g3kb-switch -v Show the program version and exit\n" );
}
int main( int argc, char **argv )
{
GTree *layouts;
gchar *new_layout = NULL;
gconstpointer layout;
enum command {
display_current_layout,
print_layouts,
activate_new_layout,
activate_next_layout,
};
enum command cmd = display_current_layout;
GError *err = NULL;
if ( argc > 1 ) {
if ( g_strcmp0( argv[ 1 ], "-h" ) == 0 ||
g_strcmp0( argv[ 1 ], "--help" ) == 0 ) {
usage();
return 0;
} else if ( g_strcmp0( argv[ 1 ], "-v" ) == 0 ||
g_strcmp0( argv[ 1 ], "--version" ) == 0 ) {
version();
return 0;
} else if ( g_strcmp0( argv[ 1 ], "-l" ) == 0 ) {
cmd = print_layouts;
} else if ( g_strcmp0( argv[ 1 ], "-p" ) == 0 ) {
cmd = display_current_layout;
} else if ( g_strcmp0( argv[ 1 ], "-s" ) == 0 ) {
if ( argc < 3 ) {
usage();
return 1;
}
cmd = activate_new_layout;
new_layout = argv[ 2 ];
} else if ( g_strcmp0( argv[ 1 ], "-n" ) == 0 ) {
cmd = activate_next_layout;
} else {
usage();
return 1;
}
}
layouts = g3kb_build_layouts_map( &err );
if ( layouts == NULL ) {
g_printerr( "Failed to build keyboard layouts map: %s\n",
err == NULL ? "<no details>" : err->message );
g_clear_error( &err );
return 1;
}
int status = 0;
switch ( cmd ) {
case print_layouts:
g_tree_foreach( layouts, g3kb_print_layouts, NULL );
break;
case display_current_layout:
layout = g3kb_safe_get_layout( layouts, &err );
if ( layout == NULL ) {
g_printerr( "Failed to find current layout: %s\n",
err == NULL ? "<no details>" : err->message );
g_clear_error( &err );
status = 1;
break;
}
g_print( "%s\n", (char *)layout );
break;
case activate_new_layout:
if ( !g3kb_safe_set_layout( layouts, new_layout, &err ) ) {
g_printerr( "Failed to activate layout %s: %s\n", new_layout,
err == NULL ? "<no details>" : err->message );
g_clear_error( &err );
status = 1;
}
break;
case activate_next_layout:
if ( !g3kb_set_next_layout( layouts, &err ) ) {
g_printerr( "Failed to activate next layout: %s\n",
err == NULL ? "<no details>" : err->message );
g_clear_error( &err );
status = 1;
}
}
g_tree_unref( layouts );
return status;
}