tests: make all tests use the new conf-parsers
[platform/upstream/kmscon.git] / tests / test_output.c
1 /*
2  * test_output - Test KMS/DRI output
3  *
4  * Copyright (c) 2011-2012 David Herrmann <dh.herrmann@googlemail.com>
5  * Copyright (c) 2011 University of Tuebingen
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files
9  * (the "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included
16  * in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  */
26
27 /*
28  * Test KMS/DRI output subsystem
29  * This is an example how to use the output subsystem. Invoked without
30  * arguments it prints a list of all connected outputs and their modes.
31  * If you pass any argument it will enable all outputs for 5seconds.
32  *
33  * This lists all outputs:
34  * $ ./test_output
35  *
36  * This would show a test screen:
37  * $ ./test_output something
38  */
39
40 static void print_help();
41
42 #include <errno.h>
43 #include <inttypes.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48
49 #include "eloop.h"
50 #include "log.h"
51 #include "uterm.h"
52 #include "test_include.h"
53
54 /* eloop object */
55 static struct ev_eloop *eloop;
56
57 struct {
58         bool fbdev;
59         bool test;
60 } output_conf;
61
62 static int blit_outputs(struct uterm_video *video)
63 {
64         struct uterm_display *iter;
65         int j, ret;
66         struct uterm_screen *screen;
67
68         j = 0;
69         iter = uterm_video_get_displays(video);
70         for ( ; iter; iter = uterm_display_next(iter)) {
71                 log_notice("Activating display %d %p...", j, iter);
72                 ret = uterm_display_activate(iter, NULL);
73                 if (ret)
74                         log_err("Cannot activate display %d: %d", j, ret);
75                 else
76                         log_notice("Successfully activated display %d", j);
77
78                 ret = uterm_display_set_dpms(iter, UTERM_DPMS_ON);
79                 if (ret)
80                         log_err("Cannot set DPMS to ON: %d", ret);
81
82                 ++j;
83         }
84
85         iter = uterm_video_get_displays(video);
86         for ( ; iter; iter = uterm_display_next(iter)) {
87                 if (uterm_display_get_state(iter) != UTERM_DISPLAY_ACTIVE)
88                         continue;
89
90                 ret = uterm_screen_new_single(&screen, iter);
91                 if (ret) {
92                         log_err("Cannot create temp-screen object: %d", ret);
93                         continue;
94                 }
95
96                 ret = uterm_screen_fill(screen, 0xff, 0xff, 0xff, 0, 0,
97                                         uterm_screen_width(screen),
98                                         uterm_screen_height(screen));
99                 if (ret) {
100                         log_err("cannot fill framebuffer");
101                         uterm_screen_unref(screen);
102                         continue;
103                 }
104
105                 ret = uterm_screen_swap(screen);
106                 if (ret) {
107                         log_err("Cannot swap screen: %d", ret);
108                         uterm_screen_unref(screen);
109                         continue;
110                 }
111
112                 log_notice("Successfully set screen on display %p", iter);
113                 uterm_screen_unref(screen);
114         }
115
116         log_notice("Waiting 5 seconds...");
117         ev_eloop_run(eloop, 5000);
118         log_notice("Exiting...");
119
120         return 0;
121 }
122
123 static int list_outputs(struct uterm_video *video)
124 {
125         struct uterm_display *iter;
126         struct uterm_mode *cur, *mode;
127         int i;
128
129         log_notice("List of Outputs:");
130
131         i = 0;
132         iter = uterm_video_get_displays(video);
133         for ( ; iter; iter = uterm_display_next(iter)) {
134                 cur = uterm_display_get_current(iter);
135
136                 log_notice("Output %d:", i++);
137                 log_notice("  active: %d", uterm_display_get_state(iter));
138                 log_notice("  has current: %s", cur ? "yes" : "no");
139
140                 mode = uterm_display_get_modes(iter);
141                 for ( ; mode; mode = uterm_mode_next(mode)) {
142                         log_notice("  Mode '%s':", uterm_mode_get_name(mode));
143                         log_notice("    x: %u", uterm_mode_get_width(mode));
144                         log_notice("    y: %u", uterm_mode_get_height(mode));
145                 }
146         }
147
148         log_notice("End of Output list");
149
150         return 0;
151 }
152
153 static void print_help()
154 {
155         /*
156          * Usage/Help information
157          * This should be scaled to a maximum of 80 characters per line:
158          *
159          * 80 char line:
160          *       |   10   |    20   |    30   |    40   |    50   |    60   |    70   |    80   |
161          *      "12345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
162          * 80 char line starting with tab:
163          *       |10|    20   |    30   |    40   |    50   |    60   |    70   |    80   |
164          *      "\t901234567890123456789012345678901234567890123456789012345678901234567890\n"
165          */
166         fprintf(stderr,
167                 "Usage:\n"
168                 "\t%1$s [options]\n"
169                 "\t%1$s -h [options]\n"
170                 "\n"
171                 "You can prefix boolean options with \"no-\" to negate it. If an argument is\n"
172                 "given multiple times, only the last argument matters if not otherwise stated.\n"
173                 "\n"
174                 "General Options:\n"
175                 TEST_HELP
176                 "\n"
177                 "Video Options:\n"
178                 "\t    --fbdev                 [off]   Use fbdev instead of DRM\n"
179                 "\t    --test                  [off]   Try displaying content instead of listing devices\n",
180                 "test_input");
181         /*
182          * 80 char line:
183          *       |   10   |    20   |    30   |    40   |    50   |    60   |    70   |    80   |
184          *      "12345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
185          * 80 char line starting with tab:
186          *       |10|    20   |    30   |    40   |    50   |    60   |    70   |    80   |
187          *      "\t901234567890123456789012345678901234567890123456789012345678901234567890\n"
188          */
189 }
190
191 struct conf_option options[] = {
192         TEST_OPTIONS,
193         CONF_OPTION_BOOL(0, "fbdev", NULL, &output_conf.fbdev, false),
194         CONF_OPTION_BOOL(0, "test", NULL, &output_conf.test, false),
195 };
196
197 int main(int argc, char **argv)
198 {
199         struct uterm_video *video;
200         int ret;
201         unsigned int mode;
202         const char *node;
203         size_t onum;
204
205         onum = sizeof(options) / sizeof(*options);
206         ret = test_prepare(options, onum, argc, argv, &eloop);
207         if (ret)
208                 goto err_fail;
209
210         if (output_conf.fbdev) {
211                 mode = UTERM_VIDEO_FBDEV;
212                 node = "/dev/fb0";
213         } else {
214                 mode = UTERM_VIDEO_DRM;
215                 node = "/dev/dri/card0";
216         }
217
218         log_notice("Creating video object using %s...", node);
219
220         ret = uterm_video_new(&video, eloop, mode, node);
221         if (ret) {
222                 if (mode == UTERM_VIDEO_DRM) {
223                         log_notice("cannot create drm device; trying dumb drm mode");
224                         ret = uterm_video_new(&video, eloop,
225                                               UTERM_VIDEO_DUMB, node);
226                         if (ret)
227                                 goto err_exit;
228                 } else {
229                         goto err_exit;
230                 }
231         }
232
233         log_notice("Wakeing up video object...");
234         ret = uterm_video_wake_up(video);
235         if (ret < 0)
236                 goto err_unref;
237
238         if (!output_conf.test) {
239                 ret = list_outputs(video);
240                 if (ret) {
241                         log_err("Cannot list outputs: %d", ret);
242                         goto err_unref;
243                 }
244         } else {
245                 ret = blit_outputs(video);
246                 if (ret) {
247                         log_err("Cannot set outputs: %d", ret);
248                         goto err_unref;
249                 }
250         }
251
252 err_unref:
253         uterm_video_unref(video);
254 err_exit:
255         test_exit(options, onum, eloop);
256 err_fail:
257         if (ret != -ECANCELED)
258                 test_fail(ret);
259         return abs(ret);
260 }