source: mainline/uspace/app/nav/nav.c@ 0e125698

Last change on this file since 0e125698 was 0e125698, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Opening directories

  • Property mode set to 100644
File size: 6.6 KB
Line 
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#include "panel.h"
48
49static void wnd_close(ui_window_t *, void *);
50static void wnd_kbd(ui_window_t *, void *, kbd_event_t *);
51
52static ui_window_cb_t window_cb = {
53 .close = wnd_close,
54 .kbd = wnd_kbd
55};
56
57/** Window close button was clicked.
58 *
59 * @param window Window
60 * @param arg Argument (navigator)
61 */
62static 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/** Window keyboard event handler.
70 *
71 * @param window Window
72 * @param arg Argument (navigator)
73 * @param event Keyboard event
74 */
75static void wnd_kbd(ui_window_t *window, void *arg, kbd_event_t *event)
76{
77 navigator_t *navigator = (navigator_t *) arg;
78
79 if (event->type == KEY_PRESS &&
80 ((event->mods & KM_ALT) == 0) &&
81 ((event->mods & KM_SHIFT) == 0) &&
82 (event->mods & KM_CTRL) != 0) {
83 switch (event->key) {
84 case KC_Q:
85 ui_quit(navigator->ui);
86 break;
87 default:
88 break;
89 }
90 }
91
92 if (event->type == KEY_PRESS &&
93 ((event->mods & (KM_CTRL | KM_ALT | KM_SHIFT)) == 0)) {
94 switch (event->key) {
95 case KC_TAB:
96 navigator_switch_panel(navigator);
97 break;
98 default:
99 break;
100 }
101 }
102
103 ui_window_def_kbd(window, event);
104}
105
106/** Create navigator.
107 *
108 * @param display_spec Display specification
109 * @param rnavigator Place to store pointer to new navigator
110 * @return EOK on success or ane error code
111 */
112errno_t navigator_create(const char *display_spec,
113 navigator_t **rnavigator)
114{
115 navigator_t *navigator;
116 ui_wnd_params_t params;
117 gfx_rect_t rect;
118 unsigned i;
119 errno_t rc;
120
121 navigator = calloc(1, sizeof(navigator_t));
122 if (navigator == NULL)
123 return ENOMEM;
124
125 rc = ui_create(display_spec, &navigator->ui);
126 if (rc != EOK) {
127 printf("Error creating UI on display %s.\n", display_spec);
128 goto error;
129 }
130
131 ui_wnd_params_init(&params);
132 params.caption = "Navigator";
133 params.style &= ~ui_wds_decorated;
134 params.placement = ui_wnd_place_full_screen;
135
136 rc = ui_window_create(navigator->ui, &params, &navigator->window);
137 if (rc != EOK) {
138 printf("Error creating window.\n");
139 goto error;
140 }
141
142 ui_window_set_cb(navigator->window, &window_cb, (void *) navigator);
143
144 rc = ui_fixed_create(&navigator->fixed);
145 if (rc != EOK) {
146 printf("Error creating fixed layout.\n");
147 goto error;
148 }
149
150 ui_window_add(navigator->window, ui_fixed_ctl(navigator->fixed));
151
152 rc = nav_menu_create(navigator->window, &navigator->menu);
153 if (rc != EOK)
154 goto error;
155
156 rc = ui_fixed_add(navigator->fixed, nav_menu_ctl(navigator->menu));
157 if (rc != EOK) {
158 printf("Error adding control to layout.\n");
159 return rc;
160 }
161
162 for (i = 0; i < 2; i++) {
163 rc = panel_create(navigator->window, i == 0,
164 &navigator->panel[i]);
165 if (rc != EOK)
166 goto error;
167
168 rect.p0.x = 40 * i;
169 rect.p0.y = 1;
170 rect.p1.x = 40 * (i + 1);
171 rect.p1.y = 24;
172 panel_set_rect(navigator->panel[i], &rect);
173
174 rc = ui_fixed_add(navigator->fixed,
175 panel_ctl(navigator->panel[i]));
176 if (rc != EOK) {
177 printf("Error adding control to layout.\n");
178 goto error;
179 }
180
181 rc = panel_read_dir(navigator->panel[i], ".");
182 if (rc != EOK) {
183 printf("Error reading directory.\n");
184 goto error;
185 }
186 }
187
188 rc = ui_window_paint(navigator->window);
189 if (rc != EOK) {
190 printf("Error painting window.\n");
191 goto error;
192 }
193
194 *rnavigator = navigator;
195 return EOK;
196error:
197 navigator_destroy(navigator);
198 return rc;
199}
200
201void navigator_destroy(navigator_t *navigator)
202{
203 unsigned i;
204
205 for (i = 0; i < 2; i++) {
206 ui_fixed_remove(navigator->fixed,
207 panel_ctl(navigator->panel[i]));
208 panel_destroy(navigator->panel[i]);
209 }
210
211 ui_fixed_remove(navigator->fixed, nav_menu_ctl(navigator->menu));
212
213 if (navigator->menu != NULL)
214 nav_menu_destroy(navigator->menu);
215 if (navigator->window != NULL)
216 ui_window_destroy(navigator->window);
217 if (navigator->ui != NULL)
218 ui_destroy(navigator->ui);
219 free(navigator);
220}
221
222/** Run navigator on the specified display. */
223errno_t navigator_run(const char *display_spec)
224{
225 navigator_t *navigator;
226 errno_t rc;
227
228 rc = navigator_create(display_spec, &navigator);
229 if (rc != EOK)
230 return rc;
231
232 ui_run(navigator->ui);
233
234 navigator_destroy(navigator);
235 return EOK;
236}
237
238/** Get the currently active navigator panel.
239 *
240 * @param navigator Navigator
241 * @return Currently active panel
242 */
243panel_t *navigator_get_active_panel(navigator_t *navigator)
244{
245 int i;
246
247 for (i = 0; i < navigator_panels; i++) {
248 if (panel_is_active(navigator->panel[i]))
249 return navigator->panel[i];
250 }
251
252 /* This should not happen */
253 assert(false);
254 return NULL;
255}
256
257/** Switch to another navigator panel.
258 *
259 * Changes the currently active navigator panel to the next panel.
260 *
261 * @param navigator Navigator
262 */
263void navigator_switch_panel(navigator_t *navigator)
264{
265 errno_t rc;
266
267 if (panel_is_active(navigator->panel[0])) {
268 rc = panel_activate(navigator->panel[1]);
269 if (rc != EOK)
270 return;
271 panel_deactivate(navigator->panel[0]);
272 } else {
273 rc = panel_activate(navigator->panel[0]);
274 if (rc != EOK)
275 return;
276 panel_deactivate(navigator->panel[1]);
277 }
278}
279
280/** @}
281 */
Note: See TracBrowser for help on using the repository browser.