Add function get tbm_bo_flag
[platform/core/uifw/libtbm.git] / src / tbm_x11.c
1 /**************************************************************************
2
3 libtbm
4
5 Copyright 2012 Samsung Electronics co., Ltd. All Rights Reserved.
6
7 Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
8 Boram Park <boram1288.park@samsung.com>, Changyeon Lee <cyeon.lee@samsung.com>
9
10 Permission is hereby granted, free of charge, to any person obtaining a
11 copy of this software and associated documentation files (the
12 "Software"), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sub license, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
17
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial portions
20 of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
25 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 **************************************************************************/
31
32 #include "config.h"
33
34 #include "tbm_bufmgr.h"
35 #include "tbm_bufmgr_int.h"
36 #include <xf86drm.h>
37 #include <X11/Xmd.h>
38 #include <dri2.h>
39
40 int
41 tbm_bufmgr_get_drm_fd_x11()
42 {
43     int screen;
44     Display *display;
45     int dri2Major, dri2Minor;
46     int eventBase, errorBase;
47     drm_magic_t magic;
48     char *driver_name, *device_name;
49     int fd;
50
51     display = XOpenDisplay(NULL);
52     if (!display)
53     {
54         TBM_LOG ("[libtbm:%d] Fail XOpenDisplay\n", getpid());
55         return -1;
56     }
57
58     screen = DefaultScreen(display);
59
60     if (!DRI2QueryExtension (display, &eventBase, &errorBase))
61     {
62         TBM_LOG ("[libtbm:%d] Fail DRI2QueryExtention\n", getpid());
63         XCloseDisplay(display);
64         return -1;
65     }
66
67     if (!DRI2QueryVersion (display, &dri2Major, &dri2Minor))
68     {
69         TBM_LOG ("[libtbm:%d] Fail DRI2QueryVersion\n", getpid());
70         XCloseDisplay(display);
71         return -1;
72     }
73
74     if (!DRI2Connect (display, RootWindow(display, screen), &driver_name, &device_name))
75     {
76         TBM_LOG ("[libtbm:%d] Fail DRI2Connect\n", getpid());
77         XCloseDisplay(display);
78         return -1;
79     }
80
81     fd = open (device_name, O_RDWR);
82     if (fd < 0)
83     {
84         TBM_LOG ("[libtbm:%d] cannot open drm device (%s)\n", getpid(), device_name);
85         free (driver_name);
86         free (device_name);
87         XCloseDisplay(display);
88         return -1;
89     }
90
91     if (drmGetMagic (fd, &magic))
92     {
93         TBM_LOG ("[libtbm:%d] Fail drmGetMagic\n", getpid());
94         free (driver_name);
95         free (device_name);
96         close(fd);
97         XCloseDisplay(display);
98         return -1;
99     }
100
101     if (!DRI2Authenticate(display, RootWindow(display, screen), magic))
102     {
103         TBM_LOG ("[libtbm:%d] Fail DRI2Authenticate\n", getpid());
104         free (driver_name);
105         free (device_name);
106         close(fd);
107         XCloseDisplay(display);
108         return -1;
109     }
110
111     if(!drmAuthMagic(fd, magic))
112     {
113         TBM_LOG ("[libtbm:%d] Fail drmAuthMagic\n", getpid());
114         free (driver_name);
115         free (device_name);
116         close(fd);
117         XCloseDisplay(display);
118         return -1;
119     }
120
121     free (driver_name);
122     free (device_name);
123     XCloseDisplay(display);
124
125     return fd;
126 }
127