Changes in uspace/app/nav/nav.c [54ddb59:b336bfd8] in mainline


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/nav/nav.c

    r54ddb59 rb336bfd8  
    11/*
    2  * Copyright (c) 2022 Jiri Svoboda
     2 * Copyright (c) 2025 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3939#include <stdlib.h>
    4040#include <str.h>
     41#include <task.h>
    4142#include <ui/fixed.h>
    4243#include <ui/filelist.h>
     
    4849#include "panel.h"
    4950
     51#define EDITOR_CMD "/app/edit"
     52
    5053static void wnd_close(ui_window_t *, void *);
    5154static void wnd_kbd(ui_window_t *, void *, kbd_event_t *);
     
    5760
    5861static void navigator_file_open(void *);
     62static void navigator_file_edit(void *);
    5963static void navigator_file_exit(void *);
    6064
    6165static nav_menu_cb_t navigator_menu_cb = {
    6266        .file_open = navigator_file_open,
     67        .file_edit = navigator_file_edit,
    6368        .file_exit = navigator_file_exit
    6469};
    6570
    6671static void navigator_panel_activate_req(void *, panel_t *);
     72static void navigator_panel_file_open(void *, panel_t *, const char *);
    6773
    6874static panel_cb_t navigator_panel_cb = {
    69         .activate_req = navigator_panel_activate_req
     75        .activate_req = navigator_panel_activate_req,
     76        .file_open = navigator_panel_file_open
    7077};
    7178
     
    97104            (event->mods & KM_CTRL) != 0) {
    98105                switch (event->key) {
     106                case KC_E:
     107                        navigator_file_edit((void *)navigator);
     108                        break;
    99109                case KC_Q:
    100110                        ui_quit(navigator->ui);
     
    318328}
    319329
     330/** Open file in text editor.
     331 *
     332 * @param navigator Navigator
     333 * @param fname File name
     334 *
     335 * @return EOK on success or an error code
     336 */
     337static errno_t navigator_edit_file(navigator_t *navigator, const char *fname)
     338{
     339        task_id_t id;
     340        task_wait_t wait;
     341        task_exit_t texit;
     342        int retval;
     343        errno_t rc;
     344
     345        /* Free up and clean console for the child task. */
     346        rc = ui_suspend(navigator->ui);
     347        if (rc != EOK)
     348                return rc;
     349
     350        rc = task_spawnl(&id, &wait, EDITOR_CMD, EDITOR_CMD, fname, NULL);
     351        if (rc != EOK)
     352                goto error;
     353
     354        rc = task_wait(&wait, &texit, &retval);
     355        if ((rc != EOK) || (texit != TASK_EXIT_NORMAL))
     356                goto error;
     357
     358        /* Resume UI operation */
     359        rc = ui_resume(navigator->ui);
     360        if (rc != EOK)
     361                return rc;
     362
     363        (void) ui_paint(navigator->ui);
     364        return EOK;
     365error:
     366        (void) ui_resume(navigator->ui);
     367        (void) ui_paint(navigator->ui);
     368        return rc;
     369}
     370
     371/** Execute file entry.
     372 *
     373 * @param navigator Navigator
     374 * @param fname File name
     375 *
     376 * @return EOK on success or an error code
     377 */
     378static errno_t navigator_exec_file(navigator_t *navigator, const char *fname)
     379{
     380        task_id_t id;
     381        task_wait_t wait;
     382        task_exit_t texit;
     383        int retval;
     384        errno_t rc;
     385
     386        /* Free up and clean console for the child task. */
     387        rc = ui_suspend(navigator->ui);
     388        if (rc != EOK)
     389                return rc;
     390
     391        rc = task_spawnl(&id, &wait, fname, fname, NULL);
     392        if (rc != EOK)
     393                goto error;
     394
     395        rc = task_wait(&wait, &texit, &retval);
     396        if ((rc != EOK) || (texit != TASK_EXIT_NORMAL))
     397                goto error;
     398
     399        /* Resume UI operation */
     400        rc = ui_resume(navigator->ui);
     401        if (rc != EOK)
     402                return rc;
     403
     404        (void) ui_paint(navigator->ui);
     405        return EOK;
     406error:
     407        (void) ui_resume(navigator->ui);
     408        (void) ui_paint(navigator->ui);
     409        return rc;
     410}
     411
     412/** Open panel file entry.
     413 *
     414 * Perform Open action on a file entry (based on extension).
     415 *
     416 * @param navigator Navigator
     417 * @param fname File name
     418 *
     419 * @return EOK on success or an error code
     420 */
     421static errno_t navigator_open_file(navigator_t *navigator, const char *fname)
     422{
     423        const char *ext;
     424
     425        ext = str_rchr(fname, '.');
     426        if (ext != NULL) {
     427                if (str_casecmp(ext, ".txt") == 0)
     428                        return navigator_edit_file(navigator, fname);
     429        }
     430
     431        return navigator_exec_file(navigator, fname);
     432}
     433
     434/** File / Edit menu entry selected */
     435static void navigator_file_edit(void *arg)
     436{
     437        navigator_t *navigator = (navigator_t *)arg;
     438        ui_file_list_entry_t *entry;
     439        ui_file_list_entry_attr_t attr;
     440        panel_t *panel;
     441
     442        panel = navigator_get_active_panel(navigator);
     443        entry = ui_file_list_get_cursor(panel->flist);
     444        ui_file_list_entry_get_attr(entry, &attr);
     445
     446        (void)navigator_edit_file(navigator, attr.name);
     447}
     448
    320449/** File / Exit menu entry selected */
    321450static void navigator_file_exit(void *arg)
     
    339468}
    340469
     470/** Panel callback requesting file open.
     471 *
     472 * @param arg Argument (navigator_t *)
     473 * @param panel Panel
     474 * @param fname File name
     475 */
     476void navigator_panel_file_open(void *arg, panel_t *panel, const char *fname)
     477{
     478        navigator_t *navigator = (navigator_t *)arg;
     479
     480        (void)panel;
     481        navigator_open_file(navigator, fname);
     482}
     483
    341484/** @}
    342485 */
Note: See TracChangeset for help on using the changeset viewer.