-
Notifications
You must be signed in to change notification settings - Fork 1
show amount of pixels painted per ip adress #6
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
Open
lkr0n
wants to merge
1
commit into
luwae:stats_by_ip
Choose a base branch
from
lkr0n:stats_per_ip
base: stats_by_ip
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,15 +3,19 @@ | |
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <pthread.h> | ||
| #include <assert.h> | ||
| #include "SDL.h" | ||
| #include "SDL2/SDL_ttf.h" | ||
|
|
||
| #include "common.h" | ||
| #include "canvas.h" | ||
| #include "connection.h" | ||
|
|
||
| #define TEX_SIZE_X 512 | ||
| #define TEX_SIZE_Y 512 | ||
| #define SCREEN_SIZE_X 1024 | ||
| #define SCREEN_SIZE_Y 1024 | ||
| #define NET_MASK 0xff | ||
|
|
||
| SDL_Window *window; | ||
| SDL_Renderer *renderer; | ||
|
|
@@ -27,7 +31,101 @@ pthread_t canvas_thread; | |
| } \ | ||
| } while (0) | ||
|
|
||
| #define WHITE (SDL_Color){0xff, 0xff, 0xff, 0xff} | ||
|
|
||
| #define FONT_TEXTURE_SIZE 512 | ||
| #define GLYPH_SIZE 256 | ||
|
|
||
| struct FontTexture { | ||
| SDL_Rect glyphs[GLYPH_SIZE]; | ||
| SDL_Texture * texture; | ||
| } fontTexture; | ||
|
|
||
| enum FontStatus { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This pattern does not really fit into the codebase. |
||
| Success, | ||
| InvalidArg, | ||
| InitFailed, | ||
| FailedOpen, | ||
| FontTooBig, | ||
| }; | ||
|
|
||
| enum FontStatus init_font(SDL_Renderer * renderer) { | ||
| SDL_Surface * text = NULL; | ||
| SDL_Surface * surface = NULL; | ||
|
|
||
| TTF_Font * font = NULL; | ||
| enum FontStatus status = Success; | ||
|
|
||
| if(renderer == NULL) { | ||
| status = InvalidArg; | ||
| goto exit; | ||
| } | ||
|
|
||
| if(TTF_Init()) { | ||
| status = InitFailed; | ||
| goto exit; | ||
| } | ||
|
|
||
| font = TTF_OpenFont("./fonts/SourceCodePro-Regular.ttf", 10); | ||
|
|
||
| if(font == NULL) { | ||
| status = FailedOpen; | ||
| goto exit; | ||
| } | ||
|
|
||
| surface = SDL_CreateRGBSurface(0, FONT_TEXTURE_SIZE, FONT_TEXTURE_SIZE, 32, 0, 0, 0, 0xff); | ||
|
|
||
| /* load all ascii printable characters into the glyph atlas */ | ||
| size_t x = 0; | ||
| size_t y = 0; | ||
| for(char c = ' '; c <= '~'; c++) { | ||
| int w, h; | ||
|
|
||
| const char ctext[] = {c, 0}; | ||
| text = TTF_RenderUTF8_Blended(font, ctext, WHITE); | ||
| assert(TTF_SizeText(font, ctext, &w, &h) == 0); | ||
|
|
||
| if (x + w >= FONT_TEXTURE_SIZE) { | ||
| x = 0; | ||
| y += h+1; | ||
| } | ||
|
|
||
| if (y + h >= FONT_TEXTURE_SIZE) { | ||
| status = FontTooBig; | ||
| goto exit; | ||
| } | ||
|
|
||
| { | ||
| SDL_Rect * rect; | ||
|
|
||
| rect = fontTexture.glyphs + (size_t)c; | ||
| rect->x = x; | ||
| rect->y = y; | ||
| rect->w = w; | ||
| rect->h = h; | ||
|
|
||
| SDL_BlitSurface(text, NULL, surface, rect); | ||
| } | ||
|
|
||
| x += w; | ||
|
|
||
| SDL_FreeSurface(text); | ||
| } | ||
|
|
||
| /* obtain a texture containing all the glyphs we are interested in */ | ||
| fontTexture.texture = SDL_CreateTextureFromSurface(renderer, surface); | ||
|
|
||
| exit: | ||
| SDL_FreeSurface(surface); | ||
| TTF_CloseFont(font); | ||
| return status; | ||
| } | ||
|
|
||
| void canvas_stop(void) { | ||
| if (fontTexture.texture) { | ||
| SDL_DestroyTexture(fontTexture.texture); | ||
| fontTexture.texture = NULL; | ||
| } | ||
| if (screen_texture) { | ||
| SDL_DestroyTexture(screen_texture); | ||
| screen_texture = NULL; | ||
|
|
@@ -43,10 +141,53 @@ void canvas_stop(void) { | |
| SDL_Quit(); | ||
| } | ||
|
|
||
|
|
||
| void canvas_connection_draw() { | ||
| size_t i = 0; | ||
|
|
||
| /* start drawing at the upper right corner */ | ||
| int x = 0; | ||
| int y = 0; | ||
|
|
||
| /* compute the pixels set for an ip */ | ||
| unsigned int ip_buckets[NET_MASK + 1]; | ||
| memset(ip_buckets, 0, (NET_MASK + 1) * sizeof(*ip_buckets)); | ||
| for(i = 0; i < num_conns; i++){ | ||
| size_t lsb = (conns + i)->addr.sin_addr.s_addr & NET_MASK; | ||
| ip_buckets[lsb] += (conns + i)->tracker.pixels_set; | ||
| } | ||
|
|
||
| for(i = 0; i < NET_MASK + 1; i++){ | ||
| if (ip_buckets[i] == 0) { | ||
| continue; | ||
| } | ||
| char text_buffer[0x100]; | ||
| memset(text_buffer, 0, sizeof(text_buffer)); | ||
|
|
||
| snprintf(text_buffer, sizeof(text_buffer), "ip: 192.168.2.%03ld %d", | ||
| i, ip_buckets[i]); | ||
|
|
||
| size_t j = 0; | ||
| int max_height = 0; | ||
| for(j = 0; j < sizeof(text_buffer) && text_buffer[j]; j++) { | ||
| SDL_Rect src = fontTexture.glyphs[(size_t)text_buffer[j]]; | ||
| SDL_Rect dst = {.x=x, .y=y, .w=src.w, .h=src.h}; | ||
| SDL_RenderCopy(renderer, fontTexture.texture, &src, &dst); | ||
| x += dst.w; | ||
| if (dst.h > max_height) { | ||
| max_height = dst.h; | ||
| } | ||
| } | ||
| x = 0; | ||
| y += max_height + 1; | ||
| } | ||
| } | ||
|
|
||
| void canvas_draw(void) { | ||
| // ? SDL_RenderClear(renderer); | ||
| SDL_UpdateTexture(screen_texture, NULL, pixels, TEX_SIZE_X*4); | ||
| SDL_RenderCopy(renderer, screen_texture, NULL, NULL); | ||
| canvas_connection_draw(); | ||
| SDL_RenderPresent(renderer); | ||
| } | ||
|
|
||
|
|
@@ -66,6 +207,8 @@ void canvas_start(void) { | |
| SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, | ||
| TEX_SIZE_X, TEX_SIZE_Y); | ||
| CLEANUP_AND_EXIT_IF(screen_texture == NULL, "SDL_CreateTexture"); | ||
|
|
||
| CLEANUP_AND_EXIT_IF(init_font(renderer), "init_font"); | ||
| } | ||
|
|
||
| int canvas_set_px(const struct pixel *px) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(context) This is supposed to be the subnetmask of your LAN. It is required for correctly grouping the connections by ip.