Changes in uspace/app/shutdown-dlg/shutdown-dlg.c [0ae9e18:0d00e53] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/shutdown-dlg/shutdown-dlg.c
r0ae9e18 r0d00e53 1 1 /* 2 * Copyright (c) 202 5Jiri Svoboda2 * Copyright (c) 2024 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 41 41 #include <ui/fixed.h> 42 42 #include <ui/label.h> 43 #include <ui/list.h>44 #include <ui/msgdialog.h>45 43 #include <ui/resource.h> 46 #include <ui/selectdialog.h>47 44 #include <ui/ui.h> 48 45 #include <ui/window.h> … … 51 48 static void wnd_close(ui_window_t *, void *); 52 49 static 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);57 50 58 51 static ui_window_cb_t bg_window_cb = { … … 71 64 .shutdown_complete = sd_shutdown_complete, 72 65 .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_close83 };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_close91 66 }; 92 67 … … 115 90 116 91 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); 122 94 ui_unlock(sddlg->ui); 123 95 } … … 163 135 164 136 return EOK; 165 }166 167 /** Create shutdown confirmation dialog.168 *169 * @param sddlg Shutdown dialog170 * @return EOK on success or an error code171 */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(¶ms);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, ¶ms, &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 dialog216 * @return EOK on success or an error code217 */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(¶ms);225 params.caption = "Shutdown failed";226 params.text = "The system failed to shut down properly.";227 228 rc = ui_msg_dialog_create(sddlg->ui, ¶ms, &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 dialog240 * @param arg Argument (ui_demo_t *)241 * @param earg Entry argument242 */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 dialog256 * @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 dialog269 * @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 dialog282 * @param arg Argument (ui_demo_t *)283 * @param bnum Button number284 */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 dialog297 * @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);305 137 } 306 138 … … 330 162 params.rect.p0.x = 0; 331 163 params.rect.p0.y = 0; 332 params.rect.p1.x = 64;164 params.rect.p1.x = 24; 333 165 params.rect.p1.y = 5; 334 166 } else { … … 413 245 static void shutdown_progress_destroy(shutdown_progress_t *progress) 414 246 { 415 if (progress == NULL)416 return;417 418 247 ui_window_destroy(progress->window); 419 248 free(progress); 420 249 } 421 250 422 /** Start shutdown.423 *424 * @param sddlg Shutdown dialog425 * @param action Shutdown actin426 * @return EOK on success or an error code427 */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 463 251 /** Run shutdown dialog on display. */ 464 252 static errno_t shutdown_dlg(const char *display_spec) 465 253 { 466 254 ui_t *ui = NULL; 255 ui_wnd_params_t params; 256 ui_window_t *window = NULL; 467 257 shutdown_dlg_t sddlg; 468 ui_wnd_params_t params;469 258 errno_t rc; 470 259 … … 476 265 goto error; 477 266 } 478 479 sddlg.ui = ui;480 267 481 268 ui_wnd_params_init(¶ms); … … 484 271 params.placement = ui_wnd_place_full_screen; 485 272 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, ¶ms, &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, ¶ms, &window); 494 288 if (rc != EOK) { 495 289 printf("Error creating window.\n"); … … 497 291 } 498 292 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); 516 303 if (rc != EOK) { 517 304 printf("Error painting window.\n"); … … 519 306 } 520 307 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 } 522 323 523 324 ui_run(ui); 524 325 525 326 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); 530 329 gfx_color_delete(sddlg.bg_color); 531 330 ui_destroy(ui); … … 537 336 if (sddlg.bg_color != NULL) 538 337 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); 541 340 if (ui != NULL) 542 341 ui_destroy(ui);
Note:
See TracChangeset
for help on using the changeset viewer.