Merge changes I83c26470,I6fe45a0f into devel/master
[platform/core/uifw/dali-adaptor.git] / dali / internal / window-system / ubuntu-x11 / window-interface-ecore-x.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/window-system/ubuntu-x11/window-interface-ecore-x.h>
20
21 // EXTERNAL INCLUDES
22 #include <X11/Xatom.h>
23 #include <X11/Xlib.h>
24 #include <X11/Xutil.h>
25
26 #include <dali/integration-api/debug.h>
27
28 // INTERNAL INCLUDES
29 #include <dali/internal/window-system/ubuntu-x11/ecore-x-types.h>
30
31 namespace Dali
32 {
33 namespace Internal
34 {
35 namespace Adaptor
36 {
37 namespace ECore
38 {
39 namespace WindowInterface
40 {
41 // CONSTANTS
42 const char* const CBHM_WINDOW = "CBHM_XWIN";
43
44 Ecore_X_Window GetWindow()
45 {
46   Ecore_X_Atom xAtomCbhm = ecore_x_atom_get(CBHM_WINDOW);
47
48   Ecore_X_Window xCbhmWin = 0;
49   unsigned char* buf      = NULL;
50   int            num      = 0;
51 // XA_WINDOW is a macro with C cast
52 #pragma GCC diagnostic push
53 #pragma GCC diagnostic ignored "-Wold-style-cast"
54   int ret = ecore_x_window_prop_property_get(0, xAtomCbhm, XA_WINDOW, 0, &buf, &num);
55 #pragma GCC diagnostic pop
56
57   if(ret && num)
58   {
59     memcpy(&xCbhmWin, buf, sizeof(Ecore_X_Window));
60   }
61   if(buf)
62   {
63     free(buf);
64   }
65
66   return xCbhmWin;
67 }
68
69 std::string GetWindowProperty(Ecore_X_Atom property, Ecore_X_Atom* xDataType, unsigned int num)
70 {
71   std::string data("");
72
73   if(!property)
74   {
75     return data;
76   }
77
78   ecore_x_sync();
79
80   long unsigned int numRet = 0, bytes = 0;
81   int               ret = 0, sizeRet;
82   unsigned int      i;
83   unsigned char*    propRet;
84   Ecore_X_Atom      typeRet;
85
86   // X11 Function to get window property
87   ret = XGetWindowProperty(static_cast<Display*>(ecore_x_display_get()), // Display* X Server Connection
88                            GetWindow(),                                  // Window  window in question
89                            property,                                     // Atom  name of property
90                            num,                                          // long offset where required data is stored
91                            LONG_MAX,                                     // long length of data to retrieve
92                            False,                                        // Bool flag to delete data
93                            ecore_x_window_prop_any_type(),               // Atom atom id associated to property type
94                            reinterpret_cast<Atom*>(&typeRet),            // Atom actual_type_return, atom id property type
95                            &sizeRet,                                     // int* format of property
96                            &numRet,                                      // unsigned long*  number of items being returned in prop_return
97                            &bytes,                                       // unsigned long* remaining bytes if partial retrieval
98                            &propRet);                                    // unsigned char** return data
99   if(ret != Success)
100   {
101     return data;
102   }
103
104   if(!numRet)
105   {
106     XFree(propRet);
107     return data;
108   }
109
110   numRet--; // As propRet in XGetWindowProperty gets an extra 0 added for compatibility reasons.
111
112   switch(sizeRet) // Format returned by XGetWindowProperty int, short, long
113   {
114     case 8:
115     {
116       for(i = 0; i < numRet; i++)
117       {
118         data += propRet[i];
119       }
120     }
121     break;
122
123     case 16:
124     {
125       for(i = 0; i < numRet; i++)
126       {
127         data += (propRet)[i];
128       }
129     }
130     break;
131
132     case 32:
133     {
134       for(i = 0; i < numRet; i++)
135       {
136         data += (propRet)[i];
137       }
138     }
139     break;
140   }
141
142   XFree(propRet);
143
144   if(xDataType)
145   {
146     *xDataType = typeRet;
147   }
148
149   return data;
150 }
151
152 void SendXEvent(Ecore_X_Display* display, Ecore_X_Window window, bool propagate, long int eventMask, Ecore_X_Atom messageType, int messageFormat, const char* msg)
153 {
154   XClientMessageEvent message;
155   memset(&message, 0, sizeof(message));
156   message.type         = ClientMessage;
157   message.display      = static_cast<Display*>(display);
158   message.message_type = messageType;
159   message.format       = messageFormat;
160   message.window       = window;
161   snprintf(message.data.b, 20, "%s", msg);
162
163   XSendEvent(static_cast<Display*>(display), window, propagate, eventMask, reinterpret_cast<XEvent*>(&message));
164 }
165
166 } // namespace WindowInterface
167
168 } // namespace ECore
169
170 } // namespace Adaptor
171
172 } // namespace Internal
173
174 } // namespace Dali