Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/shutdown-dlg/shutdown-dlg.c

    r0d00e53 r0ae9e18  
    11/*
    2  * Copyright (c) 2024 Jiri Svoboda
     2 * Copyright (c) 2025 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    4141#include <ui/fixed.h>
    4242#include <ui/label.h>
     43#include <ui/list.h>
     44#include <ui/msgdialog.h>
    4345#include <ui/resource.h>
     46#include <ui/selectdialog.h>
    4447#include <ui/ui.h>
    4548#include <ui/window.h>
     
    4851static void wnd_close(ui_window_t *, void *);
    4952static errno_t bg_wnd_paint(ui_window_t *, void *);
     53static void shutdown_progress_destroy(shutdown_progress_t *);
     54static errno_t shutdown_confirm_create(shutdown_dlg_t *);
     55static errno_t shutdown_failed_msg_create(shutdown_dlg_t *);
     56static errno_t shutdown_start(shutdown_dlg_t *, sd_action_t);
    5057
    5158static ui_window_cb_t bg_window_cb = {
     
    6471        .shutdown_complete = sd_shutdown_complete,
    6572        .shutdown_failed = sd_shutdown_failed
     73};
     74
     75static void shutdown_confirm_bok(ui_select_dialog_t *, void *, void *);
     76static void shutdown_confirm_bcancel(ui_select_dialog_t *, void *);
     77static void shutdown_confirm_close(ui_select_dialog_t *, void *);
     78
     79static ui_select_dialog_cb_t shutdown_confirm_cb = {
     80        .bok = shutdown_confirm_bok,
     81        .bcancel = shutdown_confirm_bcancel,
     82        .close = shutdown_confirm_close
     83};
     84
     85static void shutdown_failed_msg_button(ui_msg_dialog_t *, void *, unsigned);
     86static void shutdown_failed_msg_close(ui_msg_dialog_t *, void *);
     87
     88static ui_msg_dialog_cb_t shutdown_failed_msg_cb = {
     89        .button = shutdown_failed_msg_button,
     90        .close = shutdown_failed_msg_close
    6691};
    6792
     
    90115
    91116        ui_lock(sddlg->ui);
    92         (void)ui_label_set_text(sddlg->progress->label, "Shutdown failed.");
    93         (void)ui_window_paint(sddlg->progress->window);
     117        shutdown_progress_destroy(sddlg->progress);
     118        sddlg->progress = NULL;
     119        ui_window_destroy(sddlg->bgwindow);
     120        sddlg->bgwindow = NULL;
     121        (void)shutdown_failed_msg_create(sddlg);
    94122        ui_unlock(sddlg->ui);
    95123}
     
    135163
    136164        return EOK;
     165}
     166
     167/** Create shutdown confirmation dialog.
     168 *
     169 * @param sddlg Shutdown dialog
     170 * @return EOK on success or an error code
     171 */
     172static errno_t shutdown_confirm_create(shutdown_dlg_t *sddlg)
     173{
     174        ui_select_dialog_params_t params;
     175        ui_select_dialog_t *dialog;
     176        ui_list_entry_attr_t attr;
     177        errno_t rc;
     178
     179        ui_select_dialog_params_init(&params);
     180        params.caption = "Shutdown";
     181        params.prompt = "Do you want to shut the system down?";
     182        params.flags |= usdf_topmost | usdf_center;
     183
     184        rc = ui_select_dialog_create(sddlg->ui, &params, &dialog);
     185        if (rc != EOK)
     186                return rc;
     187
     188        /* Need an entry to select */
     189        ui_list_entry_attr_init(&attr);
     190
     191        attr.caption = "Power off";
     192        attr.arg = (void *)sd_poweroff;
     193        rc = ui_select_dialog_append(dialog, &attr);
     194        if (rc != EOK)
     195                goto error;
     196
     197        attr.caption = "Restart";
     198        attr.arg = (void *)sd_restart;
     199        rc = ui_select_dialog_append(dialog, &attr);
     200        if (rc != EOK)
     201                goto error;
     202
     203        ui_select_dialog_set_cb(dialog, &shutdown_confirm_cb, sddlg);
     204
     205        (void)ui_select_dialog_paint(dialog);
     206
     207        return EOK;
     208error:
     209        ui_select_dialog_destroy(dialog);
     210        return rc;
     211}
     212
     213/** Create 'shutdown failed' message dialog.
     214 *
     215 * @param sddlg Shutdown dialog
     216 * @return EOK on success or an error code
     217 */
     218static errno_t shutdown_failed_msg_create(shutdown_dlg_t *sddlg)
     219{
     220        ui_msg_dialog_params_t params;
     221        ui_msg_dialog_t *dialog;
     222        errno_t rc;
     223
     224        ui_msg_dialog_params_init(&params);
     225        params.caption = "Shutdown failed";
     226        params.text = "The system failed to shut down properly.";
     227
     228        rc = ui_msg_dialog_create(sddlg->ui, &params, &dialog);
     229        if (rc != EOK)
     230                return rc;
     231
     232        ui_msg_dialog_set_cb(dialog, &shutdown_failed_msg_cb, sddlg);
     233
     234        return EOK;
     235}
     236
     237/** Shutdown confirm dialog OK button press.
     238 *
     239 * @param dialog Message dialog
     240 * @param arg Argument (ui_demo_t *)
     241 * @param earg Entry argument
     242 */
     243static void shutdown_confirm_bok(ui_select_dialog_t *dialog, void *arg,
     244    void *earg)
     245{
     246        shutdown_dlg_t *sddlg = (shutdown_dlg_t *) arg;
     247
     248        ui_select_dialog_destroy(dialog);
     249
     250        shutdown_start(sddlg, (sd_action_t)earg);
     251}
     252
     253/** Shutdown confirm dialog Cancel button press.
     254 *
     255 * @param dialog Message dialog
     256 * @param arg Argument (ui_demo_t *)
     257 */
     258static void shutdown_confirm_bcancel(ui_select_dialog_t *dialog, void *arg)
     259{
     260        shutdown_dlg_t *sddlg = (shutdown_dlg_t *) arg;
     261
     262        ui_select_dialog_destroy(dialog);
     263        ui_quit(sddlg->ui);
     264}
     265
     266/** Shutdown confirm message dialog close request.
     267 *
     268 * @param dialog Message dialog
     269 * @param arg Argument (ui_demo_t *)
     270 */
     271static void shutdown_confirm_close(ui_select_dialog_t *dialog, void *arg)
     272{
     273        shutdown_dlg_t *sddlg = (shutdown_dlg_t *) arg;
     274
     275        ui_select_dialog_destroy(dialog);
     276        ui_quit(sddlg->ui);
     277}
     278
     279/** Shutdown faield message dialog button press.
     280 *
     281 * @param dialog Message dialog
     282 * @param arg Argument (ui_demo_t *)
     283 * @param bnum Button number
     284 */
     285static void shutdown_failed_msg_button(ui_msg_dialog_t *dialog,
     286    void *arg, unsigned bnum)
     287{
     288        shutdown_dlg_t *sddlg = (shutdown_dlg_t *) arg;
     289
     290        ui_msg_dialog_destroy(dialog);
     291        ui_quit(sddlg->ui);
     292}
     293
     294/** Shutdown failed message dialog close request.
     295 *
     296 * @param dialog Message dialog
     297 * @param arg Argument (ui_demo_t *)
     298 */
     299static void shutdown_failed_msg_close(ui_msg_dialog_t *dialog, void *arg)
     300{
     301        shutdown_dlg_t *sddlg = (shutdown_dlg_t *) arg;
     302
     303        ui_msg_dialog_destroy(dialog);
     304        ui_quit(sddlg->ui);
    137305}
    138306
     
    162330                params.rect.p0.x = 0;
    163331                params.rect.p0.y = 0;
    164                 params.rect.p1.x = 24;
     332                params.rect.p1.x = 64;
    165333                params.rect.p1.y = 5;
    166334        } else {
     
    245413static void shutdown_progress_destroy(shutdown_progress_t *progress)
    246414{
     415        if (progress == NULL)
     416                return;
     417
    247418        ui_window_destroy(progress->window);
    248419        free(progress);
    249420}
    250421
     422/** Start shutdown.
     423 *
     424 * @param sddlg Shutdown dialog
     425 * @param action Shutdown actin
     426 * @return EOK on success or an error code
     427 */
     428static errno_t shutdown_start(shutdown_dlg_t *sddlg, sd_action_t action)
     429{
     430        errno_t rc;
     431
     432        rc = shutdown_progress_create(sddlg, &sddlg->progress);
     433        if (rc != EOK)
     434                return rc;
     435
     436        rc = system_open(SYSTEM_DEFAULT, &sd_system_cb, sddlg, &sddlg->system);
     437        if (rc != EOK) {
     438                printf("Failed opening system control service.\n");
     439                goto error;
     440        }
     441
     442        rc = EINVAL;
     443
     444        switch (action) {
     445        case sd_poweroff:
     446                rc = system_poweroff(sddlg->system);
     447                break;
     448        case sd_restart:
     449                rc = system_restart(sddlg->system);
     450                break;
     451        }
     452
     453        if (rc != EOK) {
     454                printf("Failed requesting system shutdown.\n");
     455                goto error;
     456        }
     457
     458        return EOK;
     459error:
     460        return rc;
     461}
     462
    251463/** Run shutdown dialog on display. */
    252464static errno_t shutdown_dlg(const char *display_spec)
    253465{
    254466        ui_t *ui = NULL;
     467        shutdown_dlg_t sddlg;
    255468        ui_wnd_params_t params;
    256         ui_window_t *window = NULL;
    257         shutdown_dlg_t sddlg;
    258469        errno_t rc;
    259470
     
    265476                goto error;
    266477        }
     478
     479        sddlg.ui = ui;
    267480
    268481        ui_wnd_params_init(&params);
     
    271484        params.placement = ui_wnd_place_full_screen;
    272485        params.flags |= ui_wndf_topmost | ui_wndf_nofocus;
    273         if (ui_is_textmode(ui)) {
    274                 params.rect.p0.x = 0;
    275                 params.rect.p0.y = 0;
    276                 params.rect.p1.x = 24;
    277                 params.rect.p1.y = 5;
     486        /*
     487         * params.rect.p0.x = 0;
     488         * params.rect.p0.y = 0;
     489         * params.rect.p1.x = 1;
     490         * params.rect.p1.y = 1;
     491         */
     492
     493        rc = ui_window_create(sddlg.ui, &params, &sddlg.bgwindow);
     494        if (rc != EOK) {
     495                printf("Error creating window.\n");
     496                goto error;
     497        }
     498
     499        ui_window_set_cb(sddlg.bgwindow, &bg_window_cb, (void *)&sddlg);
     500
     501        if (ui_is_textmode(sddlg.ui)) {
     502                rc = gfx_color_new_ega(0x17, &sddlg.bg_color);
     503                if (rc != EOK) {
     504                        printf("Error allocating color.\n");
     505                        goto error;
     506                }
    278507        } else {
    279                 params.rect.p0.x = 0;
    280                 params.rect.p0.y = 0;
    281                 params.rect.p1.x = 300;
    282                 params.rect.p1.y = 60;
    283         }
    284 
    285         sddlg.ui = ui;
    286 
    287         rc = ui_window_create(ui, &params, &window);
    288         if (rc != EOK) {
    289                 printf("Error creating window.\n");
    290                 goto error;
    291         }
    292 
    293         ui_window_set_cb(window, &bg_window_cb, (void *) &sddlg);
    294         sddlg.bgwindow = window;
    295 
    296         rc = gfx_color_new_rgb_i16(0x8000, 0xc800, 0xffff, &sddlg.bg_color);
    297         if (rc != EOK) {
    298                 printf("Error allocating color.\n");
    299                 goto error;
    300         }
    301 
    302         rc = ui_window_paint(window);
     508                rc = gfx_color_new_rgb_i16(0x8000, 0xc800, 0xffff, &sddlg.bg_color);
     509                if (rc != EOK) {
     510                        printf("Error allocating color.\n");
     511                        goto error;
     512                }
     513        }
     514
     515        rc = ui_window_paint(sddlg.bgwindow);
    303516        if (rc != EOK) {
    304517                printf("Error painting window.\n");
     
    306519        }
    307520
    308         rc = shutdown_progress_create(&sddlg, &sddlg.progress);
    309         if (rc != EOK)
    310                 return rc;
    311 
    312         rc = system_open(SYSTEM_DEFAULT, &sd_system_cb, &sddlg, &sddlg.system);
    313         if (rc != EOK) {
    314                 printf("Failed opening system control service.\n");
    315                 goto error;
    316         }
    317 
    318         rc = system_shutdown(sddlg.system);
    319         if (rc != EOK) {
    320                 printf("Failed requesting system shutdown.\n");
    321                 goto error;
    322         }
     521        (void)shutdown_confirm_create(&sddlg);
    323522
    324523        ui_run(ui);
    325524
    326525        shutdown_progress_destroy(sddlg.progress);
    327         ui_window_destroy(window);
    328         system_close(sddlg.system);
     526        if (sddlg.bgwindow != NULL)
     527                ui_window_destroy(sddlg.bgwindow);
     528        if (sddlg.system != NULL)
     529                system_close(sddlg.system);
    329530        gfx_color_delete(sddlg.bg_color);
    330531        ui_destroy(ui);
     
    336537        if (sddlg.bg_color != NULL)
    337538                gfx_color_delete(sddlg.bg_color);
    338         if (window != NULL)
    339                 ui_window_destroy(window);
     539        if (sddlg.bgwindow != NULL)
     540                ui_window_destroy(sddlg.bgwindow);
    340541        if (ui != NULL)
    341542                ui_destroy(ui);
Note: See TracChangeset for help on using the changeset viewer.