Skip to content

Commit

Permalink
rofi: Convert to XCB events
Browse files Browse the repository at this point in the history
Signed-off-by: Quentin Glidic <sardemff7+git@sardemff7.net>
  • Loading branch information
sardemff7 committed Feb 21, 2016
1 parent 2eb6345 commit fe7ca20
Show file tree
Hide file tree
Showing 12 changed files with 331 additions and 318 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
@@ -0,0 +1,3 @@
[submodule "libgwater"]
path = libgwater
url = git://github.com/sardemff7/libgwater
7 changes: 7 additions & 0 deletions Makefile.am
@@ -1,6 +1,11 @@
# Specify automake version.
AUTOMAKE_OPTIONS = 1.11.3

ACLOCAL_AMFLAGS = -I libgwater ${ACLOCAL_FLAGS}

noinst_LIBRARIES =
include $(top_srcdir)/libgwater-xcb-nolibtool.mk

##
# Rofi the program
##
Expand Down Expand Up @@ -60,6 +65,7 @@ rofi_SOURCES=\
rofi_CFLAGS=\
$(AM_CFLAGS)\
$(glib_CFLAGS)\
$(GW_XCB_CFLAGS)\
$(x11_CFLAGS)\
$(xinerama_CFLAGS)\
$(pango_CFLAGS)\
Expand All @@ -73,6 +79,7 @@ rofi_CFLAGS=\

rofi_LDADD=\
$(glib_LIBS)\
$(GW_XCB_LIBS)\
$(x11_LIBS)\
$(xinerama_LIBS)\
$(libsn_LIBS)\
Expand Down
9 changes: 8 additions & 1 deletion configure.ac
Expand Up @@ -30,6 +30,12 @@ dnl System extensions
dnl ---------------------------------------------------------------------
AC_USE_SYSTEM_EXTENSIONS

dnl ---------------------------------------------------------------------
dnl Static libraries programs
dnl ---------------------------------------------------------------------
AC_PROG_RANLIB
AM_PROG_AR

dnl ---------------------------------------------------------------------
dnl Base CFLAGS
dnl ---------------------------------------------------------------------
Expand Down Expand Up @@ -77,7 +83,8 @@ dnl ---------------------------------------------------------------------
dnl X11, Glib, Xinerama, Pango, Cairo, libstartup notification
dnl ---------------------------------------------------------------------
PKG_CHECK_MODULES([glib], [glib-2.0 >= 2.40])
PKG_CHECK_MODULES([x11], [x11])
GW_CHECK_XCB([xcb-aux xkbcommon xkbcommon-x11])
PKG_CHECK_MODULES([x11], [x11 x11-xcb])
PKG_CHECK_MODULES([xinerama], [xinerama])
PKG_CHECK_MODULES([pango], [pango pangocairo])
PKG_CHECK_MODULES([cairo], [cairo cairo-xlib])
Expand Down
1 change: 1 addition & 0 deletions include/rofi.h
@@ -1,5 +1,6 @@
#ifndef ROFI_MAIN_H
#define ROFI_MAIN_H
#include <xcb/xcb.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <glib.h>
Expand Down
2 changes: 1 addition & 1 deletion include/textbox.h
Expand Up @@ -113,7 +113,7 @@ void textbox_draw ( textbox *tb, cairo_t *draw );
*
* @returns if the key was handled (1), unhandled(0) or handled and return was pressed (-1)
*/
int textbox_keypress ( textbox *tb, XEvent *ev, char *pad, int pad_len, KeySym key, Status stat );
int textbox_keypress ( textbox *tb, xcb_key_press_event_t *ev, char *pad, int pad_len, KeySym key, Status stat );

/**
* @param tb Handle to the textbox
Expand Down
2 changes: 1 addition & 1 deletion include/view-internal.h
Expand Up @@ -69,7 +69,7 @@ struct RofiViewState

MenuFlags menu_flags;
// Handlers.
void ( *x11_event_loop )( struct RofiViewState *state, XEvent *ev );
void ( *x11_event_loop )( struct RofiViewState *state, xcb_generic_event_t *ev );
void ( *finalize )( struct RofiViewState *state );
};
/** @} */
Expand Down
2 changes: 1 addition & 1 deletion include/view.h
Expand Up @@ -47,7 +47,7 @@ void rofi_view_finalize ( RofiViewState *state );

