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

Overhaul swaybar protocol handling (+fixes) #2640

Merged
merged 10 commits into from Sep 19, 2018
Merged

Conversation

ianyfan
Copy link
Contributor

@ianyfan ianyfan commented Sep 16, 2018

Should fix #2177

This replaces the somewhat dodgy old implementation with something a bit more sensible, I think.

Both protocols now properly handle long and multiple statuses. The old i3bar handling also allowed dodgy JSON to be sent, as long as it starts with a valid array, which this fixes.

I've tried it with many different status commands, but I may have missed something so please test if you have anything unconventional. Might also want to test performance as well, seems fine but I haven't tested it thoroughly.

One annoying thing is that since everything works in the one buffer, it is not trivial to print received JSON for debugging (since we cannot simply add a null-terminator). Ideas for adding it back in:
1. copy the string each time a valid array is encountered, then print it at the end; good if we assume that most of the time, only one status with be sent at a time;
2. don't drop the last json object, keep it around in the buffer, then add a null-terminator at the end and print it; good if we assume that most of the time, the buffer won't in fact overflow.
Thoughts? I think the second option is probably better.

In the end, I didn't go with either of the above options, instead, I printed each object as they came (using some buffer shenanigans to avoid copying the string), but printed another message for what actually gets rendered.

Also leave comments about function/ variable names, they may not be the best.

One thing I did realise afterwards is that read_line_buffer skips over \r but I haven't bothered to do that, not sure if that's wanted.

Note: there is a small issue with the bar immediately exiting due to invalid JSON which I thought I had fixed but still occurs occasionally. I'm also not sure why it exits rather than just display the error message. I think it might have something to do with receiving null bytes from the stream.

Copy link
Member

@ascent12 ascent12 left a comment

Choose a reason for hiding this comment

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

I feel a lot of this buffer/newline handling code could be simplified with fdopen() + getline().

@ianyfan
Copy link
Contributor Author

ianyfan commented Sep 16, 2018

I'll have a look into it.

@ianyfan ianyfan changed the title Overhaul swaybar protocol handling (+fixes) [WIP] Overhaul swaybar protocol handling (+fixes) Sep 16, 2018
@ddevault
Copy link
Member

Thoughts? I think the second option is probably better.

Agree

@ianyfan ianyfan force-pushed the swaybar branch 2 times, most recently from 3c4ba1d to 72008bd Compare September 17, 2018 00:18
@ianyfan
Copy link
Contributor Author

ianyfan commented Sep 17, 2018

Addressed comments; the rewriting makes me want to squash some commits together.

@ianyfan ianyfan force-pushed the swaybar branch 2 times, most recently from 17548d2 to 926e2ab Compare September 17, 2018 13:25
This now uses getline to correctly handle multiple or long statuses. It
also removes the struct text_protocol_state and moves its members into
the status_line struct.
This now uses the getline function to receive the header, replacing
read_line_buffer, which has been deleted since it is otherwise unused.
Furthermore, once the protocol has been determined, the current status
is handled immediately to be shown (though this has not been added for
the i3bar protocol since it has not yet been rewritten to handle this).
This now correctly handles an incoming json infinite array by shifting
most of the heavy listing to the json-c parser, as well as sending
multiple statuses at once. It also removes the struct
i3bar_protocol_state and moves its members into the status_line struct,
allowing the same buffer to be used for both protocols.
@ianyfan
Copy link
Contributor Author

ianyfan commented Sep 18, 2018

Ready for more review

@ddevault
Copy link
Member

This is looking pretty good. Is it still WIP? If not I'll start running it as my daily driver.

@ianyfan
Copy link
Contributor Author

ianyfan commented Sep 18, 2018

Nope, should be good to go.

@ianyfan ianyfan changed the title [WIP] Overhaul swaybar protocol handling (+fixes) Overhaul swaybar protocol handling (+fixes) Sep 18, 2018
@ddevault
Copy link
Member

Works for me, thanks!

@ddevault ddevault merged commit cdce604 into swaywm:master Sep 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

swaybar i3bar protocol json parsing double/all-you-can-free
3 participants