Changes in uspace/app/shutdown-dlg/shutdown-dlg.c [0d00e53:0ae9e18] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/shutdown-dlg/shutdown-dlg.c
r0d00e53 r0ae9e18 1 1 /* 2 * Copyright (c) 202 4Jiri Svoboda2 * Copyright (c) 2025 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> 43 45 #include <ui/resource.h> 46 #include <ui/selectdialog.h> 44 47 #include <ui/ui.h> 45 48 #include <ui/window.h> … … 48 51 static void wnd_close(ui_window_t *, void *); 49 52 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); 50 57 51 58 static ui_window_cb_t bg_window_cb = { … … 64 71 .shutdown_complete = sd_shutdown_complete, 65 72 .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 66 91 }; 67 92 … … 90 115 91 116 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); 94 122 ui_unlock(sddlg->ui); 95 123 } … … 135 163 136 164 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(¶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 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(¶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 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); 137 305 } 138 306 … … 162 330 params.rect.p0.x = 0; 163 331 params.rect.p0.y = 0; 164 params.rect.p1.x = 24;332 params.rect.p1.x = 64; 165 333 params.rect.p1.y = 5; 166 334 } else { … … 245 413 static void shutdown_progress_destroy(shutdown_progress_t *progress) 246 414 { 415 if (progress == NULL) 416 return; 417 247 418 ui_window_destroy(progress->window); 248 419 free(progress); 249 420 } 250 421 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 251 463 /** Run shutdown dialog on display. */ 252 464 static errno_t shutdown_dlg(const char *display_spec) 253 465 { 254 466 ui_t *ui = NULL; 467 shutdown_dlg_t sddlg; 255 468 ui_wnd_params_t params; 256 ui_window_t *window = NULL;257 shutdown_dlg_t sddlg;258 469 errno_t rc; 259 470 … … 265 476 goto error; 266 477 } 478 479 sddlg.ui = ui; 267 480 268 481 ui_wnd_params_init(¶ms); … … 271 484 params.placement = ui_wnd_place_full_screen; 272 485 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, ¶ms, &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 } 278 507 } 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); 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); 303 516 if (rc != EOK) { 304 517 printf("Error painting window.\n"); … … 306 519 } 307 520 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); 323 522 324 523 ui_run(ui); 325 524 326 525 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); 329 530 gfx_color_delete(sddlg.bg_color); 330 531 ui_destroy(ui); … … 336 537 if (sddlg.bg_color != NULL) 337 538 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); 340 541 if (ui != NULL) 341 542 ui_destroy(ui);
Note:
See TracChangeset
for help on using the changeset viewer.