1 | /*
|
---|
2 | * Copyright (c) 2021 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 nav
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file Navigator.
|
---|
33 | *
|
---|
34 | * HelenOS file manager.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <gfx/coord.h>
|
---|
38 | #include <stdio.h>
|
---|
39 | #include <stdlib.h>
|
---|
40 | #include <str.h>
|
---|
41 | #include <ui/fixed.h>
|
---|
42 | #include <ui/resource.h>
|
---|
43 | #include <ui/ui.h>
|
---|
44 | #include <ui/window.h>
|
---|
45 | #include "menu.h"
|
---|
46 | #include "nav.h"
|
---|
47 |
|
---|
48 | static errno_t navigator_create(const char *, navigator_t **);
|
---|
49 | static void navigator_destroy(navigator_t *);
|
---|
50 |
|
---|
51 | static void wnd_close(ui_window_t *, void *);
|
---|
52 |
|
---|
53 | static ui_window_cb_t window_cb = {
|
---|
54 | .close = wnd_close
|
---|
55 | };
|
---|
56 |
|
---|
57 | /** Window close button was clicked.
|
---|
58 | *
|
---|
59 | * @param window Window
|
---|
60 | * @param arg Argument (navigator)
|
---|
61 | */
|
---|
62 | static void wnd_close(ui_window_t *window, void *arg)
|
---|
63 | {
|
---|
64 | navigator_t *navigator = (navigator_t *) arg;
|
---|
65 |
|
---|
66 | ui_quit(navigator->ui);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /** Create navigator.
|
---|
70 | *
|
---|
71 | * @param display_spec Display specification
|
---|
72 | * @param rnavigator Place to store pointer to new navigator
|
---|
73 | * @return EOK on success or ane error code
|
---|
74 | */
|
---|
75 | static errno_t navigator_create(const char *display_spec,
|
---|
76 | navigator_t **rnavigator)
|
---|
77 | {
|
---|
78 | navigator_t *navigator;
|
---|
79 | ui_wnd_params_t params;
|
---|
80 | errno_t rc;
|
---|
81 |
|
---|
82 | navigator = calloc(1, sizeof(navigator_t));
|
---|
83 | if (navigator == NULL)
|
---|
84 | return ENOMEM;
|
---|
85 |
|
---|
86 | rc = ui_create(display_spec, &navigator->ui);
|
---|
87 | if (rc != EOK) {
|
---|
88 | printf("Error creating UI on display %s.\n", display_spec);
|
---|
89 | goto error;
|
---|
90 | }
|
---|
91 |
|
---|
92 | ui_wnd_params_init(¶ms);
|
---|
93 | params.caption = "Navigator";
|
---|
94 | params.style &= ~ui_wds_decorated;
|
---|
95 | params.placement = ui_wnd_place_full_screen;
|
---|
96 |
|
---|
97 | rc = ui_window_create(navigator->ui, ¶ms, &navigator->window);
|
---|
98 | if (rc != EOK) {
|
---|
99 | printf("Error creating window.\n");
|
---|
100 | goto error;
|
---|
101 | }
|
---|
102 |
|
---|
103 | ui_window_set_cb(navigator->window, &window_cb, (void *) navigator);
|
---|
104 |
|
---|
105 | rc = ui_fixed_create(&navigator->fixed);
|
---|
106 | if (rc != EOK) {
|
---|
107 | printf("Error creating fixed layout.\n");
|
---|
108 | goto error;
|
---|
109 | }
|
---|
110 |
|
---|
111 | ui_window_add(navigator->window, ui_fixed_ctl(navigator->fixed));
|
---|
112 |
|
---|
113 | rc = nav_menu_create(navigator, &navigator->menu);
|
---|
114 | if (rc != EOK)
|
---|
115 | goto error;
|
---|
116 |
|
---|
117 | rc = ui_window_paint(navigator->window);
|
---|
118 | if (rc != EOK) {
|
---|
119 | printf("Error painting window.\n");
|
---|
120 | goto error;
|
---|
121 | }
|
---|
122 |
|
---|
123 | *rnavigator = navigator;
|
---|
124 | return EOK;
|
---|
125 | error:
|
---|
126 | navigator_destroy(navigator);
|
---|
127 | return rc;
|
---|
128 | }
|
---|
129 |
|
---|
130 | static void navigator_destroy(navigator_t *navigator)
|
---|
131 | {
|
---|
132 | if (navigator->menu != NULL)
|
---|
133 | nav_menu_destroy(navigator->menu);
|
---|
134 | if (navigator->window != NULL)
|
---|
135 | ui_window_destroy(navigator->window);
|
---|
136 | if (navigator->ui != NULL)
|
---|
137 | ui_destroy(navigator->ui);
|
---|
138 | free(navigator);
|
---|
139 | }
|
---|
140 |
|
---|
141 | /** Run navigator on the specified display. */
|
---|
142 | static errno_t navigator_run(const char *display_spec)
|
---|
143 | {
|
---|
144 | navigator_t *navigator;
|
---|
145 | errno_t rc;
|
---|
146 |
|
---|
147 | rc = navigator_create(display_spec, &navigator);
|
---|
148 | if (rc != EOK)
|
---|
149 | return rc;
|
---|
150 |
|
---|
151 | ui_run(navigator->ui);
|
---|
152 |
|
---|
153 | navigator_destroy(navigator);
|
---|
154 | return EOK;
|
---|
155 | }
|
---|
156 |
|
---|
157 | static void print_syntax(void)
|
---|
158 | {
|
---|
159 | printf("Syntax: nav [-d <display-spec>]\n");
|
---|
160 | }
|
---|
161 |
|
---|
162 | int main(int argc, char *argv[])
|
---|
163 | {
|
---|
164 | const char *display_spec = UI_CONSOLE_DEFAULT;
|
---|
165 | errno_t rc;
|
---|
166 | int i;
|
---|
167 |
|
---|
168 | i = 1;
|
---|
169 | while (i < argc && argv[i][0] == '-') {
|
---|
170 | if (str_cmp(argv[i], "-d") == 0) {
|
---|
171 | ++i;
|
---|
172 | if (i >= argc) {
|
---|
173 | printf("Argument missing.\n");
|
---|
174 | print_syntax();
|
---|
175 | return 1;
|
---|
176 | }
|
---|
177 |
|
---|
178 | display_spec = argv[i++];
|
---|
179 | } else {
|
---|
180 | printf("Invalid option '%s'.\n", argv[i]);
|
---|
181 | print_syntax();
|
---|
182 | return 1;
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | if (i < argc) {
|
---|
187 | print_syntax();
|
---|
188 | return 1;
|
---|
189 | }
|
---|
190 |
|
---|
191 | rc = navigator_run(display_spec);
|
---|
192 | if (rc != EOK)
|
---|
193 | return 1;
|
---|
194 |
|
---|
195 | return 0;
|
---|
196 | }
|
---|
197 |
|
---|
198 | /** @}
|
---|
199 | */
|
---|