summaryrefslogtreecommitdiff
path: root/gui.c
blob: 39e2b9a5af2139128dbbce17883ecdb0230689e7 (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
/*
 * LimoX - The Linux on mobile XMPP chat client
 * Copyright (C) 2022  xengineering
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#include <SDL2/SDL.h>
#include <stdbool.h>
#include <unistd.h>

#include "xmpp.h"


void gui_run(void) {

	bool quit = false;
	SDL_Event event;
	SDL_Window* window;
	SDL_Renderer* renderer;
	SDL_Texture* texture;
	uint32_t* pixels;
	int xmpp_fd = -1;

	// init SDL2 and create window
	SDL_Init(SDL_INIT_VIDEO);
	window = SDL_CreateWindow("LimoX",
	    SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480,
	    SDL_WINDOW_RESIZABLE
	);

	// output video driver to stderr
	fprintf(stderr, "SDL2 video driver: %s\n", SDL_GetCurrentVideoDriver());

	// create and initialize renderer, texture and pixel buffer
	renderer = SDL_CreateRenderer(window, -1, 0);
	texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
	                            SDL_TEXTUREACCESS_STATIC, 640, 480);
	pixels = malloc(sizeof(uint32_t) * 640 * 480);
	memset(pixels, 255, 640 * 480 * sizeof(uint32_t));

	// handle failed window creation
	if (window == NULL) {
		fprintf(stderr, "Failed to create SDL2 window!\n");
		return;
	} else {
		while (!quit) {
			SDL_UpdateTexture(texture, NULL, pixels, 640 * sizeof(uint32_t));
			SDL_WaitEvent(&event);
			switch (event.type) {
			case SDL_QUIT:
				quit = true;
				break;
			case SDL_MOUSEBUTTONDOWN:
				if (xmpp_fd == -1) {
					xmpp_fd = xmpp_connect();
				} else {
					close(xmpp_fd);
					xmpp_fd = -1;
					printf("Closed XMPP connection.\n");
				}
				break;
			}
			SDL_RenderClear(renderer);
			SDL_RenderCopy(renderer, texture, NULL, NULL);
			SDL_RenderPresent(renderer);
		}
		SDL_DestroyWindow(window);
	}

	free(pixels);

	// TODO this seems to end in memory access errors but ... why?
	//SDL_DestroyTexture(texture);
	//SDL_DestroyRenderer(renderer);

	SDL_Quit();

}