test_input: always print keysym
[platform/upstream/kmscon.git] / tests / test_output.c
1 /*
2  * test_output - Test KMS/DRI output
3  *
4  * Copyright (c) 2011 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  * The test screen is a colored quad with 4 different colors in each corner.
39  */
40
41 #include <errno.h>
42 #include <GL/gl.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 "gl.h"
51 #include "log.h"
52 #include "uterm.h"
53 #include "test_include.h"
54
55 /* eloop object */
56 static struct ev_eloop *eloop;
57
58 /* a colored quad */
59 float d_vert[] = { -1, -1, 1, -1, -1, 1, 1, -1, 1, 1, -1, 1 };
60 float d_col[] = { 1, 1, 0, 1,
61                 1, 1, 1, 1,
62                 0, 1, 1, 1,
63                 1, 1, 1, 1,
64                 0, 0, 1, 1,
65                 0, 1, 1, 1 };
66
67 static int set_outputs(struct uterm_video *video)
68 {
69         struct uterm_display *iter;
70         int j, ret;
71         struct gl_shader *shader;
72         struct uterm_screen *screen;
73
74         ret = gl_shader_new(&shader);
75         if (ret) {
76                 log_err("Cannot create shader: %d", ret);
77                 return ret;
78         }
79
80         j = 0;
81         iter = uterm_video_get_displays(video);
82         for ( ; iter; iter = uterm_display_next(iter)) {
83                 log_notice("Activating display %d %p...", j, iter);
84                 ret = uterm_display_activate(iter, NULL);
85                 if (ret)
86                         log_err("Cannot activate display %d: %d", j, ret);
87                 else
88                         log_notice("Successfully activated display %d", j);
89
90                 ret = uterm_display_set_dpms(iter, UTERM_DPMS_ON);
91                 if (ret)
92                         log_err("Cannot set DPMS to ON: %d", ret);
93
94                 ++j;
95         }
96
97         iter = uterm_video_get_displays(video);
98         for ( ; iter; iter = uterm_display_next(iter)) {
99                 if (uterm_display_get_state(iter) != UTERM_DISPLAY_ACTIVE)
100                         continue;
101
102                 ret = uterm_screen_new_single(&screen, iter);
103                 if (ret) {
104                         log_err("Cannot create temp-screen object: %d", ret);
105                         continue;
106                 }
107
108                 ret = uterm_screen_use(screen);
109                 if (ret) {
110                         log_err("Cannot use screen: %d", ret);
111                         uterm_screen_unref(screen);
112                         continue;
113                 }
114
115                 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
116                 glClear(GL_COLOR_BUFFER_BIT);
117                 glViewport(0, 0,
118                                 uterm_screen_width(screen),
119                                 uterm_screen_height(screen));
120
121                 gl_shader_draw_def(shader, d_vert, d_col, 6);
122                 if (gl_has_error())
123                         log_err("GL error occurred");
124
125                 ret = uterm_screen_swap(screen);
126                 if (ret) {
127                         log_err("Cannot swap screen: %d", ret);
128                         uterm_screen_unref(screen);
129                         continue;
130                 }
131
132                 log_notice("Successfully set screen on display %p", iter);
133                 uterm_screen_unref(screen);
134         }
135
136         log_notice("Waiting 5 seconds...");
137         ev_eloop_run(eloop, 5000);
138         log_notice("Exiting...");
139
140         gl_shader_unref(shader);
141
142         return 0;
143 }
144
145 static int list_outputs(struct uterm_video *video)
146 {
147         struct uterm_display *iter;
148         struct uterm_mode *cur, *mode;
149         int i;
150
151         log_notice("List of Outputs:");
152
153         i = 0;
154         iter = uterm_video_get_displays(video);
155         for ( ; iter; iter = uterm_display_next(iter)) {
156                 cur = uterm_display_get_current(iter);
157
158                 log_notice("Output %d:", i++);
159                 log_notice("  active: %d", uterm_display_get_state(iter));
160                 log_notice("  has current: %s", cur ? "yes" : "no");
161
162                 mode = uterm_display_get_modes(iter);
163                 for ( ; mode; mode = uterm_mode_next(mode)) {
164                         log_notice("  Mode '%s':", uterm_mode_get_name(mode));
165                         log_notice("    x: %u", uterm_mode_get_width(mode));
166                         log_notice("    y: %u", uterm_mode_get_height(mode));
167                 }
168         }
169
170         log_notice("End of Output list");
171
172         return 0;
173 }
174
175 int main(int argc, char **argv)
176 {
177         struct uterm_video *video;
178         int ret;
179
180         ret = test_prepare(argc, argv, &eloop);
181         if (ret)
182                 goto err_fail;
183
184         log_notice("Creating video object...");
185         ret = uterm_video_new(&video, eloop, UTERM_VIDEO_DRM, "/dev/dri/card0");
186         if (ret)
187                 goto err_exit;
188
189         ret = uterm_video_use(video);
190         if (ret)
191                 goto err_unref;
192
193         log_notice("Wakeing up video object...");
194         ret = uterm_video_wake_up(video);
195         if (ret < 0)
196                 goto err_unref;
197
198         if (argc < 2) {
199                 ret = list_outputs(video);
200                 if (ret) {
201                         log_err("Cannot list outputs: %d", ret);
202                         goto err_unref;
203                 }
204         } else {
205                 ret = set_outputs(video);
206                 if (ret) {
207                         log_err("Cannot set outputs: %d", ret);
208                         goto err_unref;
209                 }
210         }
211
212 err_unref:
213         uterm_video_unref(video);
214 err_exit:
215         test_exit(eloop);
216 err_fail:
217         test_fail(ret);
218         return abs(ret);
219 }