Beispiel #1
0
func main() {
	// I assume that in is 0
	in2, err := syscall.Dup(0)
	if err != nil {
		log.Fatal("dupping 0", err)
	}

	out2, err := syscall.Dup(1)
	if err != nil {
		log.Fatal("dupping 1", err)
	}

	os.Stdin.Close()
	os.Stdout.Close()
	os.Stdout = os.NewFile(uintptr(2), "/dev/stdout")

	/* Connections to the application. */
	cin := os.NewFile(uintptr(in2), "fromapp")
	cout := os.NewFile(uintptr(out2), "toapp")

	modifyEnvironment()

	// Fire up a new devdraw here
	devdraw, err := drawfcall.New()
	if err != nil {
		log.Fatal("making a Conn", err)
	}

	// There is probably a nicer way to do this.
	// TODO(rjkroege): do it the nicer way.
	var app App
	app.o = cout
	app.i = cin

	json := NewJsonRecorder()

	for {
		// read crap from cin
		log.Print("about to read from host")
		inbuffy, err := drawfcall.ReadMsg(cin)
		log.Print("read from host")
		if err != nil {
			devdraw.Close()
			break
		}
		go marshalsxtx(inbuffy, &app, devdraw, json)
	}

	log.Print("waiting on completion")
	json.WaitToComplete()
}
Beispiel #2
0
// Init starts and connects to a server and returns a Display structure through
// which all graphics will be mediated. The arguments are an error channel on
// which to deliver errors (currently unused), the name of the font to use (the
// empty string may be used to represent the default font), the window label,
// and the window size as a string in the form XxY, as in "1000x500"; the units
// are pixels.
// TODO: Use the error channel.
func Init(errch chan<- error, fontname, label, winsize string) (*Display, error) {
	c, err := drawfcall.New()
	if err != nil {
		return nil, err
	}
	d := &Display{
		conn:    c,
		errch:   errch,
		bufsize: 10000,
	}

	// Lock Display so we maintain the contract within this library.
	d.mu.Lock()
	defer d.mu.Unlock()

	d.buf = make([]byte, 0, d.bufsize+5) // 5 for final flush
	if err := c.Init(label, winsize); err != nil {
		c.Close()
		return nil, err
	}

	i, err := d.getimage0(nil)
	if err != nil {
		c.Close()
		return nil, err
	}

	d.Image = i
	d.White, err = d.allocImage(image.Rect(0, 0, 1, 1), GREY1, true, White)
	if err != nil {
		return nil, err
	}
	d.Black, err = d.allocImage(image.Rect(0, 0, 1, 1), GREY1, true, Black)
	if err != nil {
		return nil, err
	}
	d.Opaque = d.White
	d.Transparent = d.Black

	/*
	 * Set up default font
	 */
	df, err := getdefont(d)
	if err != nil {
		return nil, err
	}
	d.DefaultSubfont = df

	if fontname == "" {
		fontname = os.Getenv("font")
	}

	/*
	 * Build fonts with caches==depth of screen, for speed.
	 * If conversion were faster, we'd use 0 and save memory.
	 */
	var font *Font
	if fontname == "" {
		buf := []byte(fmt.Sprintf("%d %d\n0 %d\t%s\n", df.Height, df.Ascent,
			df.N-1, deffontname))
		//fmt.Printf("%q\n", buf)
		//BUG: Need something better for this	installsubfont("*default*", df);
		font, err = d.buildFont(buf, deffontname)
	} else {
		font, err = d.openFont(fontname) // BUG: grey fonts
	}
	if err != nil {
		fmt.Fprintf(os.Stderr, "imageinit: can't open default font %s: %v\n", fontname, err)
		return nil, err
	}
	d.DefaultFont = font

	d.Screen, err = i.allocScreen(d.White, false)
	if err != nil {
		return nil, err
	}
	d.ScreenImage = d.Image // temporary, for d.ScreenImage.Pix
	d.ScreenImage, err = allocwindow(nil, d.Screen, i.R, 0, White)
	if err != nil {
		return nil, err
	}
	if err := d.flush(true); err != nil {
		log.Fatal(err)
	}

	screen := d.ScreenImage
	screen.draw(screen.R, d.White, nil, image.ZP)
	if err := d.flush(true); err != nil {
		log.Fatal(err)
	}

	return d, nil
}