2 #include "bcresources.h"
3 #include "bcwindowbase.h"
4 #include "colormodels.h"
17 // 24 bpp unpacked: 0bgr
20 BC_Capture::BC_Capture(int w, int h, char *display_path)
27 init_window(display_path);
32 BC_Capture::~BC_Capture()
35 XCloseDisplay(display);
38 int BC_Capture::init_window(char *display_path)
41 if(display_path && display_path[0] == 0) display_path = NULL;
42 if((display = XOpenDisplay(display_path)) == NULL)
44 printf(_("cannot connect to X server.\n"));
45 if(getenv("DISPLAY") == NULL)
46 printf(_("'DISPLAY' environment variable not set.\n"));
51 screen = DefaultScreen(display);
52 rootwin = RootWindow(display, screen);
53 vis = DefaultVisual(display, screen);
54 default_depth = DefaultDepth(display, screen);
55 client_byte_order = (*(u_int32_t*)"a ") & 0x00000001;
56 server_byte_order = (XImageByteOrder(display) == MSBFirst) ? 0 : 1;
59 ximage = XCreateImage(display,
69 bits_per_pixel = ximage->bits_per_pixel;
70 XDestroyImage(ximage);
71 bitmap_color_model = BC_WindowBase::evaluate_color_model(client_byte_order, server_byte_order, bits_per_pixel);
74 // This doesn't ensure the X Server is on the local host
75 if(use_shm && !XShmQueryExtension(display))
83 int BC_Capture::allocate_data()
86 if(!display) return 1;
89 ximage = XShmCreateImage(display, vis, default_depth, ZPixmap, (char*)NULL, &shm_info, w, h);
91 shm_info.shmid = shmget(IPC_PRIVATE, h * ximage->bytes_per_line, IPC_CREAT | 0777);
92 if(shm_info.shmid < 0) perror("BC_Capture::allocate_data shmget");
93 data = (unsigned char *)shmat(shm_info.shmid, NULL, 0);
94 shmctl(shm_info.shmid, IPC_RMID, 0);
95 ximage->data = shm_info.shmaddr = (char*)data; // setting ximage->data stops BadValue
96 shm_info.readOnly = 0;
98 // Crashes here if remote server.
99 BC_Resources::error = 0;
100 XShmAttach(display, &shm_info);
101 XSync(display, False);
102 if(BC_Resources::error)
104 XDestroyImage(ximage);
105 shmdt(shm_info.shmaddr);
112 // need to use bytes_per_line for some X servers
114 ximage = XCreateImage(display, vis, default_depth, ZPixmap, 0, (char*)data, w, h, 8, 0);
115 data = (unsigned char*)malloc(h * ximage->bytes_per_line);
116 XDestroyImage(ximage);
118 ximage = XCreateImage(display, vis, default_depth, ZPixmap, 0, (char*)data, w, h, 8, 0);
121 row_data = new unsigned char*[h];
122 for(int i = 0; i < h; i++)
124 row_data[i] = &data[i * ximage->bytes_per_line];
126 // This differs from the depth parameter of the top_level.
127 bits_per_pixel = ximage->bits_per_pixel;
131 int BC_Capture::delete_data()
133 if(!display) return 1;
138 XShmDetach(display, &shm_info);
139 XDestroyImage(ximage);
140 shmdt(shm_info.shmaddr);
144 XDestroyImage(ximage);
147 // data is automatically freed by XDestroyImage
155 int BC_Capture::get_w() { return w; }
156 int BC_Capture::get_h() { return h; }
158 // Capture a frame from the screen
159 #define CAPTURE_FRAME_HEAD \
160 for(int i = 0; i < h; i++) \
162 unsigned char *input_row = row_data[i]; \
163 unsigned char *output_row = (unsigned char*)frame->get_rows()[i]; \
164 for(int j = 0; j < w; j++) \
167 #define CAPTURE_FRAME_TAIL \
173 int BC_Capture::capture_frame(VFrame *frame, int &x1, int &y1)
175 if(!display) return 1;
178 if(x1 > get_top_w() - w) x1 = get_top_w() - w;
179 if(y1 > get_top_h() - h) y1 = get_top_h() - h;
184 XShmGetImage(display, rootwin, ximage, x1, y1, 0xffffffff);
186 XGetSubImage(display, rootwin, x1, y1, w, h, 0xffffffff, ZPixmap, ximage, 0, 0);
188 cmodel_transfer(frame->get_rows(),
205 frame->get_color_model(),
213 int BC_Capture::get_top_w()
215 Screen *screen_ptr = XDefaultScreenOfDisplay(display);
216 return WidthOfScreen(screen_ptr);
219 int BC_Capture::get_top_h()
221 Screen *screen_ptr = XDefaultScreenOfDisplay(display);
222 return HeightOfScreen(screen_ptr);