Merge pull request #24 from glyptodon/terminal-scrollbar

GUAC-803: Add mouse support to scrollbar
This commit is contained in:
James Muehlner 2015-02-04 19:21:58 -08:00
commit 4ab6466226
7 changed files with 413 additions and 23 deletions

View File

@ -34,6 +34,7 @@ noinst_HEADERS = \
cursor.h \
display.h \
ibar.h \
pointer.h \
scrollbar.h \
terminal.h \
terminal_handlers.h \
@ -47,6 +48,7 @@ libguac_terminal_la_SOURCES = \
cursor.c \
display.c \
ibar.c \
pointer.c \
scrollbar.c \
terminal.c \
terminal_handlers.c

94
src/terminal/pointer.c Normal file
View File

@ -0,0 +1,94 @@
/*
* Copyright (C) 2015 Glyptodon LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "config.h"
#include "cursor.h"
#include <cairo/cairo.h>
#include <guacamole/client.h>
#include <guacamole/protocol.h>
#include <guacamole/socket.h>
/* Macros for prettying up the embedded image. */
#define X 0x00,0x00,0x00,0xFF
#define U 0x80,0x80,0x80,0xFF
#define O 0xFF,0xFF,0xFF,0xFF
#define _ 0x00,0x00,0x00,0x00
/* Dimensions */
const int guac_terminal_pointer_width = 11;
const int guac_terminal_pointer_height = 16;
/* Format */
const cairo_format_t guac_terminal_pointer_format = CAIRO_FORMAT_ARGB32;
const int guac_terminal_pointer_stride = 44;
/* Embedded pointer graphic */
unsigned char guac_terminal_pointer[] = {
O,_,_,_,_,_,_,_,_,_,_,
O,O,_,_,_,_,_,_,_,_,_,
O,X,O,_,_,_,_,_,_,_,_,
O,X,X,O,_,_,_,_,_,_,_,
O,X,X,X,O,_,_,_,_,_,_,
O,X,X,X,X,O,_,_,_,_,_,
O,X,X,X,X,X,O,_,_,_,_,
O,X,X,X,X,X,X,O,_,_,_,
O,X,X,X,X,X,X,X,O,_,_,
O,X,X,X,X,X,X,X,X,O,_,
O,X,X,X,X,X,O,O,O,O,O,
O,X,X,O,X,X,O,_,_,_,_,
O,X,O,_,O,X,X,O,_,_,_,
O,O,_,_,O,X,X,O,_,_,_,
O,_,_,_,_,O,X,X,O,_,_,
_,_,_,_,_,O,O,O,O,_,_
};
guac_terminal_cursor* guac_terminal_create_pointer(guac_client* client) {
guac_socket* socket = client->socket;
guac_terminal_cursor* cursor = guac_terminal_cursor_alloc(client);
/* Draw to buffer */
cairo_surface_t* graphic = cairo_image_surface_create_for_data(
guac_terminal_pointer,
guac_terminal_pointer_format,
guac_terminal_pointer_width,
guac_terminal_pointer_height,
guac_terminal_pointer_stride);
guac_protocol_send_png(socket, GUAC_COMP_SRC, cursor->buffer,
0, 0, graphic);
cairo_surface_destroy(graphic);
/* Initialize cursor properties */
cursor->width = guac_terminal_pointer_width;
cursor->height = guac_terminal_pointer_height;
cursor->hotspot_x = 0;
cursor->hotspot_y = 0;
return cursor;
}

68
src/terminal/pointer.h Normal file
View File

@ -0,0 +1,68 @@
/*
* Copyright (C) 2015 Glyptodon LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef GUAC_TERMINAL_POINTER_H
#define GUAC_TERMINAL_POINTER_H
#include "config.h"
#include <cairo/cairo.h>
#include <guacamole/client.h>
/**
* Width of the embedded mouse cursor graphic.
*/
extern const int guac_terminal_pointer_width;
/**
* Height of the embedded mouse cursor graphic.
*/
extern const int guac_terminal_pointer_height;
/**
* Number of bytes in each row of the embedded mouse cursor graphic.
*/
extern const int guac_terminal_pointer_stride;
/**
* The Cairo grapic format of the mouse cursor graphic.
*/
extern const cairo_format_t guac_terminal_pointer_format;
/**
* Embedded mouse cursor graphic.
*/
extern unsigned char guac_terminal_pointer[];
/**
* Creates a new pointer cursor, returning the corresponding cursor object.
*
* @param client
* The guac_client to send the cursor to.
*
* @return
* A new cursor which must be free'd via guac_terminal_cursor_free().
*/
guac_terminal_cursor* guac_terminal_create_pointer(guac_client* client);
#endif

View File

