92b836e071a9defd579d4452b45678d74952d0cc
[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 white background with two gray triangles in the top-left
43  * and lower-right corner.
44  */
45
46 #define GL_GLEXT_PROTOTYPES
47
48 #include <inttypes.h>
49 #include <signal.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54
55 #include <GL/gl.h>
56 #include <GL/glext.h>
57 #include "output.h"
58
59 static void sig_term(int sig)
60 {
61 }
62
63 static int set_outputs(struct kmscon_compositor *comp, int num, char **list)
64 {
65         struct kmscon_output *iter;
66         int i, j, val, ret;
67
68         j = 0;
69         iter = kmscon_compositor_get_outputs(comp);
70         for ( ; iter; iter = kmscon_output_next(iter)) {
71                 for (i = 0; i < num; ++i) {
72                         val = atoi(list[i]);
73                         if (val == j)
74                                 break;
75                 }
76
77                 if (i == num) {
78                         printf("Ignoring output %d\n", j);
79                 } else {
80                         printf("Activating output %d %p...\n", j, iter);
81                         ret = kmscon_output_activate(iter, NULL);
82                         if (ret)
83                                 printf("Cannot activate output %d: %d\n", j,
84                                                                         ret);
85                         else
86                                 printf("Successfully activated output %d\n",
87                                                                         j);
88                 }
89
90                 ++j;
91         }
92
93         iter = kmscon_compositor_get_outputs(comp);
94         for ( ; iter; iter = kmscon_output_next(iter)) {
95                 if (!kmscon_output_is_active(iter))
96                         continue;
97
98                 ret = kmscon_output_use(iter);
99                 if (ret) {
100                         printf("Cannot use output %p: %d\n", iter, ret);
101                         continue;
102                 }
103
104                 glClearColor(1.0, 1.0, 1.0, 1.0);
105                 glClear(GL_COLOR_BUFFER_BIT);
106
107                 glBegin(GL_TRIANGLES);
108                 glColor4f(0.5, 0.5, 0.5, 1.0);
109                 glVertex3f(1.0, 1.0, 0.0f);
110                 glVertex3f(0, 0, 0.0f);
111                 glVertex3f(1.0, 0, 0.0f);
112                 glVertex3f(-1.0, -1.0, 0.0f);
113                 glVertex3f(0, 0, 0.0f);
114                 glVertex3f(-1.0, 0, 0.0f);
115                 glEnd();
116
117                 ret = kmscon_output_swap(iter);
118                 if (ret) {
119                         printf("Cannot swap buffers of output %p: %d\n",
120                                                                 iter, ret);
121                         continue;
122                 }
123
124                 printf("Successfully set screen on output %p\n", iter);
125         }
126
127         printf("Waiting 5 seconds...\n");
128         sleep(5);
129         printf("Exiting...\n");
130
131         return 0;
132 }
133
134 static int list_outputs(struct kmscon_compositor *comp)
135 {
136         struct kmscon_output *iter;
137         struct kmscon_mode *cur, *mode;
138         int i;
139
140         printf("List of Outputs:\n");
141
142         i = 0;
143         iter = kmscon_compositor_get_outputs(comp);
144         for ( ; iter; iter = kmscon_output_next(iter)) {
145                 cur = kmscon_output_get_current(iter);
146         
147                 printf("Output %d:\n", i++);
148                 printf("  active: %d\n", kmscon_output_is_active(iter));
149                 printf("  has current: %s\n", cur ? "yes" : "no");
150
151                 mode = kmscon_output_get_modes(iter);
152                 for ( ; mode; mode = kmscon_mode_next(mode)) {
153                         printf("  Mode '%s':\n", kmscon_mode_get_name(mode));
154                         printf("    x: %u\n", kmscon_mode_get_width(mode));
155                         printf("    y: %u\n", kmscon_mode_get_height(mode));
156                 }
157         }
158
159         printf("End of Output list\n");
160
161         return 0;
162 }
163
164 int main(int argc, char **argv)
165 {
166         struct kmscon_compositor *comp;
167         int ret;
168         struct sigaction sig;
169
170         memset(&sig, 0, sizeof(sig));
171         sig.sa_handler = sig_term;
172         sigaction(SIGTERM, &sig, NULL);
173         sigaction(SIGINT, &sig, NULL);
174
175         printf("Creating compositor...\n");
176         ret = kmscon_compositor_new(&comp);
177         if (ret) {
178                 printf("Cannot create compositor: %d\n", ret);
179                 return abs(ret);
180         }
181
182         printf("Wakeing up compositor...\n");
183         ret = kmscon_compositor_wake_up(comp);
184         if (ret < 0) {
185                 printf("Cannot wakeup compositor: %d\n", ret);
186                 goto err_unref;
187         }
188
189         kmscon_compositor_use(comp);
190
191         if (argc < 2) {
192                 ret = list_outputs(comp);
193                 if (ret) {
194                         printf("Cannot list outputs: %d\n", ret);
195                         goto err_unref;
196                 }
197         } else {
198                 ret = set_outputs(comp, argc - 1, &argv[1]);
199                 if (ret) {
200                         printf("Cannot set outputs: %d\n", ret);
201                         goto err_unref;
202                 }
203         }
204
205 err_unref:
206         kmscon_compositor_unref(comp);
207         return abs(ret);
208 }