summaryrefslogtreecommitdiff
path: root/src/screen.c
blob: d695ee782ccabf776220b0cb81316fe7ca024832 (plain)
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
/***
 * Screen - The actual point of this godforsaken project
 * 
 * This file's goal is to "render" a slot machine that the soon-to-be unlucky victim must play to get their
 * files back. To do this, I am using curses and probably the menu library to create the TUI. Once I figure
 * out how to actually do this, everything should work hopefully maybe
 * 
Example window layout

======================================================== (Window 1)         (One line long, top of screen)
VX-GAMBLEGROUND: <Random phrase from list of phrases>
======================================================== (End Window 1)     (Takes up space between top & bottom windows)
======================================================== (Window 2)

SLOTS HERE

======================================================== (End Window 2)
======================================================== (Window 3)         (One line long, bottom of screen)
> Spin              > Buy Spins             > Quit
======================================================== (End Window 3)
 */

#define _GNU_SOURCE

#include "screen.h"
#include <curses.h>
#include <locale.h>
#include <signal.h>
#include <sodium.h>
#include <unistd.h>
#include <errno.h>
#include <error.h>
#include <menu.h>
#include <time.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static const char *phrases[] = {
    // By @syxhe on telegram 
    "WE CLOWN IN THIS MF, TAKE YO SENSITIVE ASS BACK TO @GENTOOMEMES",
    "R.I.P VxHeaven",
    "tmp(2) nuked by Smelly",
    "99% of Ransomware Operators quit before compromising a bank",
    "Equation Group wuz here",
    "Lazarus wuz here",
    "LockBit wuz here",
    "Sponsored by Equation Group",
    "Sponsored by Lazarus",
    "Sponsored by LockBit",
    "Free my boy Ross Ulbricht he did nothing wrong",
    "Stay off the dark web, kids",
    "FREE BITCOIN JUST 3 SPINS AWAY",
    "We all glow in the dark",
    "Shoutouts to Simpleflips",
    "Shoutouts to BugHunter",
    "Shoutouts to EyeDeeKay",
    ":beecat:",
    ":3",
    "You think Jack Rhysider will interview me now?",
    "Check out \"Darknet Diaries\"",

    // by @danielsprofile on telegram
    "Daniel Spears loves femboys",
    "LizardSquad > Razer",
    "Sponsored by Major League Gaming",
    "Sponsored by LemonParty.org",
    "Sponsored by FTX",
    "RIP Harambe",
    "Shoutout Elliot Alderson",
    "Ted Kaczynski was right",
    "The FBI watches me jerk off to MILFs lol",
    "robux generator free online 2024 100% working undetected",
    "Doge wuz here",
    "We like Fortnite we like Fortnite",
    "We live in a society",
    "Hab you seen a alien pls?",
    "using a flipperzero makes me an APT, right?",
    "Subscribe to PewDiePie",
};

static const char *menu_choices[] = {
    "Spin",
    "Buy spins",
    "Quit"
};

static struct funcholder userfuncs[] = {
    {.callback = spin,  .type = FH_SPIN,    .params = {.spinp = {.bannerwin = NULL, .phrasenum = 0}}},
    {.callback = buy,   .type = FH_BUY,     .params = {.buyp =  {.numspins = 3, .price = 1}}},
    {.callback = quit,  .type = FH_QUIT,    .params = {.quitp = {.REPLACEME = NULL}}}
};

void intercleanup(int signum) {
    signum++; // So gcc doesn't yell at me

    endwin();
    error(0, 0, "[VX-GAMBLEGROUND] Caught interrupt");
    exit(0);

    return;
}

static const struct sigaction handlers[] = {
    {   // intercleanup
        .sa_flags = 0, 
        .sa_mask = SIGINT, 
        .sa_handler = intercleanup
    }
};

static float normalize(float value, float oldmin, float oldmax, float newmin, float newmax) {
    // x(normal) = (b - a) * ((x - x(min)) / (max x - min x)) + a, where [a, b] is the new range
    return (newmax - newmin) * ((value - oldmin) / (oldmax - oldmin)) + newmin;
}

// Find the result of x + offset and constrict it to the range [min, max]. x must be >= min
static int rangemod(int x, int offset, int min, int max) {
    return ((x - min + offset) % (max - min + 1)) + min;
}

static int init_rgb_color(int colornum, int red, int green, int blue) {
    int nred    = normalize(red,    RGB_MIN, RGB_MAX, CURSESCOLOR_MIN, CURSESCOLOR_MAX);
    int ngreen  = normalize(green,  RGB_MIN, RGB_MAX, CURSESCOLOR_MIN, CURSESCOLOR_MAX);
    int nblue   = normalize(blue,   RGB_MIN, RGB_MAX, CURSESCOLOR_MIN, CURSESCOLOR_MAX);

    return init_color(colornum, nred, ngreen, nblue);
}

