add to request dbus rwx
[framework/system/sdbd.git] / src / framebuffer_service.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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 #include <errno.h>
23 #include <sys/types.h>
24 #include <sys/wait.h>
25
26 #include "fdevent.h"
27 #include "sdb.h"
28
29 #include <linux/fb.h>
30 #include <sys/ioctl.h>
31 #include <sys/mman.h>
32
33 /* TODO:
34 ** - sync with vsync to avoid tearing
35 */
36 /* This version number defines the format of the fbinfo struct.
37    It must match versioning in ddms where this data is consumed. */
38 #define DDMS_RAWIMAGE_VERSION 1
39 struct fbinfo {
40     unsigned int version;
41     unsigned int bpp;
42     unsigned int size;
43     unsigned int width;
44     unsigned int height;
45     unsigned int red_offset;
46     unsigned int red_length;
47     unsigned int blue_offset;
48     unsigned int blue_length;
49     unsigned int green_offset;
50     unsigned int green_length;
51     unsigned int alpha_offset;
52     unsigned int alpha_length;
53 } __attribute__((packed));
54
55 void framebuffer_service(int fd, void *cookie)
56 {
57     struct fbinfo fbinfo;
58     unsigned int i;
59     char buf[640];
60     int fd_screencap;
61     int w, h, f;
62     int fds[2];
63     pid_t pid = 0;
64
65     if (pipe(fds) < 0) goto done;
66
67     pid = fork();
68     if (pid < 0) goto done;
69
70     if (pid == 0) {
71         dup2(fds[1], STDOUT_FILENO);
72         close(fds[0]);
73         close(fds[1]);
74         const char* command = "screencap";
75         const char *args[2] = {command, NULL};
76         execvp(command, (char**)args);
77         exit(1);
78     }
79
80     fd_screencap = fds[0];
81
82     /* read w, h & format */
83     if(readx(fd_screencap, &w, 4)) goto done;
84     if(readx(fd_screencap, &h, 4)) goto done;
85     if(readx(fd_screencap, &f, 4)) goto done;
86
87     fbinfo.version = DDMS_RAWIMAGE_VERSION;
88     /* see hardware/hardware.h */
89     switch (f) {
90         case 1: /* RGBA_8888 */
91             fbinfo.bpp = 32;
92             fbinfo.size = w * h * 4;
93             fbinfo.width = w;
94             fbinfo.height = h;
95             fbinfo.red_offset = 0;
96             fbinfo.red_length = 8;
97             fbinfo.green_offset = 8;
98             fbinfo.green_length = 8;
99             fbinfo.blue_offset = 16;
100             fbinfo.blue_length = 8;
101             fbinfo.alpha_offset = 24;
102             fbinfo.alpha_length = 8;
103             break;
104         case 2: /* RGBX_8888 */
105             fbinfo.bpp = 32;
106             fbinfo.size = w * h * 4;
107             fbinfo.width = w;
108             fbinfo.height = h;
109             fbinfo.red_offset = 0;
110             fbinfo.red_length = 8;
111             fbinfo.green_offset = 8;
112             fbinfo.green_length = 8;
113             fbinfo.blue_offset = 16;
114             fbinfo.blue_length = 8;
115             fbinfo.alpha_offset = 24;
116             fbinfo.alpha_length = 0;
117             break;
118         case 3: /* RGB_888 */
119             fbinfo.bpp = 24;
120             fbinfo.size = w * h * 3;
121             fbinfo.width = w;
122             fbinfo.height = h;
123             fbinfo.red_offset = 0;
124             fbinfo.red_length = 8;
125             fbinfo.green_offset = 8;
126             fbinfo.green_length = 8;
127             fbinfo.blue_offset = 16;
128             fbinfo.blue_length = 8;
129             fbinfo.alpha_offset = 24;
130             fbinfo.alpha_length = 0;
131             break;
132         case 4: /* RGB_565 */
133             fbinfo.bpp = 16;
134             fbinfo.size = w * h * 2;
135             fbinfo.width = w;
136             fbinfo.height = h;
137             fbinfo.red_offset = 11;
138             fbinfo.red_length = 5;
139             fbinfo.green_offset = 5;
140             fbinfo.green_length = 6;
141             fbinfo.blue_offset = 0;
142             fbinfo.blue_length = 5;
143             fbinfo.alpha_offset = 0;
144             fbinfo.alpha_length = 0;
145             break;
146         case 5: /* BGRA_8888 */
147             fbinfo.bpp = 32;
148             fbinfo.size = w * h * 4;
149             fbinfo.width = w;
150             fbinfo.height = h;
151             fbinfo.red_offset = 16;
152             fbinfo.red_length = 8;
153             fbinfo.green_offset = 8;
154             fbinfo.green_length = 8;
155             fbinfo.blue_offset = 0;
156             fbinfo.blue_length = 8;
157             fbinfo.alpha_offset = 24;
158             fbinfo.alpha_length = 8;
159            break;
160         default:
161             goto done;
162     }
163
164     /* write header */
165     if(writex(fd, &fbinfo, sizeof(fbinfo))) goto done;
166
167     /* write data */
168     for(i = 0; i < fbinfo.size; i += sizeof(buf)) {
169       if(readx(fd_screencap, buf, sizeof(buf))) goto done;
170       if(writex(fd, buf, sizeof(buf))) goto done;
171     }
172     if(readx(fd_screencap, buf, fbinfo.size % sizeof(buf))) goto done;
173     if(writex(fd, buf, fbinfo.size % sizeof(buf))) goto done;
174
175 done:
176     TEMP_FAILURE_RETRY(waitpid(pid, NULL, 0));
177
178     close(fds[0]);
179     close(fds[1]);
180     close(fd);
181 }