summaryrefslogtreecommitdiff
path: root/tizen/src/emulator.c
blob: 1e9a8dd743e162e0e36e2b0a5be778c76e77c6d5 (plain)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/*
 * Emulator
 *
 * Copyright (C) 2011 - 2014 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact:
 * SeokYeon Hwang <syeon.hwang@samsung.com>
 * MunKyu Im <munkyu.im@samsung.com>
 * GiWoong Kim <giwoong.kim@samsung.com>
 * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
 * HyunJun Son
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 *
 * Contributors:
 * - S-Core Co., Ltd
 *
 */

#include <stdlib.h>
#include <string.h>

#include "qemu/config-file.h"
#include "qemu/sockets.h"

#include "build_info.h"
#include "emulator.h"
#include "emul_state.h"
#include "guest_debug.h"
#include "guest_server.h"
#include "hw/maru_camera_common.h"
#include "hw/gloffscreen_test.h"
#include "maru_common.h"
#include "maru_err_table.h"
#include "maru_display.h"
#include "mloop_event.h"
#include "osutil.h"
#include "sdb.h"
#include "skin/maruskin_server.h"
#include "skin/maruskin_client.h"
#include "debug_ch.h"
#include "ecs/ecs.h"
#include "tethering/app_tethering.h"

#ifdef CONFIG_SDL
#include <SDL.h>
#endif
#if defined(CONFIG_LINUX) || defined(CONFIG_DARWIN)
#include <sys/ipc.h>
#include <sys/shm.h>
extern int g_shmid;
#endif

#ifdef CONFIG_DARWIN
#include "ns_event.h"
int thread_running = 1; /* Check if we need exit main */
#endif

MULTI_DEBUG_CHANNEL(qemu, main);

#define QEMU_ARGS_PREFIX "--qemu-args"
#define SKIN_ARGS_PREFIX "--skin-args"
#define IMAGE_PATH_PREFIX   "file="
//#define IMAGE_PATH_SUFFIX   ",if=virtio"
#define IMAGE_PATH_SUFFIX   ",if=virtio,index=1"
#define SDB_PORT_PREFIX     "sdb_port="
#define LOGS_SUFFIX         "/logs/"
#define LOGFILE             "emulator.log"
#define DISPLAY_WIDTH_PREFIX "width="
#define DISPLAY_HEIGHT_PREFIX "height="
#define INPUT_TOUCH_PARAMETER "virtio-touchscreen-pci"

#define MIDBUF  128
#define LEN_MARU_KERNEL_CMDLINE 512
gchar maru_kernel_cmdline[LEN_MARU_KERNEL_CMDLINE];

gchar bin_path[PATH_MAX] = { 0, };
gchar log_path[PATH_MAX] = { 0, };

char tizen_target_path[PATH_MAX];
char tizen_target_img_path[PATH_MAX];

int enable_gl = 0;
int enable_yagl = 0;
int enable_spice = 0;
int is_webcam_enabled;

static int _skin_argc;
static char **_skin_argv;
static int _qemu_argc;
static char **_qemu_argv;

const gchar *get_log_path(void)
{
    return log_path;
}

void exit_emulator(void)
{
    INFO("exit emulator!\n");

    mloop_ev_stop();
    shutdown_skin_server();
    shutdown_guest_server();
    stop_ecs();
    disconnect_tethering_app();

#if defined(CONFIG_LINUX) || defined(CONFIG_DARWIN)
    if (shmctl(g_shmid, IPC_RMID, 0) == -1) {
        ERR("shmctl failed\n");
        perror("emulator.c: ");
    }
#endif

    maru_display_fini();
}

static void construct_main_window(int skin_argc, char *skin_argv[],
                                int qemu_argc, char *qemu_argv[])
{
    INFO("construct main window\n");

    start_skin_server(skin_argc, skin_argv, qemu_argc, qemu_argv);

    /* the next line checks for debugging and etc.. */
    if (get_emul_skin_enable() == 1) {
        if (0 > start_skin_client(skin_argc, skin_argv)) {
            maru_register_exit_msg(MARU_EXIT_SKIN_SERVER_FAILED, NULL);
            exit(-1);
        }
    }

    set_emul_caps_lock_state(0);
    set_emul_num_lock_state(0);
}

