diff options
| author | Simon <simon@hardwired.org.uk> | 2010-08-29 12:10:58 (GMT) |
|---|---|---|
| committer | Simon <simon@hardwired.org.uk> | 2010-08-29 12:10:58 (GMT) |
| commit | 1af2d330de825a585c2c5ad8b6f840c639237034 (patch) | |
| tree | 17f95500b7ab8545949b2a3cb4d00c64c1949a0f /interface.c | |
| parent | c0563111f822f5253264c7805917a864e1ddda5d (diff) | |
| download | powder-1af2d330de825a585c2c5ad8b6f840c639237034.zip powder-1af2d330de825a585c2c5ad8b6f840c639237034.tar.gz | |
Moves and fixes
Diffstat (limited to 'interface.c')
| -rw-r--r-- | interface.c | 791 |
1 files changed, 791 insertions, 0 deletions
diff --git a/interface.c b/interface.c index d5d87e5..9a3fe4c 100644 --- a/interface.c +++ b/interface.c @@ -1,6 +1,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <bzlib.h> #include <math.h> #include "http.h" #include "md5.h" @@ -32,8 +33,30 @@ char svf_tags[256] = ""; void *svf_last = NULL; int svf_lsize; +char *search_ids[GRID_X*GRID_Y]; +int search_votes[GRID_X*GRID_Y]; +int search_publish[GRID_X*GRID_Y]; +int search_scoredown[GRID_X*GRID_Y]; +int search_scoreup[GRID_X*GRID_Y]; +char *search_names[GRID_X*GRID_Y]; +char *search_owners[GRID_X*GRID_Y]; +void *search_thumbs[GRID_X*GRID_Y]; +int search_thsizes[GRID_X*GRID_Y]; + +int search_own = 0; +int search_date = 0; +int search_page = 0; +char search_expr[256] = ""; + +char *tag_names[TAG_MAX]; +int tag_votes[TAG_MAX]; + int Z_keysym = 'z'; +int zoom_en = 0; +int zoom_x=(XRES-ZSIZE_D)/2, zoom_y=(YRES-ZSIZE_D)/2; +int zoom_wx=0, zoom_wy=0; + void menu_count(void) { int i=0; @@ -1636,4 +1659,772 @@ void set_cmode(int cm) strcpy(itc_msg, "Pressure Display"); else strcpy(itc_msg, "Velocity Display"); +} + +char *download_ui(pixel *vid_buf, char *uri, int *len) +{ + int dstate = 0; + void *http = http_async_req_start(NULL, uri, NULL, 0, 0); + int x0=(XRES-240)/2,y0=(YRES-MENUSIZE)/2; + int done, total, i, ret, zlen, ulen; + char str[16], *tmp, *res; + + while(!http_async_req_status(http)) + { + sdl_poll(); + + http_async_get_length(http, &total, &done); + + clearrect(vid_buf, x0-2, y0-2, 244, 64); + drawrect(vid_buf, x0, y0, 240, 60, 192, 192, 192, 255); + drawtext(vid_buf, x0+8, y0+8, "Please wait", 255, 216, 32, 255); + drawtext(vid_buf, x0+8, y0+26, "Downloading update...", 255, 255, 255, 255); + + if(total) + { + i = (236*done)/total; + fillrect(vid_buf, x0+1, y0+45, i+1, 14, 255, 216, 32, 255); + i = (100*done)/total; + sprintf(str, "%d%%", i); + if(i<50) + drawtext(vid_buf, x0+120-textwidth(str)/2, y0+48, str, 192, 192, 192, 255); + else + drawtext(vid_buf, x0+120-textwidth(str)/2, y0+48, str, 0, 0, 0, 255); + } + else + drawtext(vid_buf, x0+120-textwidth("Waiting...")/2, y0+48, "Waiting...", 255, 216, 32, 255); + + drawrect(vid_buf, x0, y0+44, 240, 16, 192, 192, 192, 255); + sdl_blit(0, 0, (XRES+BARSIZE), YRES+MENUSIZE, vid_buf, (XRES+BARSIZE)); + } + + tmp = http_async_req_stop(http, &ret, &zlen); + if(ret!=200) + { + error_ui(vid_buf, ret, http_ret_text(ret)); + if(tmp) + free(tmp); + return NULL; + } + if(!tmp) + { + error_ui(vid_buf, 0, "Server did not return data"); + return NULL; + } + + if(zlen<16) + { + printf("ZLen is not 16!\n"); + goto corrupt; + } + if(tmp[0]!=0x42 || tmp[1]!=0x75 || tmp[2]!=0x54 || tmp[3]!=0x54) + { + printf("Tmperr %d, %d, %d, %d\n", tmp[0], tmp[1], tmp[2], tmp[3]); + goto corrupt; + } + + ulen = (unsigned char)tmp[4]; + ulen |= ((unsigned char)tmp[5])<<8; + ulen |= ((unsigned char)tmp[6])<<16; + ulen |= ((unsigned char)tmp[7])<<24; + + res = (char *)malloc(ulen); + if(!res) + { + printf("No res!\n"); + goto corrupt; + } + dstate = BZ2_bzBuffToBuffDecompress((char *)res, (unsigned *)&ulen, (char *)(tmp+8), zlen-8, 0, 0); + if(dstate) + { + printf("Decompression failure: %d!\n", dstate); + free(res); + goto corrupt; + } + + free(tmp); + if(len) + *len = ulen; + return res; + +corrupt: + error_ui(vid_buf, 0, "Downloaded update is corrupted"); + free(tmp); + return NULL; +} + +int search_ui(pixel *vid_buf) +{ + int uih=0,nyu,nyd,b=1,bq,mx=0,my=0,mxq=0,myq=0,mmt=0,gi,gj,gx,gy,pos,i,mp,dp,own,last_own=search_own,page_count=0,last_page=0,last_date=0,j,w,h,st=0,lv; + int is_p1=0, exp_res=GRID_X*GRID_Y, tp, view_own=0; + int thumb_drawn[GRID_X*GRID_Y]; + pixel *v_buf = (pixel *)malloc(((YRES+MENUSIZE)*(XRES+BARSIZE))*PIXELSIZE); + float ry; + time_t http_last_use=HTTP_TIMEOUT; + ui_edit ed; + + + void *http = NULL; + int active = 0; + char *last = NULL; + int search = 0; + int lasttime = TIMEOUT; + char *uri; + int status; + char *results; + char *tmp, ts[64]; + + void *img_http[IMGCONNS]; + char *img_id[IMGCONNS]; + void *thumb, *data; + int thlen, dlen; + + memset(v_buf, 0, ((YRES+MENUSIZE)*(XRES+BARSIZE))*PIXELSIZE); + + memset(img_http, 0, sizeof(img_http)); + memset(img_id, 0, sizeof(img_id)); + + memset(search_ids, 0, sizeof(search_ids)); + memset(search_names, 0, sizeof(search_names)); + memset(search_scoreup, 0, sizeof(search_scoreup)); + memset(search_scoredown, 0, sizeof(search_scoredown)); + memset(search_publish, 0, sizeof(search_publish)); + memset(search_owners, 0, sizeof(search_owners)); + memset(search_thumbs, 0, sizeof(search_thumbs)); + memset(search_thsizes, 0, sizeof(search_thsizes)); + + memset(thumb_drawn, 0, sizeof(thumb_drawn)); + + do_open = 0; + + while(!sdl_poll()) + { + b = SDL_GetMouseState(&mx, &my); + if(!b) + break; + } + + ed.x = 65; + ed.y = 13; + ed.w = XRES-200; + ed.nx = 1; + ed.def = "[search terms]"; + ed.focus = 1; + ed.hide = 0; + ed.cursor = strlen(search_expr); + strcpy(ed.str, search_expr); + + sdl_wheel = 0; + + while(!sdl_poll()) + { + uih = 0; + bq = b; + mxq = mx; + myq = my; + b = SDL_GetMouseState(&mx, &my); + mx /= sdl_scale; + my /= sdl_scale; + + if(mx!=mxq || my!=myq || sdl_wheel || b) + mmt = 0; + else if(mmt<TIMEOUT) + mmt++; + + clearrect(vid_buf, -1, -1, (XRES+BARSIZE)+1, YRES+MENUSIZE+1); + + memcpy(vid_buf, v_buf, ((YRES+MENUSIZE)*(XRES+BARSIZE))*PIXELSIZE); + + drawtext(vid_buf, 11, 13, "Search:", 192, 192, 192, 255); + if(!last || (!active && strcmp(last, ed.str))) + drawtext(vid_buf, 51, 11, "\x8E", 192, 160, 32, 255); + else + drawtext(vid_buf, 51, 11, "\x8E", 32, 64, 160, 255); + drawtext(vid_buf, 51, 11, "\x8F", 255, 255, 255, 255); + drawrect(vid_buf, 48, 8, XRES-182, 16, 192, 192, 192, 255); + + if(!svf_login) + { + search_own = 0; + drawrect(vid_buf, XRES-64, 8, 56, 16, 96, 96, 96, 255); + drawtext(vid_buf, XRES-61, 11, "\x94", 96, 80, 16, 255); + drawtext(vid_buf, XRES-61, 11, "\x93", 128, 128, 128, 255); + drawtext(vid_buf, XRES-46, 13, "My Own", 128, 128, 128, 255); + } + else if(search_own) + { + fillrect(vid_buf, XRES-65, 7, 58, 18, 255, 255, 255, 255); + drawtext(vid_buf, XRES-61, 11, "\x94", 192, 160, 64, 255); + drawtext(vid_buf, XRES-61, 11, "\x93", 32, 32, 32, 255); + drawtext(vid_buf, XRES-46, 13, "My Own", 0, 0, 0, 255); + } + else + { + drawrect(vid_buf, XRES-64, 8, 56, 16, 192, 192, 192, 255); + drawtext(vid_buf, XRES-61, 11, "\x94", 192, 160, 32, 255); + drawtext(vid_buf, XRES-61, 11, "\x93", 255, 255, 255, 255); + drawtext(vid_buf, XRES-46, 13, "My Own", 255, 255, 255, 255); + } + + if(search_date) + { + fillrect(vid_buf, XRES-130, 7, 62, 18, 255, 255, 255, 255); + drawtext(vid_buf, XRES-126, 11, "\xA6", 32, 32, 32, 255); + drawtext(vid_buf, XRES-111, 13, "By date", 0, 0, 0, 255); + } + else + { + drawrect(vid_buf, XRES-129, 8, 60, 16, 192, 192, 192, 255); + drawtext(vid_buf, XRES-126, 11, "\xA9", 144, 48, 32, 255); + drawtext(vid_buf, XRES-126, 11, "\xA8", 32, 144, 32, 255); + drawtext(vid_buf, XRES-126, 11, "\xA7", 255, 255, 255, 255); + drawtext(vid_buf, XRES-111, 13, "By votes", 255, 255, 255, 255); + } + + if(search_page) + { + drawtext(vid_buf, 4, YRES+MENUSIZE-16, "\x96", 255, 255, 255, 255); + drawrect(vid_buf, 1, YRES+MENUSIZE-20, 16, 16, 255, 255, 255, 255); + } + if(page_count > 9) + { + drawtext(vid_buf, XRES-15, YRES+MENUSIZE-16, "\x95", 255, 255, 255, 255); + drawrect(vid_buf, XRES-18, YRES+MENUSIZE-20, 16, 16, 255, 255, 255, 255); + } + + ui_edit_draw(vid_buf, &ed); + + if((b && !bq && mx>=1 && mx<=17 && my>=YRES+MENUSIZE-20 && my<YRES+MENUSIZE-4) || sdl_wheel>0) + { + if(search_page) + { + search_page --; + lasttime = TIMEOUT; + } + sdl_wheel = 0; + uih = 1; + } + if((b && !bq && mx>=XRES-18 && mx<=XRES-1 && my>=YRES+MENUSIZE-20 && my<YRES+MENUSIZE-4) || sdl_wheel<0) + { + if(page_count>exp_res) + { + lasttime = TIMEOUT; + search_page ++; + page_count = exp_res; + } + sdl_wheel = 0; + uih = 1; + } + + tp = -1; + if(is_p1) + { + drawtext(vid_buf, (XRES-textwidth("Popular tags:"))/2, 31, "Popular tags:", 255, 192, 64, 255); + for(gj=0; gj<((GRID_Y-GRID_P)*YRES)/(GRID_Y*14); gj++) + for(gi=0; gi<GRID_X; gi++) + { + pos = gi+GRID_X*gj; + if(pos>TAG_MAX || !tag_names[pos]) + break; + if(tag_votes[0]) + i = 127+(128*tag_votes[pos])/tag_votes[0]; + else + i = 192; + w = textwidth(tag_names[pos]); + if(w>XRES/GRID_X-5) + w = XRES/GRID_X-5; + gx = (XRES/GRID_X)*gi; + gy = gj*14 + 46; + if(mx>=gx && mx<gx+(XRES/GRID_X) && my>=gy && my<gy+14) + { + j = (i*5)/6; + tp = pos; + } + else + j = i; + drawtextmax(vid_buf, gx+(XRES/GRID_X-w)/2, gy, XRES/GRID_X-5, tag_names[pos], j, j, i, 255); + } + } + + mp = dp = -1; + st = 0; + for(gj=0; gj<GRID_Y; gj++) + for(gi=0; gi<GRID_X; gi++) + { + if(is_p1) + { + pos = gi+GRID_X*(gj-GRID_Y+GRID_P); + if(pos<0) + break; + } + else + pos = gi+GRID_X*gj; + if(!search_ids[pos]) + break; + gx = ((XRES/GRID_X)*gi) + (XRES/GRID_X-XRES/GRID_S)/2; + gy = ((((YRES-(MENUSIZE-20))+15)/GRID_Y)*gj) + ((YRES-(MENUSIZE-20))/GRID_Y-(YRES-(MENUSIZE-20))/GRID_S+10)/2 + 18; + if(textwidth(search_names[pos]) > XRES/GRID_X-10) + { + tmp = malloc(strlen(search_names[pos])+4); + strcpy(tmp, search_names[pos]); + j = textwidthx(tmp, XRES/GRID_X-15); + strcpy(tmp+j, "..."); + drawtext(vid_buf, gx+XRES/(GRID_S*2)-textwidth(tmp)/2, gy+YRES/GRID_S+7, tmp, 192, 192, 192, 255); + free(tmp); + } + else + drawtext(vid_buf, gx+XRES/(GRID_S*2)-textwidth(search_names[pos])/2, gy+YRES/GRID_S+7, search_names[pos], 192, 192, 192, 255); + j = textwidth(search_owners[pos]); + if(mx>=gx+XRES/(GRID_S*2)-j/2 && mx<=gx+XRES/(GRID_S*2)+j/2 && + my>=gy+YRES/GRID_S+18 && my<=gy+YRES/GRID_S+31) + { + st = 1; + drawtext(vid_buf, gx+XRES/(GRID_S*2)-j/2, gy+YRES/GRID_S+20, search_owners[pos], 128, 128, 160, 255); + } + else + drawtext(vid_buf, gx+XRES/(GRID_S*2)-j/2, gy+YRES/GRID_S+20, search_owners[pos], 128, 128, 128, 255); + if(search_thumbs[pos]&&thumb_drawn[pos]==0) + { + render_thumb(search_thumbs[pos], search_thsizes[pos], 1, v_buf, gx, gy, GRID_S); + thumb_drawn[pos] = 1; + } + own = svf_login && (!strcmp(svf_user, search_owners[pos]) || svf_admin || svf_mod); + if(mx>=gx-2 && mx<=gx+XRES/GRID_S+3 && my>=gy-2 && my<=gy+YRES/GRID_S+30) + mp = pos; + if(own) + { + if(mx>=gx+XRES/GRID_S-4 && mx<=gx+XRES/GRID_S+6 && my>=gy-6 && my<=gy+4) + { + mp = -1; + dp = pos; + } + } + if(mp==pos && !st) + drawrect(vid_buf, gx-2, gy-2, XRES/GRID_S+3, YRES/GRID_S+3, 160, 160, 192, 255); + else + drawrect(vid_buf, gx-2, gy-2, XRES/GRID_S+3, YRES/GRID_S+3, 128, 128, 128, 255); + if(own) + { + if(dp == pos) + drawtext(vid_buf, gx+XRES/GRID_S-4, gy-6, "\x86", 255, 48, 32, 255); + else + drawtext(vid_buf, gx+XRES/GRID_S-4, gy-6, "\x86", 160, 48, 32, 255); + drawtext(vid_buf, gx+XRES/GRID_S-4, gy-6, "\x85", 255, 255, 255, 255); + } + if(!search_publish[pos]) + { + drawtext(vid_buf, gx-6, gy-6, "\xCD", 255, 255, 255, 255); + drawtext(vid_buf, gx-6, gy-6, "\xCE", 212, 151, 81, 255); + } + if(view_own || svf_admin || svf_mod) + { + sprintf(ts+1, "%d", search_votes[pos]); + ts[0] = 0xBB; + for(j=1; ts[j]; j++) + ts[j] = 0xBC; + ts[j-1] = 0xB9; + ts[j] = 0xBA; + ts[j+1] = 0; + w = gx+XRES/GRID_S-2-textwidth(ts); + h = gy+YRES/GRID_S-11; + drawtext(vid_buf, w, h, ts, 16, 72, 16, 255); + for(j=0; ts[j]; j++) + ts[j] -= 14; + drawtext(vid_buf, w, h, ts, 192, 192, 192, 255); + sprintf(ts, "%d", search_votes[pos]); + for(j=0; ts[j]; j++) + ts[j] += 127; + drawtext(vid_buf, w+3, h, ts, 255, 255, 255, 255); + } + if(search_scoreup[pos]>0||search_scoredown[pos]>0) + { + lv = (search_scoreup[pos]>search_scoredown[pos]?search_scoreup[pos]:search_scoredown[pos]); + + if(((YRES/GRID_S+3)/2)>lv) + { + ry = ((float)((YRES/GRID_S+3)/2)/(float)lv); + if(lv<8) + { + ry = ry/(8-lv); + } + nyu = search_scoreup[pos]*ry; + nyd = search_scoredown[pos]*ry; + } + else + { + ry = ((float)lv/(float)((YRES/GRID_S+3)/2)); + nyu = search_scoreup[pos]/ry; + nyd = search_scoredown[pos]/ry; + } + + fillrect(vid_buf, gx-2+(XRES/GRID_S)+5, gy-2+((YRES/GRID_S+3)/2)-nyu, 4, nyu, 0, 187, 40, 255); + fillrect(vid_buf, gx-2+(XRES/GRID_S)+5, gy-2+((YRES/GRID_S+3)/2)+1, 4, nyd, 187, 40, 0, 255); + + drawrect(vid_buf, gx-2+(XRES/GRID_S)+5, gy-2+((YRES/GRID_S+3)/2)-nyu, 4, nyu, 0, 107, 10, 255); + drawrect(vid_buf, gx-2+(XRES/GRID_S)+5, gy-2+((YRES/GRID_S+3)/2)+1, 4, nyd, 107, 10, 0, 255); + } + } + + if(mp!=-1 && mmt>=TIMEOUT/5 && !st) + { + gi = mp % GRID_X; + gj = mp / GRID_X; + if(is_p1) + gj += GRID_Y-GRID_P; + gx = ((XRES/GRID_X)*gi) + (XRES/GRID_X-XRES/GRID_S)/2; + gy = (((YRES+15)/GRID_Y)*gj) + (YRES/GRID_Y-YRES/GRID_S+10)/2 + 18; + i = w = textwidth(search_names[mp]); + h = YRES/GRID_Z+30; + if(w<XRES/GRID_Z) w=XRES/GRID_Z; + gx += XRES/(GRID_S*2)-w/2; + gy += YRES/(GRID_S*2)-h/2; + if(gx<2) gx=2; + if(gx+w>=XRES-2) gx=XRES-3-w; + if(gy<32) gy=32; + if(gy+h>=YRES+(MENUSIZE-2)) gy=YRES+(MENUSIZE-3)-h; + clearrect(vid_buf, gx-2, gy-3, w+4, h); + drawrect(vid_buf, gx-2, gy-3, w+4, h, 160, 160, 192, 255); + if(search_thumbs[mp]) + render_thumb(search_thumbs[mp], search_thsizes[mp], 1, vid_buf, gx+(w-(XRES/GRID_Z))/2, gy, GRID_Z); + drawtext(vid_buf, gx+(w-i)/2, gy+YRES/GRID_Z+4, search_names[mp], 192, 192, 192, 255); + drawtext(vid_buf, gx+(w-textwidth(search_owners[mp]))/2, gy+YRES/GRID_Z+16, search_owners[mp], 128, 128, 128, 255); + } + + sdl_blit(0, 0, (XRES+BARSIZE), YRES+MENUSIZE, vid_buf, (XRES+BARSIZE)); + + ui_edit_process(mx, my, b, &ed); + + if(sdl_key==SDLK_RETURN) + { + if(!last || (!active && (strcmp(last, ed.str) || last_own!=search_own || last_date!=search_date || last_page!=search_page))) + lasttime = TIMEOUT; + else if(search_ids[0] && !search_ids[1]) + { + bq = 0; + b = 1; + mp = 0; + } + } + if(sdl_key==SDLK_ESCAPE) + goto finish; + + if(b && !bq && mx>=XRES-64 && mx<=XRES-8 && my>=8 && my<=24 && svf_login) + { + search_own = !search_own; + lasttime = TIMEOUT; + } + if(b && !bq && mx>=XRES-129 && mx<=XRES-65 && my>=8 && my<=24) + { + search_date = !search_date; + lasttime = TIMEOUT; + } + + if(b && !bq && dp!=-1) + if(confirm_ui(vid_buf, "Do you want to delete?", search_names[dp], "Delete")) + { + execute_delete(vid_buf, search_ids[dp]); + lasttime = TIMEOUT; + if(last) + { + free(last); + last = NULL; + } + } + + if(b && !bq && tp!=-1) + { + strncpy(ed.str, tag_names[tp], 255); + lasttime = TIMEOUT; + } + + if(b && !bq && mp!=-1 && st) + { + sprintf(ed.str, "user:%s", search_owners[mp]); + lasttime = TIMEOUT; + } + + if(do_open==1) + { + mp = 0; + } + + if((b && !bq && mp!=-1 && !st && !uih) || do_open==1) + { + fillrect(vid_buf, 0, 0, XRES+BARSIZE, YRES+MENUSIZE, 0, 0, 0, 255); + info_box(vid_buf, "Loading..."); + + uri = malloc(strlen(search_ids[mp])*3+strlen(SERVER)+64); + strcpy(uri, "http://" SERVER "/Get.api?Op=save&ID="); + strcaturl(uri, search_ids[mp]); + data = http_simple_get(uri, &status, &dlen); + free(uri); + + if(status == 200) + { + status = parse_save(data, dlen, 1, 0, 0); + switch(status) + { + case 1: + error_ui(vid_buf, 0, "Simulation corrupted"); + break; + case 2: + error_ui(vid_buf, 0, "Simulation from a newer version"); + break; + case 3: + error_ui(vid_buf, 0, "Simulation on a too large grid"); + break; + } + if(!status) + { + char *tnames[] = {"ID", NULL}; + char *tparts[1]; + int tplens[1]; + if(svf_last) + free(svf_last); + svf_last = data; + svf_lsize = dlen; + + tparts[0] = search_ids[mp]; + tplens[0] = strlen(search_ids[mp]); + data = http_multipart_post("http://" SERVER "/Tags.api", tnames, tparts, tplens, svf_user, svf_pass, &status, NULL); + + svf_open = 1; + svf_own = svf_login && !strcmp(search_owners[mp], svf_user); + svf_publish = search_publish[mp] && svf_login && !strcmp(search_owners[mp], svf_user); + + strcpy(svf_id, search_ids[mp]); + strcpy(svf_name, search_names[mp]); + if(status == 200) + { + if(data) + { + strncpy(svf_tags, data, 255); + svf_tags[255] = 0; + } + else + svf_tags[0] = 0; + } + else + { + svf_tags[0] = 0; + } + + if(svf_login) + { + char *names[] = {"ID", NULL}; + char *parts[1]; + parts[0] = search_ids[mp]; + data = http_multipart_post("http://" SERVER "/Vote.api", names, parts, NULL, svf_user, svf_pass, &status, NULL); + if(status == 200) + { + if(data) + { + if(!strcmp(data, "Up")) + { + svf_myvote = 1; + } + else if(!strcmp(data, "Down")) + { + svf_myvote = -1; + } + else + { + svf_myvote = 0; + } + } + else + { + svf_myvote = 0; + } + } + else + { + svf_myvote = 0; + } + } + } + else + { + svf_open = 0; + svf_publish = 0; + svf_own = 0; + svf_myvote = 0; + svf_id[0] = 0; + svf_name[0] = 0; + svf_tags[0] = 0; + if(svf_last) + free(svf_last); + svf_last = NULL; + } + } + else + error_ui(vid_buf, status, http_ret_text(status)); + + if(data) + free(data); + goto finish; + } + + if(!last) + { + search = 1; + } + else if(!active && (strcmp(last, ed.str) || last_own!=search_own || last_date!=search_date || last_page!=search_page)) + { + search = 1; + if(strcmp(last, ed.str) || last_own!=search_own || last_date!=search_date) + { + search_page = 0; + page_count = 0; + } + free(last); + last = NULL; + } + else + search = 0; + + if(search && lasttime>=TIMEOUT) + { + lasttime = 0; + last = mystrdup(ed.str); + last_own = search_own; + last_date = search_date; + last_page = search_page; + active = 1; + uri = malloc(strlen(last)*3+80+strlen(SERVER)+strlen(svf_user)); + if(search_own || svf_admin || svf_mod) + tmp = "&ShowVotes=true"; + else + tmp = ""; + if(!search_own && !search_date && !*last) + { + if(search_page) + { + exp_res = GRID_X*GRID_Y; + sprintf(uri, "http://" SERVER "/Search.api?Start=%d&Count=%d%s&Query=", (search_page-1)*GRID_X*GRID_Y+GRID_X*GRID_P, exp_res+1, tmp); + } + else + { + exp_res = GRID_X*GRID_P; + sprintf(uri, "http://" SERVER "/Search.api?Start=%d&Count=%d&t=%d%s&Query=", 0, exp_res+1, ((GRID_Y-GRID_P)*YRES)/(GRID_Y*14)*GRID_X, tmp); + } + } + else + { + exp_res = GRID_X*GRID_Y; + sprintf(uri, "http://" SERVER "/Search.api?Start=%d&Count=%d%s&Query=", search_page*GRID_X*GRID_Y, exp_res+1, tmp); + } + strcaturl(uri, last); + if(search_own) + { + strcaturl(uri, " user:"); + strcaturl(uri, svf_user); + } + if(search_date) + strcaturl(uri, " sort:date"); + + http = http_async_req_start(http, uri, NULL, 0, 1); + if(svf_login) + { + http_auth_headers(http, svf_user, svf_pass); + } + http_last_use = time(NULL); + free(uri); + } + + if(active && http_async_req_status(http)) + { + http_last_use = time(NULL); + results = http_async_req_stop(http, &status, NULL); + view_own = last_own; + if(status == 200) + { + page_count = search_results(results, last_own||svf_admin||svf_mod); + memset(thumb_drawn, 0, sizeof(thumb_drawn)); + memset(v_buf, 0, ((YRES+MENUSIZE)*(XRES+BARSIZE))*PIXELSIZE); + } + is_p1 = (exp_res < GRID_X*GRID_Y); + free(results); + active = 0; + } + + if(http && !active && (time(NULL)>http_last_use+HTTP_TIMEOUT)) + { + http_async_req_close(http); + http = NULL; + } + + for(i=0; i<IMGCONNS; i++) + { + if(img_http[i] && http_async_req_status(img_http[i])) + { + thumb = http_async_req_stop(img_http[i], &status, &thlen); + if(status != 200) + { + if(thumb) + free(thumb); + thumb = calloc(1,4); + thlen = 4; + } + thumb_cache_add(img_id[i], thumb, thlen); + for(pos=0; pos<GRID_X*GRID_Y; pos++) + if(search_ids[pos] && !strcmp(search_ids[pos], img_id[i])) + break; + if(pos<GRID_X*GRID_Y) + { + search_thumbs[pos] = thumb; + search_thsizes[pos] = thlen; + } + else + free(thumb); + free(img_id[i]); + img_id[i] = NULL; + } + if(!img_id[i]) + { + for(pos=0; pos<GRID_X*GRID_Y; pos++) + if(search_ids[pos] && !search_thumbs[pos]) + { + for(gi=0; gi<IMGCONNS; gi++) + if(img_id[gi] && !strcmp(search_ids[pos], img_id[gi])) + break; + if(gi<IMGCONNS) + continue; + break; + } + if(pos<GRID_X*GRID_Y) + { + uri = malloc(strlen(search_ids[pos])*3+strlen(SERVER)+64); + strcpy(uri, "http://" SERVER "/Get.api?Op=thumb&ID="); + strcaturl(uri, search_ids[pos]); + img_id[i] = mystrdup(search_ids[pos]); + img_http[i] = http_async_req_start(img_http[i], uri, NULL, 0, 1); + free(uri); + } + } + if(!img_id[i] && img_http[i]) + { + http_async_req_close(img_http[i]); + img_http[i] = NULL; + } + } + + if(lasttime<TIMEOUT) + lasttime++; + } + +finish: + if(last) + free(last); + if(http) + http_async_req_close(http); + for(i=0; i<IMGCONNS; i++) + if(img_http[i]) + http_async_req_close(img_http[i]); + + search_results("", 0); + + strcpy(search_expr, ed.str); + + return 0; }
\ No newline at end of file |
