r878: This is Cinelerra 2.1.
[cinelerra_cv/ct.git] / guicast / bccapture.C
blob25c665b7e05adf826f0316ee869c222d8003ce86
1 #include "bccapture.h"
2 #include "bcresources.h"
3 #include "bcwindowbase.h"
4 #include "colormodels.h"
5 #include "language.h"
6 #include "vframe.h"
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <X11/Xutil.h>
15 // Byte orders:
16 // 24 bpp packed:         bgr
17 // 24 bpp unpacked:       0bgr
20 BC_Capture::BC_Capture(int w, int h, char *display_path)
22         this->w = w;
23         this->h = h;
25         data = 0;
26         use_shm = 1;
27         init_window(display_path);
28         allocate_data();
32 BC_Capture::~BC_Capture()
34         delete_data();
35         XCloseDisplay(display);
38 int BC_Capture::init_window(char *display_path)
40         int bits_per_pixel;
41         if(display_path && display_path[0] == 0) display_path = NULL;
42         if((display = XOpenDisplay(display_path)) == NULL)
43         {
44                 printf(_("cannot connect to X server.\n"));
45                 if(getenv("DISPLAY") == NULL)
46                 printf(_("'DISPLAY' environment variable not set.\n"));
47                 exit(-1);
48                 return 1;
49         }
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;
57         char *data = 0;
58         XImage *ximage;
59         ximage = XCreateImage(display, 
60                                         vis, 
61                                         default_depth, 
62                                         ZPixmap, 
63                                         0, 
64                                         data, 
65                                         16, 
66                                         16, 
67                                         8, 
68                                         0);
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);
73 // test shared memory
74 // This doesn't ensure the X Server is on the local host
75     if(use_shm && !XShmQueryExtension(display))
76     {
77         use_shm = 0;
78     }
79         return 0;
83 int BC_Capture::allocate_data()
85 // try shared memory
86         if(!display) return 1;
87     if(use_shm)
88         {
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)
103                 {
104                         XDestroyImage(ximage);
105                         shmdt(shm_info.shmaddr);
106                         use_shm = 0;
107                 }
108         }
110         if(!use_shm)
111         {
112 // need to use bytes_per_line for some X servers
113                 data = 0;
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);
119         }
121         row_data = new unsigned char*[h];
122         for(int i = 0; i < h; i++)
123         {
124                 row_data[i] = &data[i * ximage->bytes_per_line];
125         }
126 // This differs from the depth parameter of the top_level.
127         bits_per_pixel = ximage->bits_per_pixel;
128         return 0;
131 int BC_Capture::delete_data()
133         if(!display) return 1;
134         if(data)
135         {
136                 if(use_shm)
137                 {
138                         XShmDetach(display, &shm_info);
139                         XDestroyImage(ximage);
140                         shmdt(shm_info.shmaddr);
141                 }
142                 else
143                 {
144                         XDestroyImage(ximage);
145                 }
147 // data is automatically freed by XDestroyImage
148                 data = 0;
149                 delete row_data;
150         }
151         return 0;
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++) \
161         { \
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++) \
165                 {
167 #define CAPTURE_FRAME_TAIL \
168                 } \
169         }
173 int BC_Capture::capture_frame(VFrame *frame, int &x1, int &y1)
175         if(!display) return 1;
176         if(x1 < 0) x1 = 0;
177         if(y1 < 0) y1 = 0;
178         if(x1 > get_top_w() - w) x1 = get_top_w() - w;
179         if(y1 > get_top_h() - h) y1 = get_top_h() - h;
182 // Read the raw data
183         if(use_shm)
184                 XShmGetImage(display, rootwin, ximage, x1, y1, 0xffffffff);
185         else
186                 XGetSubImage(display, rootwin, x1, y1, w, h, 0xffffffff, ZPixmap, ximage, 0, 0);
188         cmodel_transfer(frame->get_rows(), 
189                 row_data,
190                 frame->get_y(),
191                 frame->get_u(),
192                 frame->get_v(),
193                 0,
194                 0,
195                 0,
196                 0, 
197                 0, 
198                 w, 
199                 h,
200                 0, 
201                 0, 
202                 frame->get_w(), 
203                 frame->get_h(),
204                 bitmap_color_model, 
205                 frame->get_color_model(),
206                 0,
207                 frame->get_w(),
208                 w);
210         return 0;
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);