-
Notifications
You must be signed in to change notification settings - Fork 2
/
graphics.c
3934 lines (3641 loc) · 129 KB
/
graphics.c
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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* The MIT License
Copyright (c) 2021-2024 Sergei Grechanik <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
////////////////////////////////////////////////////////////////////////////////
//
// This file implements a subset of the kitty graphics protocol.
//
////////////////////////////////////////////////////////////////////////////////
#define _POSIX_C_SOURCE 200809L
#include "graphics.h"
#include <zlib.h>
#include <Imlib2.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xrender.h>
#include <assert.h>
#include <ctype.h>
#include <fcntl.h>
#include <spawn.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include "khash.h"
#include "kvec.h"
extern char **environ;
#define MAX_FILENAME_SIZE 256
#define MAX_INFO_LEN 256
#define MAX_IMAGE_RECTS 20
/// The type used in this file to represent time. Used both for time differences
/// and absolute times (as milliseconds since an arbitrary point in time, see
/// `initialization_time`).
typedef int64_t Milliseconds;
enum ScaleMode {
SCALE_MODE_UNSET = 0,
/// Stretch or shrink the image to fill the box, ignoring aspect ratio.
SCALE_MODE_FILL = 1,
/// Preserve aspect ratio and fit to width or to height so that the
/// whole image is visible.
SCALE_MODE_CONTAIN = 2,
/// Do not scale. The image may be cropped if the box is too small.
SCALE_MODE_NONE = 3,
/// Do not scale, unless the box is too small, in which case the image
/// will be shrunk like with `SCALE_MODE_CONTAIN`.
SCALE_MODE_NONE_OR_CONTAIN = 4,
};
enum AnimationState {
ANIMATION_STATE_UNSET = 0,
/// The animation is stopped. Display the current frame, but don't
/// advance to the next one.
ANIMATION_STATE_STOPPED = 1,
/// Run the animation to then end, then wait for the next frame.
ANIMATION_STATE_LOADING = 2,
/// Run the animation in a loop.
ANIMATION_STATE_LOOPING = 3,
};
/// The status of an image. Each image uploaded to the terminal is cached on
/// disk, then it is loaded to ram when needed.
enum ImageStatus {
STATUS_UNINITIALIZED = 0,
STATUS_UPLOADING = 1,
STATUS_UPLOADING_ERROR = 2,
STATUS_UPLOADING_SUCCESS = 3,
STATUS_RAM_LOADING_ERROR = 4,
STATUS_RAM_LOADING_IN_PROGRESS = 5,
STATUS_RAM_LOADING_SUCCESS = 6,
};
const char *image_status_strings[6] = {
"STATUS_UNINITIALIZED",
"STATUS_UPLOADING",
"STATUS_UPLOADING_ERROR",
"STATUS_UPLOADING_SUCCESS",
"STATUS_RAM_LOADING_ERROR",
"STATUS_RAM_LOADING_SUCCESS",
};
enum ImageUploadingFailure {
ERROR_OVER_SIZE_LIMIT = 1,
ERROR_CANNOT_OPEN_CACHED_FILE = 2,
ERROR_UNEXPECTED_SIZE = 3,
ERROR_CANNOT_COPY_FILE = 4,
ERROR_CANNOT_OPEN_SHM = 5,
};
const char *image_uploading_failure_strings[6] = {
"NO_ERROR",
"ERROR_OVER_SIZE_LIMIT",
"ERROR_CANNOT_OPEN_CACHED_FILE",
"ERROR_UNEXPECTED_SIZE",
"ERROR_CANNOT_COPY_FILE",
"ERROR_CANNOT_OPEN_SHM",
};
////////////////////////////////////////////////////////////////////////////////
//
// We use the following structures to represent images and placements:
//
// - Image: this is the main structure representing an image, usually created
// by actions 'a=t', 'a=T`. Each image has an id (image id aka client id,
// specified by 'i='). An image may have multiple frames (ImageFrame) and
// placements (ImagePlacement).
//
// - ImageFrame: represents a single frame of an image, usually created by
// the action 'a=f' (and the first frame is created with the image itself).
// Each frame has an index and also:
// - a file containing the frame data (considered to be "on disk", although
// it's probably in tmpfs),
// - an imlib object containing the fully composed frame (i.e. the frame
// data from the file composed onto the background frame or color). It is
// not ready for display yet, because it needs to be scaled and uploaded
// to the X server.
//
// - ImagePlacement: represents a placement of an image, created by 'a=p' and
// 'a=T'. Each placement has an id (placement id, specified by 'p='). Also
// each placement has an array of pixmaps: one for each frame of the image.
// Each pixmap is a scaled and uploaded image ready to be displayed.
//
// Images are store in the `images` hash table, mapping image ids to Image
// objects (allocated on the heap).
//
// Placements are stored in the `placements` hash table of each Image object,
// mapping placement ids to ImagePlacement objects (also allocated on the heap).
//
// ImageFrames are stored in the `first_frame` field and in the
// `frames_beyond_the_first` array of each Image object. They are stored by
// value, so ImageFrame pointer may be invalidated when frames are
// added/deleted, be careful.
//
////////////////////////////////////////////////////////////////////////////////
struct Image;
struct ImageFrame;
struct ImagePlacement;
KHASH_MAP_INIT_INT(id2image, struct Image *)
KHASH_MAP_INIT_INT(id2placement, struct ImagePlacement *)
typedef struct ImageFrame {
/// The image this frame belongs to.
struct Image *image;
/// The 1-based index of the frame. Zero if the frame isn't initialized.
int index;
/// The last time when the frame was displayed or otherwise touched.
Milliseconds atime;
/// The background color of the frame in the 0xRRGGBBAA format.
uint32_t background_color;
/// The index of the background frame. Zero to use the color instead.
int background_frame_index;
/// The duration of the frame in milliseconds.
int gap;
/// The expected size of the frame image file (specified with 'S='),
/// used to check if uploading succeeded.
unsigned expected_size;
/// Format specification (see the `f=` key).
int format;
/// Pixel width and height of the non-composed (on-disk) frame data. May
/// differ from the image (i.e. first frame) dimensions.
int data_pix_width, data_pix_height;
/// The offset of the frame relative to the first frame.
int x, y;
/// Compression mode (see the `o=` key).
char compression;
/// The status (see `ImageStatus`).
char status;
/// The reason of uploading failure (see `ImageUploadingFailure`).
char uploading_failure;
/// Whether failures and successes should be reported ('q=').
char quiet;
/// Whether to blend the frame with the background or replace it.
char blend;
/// The file corresponding to the on-disk cache, used when uploading.
FILE *open_file;
/// The size of the corresponding file cached on disk.
unsigned disk_size;
/// The imlib object containing the fully composed frame. It's not
/// scaled for screen display yet.
Imlib_Image imlib_object;
} ImageFrame;
typedef struct Image {
/// The client id (the one specified with 'i='). Must be nonzero.
uint32_t image_id;
/// The client id specified in the query command (`a=q`). This one must
/// be used to create the response if it's non-zero.
uint32_t query_id;
/// The number specified in the transmission command (`I=`). If
/// non-zero, it may be used to identify the image instead of the
/// image_id, and it also should be mentioned in responses.
uint32_t image_number;
/// The last time when the image was displayed or otherwise touched.
Milliseconds atime;
/// The total duration of the animation in milliseconds.
int total_duration;
/// The total size of cached image files for all frames.
int total_disk_size;
/// The global index of the creation command. Used to decide which image
/// is newer if they have the same image number.
uint64_t global_command_index;
/// The 1-based index of the currently displayed frame.
int current_frame;
/// The state of the animation, see `AnimationState`.
char animation_state;
/// The absolute time that is assumed to be the start of the current
/// frame (in ms since initialization).
Milliseconds current_frame_time;
/// The absolute time of the last redraw (in ms since initialization).
/// Used to check whether it's the first time we draw the image in the
/// current redraw cycle.
Milliseconds last_redraw;
/// The absolute time of the next redraw (in ms since initialization).
/// 0 means no redraw is scheduled.
Milliseconds next_redraw;
/// The unscaled pixel width and height of the image. Usually inherited
/// from the first frame.
int pix_width, pix_height;
/// The first frame.
ImageFrame first_frame;
/// The array of frames beyond the first one.
kvec_t(ImageFrame) frames_beyond_the_first;
/// Image placements.
khash_t(id2placement) *placements;
/// The default placement.
uint32_t default_placement;
/// The initial placement id, specified with the transmission command,
/// used to report success or failure.
uint32_t initial_placement_id;
} Image;
typedef struct ImagePlacement {
/// The image this placement belongs to.
Image *image;
/// The id of the placement. Must be nonzero.
uint32_t placement_id;
/// The last time when the placement was displayed or otherwise touched.
Milliseconds atime;
/// The 1-based index of the protected pixmap. We protect a pixmap in
/// gr_load_pixmap to avoid unloading it right after it was loaded.
int protected_frame;
/// Whether the placement is used only for Unicode placeholders.
char virtual;
/// The scaling mode (see `ScaleMode`).
char scale_mode;
/// Height and width in cells.
uint16_t rows, cols;
/// Top-left corner of the source rectangle ('x=' and 'y=').
int src_pix_x, src_pix_y;
/// Height and width of the source rectangle (zero if full image).
int src_pix_width, src_pix_height;
/// The image appropriately scaled and uploaded to the X server. This
/// pixmap is premultiplied by alpha.
Pixmap first_pixmap;
/// The array of pixmaps beyond the first one.
kvec_t(Pixmap) pixmaps_beyond_the_first;
/// The dimensions of the cell used to scale the image. If cell
/// dimensions are changed (font change), the image will be rescaled.
uint16_t scaled_cw, scaled_ch;
/// If true, do not move the cursor when displaying this placement
/// (non-virtual placements only).
char do_not_move_cursor;
} ImagePlacement;
/// A rectangular piece of an image to be drawn.
typedef struct {
uint32_t image_id;
uint32_t placement_id;
/// The position of the rectangle in pixels.
int screen_x_pix, screen_y_pix;
/// The starting row on the screen.
int screen_y_row;
/// The part of the whole image to be drawn, in cells. Starts are
/// zero-based, ends are exclusive.
int img_start_col, img_end_col, img_start_row, img_end_row;
/// The current cell width and height in pixels.
int cw, ch;
/// Whether colors should be inverted.
int reverse;
} ImageRect;
/// Executes `code` for each frame of an image. Example:
///
/// foreach_frame(image, frame, {
/// printf("Frame %d\n", frame->index);
/// });
///
#define foreach_frame(image, framevar, code) { size_t __i; \
for (__i = 0; __i <= kv_size((image).frames_beyond_the_first); ++__i) { \
ImageFrame *framevar = \
__i == 0 ? &(image).first_frame \
: &kv_A((image).frames_beyond_the_first, __i - 1); \
code; \
} }
/// Executes `code` for each pixmap of a placement. Example:
///
/// foreach_pixmap(placement, pixmap, {
/// ...
/// });
///
#define foreach_pixmap(placement, pixmapvar, code) { size_t __i; \
for (__i = 0; __i <= kv_size((placement).pixmaps_beyond_the_first); ++__i) { \
Pixmap pixmapvar = \
__i == 0 ? (placement).first_pixmap \
: kv_A((placement).pixmaps_beyond_the_first, __i - 1); \
code; \
} }
static Image *gr_find_image(uint32_t image_id);
static void gr_get_frame_filename(ImageFrame *frame, char *out, size_t max_len);
static void gr_delete_image(Image *img);
static void gr_check_limits();
static char *gr_base64dec(const char *src, size_t *size);
static void sanitize_str(char *str, size_t max_len);
static const char *sanitized_filename(const char *str);
/// The array of image rectangles to draw. It is reset each frame.
static ImageRect image_rects[MAX_IMAGE_RECTS] = {{0}};
/// The known images (including the ones being uploaded).
static khash_t(id2image) *images = NULL;
/// The total number of placements in all images.
static unsigned total_placement_count = 0;
/// The total size of all image files stored in the on-disk cache.
static int64_t images_disk_size = 0;
/// The total size of all images and placements loaded into ram.
static int64_t images_ram_size = 0;
/// The id of the last loaded image.
static uint32_t last_image_id = 0;
/// Current cell width and heigh in pixels.
static int current_cw = 0, current_ch = 0;
/// The id of the currently uploaded image (when using direct uploading).
static uint32_t current_upload_image_id = 0;
/// The index of the frame currently being uploaded.
static int current_upload_frame_index = 0;
/// The time when the graphics module was initialized.
static struct timespec initialization_time = {0};
/// The time when the current frame drawing started, used for debugging fps and
/// to calculate the current frame for animations.
static Milliseconds drawing_start_time;
/// The global index of the current command.
static uint64_t global_command_counter = 0;
/// The next redraw times for each row of the terminal. Used for animations.
/// 0 means no redraw is scheduled.
static kvec_t(Milliseconds) next_redraw_times = {0, 0, NULL};
/// The number of files loaded in the current redraw cycle or command execution.
static int debug_loaded_files_counter = 0;
/// The number of pixmaps loaded in the current redraw cycle or command execution.
static int debug_loaded_pixmaps_counter = 0;
/// The directory where the cache files are stored.
static char cache_dir[MAX_FILENAME_SIZE - 16];
/// The table used for color inversion.
static unsigned char reverse_table[256];
// Declared in the header.
GraphicsDebugMode graphics_debug_mode = GRAPHICS_DEBUG_NONE;
char graphics_display_images = 1;
GraphicsCommandResult graphics_command_result = {0};
int graphics_next_redraw_delay = INT_MAX;
// Defined in config.h
extern const char graphics_cache_dir_template[];
extern unsigned graphics_max_single_image_file_size;
extern unsigned graphics_total_file_cache_size;
extern unsigned graphics_max_single_image_ram_size;
extern unsigned graphics_max_total_ram_size;
extern unsigned graphics_max_total_placements;
extern double graphics_excess_tolerance_ratio;
extern unsigned graphics_animation_min_delay;
////////////////////////////////////////////////////////////////////////////////
// Basic helpers.
////////////////////////////////////////////////////////////////////////////////
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
/// Returns the difference between `end` and `start` in milliseconds.
static int64_t gr_timediff_ms(const struct timespec *end,
const struct timespec *start) {
return (end->tv_sec - start->tv_sec) * 1000 +
(end->tv_nsec - start->tv_nsec) / 1000000;
}
/// Returns the current time in milliseconds since the initialization.
static Milliseconds gr_now_ms() {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return gr_timediff_ms(&now, &initialization_time);
}
////////////////////////////////////////////////////////////////////////////////
// Logging.
////////////////////////////////////////////////////////////////////////////////
#define GR_LOG(...) \
do { if(graphics_debug_mode) fprintf(stderr, __VA_ARGS__); } while(0)
////////////////////////////////////////////////////////////////////////////////
// Basic image management functions (create, delete, find, etc).
////////////////////////////////////////////////////////////////////////////////
/// Returns the 1-based index of the last frame. Note that you may want to use
/// `gr_last_uploaded_frame_index` instead since the last frame may be not
/// fully uploaded yet.
static inline int gr_last_frame_index(Image *img) {
return kv_size(img->frames_beyond_the_first) + 1;
}
/// Returns the frame with the given index. Returns NULL if the index is out of
/// bounds. The index is 1-based.
static ImageFrame *gr_get_frame(Image *img, int index) {
if (!img)
return NULL;
if (index == 1)
return &img->first_frame;
if (2 <= index && index <= gr_last_frame_index(img))
return &kv_A(img->frames_beyond_the_first, index - 2);
return NULL;
}
/// Returns the last frame of the image. Returns NULL if `img` is NULL.
static ImageFrame *gr_get_last_frame(Image *img) {
if (!img)
return NULL;
return gr_get_frame(img, gr_last_frame_index(img));
}
/// Returns the 1-based index of the last frame or the second-to-last frame if
/// the last frame is not fully uploaded yet.
static inline int gr_last_uploaded_frame_index(Image *img) {
int last_index = gr_last_frame_index(img);
if (last_index > 1 &&
gr_get_frame(img, last_index)->status < STATUS_UPLOADING_SUCCESS)
return last_index - 1;
return last_index;
}
/// Returns the pixmap for the frame with the given index. Returns 0 if the
/// index is out of bounds. The index is 1-based.
static Pixmap gr_get_frame_pixmap(ImagePlacement *placement, int index) {
if (index == 1)
return placement->first_pixmap;
if (2 <= index &&
index <= kv_size(placement->pixmaps_beyond_the_first) + 1)
return kv_A(placement->pixmaps_beyond_the_first, index - 2);
return 0;
}
/// Sets the pixmap for the frame with the given index. The index is 1-based.
/// The array of pixmaps is resized if needed.
static void gr_set_frame_pixmap(ImagePlacement *placement, int index,
Pixmap pixmap) {
if (index == 1) {
placement->first_pixmap = pixmap;
return;
}
// Resize the array if needed.
size_t old_size = kv_size(placement->pixmaps_beyond_the_first);
if (old_size < index - 1) {
kv_a(Pixmap, placement->pixmaps_beyond_the_first, index - 2);
for (size_t i = old_size; i < index - 1; i++)
kv_A(placement->pixmaps_beyond_the_first, i) = 0;
}
kv_A(placement->pixmaps_beyond_the_first, index - 2) = pixmap;
}
/// Finds the image corresponding to the client id. Returns NULL if cannot find.
static Image *gr_find_image(uint32_t image_id) {
khiter_t k = kh_get(id2image, images, image_id);
if (k == kh_end(images))
return NULL;
Image *res = kh_value(images, k);
return res;
}
/// Finds the newest image corresponding to the image number. Returns NULL if
/// cannot find.
static Image *gr_find_image_by_number(uint32_t image_number) {
if (image_number == 0)
return NULL;
Image *newest_img = NULL;
Image *img = NULL;
kh_foreach_value(images, img, {
if (img->image_number == image_number &&
(!newest_img || newest_img->global_command_index <
img->global_command_index))
newest_img = img;
});
if (!newest_img)
GR_LOG("Image number %u not found\n", image_number);
else
GR_LOG("Found image number %u, its id is %u\n", image_number,
img->image_id);
return newest_img;
}
/// Finds the placement corresponding to the id. If the placement id is 0,
/// returns some default placement.
static ImagePlacement *gr_find_placement(Image *img, uint32_t placement_id) {
if (!img)
return NULL;
if (placement_id == 0) {
// Try to get the default placement.
ImagePlacement *dflt = NULL;
if (img->default_placement != 0)
dflt = gr_find_placement(img, img->default_placement);
if (dflt)
return dflt;
// If there is no default placement, return the first one and
// set it as the default.
kh_foreach_value(img->placements, dflt, {
img->default_placement = dflt->placement_id;
return dflt;
});
// If there are no placements, return NULL.
return NULL;
}
khiter_t k = kh_get(id2placement, img->placements, placement_id);
if (k == kh_end(img->placements))
return NULL;
ImagePlacement *res = kh_value(img->placements, k);
return res;
}
/// Finds the placement by image id and placement id.
static ImagePlacement *gr_find_image_and_placement(uint32_t image_id,
uint32_t placement_id) {
return gr_find_placement(gr_find_image(image_id), placement_id);
}
/// Writes the name of the on-disk cache file to `out`. `max_len` should be the
/// size of `out`. The name will be something like
/// "/tmp/st-images-xxx/img-ID-FRAME".
static void gr_get_frame_filename(ImageFrame *frame, char *out,
size_t max_len) {
snprintf(out, max_len, "%s/img-%.3u-%.3u", cache_dir,
frame->image->image_id, frame->index);
}
/// Returns the (estimation) of the RAM size used by the frame right now.
static unsigned gr_frame_current_ram_size(ImageFrame *frame) {
if (!frame->imlib_object)
return 0;
return (unsigned)frame->image->pix_width * frame->image->pix_height * 4;
}
/// Returns the (estimation) of the RAM size used by a single frame pixmap.
static unsigned gr_placement_single_frame_ram_size(ImagePlacement *placement) {
return (unsigned)placement->rows * placement->cols *
placement->scaled_ch * placement->scaled_cw * 4;
}
/// Returns the (estimation) of the RAM size used by the placemenet right now.
static unsigned gr_placement_current_ram_size(ImagePlacement *placement) {
unsigned single_frame_size =
gr_placement_single_frame_ram_size(placement);
unsigned result = 0;
foreach_pixmap(*placement, pixmap, {
if (pixmap)
result += single_frame_size;
});
return result;
}
/// Unload the frame from RAM (i.e. delete the corresponding imlib object).
/// If the on-disk file of the frame is preserved, it can be reloaded later.
static void gr_unload_frame(ImageFrame *frame) {
if (!frame->imlib_object)
return;
unsigned frame_ram_size = gr_frame_current_ram_size(frame);
images_ram_size -= frame_ram_size;
imlib_context_set_image(frame->imlib_object);
imlib_free_image_and_decache();
frame->imlib_object = NULL;
GR_LOG("After unloading image %u frame %u (atime %ld ms ago) "
"ram: %ld KiB (- %u KiB)\n",
frame->image->image_id, frame->index,
drawing_start_time - frame->atime, images_ram_size / 1024,
frame_ram_size / 1024);
}
/// Unload all frames of the image.
static void gr_unload_all_frames(Image *img) {
foreach_frame(*img, frame, {
gr_unload_frame(frame);
});
}
/// Unload the placement from RAM (i.e. free all of the corresponding pixmaps).
/// If the on-disk files or imlib objects of the corresponding image are
/// preserved, the placement can be reloaded later.
static void gr_unload_placement(ImagePlacement *placement) {
unsigned placement_ram_size = gr_placement_current_ram_size(placement);
images_ram_size -= placement_ram_size;
Display *disp = imlib_context_get_display();
foreach_pixmap(*placement, pixmap, {
if (pixmap)
XFreePixmap(disp, pixmap);
});
placement->first_pixmap = 0;
placement->pixmaps_beyond_the_first.n = 0;
placement->scaled_ch = placement->scaled_cw = 0;
GR_LOG("After unloading placement %u/%u (atime %ld ms ago) "
"ram: %ld KiB (- %u KiB)\n",
placement->image->image_id, placement->placement_id,
drawing_start_time - placement->atime, images_ram_size / 1024,
placement_ram_size / 1024);
}
/// Unload a single pixmap of the placement from RAM.
static void gr_unload_pixmap(ImagePlacement *placement, int frameidx) {
Pixmap pixmap = gr_get_frame_pixmap(placement, frameidx);
if (!pixmap)
return;
Display *disp = imlib_context_get_display();
XFreePixmap(disp, pixmap);
gr_set_frame_pixmap(placement, frameidx, 0);
images_ram_size -= gr_placement_single_frame_ram_size(placement);
GR_LOG("After unloading pixmap %ld of "
"placement %u/%u (atime %ld ms ago) "
"frame %u (atime %ld ms ago) "
"ram: %ld KiB (- %u KiB)\n",
pixmap, placement->image->image_id, placement->placement_id,
drawing_start_time - placement->atime, frameidx,
drawing_start_time -
gr_get_frame(placement->image, frameidx)->atime,
images_ram_size / 1024,
gr_placement_single_frame_ram_size(placement) / 1024);
}
/// Deletes the on-disk cache file corresponding to the frame. The in-ram image
/// object (if it exists) is not deleted, placements are not unloaded either.
static void gr_delete_imagefile(ImageFrame *frame) {
// It may still be being loaded. Close the file in this case.
if (frame->open_file) {
fclose(frame->open_file);
frame->open_file = NULL;
}
if (frame->disk_size == 0)
return;
char filename[MAX_FILENAME_SIZE];
gr_get_frame_filename(frame, filename, MAX_FILENAME_SIZE);
remove(filename);
unsigned disk_size = frame->disk_size;
images_disk_size -= disk_size;
frame->image->total_disk_size -= disk_size;
frame->disk_size = 0;
GR_LOG("After deleting image file %u frame %u (atime %ld ms ago) "
"disk: %ld KiB (- %u KiB)\n",
frame->image->image_id, frame->index,
drawing_start_time - frame->atime, images_disk_size / 1024,
disk_size / 1024);
}
/// Deletes all on-disk cache files of the image (for each frame).
static void gr_delete_imagefiles(Image *img) {
foreach_frame(*img, frame, {
gr_delete_imagefile(frame);
});
}
/// Deletes the given placement: unloads, frees the object, but doesn't change
/// the `placements` hash table.
static void gr_delete_placement_keep_id(ImagePlacement *placement) {
if (!placement)
return;
GR_LOG("Deleting placement %u/%u\n", placement->image->image_id,
placement->placement_id);
gr_unload_placement(placement);
kv_destroy(placement->pixmaps_beyond_the_first);
free(placement);
total_placement_count--;
}
/// Deletes all placements of `img`.
static void gr_delete_all_placements(Image *img) {
ImagePlacement *placement = NULL;
kh_foreach_value(img->placements, placement, {
gr_delete_placement_keep_id(placement);
});
kh_clear(id2placement, img->placements);
}
/// Deletes the given image: unloads, deletes the file, frees the Image object,
/// but doesn't change the `images` hash table.
static void gr_delete_image_keep_id(Image *img) {
if (!img)
return;
GR_LOG("Deleting image %u\n", img->image_id);
foreach_frame(*img, frame, {
gr_delete_imagefile(frame);
gr_unload_frame(frame);
});
kv_destroy(img->frames_beyond_the_first);
gr_delete_all_placements(img);
kh_destroy(id2placement, img->placements);
free(img);
}
/// Deletes the given image: unloads, deletes the file, frees the Image object,
/// and also removes it from `images`.
static void gr_delete_image(Image *img) {
if (!img)
return;
uint32_t id = img->image_id;
gr_delete_image_keep_id(img);
khiter_t k = kh_get(id2image, images, id);
kh_del(id2image, images, k);
}
/// Deletes the given placement: unloads, frees the object, and also removes it
/// from `placements`.
static void gr_delete_placement(ImagePlacement *placement) {
if (!placement)
return;
uint32_t id = placement->placement_id;
Image *img = placement->image;
gr_delete_placement_keep_id(placement);
khiter_t k = kh_get(id2placement, img->placements, id);
kh_del(id2placement, img->placements, k);
}
/// Deletes all images and clears `images`.
static void gr_delete_all_images() {
Image *img = NULL;
kh_foreach_value(images, img, {
gr_delete_image_keep_id(img);
});
kh_clear(id2image, images);
}
/// Update the atime of the image.
static void gr_touch_image(Image *img) {
img->atime = gr_now_ms();
}
/// Update the atime of the frame.
static void gr_touch_frame(ImageFrame *frame) {
frame->image->atime = frame->atime = gr_now_ms();
}
/// Update the atime of the placement. Touches the images too.
static void gr_touch_placement(ImagePlacement *placement) {
placement->image->atime = placement->atime = gr_now_ms();
}
/// Creates a new image with the given id. If an image with that id already
/// exists, it is deleted first. If the provided id is 0, generates a
/// random id.
static Image *gr_new_image(uint32_t id) {
if (id == 0) {
do {
id = rand();
// Avoid IDs that don't need full 32 bits.
} while ((id & 0xFF000000) == 0 || (id & 0x00FFFF00) == 0 ||
gr_find_image(id));
GR_LOG("Generated random image id %u\n", id);
}
Image *img = gr_find_image(id);
gr_delete_image_keep_id(img);
GR_LOG("Creating image %u\n", id);
img = malloc(sizeof(Image));
memset(img, 0, sizeof(Image));
img->placements = kh_init(id2placement);
int ret;
khiter_t k = kh_put(id2image, images, id, &ret);
kh_value(images, k) = img;
img->image_id = id;
gr_touch_image(img);
img->global_command_index = global_command_counter;
return img;
}
/// Creates a new frame at the end of the frame array. It may be the first frame
/// if there are no frames yet.
static ImageFrame *gr_append_new_frame(Image *img) {
ImageFrame *frame = NULL;
if (img->first_frame.index == 0 &&
kv_size(img->frames_beyond_the_first) == 0) {
frame = &img->first_frame;
frame->index = 1;
} else {
frame = kv_pushp(ImageFrame, img->frames_beyond_the_first);
memset(frame, 0, sizeof(ImageFrame));
frame->index = kv_size(img->frames_beyond_the_first) + 1;
}
frame->image = img;
gr_touch_frame(frame);
GR_LOG("Appending frame %d to image %u\n", frame->index, img->image_id);
return frame;
}
/// Creates a new placement with the given id. If a placement with that id
/// already exists, it is deleted first. If the provided id is 0, generates a
/// random id.
static ImagePlacement *gr_new_placement(Image *img, uint32_t id) {
if (id == 0) {
do {
// Currently we support only 24-bit IDs.
id = rand() & 0xFFFFFF;
// Avoid IDs that need only one byte.
} while ((id & 0x00FFFF00) == 0 || gr_find_placement(img, id));
}
ImagePlacement *placement = gr_find_placement(img, id);
gr_delete_placement_keep_id(placement);
GR_LOG("Creating placement %u/%u\n", img->image_id, id);
placement = malloc(sizeof(ImagePlacement));
memset(placement, 0, sizeof(ImagePlacement));
total_placement_count++;
int ret;
khiter_t k = kh_put(id2placement, img->placements, id, &ret);
kh_value(img->placements, k) = placement;
placement->image = img;
placement->placement_id = id;
gr_touch_placement(placement);
if (img->default_placement == 0)
img->default_placement = id;
return placement;
}
static int64_t ceil_div(int64_t a, int64_t b) {
return (a + b - 1) / b;
}
/// Computes the best number of rows and columns for a placement if it's not
/// specified, and also adjusts the source rectangle size.
static void gr_infer_placement_size_maybe(ImagePlacement *placement) {
// The size of the image.
int image_pix_width = placement->image->pix_width;
int image_pix_height = placement->image->pix_height;
// Negative values are not allowed. Quietly set them to 0.
if (placement->src_pix_x < 0)
placement->src_pix_x = 0;
if (placement->src_pix_y < 0)
placement->src_pix_y = 0;
if (placement->src_pix_width < 0)
placement->src_pix_width = 0;
if (placement->src_pix_height < 0)
placement->src_pix_height = 0;
// If the source rectangle is outside the image, truncate it.
if (placement->src_pix_x > image_pix_width)
placement->src_pix_x = image_pix_width;
if (placement->src_pix_y > image_pix_height)
placement->src_pix_y = image_pix_height;
// If the source rectangle is not specified, use the whole image. If
// it's partially outside the image, truncate it.
if (placement->src_pix_width == 0 ||
placement->src_pix_x + placement->src_pix_width > image_pix_width)
placement->src_pix_width =
image_pix_width - placement->src_pix_x;
if (placement->src_pix_height == 0 ||
placement->src_pix_y + placement->src_pix_height > image_pix_height)
placement->src_pix_height =
image_pix_height - placement->src_pix_y;
if (placement->cols != 0 && placement->rows != 0)
return;
if (placement->src_pix_width == 0 || placement->src_pix_height == 0)
return;
if (current_cw == 0 || current_ch == 0)
return;
// If no size is specified, use the image size.
if (placement->cols == 0 && placement->rows == 0) {
placement->cols =
ceil_div(placement->src_pix_width, current_cw);
placement->rows =
ceil_div(placement->src_pix_height, current_ch);
return;
}
// Some applications specify only one of the dimensions.
if (placement->scale_mode == SCALE_MODE_CONTAIN) {
// If we preserve aspect ratio and fit to width/height, the most
// logical thing is to find the minimum size of the
// non-specified dimension that allows the image to fit the
// specified dimension.
if (placement->cols == 0) {
placement->cols = ceil_div(
placement->src_pix_width * placement->rows *
current_ch,
placement->src_pix_height * current_cw);
return;
}
if (placement->rows == 0) {
placement->rows =
ceil_div(placement->src_pix_height *
placement->cols * current_cw,
placement->src_pix_width * current_ch);
return;
}
} else {
// Otherwise we stretch the image or preserve the original size.
// In both cases we compute the best number of columns from the
// pixel size and cell size.
// TODO: In the case of stretching it's not the most logical
// thing to do, may need to revisit in the future.
// Currently we switch to SCALE_MODE_CONTAIN when only one
// of the dimensions is specified, so this case shouldn't
// happen in practice.
if (!placement->cols)
placement->cols =
ceil_div(placement->src_pix_width, current_cw);
if (!placement->rows)
placement->rows =
ceil_div(placement->src_pix_height, current_ch);
}
}
/// Adjusts the current frame index if enough time has passed since the display
/// of the current frame. Also computes the time of the next redraw of this
/// image (`img->next_redraw`). The current time is passed as an argument so
/// that all animations are in sync.
static void gr_update_frame_index(Image *img, Milliseconds now) {
if (img->current_frame == 0) {
img->current_frame_time = now;
img->current_frame = 1;
img->next_redraw = now + MAX(1, img->first_frame.gap);
return;
}
// If the animation is stopped, show the current frame.
if (!img->animation_state ||
img->animation_state == ANIMATION_STATE_STOPPED ||
img->animation_state == ANIMATION_STATE_UNSET) {
// The next redraw is never (unless the state is changed).
img->next_redraw = 0;
return;
}
int last_uploaded_frame_index = gr_last_uploaded_frame_index(img);
// If we are loading and we reached the last frame, show the last frame.
if (img->animation_state == ANIMATION_STATE_LOADING &&
img->current_frame == last_uploaded_frame_index) {
// The next redraw is never (unless the state is changed or
// frames are added).
img->next_redraw = 0;
return;
}
// Check how many milliseconds passed since the current frame was shown.
int passed_ms = now - img->current_frame_time;
// If the animation is looping and too much time has passes, we can
// make a shortcut.
if (img->animation_state == ANIMATION_STATE_LOOPING &&
img->total_duration > 0 && passed_ms >= img->total_duration) {
passed_ms %= img->total_duration;
img->current_frame_time = now - passed_ms;
}
// Find the next frame.