// Initialize sodium, curses, and set the proper locale. Exits on error, returns 0 otherwise
static int doinit(void) {
    if(sodium_init() < 0)
        error(1, errno, "[VX-GAMBLEGROUND] Could not initialize sodium");

    if(setlocale(LC_ALL, "") == NULL)  // Clear out the locale so it doesn't default to ncurses' ISO-8859-1 setting
        error(1, errno, "[VX-GAMBLEGROUND] Could not set proper locale");

    if(initscr() == NULL)   // Initialize curses
        error(1, errno, "[VX-GAMBLEGROUND] Could not init standard screen");

    if(sigaction(SIGINT, &handlers[0], NULL) < 0)
        error(1, errno, "[VX-GAMBLEGROUND] Could not set up signal handler");

    return 0;
}

// Initialize colors & define a few custom colors & color pairs. Exits on error, returns 0 otherwise
static int docolors(void) {
    if(has_colors() && can_change_color()) {      // Init colors if available & create color pairings
        start_color();

        // Init colors here
        // TODO: FIGURE OUT WHY COLORS ARE SO FUCKEY
            /* The colors were being weird because I was being retarded and redefining already existing colors
            // to numbers well outside curses' 0-1000 range. Making an enum that redefined the already existing 
            // curses colors made this go away. Also VSCode's terminal theming is fucking with it as well, so
            // use a normal terminal for accurate colors */
        
        init_rgb_color(CC_RED,      255,    0,      0);
        init_rgb_color(CC_ORANGE,   255,    128,    0);
        init_rgb_color(CC_YELLOW,   255,    255,    0);
        init_rgb_color(CC_GREEN,    0,      255,    0);
        init_rgb_color(CC_BLUE,     0,      0,      255);
        init_rgb_color(CC_PURPLE,   128,    0,      255);
        init_rgb_color(CC_MAGENTA,  255,    0,      255);
        init_rgb_color(CC_WHITE,    255,    255,    255);
        init_rgb_color(CC_BLACK,    0,      0,      0);

        init_pair(CCP_TESTING, CC_RED, CC_WHITE);
        init_pair(CCP_BANNER, CC_WHITE, CURSES_BLUE);

        init_pair(CCP_RED,      CC_WHITE, CC_RED);
        init_pair(CCP_ORANGE,   CC_BLACK, CC_ORANGE);
        init_pair(CCP_YELLOW,   CC_BLACK, CC_YELLOW);
        init_pair(CCP_GREEN,    CC_BLACK, CC_GREEN);
        init_pair(CCP_BLUE,     CC_WHITE, CC_BLUE);
        init_pair(CCP_PURPLE,   CC_WHITE, CC_PURPLE);
        init_pair(CCP_MAGENTA,  CC_WHITE, CC_MAGENTA);
        init_pair(CCP_WHITE,    CC_BLACK, CC_WHITE);
    } else {
        endwin();
        error(1, ENOTSUP, "[VX-GAMBLEGROUND] Colors are not supported on your terminal");
    }

    return 0;
}

static WINDOW* create_banner(int col, int randomnum) {
    // Create the banner window
    WINDOW *phrase = newwin(1, col, 0, 0);
    if(phrase == NULL) {
        endwin();
        error(1, errno, "[VX-GAMBLEGROUND] Could not create banner window");
    }
    wbkgd(phrase, COLOR_PAIR(CCP_BANNER));
    mvwaddstr(phrase, 0, 1, "VX-GAMBLEGROUND: ");
    waddstr(phrase, phrases[randomnum]);
    wnoutrefresh(phrase);

    return phrase;
}