MenuReturn rofi_view_get_return_value ( const RofiViewState *state );
unsigned int rofi_view_get_next_position ( const RofiViewState *state );
void rofi_view_itterrate ( RofiViewState *state, XEvent *event );
void rofi_view_itterrate ( RofiViewState *state, xcb_generic_event_t *event );
unsigned int rofi_view_get_completed ( const RofiViewState *state );
const char * rofi_view_get_user_input ( const RofiViewState *state );

Expand Down
1 change: 1 addition & 0 deletions libgwater
Submodule libgwater added at 50a0f8
20 changes: 20 additions & 0 deletions libgwater-xcb-nolibtool.mk
@@ -0,0 +1,20 @@
noinst_LIBRARIES += \
libgwater/libgwater-xcb.a

libgwater_libgwater_xcb_a_SOURCES = \
libgwater/xcb/libgwater-xcb.c \
libgwater/xcb/libgwater-xcb.h

libgwater_libgwater_xcb_a_CFLAGS = \
$(AM_CFLAGS) \
$(GW_XCB_INTERNAL_CFLAGS)


GW_XCB_CFLAGS = \
-I$(srcdir)/libgwater/xcb \
$(GW_XCB_INTERNAL_CFLAGS)

GW_XCB_LIBS = \
libgwater/libgwater-xcb.a \
$(GW_XCB_INTERNAL_LIBS)

82 changes: 39 additions & 43 deletions source/rofi.c
Expand Up @@ -34,9 +34,13 @@
#include <errno.h>
#include <time.h>
#include <locale.h>
#include <xcb/xcb.h>
#include <xcb/xcb_aux.h>
#include <xkbcommon/xkbcommon.h>
#include <X11/X.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xlib-xcb.h>
#include <X11/Xutil.h>
#include <X11/Xproto.h>
#include <X11/keysym.h>
Expand All @@ -45,6 +49,8 @@

#include <glib-unix.h>

#include <libgwater-xcb.h>

#define SN_API_NOT_YET_FROZEN
#include <libsn/sn.h>

Expand All @@ -63,21 +69,23 @@

gboolean daemon_mode = FALSE;
// Pidfile.
char *pidfile = NULL;
const char *cache_dir = NULL;
SnDisplay *sndisplay = NULL;
SnLauncheeContext *sncontext = NULL;
Display *display = NULL;
char *display_str = NULL;
char *config_path = NULL;
char *pidfile = NULL;
const char *cache_dir = NULL;
SnDisplay *sndisplay = NULL;
SnLauncheeContext *sncontext = NULL;
xcb_connection_t *xcb_connection = NULL;
xcb_screen_t *xcb_screen = NULL;
Display *display = NULL;
char *display_str = NULL;
char *config_path = NULL;
// Array of modi.
Mode **modi = NULL;
unsigned int num_modi = 0;
// Current selected switcher.
unsigned int curr_switcher = 0;

GMainLoop *main_loop = NULL;
GSource *main_loop_source = NULL;
GWaterXcbSource *main_loop_source = NULL;
gboolean quiet = FALSE;

static int dmenu_mode = FALSE;
Expand Down Expand Up @@ -277,19 +285,19 @@ int show_error_message ( const char *msg, int markup )
* Function that listens for global key-presses.
* This is only used when in daemon mode.
*/
static void handle_keypress ( XEvent *ev )
static void handle_keypress ( xcb_key_press_event_t *ev )
{
int index;
KeySym key = XkbKeycodeToKeysym ( display, ev->xkey.keycode, 0, 0 );
index = locate_switcher ( key, ev->xkey.state );
KeySym key = XkbKeycodeToKeysym ( display, ev->detail, 0, 0 );
index = locate_switcher ( key, ev->state );
if ( index >= 0 ) {
run_switcher ( index );
}
else {
fprintf ( stderr,
"Warning: Unhandled keypress in global keyhandler, keycode = %u mask = %u\n",
ev->xkey.keycode,
ev->xkey.state );
ev->detail,
ev->state );
}
}

