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

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since be1d74c1 was 9f7e9bb, checked in by jxsvoboda <5887334+jxsvoboda@…>, 4 years ago

Ctrl-Q to quit Navigator

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