source: mainline/uspace/app/nav/nav.c@ b9d689b

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

Ctrl-Q to quit Navigator

  • Property mode set to 100644
File size: 5.5 KB
RevLine 
[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
49static void wnd_close(ui_window_t *, void *);
[166aba54]50static void wnd_kbd(ui_window_t *, void *, kbd_event_t *);
[e0e1b3d]51
52static 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 */
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
[166aba54]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 if (event->key == KC_Q)
84 ui_quit(navigator->ui);
85 }
86
87 ui_window_def_kbd(window, event);
88}
89
[d097daca]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 */
[68b9e540]96errno_t navigator_create(const char *display_spec,
[d097daca]97 navigator_t **rnavigator)
[e0e1b3d]98{
[d097daca]99 navigator_t *navigator;
[e0e1b3d]100 ui_wnd_params_t params;
[0e5ed803]101 gfx_rect_t rect;
102 unsigned i;
[e0e1b3d]103 errno_t rc;
104
[d097daca]105 navigator = calloc(1, sizeof(navigator_t));
106 if (navigator == NULL)
107 return ENOMEM;
108
109 rc = ui_create(display_spec, &navigator->ui);
[e0e1b3d]110 if (rc != EOK) {
111 printf("Error creating UI on display %s.\n", display_spec);
[d097daca]112 goto error;
[e0e1b3d]113 }
114
115 ui_wnd_params_init(&params);
116 params.caption = "Navigator";
[d097daca]117 params.style &= ~ui_wds_decorated;
118 params.placement = ui_wnd_place_full_screen;
[e0e1b3d]119
[d097daca]120 rc = ui_window_create(navigator->ui, &params, &navigator->window);
[e0e1b3d]121 if (rc != EOK) {
122 printf("Error creating window.\n");
[d097daca]123 goto error;
[e0e1b3d]124 }
125
[d097daca]126 ui_window_set_cb(navigator->window, &window_cb, (void *) navigator);
[e0e1b3d]127
[d097daca]128 rc = ui_fixed_create(&navigator->fixed);
[e0e1b3d]129 if (rc != EOK) {
130 printf("Error creating fixed layout.\n");
[d097daca]131 goto error;
[e0e1b3d]132 }
133
[d097daca]134 ui_window_add(navigator->window, ui_fixed_ctl(navigator->fixed));
[e0e1b3d]135
[68b9e540]136 rc = nav_menu_create(navigator->window, &navigator->menu);
[d097daca]137 if (rc != EOK)
138 goto error;
[e0e1b3d]139
[68b9e540]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
[0e5ed803]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");
[8e5f39d]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;
[0e5ed803]168 }
169 }
170
[d097daca]171 rc = ui_window_paint(navigator->window);
[e0e1b3d]172 if (rc != EOK) {
[d097daca]173 printf("Error painting window.\n");
174 goto error;
[e0e1b3d]175 }
176
[d097daca]177 *rnavigator = navigator;
178 return EOK;
179error:
180 navigator_destroy(navigator);
181 return rc;
182}
[e0e1b3d]183
[68b9e540]184void navigator_destroy(navigator_t *navigator)
[d097daca]185{
[0e5ed803]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
[68b9e540]194 ui_fixed_remove(navigator->fixed, nav_menu_ctl(navigator->menu));
195
[d097daca]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. */
[68b9e540]206errno_t navigator_run(const char *display_spec)
[d097daca]207{
208 navigator_t *navigator;
209 errno_t rc;
[e0e1b3d]210
[d097daca]211 rc = navigator_create(display_spec, &navigator);
212 if (rc != EOK)
213 return rc;
[e0e1b3d]214
[d097daca]215 ui_run(navigator->ui);
[e0e1b3d]216
[d097daca]217 navigator_destroy(navigator);
[e0e1b3d]218 return EOK;
219}
220
221/** @}
222 */
Note: See TracBrowser for help on using the repository browser.