Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to change tools on the command line #189

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,17 @@ Usage:
will undo the last drawing stroke (or "-z")
gromit-mpx --redo
will redo the last undone drawing stroke (or "-y")
gromit-mpx --change-tool <definition>
pascal-niklaus marked this conversation as resolved.
Show resolved Hide resolved
will redefine a tool, using the same syntax as in the .cfg file (or "-T")
e.g. gromit-mpx --change-tool '"default"=RECT(color="yellow" size=2)'
gromit-mpx --change-attribute <definition>
pascal-niklaus marked this conversation as resolved.
Show resolved Hide resolved
will change one or several attributes of a tool, keeping the others
as they were. This can be used to change e.g. to color or type of a tool (or "-A")
e.g. gromit-mpx --change-attribute '"default"=(color="cyan")'
gromit-mpx --change-attribute '"default"=LINE'
gromit-mpx --line <startX> <startY> <endX> <endY> <color> <thickness>
will draw a straight line with characteristics specified by the arguments (or "-l")
eg: gromit-mpx -l 200 200 400 400 '#C4A7E7' 6
e.g.: gromit-mpx -l 200 200 400 400 '#C4A7E7' 6

If activated Gromit-MPX prevents you from using other programs with the
mouse. You can press the button and paint on the screen. Key presses
Expand Down
12 changes: 11 additions & 1 deletion gromit-mpx.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.\" Hey, vim: ft=nroff
.TH GROMIT-MPX 1 "November 3, 2018"
.TH GROMIT-MPX 1 "March 31, 2024"
.\" Please adjust this date whenever revising the manpage.
.\"
.\" Some roff macros, for reference:
Expand Down Expand Up @@ -101,6 +101,16 @@ will cause the main Gromit-MPX process to quit.
.B \-t, \-\-toggle
will toggle the grabbing of the cursor.
.TP
.B \-T <definition>, \-\-change-tool <definition>
bk138 marked this conversation as resolved.
Show resolved Hide resolved
will change the definition of a tool. The syntax is as in the .cfg file
read at startup, except for the trailing semicolon.
bk138 marked this conversation as resolved.
Show resolved Hide resolved
(example: gromit-mpx -T '"default"=RECT(color="yellow" size=2)'
.TP
.B \-A <definition>, \-\-change-attribute <definition>
will change one or several attributes of a tool, keeping the other settings.
(example 1: change just the color: gromit-mpx -A '"default"=(color="yellow")';
example 2: change tool type, keeping color, size etc: gromit-mpx -A '"default"=LINE')
.TP
.B \-v, \-\-visibility
will toggle the visibility of the window.
.TP
Expand Down
190 changes: 172 additions & 18 deletions src/callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,9 @@ void on_monitors_changed ( GdkScreen *screen,

parse_config(data); // also calls paint_context_new() :-(


data->default_pen = paint_context_new (data, GROMIT_PEN, data->red, 7, 0, GROMIT_ARROW_END,
data->default_pen = paint_context_new (data, GROMIT_PEN, gdk_rgba_copy(data->red), 7, 0, GROMIT_ARROW_END,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still have the allocated GdkRGBA here which should be freed.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pascal-niklaus what do you think about those?

5, 10, 15, 25, 1, 0, G_MAXUINT);
data->default_eraser = paint_context_new (data, GROMIT_ERASER, data->red, 75, 0, GROMIT_ARROW_END,
data->default_eraser = paint_context_new (data, GROMIT_ERASER, gdk_rgba_copy(data->red), 75, 0, GROMIT_ARROW_END,
5, 10, 15, 25, 1, 0, G_MAXUINT);

if(!data->composited) // set shape
Expand Down Expand Up @@ -211,7 +210,10 @@ void on_clientapp_selection_get (GtkWidget *widget,
g_printerr("DEBUG: clientapp received request.\n");


if (gtk_selection_data_get_target(selection_data) == GA_TOGGLEDATA || gtk_selection_data_get_target(selection_data) == GA_LINEDATA)
if (gtk_selection_data_get_target(selection_data) == GA_TOGGLEDATA ||
gtk_selection_data_get_target(selection_data) == GA_LINEDATA ||
gtk_selection_data_get_target(selection_data) == GA_CHGTOOLDATA ||
gtk_selection_data_get_target(selection_data) == GA_CHGATTRDATA)
{
ans = data->clientdata;
}
Expand Down Expand Up @@ -398,9 +400,8 @@ gboolean on_motion (GtkWidget *win,
}
if (type == GROMIT_LINE)
{
GromitArrowType atype = devdata->cur_context->arrow_type;
draw_line (data, ev->device, devdata->lastx, devdata->lasty, ev->x, ev->y);
if (devdata->cur_context->arrowsize > 0)
if (devdata->cur_context->arrowsize > 0.0)
bk138 marked this conversation as resolved.
Show resolved Hide resolved
{
GromitArrowType atype = devdata->cur_context->arrow_type;
gint width = devdata->cur_context->arrowsize * devdata->cur_context->width / 2;
Expand Down Expand Up @@ -528,7 +529,7 @@ void on_mainapp_selection_get (GtkWidget *widget,
gpointer user_data)
{
GromitData *data = (GromitData *) user_data;

gchar *uri = "OK";
GdkAtom action = gtk_selection_data_get_target(selection_data);

Expand Down Expand Up @@ -558,10 +559,20 @@ void on_mainapp_selection_get (GtkWidget *widget,
undo_drawing (data);
else if (action == GA_REDO)
redo_drawing (data);
else if (action == GA_CHGTOOL)
{
gtk_selection_convert(data->win, GA_DATA, GA_CHGTOOLDATA, time);
gtk_main();
}
else if (action == GA_CHGATTR)
{
gtk_selection_convert(data->win, GA_DATA, GA_CHGATTRDATA, time);
gtk_main();
}
else
uri = "NOK";


gtk_selection_data_set (selection_data,
gtk_selection_data_get_target(selection_data),
8, (guchar*)uri, strlen (uri));
Expand All @@ -574,6 +585,7 @@ void on_mainapp_selection_received (GtkWidget *widget,
gpointer user_data)
{
GromitData *data = (GromitData *) user_data;
gchar *name = NULL;

if(gtk_selection_data_get_length(selection_data) < 0)
{
Expand All @@ -585,35 +597,35 @@ void on_mainapp_selection_received (GtkWidget *widget,
if(gtk_selection_data_get_target(selection_data) == GA_TOGGLEDATA )
{
intptr_t dev_nr = strtoull((gchar*)gtk_selection_data_get_data(selection_data), NULL, 10);

if(data->debug)
g_printerr("DEBUG: mainapp got toggle id '%ld' back from client.\n", (long)dev_nr);

if(dev_nr < 0)
toggle_grab(data, NULL); /* toggle all */
else
else
{
/* find dev numbered dev_nr */
GHashTableIter it;
gpointer value;
GromitDeviceData* devdata = NULL;
GromitDeviceData* devdata = NULL;
g_hash_table_iter_init (&it, data->devdatatable);
while (g_hash_table_iter_next (&it, NULL, &value))
while (g_hash_table_iter_next (&it, NULL, &value))
{
devdata = value;
if(devdata->index == dev_nr)
break;
else
devdata = NULL;
}

if(devdata)
toggle_grab(data, devdata->device);
else
g_printerr("ERROR: No device at index %ld.\n", (long)dev_nr);
}
}
else if (gtk_selection_data_get_target(selection_data) == GA_LINEDATA)
else if (gtk_selection_data_get_target(selection_data) == GA_LINEDATA)
{

gchar** line_args = g_strsplit((gchar*)gtk_selection_data_get_data(selection_data), " ", 6);
Expand All @@ -624,7 +636,7 @@ void on_mainapp_selection_received (GtkWidget *widget,
gchar* hex_code = line_args[4];
int thickness = atoi(line_args[5]);

if(data->debug)
if(data->debug)
{
g_printerr("DEBUG: mainapp got line data back from client:\n");
g_printerr("startX startY endX endY: %d %d %d %d\n", startX, startY, endX, endY);
Expand Down Expand Up @@ -662,14 +674,156 @@ void on_mainapp_selection_received (GtkWidget *widget,
cairo_stroke(line_ctx->paint_ctx);

data->modified = 1;
gdk_window_invalidate_rect(gtk_widget_get_window(data->win), &rect, 0);
gdk_window_invalidate_rect(gtk_widget_get_window(data->win), &rect, 0);
data->painted = 1;

g_free(line_ctx);
g_free (color);
}
}
else
{
GdkAtom atom = gtk_selection_data_get_target(selection_data);
if (atom == GA_CHGTOOLDATA || atom == GA_CHGATTRDATA)
{
gchar *a = (gchar *)gtk_selection_data_get_data(selection_data);
if(data->debug) g_printerr("DEBUG: define tool: %s\n", a);

GScanner *scanner = g_scanner_new(NULL);
scanner_init(scanner);
g_scanner_input_text(scanner, a, strlen(a));

GTokenType token = g_scanner_get_next_token (scanner);

GromitPaintContext style;
style.paint_color = NULL;

if (token != G_TOKEN_STRING || ! (name = parse_name (scanner)))
{
g_printerr("Could not parse tool in expression '%s'!\n", a);
goto cleanup;
}

GromitPaintContext *context = g_hash_table_lookup(data->tool_config, name);
if (context == NULL)
{
g_printerr("Cannot change tool '%s' because it does not exist...\n", name);
goto cleanup;
}

if (atom == GA_CHGATTRDATA)
{
if (g_scanner_cur_token(scanner) != G_TOKEN_EQUAL_SIGN)
{
g_printerr("Expected equal sign after tool name...\n");
goto cleanup;
}
token = g_scanner_get_next_token(scanner);
if (token == G_TOKEN_SYMBOL)
{
context->type = (GromitPaintType) scanner->value.v_symbol;
token = g_scanner_get_next_token (scanner);
}
if (token == G_TOKEN_LEFT_PAREN)
{
style.paint_color = g_malloc(sizeof(GdkRGBA));
*style.paint_color = *data->red;
scanner->scope_id = 2;
while (token != G_TOKEN_RIGHT_PAREN)
{
token = g_scanner_get_next_token(scanner);
if (token == G_TOKEN_SYMBOL)
{
switch (parse_attribute(scanner, &style))
{
case SYM_SIZE:
context->width = style.width;
cairo_set_line_width(context->paint_ctx, style.width);
break;
case SYM_COLOR:
*context->paint_color = *style.paint_color;
cairo_set_source_rgba(context->paint_ctx,
style.paint_color->red,
style.paint_color->green,
style.paint_color->blue,
style.paint_color->alpha);
break;
case SYM_ARROWSIZE:
context->arrowsize = style.arrowsize;
break;
case SYM_ARROWTYPE:
context->arrow_type = style.arrow_type;
break;
case SYM_MINSIZE:
context->minwidth = style.minwidth;
break;
case SYM_MAXSIZE:
context->maxwidth = style.maxwidth;
break;
case SYM_MINLEN:
context->minlen = style.minlen;
break;
case SYM_MAXANGLE:
context->maxangle = style.maxangle;
break;
case SYM_RADIUS:
context->radius = style.radius;
break;
case SYM_SIMPLIFY:
context->simplify = style.simplify;
break;
case SYM_SNAP:
context->snapdist = style.snapdist;
break;
case SYM_ERROR:
break;
}
}
}
if (g_scanner_get_next_token(scanner) != G_TOKEN_EOF)
g_printerr("WARNING: skipping extra content after ')' !\n");
}
}
else
{
if (!parse_tool(data, scanner, &style)) goto cleanup;
if (g_scanner_cur_token(scanner) != G_TOKEN_LEFT_PAREN) goto cleanup;
if (! parse_style(scanner, &style)) goto cleanup;
if (g_scanner_cur_token(scanner) != G_TOKEN_EOF) goto cleanup;

context->type = style.type;
context->width = style.width;
context->arrowsize = style.arrowsize;
context->arrow_type = style.arrow_type;
context->minwidth = style.minwidth;
context->maxwidth = style.maxwidth;
context->radius = style.radius;
context->minlen = style.minlen;
context->maxangle = style.maxangle;
context->simplify = style.simplify;
context->snapdist = style.snapdist;
*context->paint_color = *style.paint_color;

cairo_set_source_rgba(context->paint_ctx,
style.paint_color->red,
style.paint_color->green,
style.paint_color->blue,
style.paint_color->alpha);
cairo_set_line_width(context->paint_ctx, style.width);
}
if (g_scanner_get_next_token (scanner) != G_TOKEN_EOF)
{
g_printerr("WARNING: skipping extra content tool definition!\n");
}

cleanup:
if (style.paint_color) g_free(style.paint_color);
g_scanner_destroy (scanner);
} // if (tool change) ...
}
}


if (name) g_free(name);

gtk_main_quit ();
}

Expand Down
Loading
Loading