File: cmdparser.cpp

package info (click to toggle)
gxtuner 2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 704 kB
  • sloc: cpp: 8,429; makefile: 126
file content (199 lines) | stat: -rw-r--r-- 6,441 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * Copyright (C) 2011 Hermann Meyer, Andreas Degert
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 * ---------------------------------------------------------------------------
 *
 *        file: comparse.cpp      guitar tuner for jack
 *
 * ----------------------------------------------------------------------------
 */


#include "./cmdparser.h"
#include "./config.h"

CmdParse::CmdParse() {}
CmdParse::~CmdParse() {}

void CmdParse::init() {
    error           = NULL;
    infostring      = "\n        version ";
    infostring      += VERSION;
    infostring      += "\n    A simple (linux) guitar and bass tuner for jack\n"; 
    infostring      += "    with full jack session managment support";
    opt_context     = g_option_context_new(infostring.c_str());
    jack_uuid       = NULL;
    jack_input      = NULL;
    size_x          = NULL;
    size_y          = NULL;
    pos_x           = NULL;
    pos_y           = NULL;
    desktop         = NULL;
    pitch           = NULL;
    threshold       = NULL;
}

void CmdParse::write_optvar() {
    // *** process UUID (hidden option)
    if (jack_uuid != NULL) {
        optvar[JACK_UUID] = jack_uuid; // leads to no automatic connection
        g_free(jack_uuid);
    } else if (!optvar[JACK_UUID].empty()) {
        optvar[JACK_UUID] = ""; 
    }

    // *** process ENGINE options
    if (pitch != NULL) {
        optvar[PITCH] = pitch;
        g_free(pitch);
    } else if (!optvar[PITCH].empty()) {
        optvar[PITCH] = ""; 
    }

    if (threshold != NULL) {
        optvar[THRESHOLD] = threshold;
        g_free(threshold);
    } else if (!optvar[THRESHOLD].empty()) {
        optvar[THRESHOLD] = ""; 
    }

    // *** process GTK options
    if (size_x != NULL) {
        optvar[SIZE_Y] = size_y;
        g_free(size_y);
    } else if (!optvar[SIZE_Y].empty()) {
        optvar[SIZE_Y] = ""; 
    }

    if (size_y != NULL) {
        optvar[SIZE_X] = size_x;
        g_free(size_x);
    } else if (!optvar[SIZE_X].empty()) {
        optvar[SIZE_X] = ""; 
    }

    if (pos_y != NULL) {
        optvar[POS_Y] = pos_y;
        g_free(pos_y);
    } else if (!optvar[POS_Y].empty()) {
        optvar[POS_Y] = ""; 
    }

    if (pos_x != NULL) {
        optvar[POS_X] = pos_x;
        g_free(pos_x);
    } else if (!optvar[POS_X].empty()) {
        optvar[POS_X] = ""; 
    }

    if (desktop != NULL) {
        optvar[DESK] = desktop;
        g_free(desktop);
    } else if (!optvar[DESK].empty()) {
        optvar[DESK] = ""; 
    }
    
    // *** process JACK options
    if (jack_input != NULL) {
        optvar[JACK_INP] = jack_input;
        g_free(jack_input);
    } else if (!optvar[JACK_INP].empty()) {
        optvar[JACK_INP] = ""; // leads to no automatic connection
    }
}

void CmdParse::parse(int& argc, char**& argv) {
    // parsing command options
    if (!g_option_context_parse(opt_context, &argc, &argv, &error)) {
        g_print ("option parsing failed: %s\n", error->message);
        error = NULL;
    }
    g_option_context_free(opt_context);
}

void CmdParse::setup_groups() {
    optgroup_gtk = g_option_group_new("gtk",
          "\033[1;32mGTK configuration options\033[0m",
          "\033[1;32mGTK configuration options\033[0m",
          NULL, NULL);
    GOptionEntry opt_entries_gtk[] =
    {
        { "posx", 'x', 0, G_OPTION_ARG_STRING, &pos_x,
            "window position x-axis ( -x 1 -> . .)", "POSITION_X"},
        { "posy", 'y', 0, G_OPTION_ARG_STRING, &pos_y,
            "window position y-axis ( -y 1 -> . . )", "POSITION_Y" },
        { "wigth", 'w', 0, G_OPTION_ARG_STRING, &size_x,
            "'default' wigth ( -w 120 -> . .)", "WEIGTH"},
        { "height", 'l', 0, G_OPTION_ARG_STRING, &size_y,
            "'default' height ( -l 100 -> . .)", "HEIGHT" },
        { "desktop", 'd', 0, G_OPTION_ARG_STRING, &desktop,
            "set to virtual desktop num", "NUM" },
        { NULL }
    };
    g_option_group_add_entries(optgroup_gtk, opt_entries_gtk);

    optgroup_jack = g_option_group_new("jack",
          "\033[1;32mJACK configuration options\033[0m",
          "\033[1;32mJACK configuration options\033[0m",
          NULL, NULL);
    GOptionEntry opt_entries_jack[] =
    {
        { "jack-input", 'i', 0, G_OPTION_ARG_STRING, &jack_input,
            "connect to JACK port name (-i system:capture_1)", "PORT" },
        { NULL }
    };
    g_option_group_add_entries(optgroup_jack, opt_entries_jack);

    GOptionEntry opt_entries_uuid[] =
    {
        { "jack-uuid", 'U', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &jack_uuid,
            "JACK session UUID (set by session manager)", "UUID" },
        { NULL }
    };
    g_option_group_add_entries(optgroup_jack, opt_entries_uuid);

    optgroup_engine = g_option_group_new("engine",
          "\033[1;32mENGINE configuration options\033[0m",
          "\033[1;32mENGINE configuration options\033[0m",
          NULL, NULL);
    GOptionEntry opt_entries_engine[] =
    {
        { "pitch", 'p', 0, G_OPTION_ARG_STRING, &pitch,
            "set reference pitch ( -p 415,0 <-> 467,0)", "PITCH"},
        { "threshold", 't', 0, G_OPTION_ARG_STRING, &threshold,
            "set threshold level (-t 0,001 <-> 0,5)", "THRESHOLD" },
        { NULL }
    };
    g_option_group_add_entries(optgroup_engine, opt_entries_engine);
    
    g_option_context_add_group(opt_context, optgroup_gtk);
    g_option_context_add_group(opt_context, optgroup_jack);
    g_option_context_add_group(opt_context, optgroup_engine);
    g_option_context_set_ignore_unknown_options(opt_context, true);
}

// ---- parse command line options
void CmdParse::process_cmdline_options(int& argc, char**& argv)
{
    init();
    setup_groups();
    parse(argc, argv);
    write_optvar();
}

CmdParse cmd;