diff options
author | Dave Davenport <qball@gmpclient.org> | 2018-02-05 20:47:24 +0100 |
---|---|---|
committer | Dave Davenport <qball@gmpclient.org> | 2018-02-05 20:47:24 +0100 |
commit | 83b77b96ccbb938ec2ed03bfc1f15eb458a8759d (patch) | |
tree | 1a359062187e16b958fd99a4f3d81ba1f7f38592 | |
parent | 2ca7eaaa489a0a20f57bb474ae1905a86f7109c1 (diff) |
Add check for randr header version
-rw-r--r-- | source/xcb.c | 434 |
1 files changed, 255 insertions, 179 deletions
diff --git a/source/xcb.c b/source/xcb.c index 5760fb9e..deb757ba 100644 --- a/source/xcb.c +++ b/source/xcb.c @@ -99,25 +99,25 @@ const char *netatom_names[] = { EWMH_ATOMS ( ATOM_CHAR ) }; cairo_surface_t *x11_helper_get_screenshot_surface ( void ) { return cairo_xcb_surface_create ( xcb->connection, - xcb_stuff_get_root_window (), root_visual, - xcb->screen->width_in_pixels, xcb->screen->height_in_pixels ); + xcb_stuff_get_root_window (), root_visual, + xcb->screen->width_in_pixels, xcb->screen->height_in_pixels ); } static xcb_pixmap_t get_root_pixmap ( xcb_connection_t *c, - xcb_screen_t *screen, - xcb_atom_t atom ) + xcb_screen_t *screen, + xcb_atom_t atom ) { xcb_get_property_cookie_t cookie; xcb_get_property_reply_t *reply; xcb_pixmap_t rootpixmap = XCB_NONE; cookie = xcb_get_property ( c, - 0, - screen->root, - atom, - XCB_ATOM_PIXMAP, - 0, - 1 ); + 0, + screen->root, + atom, + XCB_ATOM_PIXMAP, + 0, + 1 ); reply = xcb_get_property_reply ( c, cookie, NULL ); @@ -138,7 +138,7 @@ cairo_surface_t * x11_helper_get_bg_surface ( void ) return NULL; } return cairo_xcb_surface_create ( xcb->connection, pm, root_visual, - xcb->screen->width_in_pixels, xcb->screen->height_in_pixels ); + xcb->screen->width_in_pixels, xcb->screen->height_in_pixels ); } // retrieve a text property from a window @@ -195,7 +195,46 @@ static void x11_monitors_free ( void ) } } +/** + * Create monitor based on output id + */ +static workarea * x11_get_monitor_from_output ( xcb_randr_output_t out ) +{ + xcb_randr_get_output_info_reply_t *op_reply; + xcb_randr_get_crtc_info_reply_t *crtc_reply; + xcb_randr_get_output_info_cookie_t it = xcb_randr_get_output_info ( xcb->connection, out, XCB_CURRENT_TIME ); + op_reply = xcb_randr_get_output_info_reply ( xcb->connection, it, NULL ); + if ( op_reply->crtc == XCB_NONE ) { + free ( op_reply ); + return NULL; + } + xcb_randr_get_crtc_info_cookie_t ct = xcb_randr_get_crtc_info ( xcb->connection, op_reply->crtc, XCB_CURRENT_TIME ); + crtc_reply = xcb_randr_get_crtc_info_reply ( xcb->connection, ct, NULL ); + if ( !crtc_reply ) { + free ( op_reply ); + return NULL; + } + workarea *retv = g_malloc0 ( sizeof ( workarea ) ); + retv->x = crtc_reply->x; + retv->y = crtc_reply->y; + retv->w = crtc_reply->width; + retv->h = crtc_reply->height; + + retv->mw = op_reply->mm_width; + retv->mh = op_reply->mm_height; + + char *tname = (char *) xcb_randr_get_output_info_name ( op_reply ); + int tname_len = xcb_randr_get_output_info_name_length ( op_reply ); + retv->name = g_malloc0 ( ( tname_len + 1 ) * sizeof ( char ) ); + memcpy ( retv->name, tname, tname_len ); + + free ( crtc_reply ); + free ( op_reply ); + return retv; +} + +#if ( (XCB_RANDR_MAJOR_VERSION >= 1) && (XCB_RANDR_MINOR_VERSION >= 5) ) /** * Create monitor based on xrandr monitor id. */ @@ -229,6 +268,7 @@ static workarea *x11_get_monitor_from_randr_monitor ( xcb_randr_monitor_info_t * return retv; } +#endif static int x11_is_extension_present ( const char *extension ) { @@ -246,18 +286,18 @@ static int x11_is_extension_present ( const char *extension ) static void x11_build_monitor_layout_xinerama () { xcb_xinerama_query_screens_cookie_t screens_cookie = xcb_xinerama_query_screens_unchecked ( - xcb->connection - ); + xcb->connection + ); xcb_xinerama_query_screens_reply_t *screens_reply = xcb_xinerama_query_screens_reply ( - xcb->connection, - screens_cookie, - NULL - ); + xcb->connection, + screens_cookie, + NULL + ); xcb_xinerama_screen_info_iterator_t screens_iterator = xcb_xinerama_query_screens_screen_info_iterator ( - screens_reply - ); + screens_reply + ); for (; screens_iterator.rem > 0; xcb_xinerama_screen_info_next ( &screens_iterator ) ) { workarea *w = g_malloc0 ( sizeof ( workarea ) ); @@ -297,6 +337,7 @@ static void x11_build_monitor_layout () } g_debug ( "Query RANDR for monitor layout." ); +#if ( (XCB_RANDR_MAJOR_VERSION >= 1) && (XCB_RANDR_MINOR_VERSION >= 5) ) xcb_randr_get_monitors_cookie_t t = xcb_randr_get_monitors( xcb->connection, xcb->screen->root, 1 ); xcb_randr_get_monitors_reply_t *mreply = xcb_randr_get_monitors_reply ( xcb->connection, t, NULL ); if( mreply ) { @@ -311,6 +352,41 @@ static void x11_build_monitor_layout () } free ( mreply ); } +#endif + + // If no monitors found. + if ( xcb->monitors == NULL ) { + xcb_randr_get_screen_resources_current_reply_t *res_reply; + xcb_randr_get_screen_resources_current_cookie_t src; + src = xcb_randr_get_screen_resources_current ( xcb->connection, xcb->screen->root ); + res_reply = xcb_randr_get_screen_resources_current_reply ( xcb->connection, src, NULL ); + if ( !res_reply ) { + return; //just report error + } + int mon_num = xcb_randr_get_screen_resources_current_outputs_length ( res_reply ); + xcb_randr_output_t *ops = xcb_randr_get_screen_resources_current_outputs ( res_reply ); + + // Get primary. + xcb_randr_get_output_primary_cookie_t pc = xcb_randr_get_output_primary ( xcb->connection, xcb->screen->root ); + xcb_randr_get_output_primary_reply_t *pc_rep = xcb_randr_get_output_primary_reply ( xcb->connection, pc, NULL ); + + for ( int i = mon_num - 1; i >= 0; i-- ) { + workarea *w = x11_get_monitor_from_output ( ops[i] ); + if ( w ) { + w->next = xcb->monitors; + xcb->monitors = w; + if ( pc_rep && pc_rep->output == ops[i] ) { + w->primary = TRUE; + } + } + } + // If exists, free primary output reply. + if ( pc_rep ) { + free ( pc_rep ); + } + free ( res_reply ); + + } // Number monitor int index = 0; @@ -334,13 +410,13 @@ void display_dump_monitor_layout ( void ) printf ( "%s size%s: %d,%d\n", ( is_term ) ? color_bold : "", is_term ? color_reset : "", iter->w, iter->h ); if ( iter->mw > 0 && iter->mh > 0 ) { printf ( "%s size%s: %dmm,%dmm dpi: %.0f,%.0f\n", - ( is_term ) ? color_bold : "", - is_term ? color_reset : "", - iter->mw, - iter->mh, - iter->w * 25.4 / (double) iter->mw, - iter->h * 25.4 / (double) iter->mh - ); + ( is_term ) ? color_bold : "", + is_term ? color_reset : "", + iter->mw, + iter->mh, + iter->w * 25.4 / (double) iter->mw, + iter->h * 25.4 / (double) iter->mh + ); } printf ( "\n" ); } @@ -538,7 +614,7 @@ static int monitor_active_from_id ( int mon_id, workarea *mon ) if ( xcb_ewmh_get_desktop_viewport_reply ( &xcb->ewmh, c, &vp, NULL ) ) { if ( current_desktop < vp.desktop_viewport_len ) { monitor_dimensions ( vp.desktop_viewport[current_desktop].x, - vp.desktop_viewport[current_desktop].y, mon ); + vp.desktop_viewport[current_desktop].y, mon ); xcb_ewmh_get_desktop_viewport_reply_wipe ( &vp ); return TRUE; } @@ -657,28 +733,28 @@ static gboolean x11_button_to_nk_bindings_button ( guint32 x11_button, NkBinding { switch ( x11_button ) { - case 1: - *button = NK_BINDINGS_MOUSE_BUTTON_PRIMARY; - break; - case 3: - *button = NK_BINDINGS_MOUSE_BUTTON_SECONDARY; - break; - case 2: - *button = NK_BINDINGS_MOUSE_BUTTON_MIDDLE; - break; - case 8: - *button = NK_BINDINGS_MOUSE_BUTTON_BACK; - break; - case 9: - *button = NK_BINDINGS_MOUSE_BUTTON_FORWARD; - break; - case 4: - case 5: - case 6: - case 7: - return FALSE; - default: - *button = NK_BINDINGS_MOUSE_BUTTON_EXTRA + x11_button; + case 1: + *button = NK_BINDINGS_MOUSE_BUTTON_PRIMARY; + break; + case 3: + *button = NK_BINDINGS_MOUSE_BUTTON_SECONDARY; + break; + case 2: + *button = NK_BINDINGS_MOUSE_BUTTON_MIDDLE; + break; + case 8: + *button = NK_BINDINGS_MOUSE_BUTTON_BACK; + break; + case 9: + *button = NK_BINDINGS_MOUSE_BUTTON_FORWARD; + break; + case 4: + case 5: + case 6: + case 7: + return FALSE; + default: + *button = NK_BINDINGS_MOUSE_BUTTON_EXTRA + x11_button; } return TRUE; } @@ -688,20 +764,20 @@ static gboolean x11_button_to_nk_bindings_scroll ( guint32 x11_button, NkBinding *steps = 1; switch ( x11_button ) { - case 4: - *steps = -1; - /* fallthrough */ - case 5: - *axis = NK_BINDINGS_SCROLL_AXIS_VERTICAL; - break; - case 6: - *steps = -1; - /* fallthrough */ - case 7: - *axis = NK_BINDINGS_SCROLL_AXIS_HORIZONTAL; - break; - default: - return FALSE; + case 4: + *steps = -1; + /* fallthrough */ + case 5: + *axis = NK_BINDINGS_SCROLL_AXIS_VERTICAL; + break; + case 6: + *steps = -1; + /* fallthrough */ + case 7: + *axis = NK_BINDINGS_SCROLL_AXIS_HORIZONTAL; + break; + default: + return FALSE; } return TRUE; } @@ -718,93 +794,93 @@ static void main_loop_x11_event_handler_view ( xcb_generic_event_t *event ) switch ( event->response_type & ~0x80 ) { - case XCB_EXPOSE: - rofi_view_frame_callback (); - break; - case XCB_CONFIGURE_NOTIFY: - { - xcb_configure_notify_event_t *xce = (xcb_configure_notify_event_t *) event; - rofi_view_temp_configure_notify ( state, xce ); - break; - } - case XCB_MOTION_NOTIFY: - { - if ( config.click_to_exit == TRUE ) { - xcb->mouse_seen = TRUE; - } - xcb_motion_notify_event_t *xme = (xcb_motion_notify_event_t *) event; - rofi_view_handle_mouse_motion ( state, xme->event_x, xme->event_y ); - break; - } - case XCB_BUTTON_PRESS: - { - xcb_button_press_event_t *bpe = (xcb_button_press_event_t *) event; - NkBindingsMouseButton button; - NkBindingsScrollAxis axis; - gint32 steps; - - xcb->last_timestamp = bpe->time; - rofi_view_handle_mouse_motion ( state, bpe->event_x, bpe->event_y ); - if ( x11_button_to_nk_bindings_button ( bpe->detail, &button ) ) - nk_bindings_seat_handle_button ( xcb->bindings_seat, NULL, button, NK_BINDINGS_BUTTON_STATE_PRESS, bpe->time ); - else if ( x11_button_to_nk_bindings_scroll ( bpe->detail, &axis, &steps) ) - nk_bindings_seat_handle_scroll ( xcb->bindings_seat, NULL, axis, steps ); - break; - } - case XCB_BUTTON_RELEASE: - { - xcb_button_release_event_t *bre = (xcb_button_release_event_t *) event; - NkBindingsMouseButton button; - - xcb->last_timestamp = bre->time; - if ( x11_button_to_nk_bindings_button ( bre->detail, &button ) ) - nk_bindings_seat_handle_button ( xcb->bindings_seat, NULL, button, NK_BINDINGS_BUTTON_STATE_RELEASE, bre->time ); - if ( config.click_to_exit == TRUE ) { - if ( !xcb->mouse_seen ) { - rofi_view_temp_click_to_exit ( state, bre->event ); + case XCB_EXPOSE: + rofi_view_frame_callback (); + break; + case XCB_CONFIGURE_NOTIFY: + { + xcb_configure_notify_event_t *xce = (xcb_configure_notify_event_t *) event; + rofi_view_temp_configure_notify ( state, xce ); + break; } - xcb->mouse_seen = FALSE; - } - break; - } - // Paste event. - case XCB_SELECTION_NOTIFY: - rofi_view_paste ( state, (xcb_selection_notify_event_t *) event ); - break; - case XCB_KEYMAP_NOTIFY: - { - xcb_keymap_notify_event_t *kne = (xcb_keymap_notify_event_t *) event; - for ( gint32 by = 0; by < 31; ++by ) { - for ( gint8 bi = 0; bi < 7; ++bi ) { - if ( kne->keys[by] & ( 1 << bi ) ) { - // X11 keycodes starts at 8 - nk_bindings_seat_handle_key ( xcb->bindings_seat, NULL, ( 8 * by + bi ) + 8, NK_BINDINGS_KEY_STATE_PRESSED ); + case XCB_MOTION_NOTIFY: + { + if ( config.click_to_exit == TRUE ) { + xcb->mouse_seen = TRUE; } + xcb_motion_notify_event_t *xme = (xcb_motion_notify_event_t *) event; + rofi_view_handle_mouse_motion ( state, xme->event_x, xme->event_y ); + break; } - } - break; - } - case XCB_KEY_PRESS: - { - xcb_key_press_event_t *xkpe = (xcb_key_press_event_t *) event; - gchar *text; - - xcb->last_timestamp = xkpe->time; - text = nk_bindings_seat_handle_key_with_modmask ( xcb->bindings_seat, NULL, xkpe->state, xkpe->detail, NK_BINDINGS_KEY_STATE_PRESS ); - if ( text != NULL ) { - rofi_view_handle_text ( state, text ); - } - break; - } - case XCB_KEY_RELEASE: - { - xcb_key_release_event_t *xkre = (xcb_key_release_event_t *) event; - xcb->last_timestamp = xkre->time; - nk_bindings_seat_handle_key ( xcb->bindings_seat, NULL, xkre->detail, NK_BINDINGS_KEY_STATE_RELEASE ); - break; - } - default: - break; + case XCB_BUTTON_PRESS: + { + xcb_button_press_event_t *bpe = (xcb_button_press_event_t *) event; + NkBindingsMouseButton button; + NkBindingsScrollAxis axis; + gint32 steps; + + xcb->last_timestamp = bpe->time; + rofi_view_handle_mouse_motion ( state, bpe->event_x, bpe->event_y ); + if ( x11_button_to_nk_bindings_button ( bpe->detail, &button ) ) + nk_bindings_seat_handle_button ( xcb->bindings_seat, NULL, button, NK_BINDINGS_BUTTON_STATE_PRESS, bpe->time ); + else if ( x11_button_to_nk_bindings_scroll ( bpe->detail, &axis, &steps) ) + nk_bindings_seat_handle_scroll ( xcb->bindings_seat, NULL, axis, steps ); + break; + } + case XCB_BUTTON_RELEASE: + { + xcb_button_release_event_t *bre = (xcb_button_release_event_t *) event; + NkBindingsMouseButton button; + + xcb->last_timestamp = bre->time; + if ( x11_button_to_nk_bindings_button ( bre->detail, &button ) ) + nk_bindings_seat_handle_button ( xcb->bindings_seat, NULL, button, NK_BINDINGS_BUTTON_STATE_RELEASE, bre->time ); + if ( config.click_to_exit == TRUE ) { + if ( !xcb->mouse_seen ) { + rofi_view_temp_click_to_exit ( state, bre->event ); + } + xcb->mouse_seen = FALSE; + } + break; + } + // Paste event. + case XCB_SELECTION_NOTIFY: + rofi_view_paste ( state, (xcb_selection_notify_event_t *) event ); + break; + case XCB_KEYMAP_NOTIFY: + { + xcb_keymap_notify_event_t *kne = (xcb_keymap_notify_event_t *) event; + for ( gint32 by = 0; by < 31; ++by ) { + for ( gint8 bi = 0; bi < 7; ++bi ) { + if ( kne->keys[by] & ( 1 << bi ) ) { + // X11 keycodes starts at 8 + nk_bindings_seat_handle_key ( xcb->bindings_seat, NULL, ( 8 * by + bi ) + 8, NK_BINDINGS_KEY_STATE_PRESSED ); + } + } + } + break; + } + case XCB_KEY_PRESS: + { + xcb_key_press_event_t *xkpe = (xcb_key_press_event_t *) event; + gchar *text; + + xcb->last_timestamp = xkpe->time; + text = nk_bindings_seat_handle_key_with_modmask ( xcb->bindings_seat, NULL, xkpe->state, xkpe->detail, NK_BINDINGS_KEY_STATE_PRESS ); + if ( text != NULL ) { + rofi_view_handle_text ( state, text ); + } + break; + } + case XCB_KEY_RELEASE: + { + xcb_key_release_event_t *xkre = (xcb_key_release_event_t *) event; + xcb->last_timestamp = xkre->time; + nk_bindings_seat_handle_key ( xcb->bindings_seat, NULL, xkre->detail, NK_BINDINGS_KEY_STATE_RELEASE ); + break; + } + default: + break; } rofi_view_maybe_update ( state ); } @@ -827,28 +903,28 @@ static gboolean main_loop_x11_event_handler ( xcb_generic_event_t *ev, G_GNUC_UN if ( type == xcb->xkb.first_event ) { switch ( ev->pad0 ) { - case XCB_XKB_MAP_NOTIFY: - { - struct xkb_keymap *keymap = xkb_x11_keymap_new_from_device ( nk_bindings_seat_get_context ( xcb->bindings_seat ), xcb->connection, xcb->xkb.device_id, 0 ); - struct xkb_state *state = xkb_x11_state_new_from_device ( keymap, xcb->connection, xcb->xkb.device_id ); - nk_bindings_seat_update_keymap ( xcb->bindings_seat, keymap, state ); - xkb_keymap_unref ( keymap ); - xkb_state_unref ( state ); - break; - } - case XCB_XKB_STATE_NOTIFY: - { - xcb_xkb_state_notify_event_t *ksne = (xcb_xkb_state_notify_event_t *) ev; - nk_bindings_seat_update_mask ( xcb->bindings_seat, NULL, - ksne->baseMods, - ksne->latchedMods, - ksne->lockedMods, - ksne->baseGroup, - ksne->latchedGroup, - ksne->lockedGroup ); - rofi_view_maybe_update ( rofi_view_get_active () ); - break; - } + case XCB_XKB_MAP_NOTIFY: + { + struct xkb_keymap *keymap = xkb_x11_keymap_new_from_device ( nk_bindings_seat_get_context ( xcb->bindings_seat ), xcb->connection, xcb->xkb.device_id, 0 ); + struct xkb_state *state = xkb_x11_state_new_from_device ( keymap, xcb->connection, xcb->xkb.device_id ); + nk_bindings_seat_update_keymap ( xcb->bindings_seat, keymap, state ); + xkb_keymap_unref ( keymap ); + xkb_state_unref ( state ); + break; + } + case XCB_XKB_STATE_NOTIFY: + { + xcb_xkb_state_notify_event_t *ksne = (xcb_xkb_state_notify_event_t *) ev; + nk_bindings_seat_update_mask ( xcb->bindings_seat, NULL, + ksne->baseMods, + ksne->latchedMods, + ksne->lockedMods, + ksne->baseGroup, + ksne->latchedGroup, + ksne->lockedGroup ); + rofi_view_maybe_update ( rofi_view_get_active () ); + break; + } } return G_SOURCE_CONTINUE; } @@ -868,7 +944,7 @@ static int take_pointer ( xcb_window_t w, int iters ) exit ( EXIT_FAILURE ); } xcb_grab_pointer_cookie_t cc = xcb_grab_pointer ( xcb->connection, 1, w, XCB_EVENT_MASK_BUTTON_RELEASE, - XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, w, XCB_NONE, XCB_CURRENT_TIME ); + XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, w, XCB_NONE, XCB_CURRENT_TIME ); xcb_grab_pointer_reply_t *r = xcb_grab_pointer_reply ( xcb->connection, cc, NULL ); if ( r ) { if ( r->status == XCB_GRAB_STATUS_SUCCESS ) { @@ -894,8 +970,8 @@ static int take_keyboard ( xcb_window_t w, int iters ) exit ( EXIT_FAILURE ); } xcb_grab_keyboard_cookie_t cc = xcb_grab_keyboard ( xcb->connection, - 1, w, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, - XCB_GRAB_MODE_ASYNC ); + 1, w, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, + XCB_GRAB_MODE_ASYNC ); xcb_grab_keyboard_reply_t *r = xcb_grab_keyboard_reply ( xcb->connection, cc, NULL ); if ( r ) { if ( r->status == XCB_GRAB_STATUS_SUCCESS ) { @@ -959,7 +1035,7 @@ static void x11_helper_discover_window_manager ( void ) { xcb_window_t wm_win = 0; xcb_get_property_cookie_t cc = xcb_ewmh_get_supporting_wm_check_unchecked ( &xcb->ewmh, - xcb_stuff_get_root_window () ); + xcb_stuff_get_root_window () ); if ( xcb_ewmh_get_supporting_wm_check_reply ( &xcb->ewmh, cc, &wm_win, NULL ) ) { xcb_ewmh_get_utf8_strings_reply_t wtitle; @@ -1009,7 +1085,7 @@ gboolean display_setup ( GMainLoop *main_loop, NkBindings *bindings ) TICK_N ( "Setup XCB" ); if ( xkb_x11_setup_xkb_extension ( xcb->connection, XKB_X11_MIN_MAJOR_XKB_VERSION, XKB_X11_MIN_MINOR_XKB_VERSION, - XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS, NULL, NULL, &xcb->xkb.first_event, NULL ) < 0 ) { + XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS, NULL, NULL, &xcb->xkb.first_event, NULL ) < 0 ) { g_warning ( "cannot setup XKB extension!" ); return FALSE; } @@ -1051,11 +1127,11 @@ gboolean display_setup ( GMainLoop *main_loop, NkBindings *bindings ) .stateDetails = required_state_details, }; xcb_xkb_select_events ( xcb->connection, xcb->xkb.device_id, required_events, /* affectWhich */ - 0, /* clear */ - required_events, /* selectAll */ - required_map_parts, /* affectMap */ - required_map_parts, /* map */ - &details ); + 0, /* clear */ + required_events, /* selectAll */ + required_map_parts, /* affectMap */ + required_map_parts, /* map */ + &details ); xcb->bindings_seat = nk_bindings_seat_new ( bindings, XKB_CONTEXT_NO_FLAGS ); struct xkb_keymap *keymap = xkb_x11_keymap_new_from_device ( nk_bindings_seat_get_context ( xcb->bindings_seat ), xcb->connection, xcb->xkb.device_id, XKB_KEYMAP_COMPILE_NO_FLAGS ); |