OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
61 FROM_HERE, NewRunnableFunction(LogErrorEventDescription, d, *e)); | 61 FROM_HERE, NewRunnableFunction(LogErrorEventDescription, d, *e)); |
62 return 0; | 62 return 0; |
63 } | 63 } |
64 | 64 |
65 int DefaultX11IOErrorHandler(Display* d) { | 65 int DefaultX11IOErrorHandler(Display* d) { |
66 // If there's an IO error it likely means the X server has gone away | 66 // If there's an IO error it likely means the X server has gone away |
67 LOG(ERROR) << "X IO Error detected"; | 67 LOG(ERROR) << "X IO Error detected"; |
68 _exit(1); | 68 _exit(1); |
69 } | 69 } |
70 | 70 |
71 XErrorHandler current_error_handler = DefaultX11ErrorHandler; | |
72 | |
73 XErrorEvent* last_error_event = NULL; | |
Ami GONE FROM CHROMIUM
2011/09/20 23:45:51
Why not simply in-line?
static XErrorEvent last_er
dominich
2011/09/21 19:47:30
I wasn't sure if that was allowed, given that theo
| |
74 | |
75 int BaseX11ErrorHandler(Display* d, XErrorEvent* e) { | |
76 // If an error is reported but never checked, this will leak. | |
77 if (!last_error_event) | |
78 last_error_event = new XErrorEvent(); | |
79 *last_error_event = *e; | |
80 return current_error_handler(d, e); | |
81 } | |
82 | |
71 // Note: The caller should free the resulting value data. | 83 // Note: The caller should free the resulting value data. |
72 bool GetProperty(XID window, const std::string& property_name, long max_length, | 84 bool GetProperty(XID window, const std::string& property_name, long max_length, |
73 Atom* type, int* format, unsigned long* num_items, | 85 Atom* type, int* format, unsigned long* num_items, |
74 unsigned char** property) { | 86 unsigned char** property) { |
75 Atom property_atom = gdk_x11_get_xatom_by_name_for_display( | 87 Atom property_atom = gdk_x11_get_xatom_by_name_for_display( |
76 gdk_display_get_default(), property_name.c_str()); | 88 gdk_display_get_default(), property_name.c_str()); |
77 | 89 |
78 unsigned long remaining_bytes = 0; | 90 unsigned long remaining_bytes = 0; |
79 return XGetWindowProperty(GetXDisplay(), | 91 return XGetWindowProperty(GetXDisplay(), |
80 window, | 92 window, |
81 property_atom, | 93 property_atom, |
82 0, // offset into property data to read | 94 0, // offset into property data to read |
83 max_length, // max length to get | 95 max_length, // max length to get |
84 False, // deleted | 96 False, // deleted |
85 AnyPropertyType, | 97 AnyPropertyType, |
86 type, | 98 type, |
87 format, | 99 format, |
88 num_items, | 100 num_items, |
89 &remaining_bytes, | 101 &remaining_bytes, |
90 property); | 102 property); |
91 } | 103 } |
92 | 104 |
105 std::string BuildX11ErrorString(Display* dpy, const XErrorEvent& error_event) { | |
Ami GONE FROM CHROMIUM
2011/09/20 23:45:51
|dpy| is unnecessary since you can use |error_even
dominich
2011/09/21 19:47:30
Yes, but adding a DCHECK that these are the same.
| |
106 char error_str[256]; | |
107 char request_str[256]; | |
108 | |
109 XGetErrorText(dpy, error_event.error_code, error_str, sizeof(error_str)); | |
110 | |
111 strncpy(request_str, "Unknown", sizeof(request_str)); | |
112 if (error_event.request_code < 128) { | |
113 std::string num = base::UintToString(error_event.request_code); | |
114 XGetErrorDatabaseText( | |
115 dpy, "XRequest", num.c_str(), "Unknown", request_str, | |
116 sizeof(request_str)); | |
117 } else { | |
118 int num_ext; | |
119 char** ext_list = XListExtensions(dpy, &num_ext); | |
120 | |
121 for (int i = 0; i < num_ext; i++) { | |
122 int ext_code, first_event, first_error; | |
123 XQueryExtension(dpy, ext_list[i], &ext_code, &first_event, &first_error); | |
124 if (error_event.request_code == ext_code) { | |
125 std::string msg = StringPrintf( | |
126 "%s.%d", ext_list[i], error_event.minor_code); | |
127 XGetErrorDatabaseText( | |
128 dpy, "XRequest", msg.c_str(), "Unknown", request_str, | |
129 sizeof(request_str)); | |
130 break; | |
131 } | |
132 } | |
133 XFreeExtensionList(ext_list); | |
134 } | |
135 | |
136 std::ostringstream error_ss; | |
137 error_ss << "X Error detected: " | |
138 << "serial " << error_event.serial << ", " | |
139 << "error_code " << static_cast<int>(error_event.error_code) | |
140 << " (" << error_str << "), " | |
141 << "request_code " << static_cast<int>(error_event.request_code) | |
142 << ", " | |
143 << "minor_code " << static_cast<int>(error_event.minor_code) | |
144 << " (" << request_str << ")"; | |
145 return error_ss.str(); | |
146 } | |
147 | |
93 } // namespace | 148 } // namespace |
94 | 149 |
95 bool XDisplayExists() { | 150 bool XDisplayExists() { |
96 return (gdk_display_get_default() != NULL); | 151 return (gdk_display_get_default() != NULL); |
97 } | 152 } |
98 | 153 |
99 Display* GetXDisplay() { | 154 Display* GetXDisplay() { |
100 return base::MessagePumpForUI::GetDefaultXDisplay(); | 155 return base::MessagePumpForUI::GetDefaultXDisplay(); |
101 } | 156 } |
102 | 157 |
(...skipping 569 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
672 event.xclient.format = 32; | 727 event.xclient.format = 32; |
673 event.xclient.data.l[0] = desktop; | 728 event.xclient.data.l[0] = desktop; |
674 event.xclient.data.l[1] = 1; // source indication | 729 event.xclient.data.l[1] = 1; // source indication |
675 | 730 |
676 int result = XSendEvent(GetXDisplay(), GetX11RootWindow(), False, | 731 int result = XSendEvent(GetXDisplay(), GetX11RootWindow(), False, |
677 SubstructureNotifyMask, &event); | 732 SubstructureNotifyMask, &event); |
678 return result == Success; | 733 return result == Success; |
679 } | 734 } |
680 | 735 |
681 void SetDefaultX11ErrorHandlers() { | 736 void SetDefaultX11ErrorHandlers() { |
737 XSetErrorHandler(BaseX11ErrorHandler); | |
682 SetX11ErrorHandlers(NULL, NULL); | 738 SetX11ErrorHandlers(NULL, NULL); |
683 } | 739 } |
684 | 740 |
685 bool IsX11WindowFullScreen(XID window) { | 741 bool IsX11WindowFullScreen(XID window) { |
686 // First check if _NET_WM_STATE property contains _NET_WM_STATE_FULLSCREEN. | 742 // First check if _NET_WM_STATE property contains _NET_WM_STATE_FULLSCREEN. |
687 static Atom atom = gdk_x11_get_xatom_by_name_for_display( | 743 static Atom atom = gdk_x11_get_xatom_by_name_for_display( |
688 gdk_display_get_default(), "_NET_WM_STATE_FULLSCREEN"); | 744 gdk_display_get_default(), "_NET_WM_STATE_FULLSCREEN"); |
689 | 745 |
690 std::vector<Atom> atom_properties; | 746 std::vector<Atom> atom_properties; |
691 if (GetAtomArrayProperty(window, | 747 if (GetAtomArrayProperty(window, |
(...skipping 11 matching lines...) Expand all Loading... | |
703 gfx::Rect window_rect; | 759 gfx::Rect window_rect; |
704 if (!ui::GetWindowRect(window, &window_rect)) | 760 if (!ui::GetWindowRect(window, &window_rect)) |
705 return false; | 761 return false; |
706 | 762 |
707 return monitor_rect.x == window_rect.x() && | 763 return monitor_rect.x == window_rect.x() && |
708 monitor_rect.y == window_rect.y() && | 764 monitor_rect.y == window_rect.y() && |
709 monitor_rect.width == window_rect.width() && | 765 monitor_rect.width == window_rect.width() && |
710 monitor_rect.height == window_rect.height(); | 766 monitor_rect.height == window_rect.height(); |
711 } | 767 } |
712 | 768 |
769 void CheckFailOnX11Error() { | |
770 DCHECK(XDisplayExists()); | |
771 Display* display = GetXDisplay(); | |
772 XSync(display, False); | |
Ami GONE FROM CHROMIUM
2011/09/20 23:45:51
The Xlib calls you make here and from BuildX11Erro
dominich
2011/09/21 19:47:30
Nothing enforces it, though it is documented in th
Ami GONE FROM CHROMIUM
2011/09/21 22:21:42
I think you're talking about out-of-line execution
| |
773 CHECK(!last_error_event) << BuildX11ErrorString(display, *last_error_event); | |
774 delete last_error_event; | |
775 last_error_event = NULL; | |
776 } | |
777 | |
713 // ---------------------------------------------------------------------------- | 778 // ---------------------------------------------------------------------------- |
714 // These functions are declared in x11_util_internal.h because they require | 779 // These functions are declared in x11_util_internal.h because they require |
715 // XLib.h to be included, and it conflicts with many other headers. | 780 // XLib.h to be included, and it conflicts with many other headers. |
716 XRenderPictFormat* GetRenderARGB32Format(Display* dpy) { | 781 XRenderPictFormat* GetRenderARGB32Format(Display* dpy) { |
717 static XRenderPictFormat* pictformat = NULL; | 782 static XRenderPictFormat* pictformat = NULL; |
718 if (pictformat) | 783 if (pictformat) |
719 return pictformat; | 784 return pictformat; |
720 | 785 |
721 // First look for a 32-bit format which ignores the alpha value | 786 // First look for a 32-bit format which ignores the alpha value |
722 XRenderPictFormat templ; | 787 XRenderPictFormat templ; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
781 // always blowing away the cache. If we are, then we should figure out why | 846 // always blowing away the cache. If we are, then we should figure out why |
782 // and make it bigger. | 847 // and make it bigger. |
783 NOTREACHED(); | 848 NOTREACHED(); |
784 } | 849 } |
785 | 850 |
786 return pictformat; | 851 return pictformat; |
787 } | 852 } |
788 | 853 |
789 void SetX11ErrorHandlers(XErrorHandler error_handler, | 854 void SetX11ErrorHandlers(XErrorHandler error_handler, |
790 XIOErrorHandler io_error_handler) { | 855 XIOErrorHandler io_error_handler) { |
791 XSetErrorHandler(error_handler ? error_handler : DefaultX11ErrorHandler); | 856 current_error_handler = error_handler ? |
792 XSetIOErrorHandler( | 857 error_handler : DefaultX11ErrorHandler; |
793 io_error_handler ? io_error_handler : DefaultX11IOErrorHandler); | 858 XSetIOErrorHandler(io_error_handler ? |
859 io_error_handler : DefaultX11IOErrorHandler); | |
794 } | 860 } |
795 | 861 |
796 void LogErrorEventDescription(Display* dpy, | 862 void LogErrorEventDescription(Display* dpy, |
797 const XErrorEvent& error_event) { | 863 const XErrorEvent& error_event) { |
798 char error_str[256]; | 864 LOG(ERROR) << BuildX11ErrorString(dpy, error_event); |
799 char request_str[256]; | |
800 | |
801 XGetErrorText(dpy, error_event.error_code, error_str, sizeof(error_str)); | |
802 | |
803 strncpy(request_str, "Unknown", sizeof(request_str)); | |
804 if (error_event.request_code < 128) { | |
805 std::string num = base::UintToString(error_event.request_code); | |
806 XGetErrorDatabaseText( | |
807 dpy, "XRequest", num.c_str(), "Unknown", request_str, | |
808 sizeof(request_str)); | |
809 } else { | |
810 int num_ext; | |
811 char** ext_list = XListExtensions(dpy, &num_ext); | |
812 | |
813 for (int i = 0; i < num_ext; i++) { | |
814 int ext_code, first_event, first_error; | |
815 XQueryExtension(dpy, ext_list[i], &ext_code, &first_event, &first_error); | |
816 if (error_event.request_code == ext_code) { | |
817 std::string msg = StringPrintf( | |
818 "%s.%d", ext_list[i], error_event.minor_code); | |
819 XGetErrorDatabaseText( | |
820 dpy, "XRequest", msg.c_str(), "Unknown", request_str, | |
821 sizeof(request_str)); | |
822 break; | |
823 } | |
824 } | |
825 XFreeExtensionList(ext_list); | |
826 } | |
827 | |
828 LOG(ERROR) | |
829 << "X Error detected: " | |
830 << "serial " << error_event.serial << ", " | |
831 << "error_code " << static_cast<int>(error_event.error_code) | |
832 << " (" << error_str << "), " | |
833 << "request_code " << static_cast<int>(error_event.request_code) << ", " | |
834 << "minor_code " << static_cast<int>(error_event.minor_code) | |
835 << " (" << request_str << ")"; | |
836 } | 865 } |
837 | 866 |
838 // ---------------------------------------------------------------------------- | 867 // ---------------------------------------------------------------------------- |
839 // End of x11_util_internal.h | 868 // End of x11_util_internal.h |
840 | 869 |
841 | 870 |
842 } // namespace ui | 871 } // namespace ui |
OLD | NEW |