void decompress(int in, int out) { match_t m; window_t w; window_init(&w, BUFSIZE); while (read(in, &m, sizeof m) == sizeof m) { if (match_distance(m) == 0) { int n = match_length(m); char tab[64]; if (!read_tab(in, tab, n)) { printf("illegal state\n"); } window_append_tab(&w, tab, n); } else { window_append_match(&w, m); } if (window_should_flush(&w)) { write(out, w.start, w.block_size); window_flush(&w); } } if (0 < w.cursor-w.start) write(out, w.start, w.cursor-w.start); window_free(&w); }
int main_loop_iteration(void) { HANDLE hlist[NSOCK]; int i,j; DWORD r,delay; ltime_t tm; flush_socks(); window_flush(); for (i=0,j=0;j<NSOCK;j++) if (sockets[j].inuse && sockets[j].event) hlist[i++]=sockets[j].event; tm=nearest_timeout(); if (tm<0) delay=INFINITE; else if (tm<0x7fffffff) delay=(DWORD)tm; else delay=0x7fffffff; r=MsgWaitForMultipleObjects(i,hlist,FALSE,delay,QS_ALLINPUT); if (r<0) { clerr("MsgWaitForMultipleObjectsEx() failed"); return -1; } if (r==WAIT_OBJECT_0+i) out_process_input(); else if (r>=WAIT_OBJECT_0 && r<WAIT_OBJECT_0+i) { r-=WAIT_OBJECT_0; for (j=0;j<NSOCK;j++) if (sockets[j].event==hlist[r]) soproc(j,NULL,0); } process_timeouts(); return 0; }
static void resizor_draw(struct resizor *resizor) { cairo_surface_t *surface; cairo_t *cr; struct rectangle allocation; window_draw(resizor->window); window_get_child_allocation(resizor->window, &allocation); surface = window_get_surface(resizor->window); cr = cairo_create(surface); cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height); cairo_set_source_rgba(cr, 0, 0, 0, 0.8); cairo_fill(cr); cairo_destroy(cr); cairo_surface_destroy(surface); window_flush(resizor->window); if (fabs(resizor->height.previous - resizor->height.target) > 0.1) { wl_display_frame_callback(display_get_display(resizor->display), frame_callback, resizor); } }
static void draw_gears(struct gears *gears) { GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0; struct rectangle window_allocation; struct rectangle allocation; window_draw(gears->window); window_get_child_allocation(gears->window, &allocation); window_get_allocation(gears->window, &window_allocation); display_acquire_window_surface(gears->d, gears->window, gears->context); glViewport(allocation.x, window_allocation.height - allocation.height - allocation.x, allocation.width, allocation.height); glScissor(allocation.x, window_allocation.height - allocation.height - allocation.y, allocation.width, allocation.height); glEnable(GL_SCISSOR_TEST); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glTranslatef(0.0, 0.0, -50); glRotatef(view_rotx, 1.0, 0.0, 0.0); glRotatef(view_roty, 0.0, 1.0, 0.0); glRotatef(view_rotz, 0.0, 0.0, 1.0); glPushMatrix(); glTranslatef(-3.0, -2.0, 0.0); glRotatef(gears->angle, 0.0, 0.0, 1.0); glCallList(gears->gear_list[0]); glPopMatrix(); glPushMatrix(); glTranslatef(3.1, -2.0, 0.0); glRotatef(-2.0 * gears->angle - 9.0, 0.0, 0.0, 1.0); glCallList(gears->gear_list[1]); glPopMatrix(); glPushMatrix(); glTranslatef(-3.1, 4.2, 0.0); glRotatef(-2.0 * gears->angle - 25.0, 0.0, 0.0, 1.0); glCallList(gears->gear_list[2]); glPopMatrix(); glPopMatrix(); glFlush(); display_release(gears->d); window_flush(gears->window); }
void iconview_redraw(iconview_t *iv) { int sb_x, sb_y, sb_width, sb_height; int sr_x, sr_y, sr_width, sr_height; int rows, iheight, i; if (!iv->parent) return; /* clear the tmp buf */ for (i = 0; i < iv->active->width * iv->active->height; i++) iv->active->buf[i] = 0xFFFFFFFF; /* clear the parent window (we do this because the iv contains some * transparent pixels that needs white background. */ pixbuf_paint(iv->active, iv->parent->pixbuf, iv->x, iv->y); /* draw the active part of the iconview to the tmp buf */ pixbuf_crop(iv->pixbuf, iv->active, 0, iv->scroll_y); /* draw the scroll bar to the tmp buf */ if (!up_png) { up_png = parse_png("/usr/share/icons/up.png"); down_png = parse_png("/usr/share/icons/down.png"); } if (iv->show_scroll) { pixbuf_paint(up_png, iv->active, iv->width-up_png->width, 0); pixbuf_paint(down_png, iv->active, iv->width-down_png->width, iv->height-down_png->height); sb_x = iv->width-up_png->width; sb_y = up_png->height; sb_width = up_png->width; sb_height = iv->height-up_png->height-down_png->height; draw_solid(iv->active, sb_x, sb_y, sb_width, sb_height, 0xFFFF9494); rows = iv->cur/iv->icons_per_row + (iv->cur%iv->icons_per_row?1:0); iheight = iv->height > rows*iv->each_height? iv->height : rows*iv->each_height; sr_x = sb_x + 2; sr_y = sb_y + (iv->scroll_y*sb_height)/iheight; sr_width = sb_width-4; sr_height = (iv->height*sb_height)/iheight; draw_solid(iv->active, sr_x, sr_y, sr_width, sr_height, 0xFFA93131); } /* draw the tmp buf on the parent window */ pixbuf_paint(iv->active, iv->parent->pixbuf, iv->x, iv->y); /* flush the window buffer to VGA */ window_flush(iv->parent, iv->x, iv->y, iv->width, iv->height); }
static void draw_gears(struct gears *gears) { GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0; struct rectangle allocation; if (gears->surface[gears->current] == NULL) allocate_buffer(gears); glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, gears->color_rbo[gears->current]); window_get_child_allocation(gears->window, &allocation); glViewport(allocation.x, allocation.y, allocation.width, allocation.height); glScissor(allocation.x, allocation.y, allocation.width, allocation.height); glEnable(GL_SCISSOR_TEST); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glTranslatef(0.0, 0.0, -50); glRotatef(view_rotx, 1.0, 0.0, 0.0); glRotatef(view_roty, 0.0, 1.0, 0.0); glRotatef(view_rotz, 0.0, 0.0, 1.0); glPushMatrix(); glTranslatef(-3.0, -2.0, 0.0); glRotatef(gears->angle, 0.0, 0.0, 1.0); glCallList(gears->gear_list[0]); glPopMatrix(); glPushMatrix(); glTranslatef(3.1, -2.0, 0.0); glRotatef(-2.0 * gears->angle - 9.0, 0.0, 0.0, 1.0); glCallList(gears->gear_list[1]); glPopMatrix(); glPushMatrix(); glTranslatef(-3.1, 4.2, 0.0); glRotatef(-2.0 * gears->angle - 25.0, 0.0, 0.0, 1.0); glCallList(gears->gear_list[2]); glPopMatrix(); glPopMatrix(); glFlush(); window_set_surface(gears->window, gears->surface[gears->current]); window_flush(gears->window); }
int main_loop_iteration(void) { fd_set rfd,wfd; struct timeval delay; int i,j,r; ltime_t tm; static int siginit=0; if (!siginit) { signal(SIGCHLD,sigchld); signal(SIGINT,fatalsig); signal(SIGTERM,fatalsig); signal(SIGHUP,fatalsig); signal(SIGQUIT,fatalsig); siginit=1; } flush_socks(); window_flush(); FD_ZERO(&rfd); FD_ZERO(&wfd); FD_SET(0,&rfd); for (j=i=0;j<NSOCK;j++) if (sockets[j].inuse) { if (i<sockets[j].sock) i=sockets[j].sock; FD_SET(sockets[j].sock,&rfd); if (sockets[j].mode==SM_CONN) FD_SET(sockets[j].sock,&wfd); } tm=nearest_timeout(); if (tm<0) r=select(i+1,&rfd,&wfd,NULL,NULL); else { delay.tv_usec=(tm%1000)*1000; delay.tv_sec=tm/1000; r=select(i+1,&rfd,&wfd,NULL,&delay); } if (r<0) { if (errno!=EINTR) { clerr("select() failed"); return -1; } } out_sigcheck(); /* just in case that was SIGWINCH */ if (r>0) { if (FD_ISSET(0,&rfd)) out_process_input(); for (j=0;j<NSOCK;j++) if (sockets[j].inuse && (FD_ISSET(sockets[j].sock,&rfd) || FD_ISSET(sockets[j].sock,&wfd))) soproc(j,NULL,0); } process_timeouts(); return 0; }
static void redraw_handler(struct widget *widget, void *data) { struct view *view = data; struct rectangle allocation; cairo_surface_t *surface; cairo_t *cr; PopplerPage *page; double width, height, doc_aspect, window_aspect, scale; widget_get_allocation(view->widget, &allocation); surface = window_get_surface(view->window); cr = cairo_create(surface); cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height); cairo_clip(cr); cairo_set_source_rgba(cr, 0, 0, 0, 0.8); cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); cairo_paint(cr); if(!view->document) { cairo_destroy(cr); cairo_surface_destroy(surface); window_flush(view->window); return; } page = poppler_document_get_page(view->document, view->page); poppler_page_get_size(page, &width, &height); doc_aspect = width / height; window_aspect = (double) allocation.width / allocation.height; if (doc_aspect < window_aspect) scale = allocation.height / height; else scale = allocation.width / width; cairo_translate(cr, allocation.x, allocation.y); cairo_scale(cr, scale, scale); cairo_translate(cr, (allocation.width - width * scale) / 2 / scale, (allocation.height - height * scale) / 2 / scale); cairo_rectangle(cr, 0, 0, width, height); cairo_set_operator(cr, CAIRO_OPERATOR_OVER); cairo_set_source_rgb(cr, 1, 1, 1); cairo_fill(cr); poppler_page_render(page, cr); cairo_destroy(cr); cairo_surface_destroy(surface); g_object_unref(G_OBJECT(page)); }
void pstty_event(void *packet) { /* extract data from the packet */ char cmd = ((pstty_packet_t *) packet)->cmd; char chr = ((pstty_packet_t *) packet)->chr; char attr = ((pstty_packet_t *) packet)->attr; char x = ((pstty_packet_t *) packet)->x; char y = ((pstty_packet_t *) packet)->y; if (((pstty_packet_t *) packet)->prefix == 0x11) { /* timer alert */ window_flush(win, x1, y1, x2-x1, y2-y1); x1 = x2 = y1 = y2 = -1; scheduled = 0; return; } /* process the cmd */ switch (cmd) { case PSTTY_PUTC: pstty_putc(chr); break; case PSTTY_CHANGE_ATTR: pstty_change_attr(attr); break; case PSTTY_SET_ATTR_AT_OFF: pstty_set_attr_at_off(x, y, attr); break; case PSTTY_SET_CHAR_AT_OFF: pstty_set_char_at_off(x, y, chr); break; case PSTTY_SET_CURSOR: pstty_set_cursor(x, y); break; case PSTTY_GET_CURSOR: pstty_get_cursor(); break; default: break; } }
static void image_draw(struct image *image) { struct rectangle allocation; GdkPixbuf *pb; cairo_t *cr; cairo_surface_t *surface; window_draw(image->window); window_get_child_allocation(image->window, &allocation); pb = gdk_pixbuf_new_from_file_at_size(image->filename, allocation.width, allocation.height, NULL); if (pb == NULL) return; surface = window_get_surface(image->window); cr = cairo_create(surface); window_get_child_allocation(image->window, &allocation); cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height); cairo_clip(cr); cairo_push_group(cr); cairo_translate(cr, allocation.x, allocation.y); cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); cairo_set_source_rgba(cr, 0, 0, 0, 1); cairo_paint(cr); set_source_pixbuf(cr, pb, 0, 0, allocation.width, allocation.height); cairo_set_operator(cr, CAIRO_OPERATOR_OVER); cairo_paint(cr); g_object_unref(pb); cairo_pop_group_to_source(cr); cairo_paint(cr); cairo_destroy(cr); window_flush(image->window); cairo_surface_destroy(surface); }
void compress(int in, int out) { match_t m; window_t w; char *p; char *end; char buf[BUFSIZE]; int length; char tab[64]; int n = 0; window_init(&w, BUFSIZE); while ((length = read(in, buf, BUFSIZE)) > 0) { p = buf; end = buf+length; while (p < end) { m = window_match(&w, p, end); if (match_length(m) <= 1) { if (n >= match_length_max) { write_tab(out, tab, match_length_max); n = 0; } tab[n++] = *p; window_append(&w, *p); p++; } else { if (n) { write_tab(out, tab, n); n = 0; } write(out, &m, sizeof m); window_append_match(&w, m); p += match_length(m); } window_flush(&w); } } if (n) write_tab(out, tab, n); window_free(&w); }
int main(int argc, char *argv[]) { cairo_surface_t *s; struct flower flower; struct display *d; struct timeval tv; d = display_create(&argc, &argv, NULL, NULL); if (d == NULL) { fprintf(stderr, "failed to create display: %m\n"); return -1; } gettimeofday(&tv, NULL); srandom(tv.tv_usec); flower.width = 200; flower.height = 200; flower.display = d; flower.window = window_create(d, flower.width, flower.height); window_set_title(flower.window, "flower"); window_set_decoration(flower.window, 0); window_draw(flower.window); s = window_get_surface(flower.window); if (s == NULL || cairo_surface_status (s) != CAIRO_STATUS_SUCCESS) { fprintf(stderr, "failed to create cairo egl surface\n"); return -1; } draw_stuff(s, flower.width, flower.height); cairo_surface_flush(s); cairo_surface_destroy(s); window_flush(flower.window); window_set_motion_handler(flower.window, motion_handler); window_set_button_handler(flower.window, button_handler); window_set_user_data(flower.window, &flower); display_run(d); return 0; }
int sconnect(char *addr,int port,char *laddr,int doproc,PSHANDLER hf,void *ha) { int idx,opt; struct sockaddr_in rem,loc; #define FAIL0(m) { clwarnx(m); return -1; } #define FAIL1(m) { sockerrmsg(m,0); return -1; } #define FAIL2(m) { sockerrmsg(m,0); goto free_sock; } #define FAIL3(m) { sockerrmsg(m,0); goto free_event; } #define FAILD(m) { sockerrmsg(m,1); return -1; } #ifdef WIN32 if (initsockets()<0) return -1; #endif if (!hf) return -1; for (idx=0;idx<NSOCK;idx++) if (!sockets[idx].inuse) break; if (idx==NSOCK) { clwarnx("too many open sockets"); return -1; } sockets[idx].handler=hf; sockets[idx].data=ha; sockets[idx].ilen=sockets[idx].olen=0; sockets[idx].lp=1; sockets[idx].lpevent=-1; sockets[idx].state=sNORMAL; sockets[idx].attr=7; sockets[idx].mode=SM_NORM; sockets[idx].opmode=doproc ? SM_NORM : SM_HALF; sockets[idx].type=RT_SOCK; sockets[idx].zsp=NULL; sockets[idx].rawin=sockets[idx].rawout=sockets[idx].procin=0; window_flush(); /* flush output before a long blocking operation */ memset(&rem,0,sizeof(rem)); rem.sin_family=AF_INET; rem.sin_port=htons((short)port); if ((rem.sin_addr.s_addr=inet_addr(addr))==INADDR_NONE) { struct hostent *hp=gethostbyname(addr); if (!hp) FAILD("can't get remote address"); memcpy(&rem.sin_addr,hp->h_addr,sizeof(rem.sin_addr)); } memcpy(&sockets[idx].remote,&rem,sizeof(sockets[idx].remote)); if (laddr) { int slen; char *cp,*buf; memset(&loc,0,sizeof(loc)); loc.sin_family=AF_INET; slen=strlen(laddr); for (cp=laddr+slen;cp>laddr;--cp) if (*cp==':') break; if (*cp==':') { buf=malloc(cp-laddr+1); if (!buf) FAIL0("out of memory"); memcpy(buf,laddr,cp-laddr); buf[cp-laddr]='\0'; ++cp; loc.sin_port=htons((short)atoi(cp)); } else buf=laddr; if ((loc.sin_addr.s_addr=inet_addr(buf))==INADDR_NONE) { struct hostent *hp=gethostbyname(buf); if (!hp) FAILD("can't get local address"); memcpy(&loc.sin_addr,hp->h_addr,sizeof(loc.sin_addr)); } if (buf!=laddr) free(buf); } if ((sockets[idx].sock=socket(PF_INET,SOCK_STREAM,0))<0) FAIL1("can't create socket"); if (laddr) if (bind(sockets[idx].sock,(struct sockaddr *)&loc,sizeof(loc))<0) FAIL2("can't bind socket"); #ifdef WIN32 if ((sockets[idx].event=CreateEvent(NULL,FALSE,FALSE,NULL))==NULL) { clwarn("can't create event"); goto free_sock; } if (WSAEventSelect(sockets[idx].sock,sockets[idx].event,FD_READ|FD_CLOSE|FD_CONNECT)) FAIL3("can't bind event to socket"); #endif #ifndef WIN32 { int flags=fcntl(sockets[idx].sock,F_GETFL,0); if (flags<0) FAIL3("can't get fd flags"); if (fcntl(sockets[idx].sock,F_SETFL,flags|O_NONBLOCK)<0) FAIL3("can't set fd flags"); } #endif opt=KERN_SOCKBUFSIZE; setsockopt(sockets[idx].sock,SOL_SOCKET,SO_RCVBUF,(void*)&opt,sizeof(opt)); /* ignore result */ if (connect(sockets[idx].sock,(struct sockaddr *)&rem,sizeof(rem))<0) { #ifdef WIN32 if (WSAGetLastError()!=WSAEWOULDBLOCK) #else if (errno!=EINPROGRESS) #endif FAIL3("can't connect to remote host"); sockets[idx].mode=SM_CONN; sockets[idx].inuse=1; } else { sockets[idx].inuse=1; if (sockets[idx].handler) sockets[idx].handler(idx,SCONN,sockets[idx].data,NULL,0); } return idx; free_event: #ifdef WIN32 CloseHandle(sockets[idx].event); #endif free_sock: closesocket(sockets[idx].sock); return -1; #undef FAIL0 #undef FAIL1 #undef FAIL2 #undef FAIL3 }