Example #1
0
/* start file-based monitor */
void start(const char *name, const char *output)
{
    char *env_name;

    if (!output)
	return;
    
    env_name = NULL;
    G_asprintf(&env_name, "MONITOR_%s_MAPFILE", name);
    G_setenv(env_name, output);
}
Example #2
0
/* start wxGUI display monitor */
void start_wx(const char *name, const char *tempfile,
	      const char *env_value, const char *cmd_value,
	      const char *width, const char *height)
{
    char progname[GPATH_MAX];
    char *env_name, *map_value;

    env_name = NULL;
    G_asprintf(&env_name, "MONITOR_%s_MAPFILE", name);
    G_asprintf(&map_value, "%s.ppm", tempfile);
    G_setenv(env_name, map_value);
    /* close(creat(map_value, 0666)); */
    
    G_debug(3, "       mapfile = %s", map_value);

    sprintf(progname, "%s/etc/gui/wxpython/gui_modules/mapdisp.py", G_gisbase());
    G_spawn_ex(getenv("GRASS_PYTHON"), progname, progname,
	       name, map_value, cmd_value, env_value, width ? width : "", height ? height : "", SF_BACKGROUND, NULL);
}
Example #3
0
File: main.c Project: caomw/grass
int main(int argc, char *argv[])
{
    struct Option *type, *rc_file;
    struct Flag *update, *nolaunch;
    struct GModule *module;
    const char *gui_type_env;
    char progname[GPATH_MAX];
    char *desc;

    G_gisinit(argv[0]);

    module = G_define_module();
    G_add_keyword(_("general"));
    G_add_keyword(_("gui"));
    G_add_keyword(_("user interface"));
    module->label =
	_("Launches a GRASS graphical user interface (GUI) session.");
    module->description = _("And updates default user interface settings.");

    type = G_define_option();
    type->key = "ui";
    type->type = TYPE_STRING;
    type->label = _("User interface");
    type->description = _("Default value: GRASS_GUI if defined otherwise wxpython");
    desc = NULL;
    G_asprintf(&desc,
	        "wxpython;%s;text;%s",
	        _("wxPython based GUI (wxGUI)"),
	        _("command line interface only"));
    type->descriptions = desc;
    type->options = "wxpython,text";
    type->guisection = _("Type");
    
    rc_file = G_define_standard_option(G_OPT_F_INPUT);
    rc_file->key = "workspace";
    rc_file->required = NO;
    rc_file->key_desc = "name.gxw";
    rc_file->description = _("Name of workspace file to load on start-up (valid only for wxGUI)");

    update = G_define_flag();
    update->key = 'd';
    update->description = _("Update default user interface settings");
    update->guisection = _("Default");

    nolaunch = G_define_flag();
    nolaunch->key = 'n';
    nolaunch->description =
	_("Do not launch GUI after updating the default user interface settings");
    nolaunch->guisection = _("Default");

    if (G_parser(argc, argv))
	exit(EXIT_FAILURE);


    if (type->answer && strcmp(type->answer, "text") == 0 &&
	!nolaunch->answer)
	nolaunch->answer = TRUE;
    
    if (nolaunch->answer && !update->answer)
	update->answer = TRUE;
    
    gui_type_env = G__getenv("GUI");

    if (!type->answer) {
	if (gui_type_env && strcmp(gui_type_env, "text")) {
	    type->answer = G_store(gui_type_env);
	}
	else {
	    type->answer = "wxpython";
	}
    }

    if (((gui_type_env && update->answer) &&
	 strcmp(gui_type_env, type->answer) != 0) || !gui_type_env) {
	G_setenv("GUI", type->answer);
	G_message(_("<%s> is now the default GUI"), type->answer);
    }
    else {
	if(update->answer)
	    if(gui_type_env) {
		G_debug(1, "No change: old gui_type_env=[%s], new type->ans=[%s]",
			gui_type_env, type->answer);
	    }
    }

    if(nolaunch->answer)
	exit(EXIT_SUCCESS);


    G_message(_("Launching <%s> GUI in the background, please wait..."), type->answer);

    if (strcmp(type->answer, "wxpython") == 0) {
	sprintf(progname, "%s/gui/wxpython/wxgui.py", G_gisbase());
	if (rc_file->answer) {
	    G_spawn_ex(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), progname,
		    "--workspace", rc_file->answer, SF_BACKGROUND, NULL);
	}
	else {
	    G_spawn_ex(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), progname,
		    SF_BACKGROUND, NULL);
	}
    }

    /* stop the impatient from starting it again
        before the splash screen comes up */
    G_sleep(3);

    exit(EXIT_SUCCESS);
}
Example #4
0
int GRASS_LIB_EXPORT QgsGrassGisLib::G__gisinit( const char * version, const char * programName )
{
    Q_UNUSED( version );
    // We use this function also to init our fake lib
    QgsDebugMsg( QString( "version = %1 programName = %2" ).arg( version ).arg( programName ) );

    // Init providers path
    int argc = 1;
    char **argv = new char*[1];
    argv[0] = qstrdup( programName );


    // unfortunately it seems impossible to get QGIS prefix
    // QCoreApplication::applicationDirPath() returns $GISBASE/lib on Linux
#if 0
    QDir dir( QCoreApplication::applicationDirPath() );
    dir.cdUp();
    QString prefixPath = dir.absolutePath();
#endif

    //QCoreApplication app( argc, argv ); // to init paths
    QgsApplication app( argc, argv, false ); // to init paths

    // TODO: WCS (network) fails with: "QTimer can only be used with threads started
    // with QThread" because QCoreApplication::exec() was not called, but
    // QCoreApplication::exec() goes to loop. We need to start QThread somehow.

    // QGIS_PREFIX_PATH should be loaded by QgsApplication
    //QString prefixPath = getenv( "QGIS_PREFIX_PATH" );
    //if ( prefixPath.isEmpty() )
    //{
    //  fatal( "Cannot get QGIS_PREFIX_PATH" );
    //}
    //QgsApplication::setPrefixPath( prefixPath, true );

    QgsDebugMsg( "Plugin path: " + QgsApplication::pluginPath() );
    QgsProviderRegistry::instance( QgsApplication::pluginPath() );

    QgsDebugMsg( "qgisSettingsDirPath = " + app.qgisSettingsDirPath() );

    G_set_error_routine( &errorRoutine );
    G_set_gisrc_mode( G_GISRC_MODE_MEMORY );
    G_setenv( "OVERWRITE", "1" );  // avoid checking if map exists

    G_suppress_masking();

#if GRASS_VERSION_MAJOR<6 || (GRASS_VERSION_MAJOR == 6 && GRASS_VERSION_MINOR <= 4)
    G__init_null_patterns();
#endif

    // Read projection if set
    //mCrs.createFromOgcWmsCrs( "EPSG:900913" );
    QString crsStr = getenv( "QGIS_GRASS_CRS" );

    QgsDebugMsg( "Setting CRS to " + crsStr );

    if ( !crsStr.isEmpty() )
    {
        if ( !mCrs.createFromProj4( crsStr ) )
        {
            fatal( "Cannot create CRS from QGIS_GRASS_CRS: " + crsStr );
        }
        //TODO: createFromProj4 used to save to the user database any new CRS
        // this behavior was changed in order to separate creation and saving.
        // Not sure if it necessary to save it here, should be checked by someone
        // familiar with the code (should also give a more descriptive name to the generated CRS)
        if ( mCrs.srsid() == 0 )
        {
            QString myName = QString( " * %1 (%2)" )
                             .arg( QObject::tr( "Generated CRS", "A CRS automatically generated from layer info get this prefix for description" ) )
                             .arg( mCrs.toProj4() );
            mCrs.saveAsUserCRS( myName );
        }
    }
    mDistanceArea.setSourceCrs( mCrs.srsid() );

    // Read region fron environment variable
    // QGIS_GRASS_REGION=west,south,east,north,cols,rows
#if 0
    QString regionStr = getenv( "QGIS_GRASS_REGION" );
    QStringList regionList = regionStr.split( "," );
    if ( regionList.size() != 6 )
    {
        fatal( "Cannot read region from QGIS_GRASS_REGION environment variable" );
    }

    double xMin, yMin, xMax, yMax;
    int cols, rows;
    bool xMinOk, yMinOk, xMaxOk, yMaxOk, colsOk, rowsOk;
    xMin = regionList.value( 0 ).toDouble( &xMinOk );
    yMin = regionList.value( 1 ).toDouble( &yMinOk );
    xMax = regionList.value( 2 ).toDouble( &xMaxOk );
    yMax = regionList.value( 3 ).toDouble( &yMaxOk );
    cols = regionList.value( 4 ).toInt( &colsOk );
    rows = regionList.value( 5 ).toInt( &rowsOk );

    if ( !xMinOk || !yMinOk || !xMaxOk || !yMaxOk || !colsOk || !rowsOk )
    {
        fatal( "Cannot parse QGIS_GRASS_REGION" );
    }

    struct Cell_head window;
    window.west = xMin;
    window.south = yMin;
    window.east = xMax;
    window.north = yMax;
    window.rows = rows;
    window.cols = cols;

    char* err = G_adjust_Cell_head( &window, 1, 1 );
    if ( err )
    {
        fatal( QString( err ) );
    }
    G_set_window( &window );
#endif

    QString regionStr = getenv( "GRASS_REGION" );
    if ( regionStr.isEmpty() )
    {
        fatal( "GRASS_REGION environment variable not set" );
    }

    QgsDebugMsg( "Getting region via true lib from GRASS_REGION: " +  regionStr );
    // GRASS true lib reads GRASS_REGION environment variable
    G_get_window( &mWindow );

    mExtent = QgsRectangle( mWindow.west, mWindow.south, mWindow.east, mWindow.north );
    mRows = mWindow.rows;
    mColumns = mWindow.cols;
    mXRes = mExtent.width() / mColumns;
    mYRes = mExtent.height() / mColumns;

    QgsDebugMsg( "End" );
    return 0;
}
Example #5
0
int start_mon(const char *name, const char *output, int select,
	      const char *width, const char *height, const char *bgcolor,
	      int truecolor)
{
    const char *curr_mon;
    char *env_name, *env_value, *cmd_value;
    char *tempfile, buf[1024];
    int env_fd;
    
    curr_mon = G__getenv("MONITOR");
    if (curr_mon && strcmp(curr_mon, name) == 0 && check_mon(curr_mon))
	G_fatal_error(_("Monitor <%s> already running"), name);
    
    tempfile = G_tempfile();

    env_name = env_value = NULL;
    G_asprintf(&env_name, "MONITOR_%s_ENVFILE", name);
    G_asprintf(&env_value, "%s.env", tempfile);
    G_setenv(env_name, env_value);
    env_fd = creat(env_value, 0666);
    if (env_fd < 0)
	G_fatal_error(_("Unable to create file '%s'"), env_value);
    if (width) {
	sprintf(buf, "GRASS_WIDTH=%s\n", width);
	write(env_fd, buf, strlen(buf));
    }
    if (height) {
	sprintf(buf, "GRASS_HEIGHT=%s\n", height);
	write(env_fd, buf, strlen(buf));
    }
    if (bgcolor) {
	if (strcmp(bgcolor, "none") == 0)
	    sprintf(buf, "GRASS_TRANSPARENT=TRUE\n");
	else
	    sprintf(buf, "GRASS_BACKGROUNDCOLOR=%s\n", bgcolor);
	write(env_fd, buf, strlen(buf));
    }
    if (truecolor) {
	sprintf(buf, "GRASS_TRUECOLOR=TRUE\n");
	write(env_fd, buf, strlen(buf));
    }
    close(env_fd);

    cmd_value = NULL;
    G_asprintf(&env_name, "MONITOR_%s_CMDFILE", name);
    G_asprintf(&cmd_value, "%s.cmd", tempfile);
    G_setenv(env_name, cmd_value);
    close(creat(cmd_value, 0666));

    G_verbose_message(_("Staring monitor <%s> with env file '%s'"), name, env_value);
    
    G_debug(1, "start: name=%s ", name);
    G_debug(3, "       envfile = %s", env_value);
    G_debug(3, "       cmdfile = %s", cmd_value);
    
    if (select)
	G_setenv("MONITOR", name);
    
    if (strncmp(name, "wx", 2) == 0) /* use G_strncasecmp() instead */
	start_wx(name, tempfile, env_value, cmd_value, 
		 width, height);
    else
	start(name, output);
    
    return 0;
}
Example #6
0
int main(int argc, char *argv[])
{
    struct Option *type, *rc_file;
    struct Flag *update_ui, *fglaunch, *nolaunch;
    struct GModule *module;
    const char *gui_type_env;
    char progname[GPATH_MAX];
    char *desc;

    G_gisinit(argv[0]);

    module = G_define_module();
    G_add_keyword(_("general"));
    G_add_keyword(_("GUI"));
    G_add_keyword(_("user interface"));

    module->label =
        _("Launches a GRASS graphical user interface (GUI) session.");
    module->description = _("Optionally updates default user interface settings.");

    type = G_define_option();
    type->key = "ui";
    type->type = TYPE_STRING;
    type->description = _("User interface");
    desc = NULL;
    G_asprintf(&desc,
               "wxpython;%s;text;%s;gtext;%s;",
               _("wxPython based GUI (wxGUI)"),
               _("command line interface only"),
               _("command line interface with GUI startup screen"));
    type->descriptions = desc;
    type->options = "wxpython,text,gtext";
    type->answer = "wxpython";
    type->guisection = _("Type");

    rc_file = G_define_standard_option(G_OPT_F_INPUT);
    rc_file->key = "workspace";
    rc_file->required = NO;
    rc_file->key_desc = "name.gxw";
    rc_file->label = _("Name of workspace file to load on start-up");
    rc_file->description = _("This is valid only for wxGUI (wxpython)");

    fglaunch = G_define_flag();
    fglaunch->key = 'f';
    fglaunch->label = _("Start GUI in the foreground");
    fglaunch->description = _("By default the GUI starts in the background"
                              " and control is immediately returned to the caller."
                              " When GUI runs in foregreound, it blocks the command line");

    update_ui = G_define_flag();
    update_ui->key = 'd';
    update_ui->description = _("Update default user interface settings");
    update_ui->guisection = _("Default");

    nolaunch = G_define_flag();
    nolaunch->key = 'n';
    nolaunch->description =
        _("Do not launch GUI after updating the default user interface settings");
    nolaunch->guisection = _("Default");

    if (G_parser(argc, argv))
        exit(EXIT_FAILURE);

    gui_type_env = G_getenv_nofatal("GUI");
    G_debug(1, "GUI: %s", gui_type_env ? gui_type_env : "unset");
    if (update_ui->answer) {
        if (!gui_type_env || strcmp(type->answer, gui_type_env)) {
            G_setenv("GUI", type->answer);
            G_message(_("<%s> is now the default GUI"), type->answer);
        }
    }

    if(strcmp(type->answer, "wxpython") != 0 || nolaunch->answer) {
        if (!update_ui->answer)
            G_warning(_("Nothing to do. For setting up <%s> as default UI use -%c flag."),
                      type->answer, update_ui->key);
        exit(EXIT_SUCCESS);
    }

    sprintf(progname, "%s/gui/wxpython/wxgui.py", G_gisbase());
    if (access(progname, F_OK) == -1)
        G_fatal_error(_("Your installation doesn't include GUI, exiting."));

    if (fglaunch->answer) {
        G_message(_("Launching <%s> GUI, please wait..."), type->answer);
        if (rc_file->answer) {
            G_spawn_ex(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), progname,
                       "--workspace", rc_file->answer, NULL);
        }
        else {
            G_spawn_ex(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), progname,
                       NULL);
        }
    }
    else {
        G_message(_("Launching <%s> GUI in the background, please wait..."), type->answer);
        if (rc_file->answer) {
            G_spawn_ex(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), progname,
                       "--workspace", rc_file->answer, SF_BACKGROUND, NULL);
        }
        else {
            G_spawn_ex(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), progname,
                       SF_BACKGROUND, NULL);
        }
        /* stop the impatient from starting it again
           before the splash screen comes up */
        G_sleep(3);
    }

    exit(EXIT_SUCCESS);
}
Example #7
0
int main(int argc, char *argv[])
{
    int ret;
    struct GModule *module;
    struct Option *gisdbase_opt, *location_opt, *mapset_opt;
    struct Flag *f_add, *f_list;
    char *gisdbase_old, *location_old, *mapset_old;
    char *gisdbase_new, *location_new, *mapset_new;
    char *gis_lock;
    char *mapset_old_path, *mapset_new_path;
    char *lock_prog;
    char path[GPATH_MAX];
    char *shell, *monitor;
    struct MON_CAP *cap;

    G_gisinit(argv[0]);

    module = G_define_module();
    module->keywords = _("general, settings");
    module->label = _("Changes current mapset.");
    module->description = _("Optionally create new mapset or list available mapsets in given location.");
    
    mapset_opt = G_define_option();
    mapset_opt->key = "mapset";
    mapset_opt->type = TYPE_STRING;
    mapset_opt->required = NO;
    mapset_opt->multiple = NO;
    mapset_opt->description = _("Name of mapset where to switch");
    mapset_opt->guisection = _("Settings");

    location_opt = G_define_option();
    location_opt->key = "location";
    location_opt->type = TYPE_STRING;
    location_opt->required = NO;
    location_opt->multiple = NO;
    location_opt->description = _("Location name (not location path)");
    location_opt->guisection = _("Settings");

    gisdbase_opt = G_define_option();
    gisdbase_opt->key = "gisdbase";
    gisdbase_opt->type = TYPE_STRING;
    gisdbase_opt->required = NO;
    gisdbase_opt->multiple = NO;
    gisdbase_opt->key_desc = "path";
    gisdbase_opt->description =
	_("GIS data directory (full path to the directory where the new location is)");
    gisdbase_opt->guisection = _("Settings");
    
    f_add = G_define_flag();
    f_add->key = 'c';
    f_add->description = _("Create mapset if it doesn't exist");
    f_add->answer = FALSE;
    f_add->guisection = _("Create");

    f_list = G_define_flag();
    f_list->key = 'l';
    f_list->description = _("List available mapsets");
    f_list->guisection = _("Print");
    
    if (G_parser(argc, argv))
	exit(EXIT_FAILURE);

    if (!mapset_opt->answer && !f_list->answer)
	G_fatal_error(_("Either mapset= or -l must be used"));

    /* Store original values */
    gisdbase_old = G__getenv("GISDBASE");
    location_old = G__getenv("LOCATION_NAME");
    mapset_old = G__getenv("MAPSET");
    G_asprintf(&mapset_old_path, "%s/%s/%s", gisdbase_old, location_old,
	       mapset_old);
    monitor = G__getenv("MONITOR");

    /* New values */
    if (gisdbase_opt->answer)
	gisdbase_new = gisdbase_opt->answer;
    else
	gisdbase_new = gisdbase_old;

    if (location_opt->answer)
	location_new = location_opt->answer;
    else
	location_new = location_old;

    if (f_list->answer) {
	char **ms;
	int nmapsets;

	G__setenv("LOCATION_NAME", location_new);
	G__setenv("GISDBASE", gisdbase_new);

	ms = G_available_mapsets();

	for (nmapsets = 0; ms[nmapsets]; nmapsets++) {
	    if (G__mapset_permissions(ms[nmapsets]) > 0) {
		fprintf(stdout, "%s ", ms[nmapsets]);
	    }
	}
	fprintf(stdout, "\n");

	exit(EXIT_SUCCESS);
    }

    mapset_new = mapset_opt->answer;
    G_asprintf(&mapset_new_path, "%s/%s/%s", gisdbase_new, location_new,
	       mapset_new);

    /* TODO: this should be checked better (repeated '/' etc.) */
    if (strcmp(mapset_old_path, mapset_new_path) == 0)
	G_fatal_error(_("<%s> is already the current mapset"), mapset_new);

    /* Check if the mapset exists and user is owner */
    G_debug(2, "check : %s", mapset_new_path);

    ret = G__mapset_permissions2(gisdbase_new, location_new, mapset_new);
    switch (ret) {
    case 0:
	G_fatal_error(_("You don't have permission to use this mapset"));
	break;
    case -1:
	if (f_add->answer == TRUE) {
	    G_debug(2, "Mapset %s doesn't exist, attempting to create it",
		    mapset_new);
	    G_make_mapset(gisdbase_new, location_new, mapset_new);
	}
	else
	    G_fatal_error(_("The mapset does not exist. Use -c flag to create it."));
	break;
    default:
	break;
    }

    /* Check if the mapset is in use */
    gis_lock = getenv("GIS_LOCK");
    if (!gis_lock)
	G_fatal_error(_("Unable to read GIS_LOCK environment variable"));

    G_asprintf(&lock_prog, "%s/etc/lock", G_gisbase());

    sprintf(path, "%s/.gislock", mapset_new_path);
    G_debug(2, path);

    ret = G_spawn(lock_prog, lock_prog, path, gis_lock, NULL);
    G_debug(2, "lock result = %d", ret);
    G_free(lock_prog);

    /* Warning: the value returned by system() is not that returned by exit() in executed program
     *          e.g. exit(1) -> 256 (multiplied by 256) */
    if (ret != 0)
	G_fatal_error(_("%s is currently running GRASS in selected mapset or lock file cannot be checked"),
		      G_whoami());

    /* Erase monitors */
    G_message(_("Erasing monitors..."));
    while ((cap = R_parse_monitorcap(MON_NEXT, "")) != NULL) {
	G__setenv("MONITOR", cap->name);
	R__open_quiet();
	if (R_open_driver() == 0) {
	    D_erase(DEFAULT_BG_COLOR);
	    D_add_to_list("d.erase");
	    R_close_driver();
	    R_release_driver();
	}
    }
    if (monitor)
	G_setenv("MONITOR", monitor);

    /* Clean temporary directory */
    sprintf(path, "%s/etc/clean_temp", G_gisbase());
    G_verbose_message(_("Cleaning up temporary files..."));
    G_spawn(path, "clean_temp", NULL);

    /* Reset variables */
    G_setenv("GISDBASE", gisdbase_new);
    G_setenv("LOCATION_NAME", location_new);
    G_setenv("MAPSET", mapset_new);

    /* Remove old lock */
    sprintf(path, "%s/.gislock", mapset_old_path);
    remove(path);

    G_free(mapset_old_path);

    G_important_message(_("Your shell continues to use the history for the old mapset"));

    if ((shell = getenv("SHELL"))) {
	if (strstr(shell, "bash")) {
	    G_important_message(_("You can switch the history by commands:\n"
				  "history -w; history -r %s/.bash_history; HISTFILE=%s/.bash_history"),
				mapset_new_path, mapset_new_path);
	}
	else if (strstr(shell, "tcsh")) {
	    G_important_message(_("You can switch the history by commands:\n"
				  "history -S; history -L %s/.history; setenv histfile=%s/.history"),
				mapset_new_path, mapset_new_path);
	}
    }

    G_message(_("Your current mapset is <%s>"), mapset_new);
    
    G_free(mapset_new_path);

    return (EXIT_SUCCESS);
}