Expand Down Expand Up @@ -354,7 +362,7 @@ static void cleanup ()
rofi_view_workers_finalize ();
if ( main_loop != NULL ) {
if ( main_loop_source ) {
g_source_destroy ( main_loop_source );
g_water_xcb_source_unref ( main_loop_source );
}
g_main_loop_unref ( main_loop );
main_loop = NULL;
Expand Down Expand Up @@ -542,19 +550,14 @@ static void reload_configuration ()
/**
* Process X11 events in the main-loop (gui-thread) of the application.
*/
static gboolean main_loop_x11_event_handler ( G_GNUC_UNUSED gpointer data )
static gboolean main_loop_x11_event_handler ( xcb_generic_event_t *ev, G_GNUC_UNUSED gpointer data )
{
RofiViewState *state = rofi_view_get_active ();
if ( sndisplay != NULL ) {
sn_xcb_display_process_event ( sndisplay, ev );
}
if ( state != NULL ) {
while ( XPending ( display ) ) {
XEvent ev;
// Read event, we know this won't block as we checked with XPending.
XNextEvent ( display, &ev );
if ( sndisplay != NULL ) {
sn_display_process_event ( sndisplay, &ev );
}
rofi_view_itterrate ( state, &ev );
}
rofi_view_itterrate ( state, ev );
if ( rofi_view_get_completed ( state ) ) {
// This menu is done.
rofi_view_finalize ( state );
Expand All @@ -566,24 +569,14 @@ static gboolean main_loop_x11_event_handler ( G_GNUC_UNUSED gpointer data )
}
}
}
return G_SOURCE_CONTINUE;
}
// X11 produced an event. Consume them.
while ( XPending ( display ) ) {
XEvent ev;
// Read event, we know this won't block as we checked with XPending.
XNextEvent ( display, &ev );
if ( sndisplay != NULL ) {
sn_display_process_event ( sndisplay, &ev );
}
else {
// X11 produced an event. Consume them.
// If we get an event that does not belong to a window:
// Ignore it.
if ( ev.xany.window == None ) {
continue;
}
// If keypress, handle it.
if ( ev.type == KeyPress ) {
handle_keypress ( &ev );
if ( ( ev->response_type & ~0x80 ) == XCB_KEY_PRESS ) {
handle_keypress ( (xcb_key_press_event_t *) ev );
}
}
return G_SOURCE_CONTINUE;
Expand All @@ -606,7 +599,7 @@ static gboolean main_loop_signal_handler_hup ( G_GNUC_UNUSED gpointer data )
// Grab the possibly new keybindings.
grab_global_keybindings ();
// We need to flush, otherwise the first key presses are not caught.
XFlush ( display );
xcb_flush ( xcb_connection );
return G_SOURCE_CONTINUE;
}

Expand Down Expand Up @@ -722,8 +715,9 @@ static gboolean startup ( G_GNUC_UNUSED gpointer data )
sn_launchee_context_complete ( sncontext );
}
daemon_mode = TRUE;
XSelectInput ( display, DefaultRootWindow ( display ), KeyPressMask );
XFlush ( display );
uint32_t mask[] = { XCB_EVENT_MASK_KEY_PRESS };
xcb_change_window_attributes ( xcb_connection, xcb_screen->root, XCB_CW_EVENT_MASK, mask );
xcb_flush ( xcb_connection );
}

return G_SOURCE_REMOVE;
Expand Down Expand Up @@ -807,6 +801,9 @@ int main ( int argc, char *argv[] )
}
TICK_N ( "Open Display" );

xcb_connection = XGetXCBConnection ( display );
xcb_screen = xcb_aux_get_screen ( xcb_connection, DefaultScreen ( display ) );

main_loop = g_main_loop_new ( NULL, FALSE );

TICK_N ( "Setup mainloop" );
Expand Down Expand Up @@ -856,8 +853,7 @@ int main ( int argc, char *argv[] )
}

x11_setup ( display );
main_loop_source = x11_event_source_new ( display );
x11_event_source_set_callback ( main_loop_source, main_loop_x11_event_handler );
main_loop_source = g_water_xcb_source_new_for_connection ( NULL, xcb_connection, main_loop_x11_event_handler, NULL, NULL );

TICK_N ( "X11 Setup " );

Expand Down
31 changes: 16 additions & 15 deletions source/textbox.c
Expand Up @@ -24,6 +24,7 @@
*/

