1/*
2 * Xephyr - A kdrive X server thats runs in a host X window.
3 *          Authored by Matthew Allum <mallum@openedhand.com>
4 *
5 * Copyright © 2007 OpenedHand Ltd
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of OpenedHand Ltd not be used in
12 * advertising or publicity pertaining to distribution of the software without
13 * specific, written prior permission. OpenedHand Ltd makes no
14 * representations about the suitability of this software for any purpose.  It
15 * is provided "as is" without express or implied warranty.
16 *
17 * OpenedHand Ltd DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19 * EVENT SHALL OpenedHand Ltd BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
23 * PERFORMANCE OF THIS SOFTWARE.
24 *
25 * Authors:
26 *    Dodji Seketeli <dodji@openedhand.com>
27 */
28#ifdef HAVE_CONFIG_H
29#include <kdrive-config.h>
30#endif
31
32#include <X11/Xutil.h>
33#include <X11/Xlibint.h>
34#include <GL/glx.h>
35#include "xf86dri.h"
36#include "hostx.h"
37#include "ephyrdri.h"
38#define _HAVE_XALLOC_DECLS
39#include "ephyrlog.h"
40#include "dixstruct.h"
41#include "pixmapstr.h"
42
43#ifndef TRUE
44#define TRUE 1
45#endif /*TRUE*/
46
47#ifndef FALSE
48#define FALSE 0
49#endif /*FALSE*/
50
51Bool
52ephyrDRIQueryDirectRenderingCapable (int a_screen, Bool *a_is_capable)
53{
54    Display *dpy=hostx_get_display () ;
55    Bool is_ok=FALSE ;
56
57    EPHYR_RETURN_VAL_IF_FAIL (a_is_capable, FALSE) ;
58    EPHYR_LOG ("enter\n") ;
59    is_ok = XF86DRIQueryDirectRenderingCapable (dpy, DefaultScreen (dpy),
60                                                a_is_capable) ;
61    EPHYR_LOG ("leave. is_capable:%d, is_ok=%d\n", *a_is_capable, is_ok) ;
62
63    return is_ok ;
64}
65
66Bool
67ephyrDRIOpenConnection (int a_screen,
68                        drm_handle_t *a_sarea,
69                        char **a_bus_id_string)
70{
71    Display *dpy = hostx_get_display () ;
72    Bool is_ok=FALSE ;
73
74    EPHYR_RETURN_VAL_IF_FAIL (a_bus_id_string, FALSE) ;
75    EPHYR_LOG ("enter. screen:%d\n", a_screen) ;
76    is_ok = XF86DRIOpenConnection (dpy, DefaultScreen (dpy),
77                                   a_sarea,
78                                   a_bus_id_string) ;
79    if (*a_bus_id_string) {
80        EPHYR_LOG ("leave. bus_id_string:%s, is_ok:%d\n",
81                   *a_bus_id_string, is_ok) ;
82    } else {
83        EPHYR_LOG ("leave. bus_id_string:null, is_ok:%d\n",
84                   is_ok) ;
85    }
86    return is_ok ;
87}
88
89Bool
90ephyrDRIAuthConnection (int a_screen, drm_magic_t a_magic)
91{
92    Display *dpy = hostx_get_display () ;
93    Bool is_ok=FALSE ;
94
95    EPHYR_LOG ("enter\n") ;
96    is_ok = XF86DRIAuthConnection (dpy, DefaultScreen (dpy), a_magic) ;
97    EPHYR_LOG ("leave. is_ok:%d\n", is_ok) ;
98    return is_ok ;
99}
100
101Bool
102ephyrDRICloseConnection (int a_screen)
103{
104    Display *dpy = hostx_get_display () ;
105    Bool is_ok=FALSE ;
106
107    EPHYR_LOG ("enter\n") ;
108    is_ok = XF86DRICloseConnection (dpy, DefaultScreen (dpy)) ;
109    EPHYR_LOG ("leave\n") ;
110    return is_ok ;
111}
112
113Bool
114ephyrDRIGetClientDriverName (int a_screen,
115                             int *a_ddx_driver_major_version,
116                             int *a_ddx_driver_minor_version,
117                             int *a_ddx_driver_patch_version,
118                             char ** a_client_driver_name)
119{
120    Display *dpy = hostx_get_display () ;
121    Bool is_ok=FALSE ;
122
123    EPHYR_RETURN_VAL_IF_FAIL (a_ddx_driver_major_version
124                              && a_ddx_driver_minor_version
125                              && a_ddx_driver_patch_version
126                              && a_client_driver_name,
127                              FALSE);
128    EPHYR_LOG ("enter\n") ;
129    is_ok = XF86DRIGetClientDriverName (dpy, DefaultScreen (dpy),
130                                        a_ddx_driver_major_version,
131                                        a_ddx_driver_minor_version,
132                                        a_ddx_driver_patch_version,
133                                        a_client_driver_name) ;
134    EPHYR_LOG ("major:%d, minor:%d, patch:%d, name:%s\n",
135                *a_ddx_driver_major_version,
136                *a_ddx_driver_minor_version,
137                *a_ddx_driver_patch_version,
138                *a_client_driver_name) ;
139    EPHYR_LOG ("leave:%d\n", is_ok) ;
140    return is_ok ;
141}
142
143Bool
144ephyrDRICreateContext (int a_screen,
145                       int a_visual_id,
146                       XID *a_returned_ctxt_id,
147                       drm_context_t *a_hw_ctxt)
148{
149    Display *dpy = hostx_get_display () ;
150    Bool is_ok=FALSE ;
151    Visual v;
152
153    EPHYR_LOG ("enter. screen:%d, visual:%d\n", a_screen, a_visual_id) ;
154    memset (&v, 0, sizeof (v)) ;
155    v.visualid = a_visual_id ;
156    is_ok = XF86DRICreateContext (dpy,
157                                  DefaultScreen (dpy),
158                                  &v,
159                                  a_returned_ctxt_id,
160                                  a_hw_ctxt) ;
161    EPHYR_LOG ("leave:%d\n", is_ok) ;
162    return is_ok ;
163}
164
165Bool
166ephyrDRIDestroyContext (int a_screen,
167                        int a_context_id)
168{
169    Display *dpy = hostx_get_display () ;
170    Bool is_ok=FALSE ;
171
172    EPHYR_LOG ("enter\n") ;
173    is_ok = XF86DRIDestroyContext (dpy, DefaultScreen (dpy), a_context_id) ;
174    EPHYR_LOG ("leave:%d\n", is_ok) ;
175    return is_ok ;
176}
177
178Bool
179ephyrDRICreateDrawable (int a_screen,
180                        int a_drawable,
181                        drm_drawable_t *a_hw_drawable)
182{
183    Bool is_ok=FALSE;
184    Display *dpy=hostx_get_display () ;
185
186    EPHYR_LOG ("enter\n") ;
187    is_ok = XF86DRICreateDrawable (dpy, DefaultScreen (dpy),
188                                   a_drawable, a_hw_drawable) ;
189    EPHYR_LOG ("leave. is_ok:%d\n", is_ok) ;
190    return is_ok ;
191}
192
193Bool
194ephyrDRIDestroyDrawable (int a_screen, int a_drawable)
195{
196    EPHYR_LOG ("enter\n") ;
197    EPHYR_LOG_ERROR ("not implemented yet\n") ;
198    EPHYR_LOG ("leave\n") ;
199    return FALSE ;
200}
201
202Bool
203ephyrDRIGetDrawableInfo (int a_screen,
204                         int a_drawable,
205                         unsigned int *a_index,
206                         unsigned int *a_stamp,
207                         int *a_x,
208                         int *a_y,
209                         int *a_w,
210                         int *a_h,
211                         int *a_num_clip_rects,
212                         drm_clip_rect_t **a_clip_rects,
213                         int *a_back_x,
214                         int *a_back_y,
215                         int *a_num_back_clip_rects,
216                         drm_clip_rect_t **a_back_clip_rects)
217{
218    Bool is_ok=FALSE;
219    Display *dpy=hostx_get_display ()  ;
220    EphyrHostWindowAttributes attrs ;
221
222    EPHYR_RETURN_VAL_IF_FAIL (a_x && a_y && a_w && a_h
223                              && a_num_clip_rects,
224                              FALSE) ;
225
226    EPHYR_LOG ("enter\n") ;
227    memset (&attrs, 0, sizeof (attrs)) ;
228    if (!hostx_get_window_attributes (a_drawable, &attrs)) {
229        EPHYR_LOG_ERROR ("failed to query host window attributes\n") ;
230        goto out;
231    }
232    if (!XF86DRIGetDrawableInfo (dpy, DefaultScreen (dpy), a_drawable,
233                                 a_index, a_stamp,
234                                 a_x, a_y,
235                                 a_w, a_h,
236                                 a_num_clip_rects, a_clip_rects,
237                                 a_back_x, a_back_y,
238                                 a_num_back_clip_rects,
239                                 a_back_clip_rects)) {
240        EPHYR_LOG_ERROR ("XF86DRIGetDrawableInfo ()\n") ;
241        goto out ;
242    }
243    EPHYR_LOG ("host x,y,w,h: (%d,%d,%d,%d)\n", *a_x, *a_y, *a_w, *a_h) ;
244    if (*a_num_clip_rects) {
245        free (*a_back_clip_rects) ;
246        *a_back_clip_rects = calloc (*a_num_clip_rects,
247                                     sizeof (drm_clip_rect_t)) ;
248        memmove (*a_back_clip_rects,
249                 *a_clip_rects,
250                 *a_num_clip_rects * sizeof (drm_clip_rect_t)) ;
251        *a_num_back_clip_rects = *a_num_clip_rects;
252    }
253    EPHYR_LOG ("num back clip rects:%d, num clip rects:%d\n",
254               *a_num_clip_rects, *a_num_back_clip_rects) ;
255    *a_back_x = *a_x ;
256    *a_back_y = *a_y ;
257    *a_w = attrs.width;
258    *a_h = attrs.height;
259
260    is_ok = TRUE ;
261out:
262    EPHYR_LOG ("leave. index:%d, stamp:%d, x,y:(%d,%d), w,y:(%d,%d)\n",
263               *a_index, *a_stamp, *a_x, *a_y, *a_w, *a_h) ;
264    return is_ok ;
265}
266
267Bool
268ephyrDRIGetDeviceInfo (int a_screen,
269                       drm_handle_t *a_frame_buffer,
270                       int *a_fb_origin,
271                       int *a_fb_size,
272                       int *a_fb_stride,
273                       int *a_dev_private_size,
274                       void **a_dev_private)
275{
276    Bool is_ok = FALSE ;
277    Display *dpy = hostx_get_display () ;
278
279    EPHYR_RETURN_VAL_IF_FAIL (dpy, FALSE) ;
280    EPHYR_LOG ("enter\n") ;
281    is_ok = XF86DRIGetDeviceInfo (dpy, DefaultScreen (dpy), a_frame_buffer,
282                                  a_fb_origin, a_fb_size, a_fb_stride,
283                                  a_dev_private_size, a_dev_private) ;
284    EPHYR_LOG ("leave:%d\n", is_ok) ;
285    return is_ok ;
286}
287