@ -67,6 +67,9 @@ guac_terminal_scrollbar* guac_terminal_scrollbar_alloc(guac_client* client,
scrollbar->container = guac_client_alloc_layer(client);
scrollbar->handle = guac_client_alloc_layer(client);
/* Init mouse event state tracking */
scrollbar->dragging_handle = 0;
/* Reposition and resize to fit parent */
guac_terminal_scrollbar_parent_resized(scrollbar,
parent_width, parent_height, visible_area);
@ -87,19 +90,29 @@ void guac_terminal_scrollbar_free(guac_terminal_scrollbar* scrollbar) {
}
/**
* Calculates the render state of the scroll bar, given its minimum, maximum,
* and current values. This render state will not be reflected graphically
* unless the scrollbar is flushed.
* Calculates the state of the scroll bar, given its minimum, maximum, current
* values, and the state of any dragging operation. The resulting render state
* will not be reflected graphically unless the scrollbar is flushed, and any
* resulting value will not be assigned to the scrollbar unless explicitly set
* with guac_terminal_scrollbar_set_value().
*
* @param scrollbar
* The scrollbar whose render state should be calculated.
* The scrollbar whose state should be calculated.
*
* @param render_state
* A pointer to an existing guac_terminal_scrollbar_render_state that will
* be populated with the calculated result.
*
* @param value
* A pointer to an existing int that will be populated with the updated
* scrollbar value.
*/
static void calculate_render_state(guac_terminal_scrollbar* scrollbar,
guac_terminal_scrollbar_render_state* render_state) {
static void calculate_state(guac_terminal_scrollbar* scrollbar,
guac_terminal_scrollbar_render_state* render_state,
int* value) {
/* Use unchanged current value by default */
*value = scrollbar->value;
/* Calculate container dimensions */
render_state->container_width = GUAC_TERMINAL_SCROLLBAR_WIDTH;
@ -141,15 +154,44 @@ static void calculate_render_state(guac_terminal_scrollbar* scrollbar,
if (render_state->handle_height > max_handle_height)
render_state->handle_height = max_handle_height;
/* Calculate handle position */
/* Calculate handle X position */
render_state->handle_x = GUAC_TERMINAL_SCROLLBAR_PADDING;
/* Calculate handle Y range */
int min_handle_y = GUAC_TERMINAL_SCROLLBAR_PADDING;
int max_handle_y = min_handle_y + max_handle_height
- render_state->handle_height;
/* Position handle relative to mouse if being dragged */
if (scrollbar->dragging_handle) {
int dragged_handle_y = scrollbar->drag_current_y
- scrollbar->drag_offset_y;
/* Keep handle within bounds */
if (dragged_handle_y < min_handle_y)
dragged_handle_y = min_handle_y;
else if (dragged_handle_y > max_handle_y)
dragged_handle_y = max_handle_y;
render_state->handle_y = dragged_handle_y;
/* Calculate scrollbar value */
if (max_handle_y > min_handle_y) {
*value = scrollbar->min
+ (dragged_handle_y - min_handle_y)
* scroll_delta
/ (max_handle_y - min_handle_y);
}
}
/* Handle Y position is relative to current scroll value */
if (scroll_delta > 0)
render_state->handle_y = GUAC_TERMINAL_SCROLLBAR_PADDING
+ (max_handle_height - render_state->handle_height)
* (scrollbar->value - scrollbar->min)
/ scroll_delta;
else if (scroll_delta > 0)
render_state->handle_y = min_handle_y
+ (max_handle_y - min_handle_y)
* (scrollbar->value - scrollbar->min)
/ scroll_delta;
/* ... unless there is only one possible scroll value */
else
@ -161,12 +203,18 @@ void guac_terminal_scrollbar_flush(guac_terminal_scrollbar* scrollbar) {
guac_socket* socket = scrollbar->client->socket;
/* Get old render state */
/* Get old state */
int old_value = scrollbar->value;
guac_terminal_scrollbar_render_state* old_state = &scrollbar->render_state;
/* Calculate new render state */
/* Calculate new state */
int new_value;
guac_terminal_scrollbar_render_state new_state;
calculate_render_state(scrollbar, &new_state);
calculate_state(scrollbar, &new_state, &new_value);
/* Notify of scroll if value is changing */
if (new_value != old_value && scrollbar->scroll_handler)
scrollbar->scroll_handler(scrollbar, new_value);
/* Reposition container if moved */
if (old_state->container_x != new_state.container_x
@ -278,3 +326,53 @@ void guac_terminal_scrollbar_parent_resized(guac_terminal_scrollbar* scrollbar,
}
int guac_terminal_scrollbar_handle_mouse(guac_terminal_scrollbar* scrollbar,
int x, int y, int mask) {
/* Get container rectangle bounds */
int parent_left = scrollbar->render_state.container_x;
int parent_top = scrollbar->render_state.container_y;
int parent_right = parent_left + scrollbar->render_state.container_width;
int parent_bottom = parent_top + scrollbar->render_state.container_height;
/* Calculate handle rectangle bounds */
int handle_left = parent_left + scrollbar->render_state.handle_x;
int handle_top = parent_top + scrollbar->render_state.handle_y;
int handle_right = handle_left + scrollbar->render_state.handle_width;
int handle_bottom = handle_top + scrollbar->render_state.handle_height;
/* Handle click on handle */
if (scrollbar->dragging_handle) {
/* Update drag while mouse button is held */
if (mask & GUAC_CLIENT_MOUSE_LEFT)
scrollbar->drag_current_y = y;
/* Stop drag if mouse button is released */
else
scrollbar->dragging_handle = 0;
/* Mouse event was handled by scrollbar */
return 1;
}
else if (mask == GUAC_CLIENT_MOUSE_LEFT
&& x >= handle_left && x < handle_right
&& y >= handle_top && y < handle_bottom) {
/* Start drag */
scrollbar->dragging_handle = 1;
scrollbar->drag_offset_y = y - handle_top;
scrollbar->drag_current_y = y;
/* Mouse event was handled by scrollbar */
return 1;
}
/* Eat any events that occur within the scrollbar */
return x >= parent_left && x < parent_right
&& y >= parent_top && y < parent_bottom;
}

View File

@ -96,12 +96,21 @@ typedef struct guac_terminal_scrollbar_render_state {
} guac_terminal_scrollbar_render_state;
typedef struct guac_terminal_scrollbar guac_terminal_scrollbar;
/**
* Handler which is called whenever the scrollbar value changes outside a call
* to guac_terminal_scrollbar_set_value().
*/
typedef void guac_terminal_scrollbar_scroll_handler(
guac_terminal_scrollbar* scrollbar, int value);
/**
* A scrollbar, made up of a containing layer and inner draggable handle. The
* position of the handle within the layer represents the value of the
* scrollbar.
*/
typedef struct guac_terminal_scrollbar {
struct guac_terminal_scrollbar {
/**
* The client associated with this scrollbar.
@ -159,7 +168,36 @@ typedef struct guac_terminal_scrollbar {
*/
guac_terminal_scrollbar_render_state render_state;
} guac_terminal_scrollbar;
/**
* Whether the scrollbar handle is currently being dragged.
*/
int dragging_handle;
/**
* The offset of the Y location of the mouse pointer when the dragging
* began, relative to the top of the scrollbar handle. If dragging is not
* in progress, this value is undefined.
*/
int drag_offset_y;
/**
* The current Y location of the mouse pointer if dragging is in progress.
* If dragging is not in progress, this value is undefined.
*/
int drag_current_y;
/**
* The function to call when the scrollbar handle is being dragged, and
* the new scrollbar value needs to be handled and assigned.
*/
guac_terminal_scrollbar_scroll_handler* scroll_handler;
/**
* Arbitrary reference to data related to this scrollbar.
*/
void* data;
};
/**
* Allocates a new scrollbar, associating that scrollbar with the given client
@ -276,4 +314,29 @@ void guac_terminal_scrollbar_set_value(guac_terminal_scrollbar* scrollbar,
void guac_terminal_scrollbar_parent_resized(guac_terminal_scrollbar* scrollbar,
int parent_width, int parent_height, int visible_area);
/**
* Notifies the scrollbar of the current mouse state, allowing it to update
* itself with respect to button state and dragging.
*
* @param scrollbar
* The scrollbar to notify of the current mouse state.
*
* @param x
* The X coordinate of the mouse pointer.
*
* @param y
* The Y coordinate of the mouse pointer.
*
* @param mask
* The button mask, where the Nth bit of the button mask represents the
* pressed state of the Nth mouse button, where button 0 is the left
* mouse button, button 1 is the middle button, etc.
*
* @return
* Zero if the mouse event was not handled by the scrollbar, non-zero
* otherwise.
*/
int guac_terminal_scrollbar_handle_mouse(guac_terminal_scrollbar* scrollbar,
int x, int y, int mask);
#endif

View File

@ -29,6 +29,7 @@
#include "display.h"
#include "ibar.h"
#include "guac_clipboard.h"
#include "pointer.h"
#include "scrollbar.h"
#include "terminal.h"
#include "terminal_handlers.h"
@ -264,6 +265,10 @@ guac_terminal* guac_terminal_create(guac_client* client,
term->scrollbar = guac_terminal_scrollbar_alloc(term->client,
GUAC_DEFAULT_LAYER, width, height, term->term_height);
/* Associate scrollbar with this terminal */
term->scrollbar->data = term;
term->scrollbar->scroll_handler = guac_terminal_scroll_handler;
/* Init terminal */
guac_terminal_reset(term);
@ -272,8 +277,9 @@ guac_terminal* guac_terminal_create(guac_client* client,
term->mod_shift = 0;
/* Set up mouse cursors */
term->ibar_cursor = guac_terminal_create_ibar(client);
term->blank_cursor = guac_terminal_create_blank(client);
term->pointer_cursor = guac_terminal_create_pointer(client);
term->ibar_cursor = guac_terminal_create_ibar(client);
term->blank_cursor = guac_terminal_create_blank(client);
/* Initialize mouse cursor */
term->current_cursor = term->blank_cursor;
@ -309,6 +315,7 @@ void guac_terminal_free(guac_terminal* term) {
guac_terminal_scrollbar_free(term->scrollbar);
/* Free cursors */
guac_terminal_cursor_free(term->client, term->pointer_cursor);
guac_terminal_cursor_free(term->client, term->ibar_cursor);
guac_terminal_cursor_free(term->client, term->blank_cursor);
@ -1379,17 +1386,39 @@ int guac_terminal_send_key(guac_terminal* term, int keysym, int pressed) {
static int __guac_terminal_send_mouse(guac_terminal* term, int x, int y, int mask) {
guac_client* client = term->client;
guac_socket* socket = client->socket;
/* Determine which buttons were just released and pressed */
int released_mask = term->mouse_mask & ~mask;
int pressed_mask = ~term->mouse_mask & mask;
/* Notify scrollbar, do not handle anything handled by scrollbar */
if (guac_terminal_scrollbar_handle_mouse(term->scrollbar, x, y, mask)) {
/* Set pointer cursor if mouse is over scrollbar */
if (term->current_cursor != term->pointer_cursor) {
term->current_cursor = term->pointer_cursor;
guac_terminal_set_cursor(client, term->pointer_cursor);
}
/* Flush scrollbar */
guac_terminal_scrollbar_flush(term->scrollbar);
guac_protocol_send_sync(socket, client->last_sent_timestamp);
guac_socket_flush(socket);
return 0;
}
term->mouse_mask = mask;
/* Show mouse cursor if not already shown */
if (term->current_cursor != term->ibar_cursor) {
term->current_cursor = term->ibar_cursor;
guac_terminal_set_cursor(term->client, term->ibar_cursor);
guac_socket_flush(term->client->socket);
guac_terminal_set_cursor(client, term->ibar_cursor);
guac_protocol_send_sync(socket, client->last_sent_timestamp);
guac_socket_flush(socket);
}
/* Paste contents of clipboard on right or middle mouse button up */
@ -1417,8 +1446,8 @@ static int __guac_terminal_send_mouse(guac_terminal* term, int x, int y, int mas
free(string);
/* Send data */
guac_common_clipboard_send(term->clipboard, term->client);
guac_socket_flush(term->client->socket);
guac_common_clipboard_send(term->clipboard, client);
guac_socket_flush(socket);
}
@ -1461,6 +1490,24 @@ int guac_terminal_send_mouse(guac_terminal* term, int x, int y, int mask) {
}
void guac_terminal_scroll_handler(guac_terminal_scrollbar* scrollbar, int value) {
guac_terminal* terminal = (guac_terminal*) scrollbar->data;
/* Calculate change in scroll offset */
int delta = -value - terminal->scroll_offset;
/* Update terminal based on change in scroll offset */
if (delta < 0)
guac_terminal_scroll_display_down(terminal, -delta);
else if (delta > 0)
guac_terminal_scroll_display_up(terminal, delta);
/* Update scrollbar value */
guac_terminal_scrollbar_set_value(scrollbar, value);
}
void guac_terminal_clipboard_reset(guac_terminal* term, const char* mimetype) {
guac_common_clipboard_reset(term->clipboard, mimetype);
}

View File

@ -307,6 +307,11 @@ struct guac_terminal {
*/
int mouse_mask;
/**
* The cached pointer cursor.
*/
guac_terminal_cursor* pointer_cursor;
/**
* The cached I-bar cursor.
*/
@ -388,6 +393,19 @@ int guac_terminal_send_key(guac_terminal* term, int keysym, int pressed);
*/
int guac_terminal_send_mouse(guac_terminal* term, int x, int y, int mask);
/**
* Handles a scroll event received from the scrollbar associated with a
* terminal.
*
* @param scrollbar
* The scrollbar that has been scrolled.
*
* @param value
* The new value that should be stored within the scrollbar, and
* represented within the terminal display.
*/
void guac_terminal_scroll_handler(guac_terminal_scrollbar* scrollbar, int value);
/**
* Clears the current clipboard contents and sets the mimetype for future
* contents.