[e0e1b3d] | 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>
|
---|
[d097daca] | 39 | #include <stdlib.h>
|
---|
[e0e1b3d] | 40 | #include <str.h>
|
---|
| 41 | #include <ui/fixed.h>
|
---|
| 42 | #include <ui/resource.h>
|
---|
| 43 | #include <ui/ui.h>
|
---|
| 44 | #include <ui/window.h>
|
---|
[d097daca] | 45 | #include "menu.h"
|
---|
[e0e1b3d] | 46 | #include "nav.h"
|
---|
[0e5ed803] | 47 | #include "panel.h"
|
---|
[e0e1b3d] | 48 |
|
---|
| 49 | static void wnd_close(ui_window_t *, void *);
|
---|
[166aba54] | 50 | static void wnd_kbd(ui_window_t *, void *, kbd_event_t *);
|
---|
[e0e1b3d] | 51 |
|
---|
| 52 | static ui_window_cb_t window_cb = {
|
---|
[166aba54] | 53 | .close = wnd_close,
|
---|
| 54 | .kbd = wnd_kbd
|
---|
[e0e1b3d] | 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 |
|
---|
[166aba54] | 69 | /** Window keyboard event handler.
|
---|
| 70 | *
|
---|
| 71 | * @param window Window
|
---|
| 72 | * @param arg Argument (navigator)
|
---|
| 73 | * @param event Keyboard event
|
---|
| 74 | */
|
---|
| 75 | static 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) {
|
---|
[fe5c7a1] | 83 | switch (event->key) {
|
---|
| 84 | case KC_Q:
|
---|
[166aba54] | 85 | ui_quit(navigator->ui);
|
---|
[fe5c7a1] | 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 | }
|
---|
[166aba54] | 101 | }
|
---|
| 102 |
|
---|
| 103 | ui_window_def_kbd(window, event);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[d097daca] | 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 | */
|
---|
[68b9e540] | 112 | errno_t navigator_create(const char *display_spec,
|
---|
[d097daca] | 113 | navigator_t **rnavigator)
|
---|
[e0e1b3d] | 114 | {
|
---|
[d097daca] | 115 | navigator_t *navigator;
|
---|
[e0e1b3d] | 116 | ui_wnd_params_t params;
|
---|
[0e5ed803] | 117 | gfx_rect_t rect;
|
---|
| 118 | unsigned i;
|
---|
[e0e1b3d] | 119 | errno_t rc;
|
---|
| 120 |
|
---|
[d097daca] | 121 | navigator = calloc(1, sizeof(navigator_t));
|
---|
| 122 | if (navigator == NULL)
|
---|
| 123 | return ENOMEM;
|
---|
| 124 |
|
---|
| 125 | rc = ui_create(display_spec, &navigator->ui);
|
---|
[e0e1b3d] | 126 | if (rc != EOK) {
|
---|
| 127 | printf("Error creating UI on display %s.\n", display_spec);
|
---|
[d097daca] | 128 | goto error;
|
---|
[e0e1b3d] | 129 | }
|
---|
| 130 |
|
---|
| 131 | ui_wnd_params_init(¶ms);
|
---|
| 132 | params.caption = "Navigator";
|
---|
[d097daca] | 133 | params.style &= ~ui_wds_decorated;
|
---|
| 134 | params.placement = ui_wnd_place_full_screen;
|
---|
[e0e1b3d] | 135 |
|
---|
[d097daca] | 136 | rc = ui_window_create(navigator->ui, ¶ms, &navigator->window);
|
---|
[e0e1b3d] | 137 | if (rc != EOK) {
|
---|
| 138 | printf("Error creating window.\n");
|
---|
[d097daca] | 139 | goto error;
|
---|
[e0e1b3d] | 140 | }
|
---|
| 141 |
|
---|
[d097daca] | 142 | ui_window_set_cb(navigator->window, &window_cb, (void *) navigator);
|
---|
[e0e1b3d] | 143 |
|
---|
[d097daca] | 144 | rc = ui_fixed_create(&navigator->fixed);
|
---|
[e0e1b3d] | 145 | if (rc != EOK) {
|
---|
| 146 | printf("Error creating fixed layout.\n");
|
---|
[d097daca] | 147 | goto error;
|
---|
[e0e1b3d] | 148 | }
|
---|
| 149 |
|
---|
[d097daca] | 150 | ui_window_add(navigator->window, ui_fixed_ctl(navigator->fixed));
|
---|
[e0e1b3d] | 151 |
|
---|
[68b9e540] | 152 | rc = nav_menu_create(navigator->window, &navigator->menu);
|
---|
[d097daca] | 153 | if (rc != EOK)
|
---|
| 154 | goto error;
|
---|
[e0e1b3d] | 155 |
|
---|
[68b9e540] | 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 |
|
---|
[0e5ed803] | 162 | for (i = 0; i < 2; i++) {
|
---|
[fe5c7a1] | 163 | rc = panel_create(navigator->window, i == 0,
|
---|
| 164 | &navigator->panel[i]);
|
---|
[0e5ed803] | 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");
|
---|
[8e5f39d] | 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;
|
---|
[0e5ed803] | 185 | }
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[d097daca] | 188 | rc = ui_window_paint(navigator->window);
|
---|
[e0e1b3d] | 189 | if (rc != EOK) {
|
---|
[d097daca] | 190 | printf("Error painting window.\n");
|
---|
| 191 | goto error;
|
---|
[e0e1b3d] | 192 | }
|
---|
| 193 |
|
---|
[d097daca] | 194 | *rnavigator = navigator;
|
---|
| 195 | return EOK;
|
---|
| 196 | error:
|
---|
| 197 | navigator_destroy(navigator);
|
---|
| 198 | return rc;
|
---|
| 199 | }
|
---|
[e0e1b3d] | 200 |
|
---|
[68b9e540] | 201 | void navigator_destroy(navigator_t *navigator)
|
---|
[d097daca] | 202 | {
|
---|
[0e5ed803] | 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 |
|
---|
[68b9e540] | 211 | ui_fixed_remove(navigator->fixed, nav_menu_ctl(navigator->menu));
|
---|
| 212 |
|
---|
[d097daca] | 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. */
|
---|
[68b9e540] | 223 | errno_t navigator_run(const char *display_spec)
|
---|
[d097daca] | 224 | {
|
---|
| 225 | navigator_t *navigator;
|
---|
| 226 | errno_t rc;
|
---|
[e0e1b3d] | 227 |
|
---|
[d097daca] | 228 | rc = navigator_create(display_spec, &navigator);
|
---|
| 229 | if (rc != EOK)
|
---|
| 230 | return rc;
|
---|
[e0e1b3d] | 231 |
|
---|
[d097daca] | 232 | ui_run(navigator->ui);
|
---|
[e0e1b3d] | 233 |
|
---|
[d097daca] | 234 | navigator_destroy(navigator);
|
---|
[e0e1b3d] | 235 | return EOK;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
[fe5c7a1] | 238 | /** Get the currently active navigator panel.
|
---|
| 239 | *
|
---|
| 240 | * @param navigator Navigator
|
---|
| 241 | * @return Currently active panel
|
---|
| 242 | */
|
---|
| 243 | panel_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 | */
|
---|
| 263 | void navigator_switch_panel(navigator_t *navigator)
|
---|
| 264 | {
|
---|
[0e125698] | 265 | errno_t rc;
|
---|
| 266 |
|
---|
[fe5c7a1] | 267 | if (panel_is_active(navigator->panel[0])) {
|
---|
[0e125698] | 268 | rc = panel_activate(navigator->panel[1]);
|
---|
| 269 | if (rc != EOK)
|
---|
| 270 | return;
|
---|
[fe5c7a1] | 271 | panel_deactivate(navigator->panel[0]);
|
---|
| 272 | } else {
|
---|
[0e125698] | 273 | rc = panel_activate(navigator->panel[0]);
|
---|
| 274 | if (rc != EOK)
|
---|
| 275 | return;
|
---|
[fe5c7a1] | 276 | panel_deactivate(navigator->panel[1]);
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
| 279 |
|
---|
[e0e1b3d] | 280 | /** @}
|
---|
| 281 | */
|
---|