test_input: always print keysym
[platform/upstream/kmscon.git] / tests / test_output.c
index 7653f55..d3da2b7 100644 (file)
  * Test KMS/DRI output subsystem
  * This is an example how to use the output subsystem. Invoked without
  * arguments it prints a list of all connected outputs and their modes.
- * If you pass numbers as arguments, it will enable these outputs and show an
- * image on the given monitors for 5 seconds.
- * The application terminates automatically after 5 seconds, however, you need
- * to switch VT to re-enable output on your screen. This application does not
- * reset the screen automatically, yet.
+ * If you pass any argument it will enable all outputs for 5seconds.
  *
  * This lists all outputs:
  * $ ./test_output
  *
- * This would show a test screen on output 0 and 4:
- * $ ./test_output 0 4
- * The test screen is a white background with two gray triangles in the top-left
- * and lower-right corner.
+ * This would show a test screen:
+ * $ ./test_output something
+ * The test screen is a colored quad with 4 different colors in each corner.
  */
 
-#define GL_GLEXT_PROTOTYPES
-
+#include <errno.h>
+#include <GL/gl.h>
 #include <inttypes.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 
-#include <GL/gl.h>
-#include <GL/glext.h>
-#include "output.h"
-
-static int set_outputs(struct kmscon_compositor *comp, int num, char **list)
+#include "eloop.h"
+#include "gl.h"
+#include "log.h"
+#include "uterm.h"
+#include "test_include.h"
+
+/* eloop object */
+static struct ev_eloop *eloop;
+
+/* a colored quad */
+float d_vert[] = { -1, -1, 1, -1, -1, 1, 1, -1, 1, 1, -1, 1 };
+float d_col[] = { 1, 1, 0, 1,
+               1, 1, 1, 1,
+               0, 1, 1, 1,
+               1, 1, 1, 1,
+               0, 0, 1, 1,
+               0, 1, 1, 1 };
+
+static int set_outputs(struct uterm_video *video)
 {
-       struct kmscon_output *iter;
-       int i, j, val, ret;
+       struct uterm_display *iter;
+       int j, ret;
+       struct gl_shader *shader;
+       struct uterm_screen *screen;
 
-       j = 0;
-       iter = kmscon_compositor_get_outputs(comp);
-       for ( ; iter; iter = kmscon_output_next(iter)) {
-               for (i = 0; i < num; ++i) {
-                       val = atoi(list[i]);
-                       if (val == j)
-                               break;
-               }
+       ret = gl_shader_new(&shader);
+       if (ret) {
+               log_err("Cannot create shader: %d", ret);
+               return ret;
+       }
 
-               if (i == num) {
-                       printf("Ignoring output %d\n", j);
-               } else {
-                       printf("Activating output %d %p...\n", j, iter);
-                       ret = kmscon_output_activate(iter, NULL);
-                       if (ret)
-                               printf("Cannot activate output %d: %d\n", j,
-                                                                       ret);
-                       else
-                               printf("Successfully activated output %d\n",
-                                                                       j);
-               }
+       j = 0;
+       iter = uterm_video_get_displays(video);
+       for ( ; iter; iter = uterm_display_next(iter)) {
+               log_notice("Activating display %d %p...", j, iter);
+               ret = uterm_display_activate(iter, NULL);
+               if (ret)
+                       log_err("Cannot activate display %d: %d", j, ret);
+               else
+                       log_notice("Successfully activated display %d", j);
+
+               ret = uterm_display_set_dpms(iter, UTERM_DPMS_ON);
+               if (ret)
+                       log_err("Cannot set DPMS to ON: %d", ret);
 
                ++j;
        }
 
-       iter = kmscon_compositor_get_outputs(comp);
-       for ( ; iter; iter = kmscon_output_next(iter)) {
-               if (!kmscon_output_is_active(iter))
+       iter = uterm_video_get_displays(video);
+       for ( ; iter; iter = uterm_display_next(iter)) {
+               if (uterm_display_get_state(iter) != UTERM_DISPLAY_ACTIVE)
                        continue;
 
-               ret = kmscon_output_use(iter);
+               ret = uterm_screen_new_single(&screen, iter);
                if (ret) {
-                       printf("Cannot use output %p: %d\n", iter, ret);
+                       log_err("Cannot create temp-screen object: %d", ret);
                        continue;
                }
 
-               glClearColor(1.0, 1.0, 1.0, 1.0);
+               ret = uterm_screen_use(screen);
+               if (ret) {
+                       log_err("Cannot use screen: %d", ret);
+                       uterm_screen_unref(screen);
+                       continue;
+               }
+
+               glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
                glClear(GL_COLOR_BUFFER_BIT);
+               glViewport(0, 0,
+                               uterm_screen_width(screen),
+                               uterm_screen_height(screen));
 
-               glBegin(GL_TRIANGLES);
-               glColor4f(0.5, 0.5, 0.5, 1.0);
-               glVertex3f(1.0, 1.0, 0.0f);
-               glVertex3f(0, 0, 0.0f);
-               glVertex3f(1.0, 0, 0.0f);
-               glVertex3f(-1.0, -1.0, 0.0f);
-               glVertex3f(0, 0, 0.0f);
-               glVertex3f(-1.0, 0, 0.0f);
-               glEnd();
-
-               ret = kmscon_output_swap(iter);
+               gl_shader_draw_def(shader, d_vert, d_col, 6);
+               if (gl_has_error())
+                       log_err("GL error occurred");
+
+               ret = uterm_screen_swap(screen);
                if (ret) {
-                       printf("Cannot swap buffers of output %p: %d\n",
-                                                               iter, ret);
+                       log_err("Cannot swap screen: %d", ret);
+                       uterm_screen_unref(screen);
                        continue;
                }
 
-               printf("Successfully set screen on output %p\n", iter);
+               log_notice("Successfully set screen on display %p", iter);
+               uterm_screen_unref(screen);
        }
 
-       printf("Waiting 5 seconds...\n");
-       sleep(5);
-       printf("Exiting...\n");
+       log_notice("Waiting 5 seconds...");
+       ev_eloop_run(eloop, 5000);
+       log_notice("Exiting...");
+
+       gl_shader_unref(shader);
 
        return 0;
 }
 
-static int list_outputs(struct kmscon_compositor *comp)
+static int list_outputs(struct uterm_video *video)
 {
-       struct kmscon_output *iter;
-       struct kmscon_mode *cur, *mode;
+       struct uterm_display *iter;
+       struct uterm_mode *cur, *mode;
        int i;
 
-       printf("List of Outputs:\n");
+       log_notice("List of Outputs:");
 
        i = 0;
-       iter = kmscon_compositor_get_outputs(comp);
-       for ( ; iter; iter = kmscon_output_next(iter)) {
-               cur = kmscon_output_get_current(iter);
-       
-               printf("Output %d:\n", i++);
-               printf("  active: %d\n", kmscon_output_is_active(iter));
-               printf("  has current: %s\n", cur ? "yes" : "no");
-
-               mode = kmscon_output_get_modes(iter);
-               for ( ; mode; mode = kmscon_mode_next(mode)) {
-                       printf("  Mode '%s':\n", kmscon_mode_get_name(mode));
-                       printf("    x: %u\n", kmscon_mode_get_width(mode));
-                       printf("    y: %u\n", kmscon_mode_get_height(mode));
+       iter = uterm_video_get_displays(video);
+       for ( ; iter; iter = uterm_display_next(iter)) {
+               cur = uterm_display_get_current(iter);
+
+               log_notice("Output %d:", i++);
+               log_notice("  active: %d", uterm_display_get_state(iter));
+               log_notice("  has current: %s", cur ? "yes" : "no");
+
+               mode = uterm_display_get_modes(iter);
+               for ( ; mode; mode = uterm_mode_next(mode)) {
+                       log_notice("  Mode '%s':", uterm_mode_get_name(mode));
+                       log_notice("    x: %u", uterm_mode_get_width(mode));
+                       log_notice("    y: %u", uterm_mode_get_height(mode));
                }
        }
 
-       printf("End of Output list\n");
+       log_notice("End of Output list");
 
        return 0;
 }
 
 int main(int argc, char **argv)
 {
-       struct kmscon_compositor *comp;
+       struct uterm_video *video;
        int ret;
 
-       printf("Creating compositor...\n");
-       ret = kmscon_compositor_new(&comp);
-       if (ret) {
-               printf("Cannot create compositor: %d\n", ret);
-               return abs(ret);
-       }
+       ret = test_prepare(argc, argv, &eloop);
+       if (ret)
+               goto err_fail;
+
+       log_notice("Creating video object...");
+       ret = uterm_video_new(&video, eloop, UTERM_VIDEO_DRM, "/dev/dri/card0");
+       if (ret)
+               goto err_exit;
 
-       printf("Wakeing up compositor...\n");
-       ret = kmscon_compositor_wake_up(comp);
-       if (ret < 0) {
-               printf("Cannot wakeup compositor: %d\n", ret);
+       ret = uterm_video_use(video);
+       if (ret)
                goto err_unref;
-       }
 
-       kmscon_compositor_use(comp);
+       log_notice("Wakeing up video object...");
+       ret = uterm_video_wake_up(video);
+       if (ret < 0)
+               goto err_unref;
 
        if (argc < 2) {
-               ret = list_outputs(comp);
+               ret = list_outputs(video);
                if (ret) {
-                       printf("Cannot list outputs: %d\n", ret);
+                       log_err("Cannot list outputs: %d", ret);
                        goto err_unref;
                }
        } else {
-               ret = set_outputs(comp, argc - 1, &argv[1]);
+               ret = set_outputs(video);
                if (ret) {
-                       printf("Cannot set outputs: %d\n", ret);
+                       log_err("Cannot set outputs: %d", ret);
                        goto err_unref;
                }
        }
 
 err_unref:
-       kmscon_compositor_unref(comp);
+       uterm_video_unref(video);
+err_exit:
+       test_exit(eloop);
+err_fail:
+       test_fail(ret);
        return abs(ret);
 }