-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.c
208 lines (175 loc) · 5.69 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <SDL.h>
#include <lvgl.h>
#include "app.h"
#include "config.h"
#include "ui/app_ui.h"
#include "lvgl/display.h"
#include "lvgl/ext/lv_dir_focus.h"
#include "ss4s.h"
#include "backend/host_manager.h"
#include "backend/stream_manager.h"
#include "backend/input_manager.h"
#include "logging.h"
#include "logging_ext_ss4s.h"
#include "logging_ext_sdl.h"
#include "logging_ext_lvgl.h"
#include "os_info.h"
#if IHSPLAY_FEATURE_LIBCEC
#include "cec_sdl.h"
#endif
static void process_events();
static void logging_init();
static app_t *app = NULL;
static bool use_windowed();
int main(int argc, char *argv[]) {
logging_init();
app_preinit(argc, argv);
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC);
os_info_t os_info;
if (os_info_get(&os_info) == 0) {
char *str = os_info_str(&os_info);
commons_log_info("APP", "System: %s", str);
free(str);
}
app_settings_t settings;
app_settings_init(&settings, &os_info);
SS4S_Config ss4s_config = {
.audioDriver = settings.audio_driver,
.videoDriver = settings.video_driver,
.loggingFunction = commons_ss4s_logf,
};
SS4S_Init(argc, argv, &ss4s_config);
IHS_Init();
SDL_RegisterEvents((APP_EVENT_LAST - APP_EVENT_BEGIN) + 1);
lv_init();
lv_dir_focus_register();
int w = 1920, h = 1080;
SDL_DisplayMode mode;
SDL_GetDisplayMode(0, 0, &mode);
/* Get display size. Fallback to 1920x1080 if failed. */
if (mode.w > 0 && mode.h > 0) {
w = mode.w;
h = mode.h;
}
Uint32 fullscreen_flag;
#ifdef TARGET_WEBOS
fullscreen_flag = SDL_WINDOW_FULLSCREEN;
#elif IHSPLAY_FEATURE_FORCE_FULLSCREEN
fullscreen_flag = SDL_WINDOW_FULLSCREEN_DESKTOP;
#else
bool windowed = use_windowed();
if (windowed) {
w = 1920;
h = 1080;
fullscreen_flag = SDL_WINDOW_RESIZABLE;
} else {
fullscreen_flag = SDL_WINDOW_FULLSCREEN_DESKTOP;
}
#endif
/* Caveat: Don't use SDL_WINDOW_FULLSCREEN_DESKTOP on webOS. On older platforms it's not supported. */
SDL_Window *window = SDL_CreateWindow("IHSplay", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h,
SDL_WINDOW_ALLOW_HIGHDPI | fullscreen_flag);
SS4S_PostInit(argc, argv);
lv_disp_t *disp = app_lv_disp_init(window);
lv_disp_set_default(disp);
app = app_create(&settings, disp);
app->os_info = os_info;
#if IHSPLAY_FEATURE_LIBCEC
cec_sdl_ctx_t cec;
cec_sdl_init(&cec, "IHSplay");
#endif
while (app->running) {
process_events();
uint32_t next_delay = lv_task_handler();
SDL_Delay(stream_manager_is_active(app->stream_manager) ? 1 : next_delay);
}
// Drain remaining events
process_events();
#if IHSPLAY_FEATURE_LIBCEC
cec_sdl_deinit(&cec);
#endif
app_destroy(app);
app_lv_disp_deinit(disp);
app_settings_deinit(&settings);
SDL_DestroyWindow(window);
IHS_Quit();
SS4S_Quit();
SDL_Quit();
commons_logging_deinit();
os_info_clear(&os_info);
return 0;
}
static void process_events() {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYUP:
case SDL_KEYDOWN:
case SDL_MOUSEMOTION:
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
case SDL_CONTROLLERAXISMOTION:
case SDL_CONTROLLERBUTTONDOWN:
case SDL_CONTROLLERBUTTONUP:
case SDL_CONTROLLERDEVICEADDED:
case SDL_CONTROLLERDEVICEREMOVED:
case SDL_FINGERDOWN:
case SDL_FINGERUP:
case SDL_FINGERMOTION: {
bool intercept_by_stream = stream_manager_intercept_event(app->stream_manager, &event);
if (!intercept_by_stream) {
app_sdl_input_event(app, &event);
}
stream_manager_handle_event(app->stream_manager, &event);
break;
}
case SDL_APP_WILLENTERBACKGROUND: {
#if IHSPLAY_FEATURE_FORCE_FULLSCREEN
stream_manager_stop_active(app->stream_manager);
#endif
break;
}
case SDL_APP_DIDENTERFOREGROUND: {
lv_obj_invalidate(lv_scr_act());
break;
}
case SDL_QUIT: {
app_quit(app);
break;
}
case APP_RUN_ON_MAIN: {
void (*action)(app_t *, void *) = event.user.data1;
void *data = event.user.data2;
action(app, data);
break;
}
default: {
if (event.type > APP_UI_EVENT_BEGIN && event.type < APP_UI_EVENT_LAST) {
app_ui_event_data_t data = {.data1 = event.user.data1, .data2 = event.user.data2};
if (!app_ui_dispatch_event(app->ui, event.type, &data)) {
commons_log_debug("UI", "Unhandled UI event 0x%x", event.type);
}
}
break;
}
}
}
}
static void logging_init() {
commons_logging_init("ihsplay");
lv_log_register_print_cb(commons_lv_log);
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE);
SDL_LogSetOutputFunction(commons_sdl_log, NULL);
}
static bool use_windowed() {
const char *v = SDL_getenv("IHSPLAY_WINDOWED");
if (v == NULL) {
return false;
}
return strcmp(v, "1") == 0 || strcmp(v, "true") == 0;
}
void app_ihs_log(IHS_LogLevel level, const char *tag, const char *message) {
char app_tag[32] = "IHS.";
strncpy(app_tag + 4, tag, 28);
commons_log_printf((commons_log_level) level, app_tag, "%s", message);
}