#include <config.h>
#include <xcb/xcb.h>
#include <X11/X.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
Expand Down Expand Up @@ -532,7 +533,7 @@ static void textbox_cursor_del_word ( textbox *tb )
// 0 = unhandled
// 1 = handled
// -1 = handled and return pressed (finished)
int textbox_keypress ( textbox *tb, XEvent *ev, char *pad, int pad_len, KeySym key, Status stat )
int textbox_keypress ( textbox *tb, xcb_key_press_event_t *ev, char *pad, int pad_len, KeySym key, Status stat )
{
if ( !( tb->flags & TB_EDITABLE ) ) {
return 0;
Expand All @@ -541,67 +542,67 @@ int textbox_keypress ( textbox *tb, XEvent *ev, char *pad, int pad_len, KeySym k
tb->blink = 2;
if ( stat == XLookupKeySym || stat == XLookupBoth ) {
// Left or Ctrl-b
if ( abe_test_action ( MOVE_CHAR_BACK, ev->xkey.state, key ) ) {
if ( abe_test_action ( MOVE_CHAR_BACK, ev->state, key ) ) {
textbox_cursor_dec ( tb );
return 2;
}
// Right or Ctrl-F
else if ( abe_test_action ( MOVE_CHAR_FORWARD, ev->xkey.state, key ) ) {
else if ( abe_test_action ( MOVE_CHAR_FORWARD, ev->state, key ) ) {
textbox_cursor_inc ( tb );
return 2;
}

// Ctrl-U: Kill from the beginning to the end of the line.
else if ( abe_test_action ( CLEAR_LINE, ev->xkey.state, key ) ) {
else if ( abe_test_action ( CLEAR_LINE, ev->state, key ) ) {
textbox_text ( tb, "" );
return 1;
}
// Ctrl-A
else if ( abe_test_action ( MOVE_FRONT, ev->xkey.state, key ) ) {
else if ( abe_test_action ( MOVE_FRONT, ev->state, key ) ) {
textbox_cursor ( tb, 0 );
return 2;
}
// Ctrl-E
else if ( abe_test_action ( MOVE_END, ev->xkey.state, key ) ) {
else if ( abe_test_action ( MOVE_END, ev->state, key ) ) {
textbox_cursor_end ( tb );
return 2;
}
// Ctrl-Alt-h
else if ( abe_test_action ( REMOVE_WORD_BACK, ev->xkey.state, key ) ) {
else if ( abe_test_action ( REMOVE_WORD_BACK, ev->state, key ) ) {
textbox_cursor_bkspc_word ( tb );
return 1;
}
// Ctrl-Alt-d
else if ( abe_test_action ( REMOVE_WORD_FORWARD, ev->xkey.state, key ) ) {
else if ( abe_test_action ( REMOVE_WORD_FORWARD, ev->state, key ) ) {
textbox_cursor_del_word ( tb );
return 1;
} // Delete or Ctrl-D
else if ( abe_test_action ( REMOVE_CHAR_FORWARD, ev->xkey.state, key ) ) {
else if ( abe_test_action ( REMOVE_CHAR_FORWARD, ev->state, key ) ) {
textbox_cursor_del ( tb );
return 1;
}
// Alt-B
else if ( abe_test_action ( MOVE_WORD_BACK, ev->xkey.state, key ) ) {
else if ( abe_test_action ( MOVE_WORD_BACK, ev->state, key ) ) {
textbox_cursor_dec_word ( tb );
return 2;
}
// Alt-F
else if ( abe_test_action ( MOVE_WORD_FORWARD, ev->xkey.state, key ) ) {
else if ( abe_test_action ( MOVE_WORD_FORWARD, ev->state, key ) ) {
textbox_cursor_inc_word ( tb );
return 2;
}
// BackSpace, Ctrl-h
else if ( abe_test_action ( REMOVE_CHAR_BACK, ev->xkey.state, key ) ) {
else if ( abe_test_action ( REMOVE_CHAR_BACK, ev->state, key ) ) {
textbox_cursor_bkspc ( tb );
return 1;
}
else if ( abe_test_action ( ACCEPT_CUSTOM, ev->xkey.state, key ) ) {
else if ( abe_test_action ( ACCEPT_CUSTOM, ev->state, key ) ) {
return -2;
}
else if ( abe_test_action ( ACCEPT_ENTRY_CONTINUE, ev->xkey.state, key ) ) {
else if ( abe_test_action ( ACCEPT_ENTRY_CONTINUE, ev->state, key ) ) {
return -3;
}
else if ( abe_test_action ( ACCEPT_ENTRY, ev->xkey.state, key ) ) {
else if ( abe_test_action ( ACCEPT_ENTRY, ev->state, key ) ) {
return -1;
}
}
Expand Down

0 comments on commit fe7ca20

Please sign in to comment.