JNIEXPORT void JNICALL Java_net_hackcasual_freeciv_NativeHarness_init (JNIEnv *je, jobject o, jint w, jint h) { civ_lock(); width = w; height = h; android_width = w; android_height = h; lastTouchX = w / 2; lastTouchY = h / 2; draw_city_productions = TRUE; smooth_move_unit_msec = 200; smooth_center_slide_msec = 100; auto_center_on_combat = TRUE; if (!bufferData) bufferData = fc_malloc(BUFFER_SIZE); if (!displayCanvas || displayCanvas->width != w || displayCanvas->height != h) { displayCanvas = fc_malloc(sizeof(struct sprite)); displayCanvas->width = w; displayCanvas->height = h; displayCanvas->data = bufferData; } LOGI("Setting display size: %dx%d", android_width, android_height); map_canvas_resized(w, h); civ_unlock(); }
JNIEXPORT void JNICALL Java_net_hackcasual_freeciv_NativeHarness_startClient (JNIEnv *je, jobject o) { jmethodID register_buffer = 0; (*je)->GetJavaVM(je, &jvm); env_nh = je; jclass cls = (*env_nh)->GetObjectClass(env_nh, o); native_harness = (*env_nh)->NewGlobalRef(env_nh,o); draw_frame = (*env_nh)->GetMethodID(env_nh, cls, "updateDisplay", "()V"); client_connect = (*env_nh)->GetMethodID(env_nh, cls, "clientConnected", "()V"); measure_text = (*env_nh)->GetMethodID(env_nh, cls, "getTextSize", "(Ljava/lang/String;)I"); render_text = (*env_nh)->GetMethodID(env_nh, cls, "renderString", "(Ljava/lang/String;)I"); update_tileset_progress = (*env_nh)->GetMethodID(env_nh, cls, "updateTilesetProgress", "(Ljava/lang/String;)V"); if (draw_frame == 0) { LOGE("Failed to get updateDisplay"); return; } if (measure_text == 0) { LOGE("Failed to get measureText"); return; } if (render_text == 0) { LOGE("Failed to get renderText"); return; } register_buffer = (*env_nh)->GetMethodID(env_nh, cls, "registerNativeBuffers", "(Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;)V"); if (register_buffer == 0) { LOGE("Failed to get registerNativeBuffer"); return; } if (!bufferData) bufferData = fc_malloc(BUFFER_SIZE); // 30 pixels high by 300 width by 2 bytes per pixel incommingBuffer = fc_malloc(30 * 300 * 2); jobject direct_buffer = (*env_nh)->NewDirectByteBuffer(env_nh, bufferData, BUFFER_SIZE); jobject incomming_buffer = (*env_nh)->NewDirectByteBuffer(env_nh, incommingBuffer, BUFFER_SIZE); (*env_nh)->CallVoidMethod(env_nh, native_harness, register_buffer, direct_buffer, incomming_buffer); startClient(); }
/********************************************************************** Initialize the queue. initial_size is the numer of queue items for which memory should be preallocated, that is, the initial size of the item array the queue uses. If you insert more than n items to the queue, another n items will be allocated automatically. ***********************************************************************/ struct pqueue *pq_create(int initial_size) { struct pqueue *q = fc_malloc(sizeof(struct pqueue)); q->cells = fc_malloc(sizeof(pq_data_t) * initial_size); q->priorities = fc_malloc(sizeof(int) * initial_size); q->avail = initial_size; q->step = initial_size; q->size = 1; return q; }
/**************************************************************************** Create a canvas of the given size. ****************************************************************************/ struct canvas *canvas_create_alpha(int width, int height) { struct canvas* cp = fc_malloc(sizeof(struct canvas)); cp->width = width; cp->height = height; cp->data = fc_malloc(width * height * CANVAS_BYTE_WIDTH); cp->type = CANVAS_ARGB4444; cp->byte_width = CANVAS_BYTE_WIDTH; return cp; }
struct color *color_alloc(int r, int g, int b) { struct color *result = fc_malloc(sizeof(*result)); SDL_Color *pcolor = fc_malloc(sizeof(*pcolor)); pcolor->r = r; pcolor->g = g; pcolor->b = b; pcolor->unused = 255; result->color = pcolor; return result; }
/**************************************************************************** Duplicate a rgb color. ****************************************************************************/ static struct rgbcolor *rgbcolor_copy(const struct rgbcolor *prgb) { struct rgbcolor *pnew = fc_malloc(sizeof(*pnew)); *pnew = *prgb; return pnew; }
/********************************************************************** Returns the correct name of the gfx file (with path and extension) Must be free'd when no longer used ***********************************************************************/ char *themespec_gfx_filename(const char *gfx_filename) { const char *gfx_current_fileext; const char **gfx_fileexts = gfx_fileextensions(); while((gfx_current_fileext = *gfx_fileexts++)) { char *full_name = fc_malloc(strlen(gfx_filename) + strlen(gfx_current_fileext) + 2); const char *real_full_name; sprintf(full_name,"%s.%s",gfx_filename,gfx_current_fileext); real_full_name = fileinfoname(get_data_dirs(), full_name); FC_FREE(full_name); if (real_full_name) { return fc_strdup(real_full_name); } } log_fatal("Couldn't find a supported gfx file extension for \"%s\".", gfx_filename); exit(EXIT_FAILURE); return NULL; }
/**************************************************************************** Duplicate an attribute key. ****************************************************************************/ static struct attr_key *attr_key_dup(const struct attr_key *pkey) { struct attr_key *pnew_key = fc_malloc(sizeof(*pnew_key)); *pnew_key = *pkey; return pnew_key; }
/************************************************************************** ... **************************************************************************/ struct happiness_dlg *create_happiness_box(struct city *pcity, struct fcwin_box *hbox, HWND win) { int i; HDC hdc; struct happiness_dlg *dlg; struct fcwin_box *vbox; vbox=fcwin_vbox_new(win,FALSE); fcwin_box_add_groupbox(hbox,_("Happiness"),vbox,0, TRUE,TRUE,0); dlg=fc_malloc(sizeof(struct happiness_dlg)); dlg->win=win; dlg->pcity=pcity; for(i=0;i<NUM_HAPPINESS_MODIFIERS;i++) { fcwin_box_add_generic(vbox,bmp_row_minsize,bmp_row_setsize,NULL, &(dlg->mod_bmp_pos[i]),FALSE,FALSE,0); dlg->mod_label[i]=fcwin_box_add_static(vbox," ",0,SS_LEFT, FALSE,FALSE,5); } hdc=GetDC(win); for(i=0;i<NUM_HAPPINESS_MODIFIERS;i++) { dlg->mod_bmp[i]= CreateCompatibleBitmap(hdc, HAPPINESS_PIX_WIDTH*tileset_small_sprite_width(tileset), tileset_small_sprite_height(tileset)); } ReleaseDC(win,hdc); return dlg; }
/**************************************************************************** Enqueue a callback to be called during an idle moment. The 'callback' function should be called sometimes soon, and passed the 'data' pointer as its data. ****************************************************************************/ void add_idle_callback(void (callback)(void *), void *data) { struct callback *cb = fc_malloc(sizeof(*cb)); cb->callback = callback; cb->data = data; XtAppAddWorkProc(app_context, idle_callback_wrapper, cb); }
/************************************************************************** Create a canvas of the given size (with alpha channel). **************************************************************************/ struct canvas *canvas_create_with_alpha(int width, int height) { struct canvas *result = fc_malloc(sizeof(*result)); result->surf = create_surf_alpha(width, height, SDL_SWSURFACE); return result; }
/************************************************************************** ... **************************************************************************/ struct canvas *canvas_create(int width, int height) { struct canvas *result = fc_malloc(sizeof(*result)); result->pixmap = XCreatePixmap(display, root_window, width, height, display_depth); return result; }
/************************************************************************** Create a sprite struct and fill it with SDL_Surface pointer **************************************************************************/ static struct sprite * ctor_sprite(SDL_Surface *pSurface) { struct sprite *result = fc_malloc(sizeof(struct sprite)); result->psurface = pSurface; return result; }
/**************************************************************************** Enqueue a callback to be called during an idle moment. The 'callback' function should be called sometimes soon, and passed the 'data' pointer as its data. ****************************************************************************/ void add_idle_callback(void (callback)(void *), void *data) { struct callback *cb = fc_malloc(sizeof(*cb)); cb->callback = callback; cb->data = data; callback_list_prepend(callbacks, cb); }
/**************************************************************************** Create a canvas of the given size. ****************************************************************************/ struct canvas *canvas_create(int width, int height) { struct canvas *result = fc_malloc(sizeof(*result)); result->surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); result->drawable = NULL; return result; }
/*************************************************************************** Look up the service at hostname:port. ***************************************************************************/ struct fc_sockaddr_list *net_lookup_service(const char *name, int port, enum fc_addr_family family) { /* IPv6-enabled Freeciv always has HAVE_GETADDRINFO, IPv4-only Freeciv not * necessarily */ #ifdef HAVE_GETADDRINFO return net_lookup_getaddrinfo(name, port, family); #else /* HAVE_GETADDRINFO */ struct sockaddr_in *sock4; struct hostent *hp; struct fc_sockaddr_list *addrs = fc_sockaddr_list_new(); union fc_sockaddr *result = fc_malloc(sizeof(result)); sock4 = &result->saddr_in4; fc_assert(family != FC_ADDR_IPV6); result->saddr.sa_family = AF_INET; sock4->sin_port = htons(port); if (!name) { sock4->sin_addr.s_addr = htonl(INADDR_ANY); fc_sockaddr_list_append(addrs, result); return addrs; } #if defined(HAVE_INET_ATON) if (inet_aton(name, &sock4->sin_addr) != 0) { fc_sockaddr_list_append(addrs, result); return addrs; } #else /* HAVE_INET_ATON */ if ((sock4->sin_addr.s_addr = inet_addr(name)) != INADDR_NONE) { fc_sockaddr_list_append(addrs, result); return addrs; } #endif /* HAVE_INET_ATON */ hp = gethostbyname(name); if (!hp || hp->h_addrtype != AF_INET) { FC_FREE(result); return addrs; } memcpy(&sock4->sin_addr, hp->h_addr, hp->h_length); fc_sockaddr_list_append(addrs, result); return addrs; #endif /* !HAVE_GETADDRINFO */ }
/************************************************************************ Create a new empty genlist. ************************************************************************/ struct genlist *genlist_new(void) { struct genlist *pgenlist = fc_malloc(sizeof(*pgenlist)); pgenlist->nelements = 0; pgenlist->head_link = NULL; pgenlist->tail_link = NULL; return pgenlist; }
static void notify_goto_add_widget_tile(Widget w, struct tile *ptile) { struct widget_list *newwidget; newwidget = fc_malloc(sizeof(*newwidget)); newwidget->w = w; newwidget->tile = ptile; newwidget->next = notify_goto_widget_list; notify_goto_widget_list = newwidget; }
/********************************************************************** Allocate a new timer, or reuse t, with specified "type" and "use". The timer is created as cleared, and stopped. If t is NULL, allocate and return a new timer, else just re-initialise t and return t. This is intended to be useful to allocate a static t just once, eg: { static struct timer *t = NULL; t = timer_renew(t, TIMER_CPU, TIMER_USE); ... stuff ... log_verbose("That took %g seconds.", timer_read_seconds(t)); ... never free t ... } ***********************************************************************/ struct timer *timer_renew(struct timer *t, enum timer_timetype type, enum timer_use use) { if (!t) { t = (struct timer *)fc_malloc(sizeof(struct timer)); } t->type = type; t->use = use; timer_clear(t); return t; }
/************************************************************************** Construct and send request from player thread. **************************************************************************/ void tai_send_req(enum taireqtype type, struct player *pplayer, void *data) { struct tai_req *req = fc_malloc(sizeof(*req)); req->type = type; req->plr = pplayer; req->data = data; tai_req_from_thr(req); }
/*************************************************************************** ... ***************************************************************************/ static struct sprite *ctor_sprite(Pixmap mypixmap, int width, int height) { struct sprite *mysprite=fc_malloc(sizeof(struct sprite)); mysprite->pixmap=mypixmap; mysprite->width=width; mysprite->height=height; mysprite->ncols = 0; mysprite->pcolorarray = NULL; mysprite->has_mask=0; return mysprite; }
/**************************************************************************** Allocate a color (well, sort of) and return a pointer to it. ****************************************************************************/ struct color *color_alloc(int r, int g, int b) { struct color *color = fc_malloc(sizeof(*color)); color->color.red = (double)r/255; color->color.green = (double)g/255; color->color.blue = (double)b/255; color->color.alpha = 1.0; return color; }
/************************************************************************** ... **************************************************************************/ void log_output_window(void) { int len; char *theoutput; len=GetWindowTextLength(logoutput_win)+1; theoutput=fc_malloc(len); GetWindowText(logoutput_win,theoutput,len); write_chatline_content(theoutput); free(theoutput); }
/**************************************************************************** Create a new vision source. ****************************************************************************/ struct vision *vision_new(struct player *pplayer, struct tile *ptile) { struct vision *vision = fc_malloc(sizeof(*vision)); vision->player = pplayer; vision->tile = ptile; vision->can_reveal_tiles = TRUE; vision->radius_sq[V_MAIN] = -1; vision->radius_sq[V_INVIS] = -1; return vision; }
/*************************************************************************** Look up the service at hostname:port using getaddrinfo(). ***************************************************************************/ static struct fc_sockaddr_list *net_lookup_getaddrinfo(const char *name, int port, enum fc_addr_family family) { struct addrinfo hints; struct addrinfo *res; int err; char servname[8]; int gafam; struct fc_sockaddr_list *addrs = fc_sockaddr_list_new(); switch (family) { case FC_ADDR_IPV4: gafam = AF_INET; break; case FC_ADDR_IPV6: gafam = AF_INET6; break; case FC_ADDR_ANY: gafam = AF_UNSPEC; break; default: fc_assert(FALSE); return addrs; } /* Convert port to string for getaddrinfo() */ fc_snprintf(servname, sizeof(servname), "%d", port); /* Use getaddrinfo() to lookup IPv6 addresses */ memset(&hints, 0, sizeof(hints)); hints.ai_family = gafam; hints.ai_socktype = SOCK_DGRAM; /* any type that uses sin6_port */ hints.ai_flags = AI_PASSIVE | FC_AI_NUMERICSERV; err = getaddrinfo(name, servname, &hints, &res); if (err == 0) { struct addrinfo *current = res; while (current != NULL) { union fc_sockaddr *caddr = fc_malloc(sizeof(*caddr)); memcpy(caddr, current->ai_addr, MIN(sizeof(*caddr), current->ai_addrlen)); fc_sockaddr_list_append(addrs, caddr); current = current->ai_next; } } return addrs; }
/**************************************************************************** Allocate a color (adjusting it for our colormap if necessary on paletted systems) and return a pointer to it. ****************************************************************************/ struct color *color_alloc(int r, int g, int b) { struct color *color = fc_malloc(sizeof(*color)); GdkColormap *cmap = gtk_widget_get_default_colormap(); color->color.red = r << 8; color->color.green = g << 8; color->color.blue = b << 8; gdk_rgb_find_color(cmap, &color->color); return color; }
/************************************************************************ Insert a new element in the list, at position 'pos', with the specified user-data pointer 'data'. Existing elements at >= pos are moved one space to the "right". Recall 'pos' can be -1 meaning add at the end of the list. For an empty list pos has no effect. A bad 'pos' value for a non-empty list is treated as -1 (is this a good idea?) ************************************************************************/ static void genlist_insert(struct genlist *pgenlist, void *data, int pos) { if (pgenlist->nelements == 0) { /*list is empty, ignore pos */ struct genlist_link *plink = fc_malloc(sizeof(*plink)); plink->dataptr = data; plink->next = NULL; plink->prev = NULL; pgenlist->head_link = plink; pgenlist->tail_link = plink; } else { struct genlist_link *plink = fc_malloc(sizeof(*plink)); plink->dataptr = data; if (pos == 0) { plink->next = pgenlist->head_link; plink->prev = NULL; pgenlist->head_link->prev = plink; pgenlist->head_link = plink; } else if (pos <= -1 || pos >= pgenlist->nelements) { plink->next = NULL; plink->prev = pgenlist->tail_link; pgenlist->tail_link->next = plink; pgenlist->tail_link = plink; } else { struct genlist_link *left, *right; /* left and right of new element */ right = find_genlist_position(pgenlist, pos); left = right->prev; plink->next = right; plink->prev = left; right->prev = plink; left->next = plink; } } pgenlist->nelements++; }
/************************************************************************** Updates pplayer->attribute_block according to the given packet. **************************************************************************/ void generic_handle_player_attribute_chunk(struct player *pplayer, const struct packet_player_attribute_chunk *chunk) { log_packet("received attribute chunk %u/%u %u", (unsigned int) chunk->offset, (unsigned int) chunk->total_length, (unsigned int) chunk->chunk_length); if (chunk->total_length < 0 || chunk->chunk_length < 0 || chunk->total_length >= MAX_ATTRIBUTE_BLOCK || chunk->offset < 0 || chunk->offset > chunk->total_length /* necessary check on 32 bit systems */ || chunk->chunk_length > chunk->total_length || chunk->offset + chunk->chunk_length > chunk->total_length || (chunk->offset != 0 && chunk->total_length != pplayer->attribute_block_buffer.length)) { /* wrong attribute data */ if (pplayer->attribute_block_buffer.data) { free(pplayer->attribute_block_buffer.data); pplayer->attribute_block_buffer.data = NULL; } pplayer->attribute_block_buffer.length = 0; log_error("Received wrong attribute chunk"); return; } /* first one in a row */ if (chunk->offset == 0) { if (pplayer->attribute_block_buffer.data) { free(pplayer->attribute_block_buffer.data); pplayer->attribute_block_buffer.data = NULL; } pplayer->attribute_block_buffer.data = fc_malloc(chunk->total_length); pplayer->attribute_block_buffer.length = chunk->total_length; } memcpy((char *) (pplayer->attribute_block_buffer.data) + chunk->offset, chunk->data, chunk->chunk_length); if (chunk->offset + chunk->chunk_length == chunk->total_length) { /* Received full attribute block */ if (pplayer->attribute_block.data != NULL) { free(pplayer->attribute_block.data); } pplayer->attribute_block.data = pplayer->attribute_block_buffer.data; pplayer->attribute_block.length = pplayer->attribute_block_buffer.length; pplayer->attribute_block_buffer.data = NULL; pplayer->attribute_block_buffer.length = 0; } }
/************************************************************************** Creates escaped version of string. **************************************************************************/ static char *alloc_escaped_string(MYSQL *mysql, const char *orig) { int orig_len = strlen(orig); char *escaped = fc_malloc(orig_len*2+1); if (escaped == NULL) { freelog(LOG_ERROR, "Failed to allocate memory for escaped string %s", orig); } else { mysql_real_escape_string(mysql, escaped, orig, orig_len); } return escaped; }
struct theme_background_system *theme_background_system_read(struct section_file *file) { int i; struct theme_background_system *backgrounds = fc_malloc(sizeof(*backgrounds)); fc_assert_ret_val(ARRAY_SIZE(background_names) == BACKGROUND_LAST, NULL); for (i = 0; i < BACKGROUND_LAST; i++) { backgrounds->backgrounds[i] = themespec_gfx_filename(secfile_lookup_str(file, "backgrounds.%s", background_names[i])); } return backgrounds; }