int main() {
    doinit();
    docolors();

    cbreak();               // Disables character buffering
    noecho();               // Disable echoing characters
    curs_set(0);            // Stop the cursor from blinking


    // Variable definitions

    ITEM *items[STATIC_ARRSIZE(menu_choices) + 1];                  // An array of ITEM pointers large enough to store all the menu items
    struct funcholder *p = NULL;                                    // Function pointer used for callbacks in the menu driver
    WINDOW *menuholder = NULL;                                      // The window for displaying the menu
    int row = -1, col = -1;                                         // The rows and columns of the main screen
    int randomnum;                                                  // A random number for getting the banner phrase
    MENU *menu;                                                     // The actual menu object
    int c;                                                          // Integer for storing keys from user input


    getmaxyx(stdscr, row, col);
    if(row < 0 || col < 0) {
        endwin();
        error(1, 0, "[VX-GAMBLEGROUND] Couldn't get max terminal size for some reason");
    }

    randomnum = randombytes_uniform(STATIC_ARRSIZE(phrases));
    if(randomnum < 0) {
        endwin();
        error(1, errno, "[VX-GAMBLEGROUND] Call to randombytes_uniform failed, can't get random phrase");
    }

    WINDOW *banner = create_banner(col, randomnum);


    // Set up menu items
    for(long unsigned int i = 0; i < STATIC_ARRSIZE(menu_choices); i++) {
        items[i] = new_item(menu_choices[i], NULL);
        set_item_userptr(items[i], (void*)&userfuncs[i]);
    }
    items[STATIC_ARRSIZE(menu_choices)] = NULL;

    menu = new_menu(items);
    if(menu == NULL) {
        endwin();
        error(1, errno, "Could not create menu");
    }

    // Set menu options & format
    set_menu_format(menu, 1, col);
    menu_opts_on(menu, O_ONEVALUE | O_IGNORECASE);
    menu_opts_off(menu, O_SHOWDESC | O_NONCYCLIC);

    menuholder = newwin(1, col, row - 1, 0);
    keypad(menuholder, TRUE);


    int holder1 = -1, holder2 = -1;
    getmaxyx(menuholder, holder1, holder2);
    if(holder1  < 0 || holder2 < 0) {
        endwin();
        error(1, errno, "[VX-GAMBLEGROUND] Could not get bounds for menu subwindow");
    }
    set_menu_win(menu, menuholder);
    set_menu_sub(menu, derwin(menuholder, holder1, holder2, 0, 0));

    set_menu_mark(menu, NULL);
    post_menu(menu);
    wbkgd(menuholder, COLOR_PAIR(CCP_BANNER));
    set_menu_back(menu, COLOR_PAIR(CCP_BANNER));
    wnoutrefresh(menuholder);

    WINDOW *slots = newwin(row - 2, col, 1, 0);
    if(slots == NULL) {
        endwin();
        error(1, errno, "[VX-GAMBLEGROUND] Could not create slots window");
    }

    const char *sampletext = "Lmao longer sample";
    mvwaddstr(slots, (row - 2) / 2, (col / 2) - (strlen(sampletext) / 2), sampletext);
    wnoutrefresh(slots);

    doupdate();

    userfuncs[0].params.spinp.bannerwin = banner;
    userfuncs[0].params.spinp.phrasenum = randomnum;
    userfuncs[0].params.spinp.menuholder = menuholder;
    userfuncs[0].params.spinp.menu = menu;

    // Get user input and deal with the menu
    while((c = wgetch(menuholder)) != KEY_F(4)) {
        switch(c) {
            case KEY_DOWN:
                menu_driver(menu, REQ_DOWN_ITEM);
                break;

            case KEY_UP:
                menu_driver(menu, REQ_UP_ITEM);
                break;

            case KEY_LEFT:
                menu_driver(menu, REQ_LEFT_ITEM);
                break;

            case KEY_RIGHT:
                menu_driver(menu, REQ_RIGHT_ITEM);
                break;

            case KEY_ENTER: case FUCKED_UP_ENTER: // Enter
                //wclear(menuholder);
                //mvwaddstr(menuholder, 0, 0, "This shit brokey lmao");

                p = (struct funcholder *)item_userptr(current_item(menu));
                switch(p->type) {
                    case FH_SPIN:
                        p->callback((void*)&p->params.spinp);
                        break;

                    case FH_BUY:
                        p->callback((void*)&p->params.buyp);
                        break;

                    case FH_QUIT:
                        p->callback((void*)&p->params.quitp);
                        break;

                    default:
                        endwin();
                        error(1, ENOTSUP, "SHIT BROKE");
                }

                break;
        }
    }

    // Clean up the menu
    unpost_menu(menu);
    free_menu(menu);
    for(long unsigned int i = 0; i < STATIC_ARRSIZE(menu_choices); i++)
        free_item(items[i]);
    //*/

    endwin(); // Clean up curses
    return 0;
}

// Spin the wheel
int spin(void *spinp) {
    struct spinp *p = (struct spinp *)spinp;

    struct timespec sleeper = {
        .tv_sec = 0,
        .tv_nsec = 1000000000 /* Nanoseconds in 1 second */ / NUMCOLORPAIRS
    };

    for(int color = CCP_RED, i = 0; i < (NUMCOLORPAIRS * NUMCOLORCYCLE); color = rangemod(color, 1, CCP_RED, CCP_WHITE), i++) {
        // Change the color to testing for the banner
        wbkgd(p->bannerwin, COLOR_PAIR(color));
        wnoutrefresh(p->bannerwin);

        // Change the color to testing for the menu
        wbkgd(p->menuholder, COLOR_PAIR(color));
        set_menu_back(p->menu, COLOR_PAIR(color));
        wnoutrefresh(p->menuholder);

        doupdate();
        nanosleep(&sleeper, NULL);
    }

    // Revert colors back to normal
    wbkgd(p->bannerwin, COLOR_PAIR(CCP_BANNER));
    wnoutrefresh(p->bannerwin);

    wbkgd(p->menuholder, COLOR_PAIR(CCP_BANNER));
    set_menu_back(p->menu, COLOR_PAIR(CCP_BANNER));
    wnoutrefresh(p->menuholder);

    doupdate();

    return 0;
}

// Increase number of spins
int buy(void *buyp) {
    struct buyp *p = (struct buyp*)buyp;
    p->numspins += 3;
    p->price *= 2;

    endwin();
    error(0, 0, "buy");
    exit(0);

    return 0;
}

// Quit out
int quit(void *quitp) {
    struct quitp *p = (struct quitp*)quitp;
    p->REPLACEME = "lol";

    endwin();
    error(0, 0, "quit");
    exit(0);

    return 0;
}