summaryrefslogtreecommitdiff
path: root/src/bin/engine_software_sdl.c
blob: a786d9f87ba307a66af1daedefae21716818694d (plain) (blame)
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
#include <assert.h>
#include "main.h"

#include <SDL/SDL.h>
#include <Evas_Engine_Buffer.h>

static void *
_expedite_sdl_switch_buffer(void *data, void *dest __UNUSED__)
{
   SDL_Flip(data);
   return ((SDL_Surface*)data)->pixels;
}

Eina_Bool
engine_software_sdl_args(const char *engine, int width, int height)
{
   int ok = 0;

   if (!strcmp(engine, "sdl")) ok = 1;
   if (!ok) return EINA_FALSE;

   if (ok == 1)
     {
        Evas_Engine_Info_Buffer *einfo;

        evas_output_method_set(evas, evas_render_method_lookup("buffer"));

        einfo = (Evas_Engine_Info_Buffer *) evas_engine_info_get(evas);
        if (einfo)
          {
             SDL_Init(SDL_INIT_NOPARACHUTE);

             if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
               {
                  printf("SDL_Init failed with %s", SDL_GetError());
                  SDL_Quit();
                  return EINA_FALSE;
               }

             einfo->info.depth_type = EVAS_ENGINE_BUFFER_DEPTH_RGB32;
             einfo->info.switch_data = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
             if (!einfo->info.switch_data)
               {
                  printf("SDL_SetVideoMode failed !");
                  return EINA_FALSE;
               }

             SDL_SetAlpha(einfo->info.switch_data, SDL_SRCALPHA, 0);
             SDL_FillRect(einfo->info.switch_data, NULL, 0);

             einfo->info.dest_buffer = ((SDL_Surface*)einfo->info.switch_data)->pixels;
             einfo->info.dest_buffer_row_bytes = width * sizeof (int);
             einfo->info.use_color_key = 0;
             einfo->info.alpha_threshold = 0;
             einfo->info.func.new_update_region = NULL;
             einfo->info.func.free_update_region = NULL;
             einfo->info.func.switch_buffer = _expedite_sdl_switch_buffer;
             if (!evas_engine_info_set(evas, (Evas_Engine_Info *) einfo))
               {
                  printf("evas_engine_info_set() for engine 'sdl' with 'buffer' backend failed.");
                  return EINA_FALSE;
               }
          }
     }

   return EINA_TRUE;
}

