Modify it to adjust Tizen IVI enviroment
[platform/upstream/kmscon.git] / tests / test_include.h
1 /*
2  * kmscon - Common test functions
3  *
4  * Copyright (c) 2012 David Herrmann <dh.herrmann@googlemail.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files
8  * (the "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 #include <errno.h>
27 #include <signal.h>
28 #include <stdbool.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/signalfd.h>
32 #include "conf.h"
33 #include "eloop.h"
34 #include "shl_log.h"
35
36 #define TEST_HELP \
37         "\t-h, --help                  [off]   Print this help and exit\n" \
38         "\t-v, --verbose               [off]   Print verbose messages\n" \
39         "\t    --debug                 [off]   Enable debug mode\n" \
40         "\t    --silent                [off]   Suppress notices and warnings\n"
41
42 static struct {
43         bool help;
44         bool exit;
45         bool verbose;
46         bool debug;
47         bool silent;
48 } test_conf;
49
50 static struct conf_ctx *test_ctx;
51
52 static int aftercheck_debug(struct conf_option *opt, int argc, char **argv,
53                             int idx)
54 {
55         /* --debug implies --verbose */
56         if (test_conf.debug)
57                 test_conf.verbose = 1;
58
59         return 0;
60 }
61
62 static int aftercheck_help(struct conf_option *opt, int argc, char **argv,
63                            int idx)
64 {
65         /* exit after printing --help information */
66         if (test_conf.help) {
67                 print_help();
68                 test_conf.exit = true;
69         }
70
71         return 0;
72 }
73
74 #define TEST_OPTIONS \
75         CONF_OPTION_BOOL_FULL('h', "help", aftercheck_help, NULL, NULL, &test_conf.help, false), \
76         CONF_OPTION_BOOL('v', "verbose", &test_conf.verbose, false), \
77         CONF_OPTION_BOOL_FULL(0, "debug", aftercheck_debug, NULL, NULL, &test_conf.debug, false), \
78         CONF_OPTION_BOOL(0, "silent", &test_conf.silent, false)
79
80 static void sig_generic(struct ev_eloop *p, struct signalfd_siginfo *info,
81                         void *data)
82 {
83         struct ev_eloop *eloop = data;
84
85         ev_eloop_exit(eloop);
86         log_info("terminating due to caught signal %d", info->ssi_signo);
87 }
88
89 static int test_prepare(struct conf_option *opts, size_t len,
90                         int argc, char **argv, struct ev_eloop **out)
91 {
92         int ret;
93         struct ev_eloop *eloop;
94
95         ret = conf_ctx_new(&test_ctx, opts, len, &test_conf);
96         if (ret)
97                 return ret;
98
99         ret = conf_ctx_parse_argv(test_ctx, argc, argv);
100         if (ret)
101                 goto err_out;
102
103         if (test_conf.exit) {
104                 ret = -ECANCELED;
105                 goto err_out;
106         }
107
108         if (!test_conf.debug && !test_conf.verbose && test_conf.silent)
109                 log_set_config(&LOG_CONFIG_WARNING(0, 0, 0, 0));
110         else
111                 log_set_config(&LOG_CONFIG_INFO(test_conf.debug,
112                                                 test_conf.verbose));
113
114         log_print_init(argv[0]);
115
116         ret = ev_eloop_new(&eloop, log_llog, NULL);
117         if (ret)
118                 goto err_out;
119
120         ret = ev_eloop_register_signal_cb(eloop, SIGTERM, sig_generic, eloop);
121         if (ret)
122                 goto err_unref;
123
124         ret = ev_eloop_register_signal_cb(eloop, SIGINT, sig_generic, eloop);
125         if (ret) {
126                 ev_eloop_unregister_signal_cb(eloop, SIGTERM,
127                                                 sig_generic, eloop);
128                 goto err_unref;
129         }
130
131         *out = eloop;
132         return 0;
133
134 err_unref:
135         ev_eloop_unref(eloop);
136 err_out:
137         conf_ctx_free(test_ctx);
138         return ret;
139 }
140
141 static void test_fail(int ret)
142 {
143         if (ret)
144                 log_err("init failed, errno %d: %s", ret, strerror(-ret));
145 }
146
147 static void test_exit(struct conf_option *opts, size_t len,
148                       struct ev_eloop *eloop)
149 {
150         ev_eloop_unregister_signal_cb(eloop, SIGINT, sig_generic, eloop);
151         ev_eloop_unregister_signal_cb(eloop, SIGTERM, sig_generic, eloop);
152         ev_eloop_unref(eloop);
153         conf_ctx_free(test_ctx);
154 }