[f7a90df] | 1 | /*
|
---|
| 2 | * Copyright (c) 2020 Jiri Svoboda
|
---|
| 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 | /** @addtogroup libui
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file User interface
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <display.h>
|
---|
| 37 | #include <errno.h>
|
---|
[d284ce9] | 38 | #include <fibril.h>
|
---|
[f7a90df] | 39 | #include <stdlib.h>
|
---|
| 40 | #include <ui/ui.h>
|
---|
| 41 | #include "../private/ui.h"
|
---|
| 42 |
|
---|
| 43 | /** Create new user interface.
|
---|
| 44 | *
|
---|
| 45 | * @param ospec Output specification or @c UI_DISPLAY_DEFAULT to use
|
---|
| 46 | * the default output
|
---|
| 47 | * @param rui Place to store pointer to new UI
|
---|
| 48 | * @return EOK on success or an error code
|
---|
| 49 | */
|
---|
| 50 | errno_t ui_create(const char *ospec, ui_t **rui)
|
---|
| 51 | {
|
---|
| 52 | errno_t rc;
|
---|
| 53 | display_t *display;
|
---|
| 54 | ui_t *ui;
|
---|
| 55 |
|
---|
| 56 | rc = display_open(ospec, &display);
|
---|
| 57 | if (rc != EOK)
|
---|
| 58 | return rc;
|
---|
| 59 |
|
---|
| 60 | rc = ui_create_disp(display, &ui);
|
---|
| 61 | if (rc != EOK) {
|
---|
| 62 | display_close(display);
|
---|
| 63 | return rc;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | ui->display = display;
|
---|
| 67 | ui->myoutput = true;
|
---|
[d284ce9] | 68 | *rui = ui;
|
---|
[f7a90df] | 69 | return EOK;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | /** Create new user interface using display service.
|
---|
| 73 | *
|
---|
| 74 | * @param disp Display
|
---|
| 75 | * @param rui Place to store pointer to new UI
|
---|
| 76 | * @return EOK on success or an error code
|
---|
| 77 | */
|
---|
| 78 | errno_t ui_create_disp(display_t *disp, ui_t **rui)
|
---|
| 79 | {
|
---|
| 80 | ui_t *ui;
|
---|
| 81 |
|
---|
| 82 | ui = calloc(1, sizeof(ui_t));
|
---|
| 83 | if (ui == NULL)
|
---|
| 84 | return ENOMEM;
|
---|
| 85 |
|
---|
| 86 | ui->display = disp;
|
---|
| 87 | *rui = ui;
|
---|
| 88 | return EOK;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | /** Destroy user interface.
|
---|
| 92 | *
|
---|
| 93 | * @param ui User interface or @c NULL
|
---|
| 94 | */
|
---|
| 95 | void ui_destroy(ui_t *ui)
|
---|
| 96 | {
|
---|
| 97 | if (ui == NULL)
|
---|
| 98 | return;
|
---|
| 99 |
|
---|
| 100 | if (ui->myoutput)
|
---|
| 101 | display_close(ui->display);
|
---|
| 102 | free(ui);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[d284ce9] | 105 | /** Execute user interface.
|
---|
| 106 | *
|
---|
| 107 | * This function returns once the application starts the termination
|
---|
| 108 | * process by calling ui_quit(@a ui).
|
---|
| 109 | *
|
---|
| 110 | * @param ui User interface
|
---|
| 111 | */
|
---|
| 112 | void ui_run(ui_t *ui)
|
---|
| 113 | {
|
---|
| 114 | while (!ui->quit)
|
---|
| 115 | fibril_usleep(100000);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | /** Terminate user interface.
|
---|
| 119 | *
|
---|
| 120 | * Calling this function causes the user interface to terminate
|
---|
| 121 | * (i.e. exit from ui_run()). This would be typically called from
|
---|
| 122 | * an event handler.
|
---|
| 123 | *
|
---|
| 124 | * @param ui User interface
|
---|
| 125 | */
|
---|
| 126 | void ui_quit(ui_t *ui)
|
---|
| 127 | {
|
---|
| 128 | ui->quit = true;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[f7a90df] | 131 | /** @}
|
---|
| 132 | */
|
---|