#if 0
Message-Id: <199902041144.UAA05856@shin1.sm.sony.co.jp>
To: Pocket BSD Developpers ML <pbsd-dev@arseed.co.jp>
Subject: [pbsd-dev:01721] screen dump tool
From: takemura@sm.sony.co.jp

皆さんこんにちは。たけむらです。

まるで今思い出したのですが、
ずっと前に作った、screen dump を作成するプログラムをお送りします。

これをコンパイルして mgdump というコマンドを作成し、実行すると
いきなり標準出力に Windows bitmap format で画面のダンプを出力します。

#endif
/*
 * Screen dumper.
 *
 * Copyright (c) 1998 Shin Takemura all rights reserved.
 *
 * Apr. 4, 1999. Modefied by Ichiya KAMKI
 *
 * $Id: mgdump.c,v 1.1 1998/04/04 01:27:49 takemura Exp $
 *
 */
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/mman.h>

#define DEVNAME "/dev/ttyv0"
#define MGLINESTEP 0x100
#define MGWIDTH 640
#define MGHEIGHT 240

unsigned char os2_bmp_header[] = {
  0x42,0x4d,0x4c,0x2c,0x01,0x00,0x00,0x00,
  0x00,0x00,0x4a,0x00,0x00,0x00,0x0c,0x00,
  0x00,0x00,0x80,0x02,0xf0,0x00,0x01,0x00,
  0x04,0x00,0x14,0x19,0x17,0x33,0x40,0x39,
  0x85,0xa6,0x95,0xa3,0xcc,0xb8,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00
};

int main(){
    int fd;
    unsigned char *base;
    int x, y;
    unsigned char buf[MGWIDTH];
    unsigned char *in, *out;
    unsigned char values[4] = { 0x3, 0x2, 0x1, 0x0 };

    /* えーと、original のコードは /dev/ttyv0 ひらいて mmap してるんだけど、
     *  /dev/ttyv? って何 ^_^??
     *  linux にもあるけど、それで動くの? したら mgfb 要らないんだけど...
     */

    int fb = open("/dev/mgfb", O_RDONLY);
    if(fb < 0){
	perror("open /dev/mgfb");
	exit(1);
	return 0;
    }
    base = (unsigned char *) mmap((caddr_t)0, 0x10000,
				  PROT_READ,
				  MAP_SHARED | MAP_FILE,
				  fb,
				  0);
    if (base == (void *)(-1)) {
	perror("mmap");
	exit(1);
	return 0;
    }
    close(fb);

    /* fwrite(base, 0x100, 240, stdout); */

    if (fwrite(os2_bmp_header, sizeof(os2_bmp_header), 1, stdout) != 1) {
	perror("stdout");
	exit(1);
    }
    for (y = MGHEIGHT-1; 0 <= y; y--) {
	in = &base[MGLINESTEP*y];
	out = buf;
	for (x = 0; x < MGWIDTH/4; x++) {
	    *out++ = (values[(*in & 0x03)>>0]<<4) | values[(*in & 0x0c)>>2];
	    *out++ = (values[(*in & 0x30)>>4]<<4) | values[(*in & 0xc0)>>6];
	    in++;
	}
	if (fwrite(buf, MGWIDTH/2, 1, stdout) != 1) {
	    perror("stdout");
	    exit(1);
	}
    }
}
#if 0
具体的な使い方としては、

  1)お気に入りのアプリケーションを起動する。
  2)[メニュー] + F・2 などでコンソールを切替える
  3)「sleep 5 ; mgdump > /tmp/dump.bmp」などと実行
  4)素早く[メニュー] + F・1 などでコンソールをもとに戻す

というようなかんじです。
#endif
