-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathh2o.cpp
More file actions
709 lines (604 loc) · 14.1 KB
/
h2o.cpp
File metadata and controls
709 lines (604 loc) · 14.1 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
// CONSOLE
#include <stdp>
#include <txtp>
#include <cmath>
#pragma comment( lib, "user32")
#define SET_TSTART 0
#define SET_TEND 1
#define SET_SHOTML 2
#define SET_H2OGOAL 3
#define SET_CONSUMED 4
#define TIME(h, m, s) (((int)h << 16) | ((int)m << 8) | (int)s)
UINT_PTR timer;
FLASHWINFO flash_inf;
HANDLE mutex_lock;
char h_start = 5;
char m_start = 0;
char h_end = 20;
char m_end = 0;
int shot_ml = 80;
int h2o_goal = 2000;
int start_mins, end_mins, end_mins_grace; // Total minutes in start/end times
bool hybernation;
bool end_of_time;
bool dclick_timeout;
int h2o_consumed;
int shots_need;
int int_min, int_sec; // Interval
int min, sec; // Current Time
int decTimes(int start, int end); // OUT: raw seconds IN: TIME MACRO
void recalcInterval();
void flash(bool on_off);
void prnt(const char* t);
void pi(long long int i);
void pshots();
void pbar();
void scurt(); // Set Cursor in the position to print time
void scurtm(); // Set Cursor in the position to print negative time
void savest(); // Save State
void readst(); // Read State
void hideCursor();
void __declspec(nothrow) tproc(HWND p1, UINT p2, UINT_PTR p3, DWORD p4);
DWORD __declspec(nothrow) inputThread(LPVOID lp);
DWORD __declspec(nothrow) dclickTmThread(LPVOID lp);
int wmain()
{
mutex_lock = CreateMutex(NULL, FALSE, NULL);
COORD buf_s;
buf_s.X = 79;
buf_s.Y = 20;
SMALL_RECT wnd_pos;
wnd_pos.Top = 0;
wnd_pos.Left = 0;
wnd_pos.Bottom = 19;
wnd_pos.Right = 78;
HANDLE oh = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleWindowInfo(oh, TRUE, &wnd_pos);
SetConsoleScreenBufferSize(oh, buf_s);
SetConsoleTitle(L"H20");
hideCursor();
HWND wnd = GetConsoleWindow();
flash_inf.cbSize = sizeof(FLASHWINFO);
flash_inf.hwnd = wnd;
flash_inf.uCount = 0;
flash_inf.dwTimeout = 500;
readst();
recalcInterval();
min = int_min;
sec = int_sec;
if(h2o_consumed < h2o_goal)
{
shots_need = 1;
}
SYSTEMTIME lt;
GetLocalTime(<);
int cur_mins = lt.wHour * 60 + lt.wMinute;
start_mins = h_start * 60 + m_start;
end_mins = h_end * 60 + m_end;
end_mins_grace = end_mins + 10;
if(cur_mins >= end_mins || cur_mins < start_mins)
{
hybernation = true;
end_of_time = true;
}
else
{
flash(1);
}
timer = SetTimer(NULL, NULL, 1000, tproc);
pbar();
CreateThread(NULL, 0, inputThread, NULL, 0, NULL);
MSG msg;
while(GetMessage(&msg, NULL, 0, 0) != 0)
{
DispatchMessage(&msg);
}
KillTimer(NULL, timer);
return 0;
}
int decTimes(int start, int end)
{
char h, m, s;
char hs = char(start >> 16);
char ms = char((start & 0xFF00) >> 8);
char ss = char(start & 0xFF);
char he = char(end >> 16);
char me = char((end & 0xFF00) >> 8);
char se = char(end & 0xFF);
if(he <= hs)
{
h = char(24 - hs + he);
}
else
{
h = char(he - hs);
}
m = char(me - ms);
s = char(se - ss);
if(s < 0)
{
--m;
s = char(60 + s);
}
if(m < 0)
{
--h;
m += 60;
}
else if(h == 24)
{
h = 0;
}
return h * 3600 + m * 60 + s;
}
void recalcInterval()
{
if(end_of_time)
{
return;
}
SYSTEMTIME lt;
GetLocalTime(<);
int itot = decTimes(TIME(h_start, m_start, 0), TIME(h_end, m_end, 0));
int ielasp = decTimes(TIME(h_start, m_start, 0), TIME(lt.wHour, lt.wMinute, lt.wSecond));
int interval = (int)round(float(itot-ielasp)/floor(float(h2o_goal - h2o_consumed)/(float)shot_ml - 1.0f));
interval = interval <= 0 ? 1 : interval;
int_min = interval/60;
int_sec = interval % 60;
/* INTERVAL RECALC DISPLAY
HANDLE oh = GetStdHandle(STD_OUTPUT_HANDLE);
COORD xy;
static short a = 4;
static short b = 27;
xy.X = b;
xy.Y = a++;
if(a >= 18)
{
a = 4;
b += 6;
xy.X = b;
xy.Y = a;
}
SetConsoleCursorPosition(oh, xy);
pi(int_min);
prnt(":");
pi(int_sec);
prnt(" "); */
}
void flash(bool on_off)
{
if(hybernation)
{
return;
}
if(on_off)
{
flash_inf.dwFlags = FLASHW_TRAY | FLASHW_TIMER;
FlashWindowEx(&flash_inf);
}
else
{
flash_inf.dwFlags = 0;
FlashWindowEx(&flash_inf);
}
}
void prnt(const char* t)
{
long unsigned int wrt = 0;
static HANDLE stdo = GetStdHandle(STD_OUTPUT_HANDLE);
WriteConsoleA(stdo, t, (DWORD)strlen(t), &wrt, NULL);
}
void pi(long long int i)
{
p|i;
}
void pshots()
{
COORD shotz;
shotz.X = 29;
shotz.Y = 2;
static HANDLE oh = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(oh, shotz);
pi(shots_need);
prnt(" ");
}
void pbar()
{
static HANDLE oh = GetStdHandle(STD_OUTPUT_HANDLE);
COORD comp;
comp.X = 0;
comp.Y = 0;
SetConsoleCursorPosition(oh, comp);
float pctg = (float)h2o_consumed/(float)h2o_goal * 60.0f;
p|"▐";
for(int j = 0; j < 60; ++j)
{
if(j < (int)pctg)
{
p|BD|"█"|D;
}
else
{
p|"░";
}
}
p|"▌ ";
p|(int)(pctg/60.0f * 100.0f)|"% "|h2o_consumed|'/'|h2o_goal;
prnt(" ");
scurt();
if(hybernation)
{
p|"zZ:zZ";
}
else
{
p|SP(2, '0')|int_min|DP|':'|SP(2, '0')|int_sec|DP;
}
pshots();
}
void scurt()
{
static COORD xy = { 27 , 1 };
static HANDLE oh = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(oh, xy);
}
void scurtm()
{
static COORD xy = { 26 , 1 };
static HANDLE oh = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(oh, xy);
}
void savest()
{
// Create or open File or Device =================================================================
HANDLE fh = CreateFile(
L"h2o.db", // [I] Name of the file or device to create/open
GENERIC_WRITE, // [I] Requested access GENERIC_READ|GENERIC_WRITE|0
FILE_SHARE_READ, // [I] Sharing mode FILE_SHARE_READ|WRITE|DELETE|0
NULL, // [I|O] SECURITY_ATTRIBUTES for file, handle inheritability
OPEN_ALWAYS, // [I] Action to take if file/device exist or not
FILE_FLAG_SEQUENTIAL_SCAN, // [I] Attributes and flags for file/device
NULL); // [I|O] Handle to template file to copy attributes from
// ===============================================================================================
if(fh == INVALID_HANDLE_VALUE)
{
return;
}
txt data;
data += i2t((int)h_start) + ':' + i2t(m_start) + '\n';
data += i2t((int)h_end) + ':' + i2t(m_end) + '\n';
data += i2t(shot_ml) + '\n';
data += i2t(h2o_goal) + '\n';
data += i2t(h2o_consumed) + '\n';
DWORD bw;
WriteFile(fh, data, (DWORD)~data, &bw, NULL);
CloseHandle(fh);
}
void readst()
{
// Create or open File or Device =================================================================
HANDLE fh = CreateFile(
L"h2o.db", // [I] Name of the file or device to create/open
GENERIC_READ, // [I] Requested access GENERIC_READ|GENERIC_WRITE|0
FILE_SHARE_READ, // [I] Sharing mode FILE_SHARE_READ|WRITE|DELETE|0
NULL, // [I|O] SECURITY_ATTRIBUTES for file, handle inheritability
OPEN_EXISTING, // [I] Action to take if file/device exist or not
FILE_FLAG_SEQUENTIAL_SCAN, // [I] Attributes and flags for file/device
NULL); // [I|O] Handle to template file to copy attributes from
// ===============================================================================================
if(fh == INVALID_HANDLE_VALUE)
{
return;
}
txt buff = 0x100;
ReadFile(fh, buff, (DWORD)!buff, *buff, NULL);
int setting = 0;
txt it;
for(ui64 i = 0; i < ~buff; ++i)
{
if(buff[i] == ':')
{
if(setting == SET_TSTART)
{
h_start = (char)t2i(it);
}
else
{
h_end = (char)t2i(it);
}
it = 0;
continue;
}
if(buff[i] == '\n')
{
if(setting == SET_TSTART)
{
m_start = (char)t2i(it);
}
else if(setting == SET_TEND)
{
m_end = (char)t2i(it);
}
else if(setting == SET_SHOTML)
{
shot_ml = (int)t2i(it);
}
else if(setting == SET_H2OGOAL)
{
h2o_goal = (int)t2i(it);
}
else if(setting == SET_CONSUMED)
{
h2o_consumed = (int)t2i(it);
}
it = 0;
++setting;
continue;
}
it += buff[i];
}
CloseHandle(fh);
}
void hideCursor()
{
p|DC;
}
#pragma warning( suppress : 4100 )
void __declspec(nothrow) tproc(HWND p1, UINT p2, UINT_PTR p3, DWORD p4)
{
hideCursor();
SetTimer(NULL, timer, 1000, tproc);
// CRITICAL SECTION
WaitForSingleObject(mutex_lock, INFINITE);
--sec;
if(sec < 0)
{
sec = 59;
--min;
}
ReleaseMutex(mutex_lock);
// CRITICAL SECTION
static SYSTEMTIME lt;
GetLocalTime(<);
int cur_mins = lt.wHour * 60 + lt.wMinute;
if(lt.wHour == h_end && lt.wMinute == m_end)
{
end_of_time = true;
}
else if(cur_mins >= end_mins_grace)
{
flash(0);
hybernation = true;
end_of_time = true;
}
else if(end_of_time && cur_mins >= start_mins && cur_mins < end_mins)
{
hybernation = false;
end_of_time = false;
flash(1);
recalcInterval();
// CRITICAL SECTION
WaitForSingleObject(mutex_lock, INFINITE);
min = int_min;
sec = int_sec;
shots_need = 1;
h2o_consumed = 0;
ReleaseMutex(mutex_lock);
// CRITICAL SECTION
pbar();
return;
}
txtp tp;
if(hybernation)
{
static int cnt;
switch(cnt)
{
case 0:
tp|" ";
break;
case 1:
tp|"z";
min = 42;
break;
case 2:
tp|"zZ";
break;
case 3:
tp|"zZ:";
break;
case 4:
tp|"zZ:z";
break;
case 5:
tp|"zZ:zZ";
break;
default:
break;
}
cnt = ++cnt >= 6 ? 0 : cnt;
}
else
{
tp|SP(2, '0')|min|DP|':'|SP(2, '0')|sec|DP;
}
if(!min && !sec && !end_of_time)
{
if(h2o_consumed + shots_need * shot_ml < h2o_goal && shots_need <= 0)
{
// CRITICAL SECTION
WaitForSingleObject(mutex_lock, INFINITE);
++shots_need;
ReleaseMutex(mutex_lock);
// CRITICAL SECTION
}
// CRITICAL SECTION
WaitForSingleObject(mutex_lock, INFINITE);
min = int_min;
sec = int_sec;
ReleaseMutex(mutex_lock);
// CRITICAL SECTION
pshots();
if(shots_need > 0)
{
flash(1);
}
}
if(shots_need > 0)
{
static int throb = 0;
recalcInterval();
// CRITICAL SECTION
WaitForSingleObject(mutex_lock, INFINITE);
min = int_min;
sec = int_sec;
if(min < -9)
{
scurtm();
}
else
{
scurt();
}
if(++throb % 2 == 0)
{
p|R|SP(2, '0')|min|DP|':'|SP(2, '0')|sec|DP|D;
}
else
{
p|SP(2, '0')|min|DP|':'|SP(2, '0')|sec|DP;
}
ReleaseMutex(mutex_lock);
// CRITICAL SECTION
}
else
{
// CRITICAL SECTION
WaitForSingleObject(mutex_lock, INFINITE);
if(min < -9)
{
scurtm();
}
else
{
scurt();
}
p|!tp;
ReleaseMutex(mutex_lock);
// CRITICAL SECTION
}
hideCursor();
}
#pragma warning( suppress : 4100 )
DWORD __declspec(nothrow) inputThread(LPVOID lp)
{
static HANDLE ih = GetStdHandle(STD_INPUT_HANDLE);
static HANDLE oh = GetStdHandle(STD_OUTPUT_HANDLE);
HWND wnd = GetConsoleWindow();
COORD comp;
comp.X = 0;
comp.Y = 0;
SetConsoleMode(ih, ENABLE_EXTENDED_FLAGS | ENABLE_MOUSE_INPUT | ENABLE_PROCESSED_INPUT);
INPUT_RECORD ir[128];
DWORD nread;
while(1)
{
ReadConsoleInput(ih, ir, 128, &nread);
for(DWORD i = 0; i < nread; ++i)
{
if(ir[i].EventType == MOUSE_EVENT)
{
const MOUSE_EVENT_RECORD* m = &ir[i].Event.MouseEvent;
if(m->dwButtonState == 0x1 && !dclick_timeout)
{
// MULTI-CLICK PROTECTION
dclick_timeout = true;
CreateThread(NULL, 0, dclickTmThread, NULL, 0, NULL);
if(h2o_consumed < h2o_goal)
{
// CRITICAL SECTION
WaitForSingleObject(mutex_lock, INFINITE);
--shots_need;
h2o_consumed += shot_ml;
ReleaseMutex(mutex_lock);
// CRITICAL SECTION
if(shots_need < 0)
{
recalcInterval();
// CRITICAL SECTION
WaitForSingleObject(mutex_lock, INFINITE);
min = int_min;
sec = int_sec;
shots_need = 0;
scurt();
p|SP(2, '0')|min|DP|':'|SP(2, '0')|sec|DP;
ReleaseMutex(mutex_lock);
// CRITICAL SECTION
}
if(h2o_consumed >= h2o_goal)
{
h2o_consumed = h2o_goal;
}
if(shots_need <= 0)
{
flash(0);
}
float pctg = (float)h2o_consumed/(float)h2o_goal * 60.0f;
// CRITICAL SECTION
WaitForSingleObject(mutex_lock, INFINITE);
SetConsoleCursorPosition(oh, comp);
p|"▐";
for(int j = 0; j < 60; ++j)
{
if(j < (int)pctg)
{
p|BD|"█"|D;
}
else
{
p|"░";
}
}
p|"▌ ";
p|(int)(pctg/60.0f * 100.0f)|"% "|h2o_consumed|'/'|h2o_goal;
pshots();
ReleaseMutex(mutex_lock);
// CRITICAL SECTION
savest();
}
}
else if(m->dwButtonState == 0x2)
{
hideCursor();
ShowWindow(wnd, SW_MINIMIZE);
hideCursor();
}
}
}
}
p|EC;
return 0;
}
#pragma warning( suppress : 4100 )
DWORD __declspec(nothrow) dclickTmThread(LPVOID lp)
{
Sleep(500);
dclick_timeout = false;
return 0;
}
// IMPROVEMENTS DONE:
// UNICODE PROGRESS BAR
// TEST IF NOT MISSING 1 SECOND BETWEEEN PERIODZ OR MB OVERCOUNTIN 1 SEC
// MB RECALCULATE EVERY 1 MIN PERIOD IF FAILED TO COSUME
// IMPORTANT! RECAL INTERVAL ALSO WHEN CONSUMIN MORE H2O (I.E. -1/-2 ETC)
// SAVE STATE TO FILE RESTORE ON STARTUP
// OUT OF COMPUTA HYBERNATONE FAIL TO STOP HYBERNATION MODE
/* TODO NOT IMPORTANT Top half block ASCII CODE 223
▄▄ ▄▄ ▄▄ ▄▄
S12:33 E20:00
▀▀ ▀▀ ▀▀ ▀▀
Bottom half block ASCII CODE 220 ---> Change Into Full Block When hovered/pressed change to half thick full */
// REPOSITION WINDOW TO FIT TIGHTLY TO THE TASKBAR BY GETTIN IT'S WIDTH AND MOVING WINDOW!
// WHEN LAUNCHING FROM THE NPP IDE MSVC_BUILD.CMD WINDOW SIZE IS PADDED WITH 2 ADDITIONAL CELLS
// NOT POSSIBLE:
// TEST ICON AFTER FELOAD IF STILL REKT -> FIX -> ALL WINDOWS CONSOLE SLAVE WINDOWS ARE DPI UNAWARE!