unicode: use static global symbol table
[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 numbers as arguments, it will enable these outputs and show an
32  * image on the given monitors for 5 seconds.
33  *
34  * This lists all outputs:
35  * $ ./test_output
36  *
37  * This would show a test screen on output 0 and 4:
38  * $ ./test_output 0 4
39  * The test screen is a colored quad with 4 different colors in each corner.
40  */
41
42 #include <errno.h>
43 #include <GL/gl.h>
44 #include <inttypes.h>
45 #include <signal.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50
51 #include "eloop.h"
52 #include "gl.h"
53 #include "log.h"
54 #include "uterm.h"
55
56 /* a colored quad */
57 float d_vert[] = { -1, -1, 1, -1, -1, 1, 1, -1, 1, 1, -1, 1 };
58 float d_col[] = { 1, 1, 0, 1,
59                 1, 1, 1, 1,
60                 0, 1, 1, 1,
61                 1, 1, 1, 1,
62                 0, 0, 1, 1,
63                 0, 1, 1, 1 };
64
65 static void sig_term(int signum)
66 {
67 }
68
69 static int set_outputs(struct uterm_video *video, int num, char **list)
70 {
71         struct uterm_display *iter;
72         int i, j, val, ret;
73         struct gl_shader *shader;
74         struct uterm_screen *screen;
75
76         ret = gl_shader_new(&shader);
77         if (ret) {
78                 log_err("Cannot create shader: %d", ret);
79                 return ret;
80         }
81
82         j = 0;
83         iter = uterm_video_get_displays(video);
84         for ( ; iter; iter = uterm_display_next(iter)) {
85                 for (i = 0; i < num; ++i) {
86                         val = atoi(list[i]);
87                         if (val == j)
88                                 break;
89                 }
90
91                 if (i == num) {
92                         log_notice("Ignoring display %d", j);
93                 } else {
94                         log_notice("Activating display %d %p...", j, iter);
95                         ret = uterm_display_activate(iter, NULL);
96                         if (ret)
97                                 log_err("Cannot activate display %d: %d", j,
98                                                                         ret);
99                         else
100                                 log_notice("Successfully activated display %d",
101                                                 j);
102
103                         ret = uterm_display_set_dpms(iter, UTERM_DPMS_ON);
104                         if (ret)
105                                 log_err("Cannot set DPMS to ON: %d", ret);
106                 }
107
108                 ++j;
109         }
110
111         iter = uterm_video_get_displays(video);
112         for ( ; iter; iter = uterm_display_next(iter)) {
113                 if (uterm_display_get_state(iter) != UTERM_DISPLAY_ACTIVE)
114                         continue;
115
116                 ret = uterm_screen_new_single(&screen, iter);
117                 if (ret) {
118                         log_err("Cannot create temp-screen object: %d", ret);
119                         continue;
120                 }
121
122                 ret = uterm_screen_use(screen);
123                 if (ret) {
124                         log_err("Cannot use screen: %d", ret);
125                         uterm_screen_unref(screen);
126                         continue;
127                 }
128
129                 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
130                 glClear(GL_COLOR_BUFFER_BIT);
131                 glViewport(0, 0,
132                                 uterm_screen_width(screen),
133                                 uterm_screen_height(screen));
134
135                 gl_shader_draw_def(shader, d_vert, d_col, 6);
136                 if (gl_has_error())
137                         log_err("GL error occurred");
138
139                 ret = uterm_screen_swap(screen);
140                 if (ret) {
141                         log_err("Cannot swap screen: %d", ret);
142                         uterm_screen_unref(screen);
143                         continue;
144                 }
145
146                 log_notice("Successfully set screen on display %p", iter);
147                 uterm_screen_unref(screen);
148         }
149
150         log_notice("Waiting 5 seconds...");
151         sleep(5);
152         log_notice("Exiting...");
153
154         gl_shader_unref(shader);
155
156         return 0;
157 }
158
159 static int list_outputs(struct uterm_video *video)
160 {
161         struct uterm_display *iter;
162         struct uterm_mode *cur, *mode;
163         int i;
164
165         log_notice("List of Outputs:");
166
167         i = 0;
168         iter = uterm_video_get_displays(video);
169         for ( ; iter; iter = uterm_display_next(iter)) {
170                 cur = uterm_display_get_current(iter);
171
172                 log_notice("Output %d:", i++);
173                 log_notice("  active: %d", uterm_display_get_state(iter));
174                 log_notice("  has current: %s", cur ? "yes" : "no");
175
176                 mode = uterm_display_get_modes(iter);
177                 for ( ; mode; mode = uterm_mode_next(mode)) {
178                         log_notice("  Mode '%s':", uterm_mode_get_name(mode));
179                         log_notice("    x: %u", uterm_mode_get_width(mode));
180                         log_notice("    y: %u", uterm_mode_get_height(mode));
181                 }
182         }
183
184         log_notice("End of Output list");
185
186         return 0;
187 }
188
189 int main(int argc, char **argv)
190 {
191         struct uterm_video *video;
192         int ret;
193         struct ev_eloop *eloop;
194         struct sigaction sig;
195
196         log_print_init("test_output");
197         log_set_config(&LOG_CONFIG_INFO(1, 1));
198
199         ret = ev_eloop_new(&eloop);
200         if (ret)
201                 return EXIT_FAILURE;
202
203         memset(&sig, 0, sizeof(sig));
204         sig.sa_handler = sig_term;
205         sigaction(SIGTERM, &sig, NULL);
206         sigaction(SIGINT, &sig, NULL);
207
208         log_notice("Creating video object...");
209         ret = uterm_video_new(&video, UTERM_VIDEO_DRM, eloop);
210         if (ret) {
211                 log_err("Cannot create video object: %d", ret);
212                 return abs(ret);
213         }
214
215         log_notice("Wakeing up video object...");
216         ret = uterm_video_wake_up(video);
217         if (ret < 0) {
218                 log_err("Cannot wakeup video object: %d", ret);
219                 goto err_unref;
220         }
221
222         if (argc < 2) {
223                 ret = list_outputs(video);
224                 if (ret) {
225                         log_err("Cannot list outputs: %d", ret);
226                         goto err_unref;
227                 }
228         } else {
229                 ret = set_outputs(video, argc - 1, &argv[1]);
230                 if (ret) {
231                         log_err("Cannot set outputs: %d", ret);
232                         goto err_unref;
233                 }
234         }
235
236 err_unref:
237         uterm_video_unref(video);
238         return abs(ret);
239 }