| 1 | /*
|
|---|
| 2 | * Copyright (c) 2023 SimonJRiddix
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #include <stdio.h>
|
|---|
| 30 | #include <stdlib.h>
|
|---|
| 31 | #include <str.h>
|
|---|
| 32 | #include <str_error.h>
|
|---|
| 33 | #include <task.h>
|
|---|
| 34 | #include <ui/ui.h>
|
|---|
| 35 |
|
|---|
| 36 | static const char *display_spec = UI_DISPLAY_DEFAULT;
|
|---|
| 37 | int app_launchl(const char *app, ...);
|
|---|
| 38 |
|
|---|
| 39 | #define NAME "Apps Menu"
|
|---|
| 40 |
|
|---|
| 41 | int app_launchl(const char *app, ...)
|
|---|
| 42 | {
|
|---|
| 43 | errno_t rc;
|
|---|
| 44 | task_id_t id;
|
|---|
| 45 | task_wait_t wait;
|
|---|
| 46 | va_list ap;
|
|---|
| 47 | const char *arg;
|
|---|
| 48 | const char **argv;
|
|---|
| 49 | const char **argp;
|
|---|
| 50 | int cnt = 0;
|
|---|
| 51 | int i;
|
|---|
| 52 |
|
|---|
| 53 | va_start(ap, app);
|
|---|
| 54 | do {
|
|---|
| 55 | arg = va_arg(ap, const char *);
|
|---|
| 56 | cnt++;
|
|---|
| 57 | } while (arg != NULL);
|
|---|
| 58 | va_end(ap);
|
|---|
| 59 |
|
|---|
| 60 | argv = calloc(cnt + 4, sizeof(const char *));
|
|---|
| 61 | if (argv == NULL)
|
|---|
| 62 | return -1;
|
|---|
| 63 |
|
|---|
| 64 | task_exit_t texit;
|
|---|
| 65 | int retval;
|
|---|
| 66 |
|
|---|
| 67 | argp = argv;
|
|---|
| 68 | *argp++ = app;
|
|---|
| 69 |
|
|---|
| 70 | if (str_cmp(display_spec, UI_DISPLAY_DEFAULT) != 0) {
|
|---|
| 71 | *argp++ = "-d";
|
|---|
| 72 | *argp++ = display_spec;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | va_start(ap, app);
|
|---|
| 76 | do {
|
|---|
| 77 | arg = va_arg(ap, const char *);
|
|---|
| 78 | *argp++ = arg;
|
|---|
| 79 | } while (arg != NULL);
|
|---|
| 80 | va_end(ap);
|
|---|
| 81 |
|
|---|
| 82 | *argp++ = NULL;
|
|---|
| 83 |
|
|---|
| 84 | printf("%s: Spawning %s", NAME, app);
|
|---|
| 85 | for (i = 0; argv[i] != NULL; i++) {
|
|---|
| 86 | printf(" %s", argv[i]);
|
|---|
| 87 | }
|
|---|
| 88 | printf("\n");
|
|---|
| 89 |
|
|---|
| 90 | rc = task_spawnv(&id, &wait, app, argv);
|
|---|
| 91 | if (rc != EOK) {
|
|---|
| 92 | printf("%s: Error spawning %s (%s)\n", NAME, app, str_error(rc));
|
|---|
| 93 | return -1;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | rc = task_wait(&wait, &texit, &retval);
|
|---|
| 97 | if ((rc != EOK) || (texit != TASK_EXIT_NORMAL)) {
|
|---|
| 98 | printf("%s: Error retrieving retval from %s (%s)\n", NAME,
|
|---|
| 99 | app, str_error(rc));
|
|---|
| 100 | return -1;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | return retval;
|
|---|
| 104 | }
|
|---|