1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
|
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "main.h"
#include "shared.h"
static void sig_handler(int signal);
/* Child process pid returned by fork() */
static pid_t child_pid = (pid_t)(-1);
/*------------------------------------------------------------------------*/
int
main (int argc, char *argv[])
{
/* getopt() variables */
int option, idx, err;
gboolean new = TRUE;
/*** Signal handler related code ***/
/* new and old actions for sigaction() */
struct sigaction sa_new, sa_old;
/* initialize new actions */
sa_new.sa_handler = sig_handler;
sigemptyset( &sa_new.sa_mask );
sa_new.sa_flags = 0;
/* Register function to handle signals */
sigaction( SIGINT, &sa_new, &sa_old );
sigaction( SIGSEGV, &sa_new, NULL );
sigaction( SIGFPE, &sa_new, NULL );
sigaction( SIGTERM, &sa_new, NULL );
sigaction( SIGABRT, &sa_new, NULL );
sigaction( SIGCHLD, &sa_new, NULL );
gtk_init (&argc, &argv);
/* Create folder path for glade file */
snprintf( xnec2c_glade, sizeof(xnec2c_glade),
"/usr/share/xnec2c/xnec2c.glade" );
/* Create a default config if needed, abort on error */
if( !Create_Default_Config() ) exit( -1 );
/* Process command line options */
calc_data.num_jobs = 1;
rc_config.infile[0] = '\0';
while( (option = getopt(argc, argv, "i:j:hv") ) != -1 )
{
switch( option )
{
case 'i': /* specify input file name */
{
size_t siz = sizeof( rc_config.infile );
if( strlen(optarg) >= siz )
{
fprintf ( stderr,
_("xnec2c: input file name too long ( > %d char )\n"), (int)siz - 1 );
exit(-1);
}
/* For null term. */
Strlcpy( rc_config.infile, optarg, siz );
}
break;
case 'j': /* number of child processes = num of processors */
calc_data.num_jobs = atoi( optarg );
break;
case 'h': /* print usage and exit */
usage();
exit(0);
case 'v': /* print xnec2c version */
puts( PACKAGE_STRING );
exit(0);
default:
usage();
exit(0);
} /* switch( option ) */
} /* while( (option = getopt(argc, argv, "i:o:hv") ) != -1 ) */
/* Read input file path name if not supplied by -i option */
if( (strlen(rc_config.infile) == 0) &&
(strstr(argv[argc - 1], ".nec") ||
strstr(argv[argc - 1], ".NEC")) )
{
size_t siz = sizeof( rc_config.infile );
if( strlen(argv[argc - 1]) >= siz )
{
fprintf ( stderr,
_("xnec2c: input file path name too long ( > %d char )\n"), (int)siz - 1 );
exit(-1);
}
/* For null termination */
Strlcpy( rc_config.infile, argv[argc-1], siz );
}
/* When forking is useful, e.g. if more than 1 processor is
* available, the parent process handles the GUI and delegates
* calculations to the child processes, one per processor. The
* requested number of child processes = number of processors */
/* Allocate buffers for fork data */
if( calc_data.num_jobs > 1 )
{
size_t mreq = (size_t)calc_data.num_jobs * sizeof(forked_proc_data_t *);
mem_alloc( (void **)&forked_proc_data, mreq, "in main.c" );
for( idx = 0; idx < calc_data.num_jobs; idx++ )
{
forked_proc_data[idx] = NULL;
mreq = sizeof(forked_proc_data_t);
mem_alloc( (void **)&forked_proc_data[idx], mreq, "in main.c" );
}
/* Fork child processes */
for( idx = 0; idx < calc_data.num_jobs; idx++ )
{
/* Make pipes to transfer data */
err = pipe( forked_proc_data[idx]->pnt2child_pipe );
if( err )
{
perror( "xnec2c: pipe()" );
fprintf( stderr,
_("xnec2c: exiting after fatal error (pipe() failed)") );
exit(-1);
}
err = pipe( forked_proc_data[idx]->child2pnt_pipe );
if( err )
{
perror( "xnec2c: pipe()" );
fprintf( stderr,
_("xnec2c: exiting after fatal error (pipe() failed)") );
exit(-1);
}
/* Fork child process */
forked_proc_data[idx]->child_pid = fork();
if( forked_proc_data[idx]->child_pid == -1 )
{
perror( "xnec2c: fork()" );
fprintf( stderr,
_("xnec2c: exiting after fatal error (fork() failed)") );
exit(-1);
}
else child_pid = forked_proc_data[idx]->child_pid;
/* Child get out of forking loop! */
if( CHILD ) Child_Process( idx );
/* Ready to accept a job */
forked_proc_data[idx]->busy = FALSE;
/* Close unwanted pipe ends */
close( forked_proc_data[idx]->pnt2child_pipe[READ] );
close( forked_proc_data[idx]->child2pnt_pipe[WRITE] );
/* Set file descriptors for select() */
FD_ZERO( &forked_proc_data[idx]->read_fds );
FD_SET( forked_proc_data[idx]->child2pnt_pipe[READ],
&forked_proc_data[idx]->read_fds );
FD_ZERO( &forked_proc_data[idx]->write_fds );
FD_SET( forked_proc_data[idx]->pnt2child_pipe[WRITE],
&forked_proc_data[idx]->write_fds );
/* Count child processes */
num_child_procs++;
} /* for( idx = 0; idx < calc_data.num_jobs; idx++ ) */
FORKED = TRUE;
} /* if( calc_data.num_jobs > 1 ) */
/* Create the main window */
main_window = create_main_window( &main_window_builder );
gtk_window_set_title( GTK_WINDOW(main_window), PACKAGE_STRING );
gtk_widget_hide( Builder_Get_Object(main_window_builder, "main_hbox1") );
gtk_widget_hide( Builder_Get_Object(main_window_builder, "main_hbox2") );
gtk_widget_hide( Builder_Get_Object(main_window_builder, "main_grid1") );
gtk_widget_hide( Builder_Get_Object(main_window_builder, "main_view_menuitem") );
gtk_widget_hide( Builder_Get_Object(main_window_builder, "structure_frame") );
calc_data.zo = 50.0;
/* Read GUI state config file and reset geometry */
Read_Config();
/* If input file is specified, get the working directory */
if( strlen(rc_config.infile) )
Get_Dirname( rc_config.infile, rc_config.working_dir, NULL );
/* Main window freq spinbutton */
mainwin_frequency = GTK_SPIN_BUTTON(
Builder_Get_Object(main_window_builder, "main_freq_spinbutton") );
/* Get the structure drawing area and allocation */
structure_drawingarea =
Builder_Get_Object(main_window_builder, "structure_drawingarea");
GtkAllocation allocation;
gtk_widget_get_allocation ( structure_drawingarea, &allocation);
structure_width = allocation.width;
structure_height = allocation.height;
New_Projection_Parameters(
structure_width, structure_height,
&structure_proj_params );
/* Initialize structure projection angles */
rotate_structure = GTK_SPIN_BUTTON(
Builder_Get_Object(main_window_builder, "main_rotate_spinbutton"));
incline_structure = GTK_SPIN_BUTTON(
Builder_Get_Object(main_window_builder, "main_incline_spinbutton"));
structure_zoom = GTK_SPIN_BUTTON(
Builder_Get_Object(main_window_builder, "main_zoom_spinbutton"));
structure_fstep_entry = GTK_ENTRY(
Builder_Get_Object(main_window_builder, "structure_fstep_entry")) ;
structure_proj_params.Wr =
gtk_spin_button_get_value(rotate_structure);
structure_proj_params.Wi =
gtk_spin_button_get_value(incline_structure);
structure_proj_params.xy_zoom = 1.0;
structure_proj_params.reset = TRUE;
structure_proj_params.type = STRUCTURE_DRAWINGAREA;
rdpattern_proj_params.xy_zoom = 1.0;
rdpattern_proj_params.reset = TRUE;
rdpattern_proj_params.type = RDPATTERN_DRAWINGAREA;
/* Signal start of xnec2c */
SetFlag( XNEC2C_START );
/* Open input file if specified */
if( strlen(rc_config.infile) > 0 )
g_idle_add( Open_Input_File, (gpointer)(&new) );
else
SetFlag( INPUT_PENDING );
gtk_main ();
return 0;
}
/*-----------------------------------------------------------------------*/
/* Open_Input_File()
*
* Opens NEC2 input file
*/
gboolean
Open_Input_File( gpointer udata )
{
gboolean ok, new;
GtkWidget *widget;
/* Stop freq loop */
if( isFlagSet(FREQ_LOOP_RUNNING) )
Stop_Frequency_Loop();
/* Close open files if any */
Close_File( &input_fp );
/* Suppress activity while input file opened */
ClearFlag( OPEN_INPUT_FLAGS );
SetFlag( INPUT_PENDING );
calc_data.fstep = -1;
/* Open NEC2 input file */
if( strlen(rc_config.infile) == 0 ) return( FALSE );
Open_File( &input_fp, rc_config.infile, "r");
/* Read input file, record failures */
ok = Read_Comments() && Read_Geometry() && Read_Commands();
if( !ok )
{
/* Hide main control buttons etc */
gtk_widget_hide(
Builder_Get_Object(main_window_builder, "main_hbox1") );
gtk_widget_hide(
Builder_Get_Object(main_window_builder, "main_hbox2") );
gtk_widget_hide(
Builder_Get_Object(main_window_builder, "main_grid1") );
gtk_widget_hide(
Builder_Get_Object(main_window_builder, "main_view_menuitem") );
gtk_widget_hide(
Builder_Get_Object(main_window_builder, "structure_frame") );
/* Close plot/rdpat windows if open */
Gtk_Widget_Destroy( rdpattern_window );
Gtk_Widget_Destroy( freqplots_window );
if( nec2_edit_window == NULL )
Open_Nec2_Editor( NEC2_EDITOR_RELOAD );
else
Nec2_Input_File_Treeview( NEC2_EDITOR_CLEAR );
return( FALSE );
} /* if( !ok ) */
SetFlag( INPUT_OPENED );
/* Ask child processes to read input file */
if( FORKED )
{
int idx;
size_t lenc, leni;
lenc = strlen( fork_commands[INFILE] );
leni = strlen( rc_config.infile );
for( idx = 0; idx < num_child_procs; idx++ )
{
Write_Pipe( idx, fork_commands[INFILE], (ssize_t)lenc, TRUE );
Write_Pipe( idx, rc_config.infile, (ssize_t)leni, TRUE );
}
} /* if( FORKED ) */
/* Initialize xnec2c */
SetFlag( COMMON_PROJECTION );
SetFlag( COMMON_FREQUENCY );
SetFlag( MAIN_NEW_FREQ );
if( isFlagSet(PLOT_ENABLED) )
SetFlag( FREQ_LOOP_INIT );
floop_tag = 0;
save.last_freq = 0.0;
crnt.newer = 0;
crnt.valid = 0;
near_field.newer = 0;
near_field.valid = 0;
/* Allow redraws on expose events etc */
ClearFlag( INPUT_PENDING );
/* Set projection at 45 deg rotation and
* inclination if NEC2 editor window is not open */
if( nec2_edit_window == NULL )
New_Viewer_Angle(
45.0, 45.0, rotate_structure,
incline_structure, &structure_proj_params );
New_Structure_Projection_Angle();
/* Show current frequency */
gtk_spin_button_set_value( mainwin_frequency, (gdouble)calc_data.fmhz );
/* Show main control buttons etc */
gtk_widget_show( Builder_Get_Object(main_window_builder, "main_hbox1") );
gtk_widget_show( Builder_Get_Object(main_window_builder, "main_hbox2") );
gtk_widget_show( Builder_Get_Object(main_window_builder, "main_grid1") );
gtk_widget_show( Builder_Get_Object(main_window_builder, "structure_frame") );
gtk_widget_show( Builder_Get_Object(main_window_builder, "main_view_menuitem") );
gtk_widget_show( structure_drawingarea );
/* If currents or charges draw button is active
* re-initialize structure currents/charges drawing */
if( isFlagSet(DRAW_CURRENTS) )
Main_Currents_Togglebutton_Toggled( TRUE );
if( isFlagSet(DRAW_CHARGES) )
Main_Charges_Togglebutton_Toggled( TRUE );
/* Set input file to NEC2 editor. It will only
* happen if the NEC2 editor window is open */
new = *( (gboolean *)udata );
if( new )
Nec2_Input_File_Treeview( NEC2_EDITOR_CLEAR );
else
Nec2_Input_File_Treeview( NEC2_EDITOR_RELOAD );
/* Re-initialize Rad Pattern drawing if window open */
if( rdpattern_window != NULL )
{
widget = Builder_Get_Object(
rdpattern_window_builder, "rdpattern_zoom_spinbutton" );
gtk_spin_button_set_value(
GTK_SPIN_BUTTON(widget), (gdouble)rc_config.rdpattern_zoom_spinbutton );
/* Simulate activation of main rdpattern button */
Main_Rdpattern_Activate( FALSE );
crnt.valid = 0;
if( isFlagSet(DRAW_GAIN) )
Rdpattern_Gain_Togglebutton_Toggled( TRUE );
else if( isFlagSet(DRAW_EHFIELD) )
Rdpattern_EH_Togglebutton_Toggled( TRUE );
else
{
Rdpattern_Gain_Togglebutton_Toggled( FALSE );
Rdpattern_EH_Togglebutton_Toggled( FALSE );
}
GtkWidget *box =
Builder_Get_Object( rdpattern_window_builder, "rdpattern_box" );
gtk_widget_show( box );
}
/* Re-initiate plots if window open */
if( freqplots_window != NULL )
{
GtkWidget *box =
Builder_Get_Object( freqplots_window_builder, "freqplots_box" );
gtk_widget_show( box );
if( rc_config.main_loop_start )
{
Main_Freqplots_Activate();
Start_Frequency_Loop();
}
}
/* Restore main window projection settings */
if( isFlagSet(XNEC2C_START) )
{
widget = Builder_Get_Object(
main_window_builder, "main_rotate_spinbutton" );
gtk_spin_button_set_value(
GTK_SPIN_BUTTON(widget), (gdouble)rc_config.main_rotate_spinbutton );
widget = Builder_Get_Object(
main_window_builder, "main_incline_spinbutton" );
gtk_spin_button_set_value(
GTK_SPIN_BUTTON(widget), (gdouble)rc_config.main_incline_spinbutton );
widget = Builder_Get_Object(
main_window_builder, "main_zoom_spinbutton" );
gtk_spin_button_set_value(
GTK_SPIN_BUTTON(widget), (gdouble)rc_config.main_zoom_spinbutton );
ClearFlag( XNEC2C_START );
}
/* Open NEC2 editor if there is a saved geometry state */
GtkMenuItem *menu_item;
if( rc_config.nec2_edit_width &&
rc_config.nec2_edit_height )
{
menu_item = GTK_MENU_ITEM(
Builder_Get_Object(main_window_builder, "nec2_edit") );
gtk_menu_item_activate( menu_item );
}
return( FALSE );
} /* Open_Input_File() */
/*------------------------------------------------------------------------*/
static void sig_handler( int signal )
{
switch( signal )
{
case SIGINT:
/* Save GUI state for restoring windows */
Get_GUI_State();
Save_Config();
fprintf( stderr, _("xnec2c: exiting via user interrupt\n") );
break;
case SIGSEGV:
fprintf( stderr, _("xnec2c: segmentation fault, exiting\n") );
break;
case SIGFPE:
fprintf( stderr, _("xnec2c: floating point exception, exiting\n") );
break;
case SIGABRT:
fprintf( stderr, _("xnec2c: abort signal received, exiting\n") );
break;
case SIGTERM:
/* Save GUI state for restoring windows */
Get_GUI_State();
Save_Config();
fprintf( stderr, _("xnec2c: termination request received, exiting\n") );
break;
case SIGCHLD:
{
int idx;
pid_t pid = getpid();
if( !FORKED )
{
fprintf( stderr,
_("xnec2c: not forked, ignoring SIGCHLD from pid %d\n"), pid );
return;
}
else
{
for( idx = 0; idx < calc_data.num_jobs; idx++ )
if( forked_proc_data[idx]->child_pid == pid )
{
fprintf( stderr, _("xnec2c: child process pid %d exited\n"), pid );
if( isFlagSet(MAIN_QUIT) ) return;
else break;
}
return;
}
}
default:
fprintf( stderr, _("xnec2c: default exit with signal: %d\n"), signal );
} /* switch( signal ) */
/* Kill child processes */
if( FORKED && !CHILD )
while( num_child_procs )
{
num_child_procs--;
kill( forked_proc_data[num_child_procs]->child_pid, SIGKILL );
}
Close_File( &input_fp );
if( CHILD ) _exit( 0 );
else exit( signal );
} /* End of sig_handler() */
/*------------------------------------------------------------------------*/
/* Tests for child process */
gboolean
isChild(void)
{
return( child_pid == (pid_t)(0) );
}
/*------------------------------------------------------------------------*/
|