[748c8bd] | 1 | /*
|
---|
[54ddb59] | 2 | * Copyright (c) 2022 Jiri Svoboda
|
---|
[748c8bd] | 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>
|
---|
[5d466a1] | 39 | #include <stdlib.h>
|
---|
[748c8bd] | 40 | #include <str.h>
|
---|
| 41 | #include <ui/fixed.h>
|
---|
[54ddb59] | 42 | #include <ui/filelist.h>
|
---|
[748c8bd] | 43 | #include <ui/resource.h>
|
---|
| 44 | #include <ui/ui.h>
|
---|
| 45 | #include <ui/window.h>
|
---|
[5d466a1] | 46 | #include "menu.h"
|
---|
[748c8bd] | 47 | #include "nav.h"
|
---|
[b36ebb42] | 48 | #include "panel.h"
|
---|
[748c8bd] | 49 |
|
---|
| 50 | static void wnd_close(ui_window_t *, void *);
|
---|
[9f7e9bb] | 51 | static void wnd_kbd(ui_window_t *, void *, kbd_event_t *);
|
---|
[748c8bd] | 52 |
|
---|
| 53 | static ui_window_cb_t window_cb = {
|
---|
[9f7e9bb] | 54 | .close = wnd_close,
|
---|
| 55 | .kbd = wnd_kbd
|
---|
[748c8bd] | 56 | };
|
---|
| 57 |
|
---|
[f59212cc] | 58 | static void navigator_file_open(void *);
|
---|
| 59 | static void navigator_file_exit(void *);
|
---|
| 60 |
|
---|
| 61 | static nav_menu_cb_t navigator_menu_cb = {
|
---|
| 62 | .file_open = navigator_file_open,
|
---|
| 63 | .file_exit = navigator_file_exit
|
---|
| 64 | };
|
---|
| 65 |
|
---|
[39ab17c] | 66 | static void navigator_panel_activate_req(void *, panel_t *);
|
---|
| 67 |
|
---|
| 68 | static panel_cb_t navigator_panel_cb = {
|
---|
| 69 | .activate_req = navigator_panel_activate_req
|
---|
| 70 | };
|
---|
| 71 |
|
---|
[748c8bd] | 72 | /** Window close button was clicked.
|
---|
| 73 | *
|
---|
| 74 | * @param window Window
|
---|
| 75 | * @param arg Argument (navigator)
|
---|
| 76 | */
|
---|
| 77 | static void wnd_close(ui_window_t *window, void *arg)
|
---|
| 78 | {
|
---|
| 79 | navigator_t *navigator = (navigator_t *) arg;
|
---|
| 80 |
|
---|
| 81 | ui_quit(navigator->ui);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[9f7e9bb] | 84 | /** Window keyboard event handler.
|
---|
| 85 | *
|
---|
| 86 | * @param window Window
|
---|
| 87 | * @param arg Argument (navigator)
|
---|
| 88 | * @param event Keyboard event
|
---|
| 89 | */
|
---|
| 90 | static void wnd_kbd(ui_window_t *window, void *arg, kbd_event_t *event)
|
---|
| 91 | {
|
---|
| 92 | navigator_t *navigator = (navigator_t *) arg;
|
---|
| 93 |
|
---|
| 94 | if (event->type == KEY_PRESS &&
|
---|
| 95 | ((event->mods & KM_ALT) == 0) &&
|
---|
| 96 | ((event->mods & KM_SHIFT) == 0) &&
|
---|
| 97 | (event->mods & KM_CTRL) != 0) {
|
---|
[692c7f40] | 98 | switch (event->key) {
|
---|
| 99 | case KC_Q:
|
---|
[9f7e9bb] | 100 | ui_quit(navigator->ui);
|
---|
[692c7f40] | 101 | break;
|
---|
| 102 | default:
|
---|
| 103 | break;
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | if (event->type == KEY_PRESS &&
|
---|
| 108 | ((event->mods & (KM_CTRL | KM_ALT | KM_SHIFT)) == 0)) {
|
---|
| 109 | switch (event->key) {
|
---|
| 110 | case KC_TAB:
|
---|
| 111 | navigator_switch_panel(navigator);
|
---|
| 112 | break;
|
---|
| 113 | default:
|
---|
| 114 | break;
|
---|
| 115 | }
|
---|
[9f7e9bb] | 116 | }
|
---|
| 117 |
|
---|
| 118 | ui_window_def_kbd(window, event);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[5d466a1] | 121 | /** Create navigator.
|
---|
| 122 | *
|
---|
| 123 | * @param display_spec Display specification
|
---|
| 124 | * @param rnavigator Place to store pointer to new navigator
|
---|
| 125 | * @return EOK on success or ane error code
|
---|
| 126 | */
|
---|
[6aa85c1] | 127 | errno_t navigator_create(const char *display_spec,
|
---|
[5d466a1] | 128 | navigator_t **rnavigator)
|
---|
[748c8bd] | 129 | {
|
---|
[5d466a1] | 130 | navigator_t *navigator;
|
---|
[748c8bd] | 131 | ui_wnd_params_t params;
|
---|
[b36ebb42] | 132 | gfx_rect_t rect;
|
---|
[8edec53] | 133 | gfx_rect_t arect;
|
---|
| 134 | gfx_coord_t pw;
|
---|
[b36ebb42] | 135 | unsigned i;
|
---|
[748c8bd] | 136 | errno_t rc;
|
---|
| 137 |
|
---|
[5d466a1] | 138 | navigator = calloc(1, sizeof(navigator_t));
|
---|
| 139 | if (navigator == NULL)
|
---|
| 140 | return ENOMEM;
|
---|
| 141 |
|
---|
| 142 | rc = ui_create(display_spec, &navigator->ui);
|
---|
[748c8bd] | 143 | if (rc != EOK) {
|
---|
| 144 | printf("Error creating UI on display %s.\n", display_spec);
|
---|
[5d466a1] | 145 | goto error;
|
---|
[748c8bd] | 146 | }
|
---|
| 147 |
|
---|
| 148 | ui_wnd_params_init(¶ms);
|
---|
| 149 | params.caption = "Navigator";
|
---|
[5d466a1] | 150 | params.style &= ~ui_wds_decorated;
|
---|
| 151 | params.placement = ui_wnd_place_full_screen;
|
---|
[748c8bd] | 152 |
|
---|
[5d466a1] | 153 | rc = ui_window_create(navigator->ui, ¶ms, &navigator->window);
|
---|
[748c8bd] | 154 | if (rc != EOK) {
|
---|
| 155 | printf("Error creating window.\n");
|
---|
[5d466a1] | 156 | goto error;
|
---|
[748c8bd] | 157 | }
|
---|
| 158 |
|
---|
[5d466a1] | 159 | ui_window_set_cb(navigator->window, &window_cb, (void *) navigator);
|
---|
[8edec53] | 160 | ui_window_get_app_rect(navigator->window, &arect);
|
---|
[748c8bd] | 161 |
|
---|
[5d466a1] | 162 | rc = ui_fixed_create(&navigator->fixed);
|
---|
[748c8bd] | 163 | if (rc != EOK) {
|
---|
| 164 | printf("Error creating fixed layout.\n");
|
---|
[5d466a1] | 165 | goto error;
|
---|
[748c8bd] | 166 | }
|
---|
| 167 |
|
---|
[5d466a1] | 168 | ui_window_add(navigator->window, ui_fixed_ctl(navigator->fixed));
|
---|
[748c8bd] | 169 |
|
---|
[6aa85c1] | 170 | rc = nav_menu_create(navigator->window, &navigator->menu);
|
---|
[5d466a1] | 171 | if (rc != EOK)
|
---|
| 172 | goto error;
|
---|
[748c8bd] | 173 |
|
---|
[f59212cc] | 174 | nav_menu_set_cb(navigator->menu, &navigator_menu_cb,
|
---|
| 175 | (void *)navigator);
|
---|
| 176 |
|
---|
[6aa85c1] | 177 | rc = ui_fixed_add(navigator->fixed, nav_menu_ctl(navigator->menu));
|
---|
| 178 | if (rc != EOK) {
|
---|
| 179 | printf("Error adding control to layout.\n");
|
---|
| 180 | return rc;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[8edec53] | 183 | /* Panel width */
|
---|
| 184 | pw = (arect.p1.x - arect.p0.x) / 2;
|
---|
| 185 |
|
---|
[b36ebb42] | 186 | for (i = 0; i < 2; i++) {
|
---|
[692c7f40] | 187 | rc = panel_create(navigator->window, i == 0,
|
---|
| 188 | &navigator->panel[i]);
|
---|
[b36ebb42] | 189 | if (rc != EOK)
|
---|
| 190 | goto error;
|
---|
| 191 |
|
---|
[8edec53] | 192 | rect.p0.x = arect.p0.x + pw * i;
|
---|
| 193 | rect.p0.y = arect.p0.y + 1;
|
---|
| 194 | rect.p1.x = arect.p0.x + pw * (i + 1);
|
---|
| 195 | rect.p1.y = arect.p1.y - 1;
|
---|
[b36ebb42] | 196 | panel_set_rect(navigator->panel[i], &rect);
|
---|
| 197 |
|
---|
[39ab17c] | 198 | panel_set_cb(navigator->panel[i], &navigator_panel_cb,
|
---|
| 199 | navigator);
|
---|
| 200 |
|
---|
[b36ebb42] | 201 | rc = ui_fixed_add(navigator->fixed,
|
---|
| 202 | panel_ctl(navigator->panel[i]));
|
---|
| 203 | if (rc != EOK) {
|
---|
| 204 | printf("Error adding control to layout.\n");
|
---|
[0e80e40] | 205 | goto error;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | rc = panel_read_dir(navigator->panel[i], ".");
|
---|
| 209 | if (rc != EOK) {
|
---|
| 210 | printf("Error reading directory.\n");
|
---|
| 211 | goto error;
|
---|
[b36ebb42] | 212 | }
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[5d466a1] | 215 | rc = ui_window_paint(navigator->window);
|
---|
[748c8bd] | 216 | if (rc != EOK) {
|
---|
[5d466a1] | 217 | printf("Error painting window.\n");
|
---|
| 218 | goto error;
|
---|
[748c8bd] | 219 | }
|
---|
| 220 |
|
---|
[5d466a1] | 221 | *rnavigator = navigator;
|
---|
| 222 | return EOK;
|
---|
| 223 | error:
|
---|
| 224 | navigator_destroy(navigator);
|
---|
| 225 | return rc;
|
---|
| 226 | }
|
---|
[748c8bd] | 227 |
|
---|
[6aa85c1] | 228 | void navigator_destroy(navigator_t *navigator)
|
---|
[5d466a1] | 229 | {
|
---|
[b36ebb42] | 230 | unsigned i;
|
---|
| 231 |
|
---|
| 232 | for (i = 0; i < 2; i++) {
|
---|
[1aa8c86] | 233 | if (navigator->panel[i] != NULL) {
|
---|
| 234 | ui_fixed_remove(navigator->fixed,
|
---|
| 235 | panel_ctl(navigator->panel[i]));
|
---|
| 236 | panel_destroy(navigator->panel[i]);
|
---|
| 237 | }
|
---|
[b36ebb42] | 238 | }
|
---|
| 239 |
|
---|
[1aa8c86] | 240 | if (navigator->menu != NULL) {
|
---|
| 241 | ui_fixed_remove(navigator->fixed, nav_menu_ctl(navigator->menu));
|
---|
[5d466a1] | 242 | nav_menu_destroy(navigator->menu);
|
---|
[1aa8c86] | 243 | }
|
---|
| 244 |
|
---|
[5d466a1] | 245 | if (navigator->window != NULL)
|
---|
| 246 | ui_window_destroy(navigator->window);
|
---|
| 247 | if (navigator->ui != NULL)
|
---|
| 248 | ui_destroy(navigator->ui);
|
---|
| 249 | free(navigator);
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | /** Run navigator on the specified display. */
|
---|
[6aa85c1] | 253 | errno_t navigator_run(const char *display_spec)
|
---|
[5d466a1] | 254 | {
|
---|
| 255 | navigator_t *navigator;
|
---|
| 256 | errno_t rc;
|
---|
[748c8bd] | 257 |
|
---|
[5d466a1] | 258 | rc = navigator_create(display_spec, &navigator);
|
---|
| 259 | if (rc != EOK)
|
---|
| 260 | return rc;
|
---|
[748c8bd] | 261 |
|
---|
[5d466a1] | 262 | ui_run(navigator->ui);
|
---|
[748c8bd] | 263 |
|
---|
[5d466a1] | 264 | navigator_destroy(navigator);
|
---|
[748c8bd] | 265 | return EOK;
|
---|
| 266 | }
|
---|
| 267 |
|
---|
[692c7f40] | 268 | /** Get the currently active navigator panel.
|
---|
| 269 | *
|
---|
| 270 | * @param navigator Navigator
|
---|
| 271 | * @return Currently active panel
|
---|
| 272 | */
|
---|
| 273 | panel_t *navigator_get_active_panel(navigator_t *navigator)
|
---|
| 274 | {
|
---|
| 275 | int i;
|
---|
| 276 |
|
---|
| 277 | for (i = 0; i < navigator_panels; i++) {
|
---|
| 278 | if (panel_is_active(navigator->panel[i]))
|
---|
| 279 | return navigator->panel[i];
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | /* This should not happen */
|
---|
| 283 | assert(false);
|
---|
| 284 | return NULL;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | /** Switch to another navigator panel.
|
---|
| 288 | *
|
---|
| 289 | * Changes the currently active navigator panel to the next panel.
|
---|
| 290 | *
|
---|
| 291 | * @param navigator Navigator
|
---|
| 292 | */
|
---|
| 293 | void navigator_switch_panel(navigator_t *navigator)
|
---|
| 294 | {
|
---|
[4fcc2de] | 295 | errno_t rc;
|
---|
| 296 |
|
---|
[692c7f40] | 297 | if (panel_is_active(navigator->panel[0])) {
|
---|
[4fcc2de] | 298 | rc = panel_activate(navigator->panel[1]);
|
---|
| 299 | if (rc != EOK)
|
---|
| 300 | return;
|
---|
[692c7f40] | 301 | panel_deactivate(navigator->panel[0]);
|
---|
| 302 | } else {
|
---|
[4fcc2de] | 303 | rc = panel_activate(navigator->panel[0]);
|
---|
| 304 | if (rc != EOK)
|
---|
| 305 | return;
|
---|
[692c7f40] | 306 | panel_deactivate(navigator->panel[1]);
|
---|
| 307 | }
|
---|
| 308 | }
|
---|
| 309 |
|
---|
[f59212cc] | 310 | /** File / Open menu entry selected */
|
---|
| 311 | static void navigator_file_open(void *arg)
|
---|
| 312 | {
|
---|
| 313 | navigator_t *navigator = (navigator_t *)arg;
|
---|
| 314 | panel_t *panel;
|
---|
| 315 |
|
---|
| 316 | panel = navigator_get_active_panel(navigator);
|
---|
[54ddb59] | 317 | ui_file_list_open(panel->flist, ui_file_list_get_cursor(panel->flist));
|
---|
[f59212cc] | 318 | }
|
---|
| 319 |
|
---|
| 320 | /** File / Exit menu entry selected */
|
---|
| 321 | static void navigator_file_exit(void *arg)
|
---|
| 322 | {
|
---|
| 323 | navigator_t *navigator = (navigator_t *)arg;
|
---|
| 324 |
|
---|
| 325 | ui_quit(navigator->ui);
|
---|
| 326 | }
|
---|
| 327 |
|
---|
[39ab17c] | 328 | /** Panel callback requesting panel activation.
|
---|
| 329 | *
|
---|
| 330 | * @param arg Argument (navigator_t *)
|
---|
| 331 | * @param panel Panel
|
---|
| 332 | */
|
---|
| 333 | void navigator_panel_activate_req(void *arg, panel_t *panel)
|
---|
| 334 | {
|
---|
| 335 | navigator_t *navigator = (navigator_t *)arg;
|
---|
| 336 |
|
---|
| 337 | if (!panel_is_active(panel))
|
---|
| 338 | navigator_switch_panel(navigator);
|
---|
| 339 | }
|
---|
| 340 |
|
---|
[748c8bd] | 341 | /** @}
|
---|
| 342 | */
|
---|