Initial version of headless-server
[platform/core/uifw/headless-server.git] / src / headless_server.c
1 /*
2  * Copyright © 2019 Samsung Electronics co., Ltd. All Rights Reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <signal.h>
29
30 #include <pepper.h>
31 #include <headless_server.h>
32
33 static int
34 handle_sigint(int signal_number, void *data)
35 {
36         struct wl_display *display = (struct wl_display *)data;
37         wl_display_terminate(display);
38
39         return 0;
40 }
41
42 static pepper_bool_t
43 init_signal(pepper_compositor_t *compositor)
44 {
45         struct wl_display *display;
46         struct wl_event_loop *loop;
47         struct wl_event_source *sigint;
48
49         display = pepper_compositor_get_display(compositor);
50         loop = wl_display_get_event_loop(display);
51         sigint = wl_event_loop_add_signal(loop, SIGINT, handle_sigint, display);
52         if (!sigint)
53                 return PEPPER_FALSE;
54
55         return PEPPER_TRUE;
56 }
57
58 int main(int argc, char *argv[])
59 {
60         const char *socket_name = NULL;
61         pepper_compositor_t *compositor = NULL;
62         pepper_bool_t ret;
63
64         /* set STDOUT/STDERR bufferless */
65         setvbuf(stdout, NULL, _IONBF, 0);
66         setvbuf(stderr, NULL, _IONBF, 0);
67
68         if (getenv("PEPPER_DLOG_ENABLE")) {
69                 PEPPER_TRACE("pepper log will be written to dlog !\n");
70                 pepper_log_dlog_enable(1);
71         }
72
73         socket_name = getenv("WAYLAND_DISPLAY");
74
75         if (!socket_name)
76                 socket_name = "wayland-0";
77
78         if (!getenv("XDG_RUNTIME_DIR"))
79                 setenv("XDG_RUNTIME_DIR", "/run", 1);
80
81         /* create pepper compositir */
82         compositor = pepper_compositor_create(socket_name);
83         PEPPER_CHECK(compositor, return EXIT_FAILURE, "Failed to create compositor !");
84
85         /* Init event trace */
86         ret = headless_debug_init(compositor);
87         PEPPER_CHECK(ret, goto end, "headless_debug_init() failed\n");
88
89         /* Init input for headless */
90         ret = headless_input_init(compositor);
91         PEPPER_CHECK(ret, goto end, "headless_input_init() failed\n");
92
93         /* Init Output */
94         ret = headless_output_init(compositor);
95         PEPPER_CHECK(ret, goto end, "headless_output_init() failed.\n");
96
97         /* Init Shell */
98         ret = headless_shell_init(compositor);
99         PEPPER_CHECK(ret, goto end, "headless_shell_init() failed.\n");
100
101         /* Init Signal for SIGINT */
102         init_signal(compositor);
103
104         /* run event loop */
105         wl_display_run(pepper_compositor_get_display(compositor));
106
107 end:
108         /* Deinit Process */
109         headless_shell_deinit(compositor);
110         headless_input_deinit(compositor);
111         headless_output_deinit(compositor);
112         headless_debug_deinit(compositor);
113         pepper_compositor_destroy(compositor);
114
115         return EXIT_SUCCESS;
116 }