1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
|
#include "services/workspaces/sway-workspace-service.h"
#include "gio/gio.h"
#include "glib-object.h"
#include "glib.h"
#include "services/workspace-service.h"
#include <json-glib/json-glib.h>
#include <gtk/gtk.h>
#include <stdio.h>
#include <string.h>
static const gchar MAGIC_STRING[] = "i3-ipc";
#define MESSAGE_TYPE_COMMAND 0
#define MESSAGE_TYPE_WORKSPACES 1
typedef struct _MessageHeader MessageHeader;
typedef enum _FoobarSwayError FoobarSwayError;
//
// FoobarSwayWorkspace:
//
// Representation of a workspace in Sway.
//
#define FOOBAR_TYPE_SWAY_WORKSPACE foobar_sway_workspace_get_type( )
G_DECLARE_FINAL_TYPE( FoobarSwayWorkspace, foobar_sway_workspace, FOOBAR, SWAY_WORKSPACE, GObject )
struct _FoobarSwayWorkspace
{
GObject parent_instance;
FoobarSwayWorkspaceService* service;
gint64 id;
gint64 num;
gchar* name;
gchar* monitor;
FoobarWorkspaceFlags flags;
};
enum
{
WORKSPACE_PROP_ID = 1,
WORKSPACE_PROP_NAME,
WORKSPACE_PROP_MONITOR,
WORKSPACE_PROP_FLAGS,
N_WORKSPACE_PROPS,
};
static void foobar_sway_workspace_class_init ( FoobarSwayWorkspaceClass* klass );
static void foobar_sway_workspace_interface_init( FoobarWorkspaceInterface* iface );
static void foobar_sway_workspace_init ( FoobarSwayWorkspace* self );
static void foobar_sway_workspace_get_property ( GObject* object,
guint prop_id,
GValue* value,
GParamSpec* pspec );
static void foobar_sway_workspace_finalize ( GObject* object );
static FoobarSwayWorkspace* foobar_sway_workspace_new ( FoobarSwayWorkspaceService* service );
static gint64 foobar_sway_workspace_get_id ( FoobarWorkspace* self );
static gint64 foobar_sway_workspace_get_num ( FoobarSwayWorkspace* self );
static gchar const* foobar_sway_workspace_get_name ( FoobarWorkspace* workspace );
static gchar const* foobar_sway_workspace_get_monitor ( FoobarWorkspace* workspace );
static FoobarWorkspaceFlags foobar_sway_workspace_get_flags ( FoobarWorkspace* workspace );
static void foobar_sway_workspace_activate ( FoobarWorkspace* workspace );
static void foobar_sway_workspace_set_id ( FoobarSwayWorkspace* self,
gint64 value );
static void foobar_sway_workspace_set_num ( FoobarSwayWorkspace* self,
gint64 value );
static void foobar_sway_workspace_set_name ( FoobarSwayWorkspace* self,
gchar const* value );
static void foobar_sway_workspace_set_monitor ( FoobarSwayWorkspace* self,
gchar const* value );
static void foobar_sway_workspace_set_flags ( FoobarSwayWorkspace* self,
FoobarWorkspaceFlags value );
G_DEFINE_TYPE_WITH_CODE(
FoobarSwayWorkspace, foobar_sway_workspace, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE( FOOBAR_TYPE_WORKSPACE, foobar_sway_workspace_interface_init ) );
//
// FoobarSwayWorkspaceService:
//
// Service monitoring the workspaces provided by the window manager. This is implemented by communicating directly with
// Sway through its IPC socket.
//
struct _FoobarSwayWorkspaceService
{
GObject parent_instance;
GListStore* workspaces;
GtkSortListModel* sorted_workspaces;
gchar* socket_path;
};
enum
{
PROP_WORKSPACES = 1,
N_PROPS,
};
static void foobar_sway_workspace_service_class_init ( FoobarSwayWorkspaceServiceClass* klass );
static void foobar_sway_workspace_service_interface_init ( FoobarWorkspaceServiceInterface* iface );
static void foobar_sway_workspace_service_init ( FoobarSwayWorkspaceService* self );
static void foobar_sway_workspace_service_get_property ( GObject* object,
guint prop_id,
GValue* value,
GParamSpec* pspec );
static void foobar_sway_workspace_service_dispose ( GObject* object );
static void foobar_sway_workspace_service_finalize ( GObject* object );
static GListModel* foobar_sway_workspace_service_get_workspaces ( FoobarWorkspaceService* service );
static gboolean foobar_sway_workspace_service_is_available ( FoobarWorkspaceService* service );
static void foobar_sway_workspace_service_initialize ( FoobarSwayWorkspaceService* self );
static JsonNode* foobar_sway_workspace_service_send_request ( FoobarSwayWorkspaceService* self,
guint type,
gchar const* payload,
GError** error );
static gboolean foobar_sway_workspace_service_write ( GSocket* socket,
guint32 type,
gchar const* payload,
GCancellable* cancellable,
GError** out_error );
static JsonNode* foobar_sway_workspace_service_read ( GSocket* socket,
guint32* out_type,
GCancellable* cancellable,
GError** out_error );
static gchar* foobar_sway_workspace_service_get_socket_path ( void );
static gint foobar_sway_workspace_service_sort_func ( gconstpointer item_a,
gconstpointer item_b,
gpointer userdata );
G_DEFINE_TYPE_WITH_CODE(
FoobarSwayWorkspaceService, foobar_sway_workspace_service, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE( FOOBAR_TYPE_WORKSPACE_SERVICE, foobar_sway_workspace_service_interface_init ) );
//
// MessageHeader:
//
// Header in an i3wm/sway protocol message.
//
struct __attribute__((__packed__)) _MessageHeader
{
gchar magic_string[sizeof(MAGIC_STRING) - 1];
guint32 message_length;
guint32 message_type;
};
//
// FoobarSwayError:
//
// Custom error domain for sway-related errors.
//
enum _FoobarSwayError
{
FOOBAR_SWAY_ERROR_INVALID_MAGIC_STRING,
FOOBAR_SWAY_ERROR_MISMATCHING_TYPE,
};
G_DEFINE_QUARK( foobar-sway-error, foobar_sway_error );
#define FOOBAR_SWAY_ERROR ( foobar_sway_error_quark( ) )
// ---------------------------------------------------------------------------------------------------------------------
// Workspace
// ---------------------------------------------------------------------------------------------------------------------
//
// Static initialization for workspaces.
//
void foobar_sway_workspace_class_init( FoobarSwayWorkspaceClass* klass )
{
GObjectClass* object_klass = G_OBJECT_CLASS( klass );
object_klass->get_property = foobar_sway_workspace_get_property;
object_klass->finalize = foobar_sway_workspace_finalize;
g_object_class_override_property( object_klass, WORKSPACE_PROP_ID, "id" );
g_object_class_override_property( object_klass, WORKSPACE_PROP_NAME, "name" );
g_object_class_override_property( object_klass, WORKSPACE_PROP_MONITOR, "monitor" );
g_object_class_override_property( object_klass, WORKSPACE_PROP_FLAGS, "flags" );
}
//
// Interface initialization for workspaces.
//
void foobar_sway_workspace_interface_init( FoobarWorkspaceInterface* iface )
{
iface->get_id = foobar_sway_workspace_get_id;
iface->get_name = foobar_sway_workspace_get_name;
iface->get_monitor = foobar_sway_workspace_get_monitor;
iface->get_flags = foobar_sway_workspace_get_flags;
iface->activate = foobar_sway_workspace_activate;
}
//
// Instance initialization for workspaces.
//
void foobar_sway_workspace_init( FoobarSwayWorkspace* self )
{
(void)self;
}
//
// Property getter implementation, mapping a property id to a method.
//
void foobar_sway_workspace_get_property(
GObject* object,
guint prop_id,
GValue* value,
GParamSpec* pspec )
{
FoobarSwayWorkspace* self = (FoobarSwayWorkspace*)object;
switch ( prop_id )
{
case WORKSPACE_PROP_ID:
g_value_set_int64( value, foobar_workspace_get_id( FOOBAR_WORKSPACE( self ) ) );
break;
case WORKSPACE_PROP_NAME:
g_value_set_string( value, foobar_workspace_get_name( FOOBAR_WORKSPACE( self ) ) );
break;
case WORKSPACE_PROP_MONITOR:
g_value_set_string( value, foobar_workspace_get_monitor( FOOBAR_WORKSPACE( self ) ) );
break;
case WORKSPACE_PROP_FLAGS:
g_value_set_flags( value, foobar_workspace_get_flags( FOOBAR_WORKSPACE( self ) ) );
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID( object, prop_id, pspec );
break;
}
}
//
// Instance cleanup for workspaces.
//
void foobar_sway_workspace_finalize( GObject* object )
{
FoobarSwayWorkspace* self = (FoobarSwayWorkspace*)object;
g_clear_pointer( &self->name, g_free );
g_clear_pointer( &self->monitor, g_free );
G_OBJECT_CLASS( foobar_sway_workspace_parent_class )->finalize( object );
}
//
// Create a new workspace that is owned by the given service (captured as an unowned reference).
//
FoobarSwayWorkspace* foobar_sway_workspace_new( FoobarSwayWorkspaceService* service )
{
FoobarSwayWorkspace* self = g_object_new( FOOBAR_TYPE_SWAY_WORKSPACE, NULL );
self->service = service;
return self;
}
//
// Numeric ID of the workspace.
//
gint64 foobar_sway_workspace_get_id( FoobarWorkspace* workspace )
{
FoobarSwayWorkspace* self = (FoobarSwayWorkspace*)workspace;
return self->id;
}
//
// Logical number of the workspace.
//
gint64 foobar_sway_workspace_get_num( FoobarSwayWorkspace* self )
{
g_return_val_if_fail( FOOBAR_IS_SWAY_WORKSPACE( self ), -1 );
return self->num;
}
//
// Optional human-readable workspace name.
//
gchar const* foobar_sway_workspace_get_name( FoobarWorkspace* workspace )
{
FoobarSwayWorkspace* self = (FoobarSwayWorkspace*)workspace;
return self->name;
}
//
// Connector ID of the workspace's monitor.
//
gchar const* foobar_sway_workspace_get_monitor( FoobarWorkspace* workspace )
{
FoobarSwayWorkspace* self = (FoobarSwayWorkspace*)workspace;
return self->monitor;
}
//
// Flags indicating the current state of the workspace.
//
FoobarWorkspaceFlags foobar_sway_workspace_get_flags( FoobarWorkspace* workspace )
{
FoobarSwayWorkspace* self = (FoobarSwayWorkspace*)workspace;
return self->flags;
}
//
// Let this workspace become the active one.
//
void foobar_sway_workspace_activate( FoobarWorkspace* workspace )
{
FoobarSwayWorkspace* self = (FoobarSwayWorkspace*)workspace;
if ( !self->service ) { return; }
gchar const* name = foobar_workspace_get_name( FOOBAR_WORKSPACE( self ) );
g_autofree gchar* request = g_strdup_printf( "workspace --no-auto-back-and-forth \"%s\"", name );
g_autoptr( GError ) error = NULL;
g_autoptr( JsonNode ) response = foobar_sway_workspace_service_send_request(
self->service,
MESSAGE_TYPE_COMMAND,
request,
&error );
if ( !response )
{
g_warning( "Unable to send workspace activation command: %s", error->message );
return;
}
JsonArray* array = json_node_get_array( response );
for ( guint i = 0; i < json_array_get_length( array ); ++i )
{
JsonObject* status_object = json_array_get_object_element( array, i );
gboolean success = json_object_get_boolean_member( status_object, "success" );
if ( !success )
{
g_warning( "Could not activate workspace." );
return;
}
}
}
//
// Numeric ID of the workspace.
//
void foobar_sway_workspace_set_id(
FoobarSwayWorkspace* self,
gint64 value )
{
g_return_if_fail( FOOBAR_IS_SWAY_WORKSPACE( self ) );
if ( self->id != value )
{
self->id = value;
g_object_notify( G_OBJECT( self ), "id" );
}
}
//
// Logical number of the workspace.
//
void foobar_sway_workspace_set_num(
FoobarSwayWorkspace* self,
gint64 value )
{
g_return_if_fail( FOOBAR_IS_SWAY_WORKSPACE( self ) );
self->num = value;
}
//
// Optional human-readable workspace name.
//
void foobar_sway_workspace_set_name(
FoobarSwayWorkspace* self,
gchar const* value )
{
g_return_if_fail( FOOBAR_IS_SWAY_WORKSPACE( self ) );
if ( g_strcmp0( self->name, value ) )
{
g_clear_pointer( &self->name, g_free );
self->name = g_strdup( value );
g_object_notify( G_OBJECT( self ), "name" );
}
}
//
// Connector ID of the workspace's monitor.
//
void foobar_sway_workspace_set_monitor(
FoobarSwayWorkspace* self,
gchar const* value )
{
g_return_if_fail( FOOBAR_IS_SWAY_WORKSPACE( self ) );
if ( g_strcmp0( self->monitor, value ) )
{
g_clear_pointer( &self->monitor, g_free );
self->monitor = g_strdup( value );
g_object_notify( G_OBJECT( self ), "monitor" );
}
}
//
// Flags indicating the current state of the workspace.
//
void foobar_sway_workspace_set_flags(
FoobarSwayWorkspace* self,
FoobarWorkspaceFlags value )
{
g_return_if_fail( FOOBAR_IS_SWAY_WORKSPACE( self ) );
if ( self->flags != value )
{
self->flags = value;
g_object_notify( G_OBJECT( self ), "flags" );
}
}
// ---------------------------------------------------------------------------------------------------------------------
// Service Implementation
// ---------------------------------------------------------------------------------------------------------------------
//
// Static initialization for the workspace service.
//
void foobar_sway_workspace_service_class_init( FoobarSwayWorkspaceServiceClass* klass )
{
GObjectClass* object_klass = G_OBJECT_CLASS( klass );
object_klass->get_property = foobar_sway_workspace_service_get_property;
object_klass->dispose = foobar_sway_workspace_service_dispose;
object_klass->finalize = foobar_sway_workspace_service_finalize;
g_object_class_override_property( object_klass, PROP_WORKSPACES, "workspaces" );
}
//
// Interface initialization for the workspace service.
//
void foobar_sway_workspace_service_interface_init( FoobarWorkspaceServiceInterface* iface )
{
iface->get_workspaces = foobar_sway_workspace_service_get_workspaces;
iface->is_available = foobar_sway_workspace_service_is_available;
}
//
// Instance initialization for the workspace service.
//
void foobar_sway_workspace_service_init( FoobarSwayWorkspaceService* self )
{
self->workspaces = g_list_store_new( FOOBAR_TYPE_WORKSPACE );
GtkCustomSorter* sorter = gtk_custom_sorter_new( foobar_sway_workspace_service_sort_func, NULL, NULL );
self->sorted_workspaces = gtk_sort_list_model_new(
G_LIST_MODEL( g_object_ref( self->workspaces ) ),
GTK_SORTER( sorter ) );
self->socket_path = foobar_sway_workspace_service_get_socket_path( );
if ( !self->socket_path )
{
return;
}
foobar_sway_workspace_service_initialize( self );
(void)foobar_sway_workspace_set_id;
(void)foobar_sway_workspace_set_name;
(void)foobar_sway_workspace_set_monitor;
(void)foobar_sway_workspace_set_flags;
(void)foobar_sway_workspace_new;
}
//
// Property getter implementation, mapping a property id to a method.
//
void foobar_sway_workspace_service_get_property( GObject* object, guint prop_id, GValue* value, GParamSpec* pspec )
{
FoobarSwayWorkspaceService* self = (FoobarSwayWorkspaceService*)object;
switch ( prop_id )
{
case PROP_WORKSPACES:
g_value_set_object( value, foobar_workspace_service_get_workspaces( FOOBAR_WORKSPACE_SERVICE( self ) ) );
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID( object, prop_id, pspec );
break;
}
}
//
// Instance de-initialization for the workspace service.
//
// This is where the event thread is joined because it might still create new object references which is a bad thing to
// do in finalize.
//
void foobar_sway_workspace_service_dispose( GObject* object )
{
FoobarSwayWorkspaceService* self = (FoobarSwayWorkspaceService*)object;
(void)self;
G_OBJECT_CLASS( foobar_sway_workspace_service_parent_class )->dispose( object );
}
//
// Instance cleanup for the workspace service.
//
void foobar_sway_workspace_service_finalize( GObject* object )
{
FoobarSwayWorkspaceService* self = (FoobarSwayWorkspaceService*)object;
for ( guint i = 0; i < g_list_model_get_n_items( G_LIST_MODEL( self->workspaces ) ); ++i )
{
FoobarSwayWorkspace* workspace = g_list_model_get_item( G_LIST_MODEL( self->workspaces ), i );
workspace->service = NULL;
}
// g_clear_object( &self->event_cancellable );
g_clear_object( &self->sorted_workspaces );
g_clear_object( &self->workspaces );
g_clear_pointer( &self->socket_path, g_free );
G_OBJECT_CLASS( foobar_sway_workspace_service_parent_class )->finalize( object );
}
//
// Get a sorted list of all workspaces.
//
GListModel* foobar_sway_workspace_service_get_workspaces( FoobarWorkspaceService* service )
{
FoobarSwayWorkspaceService* self = (FoobarSwayWorkspaceService*)service;
return G_LIST_MODEL( self->sorted_workspaces );
}
//
// Check whether this service is connected to Sway.
//
gboolean foobar_sway_workspace_service_is_available( FoobarWorkspaceService* service )
{
FoobarSwayWorkspaceService* self = (FoobarSwayWorkspaceService*)service;
return self->socket_path != NULL;
}
// ---------------------------------------------------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------------------------------------------------
//
// Create a new Sway workspace service instance.
//
FoobarWorkspaceService* foobar_sway_workspace_service_new( void )
{
return g_object_new( FOOBAR_TYPE_SWAY_WORKSPACE_SERVICE, NULL );
}
// ---------------------------------------------------------------------------------------------------------------------
// Helper Methods
// ---------------------------------------------------------------------------------------------------------------------
//
// Refresh the list of workspaces by actively sending a request to Sway.
//
void foobar_sway_workspace_service_initialize( FoobarSwayWorkspaceService* self )
{
for ( guint i = 0; i < g_list_model_get_n_items( G_LIST_MODEL( self->workspaces ) ); ++i )
{
FoobarSwayWorkspace* workspace = g_list_model_get_item( G_LIST_MODEL( self->workspaces ), i );
workspace->service = NULL;
}
g_list_store_remove_all( self->workspaces );
g_autoptr( GListStore ) workspaces = g_list_store_new( FOOBAR_TYPE_WORKSPACE );
{
g_autoptr( GError ) error = NULL;
g_autoptr( JsonNode ) workspaces_node = foobar_sway_workspace_service_send_request(
self,
MESSAGE_TYPE_WORKSPACES,
"",
&error );
if ( !workspaces_node )
{
g_warning( "Unable to load workspaces: %s", error->message );
return;
}
JsonArray* workspaces_array = json_node_get_array( workspaces_node );
for ( guint i = 0; i < json_array_get_length( workspaces_array ); ++i )
{
JsonObject* workspace_object = json_array_get_object_element( workspaces_array, i );
gchar const* name = json_object_get_string_member( workspace_object, "name" );
gchar const* output = json_object_get_string_member( workspace_object, "output" );
gint64 id = json_object_get_int_member( workspace_object, "id" );
gint64 num = json_object_get_int_member( workspace_object, "num" );
gboolean visible = json_object_get_boolean_member( workspace_object, "visible" );
gboolean focused = json_object_get_boolean_member( workspace_object, "focused" );
gboolean urgent = json_object_get_boolean_member( workspace_object, "urgent" );
FoobarWorkspaceFlags flags = FOOBAR_WORKSPACE_FLAGS_NONE;
flags = visible ? flags | FOOBAR_WORKSPACE_FLAGS_VISIBLE : flags;
flags = focused ? flags | FOOBAR_WORKSPACE_FLAGS_ACTIVE : flags;
flags = urgent ? flags | FOOBAR_WORKSPACE_FLAGS_URGENT : flags;
g_autoptr(FoobarSwayWorkspace) workspace = foobar_sway_workspace_new( self );
foobar_sway_workspace_set_id( workspace, id );
foobar_sway_workspace_set_num( workspace, num );
foobar_sway_workspace_set_name( workspace, name );
foobar_sway_workspace_set_monitor( workspace, output );
foobar_sway_workspace_set_flags( workspace, flags );
g_list_store_append( workspaces, workspace );
}
}
for ( guint i = 0; i < g_list_model_get_n_items( G_LIST_MODEL( workspaces ) ); ++i )
{
FoobarWorkspace* workspace = g_list_model_get_item( G_LIST_MODEL( workspaces ), i );
g_list_store_append( self->workspaces, workspace );
}
}
//
// Synchronously send a command to Sway, returning the entire response.
//
JsonNode* foobar_sway_workspace_service_send_request(
FoobarSwayWorkspaceService* self,
guint type,
gchar const* payload,
GError** error )
{
g_autoptr( GSocket ) sock = g_socket_new(
G_SOCKET_FAMILY_UNIX,
G_SOCKET_TYPE_STREAM,
G_SOCKET_PROTOCOL_DEFAULT,
error );
if ( !sock ) { return NULL; }
g_autoptr( GSocketAddress ) addr = g_unix_socket_address_new( self->socket_path );
if ( !g_socket_connect( sock, addr, NULL, error ) ) { return NULL; }
if ( !foobar_sway_workspace_service_write( sock, type, payload, NULL, error ) ) { return NULL; }
guint reply_type;
JsonNode* reply = foobar_sway_workspace_service_read( sock, &reply_type, NULL, error );
if ( !reply ) { return NULL; }
if ( reply_type != type )
{
g_set_error(
error,
FOOBAR_SWAY_ERROR,
FOOBAR_SWAY_ERROR_MISMATCHING_TYPE,
"Mismatching message type in reply: Sent %u, but received %u.",
type,
reply_type );
json_node_unref( reply );
return NULL;
}
return reply;
}
//
// Write a request onto a Sway socket.
//
gboolean foobar_sway_workspace_service_write(
GSocket* socket,
guint32 type,
gchar const* payload,
GCancellable* cancellable,
GError** out_error )
{
guint32 payload_length = strlen( payload );
MessageHeader header = { 0 };
memcpy( header.magic_string, MAGIC_STRING, sizeof( header.magic_string ) );
header.message_length = payload_length;
header.message_type = type;
gsize buffer_length = sizeof( MessageHeader ) + payload_length;
g_autofree gchar* buffer = g_new0( gchar, buffer_length );
memcpy( buffer + 0, &header, sizeof( MessageHeader ) );
memcpy( buffer + sizeof( MessageHeader ), payload, payload_length );
return g_socket_send( socket, buffer, buffer_length, cancellable, out_error ) != -1;
}
//
// Read a response or event from a Sway socket.
//
JsonNode* foobar_sway_workspace_service_read(
GSocket* socket,
guint32* out_type,
GCancellable* cancellable,
GError** out_error )
{
gchar header_buffer[ sizeof( MessageHeader ) ];
gsize header_received = 0;
while ( header_received < sizeof( header_buffer ) )
{
gssize received = g_socket_receive(
socket,
&header_buffer[ header_received ],
sizeof( header_buffer ) - header_received,
cancellable,
out_error );
if ( received < 0 ) { return NULL; }
header_received += received;
}
MessageHeader header = { 0 };
memcpy( &header, header_buffer, sizeof( MessageHeader ) );
if ( memcmp( header.magic_string, MAGIC_STRING, sizeof( header.magic_string ) ) != 0 )
{
g_set_error(
out_error,
FOOBAR_SWAY_ERROR,
FOOBAR_SWAY_ERROR_INVALID_MAGIC_STRING,
"Received invalid magic string from Sway." );
return NULL;
}
g_autofree gchar* buffer = g_new0( gchar, header.message_length + 1 );
gsize buffer_received = 0;
while ( buffer_received < header.message_length )
{
gssize received = g_socket_receive(
socket,
&buffer[ buffer_received ],
header.message_length - buffer_received,
cancellable,
out_error );
if ( received < 0 ) { return NULL; }
buffer_received += received;
}
g_autoptr( JsonParser ) parser = json_parser_new( );
if ( !json_parser_load_from_data( parser, buffer, (gssize)header.message_length, out_error ) ) { return NULL; }
if ( out_type ) { *out_type = header.message_type; }
return json_parser_steal_root( parser );
}
//
// Get the sway socket path.
//
gchar* foobar_sway_workspace_service_get_socket_path( void )
{
gchar const* sock = g_getenv( "SWAYSOCK" );
if ( !sock )
{
return NULL;
}
return g_strdup( sock );
}
//
// Sorting callback for workspaces. Items are sorted to be in the following order:
// 1. named
// 2. numbered
//
gint foobar_sway_workspace_service_sort_func(
gconstpointer item_a,
gconstpointer item_b,
gpointer userdata )
{
(void)userdata;
FoobarSwayWorkspace* workspace_a = (FoobarSwayWorkspace*)item_a;
FoobarSwayWorkspace* workspace_b = (FoobarSwayWorkspace*)item_b;
gint64 num_a = foobar_sway_workspace_get_num( workspace_a );
gint64 num_b = foobar_sway_workspace_get_num( workspace_b );
gboolean named_a = num_a < 0;
gboolean named_b = num_b < 0;
gchar const* name_a = foobar_workspace_get_name( FOOBAR_WORKSPACE( workspace_a ) );
gchar const* name_b = foobar_workspace_get_name( FOOBAR_WORKSPACE( workspace_b ) );
// both numbered => sort by number
if ( !named_a && !named_b )
{
if ( num_a < num_b ) { return -1; }
if ( num_a > num_b ) { return 1; }
return 0;
}
// one numbered, one named
if ( named_a && !named_b ) { return -1; }
if ( !named_a && named_b ) { return 1; }
// both named
return g_strcmp0( name_a, name_b );
}
|