static void parse_options(int argc, char *argv[], int *skin_argc,
                        char ***skin_argv, int *qemu_argc, char ***qemu_argv)
{
    int i = 0;
    int skin_args_index = 0;

    if (argc <= 1) {
        fprintf(stderr, "Arguments are not enough to launch Emulator. "
                "Please try to use Emulator Manager.\n");
        exit(1);
    }

    /* classification */
    for (i = 1; i < argc; ++i) {
        if (strstr(argv[i], SKIN_ARGS_PREFIX)) {
            *skin_argv = &(argv[i + 1]);
            break;
        }
    }

    for (skin_args_index = i; skin_args_index < argc; ++skin_args_index) {
        if (strstr(argv[skin_args_index], QEMU_ARGS_PREFIX)) {
            *skin_argc = skin_args_index - i - 1;

            *qemu_argc = argc - skin_args_index - i + 1;
            *qemu_argv = &(argv[skin_args_index]);

            argv[skin_args_index] = argv[0];
        }
    }
}

static void get_host_proxy(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
{
    get_host_proxy_os(http_proxy, https_proxy, ftp_proxy, socks_proxy);
}

static void set_bin_path(gchar * exec_argv)
{
    set_bin_path_os(exec_argv);
}

gchar * get_bin_path(void)
{
    return bin_path;
}

static void check_vm_lock(void)
{
    check_vm_lock_os();
}

static void make_vm_lock(void)
{
    make_vm_lock_os();
}

static void set_image_and_log_path(char *qemu_argv)
{
    int i, j = 0;
    int name_len = 0;
    int prefix_len = 0;
    int suffix_len = 0;
    int max = 0;
    char *path = malloc(PATH_MAX);
    name_len = strlen(qemu_argv);
    prefix_len = strlen(IMAGE_PATH_PREFIX);
    suffix_len = strlen(IMAGE_PATH_SUFFIX);
    max = name_len - suffix_len;
    for (i = prefix_len , j = 0; i < max; i++) {
        path[j++] = qemu_argv[i];
    }
    path[j] = '\0';
    if (!g_path_is_absolute(path)) {
        strcpy(tizen_target_path, g_get_current_dir());
    } else {
        strcpy(tizen_target_path, g_path_get_dirname(path));
    }

    set_emul_vm_name(g_path_get_basename(tizen_target_path));
    strcpy(tizen_target_img_path, path);
    free(path);

    strcpy(log_path, tizen_target_path);
    strcat(log_path, LOGS_SUFFIX);
#ifdef CONFIG_WIN32
    if (access(g_win32_locale_filename_from_utf8(log_path), R_OK) != 0) {
        g_mkdir(g_win32_locale_filename_from_utf8(log_path), 0755);
    }
#else
    if (access(log_path, R_OK) != 0) {
        if (g_mkdir(log_path, 0755) < 0) {
            fprintf(stderr, "failed to create log directory %s\n", log_path);
        }
    }
#endif
    strcat(log_path, LOGFILE);
}

static void redir_output(void)
{
    FILE *fp;

    fp = freopen(log_path, "a+", stdout);
    if (fp == NULL) {
        fprintf(stderr, "log file open error\n");
    }

    fp = freopen(log_path, "a+", stderr);
    if (fp == NULL) {
        fprintf(stderr, "log file open error\n");
    }
    setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
    setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
}

static void extract_qemu_info(int qemu_argc, char **qemu_argv)
{
    int i = 0;

    for (i = 0; i < qemu_argc; ++i) {
        if (strstr(qemu_argv[i], IMAGE_PATH_PREFIX) != NULL) {
            set_image_and_log_path(qemu_argv[i]);
        } else if (strstr(qemu_argv[i], INPUT_TOUCH_PARAMETER) != NULL) {
            set_emul_input_touch_enable(true);
        }
    }
    if (is_emul_input_touch_enable() != true)
        set_emul_input_mouse_enable(true);
}

static void extract_skin_info(int skin_argc, char **skin_argv)
{
    int i = 0;
    int w = 0, h = 0;

    for (i = 0; i < skin_argc; ++i) {
        if (strstr(skin_argv[i], DISPLAY_WIDTH_PREFIX) != NULL) {
            char *width_arg = skin_argv[i] + strlen(DISPLAY_WIDTH_PREFIX);
            w = atoi(width_arg);

            INFO("display width option : %d\n", w);
        } else if (strstr(skin_argv[i], DISPLAY_HEIGHT_PREFIX) != NULL) {
            char *height_arg = skin_argv[i] + strlen(DISPLAY_HEIGHT_PREFIX);
            h = atoi(height_arg);

            INFO("display height option : %d\n", h);
        }

        if (w != 0 && h != 0) {
            set_emul_resolution(w, h);
            break;
        }
    }
}


static void print_system_info(void)
{
#define DIV 1024

    INFO("* Board name : %s\n", build_version);
    INFO("* Package %s\n", pkginfo_version);
    INFO("* Package %s\n", pkginfo_maintainer);
    INFO("* Git Head : %s\n", pkginfo_githead);
    INFO("* %s\n", latest_gittag);
    INFO("* User name : %s\n", g_get_real_name());
    INFO("* Host name : %s\n", g_get_host_name());

    /* time stamp */
    INFO("* Build date : %s\n", build_date);

    qemu_timeval tval = { 0, };
    if (qemu_gettimeofday(&tval) == 0) {
        char timeinfo[64] = {0, };

        time_t ti = tval.tv_sec;
        struct tm *tm_time = localtime(&ti);
        strftime(timeinfo, sizeof(timeinfo), "%Y-%m-%d %H:%M:%S", tm_time);
        INFO("* Current time : %s\n", timeinfo);
    }

#ifdef CONFIG_SDL
    /* Gets the version of the dynamically linked SDL library */
    INFO("* Host sdl version : (%d, %d, %d)\n",
            SDL_Linked_Version()->major,
            SDL_Linked_Version()->minor,
            SDL_Linked_Version()->patch);
#endif

    print_system_info_os();
}

typedef struct {
    const char *device_name;
    int found;
} device_opt_finding_t;

static int find_device_opt (QemuOpts *opts, void *opaque)
{
    device_opt_finding_t *devp = (device_opt_finding_t *) opaque;
    if (devp->found == 1) {
        return 0;
    }

    const char *str = qemu_opt_get (opts, "driver");
    if (strcmp (str, devp->device_name) == 0) {
        devp->found = 1;
    }
    return 0;
}

#define DEFAULT_QEMU_DNS_IP "10.0.2.3"
static void prepare_basic_features(void)
{
    char http_proxy[MIDBUF] ={0}, https_proxy[MIDBUF] = {0,},
        ftp_proxy[MIDBUF] = {0,}, socks_proxy[MIDBUF] = {0,},
        dns[MIDBUF] = {0};

    qemu_add_opts(&qemu_ecs_opts);

    get_host_proxy(http_proxy, https_proxy, ftp_proxy, socks_proxy);
    /* using "DNS" provided by default QEMU */
    g_strlcpy(dns, DEFAULT_QEMU_DNS_IP, strlen(DEFAULT_QEMU_DNS_IP) + 1);

    set_base_port();

    start_ecs();

    check_vm_lock();
    make_vm_lock();

    sdb_setup(); /* determine the base port for emulator */

    gchar * const tmp_str = g_strdup_printf(" sdb_port=%d,"
        " http_proxy=%s https_proxy=%s ftp_proxy=%s socks_proxy=%s"
        " dns1=%s vm_resolution=%dx%d", get_emul_vm_base_port(),
        http_proxy, https_proxy, ftp_proxy, socks_proxy, dns,
        get_emul_resolution_width(), get_emul_resolution_height());

    g_strlcat(maru_kernel_cmdline, tmp_str, LEN_MARU_KERNEL_CMDLINE);

    g_free(tmp_str);
}

#define VIRTIOGL_DEV_NAME "virtio-gl-pci"
#ifdef CONFIG_GL_BACKEND
static void prepare_opengl_acceleration(void)
{
    int capability_check_gl = 0;

    if (enable_gl && enable_yagl) {
        ERR("Error: only one openGL passthrough device can be used at one time!\n");
        exit(1);
    }


    if (enable_gl || enable_yagl) {
        capability_check_gl = gl_acceleration_capability_check();

        if (capability_check_gl != 0) {
            enable_gl = enable_yagl = 0;
            WARN("Warn: GL acceleration was disabled due to the fail of GL check!\n");
        }
    }
    if (enable_gl) {
        device_opt_finding_t devp = {VIRTIOGL_DEV_NAME, 0};
        qemu_opts_foreach(qemu_find_opts("device"), find_device_opt, &devp, 0);
        if (devp.found == 0) {
            if (!qemu_opts_parse(qemu_find_opts("device"), VIRTIOGL_DEV_NAME, 1)) {
                exit(1);
            }
        }
    }

    gchar * const tmp_str = g_strdup_printf(" gles=%d yagl=%d", (enable_gl || enable_yagl), enable_yagl);

    g_strlcat(maru_kernel_cmdline, tmp_str, LEN_MARU_KERNEL_CMDLINE);

    g_free(tmp_str);
}
#endif

#define MARUCAM_DEV_NAME "maru_camera_pci"
#define WEBCAM_INFO_IGNORE 0x00
#define WEBCAM_INFO_WRITE 0x04
static void prepare_host_webcam(void)
{
    is_webcam_enabled = marucam_device_check(WEBCAM_INFO_WRITE);

    if (!is_webcam_enabled) {
        INFO("[Webcam] <WARNING> Webcam support was disabled "
                         "due to the fail of webcam capability check!\n");
    }
    else {
        device_opt_finding_t devp = {MARUCAM_DEV_NAME, 0};
        qemu_opts_foreach(qemu_find_opts("device"), find_device_opt, &devp, 0);
        if (devp.found == 0) {
            if (!qemu_opts_parse(qemu_find_opts("device"), MARUCAM_DEV_NAME, 1)) {
                INFO("Failed to initialize the marucam device.\n");
                exit(1);
            }
        }
        INFO("[Webcam] Webcam support was enabled.\n");
    }

    gchar * const tmp_str = g_strdup_printf(" enable_cam=%d", is_webcam_enabled);

    g_strlcat(maru_kernel_cmdline, tmp_str, LEN_MARU_KERNEL_CMDLINE);

    g_free(tmp_str);
}

const gchar *prepare_maru_devices(const gchar *kernel_cmdline)
{
    INFO("Prepare maru specified kernel command line\n");

    g_strlcpy(maru_kernel_cmdline, kernel_cmdline, LEN_MARU_KERNEL_CMDLINE);

    // Prepare GL acceleration
#ifdef CONFIG_GL_BACKEND
    prepare_opengl_acceleration();
#endif

    // Prepare basic features
    prepare_basic_features();

    // Prepare host webcam
    prepare_host_webcam();

    INFO("kernel command : %s\n", maru_kernel_cmdline);

    return maru_kernel_cmdline;
}

int maru_device_check(QemuOpts *opts)
{
#if defined(CONFIG_GL_BACKEND)
    // virtio-gl pci device
    if (!enable_gl) {
        // ignore virtio-gl-pci device, even if users set it in option.
        const char *driver = qemu_opt_get(opts, "driver");
        if (driver && (strcmp (driver, VIRTIOGL_DEV_NAME) == 0)) {
            return -1;
        }
    }
#endif
    if (!is_webcam_enabled) {
        const char *driver = qemu_opt_get(opts, "driver");
        if (driver && (strcmp (driver, MARUCAM_DEV_NAME) == 0)) {
            return -1;
        }
    }

    return 0;
}

void prepare_maru(void)
{
    INFO("Prepare maru specified feature\n");

    INFO("call construct_main_window\n");

    construct_main_window(_skin_argc, _skin_argv, _qemu_argc, _qemu_argv);

    int guest_server_port = get_device_serial_number() + SDB_UDP_SENSOR_INDEX;
    start_guest_server(guest_server_port);

    mloop_ev_init();
}

int qemu_main(int argc, char **argv, char **envp);

static int emulator_main(int argc, char *argv[], char **envp)
{
    parse_options(argc, argv, &_skin_argc,
                &_skin_argv, &_qemu_argc, &_qemu_argv);
    set_bin_path(_qemu_argv[0]);
    extract_qemu_info(_qemu_argc, _qemu_argv);

    INFO("Emulator start !!!\n");
    atexit(maru_atexit);

    extract_skin_info(_skin_argc, _skin_argv);

    print_system_info();

    INFO("Prepare running...\n");
    /* Redirect stdout and stderr after debug_ch is initialized. */
    redir_output();
    INFO("tizen_target_img_path: %s\n", tizen_target_img_path);
    int i;

    fprintf(stdout, "qemu args: =========================================\n");
    for (i = 0; i < _qemu_argc; ++i) {
        fprintf(stdout, "%s ", _qemu_argv[i]);
    }
    fprintf(stdout, "\nqemu args: =========================================\n");

    fprintf(stdout, "skin args: =========================================\n");
    for (i = 0; i < _skin_argc; ++i) {
        fprintf(stdout, "%s ", _skin_argv[i]);
    }
    fprintf(stdout, "\nskin args: =========================================\n");

    INFO("socket initialize\n");
    socket_init();

    INFO("qemu main start!\n");
    qemu_main(_qemu_argc, _qemu_argv, envp);

    exit_emulator();

    return 0;
}

#ifdef CONFIG_DARWIN
int g_argc;

static void* main_thread(void* args)
{
    char** argv;
    int argc = g_argc;
    argv = (char**) args;

    emulator_main(argc, argv, NULL);

    thread_running = 0;
    pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
    char** args;
    pthread_t main_thread_id;

    g_argc = argc;
    args = argv;

    if (0 != pthread_create(&main_thread_id, NULL, main_thread, args)) {
        INFO("Create main thread failed\n");
        return -1;
    }

    ns_event_loop(&thread_running);

    return 0;
}
#elif defined (CONFIG_LINUX)
int main(int argc, char *argv[], char **envp)
{
    maru_register_exception_handler();
    return emulator_main(argc, argv, envp);
}
#else
int main(int argc, char *argv[])
{
    maru_register_exception_handler();
    return emulator_main(argc, argv, NULL);
}
#endif