Ignore:
File:
1 edited

Legend:

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

    r0ae9e18 r0d00e53  
    11/*
    2  * Copyright (c) 2025 Jiri Svoboda
     2 * Copyright (c) 2024 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>
    4543#include <ui/resource.h>
    46 #include <ui/selectdialog.h>
    4744#include <ui/ui.h>
    4845#include <ui/window.h>
     
    5148static void wnd_close(ui_window_t *, void *);
    5249static errno_t bg_wnd_paint(ui_window_t *, void *);
    53 static void shutdown_progress_destroy(shutdown_progress_t *);
    54 static errno_t shutdown_confirm_create(shutdown_dlg_t *);
    55 static errno_t shutdown_failed_msg_create(shutdown_dlg_t *);
    56 static errno_t shutdown_start(shutdown_dlg_t *, sd_action_t);
    5750
    5851static ui_window_cb_t bg_window_cb = {
     
    7164        .shutdown_complete = sd_shutdown_complete,
    7265        .shutdown_failed = sd_shutdown_failed
    73 };
    74 
    75 static void shutdown_confirm_bok(ui_select_dialog_t *, void *, void *);
    76 static void shutdown_confirm_bcancel(ui_select_dialog_t *, void *);
    77 static void shutdown_confirm_close(ui_select_dialog_t *, void *);
    78 
    79 static 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 
    85 static void shutdown_failed_msg_button(ui_msg_dialog_t *, void *, unsigned);
    86 static void shutdown_failed_msg_close(ui_msg_dialog_t *, void *);
    87 
    88 static ui_msg_dialog_cb_t shutdown_failed_msg_cb = {
    89         .button = shutdown_failed_msg_button,
    90         .close = shutdown_failed_msg_close
    9166};
    9267
     
    11590
    11691        ui_lock(sddlg->ui);
    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);
     92        (void)ui_label_set_text(sddlg->progress->label, "Shutdown failed.");
     93        (void)ui_window_paint(sddlg->progress->window);
    12294        ui_unlock(sddlg->ui);
    12395}
     
    163135
    164136        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  */
    172 static 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;
    208 error:
    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  */
    218 static 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  */
    243 static 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  */
    258 static 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  */
    271 static 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  */
    285 static 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  */
    299 static 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);
    305137}
    306138
     
    330162                params.rect.p0.x = 0;
    331163                params.rect.p0.y = 0;
    332                 params.rect.p1.x = 64;
     164                params.rect.p1.x = 24;
    333165                params.rect.p1.y = 5;
    334166        } else {
     
    413245static void shutdown_progress_destroy(shutdown_progress_t *progress)
    414246{
    415         if (progress == NULL)
    416                 return;
    417 
    418247        ui_window_destroy(progress->window);
    419248        free(progress);
    420249}
    421250
    422 /** Start shutdown.
    423  *
    424  * @param sddlg Shutdown dialog
    425  * @param action Shutdown actin
    426  * @return EOK on success or an error code
    427  */
    428 static 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;
    459 error:
    460         return rc;
    461 }
    462 
    463251/** Run shutdown dialog on display. */
    464252static errno_t shutdown_dlg(const char *display_spec)
    465253{
    466254        ui_t *ui = NULL;
     255        ui_wnd_params_t params;
     256        ui_window_t *window = NULL;
    467257        shutdown_dlg_t sddlg;
    468         ui_wnd_params_t params;
    469258        errno_t rc;
    470259
     
    476265                goto error;
    477266        }
    478 
    479         sddlg.ui = ui;
    480267
    481268        ui_wnd_params_init(&params);
     
    484271        params.placement = ui_wnd_place_full_screen;
    485272        params.flags |= ui_wndf_topmost | ui_wndf_nofocus;
    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);
     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;
     278        } 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);
    494288        if (rc != EOK) {
    495289                printf("Error creating window.\n");
     
    497291        }
    498292
    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                 }
    507         } else {
    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);
     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);
    516303        if (rc != EOK) {
    517304                printf("Error painting window.\n");
     
    519306        }
    520307
    521         (void)shutdown_confirm_create(&sddlg);
     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        }
    522323
    523324        ui_run(ui);
    524325
    525326        shutdown_progress_destroy(sddlg.progress);
    526         if (sddlg.bgwindow != NULL)
    527                 ui_window_destroy(sddlg.bgwindow);
    528         if (sddlg.system != NULL)
    529                 system_close(sddlg.system);
     327        ui_window_destroy(window);
     328        system_close(sddlg.system);
    530329        gfx_color_delete(sddlg.bg_color);
    531330        ui_destroy(ui);
     
    537336        if (sddlg.bg_color != NULL)
    538337                gfx_color_delete(sddlg.bg_color);
    539         if (sddlg.bgwindow != NULL)
    540                 ui_window_destroy(sddlg.bgwindow);
     338        if (window != NULL)
     339                ui_window_destroy(window);
    541340        if (ui != NULL)
    542341                ui_destroy(ui);
Note: See TracChangeset for help on using the changeset viewer.