OLD | NEW |
---|---|
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 743 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
754 | 754 |
755 int result = GetProperty(window, property_name, 1, | 755 int result = GetProperty(window, property_name, 1, |
756 &type, &format, &num_items, &property); | 756 &type, &format, &num_items, &property); |
757 if (result != Success) | 757 if (result != Success) |
758 return false; | 758 return false; |
759 | 759 |
760 XFree(property); | 760 XFree(property); |
761 return num_items > 0; | 761 return num_items > 0; |
762 } | 762 } |
763 | 763 |
764 bool GetRawBytesOfProperty(XID window, | |
765 Atom property, | |
766 unsigned char** out_data, | |
767 size_t* out_data_bytes, | |
768 size_t* out_data_items, | |
769 Atom* out_type) { | |
770 // Retrieve the data from our window. | |
771 unsigned long nitems = 0; | |
772 unsigned long nbytes = 0; | |
773 Atom prop_type = None; | |
774 int prop_format = 0; | |
775 unsigned char* property_data = NULL; | |
776 if (XGetWindowProperty(GetXDisplay(), | |
777 window, | |
Daniel Erat
2013/06/17 22:09:57
nit: move window and property to the end of the fi
| |
778 property, | |
779 0, 0x1FFFFFFF /* MAXINT32 / 4 */, False, | |
780 AnyPropertyType, &prop_type, &prop_format, | |
781 &nitems, &nbytes, &property_data) != Success) { | |
782 return false; | |
783 } | |
784 | |
785 if (prop_type == None) | |
786 return false; | |
787 | |
788 if (out_data) | |
789 *out_data = property_data; | |
790 | |
791 if (out_data_bytes) { | |
792 // So even though we should theoretically have nbytes (and we can't | |
793 // pass NULL there), we need to manually calculate the byte length here | |
794 // because nbytes always returns zero. | |
795 switch (prop_format) { | |
796 case 8: | |
797 *out_data_bytes = nitems; | |
798 break; | |
799 case 16: | |
800 *out_data_bytes = sizeof(short) * nitems; | |
801 break; | |
802 case 32: | |
803 *out_data_bytes = sizeof(long) * nitems; | |
804 break; | |
805 default: | |
806 NOTREACHED(); | |
807 break; | |
808 } | |
809 } | |
810 | |
811 if (out_data_items) | |
812 *out_data_items = nitems; | |
813 | |
814 if (out_type) | |
815 *out_type = prop_type; | |
816 | |
817 return true; | |
818 } | |
819 | |
764 bool GetIntProperty(XID window, const std::string& property_name, int* value) { | 820 bool GetIntProperty(XID window, const std::string& property_name, int* value) { |
765 Atom type = None; | 821 Atom type = None; |
766 int format = 0; // size in bits of each item in 'property' | 822 int format = 0; // size in bits of each item in 'property' |
767 unsigned long num_items = 0; | 823 unsigned long num_items = 0; |
768 unsigned char* property = NULL; | 824 unsigned char* property = NULL; |
769 | 825 |
770 int result = GetProperty(window, property_name, 1, | 826 int result = GetProperty(window, property_name, 1, |
771 &type, &format, &num_items, &property); | 827 &type, &format, &num_items, &property); |
772 if (result != Success) | 828 if (result != Success) |
773 return false; | 829 return false; |
774 | 830 |
775 if (format != 32 || num_items != 1) { | 831 if (format != 32 || num_items != 1) { |
776 XFree(property); | 832 XFree(property); |
777 return false; | 833 return false; |
778 } | 834 } |
779 | 835 |
780 *value = static_cast<int>(*(reinterpret_cast<long*>(property))); | 836 *value = static_cast<int>(*(reinterpret_cast<long*>(property))); |
781 XFree(property); | 837 XFree(property); |
782 return true; | 838 return true; |
783 } | 839 } |
784 | 840 |
841 bool GetXIDProperty(XID window, const std::string& property_name, XID* value) { | |
842 Atom type = None; | |
843 int format = 0; // size in bits of each item in 'property' | |
844 unsigned long num_items = 0; | |
845 unsigned char* property = NULL; | |
846 | |
847 int result = GetProperty(window, property_name, 1, | |
848 &type, &format, &num_items, &property); | |
849 if (result != Success) | |
850 return false; | |
851 | |
852 if (format != 32 || num_items != 1) { | |
853 XFree(property); | |
854 return false; | |
855 } | |
856 | |
857 *value = *(reinterpret_cast<XID*>(property)); | |
858 XFree(property); | |
859 return true; | |
860 } | |
861 | |
785 bool GetIntArrayProperty(XID window, | 862 bool GetIntArrayProperty(XID window, |
786 const std::string& property_name, | 863 const std::string& property_name, |
787 std::vector<int>* value) { | 864 std::vector<int>* value) { |
788 Atom type = None; | 865 Atom type = None; |
789 int format = 0; // size in bits of each item in 'property' | 866 int format = 0; // size in bits of each item in 'property' |
790 unsigned long num_items = 0; | 867 unsigned long num_items = 0; |
791 unsigned char* properties = NULL; | 868 unsigned char* properties = NULL; |
792 | 869 |
793 int result = GetProperty(window, property_name, | 870 int result = GetProperty(window, property_name, |
794 (~0L), // (all of them) | 871 (~0L), // (all of them) |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
885 name_atom, | 962 name_atom, |
886 type_atom, | 963 type_atom, |
887 32, // size in bits of items in 'value' | 964 32, // size in bits of items in 'value' |
888 PropModeReplace, | 965 PropModeReplace, |
889 reinterpret_cast<const unsigned char*>(data.get()), | 966 reinterpret_cast<const unsigned char*>(data.get()), |
890 value.size()); // num items | 967 value.size()); // num items |
891 XSync(ui::GetXDisplay(), False); | 968 XSync(ui::GetXDisplay(), False); |
892 return gdk_error_trap_pop() == 0; | 969 return gdk_error_trap_pop() == 0; |
893 } | 970 } |
894 | 971 |
972 bool SetAtomArrayProperty(XID window, | |
973 const std::string& name, | |
974 const std::string& type, | |
975 const std::vector<Atom>& value) { | |
976 DCHECK(!value.empty()); | |
977 Atom name_atom = GetAtom(name.c_str()); | |
978 Atom type_atom = GetAtom(type.c_str()); | |
979 | |
980 // XChangeProperty() expects values of type 32 to be longs. | |
981 scoped_ptr<Atom[]> data(new Atom[value.size()]); | |
982 for (size_t i = 0; i < value.size(); ++i) | |
983 data[i] = value[i]; | |
984 | |
985 gdk_error_trap_push(); | |
986 XChangeProperty(ui::GetXDisplay(), | |
987 window, | |
988 name_atom, | |
989 type_atom, | |
990 32, // size in bits of items in 'value' | |
991 PropModeReplace, | |
992 reinterpret_cast<const unsigned char*>(data.get()), | |
993 value.size()); // num items | |
994 XSync(ui::GetXDisplay(), False); | |
Daniel Erat
2013/06/17 22:09:57
document that this makes a round trip?
| |
995 return gdk_error_trap_pop() == 0; | |
996 } | |
997 | |
895 Atom GetAtom(const char* name) { | 998 Atom GetAtom(const char* name) { |
896 #if defined(TOOLKIT_GTK) | 999 #if defined(TOOLKIT_GTK) |
897 return gdk_x11_get_xatom_by_name_for_display( | 1000 return gdk_x11_get_xatom_by_name_for_display( |
898 gdk_display_get_default(), name); | 1001 gdk_display_get_default(), name); |
899 #else | 1002 #else |
900 // TODO(derat): Cache atoms to avoid round-trips to the server. | 1003 // TODO(derat): Cache atoms to avoid round-trips to the server. |
901 return XInternAtom(GetXDisplay(), name, false); | 1004 return XInternAtom(GetXDisplay(), name, false); |
902 #endif | 1005 #endif |
903 } | 1006 } |
904 | 1007 |
(...skipping 651 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1556 << "request_code " << static_cast<int>(error_event.request_code) << ", " | 1659 << "request_code " << static_cast<int>(error_event.request_code) << ", " |
1557 << "minor_code " << static_cast<int>(error_event.minor_code) | 1660 << "minor_code " << static_cast<int>(error_event.minor_code) |
1558 << " (" << request_str << ")"; | 1661 << " (" << request_str << ")"; |
1559 } | 1662 } |
1560 | 1663 |
1561 // ---------------------------------------------------------------------------- | 1664 // ---------------------------------------------------------------------------- |
1562 // End of x11_util_internal.h | 1665 // End of x11_util_internal.h |
1563 | 1666 |
1564 | 1667 |
1565 } // namespace ui | 1668 } // namespace ui |
OLD | NEW |