1 module unde.tick;
2 
3 import unde.global_state;
4 import unde.lib;
5 
6 static import unde.file_manager.events;
7 static import unde.viewers.image_viewer.events;
8 static import unde.viewers.text_viewer.events;
9 static import unde.command_line.events;
10 static import unde.keybar.events;
11 import unde.command_line.events: CommandLineEventHandlerResult;
12 import unde.keybar.lib;
13 
14 import derelict.sdl2.sdl;
15 import berkeleydb.all;
16 
17 import std.stdio;
18 
19 enum unDE_MouseButtons {
20     Left = 0x01,
21     Middle = 0x02,
22     Right = 0x04,
23 }
24 
25 void make_tick(GlobalState gs)
26 {
27     
28 }
29 
30 RectSize getRectSize(GlobalState gs)
31 {
32     RectSize rectsize;
33     Dbt key, data;
34     key = gs.current_path;
35     auto res = gs.db_map.get(null, &key, &data);
36     if (res == 0)
37     {
38         rectsize = data.to!(RectSize);
39     }
40     return rectsize;
41 }
42 
43 void change_current_dir(GlobalState gs, void delegate (ref RectSize rectsize) change_rectsize)
44 {
45     Dbt key, data;
46     key = gs.current_path;
47     auto res = gs.db_map.get(null, &key, &data);
48     if (res == 0)
49     {
50         RectSize rectsize;
51         rectsize = data.to!(RectSize);
52 
53         change_rectsize(rectsize);
54 
55         data = rectsize;
56         res = gs.db_map.put(null, &key, &data);
57         if (res != 0)
58             throw new Exception("Path info to map-db not written");
59     }
60     gs.dirty = true;
61 }
62 
63 void
64 process_event(GlobalState gs, SDL_Event event)
65 {
66     auto result = unde.command_line.events.process_event(gs, event);
67     if (result == CommandLineEventHandlerResult.Pass)
68     {
69         foreach (uipage; gs.uipages)
70         {
71             if (uipage.show)
72             {
73                 uipage.process_event(gs, event);
74                 return;
75             }
76         }
77 
78         final switch (gs.state)
79         {
80             case State.FileManager:
81                 unde.file_manager.events.process_event(gs, event);
82                 break;
83             case State.ImageViewer:
84                 unde.viewers.image_viewer.events.process_event(gs, event);
85                 break;
86             case State.TextViewer:
87                 unde.viewers.text_viewer.events.process_event(gs, event);
88                 break;
89         }
90     }
91 }
92 
93 void process_events(GlobalState gs)
94 {
95     /* Our SDL event placeholder. */
96     SDL_Event event;
97 
98     /* Grab all the events off the queue. */
99     while( SDL_PollEvent( &event ) )
100     {
101         KeyHandler *keyhandler;
102         if (event.type == SDL_KEYDOWN)
103         {
104             keyhandler = event.key.keysym.scancode in gs.keybar.handlers_down;
105             if (keyhandler)
106             {
107                 keyhandler.handler(gs);
108             }
109         }
110         else if (event.type == SDL_KEYUP)
111         {
112             if (event.key.keysym.scancode == gs.key_press &&
113                     SDL_GetTicks() - gs.last_key_press < DOUBLE_DELAY)
114             {
115                 keyhandler = event.key.keysym.scancode in gs.keybar.handlers_double;
116             }
117             if (keyhandler)
118                 keyhandler.handler(gs);
119             KeyHandler *dblkeyhandler = keyhandler;
120             keyhandler = event.key.keysym.scancode in gs.keybar.handlers;
121             if (keyhandler)
122                 keyhandler.handler(gs);
123             else keyhandler = dblkeyhandler;
124             gs.last_key_press = SDL_GetTicks();
125             gs.key_press = event.key.keysym.scancode;
126         }
127 
128         unde.keybar.events.process_event(gs, event, keyhandler);
129 
130         if (keyhandler is null)
131         {
132             process_event(gs, event);
133         }
134     }
135 }
136 
137 void
138 make_screenshot(GlobalState gs) { 
139     SDL_Surface *screenshot; 
140     screenshot = SDL_CreateRGBSurface(SDL_SWSURFACE,
141             gs.screen.w+32*6, 
142             gs.screen.h, 
143             32, 0x00FF0000, 0X0000FF00, 0X000000FF, 0XFF000000); 
144     SDL_RenderReadPixels(gs.renderer, null, SDL_PIXELFORMAT_ARGB8888, 
145             screenshot.pixels, screenshot.pitch);
146     SDL_SaveBMP(screenshot, "screenshot.bmp"); 
147     SDL_FreeSurface(screenshot); 
148 
149     //SDL_SaveBMP(gs.surface, "surface.bmp"); 
150 }
151