1 /*
2  * Copyright (c) 2008-2009 Intel Corporation. All Rights Reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sub license, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial portions
14  * of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <va/va_x11.h>
28 
29 static  Window window_thread0, window_thread1;
30 static  GC context_thread0, context_thread1;
31 static  pthread_mutex_t gmutex;
32 
33 static void *open_display(void);
34 static void close_display(void *win_display);
35 static int create_window(void *win_display, int x, int y, int width, int height);
36 static int check_window_event(void *x11_display, void *drawable, int *width, int *height, int *quit);
37 
38 #define CAST_DRAWABLE(a)  (Drawable)(a)
39 
40 #include "putsurface_common.c"
41 
open_display(void)42 static void *open_display(void)
43 {
44     return XOpenDisplay(NULL);
45 }
46 
close_display(void * win_display)47 static void close_display(void *win_display)
48 {
49     XCloseDisplay(win_display);
50 }
51 
create_pixmap(void * win_display,int width,int height)52 static Pixmap create_pixmap(void *win_display, int width, int height)
53 {
54     Display *x11_display = (Display *)win_display;
55     int screen = DefaultScreen(x11_display);
56     Window root;
57     Pixmap pixmap;
58     XWindowAttributes attr;
59 
60     root = RootWindow(x11_display, screen);
61 
62     XGetWindowAttributes (x11_display, root, &attr);
63 
64     printf("Create a pixmap from ROOT window %dx%d, pixmap size %dx%d\n\n", attr.width, attr.height, width, height);
65     pixmap = XCreatePixmap(x11_display, root, width, height,
66                            DefaultDepth(x11_display, DefaultScreen(x11_display)));
67 
68     return pixmap;
69 }
70 
create_window(void * win_display,int x,int y,int width,int height)71 static int create_window(void *win_display, int x, int y, int width, int height)
72 {
73     Display *x11_display = (Display *)win_display;
74     int screen = DefaultScreen(x11_display);
75     Window root, win;
76 
77     root = RootWindow(x11_display, screen);
78 
79     printf("Create window0 for thread0\n");
80     drawable_thread0 = (void *)XCreateSimpleWindow(x11_display, root, x, y, width, height,
81                                            0, 0, WhitePixel(x11_display, 0));
82 
83     win = (Window)drawable_thread0;
84     if (drawable_thread0) {
85         XSizeHints sizehints;
86         sizehints.width  = width;
87         sizehints.height = height;
88         sizehints.flags = USSize;
89         XSetNormalHints(x11_display, win, &sizehints);
90         XSetStandardProperties(x11_display, win, "Thread 0", "Thread 0",
91                                None, (char **)NULL, 0, &sizehints);
92 
93         XMapWindow(x11_display, win);
94     }
95     context_thread0 = XCreateGC(x11_display, win, 0, 0);
96     XSelectInput(x11_display, win, KeyPressMask | StructureNotifyMask);
97     XSync(x11_display, False);
98 
99     if (put_pixmap) {
100         window_thread0 = (Window)drawable_thread0;
101         drawable_thread0 = (void *)create_pixmap(x11_display, width, height);
102     }
103 
104     if (multi_thread == 0)
105         return 0;
106 
107     printf("Create window1 for thread1\n");
108 
109     drawable_thread1 = (void *)XCreateSimpleWindow(x11_display, root, width, 0, width, height,
110                                             0, 0, WhitePixel(x11_display, 0));
111     win = (Window)drawable_thread1;
112     if (drawable_thread1) {
113         XSizeHints sizehints;
114         sizehints.width  = width;
115         sizehints.height = height;
116         sizehints.flags = USSize;
117         XSetNormalHints(x11_display, win, &sizehints);
118         XSetStandardProperties(x11_display, win, "Thread 1", "Thread 1",
119                                None, (char **)NULL, 0, &sizehints);
120 
121         XMapWindow(x11_display, win);
122     }
123     if (put_pixmap) {
124         window_thread1 = (Window)drawable_thread1;
125         drawable_thread1 = (void *)create_pixmap(x11_display, width, height);
126     }
127 
128     context_thread1 = XCreateGC(x11_display, win, 0, 0);
129     XSelectInput(x11_display, win, KeyPressMask | StructureNotifyMask);
130     XSync(x11_display, False);
131 
132     return 0;
133 }
134 
check_window_event(void * win_display,void * drawable,int * width,int * height,int * quit)135 static int check_window_event(void *win_display, void *drawable, int *width, int *height, int *quit)
136 {
137     int is_event = 0;
138     XEvent event;
139     Window win = (Window)drawable;
140     Display *x11_display = (Display *)win_display;
141 
142 
143     if (check_event == 0)
144         return 0;
145 
146     pthread_mutex_lock(&gmutex);
147     is_event = XCheckWindowEvent(x11_display, win, StructureNotifyMask|KeyPressMask,&event);
148     pthread_mutex_unlock(&gmutex);
149 
150     if (is_event == 0)
151         return 0;
152 
153     /* bail on any focused key press */
154     if(event.type == KeyPress) {
155         *quit = 1;
156         return 0;
157     }
158 
159 #if 0
160     /* rescale the video to fit the window */
161     if(event.type == ConfigureNotify) {
162         *width = event.xconfigure.width;
163         *height = event.xconfigure.height;
164         printf("Scale window to %dx%d\n", width, height);
165     }
166 #endif
167 
168     return 0;
169 }
170 
171 
172 
173