Mercurial > minori
comparison dep/animia/src/win/x11.cc @ 214:8d35061e7505
dep/animia: win/x11: simplify getting strings
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Sun, 07 Jan 2024 11:19:28 -0500 |
| parents | 58e81b42a0d6 |
| children | 8ccf0302afb1 |
comparison
equal
deleted
inserted
replaced
| 213:58e81b42a0d6 | 214:8d35061e7505 |
|---|---|
| 18 /* specify that these are X types. */ | 18 /* specify that these are X types. */ |
| 19 typedef ::Window XWindow; | 19 typedef ::Window XWindow; |
| 20 typedef ::Display XDisplay; | 20 typedef ::Display XDisplay; |
| 21 typedef ::Atom XAtom; | 21 typedef ::Atom XAtom; |
| 22 | 22 |
| 23 /* should return UTF8_STRING or STRING */ | 23 /* should return UTF8_STRING or STRING. this means we are not |
| 24 static bool GetWindowPropertyAsString(XDisplay* display, XWindow window, XAtom atom, std::string& result, XAtom reqtype = AnyPropertyType) { | 24 * *guaranteed* a UTF-8 string back. |
| 25 if (atom == None || reqtype == None) | 25 */ |
| 26 return false; | 26 static bool GetWindowPropertyAsString(XDisplay* display, XWindow window, XAtom atom, std::string& result) { |
| 27 if (atom == None) | |
| 28 return false; | |
| 29 | |
| 30 XAtom Atom_UTF8_STRING = ::XInternAtom(display, "UTF8_STRING", False); | |
| 27 | 31 |
| 28 int format; | 32 int format; |
| 29 unsigned long leftover_bytes, num_of_items; | 33 unsigned long leftover_bytes, num_of_items; |
| 30 XAtom type; | 34 XAtom type; |
| 31 unsigned char* data; | 35 unsigned char* data; |
| 32 | 36 |
| 33 int status = ::XGetWindowProperty(display, window, atom, 0L, (~0L), False, reqtype, | 37 int status = ::XGetWindowProperty(display, window, atom, 0L, (~0L), False, AnyPropertyType, |
| 34 &type, &format, &num_of_items, &leftover_bytes, &data); | 38 &type, &format, &num_of_items, &leftover_bytes, &data); |
| 35 if (status != Success || !(reqtype == AnyPropertyType || type == reqtype) || !num_of_items) | 39 if (status != Success || !(type == Atom_UTF8_STRING || type == XA_STRING) || !num_of_items) |
| 36 return false; | 40 return false; |
| 37 | 41 |
| 38 result = std::string((char*)data, num_of_items); | 42 result = std::string((char*)data, num_of_items); |
| 39 | 43 |
| 40 ::XFree(data); | 44 ::XFree(data); |
| 89 | 93 |
| 90 return true; | 94 return true; |
| 91 } | 95 } |
| 92 | 96 |
| 93 static bool FetchName(XDisplay* display, XWindow window, std::string& result) { | 97 static bool FetchName(XDisplay* display, XWindow window, std::string& result) { |
| 94 /* TODO: Check if XInternAtom created None or not... */ | 98 if (GetWindowPropertyAsString(display, window, ::XInternAtom(display, "_NET_WM_NAME", True), result)) |
| 95 if (GetWindowPropertyAsString(display, window, ::XInternAtom(display, "_NET_WM_NAME", True), | |
| 96 result, ::XInternAtom(display, "UTF8_STRING", True))) | |
| 97 return true; | 99 return true; |
| 98 | 100 |
| 99 if (GetWindowPropertyAsString(display, window, ::XInternAtom(display, "WM_NAME", True), | 101 if (GetWindowPropertyAsString(display, window, ::XInternAtom(display, "WM_NAME", True), result)) |
| 100 result, XA_STRING)) | |
| 101 return true; | 102 return true; |
| 102 | 103 |
| 103 /* Fallback to XGetWMName() */ | 104 /* Fallback to XGetWMName() */ |
| 104 XTextProperty text; | 105 XTextProperty text; |
| 105 | 106 |
| 145 &format, &num_of_items, &bytes_after, &data | 146 &format, &num_of_items, &bytes_after, &data |
| 146 ); | 147 ); |
| 147 | 148 |
| 148 if (status < Success || !num_of_items) | 149 if (status < Success || !num_of_items) |
| 149 return false; | 150 return false; |
| 150 | |
| 151 if (format != 32) { | |
| 152 ::XFree(data); | |
| 153 return false; | |
| 154 } | |
| 155 } | 151 } |
| 156 | 152 |
| 157 XWindow* arr = (XWindow*)data; | 153 XWindow* arr = (XWindow*)data; |
| 158 | 154 |
| 159 for (uint32_t i = 0; i < num_of_items; i++) | 155 for (uint32_t i = 0; i < num_of_items; i++) |