void
engine_software_sdl_loop(void)
{
   SDL_Event event;
   int rmethod;

   while(SDL_PollEvent(&event))
     {
        switch(event.type)
          {
          case SDL_MOUSEMOTION:
             evas_event_feed_mouse_move(evas, event.motion.x, event.motion.y, 0, NULL);
             break;
          case SDL_MOUSEBUTTONDOWN:
             evas_event_feed_mouse_move(evas, event.button.x, event.button.y, 0, NULL);
             evas_event_feed_mouse_down(evas, event.button.button, EVAS_BUTTON_NONE, 0, NULL);
             break;
          case SDL_MOUSEBUTTONUP:
             evas_event_feed_mouse_move(evas, event.button.x, event.button.y, 0, NULL);
             evas_event_feed_mouse_up(evas, event.button.button, EVAS_BUTTON_NONE, 0, NULL);
             break;
          case SDL_VIDEORESIZE:
             rmethod = evas_output_method_get(evas);
             if (rmethod == evas_render_method_lookup("buffer"))
               {
                  Evas_Engine_Info_Buffer *einfo;

                  einfo = (Evas_Engine_Info_Buffer *) evas_engine_info_get(evas);
                  if (einfo)
                    {
                       einfo->info.depth_type = EVAS_ENGINE_BUFFER_DEPTH_RGB32;
                       einfo->info.switch_data = SDL_SetVideoMode(event.resize.w, event.resize.h, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
                       if (!einfo->info.switch_data)
                         {
                            return ;
                         }

                       SDL_SetAlpha(einfo->info.switch_data, SDL_SRCALPHA, 0);
                       SDL_FillRect(einfo->info.switch_data, NULL, 0);

                       einfo->info.dest_buffer = ((SDL_Surface*)einfo->info.switch_data)->pixels;
                       einfo->info.dest_buffer_row_bytes = event.resize.w * sizeof (int);
                       einfo->info.use_color_key = 0;
                       einfo->info.alpha_threshold = 0;
                       einfo->info.func.new_update_region = NULL;
                       einfo->info.func.free_update_region = NULL;
                       einfo->info.func.switch_buffer = _expedite_sdl_switch_buffer;
                       if (!evas_engine_info_set(evas, (Evas_Engine_Info *) einfo))
                         {
                            return ;
                         }
                    }
               }

             evas_output_viewport_set(evas, 0, 0,
                                      event.resize.h, event.resize.w);
             evas_output_size_set(evas, event.resize.h, event.resize.w);
             evas_output_size_get(evas, &win_w, &win_h);
             break;
          case SDL_VIDEOEXPOSE:
             evas_output_size_get(evas, &win_w, &win_h);
             evas_damage_rectangle_add(evas, 0, 0, win_w, win_h);
             break;
          case SDL_KEYDOWN:
             switch (event.key.keysym.sym)
               {
               case SDLK_LSHIFT:
               case SDLK_RSHIFT:
                  evas_key_modifier_on(evas, "Shift");
                  break;
               case SDLK_RCTRL:
               case SDLK_LCTRL:
                  evas_key_modifier_on(evas, "Control");
                  break;
               case SDLK_MENU:
               case SDLK_RALT:
               case SDLK_LALT:
                  evas_key_modifier_on(evas, "Alt");
                  break;
               case SDLK_LSUPER:
               case SDLK_RSUPER:
                  evas_key_modifier_on(evas, "Super");
                  break;
               case SDLK_CAPSLOCK:
                  if (evas_key_lock_is_set(evas_key_lock_get(evas), "Caps_Lock"))
                    evas_key_lock_off(evas, "Caps_Lock");
                  else
                    evas_key_lock_on(evas, "Caps_Lock");
                  break;
               case SDLK_NUMLOCK:
                  if (evas_key_lock_is_set(evas_key_lock_get(evas), "Num_Lock"))
                    evas_key_lock_off(evas, "Num_Lock");
                  else
                    evas_key_lock_on(evas, "Num_Lock");
                  break;
               case SDLK_SCROLLOCK:
                  if (evas_key_lock_is_set(evas_key_lock_get(evas), "Scroll_Lock"))
                    evas_key_lock_off(evas, "Scroll_Lock");
                  else
                    evas_key_lock_on(evas, "Scroll_Lock");
                  break;
               case SDLK_s:
               case SDLK_ESCAPE:
                  evas_event_feed_key_down(evas, "Escape", "Escape", NULL, NULL, 0, NULL);
                  break;
               case SDLK_KP_ENTER:
               case SDLK_RETURN:
                  evas_event_feed_key_down(evas, "Return", "Return", NULL, NULL, 0, NULL);
                  break;
               case SDLK_LEFT: evas_event_feed_key_down(evas, "Left", "Left", NULL, NULL, 0, NULL); break;
               case SDLK_RIGHT: evas_event_feed_key_down(evas, "Right", "Right", NULL, NULL, 0, NULL); break;
               case SDLK_q:
                  if (evas_key_lock_is_set(evas_key_lock_get(evas), "Caps_Lock"))
                    evas_event_feed_key_down(evas, "Q", "Q", NULL, NULL, 0, NULL);
                  else
                    evas_event_feed_key_down(evas, "q", "q", NULL, NULL, 0, NULL);
                  break;
	       default:
		  break;
               }
             break;
          case SDL_KEYUP:
             switch (event.key.keysym.sym)
               {
               case SDLK_LSHIFT:
               case SDLK_RSHIFT:
                  evas_key_modifier_off(evas, "Shift");
                  break;
               case SDLK_RCTRL:
               case SDLK_LCTRL:
                  evas_key_modifier_off(evas, "Control");
                  break;
               case SDLK_MENU:
               case SDLK_RALT:
               case SDLK_LALT:
                  evas_key_modifier_off(evas, "Alt");
                  break;
               case SDLK_LSUPER:
               case SDLK_RSUPER:
                  evas_key_modifier_off(evas, "Super");
                  break;
               case SDLK_ESCAPE: evas_event_feed_key_up(evas, "Escape", "Escape", NULL, NULL, 0, NULL); break;
               case SDLK_RETURN: evas_event_feed_key_up(evas, "Return", "Return", NULL, NULL, 0, NULL); break;
               case SDLK_LEFT: evas_event_feed_key_up(evas, "Left", "Left", NULL, NULL, 0, NULL); break;
               case SDLK_RIGHT: evas_event_feed_key_up(evas, "Right", "Right", NULL, NULL, 0, NULL); break;
               case SDLK_q:
                  if (evas_key_lock_is_set(evas_key_lock_get(evas), "Caps_Lock"))
                    evas_event_feed_key_up(evas, "Q", "Q", NULL, NULL, 0, NULL);
                  else
                    evas_event_feed_key_up(evas, "q", "q", NULL, NULL, 0, NULL);
                  break;
	       default:
		  break;
               }
             break;
          case SDL_QUIT:
          case SDL_SYSWMEVENT:
          case SDL_USEREVENT:
          case SDL_ACTIVEEVENT:
          case SDL_JOYAXISMOTION:
          case SDL_JOYBALLMOTION:
          case SDL_JOYHATMOTION:
          case SDL_JOYBUTTONDOWN:
          case SDL_JOYBUTTONUP:
          default:
             break;
          }
     }
}

void
engine_software_sdl_shutdown(void)
{
}