-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathmain.c
2484 lines (2049 loc) · 65.4 KB
/
main.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
#include <windows.h>
#include <windowsx.h>
#include "audio.c"
static TRACKMOUSEEVENT tme = {sizeof(TRACKMOUSEEVENT), TME_LEAVE, 0, 0};
static _Bool mouse_tracked = 0;
_Bool draw = 0;
float scale = 1.0;
_Bool connected = 0;
_Bool havefocus;
BLENDFUNCTION blend_function = {
.BlendOp = AC_SRC_OVER,
.BlendFlags = 0,
.SourceConstantAlpha = 0xFF,
.AlphaFormat = AC_SRC_ALPHA
};
/** Select the true main.c for legacy XP support.
* else default to xlib
**/
#ifdef __WIN_LEGACY
#include "main.XP.c"
#else
#include "main.7.c"
#endif
/** Translate a char* from UTF-8 encoding to OS native;
*
* Accepts char_t pointer, native array pointer, length of input;
* Returns: number of chars writen, or 0 on failure.
*
*/
static int utf8tonative(char_t *str, wchar_t *out, int length){
return MultiByteToWideChar(CP_UTF8, 0, (char*)str, length, out, length);
}
static int utf8str_to_native(char_t *str, wchar_t *out, int length){
/* must be null terminated string ↓ */
return MultiByteToWideChar(CP_UTF8, 0, (char*)str, -1, out, length);
}
void postmessage(uint32_t msg, uint16_t param1, uint16_t param2, void *data)
{
PostMessage(hwnd, WM_TOX + (msg), ((param1) << 16) | (param2), (LPARAM)data);
}
void drawalpha(int bm, int x, int y, int width, int height, uint32_t color)
{
if(!bitmap[bm]) {
return;
}
BITMAPINFO bmi = {
.bmiHeader = {
.biSize = sizeof(BITMAPINFOHEADER),
.biWidth = width,
.biHeight = -height,
.biPlanes = 1,
.biBitCount = 32,
.biCompression = BI_RGB,
}
};
// create pointer to beginning and end of the alpha-channel-only bitmap
uint8_t *alpha_pixel = bitmap[bm], *end = alpha_pixel + width * height;
// create temporary bitmap we'll combine the alpha and colors on
uint32_t *out_pixel;
HBITMAP temp = CreateDIBSection(hdcMem, &bmi, DIB_RGB_COLORS, (void**)&out_pixel, NULL, 0);
SelectObject(hdcMem, temp);
// create pixels for the drawable bitmap based on the alpha value of
// each pixel in the alpha bitmap and the color given by 'color',
// the Win32 API requires we pre-apply our alpha channel as well by
// doing (color * alpha / 255) for each color channel
// NOTE: Input color is in the format 0BGR, output pixel is in the format BGRA
while(alpha_pixel != end) {
uint8_t alpha = *alpha_pixel++;
*out_pixel++ = (((color & 0xFF) * alpha / 255) << 16) // red
| ((((color >> 8) & 0xFF) * alpha / 255) << 8) // green
| ((((color >> 16) & 0xFF) * alpha / 255) << 0) // blue
| (alpha << 24); // alpha
}
// draw temporary bitmap on screen
AlphaBlend(hdc, x, y, width, height, hdcMem, 0, 0, width, height, blend_function);
// clean up
DeleteObject(temp);
}
void image_set_filter(UTOX_NATIVE_IMAGE *image, uint8_t filter)
{
switch (filter) {
case FILTER_NEAREST:
image->stretch_mode = COLORONCOLOR;
break;
case FILTER_BILINEAR:
image->stretch_mode = HALFTONE;
break;
default:
debug("Warning: Tried to set image to unrecognized filter(%u).\n", filter);
return;
}
}
void image_set_scale(UTOX_NATIVE_IMAGE *image, double img_scale)
{
image->scaled_width = (uint32_t)(((double)image->width * img_scale) + 0.5);
image->scaled_height = (uint32_t)(((double)image->height * img_scale) + 0.5);
}
static _Bool image_is_stretched(const UTOX_NATIVE_IMAGE *image)
{
return image->width != image->scaled_width ||
image->height != image->scaled_height;
}
// NOTE: This function is way more complicated than the XRender variant, because
// the Win32 API is a lot more limited, so all scaling, clipping, and handling
// transparency has to be done explicitly
void draw_image(const UTOX_NATIVE_IMAGE *image, int x, int y, uint32_t width, uint32_t height, uint32_t imgx, uint32_t imgy)
{
HDC drawdc; // device context we'll do the eventual drawing with
HBITMAP tmp = NULL; // used when scaling
if (!image_is_stretched(image)) {
SelectObject(hdcMem, image->bitmap);
drawdc = hdcMem;
} else {
// temporary device context for the scaling operation
drawdc = CreateCompatibleDC(NULL);
// set stretch mode from image
SetStretchBltMode(drawdc, image->stretch_mode);
// scaled bitmap will be drawn onto this bitmap
tmp = CreateCompatibleBitmap(hdcMem, image->scaled_width, image->scaled_height);
SelectObject(drawdc, tmp);
SelectObject(hdcMem, image->bitmap);
// stretch image onto temporary bitmap
if (image->has_alpha) {
AlphaBlend(drawdc, 0, 0, image->scaled_width, image->scaled_height, hdcMem, 0, 0, image->width, image->height, blend_function);
} else {
StretchBlt(drawdc, 0, 0, image->scaled_width, image->scaled_height, hdcMem, 0, 0, image->width, image->height, SRCCOPY);
}
}
// clip and draw
if (image->has_alpha) {
AlphaBlend(hdc, x, y, width, height, drawdc, imgx, imgy, width, height, blend_function);
} else {
BitBlt(hdc, x, y, width, height, drawdc, imgx, imgy, SRCCOPY);
}
// clean up
if (image_is_stretched(image)) {
DeleteObject(tmp);
DeleteDC(drawdc);
}
}
void drawtext(int x, int y, char_t *str, STRING_IDX length)
{
wchar_t out[length];
length = utf8tonative(str, out, length);
TextOutW(hdc, x, y, out, length);
}
int drawtext_getwidth(int x, int y, char_t *str, STRING_IDX length)
{
wchar_t out[length];
length = utf8tonative(str, out, length);
SIZE size;
TextOutW(hdc, x, y, out, length);
GetTextExtentPoint32W(hdc, out, length, &size);
return size.cx;
}
void drawtextwidth(int x, int width, int y, char_t *str, STRING_IDX length)
{
wchar_t out[length];
length = utf8tonative(str, out, length);
RECT r = {x, y, x + width, y + 256};
DrawTextW(hdc, out, length, &r, DT_SINGLELINE | DT_END_ELLIPSIS | DT_NOPREFIX);
}
void drawtextwidth_right(int x, int width, int y, char_t *str, STRING_IDX length)
{
wchar_t out[length];
length = utf8tonative(str, out, length);
RECT r = {x, y, x + width, y + 256};
DrawTextW(hdc, out, length, &r, DT_SINGLELINE | DT_END_ELLIPSIS | DT_NOPREFIX | DT_RIGHT);
}
void drawtextrange(int x, int x2, int y, char_t *str, STRING_IDX length)
{
wchar_t out[length];
length = utf8tonative(str, out, length);
RECT r = {x, y, x2, y + 256};
DrawTextW(hdc, out, length, &r, DT_SINGLELINE | DT_END_ELLIPSIS | DT_NOPREFIX);
}
void drawtextrangecut(int x, int x2, int y, char_t *str, STRING_IDX length)
{
wchar_t out[length];
length = utf8tonative(str, out, length);
RECT r = {x, y, x2, y + 256};
DrawTextW(hdc, out, length, &r, DT_SINGLELINE | DT_NOPREFIX);
}
int textwidth(char_t *str, STRING_IDX length)
{
wchar_t out[length];
length = utf8tonative(str, out, length);
SIZE size;
GetTextExtentPoint32W(hdc, out, length, &size);
return size.cx;
}
int textfit(char_t *str, STRING_IDX len, int width)
{
wchar_t out[len];
int length = utf8tonative(str, out, len);
int fit;
SIZE size;
GetTextExtentExPointW(hdc, out, length, width, &fit, NULL, &size);
return WideCharToMultiByte(CP_UTF8, 0, out, fit, (char*)str, len, NULL, 0);
}
int textfit_near(char_t *str, STRING_IDX len, int width)
{
/*todo: near*/
wchar_t out[len];
int length = utf8tonative(str, out, len);
int fit;
SIZE size;
GetTextExtentExPointW(hdc, out, length, width, &fit, NULL, &size);
return WideCharToMultiByte(CP_UTF8, 0, out, fit, (char*)str, len, NULL, 0);
}
void framerect(int x, int y, int right, int bottom, uint32_t color)
{
RECT r = {x, y, right, bottom};
SetDCBrushColor(hdc, color);
FrameRect(hdc, &r, hdc_brush);
}
void drawrect(int x, int y, int right, int bottom, uint32_t color)
{
RECT r = {x, y, right, bottom};
SetDCBrushColor(hdc, color);
FillRect(hdc, &r, hdc_brush);
}
void drawrectw(int x, int y, int width, int height, uint32_t color)
{
RECT r = {x, y, x + width, y + height};
SetDCBrushColor(hdc, color);
FillRect(hdc, &r, hdc_brush);
}
void drawhline(int x, int y, int x2, uint32_t color)
{
RECT r = {x, y, x2, y + 1};
SetDCBrushColor(hdc, color);
FillRect(hdc, &r, hdc_brush);
}
void drawvline(int x, int y, int y2, uint32_t color)
{
RECT r = {x, y, x + 1, y2};
SetDCBrushColor(hdc, color);
FillRect(hdc, &r, hdc_brush);
}
void setfont(int id)
{
SelectObject(hdc, font[id]);
}
uint32_t setcolor(uint32_t color)
{
return SetTextColor(hdc, color);
}
RECT clip[16];
static int clipk;
void pushclip(int left, int top, int width, int height)
{
int right = left + width, bottom = top + height;
RECT *r = &clip[clipk++];
r->left = left;
r->top = top;
r->right = right;
r->bottom = bottom;
HRGN rgn = CreateRectRgn(left, top, right, bottom);
SelectClipRgn (hdc, rgn);
DeleteObject(rgn);
}
void popclip(void)
{
clipk--;
if(!clipk) {
SelectClipRgn(hdc, NULL);
return;
}
RECT *r = &clip[clipk - 1];
HRGN rgn = CreateRectRgn(r->left, r->top, r->right, r->bottom);
SelectClipRgn (hdc, rgn);
DeleteObject(rgn);
}
void enddraw(int x, int y, int width, int height)
{
SelectObject(hdc, hdc_bm);
BitBlt(main_hdc, x, y, width, height, hdc, x, y, SRCCOPY);
}
void thread(void func(void*), void *args)
{
_beginthread(func, 0, args);
}
void yieldcpu(uint32_t ms)
{
Sleep(ms);
}
uint64_t get_time(void)
{
return ((uint64_t)clock() * 1000 * 1000);
}
void openurl(char_t *str)
{
//!convert
ShellExecute(NULL, "open", (char*)str, NULL, NULL, SW_SHOW);
}
/** Open system file browser dialog */
void openfilesend(void)
{
char *filepath = malloc(1024);
filepath[0] = 0;
wchar_t dir[1024];
GetCurrentDirectoryW(countof(dir), dir);
OPENFILENAME ofn = {
.lStructSize = sizeof(OPENFILENAME),
.hwndOwner = hwnd,
.lpstrFile = filepath,
.nMaxFile = 1024,
.Flags = OFN_EXPLORER | OFN_ALLOWMULTISELECT | OFN_FILEMUSTEXIST,
};
if(GetOpenFileName(&ofn)) {
tox_postmessage(TOX_SEND_NEW_FILE, (FRIEND*)selected_item->data - friend, ofn.nFileOffset, filepath);
} else {
debug("GetOpenFileName() failed\n");
}
SetCurrentDirectoryW(dir);
}
void openfileavatar(void)
{
char *filepath = malloc(1024);
filepath[0] = 0;
wchar_t dir[1024];
GetCurrentDirectoryW(countof(dir), dir);
OPENFILENAME ofn = {
.lStructSize = sizeof(OPENFILENAME),
.lpstrFilter = "PNG Files\0*.PNG\0\0",
.hwndOwner = hwnd,
.lpstrFile = filepath,
.nMaxFile = 1024,
.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST,
};
while (1) { // loop until we have a good file or the user closed the dialog
if(GetOpenFileName(&ofn)) {
uint32_t size;
void *file_data = file_raw(filepath, &size);
if (!file_data) {
MessageBox(NULL, (const char *)S(CANT_FIND_FILE_OR_EMPTY), NULL, MB_ICONWARNING);
} else if (size > UTOX_AVATAR_MAX_DATA_LENGTH) {
free(file_data);
char_t message[1024];
if (sizeof(message) < SLEN(AVATAR_TOO_LARGE_MAX_SIZE_IS) + 16) {
debug("error: AVATAR_TOO_LARGE message is larger than allocated buffer(%u bytes)\n", (unsigned int)sizeof(message));
break;
}
// create message containing text that selected avatar is too large and what the max size is
int len = sprintf((char *)message, "%.*s", SLEN(AVATAR_TOO_LARGE_MAX_SIZE_IS), S(AVATAR_TOO_LARGE_MAX_SIZE_IS));
len += sprint_bytes(message+len, sizeof(message)-len, UTOX_AVATAR_MAX_DATA_LENGTH);
message[len++] = '\0';
MessageBox(NULL, (char *)message, NULL, MB_ICONWARNING);
} else {
postmessage(SET_AVATAR, size, 0, file_data);
break;
}
} else {
debug("GetOpenFileName() failed\n");
break;
}
}
free(filepath);
SetCurrentDirectoryW(dir);
}
void savefilerecv(uint32_t fid, MSG_FILE *file)
{
char *path = malloc(256);
memcpy(path, file->name, file->name_length);
path[file->name_length] = 0;
OPENFILENAME ofn = {
.lStructSize = sizeof(OPENFILENAME),
.hwndOwner = hwnd,
.lpstrFile = path,
.nMaxFile = UTOX_FILE_NAME_LENGTH,
.Flags = OFN_EXPLORER | OFN_NOCHANGEDIR | OFN_NOREADONLYRETURN | OFN_OVERWRITEPROMPT,
};
if(GetSaveFileName(&ofn)) {
tox_postmessage(TOX_ACCEPTFILE, fid, file->filenumber, path);
} else {
debug("GetSaveFileName() failed\n");
}
}
void savefiledata(MSG_FILE *file)
{
char *path = malloc(UTOX_FILE_NAME_LENGTH);
memcpy(path, file->name, file->name_length);
path[file->name_length] = 0;
OPENFILENAME ofn = {
.lStructSize = sizeof(OPENFILENAME),
.hwndOwner = hwnd,
.lpstrFile = path,
.nMaxFile = UTOX_FILE_NAME_LENGTH,
.Flags = OFN_EXPLORER | OFN_NOCHANGEDIR | OFN_NOREADONLYRETURN | OFN_OVERWRITEPROMPT,
};
if(GetSaveFileName(&ofn)) {
FILE *fp = fopen(path, "wb");
if(fp) {
fwrite(file->path, file->size, 1, fp);
fclose(fp);
free(file->path);
file->path = (uint8_t*)strdup(path);
file->inline_png = 0;
}
} else {
debug("GetSaveFileName() failed\n");
}
}
void setselection(char_t *data, STRING_IDX length)
{
}
/** Toggles the main window to/from hidden to tray/shown. */
void togglehide(int show){
if ( hidden || show ) {
ShowWindow(hwnd, SW_RESTORE);
SetForegroundWindow(hwnd);
redraw();
hidden = 0;
} else {
ShowWindow(hwnd, SW_HIDE);
hidden = 1;
}
}
/** Right click context menu for the tray icon */
void ShowContextMenu(void)
{
POINT pt;
GetCursorPos(&pt);
HMENU hMenu = CreatePopupMenu();
if(hMenu) {
InsertMenu(hMenu, -1, MF_BYPOSITION, TRAY_SHOWHIDE, hidden ? "Restore" : "Hide");
InsertMenu(hMenu, -1, MF_BYPOSITION | MF_SEPARATOR, 0, NULL);
InsertMenu(hMenu, -1, MF_BYPOSITION | ((self.status == TOX_USER_STATUS_NONE) ? MF_CHECKED : 0), TRAY_STATUS_AVAILABLE, "Available");
InsertMenu(hMenu, -1, MF_BYPOSITION | ((self.status == TOX_USER_STATUS_AWAY) ? MF_CHECKED : 0), TRAY_STATUS_AWAY, "Away");
InsertMenu(hMenu, -1, MF_BYPOSITION | ((self.status == TOX_USER_STATUS_BUSY) ? MF_CHECKED : 0), TRAY_STATUS_BUSY, "Busy");
InsertMenu(hMenu, -1, MF_BYPOSITION | MF_SEPARATOR, 0, NULL);
InsertMenu(hMenu, -1, MF_BYPOSITION, TRAY_EXIT, "Exit");
// note: must set window to the foreground or the
// menu won't disappear when it should
SetForegroundWindow(hwnd);
TrackPopupMenu(hMenu, TPM_BOTTOMALIGN, pt.x, pt.y, 0, hwnd, NULL);
DestroyMenu(hMenu);
}
}
static void parsecmd(uint8_t *cmd, int len)
{
debug("Command: %.*s\n", len, cmd);
//! lacks max length checks, writes to inputs even on failure, no notice of failure
//doesnt reset unset inputs
if(len > 6 && memcmp(cmd, "tox://", 6) == 0) {
cmd += 6;
len -= 6;
} else if (len > 4 && memcmp(cmd, "tox:", 4) == 0) {
cmd += 4;
len -= 4;
} else {
return;
}
uint8_t *b = edit_add_id.data, *a = cmd, *end = cmd + len;
uint16_t *l = &edit_add_id.length;
*l = 0;
while(a != end)
{
switch(*a)
{
case 'a' ... 'z':
case 'A' ... 'Z':
case '0' ... '9':
case '@':
case '.':
case ' ':
{
*b++ = *a;
*l = *l + 1;
break;
}
case '+':
{
*b++ = ' ';
*l = *l + 1;
break;
}
case '?':
case '&':
{
a++; /* Anyone know what pin is used for? */
if(end - a >= 4 && memcmp(a, "pin=", 4) == 0)
{
l = &edit_add_id.length;
b = edit_add_id.data + *l;
*b++ = ':';
*l = *l + 1;
a += 3;
break;
}
else if(end - a >= 8 && memcmp(a, "message=", 8) == 0)
{
b = edit_add_msg.data;
l = &edit_add_msg.length;
*l = 0;
a += 7;
break;
}
return;
}
case '/':
{
break;
}
default:
{
return;
}
}
a++;
}
list_selectaddfriend();
}
void loadalpha(int bm, void *data, int width, int height)
{
bitmap[bm] = data;
}
// creates an UTOX_NATIVE image based on given arguments
// image should be freed with image_free
static UTOX_NATIVE_IMAGE *create_utox_image(HBITMAP bmp, _Bool has_alpha, uint32_t width, uint32_t height)
{
UTOX_NATIVE_IMAGE *image = malloc(sizeof(UTOX_NATIVE_IMAGE));
image->bitmap = bmp;
image->has_alpha = has_alpha;
image->width = width;
image->height = height;
image->scaled_width = width;
image->scaled_height = height;
image->stretch_mode = COLORONCOLOR;
return image;
}
static void sendbitmap(HDC mem, HBITMAP hbm, int width, int height)
{
if (width == 0 || height == 0)
return;
BITMAPINFO info = {
.bmiHeader = {
.biSize = sizeof(BITMAPINFOHEADER),
.biWidth = width,
.biHeight = -(int)height,
.biPlanes = 1,
.biBitCount = 24,
.biCompression = BI_RGB,
}
};
void *bits = malloc((width + 3) * height * 3);
GetDIBits(mem, hbm, 0, height, bits, &info, DIB_RGB_COLORS);
uint8_t pbytes = width & 3, *p = bits, *pp = bits, *end = p + width * height * 3;
//uint32_t offset = 0;
while(p != end) {
int i;
for(i = 0; i != width; i++) {
uint8_t b = pp[i * 3];
p[i * 3] = pp[i * 3 + 2];
p[i * 3 + 1] = pp[i * 3 + 1];
p[i * 3 + 2] = b;
}
p += width * 3;
pp += width * 3 + pbytes;
}
uint8_t *out;
size_t size;
lodepng_encode_memory(&out, &size, bits, width, height, LCT_RGB, 8);
free(bits);
UTOX_NATIVE_IMAGE *image = create_utox_image(hbm, 0, width, height);
friend_sendimage(selected_item->data, image, width, height, (UTOX_PNG_IMAGE)out, size);
}
void copy(int value)
{
char_t data[32768];//!TODO: De-hardcode this value.
int len;
if(edit_active()) {
len = edit_copy(data, 32767);
data[len] = 0;
} else if(selected_item->item == ITEM_FRIEND) {
len = messages_selection(&messages_friend, data, 32768, value);
} else if(selected_item->item == ITEM_GROUP) {
len = messages_selection(&messages_group, data, 32768, value);
} else {
return;
}
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, (len + 1) * 2);
wchar_t *d = GlobalLock(hMem);
utf8tonative(data, d, len + 1); //because data is nullterminated
GlobalUnlock(hMem);
OpenClipboard(hwnd);
EmptyClipboard();
SetClipboardData(CF_UNICODETEXT, hMem);
CloseClipboard();
}
void paste(void)
{
OpenClipboard(NULL);
HANDLE h = GetClipboardData(CF_UNICODETEXT);
if(!h) {
h = GetClipboardData(CF_BITMAP);
if(h && selected_item->item == ITEM_FRIEND) {
HBITMAP copy;
BITMAP bm;
HDC tempdc;
GetObject(h, sizeof(bm), &bm);
tempdc = CreateCompatibleDC(NULL);
SelectObject(tempdc, h);
copy = CreateCompatibleBitmap(hdcMem, bm.bmWidth, bm.bmHeight);
SelectObject(hdcMem, copy);
BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, tempdc, 0, 0, SRCCOPY);
sendbitmap(hdcMem, copy, bm.bmWidth, bm.bmHeight);
DeleteDC(tempdc);
}
} else {
wchar_t *d = GlobalLock(h);
char_t data[65536]; //TODO: De-hardcode this value.
int len = WideCharToMultiByte(CP_UTF8, 0, d, -1, (char*)data, sizeof(data), NULL, 0);
if(edit_active()) {
edit_paste(data, len, 0);
}
}
GlobalUnlock(h);
CloseClipboard();
}
UTOX_NATIVE_IMAGE *png_to_image(const UTOX_PNG_IMAGE data, size_t size, uint16_t *w, uint16_t *h, _Bool keep_alpha)
{
uint8_t *rgba_data;
unsigned width, height;
unsigned r = lodepng_decode32(&rgba_data, &width, &height, data->png_data, size);
if(r != 0 || !width || !height) {
return NULL; // invalid image
}
BITMAPINFO bmi = {
.bmiHeader = {
.biSize = sizeof(BITMAPINFOHEADER),
.biWidth = width,
.biHeight = -height,
.biPlanes = 1,
.biBitCount = 32,
.biCompression = BI_RGB,
}
};
// create device independent bitmap, we can write the bytes to out
// to put them in the bitmap
uint8_t *out;
HBITMAP bmp = CreateDIBSection(hdcMem, &bmi, DIB_RGB_COLORS, (void**)&out, NULL, 0);
// convert RGBA data to internal format
// pre-applying the alpha if we're keeping the alpha channel,
// put the result in out
// NOTE: input pixels are in format RGBA, output is BGRA
uint8_t *p, *end = rgba_data + width * height * 4;
p = rgba_data;
if (keep_alpha) {
uint8_t alpha;
do {
alpha = p[3];
out[0] = p[2] * (alpha / 255.0); // pre-apply alpha
out[1] = p[1] * (alpha / 255.0);
out[2] = p[0] * (alpha / 255.0);
out[3] = alpha;
out += 4;
p += 4;
} while(p != end);
} else {
do {
out[0] = p[2];
out[1] = p[1];
out[2] = p[0];
out[3] = 0;
out += 4;
p += 4;
} while (p != end);
}
free(rgba_data);
UTOX_NATIVE_IMAGE *image = create_utox_image(bmp, keep_alpha, width, height);
*w = width;
*h = height;
return image;
}
void image_free(UTOX_NATIVE_IMAGE *image)
{
DeleteObject(image->bitmap);
free(image);
}
int datapath_old(uint8_t *dest)
{
if (utox_portable) {
return 0;
} else {
if(SUCCEEDED(SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, (char*)dest))) {
uint8_t *p = dest + strlen((char*)dest);
strcpy((char *)p, "\\Tox"); p += 4;
*p++ = '\\';
return p - dest;
}
}
return 0;
}
int datapath(uint8_t *dest)
{
if (utox_portable) {
uint8_t *p = dest;
strcpy((char *)p, utox_portable_save_path); p += strlen(utox_portable_save_path);
strcpy((char *)p, "\\Tox"); p += 4;
CreateDirectory((char*)dest, NULL);
*p++ = '\\';
return p - dest;
} else {
if(SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, (char*)dest))) {
uint8_t *p = dest + strlen((char*)dest);
strcpy((char *)p, "\\Tox"); p += 4;
CreateDirectory((char*)dest, NULL);
*p++ = '\\';
return p - dest;
}
}
return 0;
}
int datapath_subdir(uint8_t *dest, const char *subdir)
{
int l = datapath(dest);
l += sprintf((char*)(dest+l), "%s", subdir);
CreateDirectory((char*)dest, NULL);
dest[l++] = '\\';
return l;
}
void flush_file(FILE *file)
{
fflush(file);
int fd = _fileno(file);
_commit(fd);
}
int ch_mod(uint8_t *file){
/* You're probably looking for ./xlib as windows is lamesauce and wants nothing to do with sane permissions */
return 1;
}
int file_lock(FILE *file, uint64_t start, size_t length){
OVERLAPPED lock_overlap;
lock_overlap.Offset = start;
lock_overlap.OffsetHigh = start+length;
lock_overlap.hEvent = 0;
return !LockFileEx(file, LOCKFILE_FAIL_IMMEDIATELY, 0, start, start + length, &lock_overlap);
}
int file_unlock(FILE *file, uint64_t start, size_t length){
OVERLAPPED lock_overlap;
lock_overlap.Offset = start;
lock_overlap.OffsetHigh = start+length;
lock_overlap.hEvent = 0;
return UnlockFileEx(file, 0, start, start + length, &lock_overlap);
}
/** Creates a tray baloon popup with the message, and flashes the main window
*
* accepts: char_t *title, title length, char_t *msg, msg length;
* returns void;
*/
void notify(char_t *title, STRING_IDX title_length, char_t *msg, STRING_IDX msg_length, FRIEND *f){
if(havefocus) {
return;
}
FlashWindow(hwnd, 1);
flashing = 1;
NOTIFYICONDATAW nid = {
.uFlags = NIF_ICON | NIF_INFO,
.hWnd = hwnd,
.hIcon = unread_messages_icon,
.uTimeout = 5000,
.dwInfoFlags = 0,
.cbSize = sizeof(nid),
};
utf8tonative(title, nid.szInfoTitle, title_length > sizeof(nid.szInfoTitle) / sizeof(*nid.szInfoTitle) - 1 ? sizeof(nid.szInfoTitle) / sizeof(*nid.szInfoTitle) - 1 : title_length);
utf8tonative(msg, nid.szInfo, msg_length > sizeof(nid.szInfo) / sizeof(*nid.szInfo) - 1 ? sizeof(nid.szInfo) / sizeof(*nid.szInfo) - 1 : msg_length);
Shell_NotifyIconW(NIM_MODIFY, &nid);
}
void showkeyboard(_Bool show){} /* Added for android support. */
void edit_will_deactivate(void)
{
}
/* Redraws the main UI window */
void redraw(void)
{
panel_draw(&panel_root, 0, 0, utox_window_width, utox_window_height);
}
/**
* update_tray(void)
* creates a win32 NOTIFYICONDATAW struct, sets the tiptab flag, gives *hwnd,
* sets struct .cbSize, and resets the tibtab to native self.name;
*/
void update_tray(void){
uint32_t tip_length;
char *tip;
tip = malloc(128 * sizeof(char)); //128 is the max length of nid.szTip
snprintf(tip, 127*sizeof(char), "%s : %s", self.name, self.statusmsg);
tip_length = self.name_length + 3 + self.statusmsg_length;
NOTIFYICONDATAW nid = {
.uFlags = NIF_TIP,
.hWnd = hwnd,
.cbSize = sizeof(nid),
};
utf8str_to_native((char_t *)tip, nid.szTip, tip_length);
Shell_NotifyIconW(NIM_MODIFY, &nid);
free(tip);
}
void force_redraw(void) {
redraw();
}
static int grabx, graby, grabpx, grabpy;
static _Bool grabbing;
void desktopgrab(_Bool video)
{
int x, y, w, h;
x = GetSystemMetrics(SM_XVIRTUALSCREEN);
y = GetSystemMetrics(SM_YVIRTUALSCREEN);
w = GetSystemMetrics(SM_CXVIRTUALSCREEN);
h = GetSystemMetrics(SM_CYVIRTUALSCREEN);
debug("result: %i %i %i %i\n", x, y, w, h);
capturewnd = CreateWindowExW(WS_EX_TOOLWINDOW | WS_EX_LAYERED, L"uToxgrab", L"Tox", WS_POPUP, x, y, w, h, NULL, NULL, hinstance, NULL);
if(!capturewnd) {
debug("CreateWindowExW() failed\n");
return;
}
SetLayeredWindowAttributes(capturewnd, 0xFFFFFF, 128, LWA_ALPHA | LWA_COLORKEY);
//UpdateLayeredWindow(hwnd, NULL, NULL, NULL, NULL, NULL, 0xFFFFFF, ULW_ALPHA | ULW_COLORKEY);
ShowWindow(capturewnd, SW_SHOW);
SetForegroundWindow(capturewnd);
desktopgrab_video = video;
//SetCapture(hwnd);
//grabbing = 1;