donot need to check num_outputs
[platform/core/uifw/pepper.git] / samples / tdm-backend.c
1 /*
2 * Copyright © 2008-2012 Kristian Høgsberg
3 * Copyright © 2010-2012 Intel Corporation
4 * Copyright © 2011 Benjamin Franzke
5 * Copyright © 2012 Collabora, Ltd.
6 * Copyright © 2015 S-Core Corporation
7 * Copyright © 2015-2016 Samsung Electronics co., Ltd. All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
18 * Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <signal.h>
32
33 #include <libudev.h>
34
35 #include <config.h>
36 #include <pepper.h>
37 #include <pepper-libinput.h>
38 #include <pepper-tdm.h>
39 #include <pepper-desktop-shell.h>
40
41 static void
42 handle_signals(int s, siginfo_t *siginfo, void *context)
43 {
44         raise(SIGTRAP);
45 }
46
47 static void
48 init_signals()
49 {
50         struct sigaction action;
51
52         action.sa_flags = SA_SIGINFO | SA_RESETHAND;
53         action.sa_sigaction = handle_signals;
54         sigemptyset(&action.sa_mask);
55
56         sigaction(SIGSEGV, &action, NULL);
57         sigaction(SIGABRT, &action, NULL);
58 }
59
60 static int
61 handle_sigint(int signal_number, void *data)
62 {
63         struct wl_display *display = (struct wl_display *)data;
64         wl_display_terminate(display);
65
66         return 0;
67 }
68
69 int
70 main(int argc, char **argv)
71 {
72         pepper_compositor_t    *compositor = NULL;
73         pepper_tdm_t           *tdm = NULL;
74         pepper_tdm_output_t    *tdm_output = NULL;
75         pepper_tdm_renderer_t  *tdm_renderer[3]; // assume that maximum outputs is 3
76         pepper_libinput_t      *input = NULL;
77
78         struct udev            *udev = NULL;
79
80         struct wl_display      *display = NULL;
81         struct wl_event_loop   *loop = NULL;
82         struct wl_event_source *sigint = NULL;
83
84         int i;
85         uint32_t     num_outputs = 0;
86         const char  *render_env = getenv("PEPPER_TDM_RENDERER");
87
88         init_signals();
89
90         compositor = pepper_compositor_create("wayland-0", NULL);
91         if (!compositor)
92                 goto cleanup;
93
94         udev = udev_new();
95         if (!udev)
96                 goto cleanup;
97
98         input = pepper_libinput_create(compositor, udev);
99         if (!input)
100                 goto cleanup;
101
102         tdm = pepper_tdm_create(compositor);
103         if (!tdm)
104                 goto cleanup;
105
106         num_outputs = pepper_tdm_count_outputs(tdm);
107
108         for (i = 0; i < num_outputs; i++) {
109                 tdm_output = pepper_tdm_get_output(tdm, i);
110                 /*Setup renderer*/
111                 if (render_env && !strcmp(render_env, "gl"))
112                         tdm_renderer[i] = pepper_tdm_output_create_gl_renderer(tdm_output);
113                 else
114                         tdm_renderer[i] = pepper_tdm_output_create_pixman_renderer(tdm_output);
115
116                 if (!tdm_renderer[i])
117                         goto cleanup;
118         }
119
120         if (!pepper_desktop_shell_init(compositor))
121                 goto cleanup;
122
123         display = pepper_compositor_get_display(compositor);
124         if (!display)
125                 goto cleanup;
126
127         loop = wl_display_get_event_loop(display);
128         sigint = wl_event_loop_add_signal(loop, SIGINT, handle_sigint, display);
129         if (!sigint)
130                 goto cleanup;
131
132         wl_display_run(display);
133
134 cleanup:
135
136         if (sigint)
137                 wl_event_source_remove(sigint);
138
139         for (i = 0; i < num_outputs; i++) {
140                 if (tdm_renderer[i])
141                         pepper_tdm_renderer_destroy(tdm_renderer[i]);
142         }
143
144         if (tdm)
145                 pepper_tdm_destroy(tdm);
146
147         if (input)
148                 pepper_libinput_destroy(input);
149
150         if (udev)
151                 udev_unref(udev);
152
153         if (compositor)
154                 pepper_compositor_destroy(compositor);
155
156         return 0;
157 }