コード例 #1
0
ファイル: util.c プロジェクト: Engil/wmfs
/** malloc with error support and size_t overflow protection
 * \param nmemb number of objects
 * \param size size of single object
 * \return non null void pointer
 */
void*
xmalloc(size_t nmemb, size_t size)
{
     void *ret;

     if(SIZE_MAX / nmemb < size)
          errl(EXIT_FAILURE, "xmalloc(%zu, %zu), "
                    "size_t overflow detected", nmemb, size);

     if((ret = malloc(nmemb * size)) == NULL)
          errl(EXIT_FAILURE, "malloc(%zu)", nmemb * size);

     return ret;
}
コード例 #2
0
ファイル: font.c プロジェクト: saniv/freecraft-ale-clone
/**
**	Font symbol to id.
**
**	@param type	Type of the font (game,small,...)
**
**	@return		Integer as font identifier.
*/
global int CclFontByIdentifier(SCM type)
{
    if (gh_eq_p(type, gh_symbol2scm("game"))) {
	return GameFont;
    } else if (gh_eq_p(type, gh_symbol2scm("small"))) {
	return SmallFont;
    } else if (gh_eq_p(type, gh_symbol2scm("large"))) {
	return LargeFont;
    } else if (gh_eq_p(type, gh_symbol2scm("small-title"))) {
	return SmallTitleFont;
    } else if (gh_eq_p(type, gh_symbol2scm("large-title"))) {
	return LargeTitleFont;
    } else if (gh_eq_p(type, gh_symbol2scm("user1"))) {
	return User1Font;
    } else if (gh_eq_p(type, gh_symbol2scm("user2"))) {
	return User2Font;
    } else if (gh_eq_p(type, gh_symbol2scm("user3"))) {
	return User3Font;
    } else if (gh_eq_p(type, gh_symbol2scm("user4"))) {
	return User4Font;
    } else if (gh_eq_p(type, gh_symbol2scm("user5"))) {
	return User5Font;
    } else {
	errl("Unsupported font tag", type);
    }
    return 0;
}
コード例 #3
0
ファイル: wmfs.c プロジェクト: Engil/wmfs
int
wmfs_error_handler(Display *d, XErrorEvent *event)
{
      char mess[256];

      /* Check if there is another WM running */
      if(event->error_code == BadAccess
                && W->root == event->resourceid)
           errl(EXIT_FAILURE, "Another Window Manager is already running.");

      /* Ignore focus change error for unmapped client
       * 42 = X_SetInputFocus
       * 28 = X_GrabButton
       */
     if(client_gb_win(event->resourceid))
          if(event->error_code == BadWindow
                    || event->request_code == 42
                    || event->request_code == 28)
               return 0;


     if(XGetErrorText(d, event->error_code, mess, 128))
          warnxl("%s(%d) opcodes %d/%d\n  resource #%lx\n",
                    mess,
                    event->error_code,
                    event->request_code,
                    event->minor_code,
                    event->resourceid);

     return 1;
}
コード例 #4
0
ファイル: util.c プロジェクト: Engil/wmfs
/** calloc with error support
 * \param nmemb Number of objects
 * \param size size of single object
 * \return non null void pointer
*/
void*
xcalloc(size_t nmemb, size_t size)
{
     void *ret;

     if((ret = calloc(nmemb, size)) == NULL)
          errl(EXIT_FAILURE, "calloc(%zu * %zu)", nmemb, size);

     return ret;
}
コード例 #5
0
ファイル: util.c プロジェクト: Engil/wmfs
/** asprintf wrapper
 * \param strp target string
 * \param fmt format
 * \return non zero integer
 */
