Changeset 9bec33a in mainline


Ignore:
Timestamp:
2023-10-09T11:13:19Z (7 months ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
master, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
983052c
Parents:
7d78e466
git-author:
Jiri Svoboda <jiri@…> (2023-10-08 17:13:00)
git-committer:
Jiri Svoboda <jiri@…> (2023-10-09 11:13:19)
Message:

It's a start!

Location:
uspace/app/taskbar
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/taskbar/startmenu.sif

    r7d78e466 r9bec33a  
    1 [sif](){[entries](){[entry]([caption]=[~N~avigator][cmd]=[/app/nav]){}[entry]([caption]=[Text ~E~ditor][cmd]=[/app/edit]){}[entry]([caption]=[~T~erminal][cmd]=[/app/terminal]){}[entry]([caption]=[~C~alculator][cmd]=[/app/calculator]){}[entry]([caption]=[~U~I Demo][cmd]=[/app/uidemo]){}[entry]([caption]=[~G~FX Demo][cmd]=[/app/gfxdemo]){}}}
     1[sif](){[entries](){[entry]([caption]=[~N~avigator][cmd]=[/app/terminal -c /app/nav]){}[entry]([caption]=[Text ~E~ditor][cmd]=[/app/terminal -c /app/edit]){}[entry]([caption]=[~T~erminal][cmd]=[/app/terminal]){}[entry]([caption]=[~C~alculator][cmd]=[/app/calculator]){}[entry]([caption]=[~U~I Demo][cmd]=[/app/uidemo]){}[entry]([caption]=[~G~FX Demo][cmd]=[/app/gfxdemo ui]){}}}
  • uspace/app/taskbar/tbsmenu.c

    r7d78e466 r9bec33a  
    3838#include <stddef.h>
    3939#include <stdlib.h>
     40#include <str.h>
     41#include <task.h>
    4042#include <ui/fixed.h>
    4143#include <ui/menu.h>
     
    5961        .clicked = tbsmenu_button_clicked
    6062};
     63
     64static void tbsmenu_smenu_entry_cb(ui_menu_entry_t *, void *);
     65static errno_t tbsmenu_entry_start(tbsmenu_entry_t *);
    6166
    6267/** Create task bar start menu.
     
    119124errno_t tbsmenu_load(tbsmenu_t *tbsmenu, const char *repopath)
    120125{
    121         ui_menu_entry_t *tentry;
     126        tbsmenu_entry_t *tentry;
    122127        startmenu_t *smenu = NULL;
    123128        startmenu_entry_t *sme;
     
    135140                cmd = startmenu_entry_get_cmd(sme);
    136141
    137                 rc = ui_menu_entry_create(tbsmenu->smenu, caption, "", &tentry);
     142                rc = tbsmenu_add(tbsmenu, caption, cmd, &tentry);
    138143                if (rc != EOK)
    139144                        goto error;
    140145
    141                 (void)cmd;
     146                (void)tentry;
    142147
    143148                sme = startmenu_next(sme);
     
    171176        tbsmenu_entry_t *entry;
    172177
    173         // TODO Close libstartmenu
    174 
    175178        /* Destroy entries */
    176179        entry = tbsmenu_first(tbsmenu);
    177180        while (entry != NULL) {
    178                 (void)tbsmenu_remove(tbsmenu, entry, false);
     181                tbsmenu_remove(tbsmenu, entry, false);
    179182                entry = tbsmenu_first(tbsmenu);
    180183        }
     
    187190}
    188191
    189 /** Remove entry from strat menu.
     192/** Add entry to start menu.
     193 *
     194 * @param tbsmenu Start menu
     195 * @param caption Caption
     196 * @param cmd Command to run
     197 * @param entry Start menu entry
     198 * @return @c EOK on success or an error code
     199 */
     200errno_t tbsmenu_add(tbsmenu_t *tbsmenu, const char *caption,
     201    const char *cmd, tbsmenu_entry_t **rentry)
     202{
     203        errno_t rc;
     204        tbsmenu_entry_t *entry;
     205
     206        entry = calloc(1, sizeof(tbsmenu_entry_t));
     207        if (entry == NULL)
     208                return ENOMEM;
     209
     210        entry->caption = str_dup(caption);
     211        if (entry->caption == NULL) {
     212                rc = ENOMEM;
     213                goto error;
     214        }
     215
     216        entry->cmd = str_dup(cmd);
     217        if (entry->cmd == NULL) {
     218                rc = ENOMEM;
     219                goto error;
     220        }
     221
     222        rc = ui_menu_entry_create(tbsmenu->smenu, caption, "", &entry->mentry);
     223        if (rc != EOK)
     224                goto error;
     225
     226        ui_menu_entry_set_cb(entry->mentry, tbsmenu_smenu_entry_cb,
     227            (void *)entry);
     228
     229        entry->tbsmenu = tbsmenu;
     230        list_append(&entry->lentries, &tbsmenu->entries);
     231        *rentry = entry;
     232        return EOK;
     233error:
     234        if (entry->caption != NULL)
     235                free(entry->caption);
     236        if (entry->cmd != NULL)
     237                free(entry->cmd);
     238        free(entry);
     239        return rc;
     240}
     241
     242/** Remove entry from start menu.
    190243 *
    191244 * @param tbsmenu Start menu
    192245 * @param entry Start menu entry
    193246 * @param paint @c true to repaint start menu
    194  * @return @c EOK on success or an error code
    195  */
    196 errno_t tbsmenu_remove(tbsmenu_t *tbsmenu, tbsmenu_entry_t *entry,
     247 */
     248void tbsmenu_remove(tbsmenu_t *tbsmenu, tbsmenu_entry_t *entry,
    197249    bool paint)
    198250{
    199         errno_t rc = EOK;
    200 
    201251        assert(entry->tbsmenu == tbsmenu);
    202252
    203253        list_remove(&entry->lentries);
    204254
    205         // TODO Destroy menu entry
     255        ui_menu_entry_destroy(entry->mentry);
     256        free(entry->caption);
     257        free(entry->cmd);
    206258        free(entry);
    207         return rc;
    208259}
    209260
     
    220271        (void)tbsmenu;
    221272        ui_menu_close(menu);
     273}
     274
     275/** Start menu entry was activated.
     276 *
     277 * @param smentry Start menu entry
     278 * @param arg Argument (tbsmenu_entry_t *)
     279 */
     280static void tbsmenu_smenu_entry_cb(ui_menu_entry_t *smentry, void *arg)
     281{
     282        tbsmenu_entry_t *entry = (tbsmenu_entry_t *)arg;
     283
     284        (void)tbsmenu_entry_start(entry);
    222285}
    223286
     
    299362}
    300363
     364/** Split command string into individual parts.
     365 *
     366 * Command arguments are separated by spaces. There is no way
     367 * to provide an argument containing spaces.
     368 *
     369 * @param str Command with arguments separated by spaces
     370 * @param cmd Command structure to fill in
     371 * @return EOK on success or an error code
     372 */
     373static errno_t tbsmenu_cmd_split(const char *str, tbsmenu_cmd_t *cmd)
     374{
     375        char *arg;
     376        char *next;
     377        size_t cnt;
     378
     379        cmd->buf = str_dup(str);
     380        if (cmd->buf == NULL)
     381                return ENOMEM;
     382
     383        /* Count the entries */
     384        cnt = 0;
     385        arg = str_tok(cmd->buf, " ", &next);
     386        while (arg != NULL) {
     387                ++cnt;
     388                arg = str_tok(next, " ", &next);
     389        }
     390
     391        /* Need to copy again as buf was mangled */
     392        free(cmd->buf);
     393        cmd->buf = str_dup(str);
     394        if (cmd->buf == NULL)
     395                return ENOMEM;
     396
     397        cmd->argv = calloc(cnt + 1, sizeof(char *));
     398        if (cmd->argv == NULL) {
     399                free(cmd->buf);
     400                return ENOMEM;
     401        }
     402
     403        /* Fill in pointers */
     404        cnt = 0;
     405        arg = str_tok(cmd->buf, " ", &next);
     406        while (arg != NULL) {
     407                cmd->argv[cnt++] = arg;
     408                arg = str_tok(next, " ", &next);
     409        }
     410
     411        return EOK;
     412}
     413
     414/** Free command structure.
     415 *
     416 * @param cmd Command
     417 */
     418static void tbsmenu_cmd_fini(tbsmenu_cmd_t *cmd)
     419{
     420        free(cmd->argv);
     421        free(cmd->buf);
     422}
     423
     424/** Execute start menu entry.
     425 *
     426 * @param entry Start menu entry
     427 *
     428 * @return EOK on success or an error code
     429 */
     430static errno_t tbsmenu_entry_start(tbsmenu_entry_t *entry)
     431{
     432        task_id_t id;
     433        task_wait_t wait;
     434        task_exit_t texit;
     435        tbsmenu_cmd_t cmd;
     436        int retval;
     437        bool suspended;
     438        errno_t rc;
     439        ui_t *ui;
     440
     441        ui = ui_window_get_ui(entry->tbsmenu->window);
     442        suspended = false;
     443
     444        rc = tbsmenu_cmd_split(entry->cmd, &cmd);
     445        if (rc != EOK)
     446                return rc;
     447
     448        /* Free up and clean console for the child task. */
     449        rc = ui_suspend(ui);
     450        if (rc != EOK)
     451                goto error;
     452
     453        suspended = true;
     454
     455        rc = task_spawnv(&id, &wait, cmd.argv[0], (const char *const *)
     456            cmd.argv);
     457        if (rc != EOK)
     458                goto error;
     459
     460        rc = task_wait(&wait, &texit, &retval);
     461        if ((rc != EOK) || (texit != TASK_EXIT_NORMAL))
     462                goto error;
     463
     464        /* Resume UI operation */
     465        rc = ui_resume(ui);
     466        if (rc != EOK)
     467                goto error;
     468
     469        (void) ui_paint(ui);
     470        return EOK;
     471error:
     472        tbsmenu_cmd_fini(&cmd);
     473        if (suspended)
     474                (void) ui_resume(ui);
     475        (void) ui_paint(ui);
     476        return rc;
     477}
     478
    301479/** @}
    302480 */
  • uspace/app/taskbar/tbsmenu.h

    r7d78e466 r9bec33a  
    5050extern void tbsmenu_set_rect(tbsmenu_t *, gfx_rect_t *);
    5151extern void tbsmenu_destroy(tbsmenu_t *);
    52 extern errno_t tbsmenu_remove(tbsmenu_t *, tbsmenu_entry_t *, bool);
     52extern errno_t tbsmenu_add(tbsmenu_t *, const char *, const char *,
     53    tbsmenu_entry_t **);
     54extern void tbsmenu_remove(tbsmenu_t *, tbsmenu_entry_t *, bool);
    5355extern tbsmenu_entry_t *tbsmenu_first(tbsmenu_t *);
    5456extern tbsmenu_entry_t *tbsmenu_last(tbsmenu_t *);
  • uspace/app/taskbar/types/tbsmenu.h

    r7d78e466 r9bec33a  
    4343#include <ui/fixed.h>
    4444#include <ui/menu.h>
     45#include <ui/menuentry.h>
    4546#include <ui/window.h>
    4647
     
    5152        /** Link to tbsmenu->entries */
    5253        link_t lentries;
     54        /** Menu entry */
     55        ui_menu_entry_t *mentry;
     56        /** Caption */
     57        char *caption;
     58        /** Command to run */
     59        char *cmd;
    5360} tbsmenu_entry_t;
    5461
     
    7986} tbsmenu_t;
    8087
     88/** Command split into individual parts */
     89typedef struct {
     90        /** Buffer holding broken down command */
     91        char *buf;
     92        /** NULL-terminated array of string pointers */
     93        char **argv;
     94} tbsmenu_cmd_t;
     95
    8196#endif
    8297
Note: See TracChangeset for help on using the changeset viewer.