Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(205)

Side by Side Diff: ui/base/x/x11_util.cc

Issue 989993002: x11: Use scoped_ptr<> for X11 objects where it makes sense. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/base/x/x11_util.h ('k') | ui/display/chromeos/x11/native_display_delegate_x11.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This file defines utility functions for X11 (Linux only). This code has been 5 // This file defines utility functions for X11 (Linux only). This code has been
6 // ported from XCB since we can't use XCB on Ubuntu while its 32-bit support 6 // ported from XCB since we can't use XCB on Ubuntu while its 32-bit support
7 // remains woefully incomplete. 7 // remains woefully incomplete.
8 8
9 #include "ui/base/x/x11_util.h" 9 #include "ui/base/x/x11_util.h"
10 10
(...skipping 687 matching lines...) Expand 10 before | Expand all | Expand 10 after
698 // of the client input region with both the default input region and the 698 // of the client input region with both the default input region and the
699 // client bounding region. Any portion of the client input region that is not 699 // client bounding region. Any portion of the client input region that is not
700 // included in both the default input region and the client bounding region 700 // included in both the default input region and the client bounding region
701 // will not be included in the effective input region on the screen. 701 // will not be included in the effective input region on the screen.
702 int rectangle_kind[] = {ShapeInput, ShapeBounding}; 702 int rectangle_kind[] = {ShapeInput, ShapeBounding};
703 for (size_t kind_index = 0; 703 for (size_t kind_index = 0;
704 kind_index < arraysize(rectangle_kind); 704 kind_index < arraysize(rectangle_kind);
705 kind_index++) { 705 kind_index++) {
706 int dummy; 706 int dummy;
707 int shape_rects_size = 0; 707 int shape_rects_size = 0;
708 XRectangle* shape_rects = XShapeGetRectangles(gfx::GetXDisplay(), 708 gfx::XScopedPtr<XRectangle[]> shape_rects(XShapeGetRectangles(
709 window, 709 gfx::GetXDisplay(), window, rectangle_kind[kind_index],
710 rectangle_kind[kind_index], 710 &shape_rects_size, &dummy));
711 &shape_rects_size,
712 &dummy);
713 if (!shape_rects) { 711 if (!shape_rects) {
714 // The shape is empty. This can occur when |window| is minimized. 712 // The shape is empty. This can occur when |window| is minimized.
715 DCHECK_EQ(0, shape_rects_size); 713 DCHECK_EQ(0, shape_rects_size);
716 return false; 714 return false;
717 } 715 }
718 bool is_in_shape_rects = false; 716 bool is_in_shape_rects = false;
719 for (int i = 0; i < shape_rects_size; ++i) { 717 for (int i = 0; i < shape_rects_size; ++i) {
720 // The ShapeInput and ShapeBounding rects are to be in window space, so we 718 // The ShapeInput and ShapeBounding rects are to be in window space, so we
721 // have to translate by the window_rect's offset to map to screen space. 719 // have to translate by the window_rect's offset to map to screen space.
720 const XRectangle& rect = shape_rects[i];
722 gfx::Rect shape_rect = 721 gfx::Rect shape_rect =
723 gfx::Rect(shape_rects[i].x + window_rect.x(), 722 gfx::Rect(rect.x + window_rect.x(), rect.y + window_rect.y(),
724 shape_rects[i].y + window_rect.y(), 723 rect.width, rect.height);
725 shape_rects[i].width, shape_rects[i].height);
726 if (shape_rect.Contains(screen_loc)) { 724 if (shape_rect.Contains(screen_loc)) {
727 is_in_shape_rects = true; 725 is_in_shape_rects = true;
728 break; 726 break;
729 } 727 }
730 } 728 }
731 XFree(shape_rects);
732 if (!is_in_shape_rects) 729 if (!is_in_shape_rects)
733 return false; 730 return false;
734 } 731 }
735 return true; 732 return true;
736 } 733 }
737 734
738 735
739 bool PropertyExists(XID window, const std::string& property_name) { 736 bool PropertyExists(XID window, const std::string& property_name) {
740 XAtom type = None; 737 XAtom type = None;
741 int format = 0; // size in bits of each item in 'property' 738 int format = 0; // size in bits of each item in 'property'
742 unsigned long num_items = 0; 739 unsigned long num_items = 0;
743 unsigned char* property = NULL; 740 unsigned char* property = NULL;
744 741
745 int result = GetProperty(window, property_name, 1, 742 int result = GetProperty(window, property_name, 1,
746 &type, &format, &num_items, &property); 743 &type, &format, &num_items, &property);
744 gfx::XScopedPtr<unsigned char> scoped_property(property);
747 if (result != Success) 745 if (result != Success)
748 return false; 746 return false;
749 747
750 XFree(property);
751 return num_items > 0; 748 return num_items > 0;
752 } 749 }
753 750
754 bool GetRawBytesOfProperty(XID window, 751 bool GetRawBytesOfProperty(XID window,
755 XAtom property, 752 XAtom property,
756 scoped_refptr<base::RefCountedMemory>* out_data, 753 scoped_refptr<base::RefCountedMemory>* out_data,
757 size_t* out_data_items, 754 size_t* out_data_items,
758 XAtom* out_type) { 755 XAtom* out_type) {
759 // Retrieve the data from our window. 756 // Retrieve the data from our window.
760 unsigned long nitems = 0; 757 unsigned long nitems = 0;
761 unsigned long nbytes = 0; 758 unsigned long nbytes = 0;
762 XAtom prop_type = None; 759 XAtom prop_type = None;
763 int prop_format = 0; 760 int prop_format = 0;
764 unsigned char* property_data = NULL; 761 unsigned char* property_data = NULL;
765 if (XGetWindowProperty(gfx::GetXDisplay(), window, property, 762 if (XGetWindowProperty(gfx::GetXDisplay(), window, property,
766 0, 0x1FFFFFFF /* MAXINT32 / 4 */, False, 763 0, 0x1FFFFFFF /* MAXINT32 / 4 */, False,
767 AnyPropertyType, &prop_type, &prop_format, 764 AnyPropertyType, &prop_type, &prop_format,
768 &nitems, &nbytes, &property_data) != Success) { 765 &nitems, &nbytes, &property_data) != Success) {
769 return false; 766 return false;
770 } 767 }
768 gfx::XScopedPtr<unsigned char> scoped_property(property_data);
771 769
772 if (prop_type == None) 770 if (prop_type == None)
773 return false; 771 return false;
774 772
775 size_t bytes = 0; 773 size_t bytes = 0;
776 // So even though we should theoretically have nbytes (and we can't 774 // So even though we should theoretically have nbytes (and we can't
777 // pass NULL there), we need to manually calculate the byte length here 775 // pass NULL there), we need to manually calculate the byte length here
778 // because nbytes always returns zero. 776 // because nbytes always returns zero.
779 switch (prop_format) { 777 switch (prop_format) {
780 case 8: 778 case 8:
781 bytes = nitems; 779 bytes = nitems;
782 break; 780 break;
783 case 16: 781 case 16:
784 bytes = sizeof(short) * nitems; 782 bytes = sizeof(short) * nitems;
785 break; 783 break;
786 case 32: 784 case 32:
787 bytes = sizeof(long) * nitems; 785 bytes = sizeof(long) * nitems;
788 break; 786 break;
789 default: 787 default:
790 NOTREACHED(); 788 NOTREACHED();
791 break; 789 break;
792 } 790 }
793 791
794 if (out_data) 792 if (out_data)
795 *out_data = new XRefcountedMemory(property_data, bytes); 793 *out_data = new XRefcountedMemory(scoped_property.release(), bytes);
796 else
797 XFree(property_data);
798 794
799 if (out_data_items) 795 if (out_data_items)
800 *out_data_items = nitems; 796 *out_data_items = nitems;
801 797
802 if (out_type) 798 if (out_type)
803 *out_type = prop_type; 799 *out_type = prop_type;
804 800
805 return true; 801 return true;
806 } 802 }
807 803
808 bool GetIntProperty(XID window, const std::string& property_name, int* value) { 804 bool GetIntProperty(XID window, const std::string& property_name, int* value) {
809 XAtom type = None; 805 XAtom type = None;
810 int format = 0; // size in bits of each item in 'property' 806 int format = 0; // size in bits of each item in 'property'
811 unsigned long num_items = 0; 807 unsigned long num_items = 0;
812 unsigned char* property = NULL; 808 unsigned char* property = NULL;
813 809
814 int result = GetProperty(window, property_name, 1, 810 int result = GetProperty(window, property_name, 1,
815 &type, &format, &num_items, &property); 811 &type, &format, &num_items, &property);
812 gfx::XScopedPtr<unsigned char> scoped_property(property);
816 if (result != Success) 813 if (result != Success)
817 return false; 814 return false;
818 815
819 if (format != 32 || num_items != 1) { 816 if (format != 32 || num_items != 1)
820 XFree(property);
821 return false; 817 return false;
822 }
823 818
824 *value = static_cast<int>(*(reinterpret_cast<long*>(property))); 819 *value = static_cast<int>(*(reinterpret_cast<long*>(property)));
825 XFree(property);
826 return true; 820 return true;
827 } 821 }
828 822
829 bool GetXIDProperty(XID window, const std::string& property_name, XID* value) { 823 bool GetXIDProperty(XID window, const std::string& property_name, XID* value) {
830 XAtom type = None; 824 XAtom type = None;
831 int format = 0; // size in bits of each item in 'property' 825 int format = 0; // size in bits of each item in 'property'
832 unsigned long num_items = 0; 826 unsigned long num_items = 0;
833 unsigned char* property = NULL; 827 unsigned char* property = NULL;
834 828
835 int result = GetProperty(window, property_name, 1, 829 int result = GetProperty(window, property_name, 1,
836 &type, &format, &num_items, &property); 830 &type, &format, &num_items, &property);
831 gfx::XScopedPtr<unsigned char> scoped_property(property);
837 if (result != Success) 832 if (result != Success)
838 return false; 833 return false;
839 834
840 if (format != 32 || num_items != 1) { 835 if (format != 32 || num_items != 1)
841 XFree(property);
842 return false; 836 return false;
843 }
844 837
845 *value = *(reinterpret_cast<XID*>(property)); 838 *value = *(reinterpret_cast<XID*>(property));
846 XFree(property);
847 return true; 839 return true;
848 } 840 }
849 841
850 bool GetIntArrayProperty(XID window, 842 bool GetIntArrayProperty(XID window,
851 const std::string& property_name, 843 const std::string& property_name,
852 std::vector<int>* value) { 844 std::vector<int>* value) {
853 XAtom type = None; 845 XAtom type = None;
854 int format = 0; // size in bits of each item in 'property' 846 int format = 0; // size in bits of each item in 'property'
855 unsigned long num_items = 0; 847 unsigned long num_items = 0;
856 unsigned char* properties = NULL; 848 unsigned char* properties = NULL;
857 849
858 int result = GetProperty(window, property_name, 850 int result = GetProperty(window, property_name,
859 (~0L), // (all of them) 851 (~0L), // (all of them)
860 &type, &format, &num_items, &properties); 852 &type, &format, &num_items, &properties);
853 gfx::XScopedPtr<unsigned char> scoped_properties(properties);
861 if (result != Success) 854 if (result != Success)
862 return false; 855 return false;
863 856
864 if (format != 32) { 857 if (format != 32)
865 XFree(properties);
866 return false; 858 return false;
867 }
868 859
869 long* int_properties = reinterpret_cast<long*>(properties); 860 long* int_properties = reinterpret_cast<long*>(properties);
870 value->clear(); 861 value->clear();
871 for (unsigned long i = 0; i < num_items; ++i) { 862 for (unsigned long i = 0; i < num_items; ++i) {
872 value->push_back(static_cast<int>(int_properties[i])); 863 value->push_back(static_cast<int>(int_properties[i]));
873 } 864 }
874 XFree(properties);
875 return true; 865 return true;
876 } 866 }
877 867
878 bool GetAtomArrayProperty(XID window, 868 bool GetAtomArrayProperty(XID window,
879 const std::string& property_name, 869 const std::string& property_name,
880 std::vector<XAtom>* value) { 870 std::vector<XAtom>* value) {
881 XAtom type = None; 871 XAtom type = None;
882 int format = 0; // size in bits of each item in 'property' 872 int format = 0; // size in bits of each item in 'property'
883 unsigned long num_items = 0; 873 unsigned long num_items = 0;
884 unsigned char* properties = NULL; 874 unsigned char* properties = NULL;
885 875
886 int result = GetProperty(window, property_name, 876 int result = GetProperty(window, property_name,
887 (~0L), // (all of them) 877 (~0L), // (all of them)
888 &type, &format, &num_items, &properties); 878 &type, &format, &num_items, &properties);
879 gfx::XScopedPtr<unsigned char> scoped_properties(properties);
889 if (result != Success) 880 if (result != Success)
890 return false; 881 return false;
891 882
892 if (type != XA_ATOM) { 883 if (type != XA_ATOM)
893 XFree(properties);
894 return false; 884 return false;
895 }
896 885
897 XAtom* atom_properties = reinterpret_cast<XAtom*>(properties); 886 XAtom* atom_properties = reinterpret_cast<XAtom*>(properties);
898 value->clear(); 887 value->clear();
899 value->insert(value->begin(), atom_properties, atom_properties + num_items); 888 value->insert(value->begin(), atom_properties, atom_properties + num_items);
900 XFree(properties);
901 return true; 889 return true;
902 } 890 }
903 891
904 bool GetStringProperty( 892 bool GetStringProperty(
905 XID window, const std::string& property_name, std::string* value) { 893 XID window, const std::string& property_name, std::string* value) {
906 XAtom type = None; 894 XAtom type = None;
907 int format = 0; // size in bits of each item in 'property' 895 int format = 0; // size in bits of each item in 'property'
908 unsigned long num_items = 0; 896 unsigned long num_items = 0;
909 unsigned char* property = NULL; 897 unsigned char* property = NULL;
910 898
911 int result = GetProperty(window, property_name, 1024, 899 int result = GetProperty(window, property_name, 1024,
912 &type, &format, &num_items, &property); 900 &type, &format, &num_items, &property);
901 gfx::XScopedPtr<unsigned char> scoped_property(property);
913 if (result != Success) 902 if (result != Success)
914 return false; 903 return false;
915 904
916 if (format != 8) { 905 if (format != 8)
917 XFree(property);
918 return false; 906 return false;
919 }
920 907
921 value->assign(reinterpret_cast<char*>(property), num_items); 908 value->assign(reinterpret_cast<char*>(property), num_items);
922 XFree(property);
923 return true; 909 return true;
924 } 910 }
925 911
926 bool SetIntProperty(XID window, 912 bool SetIntProperty(XID window,
927 const std::string& name, 913 const std::string& name,
928 const std::string& type, 914 const std::string& type,
929 int value) { 915 int value) {
930 std::vector<int> values(1, value); 916 std::vector<int> values(1, value);
931 return SetIntArrayProperty(window, name, type, values); 917 return SetIntArrayProperty(window, name, type, values);
932 } 918 }
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
1171 unsigned char *data = NULL; 1157 unsigned char *data = NULL;
1172 if (GetProperty(window, 1158 if (GetProperty(window,
1173 "_NET_CLIENT_LIST_STACKING", 1159 "_NET_CLIENT_LIST_STACKING",
1174 ~0L, 1160 ~0L,
1175 &type, 1161 &type,
1176 &format, 1162 &format,
1177 &count, 1163 &count,
1178 &data) != Success) { 1164 &data) != Success) {
1179 return false; 1165 return false;
1180 } 1166 }
1167 gfx::XScopedPtr<unsigned char> scoped_data(data);
1181 1168
1182 bool result = false; 1169 bool result = false;
1183 if (type == XA_WINDOW && format == 32 && data && count > 0) { 1170 if (type == XA_WINDOW && format == 32 && data && count > 0) {
1184 result = true; 1171 result = true;
1185 XID* stack = reinterpret_cast<XID*>(data); 1172 XID* stack = reinterpret_cast<XID*>(data);
1186 for (long i = static_cast<long>(count) - 1; i >= 0; i--) 1173 for (long i = static_cast<long>(count) - 1; i >= 0; i--)
1187 windows->push_back(stack[i]); 1174 windows->push_back(stack[i]);
1188 } 1175 }
1189 1176
1190 if (data)
1191 XFree(data);
1192
1193 return result; 1177 return result;
1194 } 1178 }
1195 1179
1196 bool CopyAreaToCanvas(XID drawable, 1180 bool CopyAreaToCanvas(XID drawable,
1197 gfx::Rect source_bounds, 1181 gfx::Rect source_bounds,
1198 gfx::Point dest_offset, 1182 gfx::Point dest_offset,
1199 gfx::Canvas* canvas) { 1183 gfx::Canvas* canvas) {
1200 ui::XScopedImage scoped_image( 1184 ui::XScopedImage scoped_image(
1201 XGetImage(gfx::GetXDisplay(), drawable, 1185 XGetImage(gfx::GetXDisplay(), drawable,
1202 source_bounds.x(), source_bounds.y(), 1186 source_bounds.x(), source_bounds.y(),
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
1343 if (!GetAtomArrayProperty(GetX11RootWindow(), 1327 if (!GetAtomArrayProperty(GetX11RootWindow(),
1344 "_NET_SUPPORTED", 1328 "_NET_SUPPORTED",
1345 &supported_atoms)) { 1329 &supported_atoms)) {
1346 return false; 1330 return false;
1347 } 1331 }
1348 1332
1349 return std::find(supported_atoms.begin(), supported_atoms.end(), atom) != 1333 return std::find(supported_atoms.begin(), supported_atoms.end(), atom) !=
1350 supported_atoms.end(); 1334 supported_atoms.end();
1351 } 1335 }
1352 1336
1337 XRefcountedMemory::XRefcountedMemory(unsigned char* x11_data, size_t length)
1338 : x11_data_(length ? x11_data : nullptr), length_(length) {
1339 }
1340
1353 const unsigned char* XRefcountedMemory::front() const { 1341 const unsigned char* XRefcountedMemory::front() const {
1354 return x11_data_; 1342 return x11_data_.get();
1355 } 1343 }
1356 1344
1357 size_t XRefcountedMemory::size() const { 1345 size_t XRefcountedMemory::size() const {
1358 return length_; 1346 return length_;
1359 } 1347 }
1360 1348
1361 XRefcountedMemory::~XRefcountedMemory() { 1349 XRefcountedMemory::~XRefcountedMemory() {
1362 XFree(x11_data_);
1363 }
1364
1365 XScopedString::~XScopedString() {
1366 XFree(string_);
1367 } 1350 }
1368 1351
1369 XScopedImage::~XScopedImage() { 1352 XScopedImage::~XScopedImage() {
1370 reset(NULL); 1353 reset(NULL);
1371 } 1354 }
1372 1355
1373 void XScopedImage::reset(XImage* image) { 1356 void XScopedImage::reset(XImage* image) {
1374 if (image_ == image) 1357 if (image_ == image)
1375 return; 1358 return;
1376 if (image_) 1359 if (image_)
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
1463 XGetErrorText(dpy, error_event.error_code, error_str, sizeof(error_str)); 1446 XGetErrorText(dpy, error_event.error_code, error_str, sizeof(error_str));
1464 1447
1465 strncpy(request_str, "Unknown", sizeof(request_str)); 1448 strncpy(request_str, "Unknown", sizeof(request_str));
1466 if (error_event.request_code < 128) { 1449 if (error_event.request_code < 128) {
1467 std::string num = base::UintToString(error_event.request_code); 1450 std::string num = base::UintToString(error_event.request_code);
1468 XGetErrorDatabaseText( 1451 XGetErrorDatabaseText(
1469 dpy, "XRequest", num.c_str(), "Unknown", request_str, 1452 dpy, "XRequest", num.c_str(), "Unknown", request_str,
1470 sizeof(request_str)); 1453 sizeof(request_str));
1471 } else { 1454 } else {
1472 int num_ext; 1455 int num_ext;
1473 char** ext_list = XListExtensions(dpy, &num_ext); 1456 gfx::XScopedPtr<char* [],
1457 gfx::XObjectDeleter<char*, int, XFreeExtensionList>>
1458 ext_list(XListExtensions(dpy, &num_ext));
1474 1459
1475 for (int i = 0; i < num_ext; i++) { 1460 for (int i = 0; i < num_ext; i++) {
1476 int ext_code, first_event, first_error; 1461 int ext_code, first_event, first_error;
1477 XQueryExtension(dpy, ext_list[i], &ext_code, &first_event, &first_error); 1462 XQueryExtension(dpy, ext_list[i], &ext_code, &first_event, &first_error);
1478 if (error_event.request_code == ext_code) { 1463 if (error_event.request_code == ext_code) {
1479 std::string msg = base::StringPrintf( 1464 std::string msg = base::StringPrintf(
1480 "%s.%d", ext_list[i], error_event.minor_code); 1465 "%s.%d", ext_list[i], error_event.minor_code);
1481 XGetErrorDatabaseText( 1466 XGetErrorDatabaseText(
1482 dpy, "XRequest", msg.c_str(), "Unknown", request_str, 1467 dpy, "XRequest", msg.c_str(), "Unknown", request_str,
1483 sizeof(request_str)); 1468 sizeof(request_str));
1484 break; 1469 break;
1485 } 1470 }
1486 } 1471 }
1487 XFreeExtensionList(ext_list);
1488 } 1472 }
1489 1473
1490 LOG(WARNING) 1474 LOG(WARNING)
1491 << "X error received: " 1475 << "X error received: "
1492 << "serial " << error_event.serial << ", " 1476 << "serial " << error_event.serial << ", "
1493 << "error_code " << static_cast<int>(error_event.error_code) 1477 << "error_code " << static_cast<int>(error_event.error_code)
1494 << " (" << error_str << "), " 1478 << " (" << error_str << "), "
1495 << "request_code " << static_cast<int>(error_event.request_code) << ", " 1479 << "request_code " << static_cast<int>(error_event.request_code) << ", "
1496 << "minor_code " << static_cast<int>(error_event.minor_code) 1480 << "minor_code " << static_cast<int>(error_event.minor_code)
1497 << " (" << request_str << ")"; 1481 << " (" << request_str << ")";
1498 } 1482 }
1499 1483
1500 // ---------------------------------------------------------------------------- 1484 // ----------------------------------------------------------------------------
1501 // End of x11_util_internal.h 1485 // End of x11_util_internal.h
1502 1486
1503 1487
1504 } // namespace ui 1488 } // namespace ui
OLDNEW
« no previous file with comments | « ui/base/x/x11_util.h ('k') | ui/display/chromeos/x11/native_display_delegate_x11.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698