getcar.c

Go to the documentation of this file.
00001 
00002 #include "getcar.h"
00003 
00004 #include "sic_comm.h"
00005 #include "sic_error.h"
00006 #include "sic_launch.h"
00007 #include "sic_util.h"
00008 
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include <string.h>
00012 #include <ctype.h>
00013 #include <fcntl.h>
00014 #ifndef WIN32
00015 #include <unistd.h>
00016 #include <sys/time.h>
00017 #else
00018 #include "win32.h"
00019 #endif
00020 
00021 #define YES 1
00022 #define NO 0
00023 #define TRANSLATION_MAX_LENGTH 256
00024 
00025 static char temporary_file[512];
00026 static char temporary_name[256];
00027 
00028 win_struct_t detach_win[NWINDOWS];
00029 
00030 static FILE *fd;
00031 static char keychain[64];
00032 static int SHARE=0;
00033 
00034 static unsigned long timervalue = 0;
00035 
00036 /* Reset timer value to val */
00037 static void set_timer( unsigned long val)
00038 {
00039 #ifndef WIN32
00040     struct itimerval rttimer;
00041 
00042     rttimer.it_value.tv_sec = val;
00043     rttimer.it_value.tv_usec = 0;
00044     rttimer.it_interval.tv_sec = 0;
00045     rttimer.it_interval.tv_usec = 0;
00046 
00047     setitimer( ITIMER_REAL, &rttimer, NULL);
00048 #endif
00049 }
00050 
00051 /* Put a flag to make the task mono or multi user */
00052 void CFC_API xgag_key( CFC_FzString key)
00053 {
00054     SHARE = YES;
00055     CFC_fz2c_strcpy( keychain, key);
00056 }
00057 
00058 /* set a timer in order to exit after nh hours doing nothing */
00059 void CFC_API xgag_timer( int *nh)
00060 {
00061     int val;
00062 
00063     printf( "Timer value : %d hours\n", *nh);
00064     val = ((*nh) * 3600);
00065     if (val < 0)
00066         val = 0;
00067     timervalue = (unsigned long)val;
00068 }
00069 
00070 /* Pause the execution of an temporary X-Window detached process
00071    end inhibate command in permanent X-Menus which beep on user actions */
00072 void CFC_API xgag_wait( )
00073 {
00074     /* Pause, allowing a sic_xinput panel to set a number of variables
00075      * while executing a procedure. Unsupported with client server XML.
00076      */
00077     static int notDone = 1, doWait = 1;
00078 
00079     if (notDone) {
00080         if (strcasecmp( sic_s_get_translation( "GAG_WIDGETS"), "XML") == 0) {
00081             doWait = 0;
00082             notDone = 0;
00083         }
00084     }
00085     if (doWait) {
00086         command_line_t command_line;
00087         sic_command_from_t from;
00088 
00089         printf( "Waiting ...\n");
00090 
00091         /* TODO: look for the good place to call sic_wait_command */
00092         /* wait for GUI\END */
00093         do {
00094             sic_wait_command( &command_line, &from);
00095         } while (strcmp( command_line.line, "GUI\\END"));
00096     } else {
00097         printf( "Warning, GUI\\PAUSE unsupported by XML widgets,\n will give unpredictable results. \n");
00098     }
00099 }
00100 
00101 /* When the main task must be mono-user the command memory
00102    segment must be shared between all instances */
00103 void CFC_API search_clients( int *code)
00104 {
00105     *code = (sic_get_comm_id( ) == -1) ? 1 : 0;
00106 
00107 #ifndef WIN32
00108 #ifndef darwin
00109     setpgrp( );
00110 #else
00111     setpgid( 0, 0);                     /* This should also work on Linux */
00112 #endif
00113 #endif
00114 
00115     /* set error handlers */
00116     /* signal( SIGINT, SIG_IGN); */ /* done by calling trap_ctrlc */
00117 
00118     if (*code == 1) {              /* First client ... */
00119         sic_create_comm_board( );
00120     } else {                       /* remote client ... */
00121         sic_open_comm_board( sic_get_comm_id( ));
00122     }
00123 }
00124 
00125 void CFC_API sic_c_do_exit( int *status)
00126 {
00127     sic_do_exit( *status);
00128 }
00129 
00130 void CFC_API sic_c_on_exit( )
00131 {
00132     sic_on_exit( );
00133 }
00134 
00135 void CFC_API get_com_params( int *comm_id, int *unused_1, int *unused_2)
00136 {
00137     *comm_id = sic_get_comm_id( );
00138     *unused_1 = -1;
00139     *unused_2 = -1;
00140 }
00141 
00142 /***************************************************************************
00143   If the main task runs in mono-user mode, an instance of SIC_KEYBOARD 
00144   is created at each call of the task.
00145   Otherwise, the subsequent instances of the task will call prompt_loop with
00146   *code set to zero which override the executing task with a REMOTE_CLIENT 
00147   control task, so that there is always a single instance of the named task 
00148   currently running.
00149   ****************************************************************************/
00150 void CFC_API prompt_loop( int *code)
00151 {
00152     task_t keyboard_task = SIC_TASK_NULL;
00153     int i, keyfd;
00154     char buff[16];
00155     char keyfile[128];
00156 
00157     if (*code) {
00158         keyboard_task = launch_keyboard( );
00159 
00160         if (keyboard_task == SIC_TASK_NULL) {
00161             *code = -1;
00162             return;
00163         }
00164 #ifdef SIC_RUN_SIC_SPY
00165         argv = sic_get_static_argv( );
00166         i = 0;
00167         sprintf( argv[i++], "sic_spy");
00168         sprintf( argv[i++], "%d", sic_get_comm_id( ));
00169         argv[i++] = NULL;
00170 
00171         if (sic_launch( argv) < 0) {
00172             *code = -1;
00173             return;
00174         }
00175 #endif /* SIC_RUN_SIC_SPY */
00176 
00177         if (SHARE) {
00178             /* Parent process write keyboard_task id for other clients */
00179             sprintf( keyfile, "%s%s", "/tmp/", keychain);
00180             if ((keyfd = open( keyfile, O_WRONLY)) == -1) {
00181                 printf( "error open");
00182                 *code = -1;
00183                 return;
00184             }
00185 
00186             strncpy( buff, "0", 16);
00187             sprintf( buff, "%d", sic_get_task_id( keyboard_task));
00188             /* Move file pointer to keyboard_task id position */
00189             if (lseek( keyfd, 48L, 0L) == -1) {
00190                 printf( "error lseek");
00191                 *code = -1;
00192                 return;
00193             }
00194 
00195             write( keyfd, buff, 16);
00196 
00197             close( keyfd);
00198         }
00199 
00200         /* Init tem_win struct */
00201         for (i = 0; i < NWINDOWS; i++)
00202             strcpy( detach_win[i].name, "NONE");
00203 
00204     } else {
00205         run_xremote( );
00206     }
00207 }
00208 
00209 static void redraw_prompt( CFC_FzString prompt, CFC_FzString line, int code, sic_command_from_t from)
00210 {
00211     command_line_t command_line;
00212 
00213     CFC_fz2c_strcpy( command_line.prompt, prompt);
00214     if (code)
00215         CFC_fz2c_strcpy( command_line.line, line);
00216     else
00217         command_line.line[0] = '\0';
00218     command_line.nc = 0;
00219     command_line.code = code;
00220     if (from == SIC_KEYBOARD)
00221         sic_post_prompt( &command_line);
00222     else if (from != SIC_SILENT_MODE)
00223         sic_fire_redraw_prompt_event( &command_line);
00224 }
00225 
00226 /*********************************************************************
00227   First send the result of last executed command to the keyboard 
00228   manager program. 
00229   Then block on semaphore and read the command to be executed in shared memory
00230   at the reception of a signal form X-Window Application or at semaphore
00231   released by the keyboard manager program.
00232   ********************************************************************/
00233 int CFC_API read_linec( CFC_FzString prompt, CFC_FzString line, int *code, int *wait_command)
00234 {
00235     static sic_command_from_t from = SIC_KEYBOARD;
00236     command_line_t command_line;
00237 
00238     redraw_prompt( prompt, line, *code, from);
00239 
00240     if (timervalue != 0)
00241         set_timer( timervalue);
00242 
00243     do {
00244         sic_wait_command( &command_line, &from);
00245     } while (!*wait_command && from != SIC_KEYBOARD);
00246 
00247     if (from != SIC_KEYBOARD && from != SIC_SILENT_MODE)
00248         sic_suspend_prompt( );
00249 
00250     if (timervalue != 0)
00251         set_timer( 1000000);
00252 
00253     /* TODO: see if still needed */
00254     /*
00255        if (strncmp( command_line.line, "GTVL\\FLUSH", 11) == 0)
00256        return_prompt = NO;
00257        else
00258        return_prompt = YES;
00259      */
00260 
00261     CFC_c2fz_strcpy( prompt, command_line.prompt);
00262     CFC_c2fz_strcpy( line, command_line.line);
00263     *code = command_line.code;
00264     return (int)command_line.nc;
00265 }
00266 
00267 /* Prepare a temporary file to create a detached X-Window process */
00268 void CFC_API xgag_detach( CFC_FzString title, CFC_FzString name)
00269 {
00270 #ifndef WIN32
00271     struct timeval tp;
00272     struct timezone tzp;
00273 
00274     if (gettimeofday( &tp, &tzp) != 0)
00275         sic_perror( "gettimeofday");
00276 
00277     sprintf( temporary_file, "%s%s%ld.%ld", sic_s_get_translation( "GAG_TMP:"), "gag_tmp", tp.tv_sec, tp.tv_usec);
00278 #else
00279     char temp_path[MAX_PATH];
00280 
00281     GetTempPath( MAX_PATH, temp_path);
00282     GetTempFileName( temp_path, "tmp", 0, temporary_file);
00283 #endif
00284     sprintf( temporary_name, "%s", CFC_fz2c_string( name));
00285     fd = fopen( temporary_file, "w");
00286         if (fd == NULL) {
00287                 fprintf( stderr, "E-DIALOGS,  Cannot create %s\n", temporary_file);
00288                 sic_do_exit( 1);
00289         }
00290     fprintf( fd, "%s\n", CFC_fz2c_string( title));
00291     fprintf( fd, "%s\n", CFC_fz2c_string( name));
00292 }
00293 
00294 /* Start a command menu */
00295 void CFC_API xgag_menu( CFC_FzString label)
00296 {
00297     fprintf( fd, "%s\n", "MENU");
00298     fprintf( fd, "%s\n", CFC_fz2c_string( label));
00299 }
00300 
00301 /* End a command menu */
00302 void CFC_API xgag_endmenu( )
00303 {
00304     fprintf( fd, "%s\n", "ENDMENU");
00305 }
00306 
00307 /* Add a command in a menu */
00308 void CFC_API xgag_command( CFC_FzString label, CFC_FzString command)
00309 {
00310     fprintf( fd, "%s\n", CFC_fz2c_string( label));
00311     fprintf( fd, "%s\n", CFC_fz2c_string( command));
00312 }
00313 
00314 /* Create the detached X-Window process */
00315 void CFC_API xgag_launch( )
00316 {
00317     int i;
00318     task_t launch_task;
00319 
00320     fclose( fd);
00321 
00322     launch_task = launch_xmenu( temporary_file);
00323     for (i = 0; i < NWINDOWS; i++) {
00324         if (strcmp( detach_win[i].name, "NONE") == 0) {
00325             strcpy( detach_win[i].name, temporary_name);
00326             detach_win[i].task = launch_task;
00327             break;
00328         }
00329     }
00330 }
00331 
00332 void CFC_API xgag_end_detach( CFC_FzString name)
00333 {
00334     int i;
00335 
00336     for (i = 0; i < NWINDOWS; i++) {
00337         if (strcmp( detach_win[i].name, CFC_fz2c_string( name)) == 0) {
00338             if (!sic_terminate( detach_win[i].task)) {
00339                 strcpy( detach_win[i].name, "NONE");
00340             }
00341             break;
00342         }
00343     }
00344 }
00345 

Generated on Tue Mar 13 15:15:39 2007 for SIC by  doxygen 1.5.1