int
xasprintf(char **strp, const char *fmt, ...)
{
     int ret;
     va_list args;

     va_start(args, fmt);
     ret = vasprintf(strp, fmt, args);
     va_end(args);

     if (ret == -1)
          errl(EXIT_FAILURE, "asprintf(%s)", fmt);

     return ret;
}
コード例 #6
0
ファイル: ccl_player.c プロジェクト: hhirsch/open_conflict
/**
**	Parse the player configuration.
**
**	@param list	Tagged list of all informations.
*/
local SCM CclPlayer(SCM list)
{
    SCM value;
    SCM sublist;
    Player* player;
    int i;
    char* str;

    i=gh_scm2int(gh_car(list));
    player=&Players[i];
    if( NumPlayers<=i ) {
	NumPlayers=i+1;
    }
    player->Player = i;
    player->Color=PlayerColors[i];
    if( !(player->Units=(Unit**)calloc(UnitMax,sizeof(Unit*))) ) {
	DebugLevel0("Not enough memory to create player %d.\n" _C_ i);

	return SCM_UNSPECIFIED;
    }
    list=gh_cdr(list);

    //
    //	Parse the list:	(still everything could be changed!)
    //
    while( !gh_null_p(list) ) {

	value=gh_car(list);
	list=gh_cdr(list);

	if( gh_eq_p(value,gh_symbol2scm("name")) ) {
	    player->Name=gh_scm2newstr(gh_car(list),NULL);
	    list=gh_cdr(list);
	} else if( gh_eq_p(value,gh_symbol2scm("type")) ) {
	    value=gh_car(list);
	    list=gh_cdr(list);
	    if( gh_eq_p(value,gh_symbol2scm("neutral")) ) {
		player->Type=PlayerNeutral;
	    } else if( gh_eq_p(value,gh_symbol2scm("nobody")) ) {
		player->Type=PlayerNobody;
	    } else if( gh_eq_p(value,gh_symbol2scm("computer")) ) {
		player->Type=PlayerComputer;
	    } else if( gh_eq_p(value,gh_symbol2scm("person")) ) {
		player->Type=PlayerPerson;
	    } else if( gh_eq_p(value,gh_symbol2scm("rescue-passive")) ) {
		player->Type=PlayerRescuePassive;
	    } else if( gh_eq_p(value,gh_symbol2scm("rescue-active")) ) {
		player->Type=PlayerRescueActive;
	    } else {
	       // FIXME: this leaves a half initialized player
	       errl("Unsupported tag",value);
	    }
	} else if( gh_eq_p(value,gh_symbol2scm("race")) ) {
	    str=gh_scm2newstr(gh_car(list),NULL);
	    if( RaceWcNames ) {
		for( i=0; RaceWcNames[i]; ++i ) {
		    if( !strcmp(str,RaceWcNames[i]) ) {
			player->RaceName=RaceWcNames[i];
			player->Race=i;
			break;
		    }
		}
	    }
	    free(str);
	    if( !RaceWcNames || !RaceWcNames[i] ) {
	       // FIXME: this leaves a half initialized player
	       errl("Unsupported tag",gh_car(list));
	    }
#if 0
	    player->RaceName=str=gh_scm2newstr(gh_car(list),NULL);
	    if( !strcmp(str,"human") ) {
		player->Race=PlayerRaceHuman;
	    } else if( !strcmp(str,"orc") ) {
		player->Race=PlayerRaceOrc;
	    } else if( !strcmp(str,"neutral") ) {
		player->Race=PlayerRaceNeutral;
	    } else {
	       // FIXME: this leaves a half initialized player
	       errl("Unsupported tag",gh_car(list));
	    }
#endif
	    list=gh_cdr(list);
	} else if( gh_eq_p(value,gh_symbol2scm("ai")) ) {
	    player->AiNum=gh_scm2int(gh_car(list));
	    list=gh_cdr(list);
	} else if( gh_eq_p(value,gh_symbol2scm("team")) ) {
	    player->Team=gh_scm2int(gh_car(list));
	    list=gh_cdr(list);
	} else if( gh_eq_p(value,gh_symbol2scm("enemy")) ) {
	    str=gh_scm2newstr(gh_car(list),NULL);
	    list=gh_cdr(list);
	    for( i=0; i<PlayerMax && *str; ++i,++str ) {
		if( *str=='-' || *str=='_' || *str==' ' ) {
		    player->Enemy&=~(1<<i);
		} else {
		    player->Enemy|=(1<<i);
		}
	    }
	} else if( gh_eq_p(value,gh_symbol2scm("allied")) ) {
	    str=gh_scm2newstr(gh_car(list),NULL);
	    list=gh_cdr(list);
	    for( i=0; i<PlayerMax && *str; ++i,++str ) {
		if( *str=='-' || *str=='_' || *str==' ' ) {
		    player->Allied&=~(1<<i);
		} else {
		    player->Allied|=(1<<i);
		}
	    }
	} else if( gh_eq_p(value,gh_symbol2scm("shared-vision")) ) {
	    str=gh_scm2newstr(gh_car(list),NULL);
	    list=gh_cdr(list);
	    for( i=0; i<PlayerMax && *str; ++i,++str ) {
		if( *str=='-' || *str=='_' || *str==' ' ) {
		    player->SharedVision&=~(1<<i);
		} else {
		    player->SharedVision|=(1<<i);
		}
	    }
	} else if( gh_eq_p(value,gh_symbol2scm("start")) ) {
	    value=gh_car(list);
	    list=gh_cdr(list);
	    player->StartX=gh_scm2int(gh_car(value));
	    player->StartY=gh_scm2int(gh_cadr(value));
	} else if( gh_eq_p(value,gh_symbol2scm("resources")) ) {
	    sublist=gh_car(list);
	    list=gh_cdr(list);
	    while( !gh_null_p(sublist) ) {

		value=gh_car(sublist);
		sublist=gh_cdr(sublist);

		for( i=0; i<MaxCosts; ++i ) {
		    if( gh_eq_p(value,gh_symbol2scm((char*)DefaultResourceNames[i])) ) {
			player->Resources[i]=gh_scm2int(gh_car(sublist));
			break;
		    }
		}
		if( i==MaxCosts ) {
		   // FIXME: this leaves a half initialized player
		   errl("Unsupported tag",value);
		}
		sublist=gh_cdr(sublist);
	    }
	} else if( gh_eq_p(value,gh_symbol2scm("incomes")) ) {
	    sublist=gh_car(list);
	    list=gh_cdr(list);
	    while( !gh_null_p(sublist) ) {

		value=gh_car(sublist);
		sublist=gh_cdr(sublist);

		for( i=0; i<MaxCosts; ++i ) {
		    if( gh_eq_p(value,gh_symbol2scm((char*)DefaultResourceNames[i])) ) {
			player->Incomes[i]=gh_scm2int(gh_car(sublist));
			break;
		    }
		}
		if( i==MaxCosts ) {
		   // FIXME: this leaves a half initialized player
		   errl("Unsupported tag",value);
		}
		sublist=gh_cdr(sublist);
	    }
	} else if( gh_eq_p(value,gh_symbol2scm("ai-enabled")) ) {
	    player->AiEnabled=1;
	} else if( gh_eq_p(value,gh_symbol2scm("ai-disabled")) ) {
	    player->AiEnabled=0;
	} else if( gh_eq_p(value,gh_symbol2scm("food")) ) {
	    player->Food=gh_scm2int(gh_car(list));
	    list=gh_cdr(list);
	} else if( gh_eq_p(value,gh_symbol2scm("food-unit-limit")) ) {
	    player->FoodUnitLimit=gh_scm2int(gh_car(list));
	    list=gh_cdr(list);
	} else if( gh_eq_p(value,gh_symbol2scm("building-limit")) ) {
	    player->BuildingLimit=gh_scm2int(gh_car(list));
	    list=gh_cdr(list);
	} else if( gh_eq_p(value,gh_symbol2scm("total-unit-limit")) ) {
	    player->TotalUnitLimit=gh_scm2int(gh_car(list));
	    list=gh_cdr(list);
	} else if( gh_eq_p(value,gh_symbol2scm("score")) ) {
	    player->Score=gh_scm2int(gh_car(list));
	    list=gh_cdr(list);
	} else if( gh_eq_p(value,gh_symbol2scm("total-units")) ) {
	    player->TotalUnits=gh_scm2int(gh_car(list));
	    list=gh_cdr(list);
	} else if( gh_eq_p(value,gh_symbol2scm("total-buildings")) ) {
	    player->TotalBuildings=gh_scm2int(gh_car(list));
	    list=gh_cdr(list);
	} else if( gh_eq_p(value,gh_symbol2scm("total-razings")) ) {
	    player->TotalRazings=gh_scm2int(gh_car(list));
	    list=gh_cdr(list);
	} else if( gh_eq_p(value,gh_symbol2scm("total-kills")) ) {
	    player->TotalKills=gh_scm2int(gh_car(list));
	    list=gh_cdr(list);
	} else if( gh_eq_p(value,gh_symbol2scm("total-resources")) ) {
	    sublist=gh_car(list);
	    list=gh_cdr(list);
	    i=gh_length(sublist);
	    if( i!=MaxCosts ) {
		fprintf(stderr,"Wrong number of total-resources %d\n",i);
	    }
	    i=0;
	    while( !gh_null_p(sublist) ) {
		if( i<MaxCosts ) {
		    player->TotalResources[i]=gh_scm2int(gh_car(sublist));
		}
		sublist=gh_cdr(sublist);
		++i;
	    }
	    player->TotalUnits=gh_scm2int(gh_car(list));
	} else if( gh_eq_p(value,gh_symbol2scm("timers")) ) {
	    sublist=gh_car(list);
	    list=gh_cdr(list);
	    i=gh_length(sublist);
	    if( i!=UpgradeMax ) {
		fprintf(stderr,"Wrong upgrade timer length %d\n",i);
	    }

	    i=0;
	    while( !gh_null_p(sublist) ) {
		if( i<UpgradeMax ) {
		    player->UpgradeTimers.Upgrades[i]=
			    gh_scm2int(gh_car(sublist));
		}
		sublist=gh_cdr(sublist);
		++i;
	    }
	} else {
	   // FIXME: this leaves a half initialized player
	   errl("Unsupported tag",value);
	}
    }

    return SCM_UNSPECIFIED;
}
コード例 #7
0
ファイル: gl_image_area.c プロジェクト: PennPanda/libbot2
int
bot_gtk_gl_image_area_set_image_format (BotGtkGlImageArea *self,
        int width, int height, GLenum format)
{
    if (format != GL_LUMINANCE && 
        format != GL_RGB && 
        format != GL_BGR && 
        format != GL_RGBA && 
        format != GL_BGRA)
    {
        errl ("Error: BotGtkGlImageArea does not support GL format %s\n",
                _gl_format_str (format));
        return -1;
    }

    int has_non_power_of_two = 0;
    int has_texture_rectangle = 0;
    int has_pbo = 0;

    const char * extstr = (const char *) glGetString (GL_EXTENSIONS);
    gchar ** exts = g_strsplit (extstr, " ", 0);
    int i;
    for (i = 0; exts[i]; i++) {
        gchar * ext = exts[i];
        if (!strcmp (ext, "GL_ARB_texture_non_power_of_two"))
            has_non_power_of_two = 1;
        if (!strcmp (ext, "GL_ARB_texture_rectangle"))
            has_texture_rectangle = 1;
        if (!strcmp (ext, "GL_ARB_pixel_buffer_object"))
            has_pbo = 1;
    }
    g_strfreev (exts);

    self->use_pbo = has_pbo;
    self->int_format = GL_RGBA8;
    self->width = width;
    self->height = height;

    if (has_non_power_of_two) {
        self->target = GL_TEXTURE_2D;
        self->texc_width = 1;
        self->texc_height = 1;
    }
    else if (has_texture_rectangle) {
        self->target = GL_TEXTURE_RECTANGLE_ARB;
        self->texc_width = width;
        self->texc_height = height;
    }
    else {
        fprintf (stderr, "Error: GL supports neither non-power-of-two nor "
                "texture-rectangle\n");
        return -1;
    }

    if (self->texname) {
        glDeleteTextures (1, &self->texname);
    }

    glGenTextures (1, &self->texname);

    self->max_data_size = width * height * 4;
    if (self->use_pbo) {
        if (self->pbo) {
            glDeleteBuffersARB (1, &self->pbo);
        }
        glGenBuffersARB (1, &self->pbo);
        glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, self->pbo);
        glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB, self->max_data_size, NULL, 
                GL_STREAM_DRAW);
        glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, 0);
    }

    self->format = format;

    return 0;
}
コード例 #8
0
ファイル: construct.c プロジェクト: saniv/freecraft-ale-clone
/**
**	Parse the construction.
**
**	@param list	List describing the construction.
**
**	@note make this more flexible
*/
local SCM CclDefineConstruction(SCM list)
{
    SCM value;
    SCM sublist;
    char* str;
    Construction* construction;
    Construction** cop;
    int i;

    //	Slot identifier

    str=gh_scm2newstr(gh_car(list),NULL);
    list=gh_cdr(list);

    for( i=0; ConstructionWcNames[i]; ++i ) {
        if( !strcmp(ConstructionWcNames[i],str) ) {
            break;
        }
    }
    if( !ConstructionWcNames[i] ) {
        DebugLevel0Fn("Construction not found.\n");
        free(str);
        return SCM_UNSPECIFIED;
    }

    if( (cop=Constructions)==NULL ) {
        Constructions=malloc(2*sizeof(Construction*));
        Constructions[0]=calloc(1,sizeof(Construction));
        Constructions[1]=NULL;
        construction=Constructions[0];
    } else {
        for( i=0; *cop; ++i,++cop ) {
        }
        Constructions=realloc(Constructions,(i+2)*sizeof(Construction*));
        Constructions[i]=calloc(1,sizeof(Construction));
        Constructions[i+1]=NULL;
        construction=Constructions[i];
    }
    construction->OType=ConstructionType;
    construction->Ident=str;

    //
    //	Parse the arguments, in tagged format.
    //
    while( !gh_null_p(list) ) {
        value=gh_car(list);
        list=gh_cdr(list);
        if( gh_eq_p(value,gh_symbol2scm("files")) ) {
            sublist=gh_car(list);

            //
            //	Parse the tilesets
            //
            while( !gh_null_p(sublist) ) {
                str=gh_scm2newstr(gh_car(sublist),NULL);

                // FIXME: use a general get tileset function here!
                i=0;
                if( strcmp(str,"default") ) {
                    for( ; i<NumTilesets; ++i ) {
                        if( !strcmp(str,Tilesets[i]->Ident) ) {
                            break;
                        }
                        if( !strcmp(str,Tilesets[i]->Class) ) {
                            break;
                        }
                    }
                    if( i==NumTilesets ) {
                        fprintf(stderr,"Tileset `%s' not available\n",str);
                        errl("tileset not available",gh_car(sublist));
                    }
                }
                sublist=gh_cdr(sublist);
                free(str);
                free(construction->File[i]);
                construction->File[i]=gh_scm2newstr(gh_car(sublist),NULL);
                sublist=gh_cdr(sublist);
            }

        } else if( gh_eq_p(value,gh_symbol2scm("size")) ) {
            value=gh_car(list);
            construction->Width=gh_scm2int(gh_car(value));
            value=gh_cdr(value);
            construction->Height=gh_scm2int(gh_car(value));

        } else if( gh_eq_p(value,gh_symbol2scm("shadow")) ) {
            sublist=gh_car(list);
            while( !gh_null_p(sublist) ) {
                value=gh_car(sublist);
                sublist=gh_cdr(sublist);

                if( gh_eq_p(value,gh_symbol2scm("file")) ) {
                    construction->ShadowFile=gh_scm2newstr(gh_car(sublist),NULL);
                } else if( gh_eq_p(value,gh_symbol2scm("width")) ) {
                    construction->ShadowWidth=gh_scm2int(gh_car(sublist));
                } else if( gh_eq_p(value,gh_symbol2scm("height")) ) {
                    construction->ShadowHeight=gh_scm2int(gh_car(sublist));
                } else {
                    errl("Unsupported shadow tag",value);
                }
                sublist=gh_cdr(sublist);
            }
        } else {
            // FIXME: this leaves a half initialized construction
            errl("Unsupported tag",value);
        }
        list=gh_cdr(list);
    }

    return SCM_UNSPECIFIED;
}