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