blob: 434eb1c9f16cfd0c5fd17ba92757e0d9a1fd8348 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdlib.h>
18#include <stdio.h>
19#include <unistd.h>
20#include <string.h>
21#include <fcntl.h>
22
David 'Digit' Turner414ff7d2009-05-18 17:07:46 +020023#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024#include "adb.h"
25
26#include <linux/fb.h>
27#include <sys/ioctl.h>
28#include <sys/mman.h>
29
30/* TODO:
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031** - sync with vsync to avoid tearing
32*/
Rebecca Schultz Zavin154b7d72009-09-15 21:06:12 -070033/* This version number defines the format of the fbinfo struct.
34 It must match versioning in ddms where this data is consumed. */
35#define DDMS_RAWIMAGE_VERSION 1
36struct fbinfo {
37 unsigned int version;
38 unsigned int bpp;
39 unsigned int size;
40 unsigned int width;
41 unsigned int height;
42 unsigned int red_offset;
43 unsigned int red_length;
44 unsigned int blue_offset;
45 unsigned int blue_length;
46 unsigned int green_offset;
47 unsigned int green_length;
48 unsigned int alpha_offset;
49 unsigned int alpha_length;
50} __attribute__((packed));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051
52void framebuffer_service(int fd, void *cookie)
53{
Rebecca Schultz Zavin154b7d72009-09-15 21:06:12 -070054 struct fbinfo fbinfo;
Mathias Agopian0715f912010-09-26 18:44:28 -070055 unsigned int i;
56 char buf[640];
57 int fd_screencap;
58 int w, h, f;
59 int fds[2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
Mathias Agopian0715f912010-09-26 18:44:28 -070061 if (pipe(fds) < 0) goto done;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062
Mathias Agopian0715f912010-09-26 18:44:28 -070063 pid_t pid = fork();
64 if (pid < 0) goto done;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065
Mathias Agopian0715f912010-09-26 18:44:28 -070066 if (pid == 0) {
67 dup2(fds[1], STDOUT_FILENO);
68 close(fds[0]);
69 close(fds[1]);
70 const char* command = "screencap";
71 const char *args[2] = {command, NULL};
72 execvp(command, (char**)args);
73 exit(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074 }
75
Mathias Agopian0715f912010-09-26 18:44:28 -070076 fd_screencap = fds[0];
77
78 /* read w, h & format */
79 if(readx(fd_screencap, &w, 4)) goto done;
80 if(readx(fd_screencap, &h, 4)) goto done;
81 if(readx(fd_screencap, &f, 4)) goto done;
82
83 /* for now always assume RGBX_8888 format */
84 fbinfo.version = DDMS_RAWIMAGE_VERSION;
85 fbinfo.bpp = 32;
86 fbinfo.size = w * h * 4;
87 fbinfo.width = w;
88 fbinfo.height = h;
89 fbinfo.red_offset = 0;
90 fbinfo.red_length = 8;
91 fbinfo.green_offset = 8;
92 fbinfo.green_length = 8;
93 fbinfo.blue_offset = 16;
94 fbinfo.blue_length = 8;
95 fbinfo.alpha_offset = 24;
96 fbinfo.alpha_length = 8;
97
98 /* write header */
99 if(writex(fd, &fbinfo, sizeof(fbinfo))) goto done;
100
101 /* write data */
102 for(i = 0; i < fbinfo.size; i += sizeof(buf)) {
103 if(readx(fd_screencap, buf, sizeof(buf))) goto done;
104 if(writex(fd, buf, sizeof(buf))) goto done;
105 }
106 if(readx(fd_screencap, buf, fbinfo.size % sizeof(buf))) goto done;
107 if(writex(fd, buf, fbinfo.size % sizeof(buf))) goto done;
Rebecca Schultz Zavin04bee292009-09-09 21:39:41 -0700108
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109done:
Mathias Agopian0715f912010-09-26 18:44:28 -0700110 close(fds[0]);
111 close(fds[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112 close(fd);
113}