[748c8bd] | 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>
|
---|
[5d466a1] | 39 | #include <stdlib.h>
|
---|
[748c8bd] | 40 | #include <str.h>
|
---|
| 41 | #include <ui/fixed.h>
|
---|
| 42 | #include <ui/resource.h>
|
---|
| 43 | #include <ui/ui.h>
|
---|
| 44 | #include <ui/window.h>
|
---|
[5d466a1] | 45 | #include "menu.h"
|
---|
[748c8bd] | 46 | #include "nav.h"
|
---|
[b36ebb42] | 47 | #include "panel.h"
|
---|
[748c8bd] | 48 |
|
---|
| 49 | static void wnd_close(ui_window_t *, void *);
|
---|
[9f7e9bb] | 50 | static void wnd_kbd(ui_window_t *, void *, kbd_event_t *);
|
---|
[748c8bd] | 51 |
|
---|
| 52 | static ui_window_cb_t window_cb = {
|
---|
[9f7e9bb] | 53 | .close = wnd_close,
|
---|
| 54 | .kbd = wnd_kbd
|
---|
[748c8bd] | 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 |
|
---|
[9f7e9bb] | 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) {
|
---|
| 83 | if (event->key == KC_Q)
|
---|
| 84 | ui_quit(navigator->ui);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | ui_window_def_kbd(window, event);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[5d466a1] | 90 | /** Create navigator.
|
---|
| 91 | *
|
---|
| 92 | * @param display_spec Display specification
|
---|
| 93 | * @param rnavigator Place to store pointer to new navigator
|
---|
| 94 | * @return EOK on success or ane error code
|
---|
| 95 | */
|
---|
[6aa85c1] | 96 | errno_t navigator_create(const char *display_spec,
|
---|
[5d466a1] | 97 | navigator_t **rnavigator)
|
---|
[748c8bd] | 98 | {
|
---|
[5d466a1] | 99 | navigator_t *navigator;
|
---|
[748c8bd] | 100 | ui_wnd_params_t params;
|
---|
[b36ebb42] | 101 | gfx_rect_t rect;
|
---|
| 102 | unsigned i;
|
---|
[748c8bd] | 103 | errno_t rc;
|
---|
| 104 |
|
---|
[5d466a1] | 105 | navigator = calloc(1, sizeof(navigator_t));
|
---|
| 106 | if (navigator == NULL)
|
---|
| 107 | return ENOMEM;
|
---|
| 108 |
|
---|
| 109 | rc = ui_create(display_spec, &navigator->ui);
|
---|
[748c8bd] | 110 | if (rc != EOK) {
|
---|
| 111 | printf("Error creating UI on display %s.\n", display_spec);
|
---|
[5d466a1] | 112 | goto error;
|
---|
[748c8bd] | 113 | }
|
---|
| 114 |
|
---|
| 115 | ui_wnd_params_init(¶ms);
|
---|
| 116 | params.caption = "Navigator";
|
---|
[5d466a1] | 117 | params.style &= ~ui_wds_decorated;
|
---|
| 118 | params.placement = ui_wnd_place_full_screen;
|
---|
[748c8bd] | 119 |
|
---|
[5d466a1] | 120 | rc = ui_window_create(navigator->ui, ¶ms, &navigator->window);
|
---|
[748c8bd] | 121 | if (rc != EOK) {
|
---|
| 122 | printf("Error creating window.\n");
|
---|
[5d466a1] | 123 | goto error;
|
---|
[748c8bd] | 124 | }
|
---|
| 125 |
|
---|
[5d466a1] | 126 | ui_window_set_cb(navigator->window, &window_cb, (void *) navigator);
|
---|
[748c8bd] | 127 |
|
---|
[5d466a1] | 128 | rc = ui_fixed_create(&navigator->fixed);
|
---|
[748c8bd] | 129 | if (rc != EOK) {
|
---|
| 130 | printf("Error creating fixed layout.\n");
|
---|
[5d466a1] | 131 | goto error;
|
---|
[748c8bd] | 132 | }
|
---|
| 133 |
|
---|
[5d466a1] | 134 | ui_window_add(navigator->window, ui_fixed_ctl(navigator->fixed));
|
---|
[748c8bd] | 135 |
|
---|
[6aa85c1] | 136 | rc = nav_menu_create(navigator->window, &navigator->menu);
|
---|
[5d466a1] | 137 | if (rc != EOK)
|
---|
| 138 | goto error;
|
---|
[748c8bd] | 139 |
|
---|
[6aa85c1] | 140 | rc = ui_fixed_add(navigator->fixed, nav_menu_ctl(navigator->menu));
|
---|
| 141 | if (rc != EOK) {
|
---|
| 142 | printf("Error adding control to layout.\n");
|
---|
| 143 | return rc;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[b36ebb42] | 146 | for (i = 0; i < 2; i++) {
|
---|
| 147 | rc = panel_create(navigator->window, &navigator->panel[i]);
|
---|
| 148 | if (rc != EOK)
|
---|
| 149 | goto error;
|
---|
| 150 |
|
---|
| 151 | rect.p0.x = 40 * i;
|
---|
| 152 | rect.p0.y = 1;
|
---|
| 153 | rect.p1.x = 40 * (i + 1);
|
---|
| 154 | rect.p1.y = 24;
|
---|
| 155 | panel_set_rect(navigator->panel[i], &rect);
|
---|
| 156 |
|
---|
| 157 | rc = ui_fixed_add(navigator->fixed,
|
---|
| 158 | panel_ctl(navigator->panel[i]));
|
---|
| 159 | if (rc != EOK) {
|
---|
| 160 | printf("Error adding control to layout.\n");
|
---|
[0e80e40] | 161 | goto error;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | rc = panel_read_dir(navigator->panel[i], ".");
|
---|
| 165 | if (rc != EOK) {
|
---|
| 166 | printf("Error reading directory.\n");
|
---|
| 167 | goto error;
|
---|
[b36ebb42] | 168 | }
|
---|
| 169 | }
|
---|
| 170 |
|
---|
[5d466a1] | 171 | rc = ui_window_paint(navigator->window);
|
---|
[748c8bd] | 172 | if (rc != EOK) {
|
---|
[5d466a1] | 173 | printf("Error painting window.\n");
|
---|
| 174 | goto error;
|
---|
[748c8bd] | 175 | }
|
---|
| 176 |
|
---|
[5d466a1] | 177 | *rnavigator = navigator;
|
---|
| 178 | return EOK;
|
---|
| 179 | error:
|
---|
| 180 | navigator_destroy(navigator);
|
---|
| 181 | return rc;
|
---|
| 182 | }
|
---|
[748c8bd] | 183 |
|
---|
[6aa85c1] | 184 | void navigator_destroy(navigator_t *navigator)
|
---|
[5d466a1] | 185 | {
|
---|
[b36ebb42] | 186 | unsigned i;
|
---|
| 187 |
|
---|
| 188 | for (i = 0; i < 2; i++) {
|
---|
| 189 | ui_fixed_remove(navigator->fixed,
|
---|
| 190 | panel_ctl(navigator->panel[i]));
|
---|
| 191 | panel_destroy(navigator->panel[i]);
|
---|
| 192 | }
|
---|
| 193 |
|
---|
[6aa85c1] | 194 | ui_fixed_remove(navigator->fixed, nav_menu_ctl(navigator->menu));
|
---|
| 195 |
|
---|
[5d466a1] | 196 | if (navigator->menu != NULL)
|
---|
| 197 | nav_menu_destroy(navigator->menu);
|
---|
| 198 | if (navigator->window != NULL)
|
---|
| 199 | ui_window_destroy(navigator->window);
|
---|
| 200 | if (navigator->ui != NULL)
|
---|
| 201 | ui_destroy(navigator->ui);
|
---|
| 202 | free(navigator);
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | /** Run navigator on the specified display. */
|
---|
[6aa85c1] | 206 | errno_t navigator_run(const char *display_spec)
|
---|
[5d466a1] | 207 | {
|
---|
| 208 | navigator_t *navigator;
|
---|
| 209 | errno_t rc;
|
---|
[748c8bd] | 210 |
|
---|
[5d466a1] | 211 | rc = navigator_create(display_spec, &navigator);
|
---|
| 212 | if (rc != EOK)
|
---|
| 213 | return rc;
|
---|
[748c8bd] | 214 |
|
---|
[5d466a1] | 215 | ui_run(navigator->ui);
|
---|
[748c8bd] | 216 |
|
---|
[5d466a1] | 217 | navigator_destroy(navigator);
|
---|
[748c8bd] | 218 | return EOK;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | /** @}
|
---|
| 222 | */
|
---|