| 1 |
/***************************************************************************** |
| 2 |
* |
| 3 |
* cdebconf - An implementation of the Debian Configuration Management |
| 4 |
* System |
| 5 |
* |
| 6 |
* cdebconf is (c) 2000-2007 Randolph Chung and others under the following |
| 7 |
* license. |
| 8 |
* |
| 9 |
* Redistribution and use in source and binary forms, with or without |
| 10 |
* modification, are permitted provided that the following conditions |
| 11 |
* are met: |
| 12 |
* |
| 13 |
* 1. Redistributions of source code must retain the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer. |
| 15 |
* |
| 16 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 17 |
* notice, this list of conditions and the following disclaimer in the |
| 18 |
* documentation and/or other materials provided with the distribution. |
| 19 |
* |
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND |
| 21 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 24 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 26 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 29 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 30 |
* SUCH DAMAGE. |
| 31 |
* |
| 32 |
*****************************************************************************/ |
| 33 |
|
| 34 |
/** @file fe_gtk.c |
| 35 |
* "Main" file for the GTK+ frontend of cdebconf. |
| 36 |
*/ |
| 37 |
|
| 38 |
#include <syslog.h> |
| 39 |
#include <glib.h> |
| 40 |
#include <gtk/gtk.h> |
| 41 |
#include <signal.h> |
| 42 |
#include <string.h> |
| 43 |
#include <errno.h> |
| 44 |
|
| 45 |
#include "common.h" |
| 46 |
#include "template.h" |
| 47 |
#include "question.h" |
| 48 |
#include "frontend.h" |
| 49 |
#include "database.h" |
| 50 |
#include "plugin.h" |
| 51 |
#include "strutl.h" |
| 52 |
|
| 53 |
#include "cdebconf_gtk.h" |
| 54 |
#include "fe_data.h" |
| 55 |
#include "handlers.h" |
| 56 |
#include "align_text_renderer.h" |
| 57 |
#include "select_handlers.h" |
| 58 |
#include "descriptions.h" |
| 59 |
#include "progress.h" |
| 60 |
#include "go.h" |
| 61 |
#include "ui.h" |
| 62 |
#ifdef DI_UDEB |
| 63 |
# include "di.h" |
| 64 |
#endif /* DI_UDEB */ |
| 65 |
|
| 66 |
/** Get the description of a given template. |
| 67 |
* |
| 68 |
* The caller is responsible to free the returned string. |
| 69 |
* |
| 70 |
* @todo |
| 71 |
* There is no real reason that we can't use question_get_text defined in |
| 72 |
* cdebconf except that question_get_text doesn't strdup the fallback string. |
| 73 |
* This means than there is no way to have correct memory management. |
| 74 |
* question_get_text should be fixed and this function removed from the |
| 75 |
* GTK+ frontend. |
| 76 |
* |
| 77 |
* @param fe cdebconf frontend |
| 78 |
* @param template template name |
| 79 |
* @param fallback fallback string when the template does not exists |
| 80 |
* @return a newly allocated string containing the description of the template |
| 81 |
* or a copy of fallback if the template was not found |
| 82 |
*/ |
| 83 |
char * cdebconf_gtk_get_text(struct frontend * fe, const char * template, |
| 84 |
const char * fallback) |
| 85 |
{ |
| 86 |
/* XXX: add macro? */ |
| 87 |
struct question * question = fe->qdb->methods.get(fe->qdb, template); |
| 88 |
char * text; |
| 89 |
|
| 90 |
text = question ? q_get_description(fe, question) : g_strdup(fallback); |
| 91 |
question_deref(question); |
| 92 |
return text; |
| 93 |
} |
| 94 |
|
| 95 |
/** A GThreadFunc calling the GTK+ main loop. |
| 96 |
* |
| 97 |
* This function will be run in another thread and will happily process |
| 98 |
* GTK+ events resulting in user interaction. |
| 99 |
* |
| 100 |
* @param dummy an unused parameter |
| 101 |
* @return always return NULL |
| 102 |
*/ |
| 103 |
static void * handle_gtk_events(void * dummy) |
| 104 |
{ |
| 105 |
gdk_threads_enter(); |
| 106 |
gtk_main(); |
| 107 |
gdk_threads_leave(); |
| 108 |
|
| 109 |
return NULL /* no one cares what value is returned */; |
| 110 |
} |
| 111 |
|
| 112 |
/** Determines if a question is the first question requiring user input of the |
| 113 |
* current question row. |
| 114 |
* |
| 115 |
* @param question question that will be tested |
| 116 |
* @return TRUE if the question is the first question requiring user input |
| 117 |
*/ |
| 118 |
gboolean cdebconf_gtk_is_first_question(struct question * question) |
| 119 |
{ |
| 120 |
struct question * crawl; |
| 121 |
|
| 122 |
crawl = question; |
| 123 |
|
| 124 |
while (NULL != crawl->prev) { |
| 125 |
if (0 != strcmp(crawl->prev->template->type, "note")) { |
| 126 |
return FALSE; |
| 127 |
} |
| 128 |
crawl = crawl->prev; |
| 129 |
} |
| 130 |
return TRUE; |
| 131 |
} |
| 132 |
|
| 133 |
/* documented in cdebconf_gtk.h */ |
| 134 |
void cdebconf_gtk_force_quit(struct frontend * fe) |
| 135 |
{ |
| 136 |
if (-1 == kill(0 /* myself */, SIGTERM)) { |
| 137 |
g_critical("kill failed: %s", strerror(errno)); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/** De-allocate and de-init all elements of the GTK+ frontend data. |
| 142 |
* |
| 143 |
* @param fe cdebconf frontend |
| 144 |
*/ |
| 145 |
static void destroy_frontend_data(struct frontend * fe) |
| 146 |
{ |
| 147 |
struct frontend_data * fe_data = fe->data; |
| 148 |
|
| 149 |
if (NULL == fe_data) { |
| 150 |
return; |
| 151 |
} |
| 152 |
cdebconf_gtk_progress_stop(fe); |
| 153 |
/* Exit any running GO in order to cleanup. */ |
| 154 |
cdebconf_gtk_set_answer_notok(fe); |
| 155 |
fe->data = NULL; |
| 156 |
if (NULL != fe_data->window) { |
| 157 |
g_object_unref(G_OBJECT(fe_data->window)); |
| 158 |
} |
| 159 |
if (NULL != fe_data->title) { |
| 160 |
g_object_unref(G_OBJECT(fe_data->title)); |
| 161 |
} |
| 162 |
if (NULL != fe_data->target_box) { |
| 163 |
g_object_unref(G_OBJECT(fe_data->target_box)); |
| 164 |
} |
| 165 |
if (NULL != fe_data->action_box) { |
| 166 |
g_object_unref(G_OBJECT(fe_data->action_box)); |
| 167 |
} |
| 168 |
/* There is no safe way to free the mutex: this function can be called |
| 169 |
* as part as a signal handler while the mutex is actually held in |
| 170 |
* go.c:wait_answer(). |
| 171 |
*/ |
| 172 |
if (NULL == fe_data->plugins) { |
| 173 |
g_hash_table_destroy(fe_data->plugins); |
| 174 |
} |
| 175 |
g_free(fe_data); |
| 176 |
} |
| 177 |
|
| 178 |
/** Allocate and initialize non-graphical elements of the GTK+ frontend. |
| 179 |
* |
| 180 |
* The data field of the given frontend will be set to the newly |
| 181 |
* allocated structure. |
| 182 |
* |
| 183 |
* @param fe cdebconf frontend |
| 184 |
* @see frontend_data |
| 185 |
*/ |
| 186 |
static gboolean create_frontend_data(struct frontend * fe) |
| 187 |
{ |
| 188 |
struct frontend_data * fe_data; |
| 189 |
|
| 190 |
g_assert(NULL == fe->data); |
| 191 |
|
| 192 |
if (NULL == (fe->data = g_malloc0(sizeof (struct frontend_data)))) { |
| 193 |
g_critical("Unable to allocate frontend_data."); |
| 194 |
return FALSE; |
| 195 |
} |
| 196 |
|
| 197 |
fe_data = fe->data; |
| 198 |
|
| 199 |
g_cond_init(&(fe_data->answer_cond)); |
| 200 |
g_mutex_init(&(fe_data->answer_mutex)); |
| 201 |
fe_data->plugins = g_hash_table_new_full( |
| 202 |
g_str_hash, g_str_equal, g_free /* key destroy function (strings) */, |
| 203 |
(GDestroyNotify) plugin_delete /* value destroy function (plugin) */); |
| 204 |
if (NULL == fe_data->plugins) { |
| 205 |
g_critical("g_hash_table_new_full failed."); |
| 206 |
goto failed; |
| 207 |
} |
| 208 |
|
| 209 |
fe->data = fe_data; |
| 210 |
|
| 211 |
return TRUE; |
| 212 |
|
| 213 |
failed: |
| 214 |
destroy_frontend_data(fe); |
| 215 |
return FALSE; |
| 216 |
} |
| 217 |
|
| 218 |
/** Implements the shutdown method of cdebconf frontends. |
| 219 |
* |
| 220 |
* @param fe cdebconf frontend |
| 221 |
*/ |
| 222 |
static int cdebconf_gtk_shutdown(struct frontend * fe) |
| 223 |
{ |
| 224 |
struct frontend_data * fe_data = fe->data; |
| 225 |
|
| 226 |
if (NULL != fe_data) |
| 227 |
{ |
| 228 |
if (NULL != fe_data->event_listener) { |
| 229 |
gtk_main_quit(); |
| 230 |
if (g_thread_self() != fe_data->event_listener) { |
| 231 |
(void) g_thread_join(fe_data->event_listener); |
| 232 |
} |
| 233 |
fe_data->event_listener = NULL; |
| 234 |
} |
| 235 |
cdebconf_gtk_destroy_main_window(fe); |
| 236 |
#ifdef DI_UDEB |
| 237 |
cdebconf_gtk_di_shutdown(fe); |
| 238 |
#endif /* DI_UDEB */ |
| 239 |
destroy_frontend_data(fe); |
| 240 |
} |
| 241 |
|
| 242 |
return DC_OK; |
| 243 |
} |
| 244 |
|
| 245 |
/** Show the GTK+ frontend window. |
| 246 |
* |
| 247 |
* @param fe cdebconf frontend |
| 248 |
*/ |
| 249 |
static void show_main_window(struct frontend * fe) |
| 250 |
{ |
| 251 |
struct frontend_data * fe_data = fe->data; |
| 252 |
|
| 253 |
g_assert(NULL != fe_data->window); |
| 254 |
|
| 255 |
gtk_widget_show_all(fe_data->window); |
| 256 |
} |
| 257 |
|
| 258 |
/* documented in cdebconf_gtk.h */ |
| 259 |
int cdebconf_gtk_get_answer(struct frontend * fe) |
| 260 |
{ |
| 261 |
struct frontend_data * fe_data = fe->data; |
| 262 |
int answer; |
| 263 |
|
| 264 |
g_mutex_lock(&(fe_data->answer_mutex)); |
| 265 |
answer = fe_data->answer; |
| 266 |
g_mutex_unlock(&(fe_data->answer_mutex)); |
| 267 |
return answer; |
| 268 |
} |
| 269 |
|
| 270 |
/* documented in cdebconf_gtk.h */ |
| 271 |
void cdebconf_gtk_set_answer(struct frontend * fe, int answer) |
| 272 |
{ |
| 273 |
struct frontend_data * fe_data = fe->data; |
| 274 |
|
| 275 |
g_mutex_lock(&(fe_data->answer_mutex)); |
| 276 |
fe_data->answer = answer; |
| 277 |
g_cond_broadcast(&(fe_data->answer_cond)); |
| 278 |
g_mutex_unlock(&(fe_data->answer_mutex)); |
| 279 |
} |
| 280 |
|
| 281 |
/* documented in cdebconf_gtk.h */ |
| 282 |
void cdebconf_gtk_set_answer_ok(struct frontend * fe) |
| 283 |
{ |
| 284 |
cdebconf_gtk_set_answer(fe, DC_OK); |
| 285 |
} |
| 286 |
|
| 287 |
/* documented in cdebconf_gtk.h */ |
| 288 |
void cdebconf_gtk_set_answer_notok(struct frontend * fe) |
| 289 |
{ |
| 290 |
cdebconf_gtk_set_answer(fe, DC_NOTOK); |
| 291 |
} |
| 292 |
|
| 293 |
/* documented in cdebconf_gtk.h */ |
| 294 |
void cdebconf_gtk_set_answer_goback(struct frontend * fe) |
| 295 |
{ |
| 296 |
cdebconf_gtk_set_answer(fe, DC_GOBACK); |
| 297 |
} |
| 298 |
|
| 299 |
/* documented in cdebconf_gtk.h */ |
| 300 |
void cdebconf_gtk_help(struct frontend * fe) |
| 301 |
{ |
| 302 |
struct frontend_data * fe_data = fe->data; |
| 303 |
char * description; |
| 304 |
char * ext_description; |
| 305 |
|
| 306 |
if (NULL == fe_data || NULL == fe_data->help_question) |
| 307 |
return; |
| 308 |
|
| 309 |
description = q_get_description(fe, fe_data->help_question); |
| 310 |
ext_description = q_get_extended_description(fe, fe_data->help_question); |
| 311 |
cdebconf_gtk_run_message_dialog(fe, description, ext_description); |
| 312 |
g_free(ext_description); |
| 313 |
g_free(description); |
| 314 |
} |
| 315 |
|
| 316 |
/** Create the event listener thread. |
| 317 |
* |
| 318 |
* @param fe cdebconf frontend |
| 319 |
* @return FALSE in case of errors, TRUE otherwise |
| 320 |
* @see handle_gtk_events() |
| 321 |
*/ |
| 322 |
static gboolean create_event_listener_thread(struct frontend * fe) |
| 323 |
{ |
| 324 |
struct frontend_data * fe_data = fe->data; |
| 325 |
GError * error; |
| 326 |
|
| 327 |
g_assert(NULL == fe_data->event_listener); |
| 328 |
|
| 329 |
fe_data->event_listener = g_thread_try_new( |
| 330 |
"event_listener", (GThreadFunc) handle_gtk_events, NULL /* no data */, |
| 331 |
&error); |
| 332 |
if (NULL == fe_data->event_listener) { |
| 333 |
g_critical("g_thread_try_new failed: %s", error->message); |
| 334 |
g_error_free(error); |
| 335 |
return FALSE; |
| 336 |
} |
| 337 |
return TRUE; |
| 338 |
} |
| 339 |
|
| 340 |
/** Implements the initialize method of cdebconf frontends. |
| 341 |
* |
| 342 |
* This will initialize the GTK+ frontend data, the GTK+ window and common |
| 343 |
* widgets and forks the event handling thread. |
| 344 |
* |
| 345 |
* @return DC_OK if initialization goes fine, DC_NOTOK if something went wrong |
| 346 |
*/ |
| 347 |
static int cdebconf_gtk_initialize(struct frontend * fe, |
| 348 |
struct configuration * conf) |
| 349 |
{ |
| 350 |
/* INFO(INFO_DEBUG, "GTK_DI - gtk_initialize() called"); */ |
| 351 |
gdk_threads_init(); |
| 352 |
gtk_init(NULL /* no argc */, NULL /* no argv */); |
| 353 |
|
| 354 |
if (!create_frontend_data(fe)) { |
| 355 |
g_critical("create_frontend_data failed."); |
| 356 |
goto failed; |
| 357 |
} |
| 358 |
if (!cdebconf_gtk_create_main_window(fe)) { |
| 359 |
g_critical("cdebconf_gtk_create_main_window failed."); |
| 360 |
goto failed; |
| 361 |
} |
| 362 |
fe->interactive = TRUE; |
| 363 |
|
| 364 |
#ifdef DI_UDEB |
| 365 |
if (!cdebconf_gtk_di_setup(fe)) { |
| 366 |
g_critical("cdebconf_gtk_di_setup failed."); |
| 367 |
goto failed; |
| 368 |
} |
| 369 |
#endif /* DI_UDEB */ |
| 370 |
|
| 371 |
/* XXX: here? */ |
| 372 |
show_main_window(fe); |
| 373 |
|
| 374 |
if (!create_event_listener_thread(fe)) { |
| 375 |
g_critical("create_event_listener_thread failed."); |
| 376 |
goto failed; |
| 377 |
} |
| 378 |
|
| 379 |
return DC_OK; |
| 380 |
|
| 381 |
failed: |
| 382 |
cdebconf_gtk_shutdown(fe); |
| 383 |
return DC_NOTOK; |
| 384 |
} |
| 385 |
|
| 386 |
/** Implements the set_title method of cdebconf frontends. |
| 387 |
* |
| 388 |
* When build for the debian-installer, this will only update the corresponding |
| 389 |
* fields in frontend data. The title widget will then be updated by |
| 390 |
* cdebconf_gtk_di_run_dialog() on the next GO or progress start. |
| 391 |
* |
| 392 |
* @param fe cdebconf frontend |
| 393 |
* @param title new frontend title |
| 394 |
*/ |
| 395 |
static void cdebconf_gtk_set_title(struct frontend * fe, const char * title) |
| 396 |
{ |
| 397 |
g_free(fe->title); |
| 398 |
fe->title = g_strdup(title); |
| 399 |
#ifndef DI_UDEB /* Only for non d-i frontend */ |
| 400 |
gdk_threads_enter(); |
| 401 |
cdebconf_gtk_update_frontend_title(fe); |
| 402 |
gdk_threads_leave(); |
| 403 |
#else |
| 404 |
/* Workaround for #882804, GTK 2 only */ |
| 405 |
gdk_threads_enter(); |
| 406 |
cdebconf_gtk_update_frontend_info(fe); |
| 407 |
gdk_threads_leave(); |
| 408 |
#endif |
| 409 |
} |
| 410 |
|
| 411 |
/** Implements the lookup_directive function of cdebconf frontends. |
| 412 |
* |
| 413 |
* When the align cabability is set, the TAB directive returns "\t". |
| 414 |
* |
| 415 |
* @param fe cdebconf frontend |
| 416 |
* @param directive the directive name |
| 417 |
* @return the expansion value, or NULL if expansion should be skipped |
| 418 |
*/ |
| 419 |
static const char * cdebconf_gtk_lookup_directive(struct frontend * fe, |
| 420 |
const char * directive) |
| 421 |
{ |
| 422 |
if (CAN_ALIGN(fe) && 0 == strcmp(directive, "TAB")) { |
| 423 |
return "\t"; |
| 424 |
} |
| 425 |
if (CAN_ALIGN(fe) && 0 == strcmp(directive, "ALIGN=CENTER")) { |
| 426 |
return ALIGN_CENTER_STRING; |
| 427 |
} |
| 428 |
if (CAN_ALIGN(fe) && 0 == strcmp(directive, "ALIGN=RIGHT")) { |
| 429 |
return ALIGN_RIGHT_STRING; |
| 430 |
} |
| 431 |
/* Remove unhandled directives */ |
| 432 |
return ""; |
| 433 |
} |
| 434 |
|
| 435 |
/** Implements the can_go_back function of cdebconf frontends. |
| 436 |
* |
| 437 |
* @param fe cdebconf frontend |
| 438 |
* @param question question which needs to be backed up |
| 439 |
* @return TRUE if backup is possible, FALSE otherwise |
| 440 |
*/ |
| 441 |
static bool cdebconf_gtk_can_go_back(struct frontend * fe, |
| 442 |
struct question * question) |
| 443 |
{ |
| 444 |
return DCF_CAPB_BACKUP == (fe->capability & DCF_CAPB_BACKUP); |
| 445 |
} |
| 446 |
|
| 447 |
/** Implements the can_align function of cdebconf frontends. |
| 448 |
* |
| 449 |
* @param fe cdebconf frontend |
| 450 |
* @param question question which needs to be aligned |
| 451 |
* @return TRUE if align is possible, FALSE otherwise |
| 452 |
*/ |
| 453 |
static bool cdebconf_gtk_can_align(struct frontend * fe, |
| 454 |
struct question * question) |
| 455 |
{ |
| 456 |
return DCF_CAPB_ALIGN == (fe->capability & DCF_CAPB_ALIGN); |
| 457 |
} |
| 458 |
|
| 459 |
#ifdef DI_UDEB |
| 460 |
/* Workaround for #882804, GTK 2 only |
| 461 |
* |
| 462 |
* If we have a info message (e.g. rescue mode was selected in the |
| 463 |
* bootloader or preseeded), check that we've got a banner widget and |
| 464 |
* that we didn't send an expose_event already. Send a one-shot signal |
| 465 |
* to fix the display on the start-up screen if all those conditions |
| 466 |
* are met. |
| 467 |
* |
| 468 |
* @param ce cdebconf frontend |
| 469 |
*/ |
| 470 |
void cdebconf_gtk_update_frontend_info(struct frontend * fe) |
| 471 |
{ |
| 472 |
struct frontend_data * fe_data = fe->data; |
| 473 |
|
| 474 |
if (fe->info != NULL && |
| 475 |
fe_data->banner_widget && |
| 476 |
fe_data->banner_workaround_needed) |
| 477 |
{ |
| 478 |
/* Return value cannot be omitted (callback returns a gboolean) */ |
| 479 |
gboolean rval = FALSE; |
| 480 |
g_signal_emit_by_name (G_OBJECT(fe_data->banner_widget), |
| 481 |
"expose_event", |
| 482 |
(gpointer) fe, |
| 483 |
(gpointer) &rval); |
| 484 |
fe_data->banner_workaround_needed = FALSE; |
| 485 |
} |
| 486 |
} |
| 487 |
#endif |
| 488 |
|
| 489 |
/** Describe our frontend implementation to cdebconf. |
| 490 |
*/ |
| 491 |
struct frontend_module debconf_frontend_module = { |
| 492 |
.initialize = cdebconf_gtk_initialize, |
| 493 |
.shutdown = cdebconf_gtk_shutdown, |
| 494 |
.can_go_back = cdebconf_gtk_can_go_back, |
| 495 |
.can_align = cdebconf_gtk_can_align, |
| 496 |
.set_title = cdebconf_gtk_set_title, |
| 497 |
.lookup_directive = cdebconf_gtk_lookup_directive, |
| 498 |
/* see go.c */ |
| 499 |
.go = cdebconf_gtk_go, |
| 500 |
/* see progress.c */ |
| 501 |
.can_cancel_progress = cdebconf_gtk_can_cancel_progress, |
| 502 |
.progress_start = cdebconf_gtk_progress_start, |
| 503 |
.progress_info = cdebconf_gtk_progress_info, |
| 504 |
.progress_set = cdebconf_gtk_progress_set, |
| 505 |
.progress_stop = cdebconf_gtk_progress_stop, |
| 506 |
}; |
| 507 |
|
| 508 |
/* vim: et sw=4 si |
| 509 |
*/ |