Changeset 0e80e40 in mainline


Ignore:
Timestamp:
2021-10-25T00:32:45Z (2 years ago)
Author:
jxsvoboda <5887334+jxsvoboda@…>
Branches:
master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
9f7e9bb
Parents:
61784ed
git-author:
Jiri Svoboda <jiri@…> (2021-10-07 17:17:36)
git-committer:
jxsvoboda <5887334+jxsvoboda@…> (2021-10-25 00:32:45)
Message:

Read and display directory contents

Location:
uspace
Files:
5 edited

Legend:

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

    r61784ed r0e80e40  
    136136                if (rc != EOK) {
    137137                        printf("Error adding control to layout.\n");
    138                         return rc;
     138                        goto error;
     139                }
     140
     141                rc = panel_read_dir(navigator->panel[i], ".");
     142                if (rc != EOK) {
     143                        printf("Error reading directory.\n");
     144                        goto error;
    139145                }
    140146        }
  • uspace/app/nav/panel.c

    r61784ed r0e80e40  
    3535 */
    3636
     37#include <dirent.h>
    3738#include <errno.h>
    3839#include <gfx/render.h>
     40#include <gfx/text.h>
    3941#include <stdlib.h>
    4042#include <str.h>
    4143#include <ui/control.h>
    4244#include <ui/paint.h>
     45#include <ui/resource.h>
    4346#include "panel.h"
    4447#include "nav.h"
     
    8184                goto error;
    8285
     86        rc = gfx_color_new_ega(0x30, &panel->curs_color);
     87        if (rc != EOK)
     88                goto error;
     89
    8390        panel->window = window;
    8491        list_initialize(&panel->entries);
     
    8693        return EOK;
    8794error:
     95        if (panel->color != NULL)
     96                gfx_color_delete(panel->color);
     97        if (panel->curs_color != NULL)
     98                gfx_color_delete(panel->curs_color);
    8899        ui_control_delete(panel->control);
    89100        free(panel);
     
    97108void panel_destroy(panel_t *panel)
    98109{
    99         panel_entry_t *entry;
    100 
    101         entry = panel_first(panel);
    102         while (entry != NULL) {
    103                 panel_entry_delete(entry);
    104                 entry = panel_first(panel);
    105         }
    106 
     110        gfx_color_delete(panel->color);
     111        gfx_color_delete(panel->curs_color);
     112        panel_clear_entries(panel);
    107113        ui_control_delete(panel->control);
    108114        free(panel);
     
    117123        gfx_context_t *gc = ui_window_get_gc(panel->window);
    118124        ui_resource_t *res = ui_window_get_res(panel->window);
     125        gfx_font_t *font = ui_resource_get_font(res);
     126        gfx_text_fmt_t fmt;
     127        panel_entry_t *entry;
     128        gfx_coord2_t pos;
     129        gfx_rect_t rect;
    119130        errno_t rc;
     131
     132        gfx_text_fmt_init(&fmt);
    120133
    121134        rc = gfx_set_color(gc, panel->color);
     
    131144        if (rc != EOK)
    132145                return rc;
     146
     147        pos.x = panel->rect.p0.x + 1;
     148        pos.y = panel->rect.p0.y + 1;
     149
     150        entry = panel->page;
     151        while (entry != NULL && pos.y < panel->rect.p1.y - 1) {
     152                if (entry == panel->cursor) {
     153                        /* Draw cursor background */
     154                        rect.p0 = pos;
     155                        rect.p1.x = panel->rect.p1.x - 1;
     156                        rect.p1.y = rect.p0.y + 1;
     157
     158                        rc = gfx_set_color(gc, panel->curs_color);
     159                        if (rc != EOK)
     160                                return rc;
     161
     162                        rc = gfx_fill_rect(gc, &rect);
     163                        if (rc != EOK)
     164                                return rc;
     165
     166                        fmt.color = panel->curs_color;
     167                } else {
     168                        fmt.color = panel->color;
     169                }
     170
     171                rc = gfx_puttext(font, &pos, &fmt, entry->name);
     172                if (rc != EOK)
     173                        return rc;
     174
     175                pos.y++;
     176                entry = panel_next(entry);
     177        }
    133178
    134179        rc = gfx_update(gc);
     
    240285void panel_entry_delete(panel_entry_t *entry)
    241286{
     287        if (entry->panel->cursor == entry)
     288                entry->panel->cursor = NULL;
     289        if (entry->panel->page == entry)
     290                entry->panel->page = NULL;
     291
    242292        list_remove(&entry->lentries);
    243293        free(entry->name);
     
    245295}
    246296
     297/** Clear panel entry list.
     298 *
     299 * @param panel Panel
     300 */
     301void panel_clear_entries(panel_t *panel)
     302{
     303        panel_entry_t *entry;
     304
     305        entry = panel_first(panel);
     306        while (entry != NULL) {
     307                panel_entry_delete(entry);
     308                entry = panel_first(panel);
     309        }
     310}
     311
     312/** Read directory into panel entry list.
     313 *
     314 * @param panel Panel
     315 * @param dirname Directory path
     316 * @return EOK on success or an error code
     317 */
     318errno_t panel_read_dir(panel_t *panel, const char *dirname)
     319{
     320        DIR *dir;
     321        struct dirent *dirent;
     322        errno_t rc;
     323
     324        dir = opendir(dirname);
     325        if (dir == NULL)
     326                return errno;
     327
     328        dirent = readdir(dir);
     329        while (dirent != NULL) {
     330                rc = panel_entry_append(panel, dirent->d_name, 1);
     331                if (rc != EOK)
     332                        goto error;
     333                dirent = readdir(dir);
     334        }
     335
     336        closedir(dir);
     337
     338        panel->cursor = panel_first(panel);
     339        panel->page = panel_first(panel);
     340        return EOK;
     341error:
     342        closedir(dir);
     343        return rc;
     344}
     345
    247346/** Return first panel entry.
    248347 *
  • uspace/app/nav/panel.h

    r61784ed r0e80e40  
    4848#include "panel.h"
    4949
     50/** Panel entry */
     51typedef struct {
     52        /** Containing panel */
     53        struct panel *panel;
     54        /** Link to @c panel->entries */
     55        link_t lentries;
     56        /** File name */
     57        char *name;
     58        /** File size */
     59        uint64_t size;
     60} panel_entry_t;
     61
    5062/** Navigator panel
    5163 *
     
    6577        gfx_color_t *color;
    6678
     79        /** Panel cursor color */
     80        gfx_color_t *curs_color;
     81
    6782        /** Panel entries (list of panel_entry_t) */
    6883        list_t entries;
     84
     85        /** First entry of current page */
     86        panel_entry_t *page;
     87
     88        /** Cursor position */
     89        panel_entry_t *cursor;
    6990} panel_t;
    70 
    71 /** Panel entry */
    72 typedef struct {
    73         /** Containing panel */
    74         panel_t *panel;
    75         /** Link to @c panel->entries */
    76         link_t lentries;
    77         /** File name */
    78         char *name;
    79         /** File size */
    80         uint64_t size;
    81 } panel_entry_t;
    8291
    8392extern errno_t panel_create(ui_window_t *, panel_t **);
     
    8998extern errno_t panel_entry_append(panel_t *, const char *, uint64_t);
    9099extern void panel_entry_delete(panel_entry_t *);
     100extern void panel_clear_entries(panel_t *);
     101extern errno_t panel_read_dir(panel_t *, const char *);
    91102extern panel_entry_t *panel_first(panel_t *);
    92103extern panel_entry_t *panel_next(panel_entry_t *);
  • uspace/app/nav/test/panel.c

    r61784ed r0e80e40  
    2929#include <errno.h>
    3030#include <pcut/pcut.h>
     31#include <stdio.h>
     32#include <vfs/vfs.h>
    3133#include "../panel.h"
    3234
     
    194196}
    195197
     198/** panel_clear_entries() removes all entries from panel */
     199PCUT_TEST(clear_entries)
     200{
     201        panel_t *panel;
     202        errno_t rc;
     203
     204        rc = panel_create(NULL, &panel);
     205        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     206
     207        rc = panel_entry_append(panel, "a", 1);
     208        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     209
     210        rc = panel_entry_append(panel, "b", 2);
     211        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     212
     213        PCUT_ASSERT_INT_EQUALS(2, list_count(&panel->entries));
     214
     215        panel_clear_entries(panel);
     216        PCUT_ASSERT_INT_EQUALS(0, list_count(&panel->entries));
     217
     218        panel_destroy(panel);
     219}
     220
     221/** panel_read_dir() reads the contents of a directory */
     222PCUT_TEST(read_dir)
     223{
     224        panel_t *panel;
     225        panel_entry_t *entry;
     226        char buf[L_tmpnam];
     227        char *fname;
     228        char *p;
     229        errno_t rc;
     230        FILE *f;
     231        int rv;
     232
     233        /* Create name for temporary directory */
     234        p = tmpnam(buf);
     235        PCUT_ASSERT_NOT_NULL(p);
     236
     237        /* Create temporary directory */
     238        rc = vfs_link_path(p, KIND_DIRECTORY, NULL);
     239        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     240
     241        rv = asprintf(&fname, "%s/%s", p, "a");
     242        PCUT_ASSERT_TRUE(rv >= 0);
     243
     244        f = fopen(fname, "wb");
     245        PCUT_ASSERT_NOT_NULL(f);
     246
     247        rv = fprintf(f, "X");
     248        PCUT_ASSERT_TRUE(rv >= 0);
     249
     250        rv = fclose(f);
     251        PCUT_ASSERT_INT_EQUALS(0, rv);
     252
     253        rc = panel_create(NULL, &panel);
     254        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     255
     256        rc = panel_read_dir(panel, p);
     257        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     258
     259        PCUT_ASSERT_INT_EQUALS(1, list_count(&panel->entries));
     260
     261        entry = panel_first(panel);
     262        PCUT_ASSERT_NOT_NULL(entry);
     263        PCUT_ASSERT_STR_EQUALS("a", entry->name);
     264        // PCUT_ASSERT_INT_EQUALS(1, entry->size);
     265
     266        panel_destroy(panel);
     267
     268        rv = remove(fname);
     269        PCUT_ASSERT_INT_EQUALS(0, rv);
     270
     271        rv = remove(p);
     272        PCUT_ASSERT_INT_EQUALS(0, rv);
     273        free(fname);
     274}
     275
    196276/** panel_first() returns valid entry or @c NULL as appropriate */
    197277PCUT_TEST(first)
  • uspace/lib/gfxfont/include/gfx/text.h

    r61784ed r0e80e40  
    3838
    3939#include <errno.h>
     40#include <stddef.h>
    4041#include <types/gfx/coord.h>
    4142#include <types/gfx/font.h>
Note: See TracChangeset for help on using the changeset viewer.