Skip to content

Commit

Permalink
implement pager
Browse files Browse the repository at this point in the history
  • Loading branch information
LBCrion committed Dec 28, 2020
1 parent 7fa3036 commit 6dacbde
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 7 deletions.
4 changes: 4 additions & 0 deletions config/sfwbar.config
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
"type":"label",
"expand":false
},
{
"type":"pager",
"rows":2
},
{
"type": "button",
"action": "firefox",
Expand Down
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ src = ['src/sfwbar.c',
'src/scanner.c',
'src/expr.c',
'src/misc.c',
'src/pager.c',
'src/clampgrid.c',
'src/parser.tab.c',
'src/lex.yy.c']
Expand Down
8 changes: 7 additions & 1 deletion sfwbar.1
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ each item in the array is a widget. The following widget types are supported:
.B taskbar
this is a special widget displaying a list of all floating windows.
.TP
.B pager
this is a special widget display a list of all workspaces.
.TP
.B grid
a layout grid used to fine tune placement of widgets. You can use these to
further subdivide each cell of the main grid and arrange items therein.
Expand Down Expand Up @@ -96,6 +99,9 @@ Defaults to "horizontal".
Add children to widget. Applicable to "grid" only. Syntax is the same as
for the main "layout".
.TP
.B title
[true|false]
An indicator whether to display an application title within the taskbar.
.B icon
[string]|[true|false]
A name of an icon to display in a button or an indicator whether to display
Expand All @@ -107,7 +113,7 @@ Specify the size of the icon in pixels (for button or taskbar)
.TP
.B rows
[number]
Specify number of rows in a taskbar
Specify number of rows in a taskbar or a pager
.SH EXPRESSIONS
Values in widgets can contain basic arithmetic and string manipulation
expressions. Each numeric scan variable contains four values
Expand Down
2 changes: 1 addition & 1 deletion src/ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ int ipc_send ( int sock, gint32 type, gchar *command )

int ipc_subscribe ( int sock )
{
if ( ipc_send(sock, 2, "['window']") == -1)
if ( ipc_send(sock, 2, "['workspace','window']") == -1)
return -1;

return sock;
Expand Down
8 changes: 7 additions & 1 deletion src/layout.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ void layout_config_iter ( struct context *context, json_object *obj, GtkWidget *
if(context->tb_rows<1)
context->tb_rows = 1;
widget = taskbar_init(context);

}
if(g_strcmp0(type,"pager")==0)
{
context->pager_rows = json_int_by_name(obj,"rows",1);
if(context->pager_rows<1)
context->pager_rows = 1;
widget = pager_init(context);
}

if(widget==NULL)
Expand Down
70 changes: 70 additions & 0 deletions src/pager.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <gtk/gtk.h>
#include <json.h>
#include "sfwbar.h"

GtkWidget *pager_init ( struct context *context )
{
context->pager = gtk_grid_new();
context->features |= F_PAGER;
pager_update(context);
return context->pager;
}

void pager_remove_button ( GtkWidget *widget, struct context *context )
{
gtk_container_remove ( GTK_CONTAINER(context->pager), widget );
}

void pager_button_click( GtkWidget *widget, struct context *context )
{
char buff[256];
gchar *label;
label = (gchar *)gtk_button_get_label(GTK_BUTTON(widget));
if(label==NULL)
return;
snprintf(buff,255,"workspace '%s'",label);
ipc_send ( context->ipc, 0, buff );
}

void pager_update ( struct context *context )
{
int i,c=0;
int sock;
gint32 etype;
json_object *obj,*iter;
GtkWidget *widget;
gchar *label;

sock=ipc_open();
if(sock==-1)
return;

ipc_send(sock,1,"");
obj = ipc_poll(sock,&etype);
close(sock);

if(!json_object_is_type(obj, json_type_array))
return;
gtk_container_foreach(GTK_CONTAINER(context->pager),(GtkCallback)pager_remove_button,context);
for(i=0;i<json_object_array_length(obj);i++)
{
iter = json_object_array_get_idx(obj,i);
label = json_string_by_name(iter,"name");
if(label!=NULL)
{
widget = gtk_button_new_with_label(label);
g_free(label);
gtk_widget_set_name(widget, "pager_normal");
if(json_bool_by_name(iter,"visible",FALSE)==TRUE)
gtk_widget_set_name(widget, "pager_visible");
if(json_bool_by_name(iter,"focused",FALSE)==TRUE)
gtk_widget_set_name(widget, "pager_focused");
g_signal_connect(widget,"clicked",G_CALLBACK(pager_button_click),context);
gtk_grid_attach(GTK_GRID(context->pager),widget, c/(context->pager_rows),
c%(context->pager_rows),1,1);
c++;
}
}
gtk_widget_show_all(context->pager);
json_object_put(obj);
}
4 changes: 3 additions & 1 deletion src/sfwbar.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ gint shell_timer ( struct context *context )
ev = ipc_parse_event(obj);
if(etype==0x80000003)
dispatch_event(&ev,context);
if(etype==0x80000000)
pager_update(context);
json_object_put(obj);
obj = ipc_poll(context->ipc,&etype);
}
Expand Down Expand Up @@ -135,7 +137,7 @@ static void activate (GtkApplication* app, struct context *context)
if(context->features & F_TASKBAR)
taskbar_populate(context);

if((context->features & F_TASKBAR)||(context->features & F_PLACEMENT))
if((context->features & F_TASKBAR)||(context->features & F_PLACEMENT)||(context->features & F_PAGER))
{
context->ipc = ipc_open();
ipc_subscribe(context->ipc);
Expand Down
12 changes: 9 additions & 3 deletions src/sfwbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ struct context {
gint32 tb_focus;
gint32 tb_rows;
gint32 tb_isize;
gint32 pager_rows;
gint32 position;
gint32 wp_x,wp_y;
GtkCssProvider *css;
GtkWidget *box;
GtkWidget *pager;
GList *buttons;
GList *widgets;
GList *file_list;
Expand Down Expand Up @@ -83,6 +85,9 @@ void taskbar_refresh ( struct context *context );
void taskbar_delete_window (gint64 pid, struct context *context);
void taskbar_update_window (struct ipc_event *ev, struct context *context);

GtkWidget *pager_init ( struct context *context );
void pager_update ( struct context *context );

GtkWidget *layout_init ( struct context *context, json_object *obj );
void widget_update_all( struct context *context );
void widget_action ( GtkWidget *widget, gpointer data );
Expand Down Expand Up @@ -118,9 +123,10 @@ int md5_file( char *path, unsigned char output[16] );
enum {
F_TASKBAR = 1<<0,
F_PLACEMENT = 1<<1,
F_TB_ICON = 1<<2,
F_TB_LABEL = 1<<3,
F_TB_EXPAND = 1<<4
F_PAGER = 1<<2,
F_TB_ICON = 1<<3,
F_TB_LABEL = 1<<4,
F_TB_EXPAND = 1<<5
};

enum {
Expand Down

0 comments on commit 6dacbde

Please sign in to comment.