eloop: move prefix to "ev_" instead of "kmscon_"
[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  * The application terminates automatically after 5 seconds, however, you need
34  * to switch VT to re-enable output on your screen. This application does not
35  * reset the screen automatically, yet.
36  *
37  * This lists all outputs:
38  * $ ./test_output
39  *
40  * This would show a test screen on output 0 and 4:
41  * $ ./test_output 0 4
42  * The test screen is a colored quad with 4 different colors in each corner.
43  */
44
45 #include <errno.h>
46 #include <inttypes.h>
47 #include <signal.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52
53 #include "log.h"
54 #include "output.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 sig)
66 {
67 }
68
69 static int set_outputs(struct kmscon_compositor *comp, int num, char **list)
70 {
71         struct kmscon_output *iter;
72         int i, j, val, ret;
73         struct kmscon_context *ctx;
74
75         ctx = kmscon_compositor_get_context(comp);
76         if (!ctx)
77                 return -EINVAL;
78
79         j = 0;
80         iter = kmscon_compositor_get_outputs(comp);
81         for ( ; iter; iter = kmscon_output_next(iter)) {
82                 for (i = 0; i < num; ++i) {
83                         val = atoi(list[i]);
84                         if (val == j)
85                                 break;
86                 }
87
88                 if (i == num) {
89                         log_info("Ignoring output %d\n", j);
90                 } else {
91                         log_info("Activating output %d %p...\n", j, iter);
92                         ret = kmscon_output_activate(iter, NULL);
93                         if (ret)
94                                 log_err("Cannot activate output %d: %d\n", j,
95                                                                         ret);
96                         else
97                                 log_info("Successfully activated output %d\n",
98                                                                         j);
99                 }
100
101                 ++j;
102         }
103
104         iter = kmscon_compositor_get_outputs(comp);
105         for ( ; iter; iter = kmscon_output_next(iter)) {
106                 if (!kmscon_output_is_active(iter))
107                         continue;
108
109                 ret = kmscon_output_use(iter);
110                 if (ret) {
111                         log_err("Cannot use output %p: %d\n", iter, ret);
112                         continue;
113                 }
114
115                 kmscon_context_clear(ctx);
116                 kmscon_context_draw_def(ctx, d_vert, d_col, 6);
117
118                 ret = kmscon_output_swap(iter);
119                 if (ret) {
120                         log_err("Cannot swap buffers of output %p: %d\n",
121                                                                 iter, ret);
122                         continue;
123                 }
124
125                 log_info("Successfully set screen on output %p\n", iter);
126         }
127
128         log_info("Waiting 5 seconds...\n");
129         sleep(5);
130         log_info("Exiting...\n");
131
132         return 0;
133 }
134
135 static int list_outputs(struct kmscon_compositor *comp)
136 {
137         struct kmscon_output *iter;
138         struct kmscon_mode *cur, *mode;
139         int i;
140
141         log_info("List of Outputs:\n");
142
143         i = 0;
144         iter = kmscon_compositor_get_outputs(comp);
145         for ( ; iter; iter = kmscon_output_next(iter)) {
146                 cur = kmscon_output_get_current(iter);
147
148                 log_info("Output %d:\n", i++);
149                 log_info("  active: %d\n", kmscon_output_is_active(iter));
150                 log_info("  has current: %s\n", cur ? "yes" : "no");
151
152                 mode = kmscon_output_get_modes(iter);
153                 for ( ; mode; mode = kmscon_mode_next(mode)) {
154                         log_info("  Mode '%s':\n", kmscon_mode_get_name(mode));
155                         log_info("    x: %u\n", kmscon_mode_get_width(mode));
156                         log_info("    y: %u\n", kmscon_mode_get_height(mode));
157                 }
158         }
159
160         log_info("End of Output list\n");
161
162         return 0;
163 }
164
165 int main(int argc, char **argv)
166 {
167         struct kmscon_compositor *comp;
168         int ret;
169         struct sigaction sig;
170
171         memset(&sig, 0, sizeof(sig));
172         sig.sa_handler = sig_term;
173         sigaction(SIGTERM, &sig, NULL);
174         sigaction(SIGINT, &sig, NULL);
175
176         log_info("Creating compositor...\n");
177         ret = kmscon_compositor_new(&comp);
178         if (ret) {
179                 log_err("Cannot create compositor: %d\n", ret);
180                 return abs(ret);
181         }
182
183         ret = kmscon_compositor_use(comp);
184         if (ret) {
185                 log_err("Cannot use compositor: %d\n", ret);
186                 goto err_unref;
187         }
188
189         log_info("Wakeing up compositor...\n");
190         ret = kmscon_compositor_wake_up(comp);
191         if (ret < 0) {
192                 log_err("Cannot wakeup compositor: %d\n", ret);
193                 goto err_unref;
194         }
195
196         if (argc < 2) {
197                 ret = list_outputs(comp);
198                 if (ret) {
199                         log_err("Cannot list outputs: %d\n", ret);
200                         goto err_unref;
201                 }
202         } else {
203                 ret = set_outputs(comp, argc - 1, &argv[1]);
204                 if (ret) {
205                         log_err("Cannot set outputs: %d\n", ret);
206                         goto err_unref;
207                 }
208         }
209
210 err_unref:
211         kmscon_compositor_unref(comp);
212         return abs(ret);
213 }