-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIMS.cpp
More file actions
1036 lines (972 loc) · 36.4 KB
/
IMS.cpp
File metadata and controls
1036 lines (972 loc) · 36.4 KB
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
// g++ IMS.cpp -o IMS.cpp.o -lSDL2 -lSDL2_image -lSDL2_ttf `pkg-config --cflags --libs libudev libinput` && ./IMS.cpp.o
#include <linux/input-event-codes.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#include <filesystem> // to check if the input file exist
#include <libinput.h> // to get input even when out of focus
#include <libudev.h> // needed with libinput
#include <poll.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#define WARN_COL "\e[0;33m" // warning color
#define ERR_COL "\e[0;31m" // error color
#define NOR_COL "\e[0m" // normal color
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
int BUTTON_WIDTH = 100;
int BUTTON_HIEGHT = 90;
int MOUSE_WIDTH = 90;
int MOUSE_HIEGHT = 90;
bool SHOW_CTRL = true;
bool SHOW_SHIFT = true;
bool SHOW_SUPER = true;
bool SHOW_ALT = true;
bool SHOW_LETTERS = true;
bool SHOW_BORDERS = true;
bool SHOW_MOUSE = true;
bool TRANSPARENT_MODE = false;
bool CONFIG_GUI = true;
bool POS_RIGHT = false; // when used the "-p right <something>"
bool POS_BUTTOM = false; // when used the "-p <something> buttom"
bool NEED_REFRESH = false; // to know if a refresh is needed
// factors to multibly the width of the button to increase it
float FAC_BACKSPACE = 1.4;
float FAC_ENTER = 1.5;
float FAC_SPACE = 2;
float FAC_TAB = 1.2;
// a boolean for each button if it is pressed and the mouse
bool PRESSED_BUTTONS[5] = {false, false, false, false, false};
bool PRESSED_MOUSE[4] = {false, false, false, false}; // the scroll up/down are a single boolean
int window_hieght, window_width; // window sizes
short int nButtons = 0; // this shows the number of buttons including the general button
using namespace std;
string home_dir = getenv("HOME"); // get the home directory in linux
string current_path;
TTF_Font* font;
SDL_Color font_color;
// the offsets for the characters inside the buttons
int horizontal_offset = 15;
int vertical_offset = 15;
// blank surface to hide the defects
SDL_Rect rect_blank;
SDL_Surface* sur_blank = NULL;
SDL_Texture* tex_blank = NULL;
// the backgraound color
#define BG_COLOR_R 110
#define BG_COLOR_G 188
#define BG_COLOR_B 197
#define BG_COLOR_A 255
// the x,y positons and width and hieght on the window
SDL_Rect rect_ctrl;
SDL_Rect rect_shift;
SDL_Rect rect_super;
SDL_Rect rect_alt;
SDL_Rect rect_letters;
SDL_Rect rect_mouse;
// spetial rectangles for the letters that have factors
SDL_Rect rect_backspace;
SDL_Rect rect_enter;
SDL_Rect rect_space;
SDL_Rect rect_tab;
// for the current letter being pressed
SDL_Rect * rect_current_letters;
// the offseted rectangles (inside the original) (used for the text)
SDL_Rect rect_offset_ctrl;
SDL_Rect rect_offset_shift;
SDL_Rect rect_offset_super;
SDL_Rect rect_offset_alt;
SDL_Rect rect_offset_letters;
SDL_Rect rect_offset_mouse;
// spetial rectangles for the letters that have factors
SDL_Rect rect_offset_backspace;
SDL_Rect rect_offset_enter;
SDL_Rect rect_offset_space;
SDL_Rect rect_offset_tab;
// for the current letter being pressed
SDL_Rect * rect_current_offset_letters;
SDL_Texture * tex_checked = NULL;
SDL_Texture * tex_notchecked = NULL;
SDL_Texture * tex_btn_normal = NULL;
SDL_Texture * tex_btn_hoover = NULL;
SDL_Texture * tex_btn_holded = NULL;
// initializing outside the if statments to be able to use them
SDL_Texture* tex_ctrl = NULL;
SDL_Texture* tex_shift = NULL;
SDL_Texture* tex_super = NULL;
SDL_Texture* tex_alt = NULL;
SDL_Texture* tex_general = NULL;
SDL_Texture* tex_generalP = NULL;
SDL_Texture* tex_mouse = NULL;
SDL_Texture* tex_mouse_rightP = NULL;
SDL_Texture* tex_mouse_leftP = NULL;
SDL_Texture* tex_mouse_wheelP = NULL;
SDL_Texture* tex_mouse_wheelup = NULL;
SDL_Texture* tex_mouse_wheeldown = NULL;
// for the current letter pressed so that we render it when the size of the window has changed
SDL_Texture* tex_current_letter = NULL;
// the boring part of initializing all the letters
SDL_Texture* tex_A = NULL;
SDL_Texture* tex_B = NULL;
SDL_Texture* tex_C = NULL;
SDL_Texture* tex_D = NULL;
SDL_Texture* tex_E = NULL;
SDL_Texture* tex_F = NULL;
SDL_Texture* tex_G = NULL;
SDL_Texture* tex_H = NULL;
SDL_Texture* tex_I = NULL;
SDL_Texture* tex_J = NULL;
SDL_Texture* tex_K = NULL;
SDL_Texture* tex_L = NULL;
SDL_Texture* tex_M = NULL;
SDL_Texture* tex_N = NULL;
SDL_Texture* tex_O = NULL;
SDL_Texture* tex_P = NULL;
SDL_Texture* tex_Q = NULL;
SDL_Texture* tex_R = NULL;
SDL_Texture* tex_S = NULL;
SDL_Texture* tex_T = NULL;
SDL_Texture* tex_U = NULL;
SDL_Texture* tex_V = NULL;
SDL_Texture* tex_W = NULL;
SDL_Texture* tex_X = NULL;
SDL_Texture* tex_Y = NULL;
SDL_Texture* tex_Z = NULL;
SDL_Texture* tex_0 = NULL;
SDL_Texture* tex_1 = NULL;
SDL_Texture* tex_2 = NULL;
SDL_Texture* tex_3 = NULL;
SDL_Texture* tex_4 = NULL;
SDL_Texture* tex_5 = NULL;
SDL_Texture* tex_6 = NULL;
SDL_Texture* tex_7 = NULL;
SDL_Texture* tex_8 = NULL;
SDL_Texture* tex_9 = NULL;
SDL_Texture* tex_APOSTROPHE=NULL;
SDL_Texture* tex_BACKSLASH =NULL;
SDL_Texture* tex_BACKSPACE =NULL;
SDL_Texture* tex_COMMA = NULL;
SDL_Texture* tex_DELETE = NULL;
SDL_Texture* tex_DOT = NULL;
SDL_Texture* tex_ENTER = NULL;
SDL_Texture* tex_EQUAL = NULL;
SDL_Texture* tex_ESC = NULL;
SDL_Texture* tex_GRAVE = NULL;
SDL_Texture* tex_LEFTBRACE =NULL;
SDL_Texture* tex_MINUS = NULL;
SDL_Texture* tex_RIGHTBRACE=NULL;
SDL_Texture* tex_SEMICOLON =NULL;
SDL_Texture* tex_SLASH = NULL;
SDL_Texture* tex_SPACE = NULL;
SDL_Texture* tex_TAB = NULL;
bool RenderCustom(SDL_Texture * textureT, const SDL_Rect * dstRectT)
{
bool unique_tex = false;
// to detect the letters textures and Rect's
if (textureT != tex_blank &&
textureT != tex_ctrl &&
textureT != tex_shift &&
textureT != tex_super &&
textureT != tex_alt &&
textureT != tex_general &&
textureT != tex_generalP &&
textureT != tex_mouse &&
textureT != tex_mouse_rightP &&
textureT != tex_mouse_leftP &&
textureT != tex_mouse_wheelP &&
textureT != tex_mouse_wheelup &&
textureT != tex_mouse_wheeldown)
{
tex_current_letter = textureT;
unique_tex = true;
}
SDL_RenderCopy(renderer, textureT, NULL, dstRectT);
NEED_REFRESH = true;
return unique_tex;
}
// to render the button and then the letter
void RenderLetters(SDL_Texture * textureT, SDL_Rect * dstRectT, SDL_Texture * textureT2, SDL_Rect * dstRectT2)
{
if(RenderCustom(textureT, dstRectT) || RenderCustom(textureT2, dstRectT2))
{
rect_current_letters = dstRectT;
rect_current_offset_letters = dstRectT2;
}
NEED_REFRESH = true;
}
int ntrues_keyboard()
{
int count = 0;
if (PRESSED_BUTTONS[0] && SHOW_CTRL) count++;
if (PRESSED_BUTTONS[1] && SHOW_SHIFT) count++;
if (PRESSED_BUTTONS[2] && SHOW_SUPER) count++;
if (PRESSED_BUTTONS[3] && SHOW_ALT) count++;
if (PRESSED_BUTTONS[4] && SHOW_LETTERS) count++;
return count;
}
int ntrues_mouse()
{
int count = 0;
for (int i=0;i<4;i++)
if (PRESSED_MOUSE[i]) count++;
return count;
}
int check_mouse_clicked()
{
if ((PRESSED_MOUSE[0] || PRESSED_MOUSE[1] || PRESSED_MOUSE[2] || PRESSED_MOUSE[3]) && SHOW_MOUSE) return 1;
else return 0;
}
void check_show_window()
{
if (TRANSPARENT_MODE)
{
if (ntrues_keyboard() == 0 && check_mouse_clicked() == 0) SDL_HideWindow(window);
else SDL_ShowWindow(window);
}
}
void update_window_hieght()
{
if (check_mouse_clicked() == 1) window_hieght = MOUSE_HIEGHT > BUTTON_HIEGHT? MOUSE_HIEGHT : BUTTON_HIEGHT;
else window_hieght = BUTTON_HIEGHT;
}
// update the window width and the mouse x position using the factor
void update_window_width(float factor)
{
// update window width
if (TRANSPARENT_MODE)
{
window_width = ntrues_keyboard() * BUTTON_WIDTH;
if (SHOW_MOUSE) window_width += check_mouse_clicked() * MOUSE_WIDTH;
if (PRESSED_BUTTONS[4]) window_width = window_width - BUTTON_WIDTH + int(factor*BUTTON_WIDTH);
rect_tab.x = rect_space.x = rect_enter.x = rect_backspace.x = window_width - int(factor*BUTTON_WIDTH);
rect_offset_letters.x = rect_tab.x + horizontal_offset;
}
else
{
window_width = (nButtons-1) * BUTTON_WIDTH + int(BUTTON_WIDTH*factor);
if (SHOW_MOUSE) window_width += MOUSE_WIDTH;
}
SDL_SetWindowSize(window, window_width, window_hieght);
// changing the mouse position
if (SHOW_MOUSE)
{
rect_mouse.x = window_width - MOUSE_WIDTH;
if ( (TRANSPARENT_MODE && check_mouse_clicked()) || (!TRANSPARENT_MODE) )
RenderCustom(tex_mouse, &rect_mouse);
if (PRESSED_MOUSE[0]) RenderCustom(tex_mouse_leftP, &rect_mouse);
if (PRESSED_MOUSE[1]) RenderCustom(tex_mouse_rightP, &rect_mouse);
if (PRESSED_MOUSE[2]) RenderCustom(tex_mouse_wheelP, &rect_mouse);
}
// updating the blank rectangle to use it
rect_blank = {rect_letters.x, 0, int(BUTTON_WIDTH*factor), window_hieght};
}
void imageToTexture(string image_path_temp, SDL_Texture* &tex_temp)
{
if (!filesystem::is_regular_file(image_path_temp))
{
cerr <<ERR_COL "file\"" << image_path_temp << "\" does not exist" NOR_COL<< endl;
exit(1);
}
// making the surfaces from the images
SDL_Surface* sur_temp = IMG_Load(image_path_temp.c_str());
// making the texture from the surfaces
tex_temp = SDL_CreateTextureFromSurface(renderer, sur_temp);
// we do not need the surfaces from now so we freed them
SDL_FreeSurface(sur_temp);
}
void wordToTexture(const char* word, SDL_Texture* &tex_temp)
{
// Render Latin1 text at fast quality to a new 8-bit surface.
//SDL_Surface* sur_text = TTF_RenderText_Solid(font, "Text", font_color);
// Render Latin1 text at high quality to a new ARGB surface.
SDL_Surface* sur_text_temp = TTF_RenderText_Blended(font, word, font_color);
tex_temp = SDL_CreateTextureFromSurface(renderer, sur_text_temp);
SDL_FreeSurface(sur_text_temp);
}
void print_help()
{
// use the current_path to make run even from a differant directory
if (filesystem::is_regular_file(current_path+"IMS.1"))
system(("man -c "+current_path+"IMS.1").c_str());
else
system("man IMS");
exit(0);
}
// to find if a file (image or font) is in ~/config/IMS/ or /usr/shar/IMS/ or ./resources/
string find_file(string file_name)
{
if (filesystem::is_regular_file(home_dir+"/.config/IMS/"+file_name))
return home_dir+"/.config/IMS/"+file_name;
else if (filesystem::is_regular_file(current_path+"resources/"+file_name))
return current_path+"resources/"+file_name;
else
return "/usr/share/IMS/"+file_name;
}
int open_callback(const char *path, int flags, void *user_data)
{
int fd = open(path, flags);
if (fd == -1)
{
cerr << ERR_COL;
switch (errno)
{
case EACCES: cerr << "Error accessing the input files: make sure you are in the input group by running \"usermod -a -G input username\" and then restarting the computer"; break;
case ENOMEM: cerr << "Error no enough memory for accessing the file"; break;
defualt: cerr << "Error opening the input file";
}
cerr << NOR_COL << endl;
exit(1);
}
return fd;
}
void close_callback(int fd, void *user_data)
{
close(fd);
}
SDL_Event sdl_input; // the input from SDL2 (the input when the application is in focus)
bool close_program = false; // if set to true the loop exit (to close the app)
int main(int argc, char* argv[]) {
SDL_SetHint(SDL_HINT_VIDEODRIVER, "x11");
if ( SDL_Init( SDL_INIT_EVENTS ) < 0 ) {
cerr <<ERR_COL "Error initializing SDL: " << SDL_GetError() << NOR_COL<< endl;
return 1;
}
current_path = argv[0];
// remove last seven chars(IMS) by subtracting totall length - 3
while (current_path.back() != '/')
current_path.pop_back();
//current_path.erase(current_path.length() - 3);
TTF_Init();
font = TTF_OpenFont(find_file("font.ttf").c_str(), 52);
font_color = {255, 255, 255};
// variables for handing arguments
int i;
string arg_next;
string arg_next2;
int X=0,Y=0; // position variables
#include "arg.h"
if (CONFIG_GUI)
{
#include "config_gui.h"
}
if (close_program)
{
#include "quit.h" // clear things and exit
}
string session_type = getenv("XDG_SESSION_TYPE"); // get the home directory in linux
if (session_type == "wayland")
{
cout << WARN_COL "unfortunatly most wayland compositors does not support \"Always on Top\" so you have to right click on the title bar of the window and then click \"Always on Top\"" NOR_COL<< endl;
}
if (SHOW_CTRL)
{
rect_ctrl = {nButtons*BUTTON_WIDTH, 0, BUTTON_WIDTH, BUTTON_HIEGHT};
rect_offset_ctrl = {rect_ctrl.x+horizontal_offset, rect_ctrl.y+vertical_offset, rect_ctrl.w-2*horizontal_offset, rect_ctrl.h-2*vertical_offset};
nButtons++;
}
if (SHOW_SHIFT)
{
rect_shift = {nButtons*BUTTON_WIDTH, 0, BUTTON_WIDTH, BUTTON_HIEGHT};
rect_offset_shift = {rect_shift.x+horizontal_offset, rect_shift.y+vertical_offset, rect_shift.w-2*horizontal_offset, rect_shift.h-2*vertical_offset};
nButtons++;
}
if (SHOW_SUPER)
{
rect_super = {nButtons*BUTTON_WIDTH, 0, BUTTON_WIDTH, BUTTON_HIEGHT};
rect_offset_super = {rect_super.x+horizontal_offset, rect_super.y+vertical_offset, rect_super.w-2*horizontal_offset, rect_super.h-2*vertical_offset};
nButtons++;
}
if (SHOW_ALT)
{
rect_alt = {nButtons*BUTTON_WIDTH, 0, BUTTON_WIDTH, BUTTON_HIEGHT};
rect_offset_alt = {rect_alt.x+horizontal_offset, rect_alt.y+vertical_offset, rect_alt.w-2*horizontal_offset, rect_alt.h-2*vertical_offset};
nButtons++;
}
if (SHOW_LETTERS)
{
rect_letters = {nButtons*BUTTON_WIDTH, 0, BUTTON_WIDTH, BUTTON_HIEGHT};
rect_backspace = {nButtons*BUTTON_WIDTH, 0, int(BUTTON_WIDTH*FAC_BACKSPACE), BUTTON_HIEGHT};
rect_enter = {nButtons*BUTTON_WIDTH, 0, int(BUTTON_WIDTH*FAC_ENTER), BUTTON_HIEGHT};
rect_space = {nButtons*BUTTON_WIDTH, 0, int(BUTTON_WIDTH*FAC_SPACE), BUTTON_HIEGHT};
rect_tab = {nButtons*BUTTON_WIDTH, 0, int(BUTTON_WIDTH*FAC_TAB), BUTTON_HIEGHT};
rect_offset_letters = {rect_letters.x+horizontal_offset, rect_letters.y+vertical_offset, rect_letters.w-2*horizontal_offset, rect_letters.h-2*vertical_offset};
rect_offset_backspace = {rect_backspace.x+horizontal_offset, rect_backspace.y+vertical_offset, rect_backspace.w-2*horizontal_offset, rect_backspace.h-2*vertical_offset};
rect_offset_enter = {rect_enter.x+horizontal_offset, rect_enter.y+vertical_offset, rect_enter.w-2*horizontal_offset, rect_enter.h-2*vertical_offset};
rect_offset_space = {rect_space.x+horizontal_offset, rect_space.y+vertical_offset, rect_space.w-2*horizontal_offset, rect_space.h-2*vertical_offset};
rect_offset_tab = {rect_tab.x+horizontal_offset, rect_tab.y+vertical_offset, rect_tab.w-2*horizontal_offset, rect_tab.h-2*vertical_offset};
nButtons++;
}
if (SHOW_MOUSE)
{
rect_mouse = {nButtons*BUTTON_WIDTH, 0, MOUSE_WIDTH, MOUSE_HIEGHT};
window_hieght = BUTTON_HIEGHT > MOUSE_HIEGHT ? BUTTON_HIEGHT : MOUSE_HIEGHT;
window_width = BUTTON_WIDTH*nButtons + MOUSE_WIDTH;
}
else
{
window_hieght = BUTTON_HIEGHT;
window_width = BUTTON_WIDTH*nButtons;
}
if (POS_RIGHT)
X = SCREEN_W-window_width;
if (POS_BUTTOM)
Y = SCREEN_H-window_hieght;
if (!TRANSPARENT_MODE)
{
if (SHOW_BORDERS)
window = SDL_CreateWindow("IMS", X, Y, window_width, window_hieght, SDL_WINDOW_SHOWN | SDL_WINDOW_UTILITY | SDL_WINDOW_ALWAYS_ON_TOP);
else
window = SDL_CreateWindow("IMS", X, Y, window_width, window_hieght, SDL_WINDOW_SHOWN | SDL_WINDOW_BORDERLESS | SDL_WINDOW_UTILITY | SDL_WINDOW_ALWAYS_ON_TOP);
}
else // transparent mode on then make the window as TOOLTIP to make it not focusable
{
window_width = ntrues_keyboard() * BUTTON_WIDTH + check_mouse_clicked() * MOUSE_WIDTH;
update_window_hieght();
window = SDL_CreateWindow("IMS", X, Y, window_width, window_hieght, SDL_WINDOW_TOOLTIP | SDL_WINDOW_ALWAYS_ON_TOP);
}
renderer = SDL_CreateRenderer(window, 3, 0);
// initializing the blank texture
sur_blank = SDL_GetWindowSurface(window);
SDL_FillRect(sur_blank, NULL, SDL_MapRGB(sur_blank->format, BG_COLOR_R, BG_COLOR_G, BG_COLOR_B));
tex_blank = SDL_CreateTextureFromSurface(renderer, sur_blank);
// display the background color
SDL_SetRenderDrawColor(renderer, BG_COLOR_R, BG_COLOR_G, BG_COLOR_B, BG_COLOR_A);
SDL_RenderClear(renderer);
if (SHOW_CTRL || SHOW_SHIFT || SHOW_SUPER || SHOW_ALT || SHOW_LETTERS)
{
imageToTexture(find_file("button.png"), tex_general);
imageToTexture(find_file("buttonP.png"), tex_generalP);
}
if (SHOW_CTRL)
{
// making the surfaces from the images
wordToTexture("ctrl", tex_ctrl);
// render the normal button at the beginning
RenderLetters(tex_general, &rect_ctrl, tex_ctrl, &rect_offset_ctrl);
}
if (SHOW_SHIFT)
{
// making the surfaces from the images
wordToTexture("shift", tex_shift);
// render the normal button at the beginning
RenderLetters(tex_general, &rect_shift, tex_shift, &rect_offset_shift);
}
if (SHOW_SUPER)
{
// making the surfaces from the images
imageToTexture(find_file("Meta.png"), tex_super);
// render the normal button at the beginning
RenderLetters(tex_general, &rect_super, tex_super, &rect_offset_super);
}
if (SHOW_ALT)
{
// making the surfaces from the images
wordToTexture("alt", tex_alt);
// render the normal button at the beginning
RenderLetters(tex_general, &rect_alt, tex_alt, &rect_offset_alt);
}
if (SHOW_LETTERS)
{
// render the normal button at the beginning
SDL_RenderCopy(renderer, tex_general, NULL, &rect_letters);
}
if (SHOW_MOUSE)
{
// making the surfaces from the images
imageToTexture(find_file("mouse.png"), tex_mouse);
imageToTexture(find_file("mouse_rightP.png"), tex_mouse_rightP);
imageToTexture(find_file("mouse_leftP.png"), tex_mouse_leftP);
imageToTexture(find_file("mouse_wheelP.png"), tex_mouse_wheelP);
imageToTexture(find_file("mouse_wheelup.png"), tex_mouse_wheelup);
imageToTexture(find_file("mouse_wheeldown.png"), tex_mouse_wheeldown);
// render the normal button at the beginning
SDL_RenderCopy(renderer, tex_mouse, NULL, &rect_mouse);
}
SDL_RenderPresent(renderer);
if (SHOW_LETTERS)
{
// the boring part of initializing all the letters
wordToTexture("A", tex_A);
wordToTexture("B", tex_B);
wordToTexture("C", tex_C);
wordToTexture("D", tex_D);
wordToTexture("E", tex_E);
wordToTexture("F", tex_F);
wordToTexture("G", tex_G);
wordToTexture("H", tex_H);
wordToTexture("I", tex_I);
wordToTexture("J", tex_J);
wordToTexture("K", tex_K);
wordToTexture("L", tex_L);
wordToTexture("M", tex_M);
wordToTexture("N", tex_N);
wordToTexture("O", tex_O);
wordToTexture("P", tex_P);
wordToTexture("Q", tex_Q);
wordToTexture("R", tex_R);
wordToTexture("S", tex_S);
wordToTexture("T", tex_T);
wordToTexture("U", tex_U);
wordToTexture("V", tex_V);
wordToTexture("W", tex_W);
wordToTexture("X", tex_X);
wordToTexture("Y", tex_Y);
wordToTexture("Z", tex_Z);
wordToTexture("0", tex_0);
wordToTexture("1", tex_1);
wordToTexture("2", tex_2);
wordToTexture("3", tex_3);
wordToTexture("4", tex_4);
wordToTexture("5", tex_5);
wordToTexture("6", tex_6);
wordToTexture("7", tex_7);
wordToTexture("8", tex_8);
wordToTexture("9", tex_9);
wordToTexture("'", tex_APOSTROPHE);
wordToTexture("\\", tex_BACKSLASH);
wordToTexture("backspace", tex_BACKSPACE);
wordToTexture(",", tex_COMMA);
wordToTexture("delete", tex_DELETE);
wordToTexture(".", tex_DOT);
wordToTexture("enter", tex_ENTER);
wordToTexture("=", tex_EQUAL);
wordToTexture("esc", tex_ESC);
wordToTexture("`", tex_GRAVE);
wordToTexture("[", tex_LEFTBRACE);
wordToTexture("-", tex_MINUS);
wordToTexture("]", tex_RIGHTBRACE);
wordToTexture(";", tex_SEMICOLON);
wordToTexture("/", tex_SLASH);
wordToTexture("space", tex_SPACE);
wordToTexture("tab", tex_TAB);
}
struct libinput_interface linterface = {
.open_restricted = open_callback,
.close_restricted = close_callback,
};
struct udev *Sudev = udev_new();
if (Sudev == NULL)
{
cerr << ERR_COL "error creating new udev" NOR_COL << endl;
#include "quit.h"
}
struct libinput *linput = libinput_udev_create_context(&linterface, NULL, Sudev);
if (linput == NULL)
{
cerr << ERR_COL "error creating libinput context" NOR_COL << endl;
#include "quit.h"
}
if (libinput_udev_assign_seat(linput, "seat0") == -1)
{
cerr << ERR_COL "error assigning the seat" NOR_COL << endl;
#include "quit.h"
}
struct pollfd pfd = {
.fd = libinput_get_fd(linput),
.events = POLLIN,
.revents = 0,
};
struct libinput_event *levent; // the main event
enum libinput_event_type ltype; // event type
struct libinput_event_keyboard *kbevent; // keyboard event
struct libinput_event_pointer *pevent; // pointer event
uint32_t kbutton; // keyboard button
uint32_t pbutton; // pointer button
enum libinput_button_state pstate;
//struct libinput_event_pointer *pevent;
double scroll_value;
while (poll(&pfd, 1, -1) > -1 && !close_program)
{
libinput_dispatch(linput); // has to be called after poll()
while((levent = libinput_get_event(linput))) {
ltype = libinput_event_get_type(levent);
if(ltype == LIBINPUT_EVENT_KEYBOARD_KEY) {
kbevent = libinput_event_get_keyboard_event(levent);
kbutton = libinput_event_keyboard_get_key(kbevent);
if(libinput_event_keyboard_get_key_state(kbevent) == LIBINPUT_KEY_STATE_PRESSED)
{
switch (kbutton)
{
case KEY_RIGHTCTRL:
case KEY_LEFTCTRL:
if (SHOW_CTRL)
{
PRESSED_BUTTONS[0] = true;
if (TRANSPARENT_MODE)
{
rect_ctrl.x = ntrues_keyboard() * BUTTON_WIDTH + check_mouse_clicked() * MOUSE_WIDTH;
rect_offset_ctrl.x = rect_ctrl.x + horizontal_offset;
window_width = ntrues_keyboard() * BUTTON_WIDTH + check_mouse_clicked() * MOUSE_WIDTH;
SDL_SetWindowSize(window, window_width, window_hieght);
}
RenderLetters(tex_generalP, &rect_ctrl, tex_ctrl, &rect_offset_ctrl);
}
break;
case KEY_RIGHTSHIFT:
case KEY_LEFTSHIFT:
if (SHOW_SHIFT)
{
PRESSED_BUTTONS[1] = true;
if (TRANSPARENT_MODE)
{
rect_shift.x = ntrues_keyboard() * BUTTON_WIDTH + check_mouse_clicked() * MOUSE_WIDTH;
rect_offset_shift.x = rect_shift.x + horizontal_offset;
window_width = ntrues_keyboard() * BUTTON_WIDTH + check_mouse_clicked() * MOUSE_WIDTH;
SDL_SetWindowSize(window, window_width, window_hieght);
}
RenderLetters(tex_generalP, &rect_shift, tex_shift, &rect_offset_shift);
}
break;
case KEY_LEFTMETA:
if (SHOW_SUPER)
{
PRESSED_BUTTONS[2] = true;
if (TRANSPARENT_MODE)
{
rect_super.x = ntrues_keyboard() * BUTTON_WIDTH + check_mouse_clicked() * MOUSE_WIDTH;
rect_offset_super.x = rect_super.x + horizontal_offset;
window_width = ntrues_keyboard() * BUTTON_WIDTH + check_mouse_clicked() * MOUSE_WIDTH;
SDL_SetWindowSize(window, window_width, window_hieght);
}
RenderLetters(tex_generalP, &rect_super, tex_super, &rect_offset_super);
}
break;
case KEY_RIGHTALT:
case KEY_LEFTALT:
if (SHOW_ALT)
{
PRESSED_BUTTONS[3] = true;
if (TRANSPARENT_MODE)
{
rect_alt.x = ntrues_keyboard() * BUTTON_WIDTH + check_mouse_clicked() * MOUSE_WIDTH;
rect_offset_alt.x = rect_alt.x + horizontal_offset;
window_width = ntrues_keyboard() * BUTTON_WIDTH + check_mouse_clicked() * MOUSE_WIDTH;
SDL_SetWindowSize(window, window_width, window_hieght);
}
RenderLetters(tex_generalP, &rect_alt, tex_alt, &rect_offset_alt);
}
break;
// the boring part of checking all the letters
default: if (SHOW_LETTERS)
{
PRESSED_BUTTONS[4] = true;
if (TRANSPARENT_MODE)
{
rect_letters.x = ntrues_keyboard() * BUTTON_WIDTH + check_mouse_clicked() * MOUSE_WIDTH;
rect_offset_letters.x = rect_letters.x + horizontal_offset;
window_width = ntrues_keyboard() * BUTTON_WIDTH + check_mouse_clicked() * MOUSE_WIDTH;
SDL_SetWindowSize(window, window_width, window_hieght);
}
RenderCustom(tex_generalP, &rect_letters);
switch (kbutton)
{
case KEY_A: RenderCustom(tex_A, &rect_offset_letters); break;
case KEY_B: RenderCustom(tex_B, &rect_offset_letters); break;
case KEY_C: RenderCustom(tex_C, &rect_offset_letters); break;
case KEY_D: RenderCustom(tex_D, &rect_offset_letters); break;
case KEY_E: RenderCustom(tex_E, &rect_offset_letters); break;
case KEY_F: RenderCustom(tex_F, &rect_offset_letters); break;
case KEY_G: RenderCustom(tex_G, &rect_offset_letters); break;
case KEY_H: RenderCustom(tex_H, &rect_offset_letters); break;
case KEY_I: RenderCustom(tex_I, &rect_offset_letters); break;
case KEY_J: RenderCustom(tex_J, &rect_offset_letters); break;
case KEY_K: RenderCustom(tex_K, &rect_offset_letters); break;
case KEY_L: RenderCustom(tex_L, &rect_offset_letters); break;
case KEY_M: RenderCustom(tex_M, &rect_offset_letters); break;
case KEY_N: RenderCustom(tex_N, &rect_offset_letters); break;
case KEY_O: RenderCustom(tex_O, &rect_offset_letters); break;
case KEY_P: RenderCustom(tex_P, &rect_offset_letters); break;
case KEY_Q: RenderCustom(tex_Q, &rect_offset_letters); break;
case KEY_R: RenderCustom(tex_R, &rect_offset_letters); break;
case KEY_S: RenderCustom(tex_S, &rect_offset_letters); break;
case KEY_T: RenderCustom(tex_T, &rect_offset_letters); break;
case KEY_U: RenderCustom(tex_U, &rect_offset_letters); break;
case KEY_V: RenderCustom(tex_V, &rect_offset_letters); break;
case KEY_W: RenderCustom(tex_W, &rect_offset_letters); break;
case KEY_X: RenderCustom(tex_X, &rect_offset_letters); break;
case KEY_Y: RenderCustom(tex_Y, &rect_offset_letters); break;
case KEY_Z: RenderCustom(tex_Z, &rect_offset_letters); break;
case KEY_0: RenderCustom(tex_0, &rect_offset_letters); break;
case KEY_1: RenderCustom(tex_1, &rect_offset_letters); break;
case KEY_2: RenderCustom(tex_2, &rect_offset_letters); break;
case KEY_3: RenderCustom(tex_3, &rect_offset_letters); break;
case KEY_4: RenderCustom(tex_4, &rect_offset_letters); break;
case KEY_5: RenderCustom(tex_5, &rect_offset_letters); break;
case KEY_6: RenderCustom(tex_6, &rect_offset_letters); break;
case KEY_7: RenderCustom(tex_7, &rect_offset_letters); break;
case KEY_8: RenderCustom(tex_8, &rect_offset_letters); break;
case KEY_9: RenderCustom(tex_9, &rect_offset_letters); break;
case KEY_APOSTROPHE: RenderCustom(tex_APOSTROPHE,&rect_offset_letters); break;
case KEY_BACKSLASH: RenderCustom(tex_BACKSLASH, &rect_offset_letters); break;
case KEY_BACKSPACE:
update_window_width(FAC_BACKSPACE);
RenderCustom(tex_blank, &rect_blank);
RenderLetters(tex_generalP, &rect_backspace, tex_BACKSPACE, &rect_offset_backspace); break;
case KEY_COMMA: RenderCustom(tex_COMMA, &rect_letters); break;
case KEY_DELETE: RenderCustom(tex_DELETE, &rect_letters); break;
case KEY_DOT: RenderCustom(tex_DOT, &rect_letters); break;
case KEY_ENTER:
update_window_width(FAC_ENTER);
RenderCustom(tex_blank, &rect_blank);
RenderLetters(tex_generalP, &rect_enter, tex_ENTER, &rect_offset_enter); break;
case KEY_EQUAL: RenderCustom(tex_EQUAL, &rect_offset_letters); break;
case KEY_ESC: RenderCustom(tex_ESC, &rect_offset_letters); break;
case KEY_GRAVE: RenderCustom(tex_GRAVE, &rect_offset_letters); break;
case KEY_LEFTBRACE: RenderCustom(tex_LEFTBRACE, &rect_offset_letters); break;
case KEY_MINUS: RenderCustom(tex_MINUS, &rect_offset_letters); break;
case KEY_RIGHTBRACE: RenderCustom(tex_RIGHTBRACE,&rect_offset_letters); break;
case KEY_SEMICOLON: RenderCustom(tex_SEMICOLON, &rect_offset_letters); break;
case KEY_SLASH: RenderCustom(tex_SLASH, &rect_offset_letters); break;
case KEY_SPACE:
update_window_width(FAC_SPACE);
RenderCustom(tex_blank, &rect_blank);
RenderLetters(tex_generalP, &rect_space, tex_SPACE, &rect_offset_space); break;
case KEY_TAB:
update_window_width(FAC_TAB);
RenderCustom(tex_blank, &rect_blank);
RenderLetters(tex_generalP, &rect_tab, tex_TAB, &rect_offset_tab); break;
default: cout << "code:" << kbutton << endl;
} // switch (global_keyboard[fn].code)
} // default: if (SHOW_LETTERS)
} // switch (kbutton)
} // the button state
else
{
switch (kbutton)
{
case KEY_RIGHTCTRL:
case KEY_LEFTCTRL:
if (SHOW_CTRL)
{
PRESSED_BUTTONS[0] = false;
if (TRANSPARENT_MODE)
{
window_width = ntrues_keyboard() * BUTTON_WIDTH + check_mouse_clicked() * MOUSE_WIDTH;
SDL_SetWindowSize(window, window_width, window_hieght);
}
else RenderLetters(tex_general, &rect_ctrl, tex_ctrl, &rect_offset_ctrl);
}
break;
case KEY_RIGHTSHIFT:
case KEY_LEFTSHIFT:
if (SHOW_SHIFT)
{
PRESSED_BUTTONS[1] = false;
if (TRANSPARENT_MODE)
{
window_width = ntrues_keyboard() * BUTTON_WIDTH + check_mouse_clicked() * MOUSE_WIDTH;
SDL_SetWindowSize(window, window_width, window_hieght);
}
else RenderLetters(tex_general, &rect_shift, tex_shift, &rect_offset_shift);
}
break;
case KEY_LEFTMETA:
if (SHOW_SUPER)
{
PRESSED_BUTTONS[2] = false;
if (TRANSPARENT_MODE)
{
window_width = ntrues_keyboard() * BUTTON_WIDTH + check_mouse_clicked() * MOUSE_WIDTH;
SDL_SetWindowSize(window, window_width, window_hieght);
}
else RenderLetters(tex_general, &rect_super, tex_super, &rect_offset_super);
}
break;
case KEY_RIGHTALT:
case KEY_LEFTALT:
if (SHOW_ALT)
{
PRESSED_BUTTONS[3] = false;
if (TRANSPARENT_MODE)
{
window_width = ntrues_keyboard() * BUTTON_WIDTH + check_mouse_clicked() * MOUSE_WIDTH;
SDL_SetWindowSize(window, window_width, window_hieght);
}
else RenderLetters(tex_general, &rect_alt, tex_alt, &rect_offset_alt);
}
break;
default:
if (SHOW_LETTERS)
{
PRESSED_BUTTONS[4] = false;
if(TRANSPARENT_MODE)
{
window_width = ntrues_keyboard() * BUTTON_WIDTH + check_mouse_clicked() * MOUSE_WIDTH;
SDL_SetWindowSize(window, window_width, window_hieght);
}
else RenderCustom(tex_general, &rect_letters);
// returning the size of the window for the large keys
if (kbutton == KEY_BACKSPACE ||
kbutton == KEY_ENTER ||
kbutton == KEY_SPACE ||
kbutton == KEY_TAB)
update_window_width(1);
}
} // switch (kbutton)
} // button state
} // if (ltype == LIBINPUT_EVENT_KEYBOARD_KEY)
if (ltype == LIBINPUT_EVENT_POINTER_BUTTON)
{
pevent = libinput_event_get_pointer_event(levent);
pbutton = libinput_event_pointer_get_button(pevent);
pstate = libinput_event_pointer_get_button_state(pevent);
//checking if any of the three buttons release(right,left,wheel)
if (pstate == LIBINPUT_BUTTON_STATE_RELEASED && pbutton > 271 && pbutton < 275)
{
if (TRANSPARENT_MODE)
{
if (pbutton == 272) PRESSED_MOUSE[0] = false;
if (pbutton == 273) PRESSED_MOUSE[1] = false;
if (pbutton == 274) PRESSED_MOUSE[2] = false;
window_width = ntrues_keyboard() * BUTTON_WIDTH + check_mouse_clicked() * MOUSE_WIDTH;
update_window_hieght();
SDL_SetWindowSize(window, window_width, window_hieght);
}
if (check_mouse_clicked() == 0) RenderCustom(tex_mouse, &rect_mouse);
}
if (pstate == LIBINPUT_BUTTON_STATE_PRESSED)
{
switch (pbutton)
{
case 272: // left click
if (TRANSPARENT_MODE)
{
PRESSED_MOUSE[0] = true;
rect_mouse.x = ntrues_keyboard() * BUTTON_WIDTH;
window_width = ntrues_keyboard() * BUTTON_WIDTH + check_mouse_clicked() * MOUSE_WIDTH;
update_window_hieght();
SDL_SetWindowSize(window, window_width, window_hieght);
if (ntrues_mouse() <= 1) RenderCustom(tex_mouse, &rect_mouse);
}
RenderCustom(tex_mouse_leftP, &rect_mouse);
break;
case 273: // right click
if (TRANSPARENT_MODE)
{
PRESSED_MOUSE[1] = true;
rect_mouse.x = ntrues_keyboard() * BUTTON_WIDTH;
window_width = ntrues_keyboard() * BUTTON_WIDTH + check_mouse_clicked() * MOUSE_WIDTH;
update_window_hieght();
SDL_SetWindowSize(window, window_width, window_hieght);
if (ntrues_mouse() <= 1) RenderCustom(tex_mouse, &rect_mouse);
}
RenderCustom(tex_mouse_rightP, &rect_mouse);
break;
case 274: // middle wheel
if (TRANSPARENT_MODE)
{
PRESSED_MOUSE[2] = true;
rect_mouse.x = ntrues_keyboard() * BUTTON_WIDTH;
window_width = ntrues_keyboard() * BUTTON_WIDTH + check_mouse_clicked() * MOUSE_WIDTH;
update_window_hieght();
SDL_SetWindowSize(window, window_width, window_hieght);
if (ntrues_mouse() <= 1) RenderCustom(tex_mouse, &rect_mouse);
}
RenderCustom(tex_mouse_wheelP, &rect_mouse);
break;
} // switch (pbutton)
} // if (pstate == LIBINPUT_BUTTON_STATE_PRESSED)
} // if (ltype == LIBINPUT_EVENT_POINTER_BUTTON)
if (ltype == LIBINPUT_EVENT_POINTER_SCROLL_WHEEL)
{
pevent = libinput_event_get_pointer_event(levent);
scroll_value = libinput_event_pointer_get_scroll_value(pevent, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
if (TRANSPARENT_MODE)
{
PRESSED_MOUSE[3] = true;
rect_mouse.x = ntrues_keyboard() * BUTTON_WIDTH;
window_width = ntrues_keyboard() * BUTTON_WIDTH + check_mouse_clicked() * MOUSE_WIDTH;
SDL_SetWindowSize(window, window_width, window_hieght);
SDL_ShowWindow(window);
}
// checking if scrolled up
if (scroll_value < 0)
RenderCustom(tex_mouse_wheelup, &rect_mouse);
else // scrolled down
RenderCustom(tex_mouse_wheeldown, &rect_mouse);
SDL_RenderPresent(renderer);
// to make the arrow show for a small period of time we should sleep
// then show the normal mouse image
// other wise the arrows will stay until a button is released
SDL_Delay(200);
RenderCustom(tex_mouse, &rect_mouse);
PRESSED_MOUSE[3] = false;
}
if (TRANSPARENT_MODE)
{
if (ntrues_keyboard() == 0 && check_mouse_clicked() == 0) SDL_HideWindow(window);
else SDL_ShowWindow(window);
}
libinput_event_destroy(levent);
}
if (NEED_REFRESH)
{
SDL_RenderPresent(renderer);
NEED_REFRESH = false;
}
while ( SDL_PollEvent( &sdl_input ) != 0 )
{
// checking if you want to close the app
if (sdl_input.type == SDL_QUIT) close_program =true;
if (sdl_input.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
{
SDL_SetRenderDrawColor(renderer, BG_COLOR_R, BG_COLOR_G, BG_COLOR_B, BG_COLOR_A);
SDL_RenderClear(renderer);
if (SHOW_CTRL)
{
if (PRESSED_BUTTONS[0])
RenderLetters(tex_generalP, &rect_ctrl, tex_ctrl, &rect_offset_ctrl);
else
RenderLetters(tex_general, &rect_ctrl, tex_ctrl, &rect_offset_ctrl);
}
if (SHOW_SHIFT)
{
if (PRESSED_BUTTONS[1])
RenderLetters(tex_generalP, &rect_shift, tex_shift, &rect_offset_shift);
else
RenderLetters(tex_general, &rect_shift, tex_shift, &rect_offset_shift);
}
if (SHOW_SUPER)
{
if (PRESSED_BUTTONS[2])
RenderLetters(tex_generalP, &rect_super, tex_super, &rect_offset_super);
else
RenderLetters(tex_general, &rect_super, tex_super, &rect_offset_super);
}