| 1 | // SPDX-License-Identifier: MIT |
| 2 | // Copyright (c) 2011-2013, Christopher Jeffrey |
| 3 | // Copyright (c) 2018, Yuxuan Shui <[email protected]> |
| 4 | |
| 5 | #pragma once |
| 6 | |
| 7 | // === Options === |
| 8 | |
| 9 | // Debug options, enable them using -D in CFLAGS |
| 10 | // #define DEBUG_REPAINT 1 |
| 11 | // #define DEBUG_EVENTS 1 |
| 12 | // #define DEBUG_RESTACK 1 |
| 13 | // #define DEBUG_WINMATCH 1 |
| 14 | // #define DEBUG_C2 1 |
| 15 | // #define DEBUG_GLX_DEBUG_CONTEXT 1 |
| 16 | |
| 17 | #define MAX_ALPHA (255) |
| 18 | |
| 19 | // === Includes === |
| 20 | |
| 21 | // For some special functions |
| 22 | #include <stdbool.h> |
| 23 | #include <sys/time.h> |
| 24 | #include <time.h> |
| 25 | |
| 26 | #include <X11/Xlib.h> |
| 27 | #include <ev.h> |
| 28 | #include <pixman.h> |
| 29 | #include <uthash.h> |
| 30 | #include <xcb/render.h> |
| 31 | #include <xcb/sync.h> |
| 32 | #include <xcb/xproto.h> |
| 33 | |
| 34 | #include <picom/backend.h> |
| 35 | #include <picom/types.h> |
| 36 | |
| 37 | // FIXME This list of includes should get shorter |
| 38 | #include "backend/driver.h" |
| 39 | #include "config.h" |
| 40 | #include "region.h" |
| 41 | #include "utils/statistics.h" |
| 42 | #include "wm/defs.h" |
| 43 | #include "x.h" |
| 44 | |
| 45 | // === Constants ===0 |
| 46 | |
| 47 | #define NS_PER_SEC 1000000000L |
| 48 | #define US_PER_SEC 1000000L |
| 49 | #define MS_PER_SEC 1000 |
| 50 | |
| 51 | /// @brief Maximum OpenGL buffer age. |
| 52 | #define CGLX_MAX_BUFFER_AGE 5 |
| 53 | |
| 54 | // Window flags |
| 55 | |
| 56 | // === Types === |
| 57 | struct atom; |
| 58 | struct conv; |
| 59 | |
| 60 | struct shader_source { |
| 61 | const char *path; |
| 62 | const char *source; |
| 63 | UT_hash_handle hh; |
| 64 | }; |
| 65 | |
| 66 | struct shader_info { |
| 67 | const struct shader_specification *spec; |
| 68 | const char *source; |
| 69 | shader_handle backend_shader; |
| 70 | uint64_t attributes; |
| 71 | UT_hash_handle hh; |
| 72 | }; |
| 73 | |
| 74 | /// Structure containing all necessary data for a session. |
| 75 | typedef struct session { |
| 76 | // === Event handlers === |
| 77 | /// ev_io for X connection |
| 78 | ev_io xiow; |
| 79 | /// Timeout for delayed unredirection. |
| 80 | ev_timer unredir_timer; |
| 81 | /// Use an ev_timer callback for drawing |
| 82 | ev_timer draw_timer; |
| 83 | /// Called every time we have timeouts or new data on socket, |
| 84 | /// so we can be sure if xcb read from X socket at anytime during event |
| 85 | /// handling, we will not left any event unhandled in the queue |
| 86 | ev_prepare event_check; |
| 87 | /// Signal handler for SIGUSR1 |
| 88 | ev_signal usr1_signal; |
| 89 | /// Signal handler for SIGINT |
| 90 | ev_signal int_signal; |
| 91 | |
| 92 | // === Backend related === |
| 93 | /// backend data |
| 94 | backend_t *backend_data; |
| 95 | /// backend blur context |
| 96 | void *backend_blur_context; |
| 97 | /// graphic drivers used |
| 98 | enum driver drivers; |
| 99 | /// file watch handle |
| 100 | void *file_watch_handle; |
| 101 | /// libev mainloop |
| 102 | struct ev_loop *loop; |
| 103 | struct shader_info *root_pixmap_shader; |
| 104 | /// Shader sources |
| 105 | struct shader_source *shader_sources; |
| 106 | /// Shaders |
| 107 | struct shader_info *shaders; |
| 108 | |
| 109 | // === Display related === |
| 110 | /// X connection |
| 111 | struct x_connection c; |
| 112 | /// Width of root window. |
| 113 | int root_width; |
| 114 | /// Height of root window. |
| 115 | int root_height; |
| 116 | /// X Composite overlay window. |
| 117 | xcb_window_t overlay; |
| 118 | /// The target window for debug mode |
| 119 | xcb_window_t debug_window; |
| 120 | /// The backend data the root pixmap bound to |
| 121 | image_handle root_image; |
| 122 | /// The geometry of the root image |
| 123 | rect_t root_image_extent; |
| 124 | /// The root pixmap generation, incremented every time |
| 125 | /// the root pixmap changes |
| 126 | uint64_t root_image_generation; |
| 127 | /// A region of the size of the screen. |
| 128 | region_t screen_reg; |
| 129 | /// Window ID of the window we register as a symbol. |
| 130 | xcb_window_t reg_win; |
| 131 | /// Sync fence to sync draw operations |
| 132 | xcb_sync_fence_t sync_fence; |
| 133 | /// Whether we are rendering the first frame after screen is redirected |
| 134 | bool first_frame; |
| 135 | /// When last MSC event happened, in useconds. |
| 136 | uint64_t last_msc_instant; |
| 137 | /// The last MSC number |
| 138 | uint64_t last_msc; |
| 139 | /// The delay between when the last frame was scheduled to be rendered, and when |
| 140 | /// the render actually started. |
| 141 | uint64_t last_schedule_delay; |
| 142 | /// When do we want our next frame to start rendering. |
| 143 | uint64_t next_render; |
| 144 | /// Whether we can perform frame pacing. |
| 145 | bool frame_pacing; |
| 146 | /// Vblank event scheduler |
| 147 | struct vblank_scheduler *vblank_scheduler; |
| 148 | |
| 149 | /// Render statistics |
| 150 | struct render_statistics render_stats; |
| 151 | |
| 152 | // === Operation related === |
| 153 | /// Whether there is a pending quest to get the focused window |
| 154 | bool pending_focus_check; |
| 155 | /// Program options. |
| 156 | options_t o; |
| 157 | /// State object for c2. |
| 158 | struct c2_state *c2_state; |
| 159 | /// Whether we have hit unredirection timeout. |
| 160 | bool tmout_unredir_hit; |
| 161 | /// If the backend is busy. This means two things: |
| 162 | /// Either the backend is currently rendering a frame, or a frame has been |
| 163 | /// rendered but has yet to be presented. In either case, we should not start |
| 164 | /// another render right now. As if we start issuing rendering commands now, we |
| 165 | /// will have to wait for either the current render to finish, or the current |
| 166 | /// back buffer to become available again. In either case, we will be wasting |
| 167 | /// time. |
| 168 | bool backend_busy; |
| 169 | /// Whether a render is queued. This generally means there are pending updates |
| 170 | /// to the screen that's neither included in the current render, nor on the |
| 171 | /// screen. |
| 172 | bool render_queued; |
| 173 | /// A X region used for various operations. Kept to avoid repeated allocation. |
| 174 | xcb_xfixes_region_t x_region; |
| 175 | // TODO(yshui) move render related fields into separate struct |
| 176 | /// Render planner |
| 177 | struct layout_manager *layout_manager; |
| 178 | /// Render command builder |
| 179 | struct command_builder *command_builder; |
| 180 | struct renderer *renderer; |
| 181 | /// Whether all windows are currently redirected. |
| 182 | bool redirected; |
| 183 | /// Time of last fading. In milliseconds. |
| 184 | long long fade_time; |
| 185 | /// If we should quit |
| 186 | bool quit : 1; |
| 187 | // TODO(yshui) use separate flags for different kinds of updates so we don't |
| 188 | // waste our time. |
| 189 | /// Whether there are pending updates, like window creation, etc. |
| 190 | bool pending_updates : 1; |
| 191 | |
| 192 | struct wm *wm; |
| 193 | |
| 194 | struct window_options window_options_default; |
| 195 | |
| 196 | // === X extension related === |
| 197 | /// Information about monitors. |
| 198 | struct x_monitors monitors; |
| 199 | |
| 200 | // === Atoms === |
| 201 | struct atom *atoms; |
| 202 | |
| 203 | #ifdef CONFIG_DBUS |
| 204 | // === DBus related === |
| 205 | struct cdbus_data *dbus_data; |
| 206 | #endif |
| 207 | } session_t; |
| 208 | |
| 209 | struct wintype_info { |
| 210 | const char *name; |
| 211 | const char *atom; |
| 212 | }; |
| 213 | extern const struct wintype_info WINTYPES[NUM_WINTYPES]; |
| 214 | extern session_t *ps_g; |
| 215 | |
| 216 | // === Functions === |
| 217 | |
| 218 | /** |
| 219 | * Get current time in struct timespec. |
| 220 | * |
| 221 | * Note its starting time is unspecified. |
| 222 | */ |
| 223 | static inline struct timespec get_time_timespec(void) { |
| 224 | struct timespec tm = {0, 0}; |
| 225 | |
| 226 | clock_gettime(CLOCK_MONOTONIC, &tm); |
| 227 | |
| 228 | // Return a time of all 0 if the call fails |
| 229 | return tm; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Determine if a window has a specific property. |
| 234 | * |
| 235 | * @param ps current session |
| 236 | * @param w window to check |
| 237 | * @param atom atom of property to check |
| 238 | * @return true if it has the attribute, false otherwise |
| 239 | */ |
| 240 | static inline bool wid_has_prop(xcb_connection_t *c, xcb_window_t w, xcb_atom_t atom) { |
| 241 | auto r = xcb_get_property_reply( |
| 242 | c, xcb_get_property(c, 0, w, atom, XCB_GET_PROPERTY_TYPE_ANY, 0, 0), NULL); |
| 243 | if (!r) { |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | auto rtype = r->type; |
| 248 | free(r); |
| 249 | |
| 250 | if (rtype != XCB_NONE) { |
| 251 | return true; |
| 252 | } |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | void force_repaint(session_t *ps); |